tools/awc_opus: honor hdr24 block_samples budget, interleaved skip, and deficit continuation #1072

Merged
leonida/mandy
Elijah-J merged 3 commits from fix/awc-hdr24-chunk-budget-skip into main 2026-11-28 13:09:29 +00:00

Pull request successfully merged and closed

Elijah-J commented 2026-11-24 17:48:45 +00:00

Claimed in the coordination issue as: AWC-HDR24-BUDGET (posted after the draft was opened)

What and why

Streamed music (0x48) chunks carry a 24-byte per-channel header with (subblocks, skip_samples, block_samples) at +4..+16. The sampled stereo banks use interleaved counts (2× per-channel 48 kHz frames), while the exporter also accepts a per-channel skip that verifies an exact packet overlap.

Previously music_channels() only checked skip as a per-channel frame count and ignored block_samples (+12), which rejected multi-stem/multi-block streamed music banks that:

  • pad a chunk's trailing 2048-byte subblock with extra Opus packets past block_samples / packet_channels,
  • store skip_samples in interleaved stereo units (2 * frames), or
  • stop a chunk short of its block_samples budget so the deficit carries into the next chunk's skip_samples.

Changes:

  • tools/awc_opus.py: read (count, skip, block_samples) from hdr24 +4..+16; trim trailing subblock padding packets past block_samples // pch; match interleaved (skip // pch) and per-channel (skip) repeated prefixes; handle exact-boundary deficit continuation when a preceding block stopped short of its block_samples budget while continuing to fail closed on partial/corrupt skips or odd block_samples.
  • docs/audio.md: document +8 (skip_samples), +12 (block_samples), and the conditions for carry-over recovery under ### Streamed music (0x48).
  • Metadata-only reads keep block locations and only the packet buffers needed for the current overlap check. On an 8.6 MB test bank, traced temporary allocations peaked at 380 KB, down from 9.1 MB in the first draft.
  • tests/test_awc_opus.py: add synthetic unit tests covering block-budget padding trimming, interleaved stereo skip deduplication, short-block deficit continuation, and partial-boundary fail-closed rejection.

Tested

Synthetic unit tests on macOS:

  • python3 -I tests/test_awc_opus.py -v — 64/64 OK
  • python3 -I tests/test_awc_manifest.py -v — 23/23 OK
  • python3 -I tests/test_text_write_line_endings.py -v — 3/3 OK
  • python3 -m compileall -q make_game.py tools tests — exit 0

A separate read-only Xbox Series 1.0.2.1 check recovered four banks (11 stems) rejected by the previous overlap-only extractor. FFmpeg decoded every stem to its descriptor length after Ogg end trimming. Three carry-over transitions exercised a zero net skip. Positive net skip was tested synthetically.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — audio tool/docs only)
Claimed in the coordination issue as: [AWC-HDR24-BUDGET](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7140624662) (posted after the draft was opened) ### What and why Streamed music (`0x48`) chunks carry a 24-byte per-channel header with `(subblocks, skip_samples, block_samples)` at `+4..+16`. The sampled stereo banks use interleaved counts (2× per-channel 48 kHz frames), while the exporter also accepts a per-channel skip that verifies an exact packet overlap. Previously `music_channels()` only checked `skip` as a per-channel frame count and ignored `block_samples` (`+12`), which rejected multi-stem/multi-block streamed music banks that: - pad a chunk's trailing 2048-byte subblock with extra Opus packets past `block_samples / packet_channels`, - store `skip_samples` in interleaved stereo units (`2 * frames`), or - stop a chunk short of its `block_samples` budget so the deficit carries into the next chunk's `skip_samples`. Changes: - `tools/awc_opus.py`: read `(count, skip, block_samples)` from `hdr24` `+4..+16`; trim trailing subblock padding packets past `block_samples // pch`; match interleaved (`skip // pch`) and per-channel (`skip`) repeated prefixes; handle exact-boundary deficit continuation when a preceding block stopped short of its `block_samples` budget while continuing to fail closed on partial/corrupt skips or odd `block_samples`. - `docs/audio.md`: document `+8` (`skip_samples`), `+12` (`block_samples`), and the conditions for carry-over recovery under `### Streamed music (0x48)`. - Metadata-only reads keep block locations and only the packet buffers needed for the current overlap check. On an 8.6 MB test bank, traced temporary allocations peaked at 380 KB, down from 9.1 MB in the first draft. - `tests/test_awc_opus.py`: add synthetic unit tests covering block-budget padding trimming, interleaved stereo skip deduplication, short-block deficit continuation, and partial-boundary fail-closed rejection. ### Tested Synthetic unit tests on macOS: - `python3 -I tests/test_awc_opus.py -v` — 64/64 OK - `python3 -I tests/test_awc_manifest.py -v` — 23/23 OK - `python3 -I tests/test_text_write_line_endings.py -v` — 3/3 OK - `python3 -m compileall -q make_game.py tools tests` — exit 0 A separate read-only Xbox Series 1.0.2.1 check recovered four banks (11 stems) rejected by the previous overlap-only extractor. FFmpeg decoded every stem to its descriptor length after Ogg end trimming. Three carry-over transitions exercised a zero net skip. Positive net skip was tested synthetically. GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. *(n/a — audio tool/docs only)*
copilot-pull-request-reviewer[bot] reviewed 2026-11-24 18:43:23 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
> [!WARNING] > Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a `copilot-code-review.yml` file specifying one with the `runs-on` attribute. See the [docs](https://gh.io/AA11tgch) for more details. <!-- ccr-overview-v2 --> ## Copilot review overview <details open> <summary><strong>3 open issues</strong></summary> - <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> [`prev_deficit` is only updated when `kept` is non-empty. If a chunk has a positive `block_samples`…](https://github.com/ubiquitarianism/leonida/pull/1072#discussion_r4684844022) · New - <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> [`opus_packets()` always copies packet payloads into new `bytes`. In `collect_packets=False` mode,…](https://github.com/ubiquitarianism/leonida/pull/1072#discussion_r4684844106) · New - <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> [`_trim_packets_to_budget` computes `opus_samples()` for every packet twice (once in `sum(...)`,…](https://github.com/ubiquitarianism/leonida/pull/1072#discussion_r4684844181) · New </details> <details> <summary><strong>What changed in this PR</strong></summary> Updates streamed-music (`0x48`) parsing to correctly interpret HDR24 per-channel headers, honoring `block_samples` budgets and stereo-interleaved `skip_samples`, including deficit carry-over between chunks. **Changes:** - Parse `(subblocks, skip_samples, block_samples)` from the per-channel 24-byte headers and trim packets beyond `block_samples`. - Accept both interleaved (`skip // pch`) and per-channel skip when verifying repeated-prefix overlaps, plus deficit continuation when a prior chunk undershot its budget. - Add unit tests for padding-trim, stereo interleaved skip, deficit continuation, and metadata-mode memory behavior; update audio format documentation. | File | Description | | ---- | ----------- | | tools/​awc_opus.py | Implements budget trimming, interleaved/per-channel skip matching, deficit continuation, and packet-range iteration. | | tests/​test_awc_opus.py | Adds synthetic tests for the new trimming/skip/deficit behaviors plus a metadata allocation check. | | docs/​audio.md | Documents HDR24 `skip_samples` / `block_samples` semantics and deficit carry-over recovery rules. | </details> <sub>Review effort: Lite</sub>
Elijah-J commented 2026-11-24 18:54:00 +00:00

I addressed the three review notes in c640232: empty budgeted chunks now update skip state, metadata-only scans avoid copying packet payloads, and budget trimming stops once the target is reached. All 93 focused tests passed, and FFmpeg again matched the declared lengths of all 11 sampled stems. The current CI and No Game Data runs are waiting for maintainer approval. CI · No Game Data

I addressed the three review notes in `c640232`: empty budgeted chunks now update skip state, metadata-only scans avoid copying packet payloads, and budget trimming stops once the target is reached. All 93 focused tests passed, and FFmpeg again matched the declared lengths of all 11 sampled stems. The current CI and No Game Data runs are waiting for maintainer approval. [CI](https://github.com/ubiquitarianism/leonida/actions/runs/45464288114) · [No Game Data](https://github.com/ubiquitarianism/leonida/actions/runs/45464288155)
MoonEyes commented 2026-11-28 13:09:23 +00:00

Red/green: tests/test_awc_opus.py 6 failed on main, 67 passed with the PR.
Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python.
AI-assisted (Claude Code, Claude Opus 5.5)

Red/green: tests/test_awc_opus.py 6 failed on main, 67 passed with the PR. Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No description provided.