# VorbisUtil

Artifact: [androidx.media3:media3-container](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/container/VorbisUtil.java+class:androidx.media3.container.VorbisUtil)

*** ** * ** ***


```
@UnstableApi
public final class VorbisUtil
```

<br />

*** ** * ** ***

Utility methods for parsing Vorbis streams.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/container/VorbisUtil.CommentHeader` Vorbis comment header. |
| `public final class https://developer.android.com/reference/androidx/media3/container/VorbisUtil.Mode` Vorbis setup header modes. |
| `public final class https://developer.android.com/reference/androidx/media3/container/VorbisUtil.VorbisIdHeader` Vorbis identification header. |

| ### Public methods |
|---|---|
| `static int` | `https://developer.android.com/reference/androidx/media3/container/VorbisUtil#getPacketBlockSize(byte,androidx.media3.container.VorbisUtil.VorbisIdHeader,androidx.media3.container.VorbisUtil.Mode[])( byte firstByteOfAudioPacket, https://developer.android.com/reference/androidx/media3/container/VorbisUtil.VorbisIdHeader idHeader, VorbisUtil.Mode[] modes )` Returns the block size of the given packet. |
| `static int` | `https://developer.android.com/reference/androidx/media3/container/VorbisUtil#getSampleCountInPacket(int,int,boolean)( int packetBlockSize, int previousPacketBlockSize, boolean seenFirstAudioPacket )` Returns the number of samples in the given packet. |
| `static int` | `https://developer.android.com/reference/androidx/media3/container/VorbisUtil#iLog(int)(int x)` Returns ilog(x), which is the index of the highest set bit in `x`. |
| `static https://developer.android.com/reference/androidx/media3/container/VorbisUtil.CommentHeader` | `https://developer.android.com/reference/androidx/media3/container/VorbisUtil#readVorbisCommentHeader(androidx.media3.common.util.ParsableByteArray)(https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray headerData)` Reads a Vorbis comment header. |
| `static https://developer.android.com/reference/androidx/media3/container/VorbisUtil.CommentHeader` | `https://developer.android.com/reference/androidx/media3/container/VorbisUtil#readVorbisCommentHeader(androidx.media3.common.util.ParsableByteArray,boolean,boolean)( https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray headerData, boolean hasMetadataHeader, boolean hasFramingBit )` Reads a Vorbis comment header. |
| `static https://developer.android.com/reference/androidx/media3/container/VorbisUtil.VorbisIdHeader` | `https://developer.android.com/reference/androidx/media3/container/VorbisUtil#readVorbisIdentificationHeader(androidx.media3.common.util.ParsableByteArray)(https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray headerData)` Reads a Vorbis identification header from `headerData`. |
| `static VorbisUtil.Mode[]` | `https://developer.android.com/reference/androidx/media3/container/VorbisUtil#readVorbisModes(androidx.media3.common.util.ParsableByteArray,int)(https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray headerData, int channels)` This method reads the modes which are located at the very end of the Vorbis setup header. |
| `static boolean` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/container/VorbisUtil#verifyVorbisHeaderCapturePattern(int,androidx.media3.common.util.ParsableByteArray,boolean)( int headerType, https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray header, boolean quiet )` Verifies whether the next bytes in `header` are a Vorbis header of the given ` headerType`. |

## Public methods

### getPacketBlockSize

```
public static int getPacketBlockSize(
    byte firstByteOfAudioPacket,
    VorbisUtil.VorbisIdHeader idHeader,
    VorbisUtil.Mode[] modes
)
```

Returns the block size of the given packet.

| Parameters |
|---|---|
| `byte firstByteOfAudioPacket` | The first byte of the audio packet. |
| `https://developer.android.com/reference/androidx/media3/container/VorbisUtil.VorbisIdHeader idHeader` | The Vorbis identification header. |
| `VorbisUtil.Mode[] modes` | The Vorbis modes. |

| Returns |
|---|---|
| `int` | The block size of the packet. |

### getSampleCountInPacket

```
public static int getSampleCountInPacket(
    int packetBlockSize,
    int previousPacketBlockSize,
    boolean seenFirstAudioPacket
)
```

Returns the number of samples in the given packet.

| Parameters |
|---|---|
| `int packetBlockSize` | The block size of the packet. |
| `int previousPacketBlockSize` | The block size of the previous packet. |
| `boolean seenFirstAudioPacket` | Whether the first audio packet has been seen. |

| Returns |
|---|---|
| `int` | The number of samples in the packet. |

### 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](https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-1190009.2.1)

| Parameters |
|---|---|
| `int x` | the value of which the ilog should be calculated. |

| Returns |
|---|---|
| `int` | ilog(x) |

### readVorbisCommentHeader

```
public static VorbisUtil.CommentHeader readVorbisCommentHeader(ParsableByteArray headerData)
```

Reads a Vorbis comment header.

See the [Vorbis spec/Comment header](https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-640004.2.3)

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray headerData` | A `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray` wrapping the header data. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/container/VorbisUtil.CommentHeader` | A `https://developer.android.com/reference/androidx/media3/container/VorbisUtil.CommentHeader` with all the comments. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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](https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-640004.2.3)

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray headerData` | A `https://developer.android.com/reference/androidx/media3/common/util/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 |
|---|---|
| `https://developer.android.com/reference/androidx/media3/container/VorbisUtil.CommentHeader` | A `https://developer.android.com/reference/androidx/media3/container/VorbisUtil.CommentHeader` with all the comments. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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](https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-630004.2.2)

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray headerData` | a `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray` wrapping the header data. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/container/VorbisUtil.VorbisIdHeader` | a `https://developer.android.com/reference/androidx/media3/container/VorbisUtil.VorbisIdHeader` with meta data. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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](https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-650004.2.4)

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray headerData` | a `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray` containing setup header data. |
| `int channels` | the number of channels. |

| Returns |
|---|---|
| `VorbisUtil.Mode[]` | an array of `https://developer.android.com/reference/androidx/media3/container/VorbisUtil.Mode`s. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/ParserException` | thrown if bit stream is invalid. |

### verifyVorbisHeaderCapturePattern

```
@CanIgnoreReturnValue
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. |
| `https://developer.android.com/reference/androidx/media3/common/util/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 |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/ParserException` | thrown if header type or capture pattern is not as expected. |