XAudio2: XMA2 decoder prematurely decodes 3+-packet frames and leaves stale reservoir bits after packet loss #1391

Closed
opened 2026-11-26 04:52:42 +00:00 by github-bot · 3 comments
Owner

@⁠microwave805 on GitHub · 2026-11-24 15:31 UTC · original

Checked on main (930e8b4).

Problem

StreamDecoder::decode_packet_step (crates/xcl/src/hle/xma2/mod.rs:1566-1582) has two compounding state-machine bugs in packet-continuation handling (diverging from FFmpeg's wmaprodec.c):

if num_bits_prev_frame > 0 {
    let remaining_packet_bits = buf_bit_size.saturating_sub(pgb.bits_count());
    if num_bits_prev_frame >= remaining_packet_bits {
        num_bits_prev_frame = remaining_packet_bits;
        self.packet_done = true;
    }
    self.save_bits(&mut pgb, num_bits_prev_frame, true);
    if !self.packet_loss {
        let (_, gf) = self.decode_frame(out_frame);
        got_frame = gf;
    }
}

if self.packet_loss {
    self.num_saved_bits = 0;
    self.packet_loss = false;
}
  1. Premature decode on 3+-packet frames: Line 1573 calls self.decode_frame(out_frame) whenever !self.packet_loss, without checking && !self.packet_done. On the middle packet of a frame spanning 3+ packets (num_bits_prev_frame >= remaining_packet_bits, e.g. 0x7FFF >= 16352), self.packet_done is set true, yet decode_frame runs immediately on the incomplete frame, fails, sets self.packet_loss = true, and drops the valid frame.
  2. Stale bit reservoir after packet_loss: When self.packet_loss is true (either from #1 or on stream start/seek where StreamDecoder::new initializes packet_loss: true at line 841), line 1572 still appends num_bits_prev_frame bits into self.reservoir (advancing self.reservoir.bit_pos), and lines 1579-1582 clear self.num_saved_bits = 0; self.packet_loss = false; without calling self.reservoir.reset() or zeroing self.frame_offset, and even when self.packet_done == true. On the next packet with num_bits_prev_frame > 0, save_bits(..., true) appends new bits at the stale reservoir.bit_pos (16352+), while decode_frame reads frame_offset .. num_saved_bits from the start of self.reservoir.buf (stale bytes from an earlier packet).

Minimal trigger

  • Any valid 3-packet XMA1/XMA2 frame (packet 0 ends mid-frame, packet 1 has num_bits_prev_frame = 0x7FFF, packet 2 finishes with num_bits_prev_frame = 500) → packet 1 prematurely fails decode_frame, drops the frame, and leaves stale reservoir state that corrupts packet 2.
  • Starting or seeking a stream onto a continuation packet (num_bits_prev_frame = 0x7FFF with initial packet_loss = true) followed by a packet with num_bits_prev_frame > 0 → decodes stale reservoir bytes instead of incoming bits.

Suggested fix

When self.packet_loss is true, skip num_bits_prev_frame bits via pgb.skip_bits instead of calling save_bits; guard decode_frame at line 1573 with if !self.packet_done && !self.packet_loss; and on recovery (lines 1579-1582), reset self.reservoir and self.frame_offset = 0, keeping self.packet_loss = true when self.packet_done is true.


AI use: Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on 930e8b4 before opening.

**@⁠microwave805** on GitHub · 2026-11-24 15:31 UTC · [original](https://github.com/xcl-dev/xcl/issues/1391) Checked on `main` (`930e8b4`). ### Problem `StreamDecoder::decode_packet_step` (`crates/xcl/src/hle/xma2/mod.rs:1566-1582`) has two compounding state-machine bugs in packet-continuation handling (diverging from FFmpeg's `wmaprodec.c`): ```rust if num_bits_prev_frame > 0 { let remaining_packet_bits = buf_bit_size.saturating_sub(pgb.bits_count()); if num_bits_prev_frame >= remaining_packet_bits { num_bits_prev_frame = remaining_packet_bits; self.packet_done = true; } self.save_bits(&mut pgb, num_bits_prev_frame, true); if !self.packet_loss { let (_, gf) = self.decode_frame(out_frame); got_frame = gf; } } if self.packet_loss { self.num_saved_bits = 0; self.packet_loss = false; } ``` 1. **Premature decode on 3+-packet frames:** Line `1573` calls `self.decode_frame(out_frame)` whenever `!self.packet_loss`, without checking `&& !self.packet_done`. On the middle packet of a frame spanning 3+ packets (`num_bits_prev_frame >= remaining_packet_bits`, e.g. `0x7FFF >= 16352`), `self.packet_done` is set `true`, yet `decode_frame` runs immediately on the incomplete frame, fails, sets `self.packet_loss = true`, and drops the valid frame. 2. **Stale bit reservoir after `packet_loss`:** When `self.packet_loss` is `true` (either from #1 or on stream start/seek where `StreamDecoder::new` initializes `packet_loss: true` at line `841`), line `1572` still appends `num_bits_prev_frame` bits into `self.reservoir` (advancing `self.reservoir.bit_pos`), and lines `1579-1582` clear `self.num_saved_bits = 0; self.packet_loss = false;` without calling `self.reservoir.reset()` or zeroing `self.frame_offset`, and even when `self.packet_done == true`. On the next packet with `num_bits_prev_frame > 0`, `save_bits(..., true)` appends new bits at the stale `reservoir.bit_pos` (`16352+`), while `decode_frame` reads `frame_offset .. num_saved_bits` from the start of `self.reservoir.buf` (stale bytes from an earlier packet). ### Minimal trigger - Any valid 3-packet XMA1/XMA2 frame (packet 0 ends mid-frame, packet 1 has `num_bits_prev_frame = 0x7FFF`, packet 2 finishes with `num_bits_prev_frame = 500`) → packet 1 prematurely fails `decode_frame`, drops the frame, and leaves stale reservoir state that corrupts packet 2. - Starting or seeking a stream onto a continuation packet (`num_bits_prev_frame = 0x7FFF` with initial `packet_loss = true`) followed by a packet with `num_bits_prev_frame > 0` → decodes stale reservoir bytes instead of incoming bits. ### Suggested fix When `self.packet_loss` is `true`, skip `num_bits_prev_frame` bits via `pgb.skip_bits` instead of calling `save_bits`; guard `decode_frame` at line `1573` with `if !self.packet_done && !self.packet_loss`; and on recovery (`lines 1579-1582`), reset `self.reservoir` and `self.frame_offset = 0`, keeping `self.packet_loss = true` when `self.packet_done` is `true`. --- **AI use:** Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on `930e8b4` before opening.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:42 +00:00
Author
Owner

@⁠tiagonogueiraa-bit on GitHub · 2026-11-24 15:33 UTC · original

claiming: decode_packet_step gets FFmpeg's wmaprodec.c continuation handling: no decode_frame on a packet that only continues a frame, the continuation bits skipped (not saved) after a packet loss, and the reservoir reset on recovery. xma2 is std-only, so I'll check it on the CPU against synthetic multi-packet frames and the retail package's own XMA streams. It comes with a unit test that fails on main.

Written with Claude Code (Claude Opus 5.5).

**@⁠tiagonogueiraa-bit** on GitHub · 2026-11-24 15:33 UTC · [original](https://github.com/xcl-dev/xcl/issues/1391#issuecomment-7136218606) claiming: `decode_packet_step` gets FFmpeg's `wmaprodec.c` continuation handling: no `decode_frame` on a packet that only continues a frame, the continuation bits skipped (not saved) after a packet loss, and the reservoir reset on recovery. `xma2` is std-only, so I'll check it on the CPU against synthetic multi-packet frames and the retail package's own XMA streams. It comes with a unit test that fails on main. Written with Claude Code (Claude Opus 5.5).
Author
Owner

@⁠tiagonogueiraa-bit on GitHub · 2026-11-24 15:34 UTC · original

Withdrawing my claim, with what I found first: this code does not diverge from FFmpeg. decode_packet_step's continuation branch is the same as decode_packet in FFmpeg master's libavcodec/wmaprodec.c (lines 1705–1730 today):

  • remaining_packet_bits clamps num_bits_prev_frame and sets packet_done;
  • save_bits(s, gb, num_bits_prev_frame, 1) runs even with packet_loss set;
  • decode_frame is guarded by !packet_loss only, not by !packet_done;
  • num_saved_bits = 0; packet_loss = 0 on recovery, with no reset of the bit writer.

The decoder's tests compare against FFmpeg's output (decodes_xma2_and_xma1_bitstreams_matching_ffmpeg, cross_packet_frames_and_streaming_chunks_match_single_shot), so changing this branch would move XCL away from its reference.

Both scenarios need a frame spanning three or more 2 KB packets, or a stream that starts on a pure-continuation packet. Whether real XMA streams contain such frames would settle it. If someone has one (a captured SubmitSourceBuffer XMA buffer that loses a frame here), that is the case to fix and test against. Without it, the change would be a guess against the reference implementation.

Written with Claude Code (Claude Opus 5.5).

**@⁠tiagonogueiraa-bit** on GitHub · 2026-11-24 15:34 UTC · [original](https://github.com/xcl-dev/xcl/issues/1391#issuecomment-7136252114) Withdrawing my claim, with what I found first: **this code does not diverge from FFmpeg.** `decode_packet_step`'s continuation branch is the same as `decode_packet` in FFmpeg master's `libavcodec/wmaprodec.c` (lines 1705–1730 today): - `remaining_packet_bits` clamps `num_bits_prev_frame` and sets `packet_done`; - `save_bits(s, gb, num_bits_prev_frame, 1)` runs even with `packet_loss` set; - `decode_frame` is guarded by `!packet_loss` only, not by `!packet_done`; - `num_saved_bits = 0; packet_loss = 0` on recovery, with no reset of the bit writer. The decoder's tests compare against FFmpeg's output (`decodes_xma2_and_xma1_bitstreams_matching_ffmpeg`, `cross_packet_frames_and_streaming_chunks_match_single_shot`), so changing this branch would move XCL away from its reference. Both scenarios need a frame spanning three or more 2 KB packets, or a stream that starts on a pure-continuation packet. Whether real XMA streams contain such frames would settle it. If someone has one (a captured `SubmitSourceBuffer` XMA buffer that loses a frame here), that is the case to fix and test against. Without it, the change would be a guess against the reference implementation. Written with Claude Code (Claude Opus 5.5).
Author
Owner

@⁠microwave805 on GitHub · 2026-11-24 15:37 UTC · original

Good catch — just checked FFmpeg master (libavcodec/wmaprodec.c:1705-1732) directly and confirmed decode_packet_step matches it line for line. The original write-up mistakenly claimed it diverged from FFmpeg. Closing since XCL targets FFmpeg parity here unless a real failing XMA sample turns up. Thanks for checking!

(Verified and drafted with Antigravity / Gemini 4 Neon.)

**@⁠microwave805** on GitHub · 2026-11-24 15:37 UTC · [original](https://github.com/xcl-dev/xcl/issues/1391#issuecomment-7136336151) Good catch — just checked FFmpeg master (libavcodec/wmaprodec.c:1705-1732) directly and confirmed decode_packet_step matches it line for line. The original write-up mistakenly claimed it diverged from FFmpeg. Closing since XCL targets FFmpeg parity here unless a real failing XMA sample turns up. Thanks for checking! *(Verified and drafted with Antigravity / Gemini 4 Neon.)*
This discussion has been locked. Commenting is limited to contributors.
No description provided.