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

Closed
opened 2026-11-24 15:31:24 +00:00 by microwave805 · 3 comments
microwave805 commented 2026-11-24 15:31:24 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by microwave805 on 2026-11-24T15:31:24Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6585384880 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/1391). Originally posted by [microwave805](https://github.com/microwave805) on 2026-11-24T15:31:24Z. --- 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.
tiagonogueiraa-bit commented 2026-11-24 16:07:07 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by tiagonogueiraa-bit on 2026-11-24T15:33:08Z.


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).

<!-- greyfinch-sync:ivall/xcl:comment:7136218606 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/1391#issuecomment-7136218606). Originally posted by [tiagonogueiraa-bit](https://github.com/tiagonogueiraa-bit) on 2026-11-24T15:33:08Z. --- 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 commented 2026-11-24 16:07:08 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by tiagonogueiraa-bit on 2026-11-24T15:34:19Z.


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).

<!-- greyfinch-sync:ivall/xcl:comment:7136252114 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/1391#issuecomment-7136252114). Originally posted by [tiagonogueiraa-bit](https://github.com/tiagonogueiraa-bit) on 2026-11-24T15:34:19Z. --- 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).
microwave805 commented 2026-11-24 16:07:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by microwave805 on 2026-11-24T15:37:21Z.


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.)

<!-- greyfinch-sync:ivall/xcl:comment:7136336151 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/1391#issuecomment-7136336151). Originally posted by [microwave805](https://github.com/microwave805) on 2026-11-24T15:37:21Z. --- 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.)*
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl#1381
No description provided.