XAudio2 / Opus: single length-prefixed packet misread as raw RFC 6716, 6.1 mapping stream count, and MFT output checks #327

Open
opened 2026-11-26 04:20:07 +00:00 by github-bot · 3 comments
Owner

@⁠mastrr9 on GitHub · 2026-11-23 23:29 UTC · original

Static audit of crates/xcl/src/hle/opus.rs on main (85bdf21) found three bugs on the XAudio2 Opus path (independent of xapu / #294):

  1. Single unpadded [u16 LE len][pkt] buffer misread as raw RFC 6716 (opus.rs:734-739)

    • split_packets prefers parse_packet(data, multistream) over an exact-fit single length-prefixed packet when there is no zero-padded tail.
    • Because parse_packet treats the 2-byte length prefix as the TOC + first payload byte, almost any length passes (len % 4 == 0 -> code 0; 1 -> code 1; 2 -> code 2 with len1 = 0; 3 -> code 3 for high byte 1..=47).
    • Verified with a clean-room RFC 6716 parser check: [0x50, 0x00] ++ <80-byte 0xF8 CELT packet> has an exact-fit prefix (len == 80 == data.len() - 2), yet parse_packet(data) reads TOC 0x50 (SILK WB 40 ms, code 0) and accepts all 82 bytes as one raw packet, causing garbled decode and a wrong estimate_buffer_frames count (1920 instead of 960).
    • Suggested fix: When try_length_prefixed_block finds an exact-fit single packet (len == data.len() - 2) whose inner packet parses cleanly, prefer the length-prefixed interpretation (or pass an explicit framing hint from the caller). Add a unit test covering [0x50, 0x00] ++ <80-byte 0xF8>.
  2. 7-channel (6.1) Vorbis channel mapping stream count (opus.rs:410)

    • vorbis_mapping(7) returns Some((4, 3, &[0, 4, 1, 2, 3, 5, 6])) (streams = 4, coupled = 3).
    • Standard libopus / RFC 7845 6.1 mapping is (5, 2, &[0, 4, 1, 2, 3, 5, 6]) (2 coupled pairs + 3 uncoupled = 5 streams, 7 channels).
  3. MftDecoder::configure_output / drain_output guards (opus.rs:951-965, 1077-1080)

    • configure_output accepts the first PCM16/Float32 output type without checking ch == self.channels and overwrites self.channels, which can cause an out-of-bounds panic during multistream interleaving if the MFT enumerates a different channel count first.
    • drain_output retries MF_E_TRANSFORM_STREAM_CHANGE in an unbounded loop; capping retries (e.g. 4) avoids hanging on a misbehaving MFT.

Not tested: on Windows (static code audit + clean-room Python RFC 6716 verification on macOS; no game files).

AI use: Audited with Antigravity (Gemini 4 Neon) and Claude Opus 5.5 (static code review only).

**@⁠mastrr9** on GitHub · 2026-11-23 23:29 UTC · [original](https://github.com/xcl-dev/xcl/issues/327) Static audit of `crates/xcl/src/hle/opus.rs` on `main` (`85bdf21`) found three bugs on the XAudio2 Opus path (independent of `xapu` / #294): 1. **Single unpadded `[u16 LE len][pkt]` buffer misread as raw RFC 6716 (`opus.rs:734-739`)** - `split_packets` prefers `parse_packet(data, multistream)` over an exact-fit single length-prefixed packet when there is no zero-padded tail. - Because `parse_packet` treats the 2-byte length prefix as the TOC + first payload byte, almost any length passes (`len % 4 == 0` -> code 0; `1` -> code 1; `2` -> code 2 with `len1 = 0`; `3` -> code 3 for high byte `1..=47`). - Verified with a clean-room RFC 6716 parser check: `[0x50, 0x00] ++ <80-byte 0xF8 CELT packet>` has an exact-fit prefix (`len == 80 == data.len() - 2`), yet `parse_packet(data)` reads TOC `0x50` (SILK WB 40 ms, code 0) and accepts all 82 bytes as one raw packet, causing garbled decode and a wrong `estimate_buffer_frames` count (`1920` instead of `960`). - **Suggested fix:** When `try_length_prefixed_block` finds an exact-fit single packet (`len == data.len() - 2`) whose inner packet parses cleanly, prefer the length-prefixed interpretation (or pass an explicit framing hint from the caller). Add a unit test covering `[0x50, 0x00] ++ <80-byte 0xF8>`. 2. **7-channel (6.1) Vorbis channel mapping stream count (`opus.rs:410`)** - `vorbis_mapping(7)` returns `Some((4, 3, &[0, 4, 1, 2, 3, 5, 6]))` (`streams = 4, coupled = 3`). - Standard libopus / RFC 7845 6.1 mapping is `(5, 2, &[0, 4, 1, 2, 3, 5, 6])` (2 coupled pairs + 3 uncoupled = 5 streams, 7 channels). 3. **`MftDecoder::configure_output` / `drain_output` guards (`opus.rs:951-965, 1077-1080`)** - `configure_output` accepts the first PCM16/Float32 output type without checking `ch == self.channels` and overwrites `self.channels`, which can cause an out-of-bounds panic during multistream interleaving if the MFT enumerates a different channel count first. - `drain_output` retries `MF_E_TRANSFORM_STREAM_CHANGE` in an unbounded loop; capping retries (e.g. 4) avoids hanging on a misbehaving MFT. **Not tested:** on Windows (static code audit + clean-room Python RFC 6716 verification on macOS; no game files). **AI use:** Audited with Antigravity (Gemini 4 Neon) and Claude Opus 5.5 (static code review only).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:20:07 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 00:48 UTC · original

Tried item 1 on a branch: prefer the length-prefixed reading whenever try_length_prefixed_block returns a clean single packet. It fixes the reported case ([0x50,0x00] ++ <0xF8 packet> now yields the 80-byte inner packet), but it also flips an existing assertion in framing_detection_awc_blocks_and_raw: the tricky buffer [0xF8,0x00] ++ 248 zero bytes is genuinely ambiguous — as raw it is a valid CELT packet, and as [u16 len=248][packet] the inner 248 bytes also parse (TOC 0x00).

So the two interpretations cannot be separated by "inner parses cleanly" alone; whichever rule is chosen has to pick a side on that shape. Options I see: (a) prefer length-prefixed only when the caller knows the buffer is AWC-framed (an explicit framing hint, as the issue suggests — extract_packets callers in the XAudio2 path may know the submit format); (b) prefer raw when the first byte is a plausible TOC for the expected stream config; (c) accept flipping the tricky expectation if upstream decides single unpadded buffers are always framed. Not opening a PR until the tie-break is settled — happy to implement whichever rule you prefer.

**@⁠DavidM1911** on GitHub · 2026-11-24 00:48 UTC · [original](https://github.com/xcl-dev/xcl/issues/327#issuecomment-7118445279) Tried item 1 on a branch: prefer the length-prefixed reading whenever `try_length_prefixed_block` returns a clean single packet. It fixes the reported case (`[0x50,0x00] ++ <0xF8 packet>` now yields the 80-byte inner packet), but it also flips an existing assertion in `framing_detection_awc_blocks_and_raw`: the `tricky` buffer `[0xF8,0x00] ++ 248 zero bytes` is genuinely ambiguous — as raw it is a valid CELT packet, and as `[u16 len=248][packet]` the inner 248 bytes also parse (TOC `0x00`). So the two interpretations cannot be separated by "inner parses cleanly" alone; whichever rule is chosen has to pick a side on that shape. Options I see: (a) prefer length-prefixed only when the caller knows the buffer is AWC-framed (an explicit framing hint, as the issue suggests — `extract_packets` callers in the XAudio2 path may know the submit format); (b) prefer raw when the first byte is a plausible TOC for the expected stream config; (c) accept flipping the `tricky` expectation if upstream decides single unpadded buffers are always framed. Not opening a PR until the tie-break is settled — happy to implement whichever rule you prefer.
Author
Owner

@⁠Grimkaaah on GitHub · 2026-11-24 01:03 UTC · original

PR opened covering all three items from the issue (framing preference, 6.1 mapping, MFT channel/stream-change guards) with unit tests.

Made with Cursor

**@⁠Grimkaaah** on GitHub · 2026-11-24 01:03 UTC · [original](https://github.com/xcl-dev/xcl/issues/327#issuecomment-7118713329) PR opened covering all three items from the issue (framing preference, 6.1 mapping, MFT channel/stream-change guards) with unit tests. Made with [Cursor](https://cursor.com)
Author
Owner

@⁠lordon1a on GitHub · 2026-11-24 04:07 UTC · original

On item 2 of this issue and the two PRs that act on it (#503, #506): the current 6.1 mapping on main is already the one libopus uses, and both PRs would move it away from that.

libopus src/opus_multistream_encoder.c (current main of xiph/opus), vorbis_mappings[8]:

      {4, 2, {0, 4, 1, 2, 3, 5}},       /* 6: 5.1 surround */
      {4, 3, {0, 4, 1, 2, 3, 5, 6}},    /* 7: 6.1 surround */
      {5, 3, {0, 6, 1, 2, 3, 4, 5, 7}}, /* 8: 7.1 surround */

So 6.1 is 4 streams / 3 coupled, exactly what vorbis_mapping(7) returns on main. The issue's "standard libopus / RFC 7845 6.1 mapping is (5, 2, …)" does not match libopus. With the same mapping list, (5, 2) and (4, 3) both give 7 decoded channels but group them differently: under (4, 3), centre and rear centre are one coupled stream. A multistream packet made by a libopus encoder for 7 channels has 4 streams. Split as 5 streams, the 4th self-delimited sub-packet's length is read as the start of a 5th, so split_multistream_packet rejects the packet or decodes the wrong bytes per channel. build_opus_head is what Decoder::new uses when the format has no OpusHead, so that is the path a title's 6.1 Opus stream takes.

  • #506 (only this change, checks pass): I'd suggest not merging. Its new test asserts (5, 2).
  • #503: includes the same change in vorbis_mapping and in its test vorbis_mapping_6_1_matches_rfc7845. Separately, for item 1 it changes the input of the existing framing_detection_awc_blocks_and_raw test (tricky[1] = 0x00 → 0xFF). That test covered the buffer that is valid both as one raw packet and as one length-prefixed packet, which @⁠DavidM1911 described above. With the new byte, the ambiguous case is no longer tested; the test was changed to fit the new preference. Items 1 and 3 are separate changes and would be easier to review as their own PRs, without the mapping change.

I have not run a 6.1 title stream. This is from the libopus source and the packet layout, not a decode on a title. If a console-encoded 6.1 stream really is 5 / 2, the right place for that is the stream's own OpusHead (which parse_opus_head already honours), not the default table.

Written with Claude Code (Claude Opus 5.5). I checked the libopus table against its source.

**@⁠lordon1a** on GitHub · 2026-11-24 04:07 UTC · [original](https://github.com/xcl-dev/xcl/issues/327#issuecomment-7121722096) On item 2 of this issue and the two PRs that act on it (#503, #506): the current 6.1 mapping on `main` is already the one libopus uses, and both PRs would move it away from that. libopus `src/opus_multistream_encoder.c` (current `main` of xiph/opus), `vorbis_mappings[8]`: ```c {4, 2, {0, 4, 1, 2, 3, 5}}, /* 6: 5.1 surround */ {4, 3, {0, 4, 1, 2, 3, 5, 6}}, /* 7: 6.1 surround */ {5, 3, {0, 6, 1, 2, 3, 4, 5, 7}}, /* 8: 7.1 surround */ ``` So 6.1 is 4 streams / 3 coupled, exactly what `vorbis_mapping(7)` returns on `main`. The issue's "standard libopus / RFC 7845 6.1 mapping is `(5, 2, …)`" does not match libopus. With the same mapping list, (5, 2) and (4, 3) both give 7 decoded channels but group them differently: under (4, 3), centre and rear centre are one coupled stream. A multistream packet made by a libopus encoder for 7 channels has 4 streams. Split as 5 streams, the 4th self-delimited sub-packet's length is read as the start of a 5th, so `split_multistream_packet` rejects the packet or decodes the wrong bytes per channel. `build_opus_head` is what `Decoder::new` uses when the format has no `OpusHead`, so that is the path a title's 6.1 Opus stream takes. - **#506** (only this change, checks pass): I'd suggest not merging. Its new test asserts (5, 2). - **#503**: includes the same change in `vorbis_mapping` and in its test `vorbis_mapping_6_1_matches_rfc7845`. Separately, for item 1 it changes the input of the existing `framing_detection_awc_blocks_and_raw` test (`tricky[1] = 0x00` → `0xFF`). That test covered the buffer that is valid both as one raw packet and as one length-prefixed packet, which @⁠DavidM1911 described above. With the new byte, the ambiguous case is no longer tested; the test was changed to fit the new preference. Items 1 and 3 are separate changes and would be easier to review as their own PRs, without the mapping change. I have not run a 6.1 title stream. This is from the libopus source and the packet layout, not a decode on a title. If a console-encoded 6.1 stream really is 5 / 2, the right place for that is the stream's own `OpusHead` (which `parse_opus_head` already honours), not the default table. Written with Claude Code (Claude Opus 5.5). I checked the libopus table against its source.
This discussion has been locked. Commenting is limited to contributors.
No description provided.