Ogg mapping for Opus
The IETF Opus codec is a low-latency audio codec optimized for both voice and general-purpose audio. See [tools.ietf.org/html/draft-ietf-codec-opus the spec] for technical details.
Almost everything about this codec is either fixed or dynamically switchable, so the usual id and setup header parameters in the header packets of an Ogg encapsulation aren't useful. In particular, bitrate, frame size, mono/stereo, and coding modes are all dynamically switchable from packet to packet. A one-byte header on each data packet defines the parameters for that particular packet.
Remaining parameters we need to signal are:
- magic number for stream identification
- comment/metadata tags
Additionally there's been a desire to support some kind of channel bonding for surround, and some kind of option signalling for "Opus Custom", in particular the granulerate.
Granulepos is the count of decodeable samples at a fixed rate of 48 kHz.
Two headers: id, comment
- "OpusHead" (64 bits) - version number (8 bits) zero for this spec - Number of channels 'c' (8 bits) (must be > 0) - Pre-skip (16 bits) - Input sample rate (32 bits) (informational only) - stream mapping (8 bits) -- 0 = channels in vorbis spec order, 1..254 reserved (treat as 255), 255 = no defined channel meaning - Number of streams 'N'(8 bits) (must be > 0) - Number of two-output streams 'M' (8 bits) (M+N strictly smaller than 255) - for each output channel [0..c] -- read stream index (8 bits) (255 means silent through the file)
All two-output streams come first, so if the stream index is < 2*M, the channel decode the (index/2)th opus stream as stereo, selecting the (index%2)th output (left for even, right for odd). If index >= 2*M, decode the (index - M)th stream as mono and use that as the output. As a special case, a stream index of 255 means to write silence to that output channel.
- 8 byte 'OpusTags' magic signature (64 bits) - rest follows the vorbis-comment header design used in OggVorbis, OggTheora, and Speex. ** Vendor string (always present) ** tag=value metadata strings (zero or more)
Some discussion is in order.
- magic signature
The signature magic values allow codec identification and are being human readable. Starting with 'Op' helps distinguish them from data packets.
Version number. Must always be zero for this version of the encapsulation spec. In general revising the spec later isn't a good idea, but this also acts as a null terminator for the signature bytes and helps align the rest of the fields.
- stream mapping
We want to support multichannel. This defines the order and semantic meaning of the various channels encoded in each Opus packet.
For example, we can't just code 5.1 as three stereo Opus streams, because then LFE ends up sharing a stereo pair with another channel (RR in the Vorbis channel order) which isn't a good idea, while 6 mono channels wastes bandwidth. Or, when routing multitrack audio between mixing boards, it helps to be able to flag which instruments should be treated as mono and which are stereo.
We don't need 8 bits of separate channel meanings, so if we want to make it easier to parse the number of channels, we can make that part of some of the stream mappings: 0 = mono, 1 = stereo, 2 = 5.1 in vorbis order, 3 = 6.0 in some order, 4 = 7.1, etc.
This is the number of samples (at 48 kHz) to discard from the decoder output before starting playback. The idea is to mitigate transients, and to allow sample-accurate editing through Ogg chaining.
- input rate
This is not the sample rate for playback of the encoded data.
Opus has a handful of coding modes, supporting 8, 12, 16, 24, and 48 kHz signals. Which mode is chosen can be switched dynamically from packet to packet in the stream, but the reference decoder can generate output at any of those sample rates from the compressed data. Fidelity to the original sample rate of the encode input is not preserved by the lossy compression. Therefore, if the playback system supports one of those modes natively, the best option is to not resample but to play back directly at 48 kHz for best quality regardless of the value of this field.
However, the Ogg mapping allows the encoder to pass the sample rate of the original input stream as metadata. We felt this could be useful downstream, and as something intended for machine consumption, didn't belong in the tag header. For example, a decoder writing PCM format to disk might choose to resample the output audio back to the original input rate to reduce surprise.
- stream count
It is necessary to describe the number of streams so the decoder can correctly parse the packed frames inside the packet. We store the count-minus-one here, to remove invalid configuration of zero Opus streams in the this Ogg stream.
- stream description
For each Opus stream framed into the ogg packets of this logical bitstream, we define whether to decode it as mono or stereo, and give a channel index for how it should be mapped to playback. The semantic meaning of each channel index is defined by the stream mapping byte. E.g. it might be LEFT_REAR or CENTER.
For example, we can't just code 5.1 and three stereo Opus streams, because then LFE ends up sharing a stereo pair with another channel (RR in the Vorbis channel order) which isn't a good idea, while 6 mono channels wastes bandwidth. So we want to be able to say:
(stream mapping: vorbis channel order) stream 0: stereo: LEFT_FRONT, RIGHT_FRONT stream 1: mono: CENTER stream 2: stereo: LEFT_REAR, RIGHT_REAR stream 3: mono: LFE
- Planned test vectors for OggOpus
- Opus test vectors