Field notes/audioopussurround

Opus Multistream in Rust: Surround Decoding in rusty-opus 1.2

One Opus stream stops at stereo. Opus multistream packs several into one packet for 5.1, 7.1 and custom layouts. What rusty-opus 1.2 fixed, and how to decode it in Rust.

Signed by M·
Painted stone bell tower room at dawn where six bell ringers stand in a circle, each holding a striped rope that rises toward a different bell above, with soft window light and a few drifting motes
How do I decode Opus multistream surround audio in Rust?

Use rusty-opus 1.2.0 or later. Build a ChannelLayout from the stream's OpusHead with ChannelLayout::from_opus_head, or use the standard 5.1 or 7.1 layout, then decode each packet with OpusMSDecoder into interleaved samples. Versions before 1.2.0 garbled every stream except the last, so re-decode old multichannel output.

A single Opus stream carries one or two channels. That covers voice chat, podcasts and stereo music, which is most of what Opus is used for. Surround sound needs more: six channels for 5.1, eight for 7.1, and up to 255 for layouts an application defines itself. Opus multistream is how the format gets there, by packing several ordinary Opus streams into one packet and telling the decoder which speaker each channel belongs to.

rusty-opus already shipped a multistream encoder and decoder before this month. Version 1.2.0, published November 19, 2026, rewrote the decoder side after a real defect turned up, and added explicit layouts so the decoder can follow whatever a file's header declares. Version 1.2.1 on November 23 is a dependency bump with no codec change listed.

If you decode more than two channels with rusty-opus, read the second section before anything else. The rest explains how multichannel Opus is put together and how to use the new API.

How Opus multistream packs surround into one packet

The framing lives in RFC 7845, the Ogg Opus specification. A multistream packet holds N Opus streams. The first M of them are coupled, meaning each decodes to a stereo pair, and the rest decode to mono. A 5.1 signal, for example, is usually four streams: two coupled pairs plus two mono streams for the center and the LFE.

The streams sit back to back. Only the last one uses the regular Opus framing from RFC 6716. Every earlier one uses the self-delimiting framing from RFC 6716 Appendix B, which adds a length so the decoder knows where the next stream starts. The appendix is blunt about the catch: nothing in a packet tells a decoder which framing it is looking at. The container has to say so. All streams in one packet must also cover the same duration.

A mapping table then assigns decoded channels to output channels. RFC 7845 groups the common cases into channel mapping families. Family 0 is mono or stereo with no table at all. Family 1 covers one to eight channels in Vorbis order, so six channels mean front left, front center, front right, rear left, rear right and LFE. That order is not the one WAVE and FLAC use, which matters if you convert surround Opus into a lossless archive. Family 255 allows up to 255 channels with no defined meaning, and a table entry of 255 marks a channel as silence. RFC 8486 later assigned families 2 and 3 to Ambisonics.

What changed in rusty-opus 1.2

The 1.2.0 commit describes three changes: a decoding fix, a layout API and a new oracle test.

The self-delimited framing bug

Before 1.2.0, OpusMSDecoder::decode handed each sub-packet to the single-stream decoder unchanged. The self-delimiting length was decoded as if it were audio. Checked against libopus 1.6.1, every channel of every stream except the last came out as full-scale garbage, up to about 52,000 sixteen-bit steps off, while the last stream matched. Packets of 60 ms were rejected outright, and the offset to the next stream ignored Opus padding.

The practical advice is short. If you decoded multichannel Opus with rusty-opus 1.1.1 or earlier, upgrade and decode that material again. Stereo and mono users were not affected: the commit says the single-stream path is unchanged.

Explicit layouts from an OpusHead

The decoder used to assume the standard layout for a channel count. Now it can take any layout RFC 7845 allows. ChannelLayout::new builds and validates one by hand. ChannelLayout::from_opus_head reads the identification header a file or stream carries, for families 0, 1 and 255. OpusMSDecoder::with_layout decodes with it, including silent outputs and outputs that duplicate a decoded channel.

Decoding is also stricter in the way libopus is. The whole packet is validated first: every stream present, one duration for all of them, and an output buffer large enough. An empty packet counts as lost, so every stream is concealed.

How the fix was checked

The release adds tests/multistream_oracle.rs, which compares rusty-opus Opus multistream output against committed libopus 1.6.1 output for 5.1, 7.1 and three-channel layouts, family 255, 10, 20 and 60 ms frames, all three coding modes and CBR padding. The commit reports agreement within one sixteen-bit step everywhere and exact SILK output.

We also ran a scratch crate pinned to rusty-opus 1.2.1 on rustc 1.100.0. The standard 5.1 layout came back as four streams, two coupled, with mapping [0, 4, 1, 2, 3, 5]. Fifty 20 ms frames of a six-tone test signal decoded with signal on all six channels, an invalid mapping table returned an error rather than a panic, and a family 2 header was refused. That is a smoke test, not a benchmark.

Decoding surround audio in a Rust project

The API is small. These are the calls the documentation for 1.2.1 lists, used the way the scratch crate used them.

Decode a standard 5.1 or 7.1 stream

When you know the stream is plain family 1 surround, ask for the standard layout by channel count:

use rusty_opus::multistream::OpusMSDecoder;

let mut dec = OpusMSDecoder::new(48_000, 6, 1)?; // 6 channels, family 1 (5.1)
let mut pcm = vec![0.0f32; 960 * 6];             // room for one 20 ms frame
let n = dec.decode(&packet, 960, &mut pcm)?;     // n samples per channel, interleaved

The frame size is a capacity, as in libopus, and the decoder returns the packet's own duration. Pass an empty slice for a lost packet.

Decode whatever the header declares

For files and streams you did not produce, read the layout instead of guessing it:

use rusty_opus::multistream::{ChannelLayout, OpusMSDecoder};

let layout = ChannelLayout::from_opus_head(&opus_head)?; // the whole OpusHead packet
let mut dec = OpusMSDecoder::with_layout(48_000, layout)?;

Treat family 255 with care. RFC 7845 says general-purpose players should not try to play those channels as speakers, and suggests offline tools write each channel to its own file instead. The family suits applications that define their own channel meaning and route each channel themselves.

Plan for what rusty-opus does not do yet

Four limits are worth knowing before you build on it. Multistream encoding supports families 0 and 1 only. The encoder splits bitrate evenly across streams, with coupled streams getting twice a mono share, while libopus adds a surround-masking analysis that the documentation calls a quality refinement. Ambisonics families 2 and 3 are not supported. And as with stereo, rusty-opus works with packets and does not document an Ogg writer, so a .opus file still needs a container layer.

Start from the rusty-opus repository and its field note, which game developers can now also find on Are We Game Yet. For whole media pipelines, remade_ffmpeg_rs is the place to look, and the Media band collects the rest of the audio and video work.

FAQ

Quick answers for builders evaluating this technology.

Can a single Opus stream carry 5.1 surround?

No. A regular Opus stream carries one or two channels. RFC 6716 says a multichannel stream is formed from several Opus streams packed together, and RFC 7845 defines how: N streams per packet, the first M of them coupled stereo pairs, plus a table that maps the decoded channels onto output speakers.

What are Opus channel mapping families 0, 1 and 255?

RFC 7845 defines them. Family 0 is plain mono or stereo with no mapping table. Family 1 covers one to eight channels in Vorbis speaker order, including 5.1 and 7.1. Family 255 allows up to 255 channels with no defined meaning, so general players should not play it as speakers.

What bug did rusty-opus 1.2.0 fix in multistream decoding?

Every stream in a multistream packet except the last uses self-delimited framing, which adds a length field. Earlier releases decoded that field as audio, so against libopus 1.6.1 every channel of every non-last stream came out as full-scale garbage while the last stream matched. 60 ms packets were rejected.

Does rusty-opus support Ambisonics (mapping families 2 and 3)?

Not as of 1.2.1. The documentation lists decode support for families 0, 1 and 255 and encode support for 0 and 1. In a scratch test, ChannelLayout::from_opus_head returned a BadArg error for a family 2 header, so Ambisonics from RFC 8486 needs another path for now.

Is rusty-opus multistream encoding equal to libopus?

The bitstream is standard and interoperates, but the encoder is simpler. Its documentation says bitrate is split evenly across streams, with coupled streams getting twice a mono stream's share, while libopus adds a surround-masking analysis on top. That is a quality refinement, not a conformance requirement.