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

Closed
fuzeofficlal wants to merge 1 commit from upstream-pr/3223 into main
fuzeofficlal commented 2026-11-26 13:39:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T13:39:08Z.


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
<!-- greyfinch-sync:Marvisak/VIctory:issue:6627879601 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3223). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T13:39:08Z. --- ## 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
AurumCarlos commented 2026-11-26 13:51:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T13:51:10Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6075272423 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3223#pullrequestreview-6075272423). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-26T13:51:10Z. **Review:** COMMENTED --- 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.
cloph-dsp commented 2026-11-26 14:01:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T13:59:16Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7194114491 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3223#issuecomment-7194114491). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T13:59:16Z. --- 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).
Dukestar69 commented 2026-11-26 14:12:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T14:12:27Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64:

  • Treating PacketSkipCount == 0xff as end-of-stream in the XMA2 packet reader while keeping
<!-- greyfinch-sync:Marvisak/VIctory:review:6075585258 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3223#pullrequestreview-6075585258). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T14:12:27Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Treating PacketSkipCount == 0xff as end-of-stream in the XMA2 packet reader while keeping
andrewpozdnakov7-jpg commented 2026-11-26 15:02:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T14:20:44Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7194671876 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3223#issuecomment-7194671876). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T14:20:44Z. --- 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.
andrewpozdnakov7-jpg commented 2026-11-26 15:02:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T14:25:19Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7194797575 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3223#issuecomment-7194797575). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T14:25:19Z. --- 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.
fuzeofficlal commented 2026-11-26 16:46:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T16:46:13Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7198072381 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3223#issuecomment-7198072381). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T16:46:13Z. --- 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

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
Marvisak/VIctory!3221
No description provided.