XAudio2: XMA2 decoder prematurely decodes 3+-packet frames and leaves stale reservoir bits after packet loss #1391
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl#1391
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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'swmaprodec.c):1573callsself.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_doneis settrue, yetdecode_frameruns immediately on the incomplete frame, fails, setsself.packet_loss = true, and drops the valid frame.packet_loss: Whenself.packet_lossistrue(either from #1 or on stream start/seek whereStreamDecoder::newinitializespacket_loss: trueat line841), line1572still appendsnum_bits_prev_framebits intoself.reservoir(advancingself.reservoir.bit_pos), and lines1579-1582clearself.num_saved_bits = 0; self.packet_loss = false;without callingself.reservoir.reset()or zeroingself.frame_offset, and even whenself.packet_done == true. On the next packet withnum_bits_prev_frame > 0,save_bits(..., true)appends new bits at the stalereservoir.bit_pos(16352+), whiledecode_framereadsframe_offset .. num_saved_bitsfrom the start ofself.reservoir.buf(stale bytes from an earlier packet).Minimal trigger
num_bits_prev_frame = 0x7FFF, packet 2 finishes withnum_bits_prev_frame = 500) → packet 1 prematurely failsdecode_frame, drops the frame, and leaves stale reservoir state that corrupts packet 2.num_bits_prev_frame = 0x7FFFwith initialpacket_loss = true) followed by a packet withnum_bits_prev_frame > 0→ decodes stale reservoir bytes instead of incoming bits.Suggested fix
When
self.packet_lossistrue, skipnum_bits_prev_framebits viapgb.skip_bitsinstead of callingsave_bits; guarddecode_frameat line1573withif !self.packet_done && !self.packet_loss; and on recovery (lines 1579-1582), resetself.reservoirandself.frame_offset = 0, keepingself.packet_loss = truewhenself.packet_doneistrue.AI use: Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on
930e8b4before opening.@tiagonogueiraa-bit on GitHub · 2026-11-24 15:33 UTC · original
claiming:
decode_packet_stepgets FFmpeg'swmaprodec.ccontinuation handling: nodecode_frameon a packet that only continues a frame, the continuation bits skipped (not saved) after a packet loss, and the reservoir reset on recovery.xma2is 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: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 asdecode_packetin FFmpeg master'slibavcodec/wmaprodec.c(lines 1705–1730 today):remaining_packet_bitsclampsnum_bits_prev_frameand setspacket_done;save_bits(s, gb, num_bits_prev_frame, 1)runs even withpacket_lossset;decode_frameis guarded by!packet_lossonly, not by!packet_done;num_saved_bits = 0; packet_loss = 0on 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
SubmitSourceBufferXMA 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 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.)