An Opus stream in Ogg starts with two header packets, described in RFC 7845. Every other packet is audio. All numbers are little-endian.
OpusHead
The identification header is alone on the stream’s first page. It is 19 bytes, or more when a mapping table follows.
| Offset | Size | Name | Description |
|---|---|---|---|
| 0 | 8 | magic | the ASCII characters OpusHead |
| 8 | 1 | version | high 4 bits the major version, which must be 0; low 4 bits the minor version. Readers accept any minor version |
| 9 | 1 | channel count | the number of output channels, from 1 upwards |
| 10 | 2 | pre-skip | samples at 48 kHz to drop from the start of the decoded audio |
| 12 | 4 | input sample rate | the rate of the source, for information only; decoders output 48 kHz |
| 16 | 2 | output gain | a gain in Q7.8 dB (signed), applied at the output |
| 18 | 1 | mapping family | how channels are carried: see below |
| 19 | 1 | stream count | family ≠ 0: the number of Opus streams in each packet |
| 20 | 1 | coupled count | family ≠ 0: how many of those are stereo pairs |
| 21 | C | channel mapping | family ≠ 0 (except family 3): for each output channel, the decoded channel number, or 255 for silence |
Channel mapping families
| Family | Meaning |
|---|---|
| 0 | one stream, mono or stereo. No table. Every player must support it |
| 1 | 1 to 8 channels in the Vorbis channel order (below), with a mapping table. Every player must support it, and should be able to downmix it to stereo |
| 2 | ambisonics (RFC 8486): (order + 1)² channels, plus 2 when a non-diegetic stereo pair is added, with a mapping table in ACN order and SN3D normalisation |
| 3 | ambisonics with a demixing matrix (RFC 8486): in place of the mapping table, a matrix of channels × (streams + coupled) 16-bit values (below) |
| 255 | discrete channels with no defined layout, with a mapping table. A general-purpose player should not play such a stream; it is for applications that know what the channels are |
| 4 to 254 | reserved. A reader treats a reserved family as though it were 255: the header has a stream count, a coupled count and a mapping table, and the channels have no defined meaning |
A stream count of N and coupled count of M give N + M decoded channels. Decoded channel numbers below 2M are the coupled pairs’ left and right; the rest are the mono streams.
Family 1 layouts
The channel count fixes the layout, as RFC 7845 section 5.1.1.2 gives it (the order Vorbis uses):
| Channels | Layout | Order |
|---|---|---|
| 1 | mono | mono |
| 2 | stereo | left, right |
| 3 | linear surround | left, centre, right |
| 4 | quadraphonic | front left, front right, rear left, rear right |
| 5 | 5.0 surround | front left, front centre, front right, rear left, rear right |
| 6 | 5.1 surround | front left, front centre, front right, rear left, rear right, LFE |
| 7 | 6.1 surround | front left, front centre, front right, side left, side right, rear centre, LFE |
| 8 | 7.1 surround | front left, front centre, front right, side left, side right, rear left, rear right, LFE |
The family 3 demixing matrix
Family 3 (RFC 8486) codes K = streams + coupled channels that are a mix of the ambisonic channels, and gives the matrix that turns them back. The output channel i is the sum over the decoded channels j of D[i][j] × decoded[j]. The matrix follows the coupled count, at offset 21, as C × K signed 16-bit little-endian values in Q15 (32,767 is just under 1), stored column by column: all C coefficients for decoded channel 0, then all for channel 1, and so on. There is no channel mapping table.
Multistream packets
With N streams, each audio packet holds N Opus packets one after another. Every one but the last uses the self-delimiting framing of RFC 6716 appendix B, which writes even the lengths an ordinary packet leaves implied, so a reader can find where it ends; the last uses the ordinary framing. All the streams of one packet must last the same time.
Pages
- OpusHead is alone on the stream’s first page, which has the BOS flag.
- OpusTags starts on the second page and may span several. The page it finishes on holds nothing else: the first audio packet starts a new page.
- Pages that finish only header packets have granule position 0.
Time and trimming
Granule positions count 48 kHz samples: a page’s granule position is the number of samples
decoded up to the end of its last finished packet, counting from the stream’s start position.
The playing time of a granule position is (granule − pre-skip) / 48000 seconds.
- Start. The first page with audio may have a granule position larger than the samples its packets give: the stream then starts later than 0 (it was cut from a longer one), at the difference. A granule position smaller than those samples makes the stream invalid, unless the page is also the last one (EOS), when a reader takes the start position to be 0 and the granule position trims the end. Samples are dropped from the start only through pre-skip.
- Pre-skip. The first pre-skip samples decoded are thrown away; they let the decoder settle. The last page’s granule position must be at least the pre-skip.
- End. The last page may give a granule position smaller than the samples decoded: the excess at the end is discarded, which is how Opus files end on an exact sample. It may discard no more than the last packet decodes.
- Seeking. After a seek a decoder needs 80 ms (3,840 samples) of pre-roll to converge, so a reader starts decoding at least that far before the target.
- Gaps. When a live stream loses packets, a muxer should fill the gap with packets the decoder conceals (frames of no data), so the granule positions stay continuous.
Decoders output 48 kHz at the header’s output gain. Decoding follows RFC 6716 as updated by RFC 8251.