Formats/XMA: treat PacketSkipCount 0xff as end of stream #3223

Closed
github-bot wants to merge 1 commit from github-bot/gh-3223 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 13:39 UTC · original
fuzeofficlal:fix/xma-packet-skip-eos → main · +151 −4 · 1 files · 1 commits · closed without merging

Summary

  • Stop walking an XMA2 stream when a packet header's PacketSkipCount is 0xff (255), the end-of-stream sentinel in the XMA2 packet header specification (XMA2PACKET::PacketSkipCount).
  • Previously Cursor::bit and next_frame added 0xff literally as a 255-packet skip, causing streams ending with skip == 0xff inside buffers longer than 256 packets to jump 256 packets ahead into another interleaved stream's packets instead of terminating.

Changes

  • crates/victory-formats/src/xma.rs:
    • Define END_OF_STREAM = 0xff and add Packet::next(self, index) -> Option<usize>, returning None on 0xff and index + 1 + skip (checked) for 0x00..=0xfe.
    • Use Packet::next in Cursor::bit and next_frame (including the frames == 0 continuation-packet loop). Frames contained inside the final 0xff packet (including one ending right at the packet edge) still decode normally before returning next: None.
    • Add three deterministic unit tests covering multi-frame and packet-boundary 0xff final packets, frames == 0 continuation packets / cursor boundary reads on 0xff, and the maximum non-sentinel skip 0xfe (254) as a negative control.

Verification

  • cargo test -p victory-formats --locked: 49 passed, 0 failed, 5 ignored
  • cargo test -p victory-formats --release --locked: 49 passed, 0 failed, 5 ignored
  • cargo test -p victory-hle --locked audio: 8 passed, 0 failed
  • cargo clippy --workspace --all-targets --locked -- -D warnings: passed
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passed
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: passed
**@⁠fuzeofficlal** on GitHub · 2026-11-26 13:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/3223) `fuzeofficlal:fix/xma-packet-skip-eos` → `main` · +151 −4 · 1 files · 1 commits · closed without merging ## Summary - Stop walking an XMA2 stream when a packet header's `PacketSkipCount` is `0xff` (255), the end-of-stream sentinel in the XMA2 packet header specification (`XMA2PACKET::PacketSkipCount`). - Previously `Cursor::bit` and `next_frame` added `0xff` literally as a 255-packet skip, causing streams ending with `skip == 0xff` inside buffers longer than 256 packets to jump 256 packets ahead into another interleaved stream's packets instead of terminating. ## Changes - **`crates/victory-formats/src/xma.rs`**: - Define `END_OF_STREAM = 0xff` and add `Packet::next(self, index) -> Option<usize>`, returning `None` on `0xff` and `index + 1 + skip` (checked) for `0x00..=0xfe`. - Use `Packet::next` in `Cursor::bit` and `next_frame` (including the `frames == 0` continuation-packet loop). Frames contained inside the final `0xff` packet (including one ending right at the packet edge) still decode normally before returning `next: None`. - Add three deterministic unit tests covering multi-frame and packet-boundary `0xff` final packets, `frames == 0` continuation packets / cursor boundary reads on `0xff`, and the maximum non-sentinel skip `0xfe` (254) as a negative control. ## Verification - `cargo test -p victory-formats --locked`: 49 passed, 0 failed, 5 ignored - `cargo test -p victory-formats --release --locked`: 49 passed, 0 failed, 5 ignored - `cargo test -p victory-hle --locked audio`: 8 passed, 0 failed - `cargo clippy --workspace --all-targets --locked -- -D warnings`: passed - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passed - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: passed
In the XMA2 packet header format (`XMA2PACKET::PacketSkipCount`), values
`0x00..=0xfe` give the number of interleaved packets of other streams to
skip before this stream's next packet, while `0xff` (255) is reserved as
the end-of-stream sentinel indicating that no further packets belong to
this stream.

`Cursor::bit` and `next_frame` previously added `0xff` literally as a
255-packet skip, so in any multi-stream XMA2 buffer longer than 256
packets a stream ending with `skip == 0xff` jumped 256 packets forward
into another stream's packets instead of stopping.

Add `Packet::next` to stop on `END_OF_STREAM` (`0xff`) and use it in
both `Cursor::bit` and `next_frame` while preserving all valid skips
`0x00..=0xfe` and decoding every frame within the final packet.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:41:51 +00:00
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 13:51 UTC · original · reviewed

The code change is tidy. Routing every skip through Packet::next means the three places that used skip (Cursor::bit when leaving a packet, the first hop in next_frame, and its frames == 0 walk) all stop on the sentinel the same way, and the index arithmetic is now overflow-checked as a bonus. I didn't find any other reader of Packet::skip: audio.rs only uses first_frame_bit from xma::packet. The tests cover the right corners: a final packet with two frames, a frame ending exactly on the packet edge, a continuation-only packet, a chain of them, a payload read that would cross the end, and 0xfe still skipping 254 packets.

One thing I'd like to see in the description: where the 0xff = end-of-stream rule comes from. It cites "the XMA2 packet header specification (XMA2PACKET::PacketSkipCount)" but gives no link or quote. Without the sentinel rule, 255 reads like an ordinary skip count. A stream's next packet can legitimately be far away when other streams use more packets in between, so the change rests entirely on that rule. A pointer to the header comment in xma2defs.h, or to another decoder that treats 255 the same way, would let a reviewer confirm it without guessing. If a decoder you checked against was the source, saying which one would help too.

Nit: the "Changes" section has a bullet for xma.rs with nothing after it.

**@⁠AurumCarlos** on GitHub · 2026-11-26 13:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/3223#pullrequestreview-6075272423) · **reviewed** The code change is tidy. Routing every skip through `Packet::next` means the three places that used `skip` (`Cursor::bit` when leaving a packet, the first hop in `next_frame`, and its `frames == 0` walk) all stop on the sentinel the same way, and the index arithmetic is now overflow-checked as a bonus. I didn't find any other reader of `Packet::skip`: `audio.rs` only uses `first_frame_bit` from `xma::packet`. The tests cover the right corners: a final packet with two frames, a frame ending exactly on the packet edge, a continuation-only packet, a chain of them, a payload read that would cross the end, and `0xfe` still skipping 254 packets. One thing I'd like to see in the description: **where the `0xff` = end-of-stream rule comes from.** It cites "the XMA2 packet header specification (`XMA2PACKET::PacketSkipCount`)" but gives no link or quote. Without the sentinel rule, 255 reads like an ordinary skip count. A stream's next packet can legitimately be far away when other streams use more packets in between, so the change rests entirely on that rule. A pointer to the header comment in `xma2defs.h`, or to another decoder that treats 255 the same way, would let a reviewer confirm it without guessing. If a decoder you checked against was the source, saying which one would help too. Nit: the "Changes" section has a bullet for `xma.rs` with nothing after it.
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 13:59 UTC · original

Independent local verification of head c724815 on Windows 11, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-formats --lib -- xma::tests::packet_skip_0xff_stops_continuation_and_spanning_reads --exact — 1 passed, 0 failed.
  • git diff --check origin/main HEAD — clean used a worktree-local CARGO_TARGET_DIR.

Synthetic XMA packet words only; no game audio, extracted assets or DRM material used. AI-assisted (Hy4 flash). GPU/driver N/A (CPU-only formats crate).

**@⁠cloph-dsp** on GitHub · 2026-11-26 13:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/3223#issuecomment-7194114491) Independent local verification of head `c724815` on Windows 11, Rust 1.100.0/MSVC: - `cargo test --locked -p victory-formats --lib -- xma::tests::packet_skip_0xff_stops_continuation_and_spanning_reads --exact` — 1 passed, 0 failed. - `git diff --check origin/main HEAD` — clean used a worktree-local `CARGO_TARGET_DIR`. Synthetic XMA packet words only; no game audio, extracted assets or DRM material used. AI-assisted (Hy4 flash). GPU/driver N/A (CPU-only formats crate).
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 14:12 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64:

  • Treating PacketSkipCount == 0xff as end-of-stream in the XMA2 packet reader while keeping
**@⁠Dukestar69** on GitHub · 2026-11-26 14:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/3223#pullrequestreview-6075585258) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64: - Treating PacketSkipCount == 0xff as end-of-stream in the XMA2 packet reader while keeping
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 14:20 UTC · original

I ran the three new packet-walking tests from c724815 on Windows. With the same tests transplanted onto main (63bcca4), all three fail at the 0xff termination assertions. The 0xfe case successfully reaches packet 255 on both revisions and differs at the following 0xff. On the PR head all three pass, and the formats suite is 49 passed, 5 ignored.

For the source question in the review, vgmstream's XMA2 sample counter gives 0xff special handling as a full-packet skip. Its loop resumes at the next physical packet. FFmpeg's packet parser counts the byte literally. The synthetic tests pin the behavior this PR proposes. The console contract for a packet carrying both 0xff and new frame starts remains unverified in these sources.

Can you link the exact specification passage or share a native decoder result for that case? That would settle the remaining review question.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 14:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/3223#issuecomment-7194671876) I ran the three new packet-walking tests from `c724815` on Windows. With the same tests transplanted onto `main` (`63bcca4`), all three fail at the `0xff` termination assertions. The `0xfe` case successfully reaches packet 255 on both revisions and differs at the following `0xff`. On the PR head all three pass, and the formats suite is 49 passed, 5 ignored. For the source question in the review, [vgmstream's XMA2 sample counter](https://github.com/vgmstream/vgmstream/blob/025860b137a392e017942c078161b67a25bdc016/src/coding/coding_utils.c#L53-L64) gives `0xff` special handling as a full-packet skip. [Its loop](https://github.com/vgmstream/vgmstream/blob/025860b137a392e017942c078161b67a25bdc016/src/coding/coding_utils.c#L87-L99) resumes at the next physical packet. [FFmpeg's packet parser](https://github.com/FFmpeg/FFmpeg/blob/94cafa1805620244885192f3d9d020e2a425099d/libavcodec/xma_parser.c#L34-L42) counts the byte literally. The synthetic tests pin the behavior this PR proposes. The console contract for a packet carrying both `0xff` and new frame starts remains unverified in these sources. Can you link the exact specification passage or share a native decoder result for that case? That would settle the remaining review question.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 14:25 UTC · original

I ran the three packet-skip tests on main 63bcca4 with only your test hunk applied and on exact head c724815. The tests went from 0/3 to 3/3 on Windows/MSVC. On main the final packet in the two-frame case incorrectly reports packet 256 as its next frame. The 0xfe control still reaches packet 255 and preserves the spanning payload with the patch. The full formats lib suite passed 49 (5 ignored), the HLE audio subset passed 8/8, and strict formats Clippy passed. Fixtures were synthetic.

Could you link the XMA2PACKET::PacketSkipCount reference or share a hardware result for a full-skip packet followed by a normal packet in the same input buffer? The patches and tests establish the behavior change. The end-of-stream rule is the part that needs a format or hardware reference. Xenia Canary advances to the next sequential packet for a full-skip packet at the current read position and stops its continuation walk on that same value. vgmstream describes 0xff as a full skip with no new frame starts, where a previous frame can end. Xenia current packet, Xenia continuation, vgmstream packet header

AI-assisted with Codex.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 14:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/3223#issuecomment-7194797575) I ran the three packet-skip tests on main `63bcca4` with only your test hunk applied and on exact head `c724815`. The tests went from **0/3 to 3/3** on Windows/MSVC. On main the final packet in the two-frame case incorrectly reports packet 256 as its next frame. The `0xfe` control still reaches packet 255 and preserves the spanning payload with the patch. The full formats lib suite passed **49** (5 ignored), the HLE audio subset passed **8/8**, and strict formats Clippy passed. Fixtures were synthetic. Could you link the `XMA2PACKET::PacketSkipCount` reference or share a hardware result for a full-skip packet followed by a normal packet in the same input buffer? The patches and tests establish the behavior change. The end-of-stream rule is the part that needs a format or hardware reference. Xenia Canary advances to the next sequential packet for a full-skip packet at the current read position and stops its continuation walk on that same value. vgmstream describes `0xff` as a full skip with no new frame starts, where a previous frame can end. [Xenia current packet](https://github.com/xenia-canary/xenia-canary/blob/2da25198960769ccc2baa97529f77485351732fe/src/xenia/apu/xma_context_new.cc#L443-L458), [Xenia continuation](https://github.com/xenia-canary/xenia-canary/blob/2da25198960769ccc2baa97529f77485351732fe/src/xenia/apu/xma_context_new.cc#L743-L750), [vgmstream packet header](https://github.com/vgmstream/vgmstream/blob/025860b137a392e017942c078161b67a25bdc016/src/coding/coding_utils.c#L52-L61) AI-assisted with Codex.
github-bot closed this pull request 2026-11-26 16:51:46 +00:00
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 16:46 UTC · original

Thank you both (@⁠AurumCarlos and @⁠andrewpozdnakov7-jpg) for pressing for a primary source here.

After re-checking Microsoft's xma2defs.h (GetXmaFrameBitPosition / GetLastXmaFrameBitPosition), hcs64/xma_parse, FFmpeg (xma_parser.c / wmaprodec.c), vgmstream (coding_utils.c), and Xenia (xma_context_new.cc / upstream xma_context.cc), I cannot substantiate PacketSkipCount == 0xff as a documented end-of-stream sentinel after decoding new frames in that packet:

  • xma2defs.h, xma_parse, FFmpeg, and upstream Xenia all treat PacketSkipCount literally (skip + 1) and only special-case 0x7FFF for FrameOffsetInBits / LengthInBits.
  • vgmstream and Xenia Canary (470e62af) special-case 0xFF as a full packet skip (assuming no new frames start in the packet and advancing to the next physical packet), which is a different contract from decoding frames in a 0xFF packet and then terminating the stream.

Without a primary specification or hardware capture proving EOS semantics for 0xFF, this change shouldn't land. Closing this PR rather than leaving an unverified format assumption open.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 16:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/3223#issuecomment-7198072381) Thank you both (@⁠AurumCarlos and @⁠andrewpozdnakov7-jpg) for pressing for a primary source here. After re-checking Microsoft's `xma2defs.h` (`GetXmaFrameBitPosition` / `GetLastXmaFrameBitPosition`), `hcs64/xma_parse`, FFmpeg (`xma_parser.c` / `wmaprodec.c`), `vgmstream` (`coding_utils.c`), and Xenia (`xma_context_new.cc` / upstream `xma_context.cc`), I cannot substantiate `PacketSkipCount == 0xff` as a documented end-of-stream sentinel after decoding new frames in that packet: - `xma2defs.h`, `xma_parse`, FFmpeg, and upstream Xenia all treat `PacketSkipCount` literally (`skip + 1`) and only special-case `0x7FFF` for `FrameOffsetInBits` / `LengthInBits`. - `vgmstream` and Xenia Canary (`470e62af`) special-case `0xFF` as a *full packet skip* (assuming no new frames start in the packet and advancing to the next physical packet), which is a different contract from decoding frames in a `0xFF` packet and then terminating the stream. Without a primary specification or hardware capture proving EOS semantics for `0xFF`, this change shouldn't land. Closing this PR rather than leaving an unverified format assumption open.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3223
No description provided.