OggOpus

From XiphWiki
Revision as of 14:48, 4 August 2011 by Rillian (talk | contribs) (Draft spec: a few more points)
Jump to: navigation, 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:

   5 /* Header contents:
   6   - "OpusHead" (64 bits)
   7   - version number (8 bits) zero for this spec
   8   - stream mapping (8 bits)
   9   --  0 = mono, 1 = L,R stereo, 2 = undefined discrete, 3 = 5.1 in vorbis order, etc. *OR*
   10  --  0 = channels in vorbis spec order, 1 = no defined channel meaning, >= 2 reserved, treat as 1
   11  - Pre-skip (16 bits)
   12  - Input sample rate (32 bits)
   13  - Highest stream number N (8 bits)
   14      - N+1 times:
   15           - stereo flag (8 bits, 0=mono, 1=stereo)
   16           - channel index for left (8 bits)
   17           - if stereo:
   18              - channel index for right (8 bits)
   19 */

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)

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.

  • 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 for quality is to not resample' but to play back directly at 48 kHz 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 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 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. 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.

  • stream count

It is necessary to describe the number of streams so the decoder can correctly parse the packed frames inside the packet.

  • 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