@UnstableApi
public final class VorbisUtil


Utility methods for parsing Vorbis streams.

Summary

Nested types

public final class VorbisUtil.CommentHeader

Vorbis comment header.

public final class VorbisUtil.Mode

Vorbis setup header modes.

public final class VorbisUtil.VorbisIdHeader

Vorbis identification header.

Public methods

static @Nullable int[]

Returns the mapping from VORBIS channel layout to the channel layout expected by Android, or null if the mapping is unchanged.

static int
iLog(int x)

Returns ilog(x), which is the index of the highest set bit in x.

static @Nullable Metadata
parseVorbisComments(List<String> vorbisComments)

Builds a Metadata instance from a list of Vorbis Comments.

static ImmutableList<byte[]>
parseVorbisCsdFromEsdsInitializationData(byte[] initializationData)

Returns codec-specific data for configuring a media codec for decoding Vorbis.

static VorbisUtil.CommentHeader

Reads a Vorbis comment header.

static VorbisUtil.CommentHeader
readVorbisCommentHeader(
    ParsableByteArray headerData,
    boolean hasMetadataHeader,
    boolean hasFramingBit
)

Reads a Vorbis comment header.

static VorbisUtil.VorbisIdHeader

Reads a Vorbis identification header from headerData.

static VorbisUtil.Mode[]
readVorbisModes(ParsableByteArray headerData, int channels)

This method reads the modes which are located at the very end of the Vorbis setup header.

static boolean
verifyVorbisHeaderCapturePattern(
    int headerType,
    ParsableByteArray header,
    boolean quiet
)

Verifies whether the next bytes in header are a Vorbis header of the given headerType.

Public methods

getVorbisToAndroidChannelLayoutMapping

public static @Nullable int[] getVorbisToAndroidChannelLayoutMapping(int channelCount)

Returns the mapping from VORBIS channel layout to the channel layout expected by Android, or null if the mapping is unchanged.

See https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-140001.2.3 and https://developer.android.com/reference/android/media/AudioFormat#channelMask.

iLog

public static int iLog(int x)

Returns ilog(x), which is the index of the highest set bit in x.

See the Vorbis spec

Parameters
int x

the value of which the ilog should be calculated.

Returns
int

ilog(x)

parseVorbisComments

public static @Nullable Metadata parseVorbisComments(List<String> vorbisComments)

Builds a Metadata instance from a list of Vorbis Comments.

METADATA_BLOCK_PICTURE comments will be transformed into PictureFrame entries. All others will be transformed into VorbisComment entries.

Parameters
List<String> vorbisComments

The raw input of comments, as a key-value pair KEY=VAL.

Returns
@Nullable Metadata

The fully parsed Metadata instance. Null if no vorbis comments could be parsed.

parseVorbisCsdFromEsdsInitializationData

public static ImmutableList<byte[]> parseVorbisCsdFromEsdsInitializationData(byte[] initializationData)

Returns codec-specific data for configuring a media codec for decoding Vorbis.

Parameters
byte[] initializationData

The initialization data from the ESDS box.

Returns
ImmutableList<byte[]>

Codec-specific data for configuring a media codec for decoding Vorbis.

readVorbisCommentHeader

public static VorbisUtil.CommentHeader readVorbisCommentHeader(ParsableByteArray headerData)

Reads a Vorbis comment header.

See the Vorbis spec/Comment header

Parameters
ParsableByteArray headerData

A ParsableByteArray wrapping the header data.

Returns
VorbisUtil.CommentHeader

A VorbisUtil.CommentHeader with all the comments.

Throws
androidx.media3.common.ParserException

If an error occurs parsing the comment header.

readVorbisCommentHeader

public static VorbisUtil.CommentHeader readVorbisCommentHeader(
    ParsableByteArray headerData,
    boolean hasMetadataHeader,
    boolean hasFramingBit
)

Reads a Vorbis comment header.

The data provided may not contain the Vorbis metadata common header and the framing bit.

See the Vorbis spec/Comment header

Parameters
ParsableByteArray headerData

A ParsableByteArray wrapping the header data.

boolean hasMetadataHeader

Whether the headerData contains a Vorbis metadata common header preceding the comment header.

boolean hasFramingBit

Whether the headerData contains a framing bit.

Returns
VorbisUtil.CommentHeader

A VorbisUtil.CommentHeader with all the comments.

Throws
androidx.media3.common.ParserException

If an error occurs parsing the comment header.

readVorbisIdentificationHeader

public static VorbisUtil.VorbisIdHeader readVorbisIdentificationHeader(ParsableByteArray headerData)

Reads a Vorbis identification header from headerData.

See the Vorbis spec/Identification header

Parameters
ParsableByteArray headerData

a ParsableByteArray wrapping the header data.

Throws
androidx.media3.common.ParserException

thrown if invalid capture pattern is detected.

readVorbisModes

public static VorbisUtil.Mode[] readVorbisModes(ParsableByteArray headerData, int channels)

This method reads the modes which are located at the very end of the Vorbis setup header. That's why we need to partially decode or at least read the entire setup header to know where to start reading the modes.

See the Vorbis spec/Setup header

Parameters
ParsableByteArray headerData

a ParsableByteArray containing setup header data.

int channels

the number of channels.

Returns
VorbisUtil.Mode[]

an array of Modes.

Throws
androidx.media3.common.ParserException

thrown if bit stream is invalid.

verifyVorbisHeaderCapturePattern

public static boolean verifyVorbisHeaderCapturePattern(
    int headerType,
    ParsableByteArray header,
    boolean quiet
)

Verifies whether the next bytes in header are a Vorbis header of the given headerType.

Parameters
int headerType

the type of the header expected.

ParsableByteArray header

the alleged header bytes.

boolean quiet

if true no exceptions are thrown. Instead false is returned.

Returns
boolean

Whether the header is a Vorbis header.

Throws
androidx.media3.common.ParserException

thrown if header type or capture pattern is not as expected.