Repository navigation
Formats/xma: fix quant_step escape, missing scale factors, empty subframes, and frame truncation - #877
Formats/xma: fix quant_step escape, missing scale factors, empty subframes, and frame truncation#877adritoyalser wants to merge 1 commit into
Conversation
…rames, and frame truncation
Four bitstream edge cases in `victory_formats::xma::Decoder`:
- `subframe` quant_step escape: `while b.pos + 5 < b.end` missed a valid
5-bit escape when `b.pos + 5 == b.end`, and when truncated before reading
any 5-bit group (or ending on `31`) fell through with the stale initial
`step == -32`, adding `-(0 + -32) = +32` instead of failing. Check
`b.pos + 5 > b.end` inside the loop and return
`Err(Error("quantisation step"))` on truncation or non-termination, with
saturating accumulation.
- `scale_factors`: when subframe 0 had `transmit_coefs == false` (leaving
`ch.reuse_sf == false`) and a later subframe had the reuse flag clear,
`scale_factors` returned `Ok(())` using stale/uninitialized scale factors.
Check `b.overread()` inside `scale_factors`, reject `!ch.reuse_sf`
afterwards (matching FFmpeg's `wmaprodec.c`), and use saturating
arithmetic in scale-factor accumulation and dequantization exponents.
- `tile_header`: reject a subframe assigned to no channel (`!contains`)
immediately with `Err(Error("empty subframe"))` instead of spinning
through the remaining bits until overread.
- `decode`: reject `bits > data.len() * 8` with `Err(Error("frame truncated"))`
(still outputting silence to preserve stream timing) instead of silently
zero-padding past `data`.
…s (fork fxceless#877, adritoyalser) Ports the parts of fxceless/victory-fork fxceless#877 that still applied here; the empty-subframe case is already refused by `no channel in subframe`. - `decode` returns `frame truncated` when the bit count is past the buffer (it used to clamp the read end and report success on zeros), still emitting the silent frame that keeps the stream's timing. - The quantisation-step escape needs a full 5 bits; the old loop needed 6, and on a truncated escape it added +32 from the stale -32 step. - `scale_factors` refuses a subframe that reuses scale factors it never decoded (after a silent subframe 0), and any overread there. - Saturating arithmetic on scale factors, quant step and the dequantisation exponent, so corrupted frames cannot overflow i32. Proof: the three new tests fail on the old code (`Ok(())` vs `missing scale factors`; `subframe overread` vs `quantisation step`; `Ok(())` vs `frame truncated`) and pass here.
|
Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head AI-assisted (Claude Code); no game data. |
Summary
Fixes four bitstream handling bugs in
crates/victory-formats/src/xma.rs(Decoder, disjoint from #627 which only touchesnext_frame):quant_stepescape loop off-by-one and truncation sign inversion (subframe):while b.pos + 5 < b.endskipped a valid 5-bit escape when exactly 5 bits remained (b.pos + 5 == b.end).b.pos + 5 > b.end) with initialstep == -32, the loop did not run andquant_step += -(0 + -32)added+32instead of failing.Err(Error("quantisation step"))whenever fewer than 5 bits remain before a terminating< 31group, and uses saturating arithmetic so corrupted all-1s frames cannot overflowi32.scale_factors):reuse_sf == false) and a subsequent subframe cleared the "new scale factors" bit,scale_factorspreviously succeeded with stale/uninitialized scale factors.Err(Error("missing scale factors"))when!ch.reuse_sfafter the decode branch (matching FFmpeg'swmaprodec.c), checksb.overread(), and uses saturating arithmetic in scale-factor updates and dequantization exponents (exp).tile_header):Err(Error("empty subframe"))) instead of spinning until bitstream overread.bitsargument (decode):bits > data.len() * 8withErr(Error("frame truncated"))while still outputting a silent frame to preserve stream timing.Verification
crates/victory-formats/src/xma.rs:tile_header_rejects_empty_subframe_without_spinningscale_factors_rejects_reuse_without_prior_factors_after_silent_subframequant_step_escape_rejects_truncation_and_reads_exact_taildecode_rejects_bit_length_past_buffer_and_keeps_timingcargo test -q -p victory-formats --lib --locked xma: 9 passed, 0 failed, 2 ignored.cargo clippy -q -p victory-formats --all-targets --locked -- -D warnings: clean (0 warnings).