Audio: keep XMA frames split across rolling inputs #443

Open
Quat3rnion wants to merge 3 commits from upstream-pr/443 into main
Quat3rnion commented 2026-11-24 03:28:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Quat3rnion on 2026-11-24T03:28:15Z.


What

XMA frames can straddle the two rolling input buffers the title gives the APU. The continuation lives in the first_frame_bit span of the next packet in the alternate bank. read_frame only saw one slice, so decode treated the bank as spent, switched, and restarted at the alternate bank's first new frame — dropping a valid 512-sample frame and letting the output pump write silence.

This adds a bit-level spanning reader in victory-formats and uses it before switching banks. The decoded frame is kept and the read position moves to the frame after the continuation in the alternate bank.

An AI tool wrote part of this change.

Tested

  • Linux: cargo test --workspace: 166 passed, 0 failed, 3 ignored. Two new hermetic tests — one compares the spanning bit gather against an unsplit frame byte-for-byte, the other drives the CPU output ring with a frame split across two separately allocated banks and asserts the frame is decoded.
  • Windows cross-build: cargo check --target x86_64-pc-windows-gnu --workspace --tests passes. No Windows host here.
  • cargo xtask policy passes (150 files ok). No game files or data from them were added.
  • Not measured: how often GTA VI actually splits frames across banks. The fix is deterministic from the documented packet layout, not tuned to an observed case.

Checklist

  • Workspace tests and Windows cross-check pass.
  • cargo xtask policy passes. No game files or data from them were added.
  • AI authorship is disclosed above.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5465526928 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/443). Originally posted by [Quat3rnion](https://github.com/Quat3rnion) on 2026-11-24T03:28:15Z. --- ## What XMA frames can straddle the two rolling input buffers the title gives the APU. The continuation lives in the `first_frame_bit` span of the next packet in the alternate bank. `read_frame` only saw one slice, so `decode` treated the bank as spent, switched, and restarted at the alternate bank's first new frame — dropping a valid 512-sample frame and letting the output pump write silence. This adds a bit-level spanning reader in `victory-formats` and uses it before switching banks. The decoded frame is kept and the read position moves to the frame after the continuation in the alternate bank. An AI tool wrote part of this change. ## Tested - Linux: `cargo test --workspace`: 166 passed, 0 failed, 3 ignored. Two new hermetic tests — one compares the spanning bit gather against an unsplit frame byte-for-byte, the other drives the CPU output ring with a frame split across two separately allocated banks and asserts the frame is decoded. - Windows cross-build: `cargo check --target x86_64-pc-windows-gnu --workspace --tests` passes. No Windows host here. - `cargo xtask policy` passes (150 files ok). No game files or data from them were added. - Not measured: how often GTA VI actually splits frames across banks. The fix is deterministic from the documented packet layout, not tuned to an observed case. ## Checklist - [x] Workspace tests and Windows cross-check pass. - [x] `cargo xtask policy` passes. No game files or data from them were added. - [x] AI authorship is disclosed above.
mercuryVM commented 2026-11-24 06:14:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T06:14:42Z.

Review: CHANGES_REQUESTED


Tested ab68f34 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (249 passed, 0 failed; the branch is based on an older main).

The idea is right: finish a frame that straddles the two rolling inputs instead of dropping it. But read_frame_spanning reads the head part differently from read_frame.

crates/victory-formats/src/xma.rs, read_frame_spanning: the head bits are copied raw, without the Cursor's packet handling.

  • Cursor::bit jumps over each packet's 32-bit header and over other streams' packets (skip) when it crosses a packet boundary. The new loop copies head[start + i] straight up to head_bits.
  • A frame can be longer than the rest of its packet (lengths go up to 0x7ffe bits, a packet is 16384). If such a frame starts before the head's last packet, or the last packet's skip is non-zero, the copied frame includes header bits as payload, so the decoder gets a corrupted frame. That is audible noise instead of the current dropped frame.
  • head_bits - start >= bits has the same issue, since it measures raw distance including headers.
  • The length is read with the Cursor but the body without it, so the two disagree whenever a header sits in between.

Suggested fix:

  • Gather the head part with a Cursor until it would run past data, counting the payload bits it produced.
  • Take the remaining bits - got bits from the tail starting at bit 32.
  • Require continuation == bits - got. That also catches a tail that doesn't belong to this frame; today only continuation < bits is checked.
  • Add a test where the frame starts in the head's second-to-last packet, so a header lies in the middle.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6034546755 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/443#pullrequestreview-6034546755). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T06:14:42Z. **Review:** CHANGES_REQUESTED --- Tested ab68f34 on Windows 11, RTX 5070, MSVC: `cargo test --workspace --no-fail-fast` passes (249 passed, 0 failed; the branch is based on an older main). The idea is right: finish a frame that straddles the two rolling inputs instead of dropping it. But `read_frame_spanning` reads the head part differently from `read_frame`. **`crates/victory-formats/src/xma.rs`, `read_frame_spanning`: the head bits are copied raw, without the `Cursor`'s packet handling.** - `Cursor::bit` jumps over each packet's 32-bit header and over other streams' packets (`skip`) when it crosses a packet boundary. The new loop copies `head[start + i]` straight up to `head_bits`. - A frame can be longer than the rest of its packet (lengths go up to 0x7ffe bits, a packet is 16384). If such a frame starts before the head's last packet, or the last packet's `skip` is non-zero, the copied frame includes header bits as payload, so the decoder gets a corrupted frame. That is audible noise instead of the current dropped frame. - `head_bits - start >= bits` has the same issue, since it measures raw distance including headers. - The length is read with the `Cursor` but the body without it, so the two disagree whenever a header sits in between. Suggested fix: - Gather the head part with a `Cursor` until it would run past `data`, counting the payload bits it produced. - Take the remaining `bits - got` bits from the tail starting at bit 32. - Require `continuation == bits - got`. That also catches a tail that doesn't belong to this frame; today only `continuation < bits` is checked. - Add a test where the frame starts in the head's second-to-last packet, so a header lies in the middle. AI-assisted (Claude Code).
DaviTostes commented 2026-11-24 06:55:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T03:38:12Z.


Verified ab68f34 on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-formats): 21 passed, 0 failed, 3 ignored.

Not covered: decoding real title audio streams (no game data) and playback under Wine.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121280923 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/443#issuecomment-7121280923). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T03:38:12Z. --- Verified `ab68f34` on **Arch Linux x86_64** (rustc 1.98.1, `cargo test -p victory-formats`): **21 passed, 0 failed, 3 ignored**. Not covered: decoding real title audio streams (no game data) and playback under Wine.
Quat3rnion commented 2026-11-24 08:02:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Quat3rnion on 2026-11-24T07:30:05Z.


Rebased on current main. read_frame_spanning now gathers the head through the packet-aware Cursor, takes exactly the remaining bits from the tail, and requires first_frame_bit to match. Added a regression with a packet header inside the frame. New head f546d65.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7125059734 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/443#issuecomment-7125059734). Originally posted by [Quat3rnion](https://github.com/Quat3rnion) on 2026-11-24T07:30:05Z. --- Rebased on current main. read_frame_spanning now gathers the head through the packet-aware Cursor, takes exactly the remaining bits from the tail, and requires first_frame_bit to match. Added a regression with a packet header inside the frame. New head f546d65.
mercuryVM commented 2026-11-24 12:34:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:34:19Z.

Review: APPROVED


Re-tested f546d65 on Windows 11, RTX 5070, MSVC: cargo test -p victory-formats -p victory-hle 110 passed, including a_spanning_frame_skips_the_packet_header_it_crosses.

This addresses my earlier review. The head part of a spanning frame is now gathered through the packet-aware Cursor, which skips packet headers and other streams' packets. The tail is taken only when its header's continuation count equals the bits still needed (continuation != needed gives None), so a tail from a different frame is no longer glued on.

  • Nit in the new test: the skip range is PACKET..PACKET + HEADER_BITS (packet 1's header), but START is already past it, late in packet 1. The frame actually crosses packet 2's header at 2 * PACKET, which the payload loop then overwrites with frame bits. The test still passes because read_frame(&whole) and read_frame_spanning run the same cursor over the same bytes, but it no longer shows a real header being skipped. Changing the skip to (2 * PACKET..2 * PACKET + HEADER_BITS) keeps packet 2's header intact and makes the assertion mean what its name says.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039267535 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/443#pullrequestreview-6039267535). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T12:34:19Z. **Review:** APPROVED --- Re-tested f546d65 on Windows 11, RTX 5070, MSVC: `cargo test -p victory-formats -p victory-hle` 110 passed, including `a_spanning_frame_skips_the_packet_header_it_crosses`. This addresses my earlier review. The head part of a spanning frame is now gathered through the packet-aware `Cursor`, which skips packet headers and other streams' packets. The tail is taken only when its header's continuation count equals the bits still needed (`continuation != needed` gives `None`), so a tail from a different frame is no longer glued on. - Nit in the new test: the skip range is `PACKET..PACKET + HEADER_BITS` (packet 1's header), but `START` is already past it, late in packet 1. The frame actually crosses packet 2's header at `2 * PACKET`, which the payload loop then overwrites with frame bits. The test still passes because `read_frame(&whole)` and `read_frame_spanning` run the same cursor over the same bytes, but it no longer shows a real header being skipped. Changing the skip to `(2 * PACKET..2 * PACKET + HEADER_BITS)` keeps packet 2's header intact and makes the assertion mean what its name says. AI-assisted (Claude Code).
amorcayReal commented 2026-11-24 21:03:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by amorcayReal on 2026-11-24T20:09:06Z.


I found a remaining boundary case on head 54f4329932, after the packet-aware Cursor changes.

When the first rolling input ends with another stream’s packet, cursor.bit() follows the preceding packet’s skip past the end of the input. The ? then returns None before read_frame_spanning can gather the continuation from the second input.

The attached patch adds a synthetic regression with packets laid out as:

  • First input: A0, then an interleaved B1 packet.
  • Second input: A2, containing the remaining frame bits.
    The same frame is read correctly from the unsplit buffer, but the spanning read fails on the current PR head.

The proposed fix breaks out of the head-gathering loop when the cursor reaches the input boundary, allowing the existing continuation-length check to run.

Verification: the regression fails before the fix. Afterward, the standalone XMA suite reports 8 passed, 2 ignored on Windows, Rust 1.98.1.

No game execution or playback improvement was verified. AI-assisted investigation and implementation using Codex/Luna; synthetic data only.

pr443-xma-fix.diff.txt

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143112462 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/443#issuecomment-7143112462). Originally posted by [amorcayReal](https://github.com/amorcayReal) on 2026-11-24T20:09:06Z. --- I found a remaining boundary case on head 54f4329932b163fe9a83428c1177cd5bc8e3ca50, after the packet-aware Cursor changes. When the first rolling input ends with another stream’s packet, cursor.bit() follows the preceding packet’s skip past the end of the input. The ? then returns None before read_frame_spanning can gather the continuation from the second input. The attached patch adds a synthetic regression with packets laid out as: - First input: A0, then an interleaved B1 packet. - Second input: A2, containing the remaining frame bits. The same frame is read correctly from the unsplit buffer, but the spanning read fails on the current PR head. The proposed fix breaks out of the head-gathering loop when the cursor reaches the input boundary, allowing the existing continuation-length check to run. Verification: the regression fails before the fix. Afterward, the standalone XMA suite reports 8 passed, 2 ignored on Windows, Rust 1.98.1. No game execution or playback improvement was verified. AI-assisted investigation and implementation using Codex/Luna; synthetic data only. [pr443-xma-fix.diff.txt](https://github.com/user-attachments/files/35372536/pr443-xma-fix.diff.txt)
Marvisak commented 2026-11-25 01:07:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:30:00Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-formats/src/xma.rs, crates/victory-hle/src/modules/audio.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148572407 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/443#issuecomment-7148572407). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:30:00Z. --- <!-- victory-triage v1 head=54f4329932b163fe9a83428c1177cd5bc8e3ca50 verdict=needs-rebase at=2026-11-25T01:16:06Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-formats/src/xma.rs`, `crates/victory-hle/src/modules/audio.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
Quat3rnion commented 2026-11-27 02:47:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Quat3rnion on 2026-11-27T02:47:26Z.


@amorcayReal Thanks for the boundary case and the reproducer. New head: the head-gathering loop now stops when the packet cursor leaves the head input, so the existing continuation path runs; I added your regression test. cargo test -p victory-formats xma: 23 passed, 0 failed, 3 ignored (the new case included). Windows GNU workspace check passes. Please re-check.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7208436504 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/443#issuecomment-7208436504). Originally posted by [Quat3rnion](https://github.com/Quat3rnion) on 2026-11-27T02:47:26Z. --- @amorcayReal Thanks for the boundary case and the reproducer. New head: the head-gathering loop now stops when the packet cursor leaves the head input, so the existing continuation path runs; I added your regression test. `cargo test -p victory-formats xma`: 23 passed, 0 failed, 3 ignored (the new case included). Windows GNU workspace check passes. Please re-check.
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/443:upstream-pr/443'
git switch 'upstream-pr/443'

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/443'
git switch 'upstream-pr/443'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/443'
git switch 'upstream-pr/443'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/443'
git switch 'main'
git merge --squash 'upstream-pr/443'
git switch 'main'
git merge --ff-only 'upstream-pr/443'
git switch 'main'
git merge 'upstream-pr/443'
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
Marvisak/VIctory!443
No description provided.