Formats/xma: fix quant_step escape, missing scale factors, empty subframes, and frame truncation #876

Open
adritoyalser wants to merge 1 commit from upstream-pr/877 into main
adritoyalser commented 2026-11-25 11:34:13 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by adritoyalser on 2026-11-25T11:34:13Z.


Summary

Fixes four bitstream handling bugs in crates/victory-formats/src/xma.rs (Decoder, disjoint from #627 which only touches next_frame):

  1. quant_step escape loop off-by-one and truncation sign inversion (subframe):
    • while b.pos + 5 < b.end skipped a valid 5-bit escape when exactly 5 bits remained (b.pos + 5 == b.end).
    • On a truncated escape (b.pos + 5 > b.end) with initial step == -32, the loop did not run and quant_step += -(0 + -32) added +32 instead of failing.
    • Now returns Err(Error("quantisation step")) whenever fewer than 5 bits remain before a terminating < 31 group, and uses saturating arithmetic so corrupted all-1s frames cannot overflow i32.
  2. Missing scale factors after a silent subframe 0 (scale_factors):
    • If subframe 0 had no coefficients (reuse_sf == false) and a subsequent subframe cleared the "new scale factors" bit, scale_factors previously succeeded with stale/uninitialized scale factors.
    • Now returns Err(Error("missing scale factors")) when !ch.reuse_sf after the decode branch (matching FFmpeg's wmaprodec.c), checks b.overread(), and uses saturating arithmetic in scale-factor updates and dequantization exponents (exp).
  3. Empty subframe spin (tile_header):
    • Immediately rejects a subframe with no channel selected (Err(Error("empty subframe"))) instead of spinning until bitstream overread.
  4. Over-long bits argument (decode):
    • Rejects bits > data.len() * 8 with Err(Error("frame truncated")) while still outputting a silent frame to preserve stream timing.

Verification

  • Added 4 unit tests in crates/victory-formats/src/xma.rs:
    • tile_header_rejects_empty_subframe_without_spinning
    • scale_factors_rejects_reuse_without_prior_factors_after_silent_subframe
    • quant_step_escape_rejects_truncation_and_reads_exact_tail
    • decode_rejects_bit_length_past_buffer_and_keeps_timing
  • cargo 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).
<!-- greyfinch-sync:fxceless/victory-fork:issue:6603552968 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/877). Originally posted by [adritoyalser](https://github.com/adritoyalser) on 2026-11-25T11:34:13Z. --- ### Summary Fixes four bitstream handling bugs in `crates/victory-formats/src/xma.rs` (`Decoder`, disjoint from #627 which only touches `next_frame`): 1. **`quant_step` escape loop off-by-one and truncation sign inversion (`subframe`)**: - `while b.pos + 5 < b.end` skipped a valid 5-bit escape when exactly 5 bits remained (`b.pos + 5 == b.end`). - On a truncated escape (`b.pos + 5 > b.end`) with initial `step == -32`, the loop did not run and `quant_step += -(0 + -32)` added `+32` instead of failing. - Now returns `Err(Error("quantisation step"))` whenever fewer than 5 bits remain before a terminating `< 31` group, and uses saturating arithmetic so corrupted all-1s frames cannot overflow `i32`. 2. **Missing scale factors after a silent subframe 0 (`scale_factors`)**: - If subframe 0 had no coefficients (`reuse_sf == false`) and a subsequent subframe cleared the "new scale factors" bit, `scale_factors` previously succeeded with stale/uninitialized scale factors. - Now returns `Err(Error("missing scale factors"))` when `!ch.reuse_sf` after the decode branch (matching FFmpeg's `wmaprodec.c`), checks `b.overread()`, and uses saturating arithmetic in scale-factor updates and dequantization exponents (`exp`). 3. **Empty subframe spin (`tile_header`)**: - Immediately rejects a subframe with no channel selected (`Err(Error("empty subframe"))`) instead of spinning until bitstream overread. 4. **Over-long `bits` argument (`decode`)**: - Rejects `bits > data.len() * 8` with `Err(Error("frame truncated"))` while still outputting a silent frame to preserve stream timing. ### Verification - Added 4 unit tests in `crates/victory-formats/src/xma.rs`: - `tile_header_rejects_empty_subframe_without_spinning` - `scale_factors_rejects_reuse_without_prior_factors_after_silent_subframe` - `quant_step_escape_rejects_truncation_and_reads_exact_tail` - `decode_rejects_bit_length_past_buffer_and_keeps_timing` - `cargo 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).
MamaLeVrai commented 2026-11-25 13:05:59 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by MamaLeVrai on 2026-11-25T12:23:12Z.


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 290932f into main f85101b in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings fails with exactly the errors main itself has on this machine (placed_filler_twins, depth_written, a sort; PR #841 fixes them), also with --all-features; this PR adds none. cargo test --workspace --locked: run 1: stopped at indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields (the one that fails on main itself most runs here (PR #849 and #847 fix its assertion)); run 2: 559 passed, 0 failed, 11 ignored (main alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7162302695 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/877#issuecomment-7162302695). Originally posted by [MamaLeVrai](https://github.com/MamaLeVrai) on 2026-11-25T12:23:12Z. --- 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 `290932f` into `main` `f85101b` in a throwaway tree (no conflict). `cargo clippy --workspace --all-targets --locked -- -D warnings` fails with exactly the errors `main` itself has on this machine (`placed_filler_twins`, `depth_written`, a sort; PR #841 fixes them), also with `--all-features`; this PR adds none. `cargo test --workspace --locked`: run 1: stopped at `indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields` (the one that fails on `main` itself most runs here (PR #849 and #847 fix its assertion)); run 2: 559 passed, 0 failed, 11 ignored (`main` alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data. AI-assisted (Claude Code); no game data.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/877:upstream-pr/877'
git switch 'upstream-pr/877'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/877'
git switch 'upstream-pr/877'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/877'
git switch 'upstream-pr/877'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/877'
git switch 'main'
git merge --squash 'upstream-pr/877'
git switch 'main'
git merge --ff-only 'upstream-pr/877'
git switch 'main'
git merge 'upstream-pr/877'
git push origin 'main'
Sign in to join this conversation.
No reviewers
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
fxceless/victory-fork!876
No description provided.