You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
XAudio2 / Opus: single length-prefixed packet misread as raw RFC 6716, 6.1 mapping stream count, and MFT output checks #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):
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>.
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).
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.
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]:
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.
Opus: prefer length-prefixed framing, fix 6.1 mapping, harden MFT (#327) #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.
Activity
DavidM1911 commentedon Nov 24, 2026
Tried item 1 on a branch: prefer the length-prefixed reading whenever
try_length_prefixed_blockreturns 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 inframing_detection_awc_blocks_and_raw: thetrickybuffer[0xF8,0x00] ++ 248 zero bytesis genuinely ambiguous — as raw it is a valid CELT packet, and as[u16 len=248][packet]the inner 248 bytes also parse (TOC0x00).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_packetscallers 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 thetrickyexpectation 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.Grimkaaah commentedon Nov 24, 2026
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
Opus: prefer length-prefixed framing, fix 6.1 mapping, harden MFT (iv…
Opus: the MFT decoder takes only an output type with its channel count
Opus: the MFT decoder takes only an output type with its channel count
Opus: the MFT decoder takes only an output type with its channel count
Opus: the MFT decoder takes only an output type with its channel count
lordon1a commentedon Nov 24, 2026
On item 2 of this issue and the two PRs that act on it (#503, #506): the current 6.1 mapping on
mainis already the one libopus uses, and both PRs would move it away from that.libopus
src/opus_multistream_encoder.c(currentmainof 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 onmain. 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, sosplit_multistream_packetrejects the packet or decodes the wrong bytes per channel.build_opus_headis whatDecoder::newuses when the format has noOpusHead, so that is the path a title's 6.1 Opus stream takes.vorbis_mappingand in its testvorbis_mapping_6_1_matches_rfc7845. Separately, for item 1 it changes the input of the existingframing_detection_awc_blocks_and_rawtest (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(whichparse_opus_headalready honours), not the default table.Written with Claude Code (Claude Opus 5.5). I checked the libopus table against its source.
Opus: prefer length-prefixed framing, fix 6.1 mapping, harden MFT (xc…