OggOpus

From XiphWiki
Revision as of 12:25, 17 August 2011 by Bemasc (talk | contribs) (Change to fixed-point R128_TRACK_GAIN)
Jump to navigation Jump to search

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.

Draft spec

Granulepos is the count of decodeable samples at a fixed rate of 48 kHz.

Two headers: id, comment

Id header:

- "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)
- Output gain (16 bits, signed Q7.8 in dB) to apply when decoding
- stream mapping (8 bits)
 --  0 = one stream, RTP order, 1 = channels in vorbis spec order, 2..254 reserved (treat as 255), 255 = no defined channel meaning
if stream mapping > 0
- 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.

Comment header:

- 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)

One new comment field is introduced for Ogg Opus:

R128_TRACK_GAIN=-573  

representing the volume shift needed to normalize the track's volume. The gain is a Q7.8 fixed point number in dB, as in the OpusHead "output gain" field. This field acts similarly to the REPLAYGAIN_TRACK_GAIN field in Vorbis, although the normal volume reference is the EBU-R128 standard.

An Ogg Opus file MUST NOT have more than one such field, and if present its value MUST be an integer from -32768 to +32767 inclusive, represented in ASCII with no whitespace. When a player makes use of the TRACK_GAIN, it MUST be applied in addition to the OpusHead output gain, and MUST correctly represent the R128 normalization gain. If an encoder populates the TRACK_GAIN field, and the output gain is not otherwise constrained or specified, the encoder SHOULD write the R128 gain into the OpusHead output gain and set "R128_TRACK_GAIN=0". If a tool modifies the OpusHead "output gain" field, it MUST also update or remove the R128_TRACK_GAIN comment field.

There is no comment field corresponding to Replaygain's ALBUM_GAIN; that information should instead be stored in the OpusHead "output gain" field.

To avoid confusion with multiple normalization schemes, an OpusTags packet SHOULD NOT contain any of the REPLAYGAIN_TRACK_GAIN, REPLAYGAIN_TRACK_PEAK, REPLAYGAIN_ALBUM_GAIN, or REPLAYGAIN_ALBUM_PEAK fields.

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

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.

  • pre-skip

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.

  • output gain

This is a gain to be applied by the decoder. Virtually all players and media frameworks should apply it by default. Any volume adjustment or gain modification, such as the R128_TRACK_GAIN or a user-facing volume knob, MUST be applied in addition to this output gain in order to achieve playback at the desired volume.

An encoder SHOULD set the output gain to zero, and apply any gain prior to encoding, when this is possible and does not conflict with the user's wishes. The output gain should only be nonzero when the gain is adjusted after encoding, or when the user wishes to adjust the gain for playback while preserving the ability to recover the original signal amplitude.

Note that although the output gain has enormous range (+/- 128 dB, enough to amplify inaudible sounds to the threshold of physical pain), most applications can only reasonably use a small portion of this range around zero. The large range serves in part to ensure that gain can always be losslessly transferred between OpusHead and R128_TRACK_GAIN without saturating.

  • 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

Test vectors