xapu: optional Opus packet decode behind X2W_WITH_OPUS #2318

Merged
NiiRoZz merged 4 commits from upstream-pr/2326 into main 2026-11-25 12:46:45 +00:00
NiiRoZz commented 2026-11-25 12:26:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T12:26:06Z.


Refs #1145, #1887

What

Optional Opus packet decoding in the xapu.dll stand-in, as agreed in #1145.

  • New CMake option X2W_WITH_OPUS, OFF by default. When on, find_package(Opus CONFIG REQUIRED) links Opus::opus into startup_stub_xapu. The default configure and CI do not depend on libopus.
  • With X2W_AUDIO_PERMISSIVE=1 and X2W_XAPU_TYPED_COMPLETIONS=1, a type 3 command decodes one Opus packet into the caller's output (interleaved float). The result carries the decoded byte count at +0x34.
    • Command fields: +0x1C rate (float), +0x24 frames, +0x28/+0x2C packet bytes and pointer, +0x34/+0x38 output bytes and pointer.
    • Each slot has its own decoder. Type 5 resets it; type 2 and XApuDisconnect free it.
  • Refusals queue no result, leave the output untouched and keep the slot's decoder:
    • E_INVALIDARG for a bad rate, channel count, frames, packet, output or slot;
    • E_FAIL for a packet libopus rejects;
    • E_POINTER for a command shorter than 0x40 bytes;
    • E_OUTOFMEMORY for a full queue, checked before any decode, reset or close.
    • Decoding goes through a scratch buffer, and the output is written only after the result is queued.
  • A command with no packet (null pointer and 0 bytes) completes with 0 bytes, as before. So startup_xapu_queue is unchanged and passes in both builds.
  • Without the option, behaviour is unchanged apart from the queue entry growing to 12 bytes (cap 192 KiB per connection; docs updated).
  • Tested with libopus 1.5.2. Its BSD-3-Clause notice is in src/startup_stubs/THIRD_PARTY_NOTICES.md. The repo contains no libopus source or binary.

Tests

startup_xapu_opus is registered only when X2W_WITH_OPUS=ON. It encodes generated 1 kHz tones with the same libopus; there are no binary fixtures. It checks:

  • exact byte counts: 3840 (48 kHz mono), 7680 (stereo), 1920 (a 10 ms packet in a 20 ms buffer), 1280 (16 kHz);
  • PCM bit-exact against a reference decoder, with guard floats past the decoded frames untouched;
  • a malformed packet, giving E_FAIL with the output and queue untouched;
  • each refusal, with the output untouched and nothing queued;
  • a full queue (16384 entries), after which the slot's stream continues;
  • reset and close, each separately starting a fresh decoder, with a control showing that a used decoder differs;
  • a rate change on a live slot, and a failed decode at a new rate that keeps the old decoder;
  • no decode when typed completions are off.

Environment: Windows 11 (26300), MSVC 19.44, Ninja Debug, commit ccde4c1e (based on 31f46857; merges cleanly into current main).

cmake -S . -B out/build/def -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build out/build/def --target startup_stub_xapu startup_xapu_queue_check
ctest --test-dir out/build/def --no-tests=error -R '^startup_xapu_'
# startup_xapu_queue: Passed (1/1)

cmake -S . -B out/build/opus -G Ninja -DCMAKE_BUILD_TYPE=Debug -DX2W_WITH_OPUS=ON -DOpus_DIR=<opus 1.5.2 install>/lib/cmake/Opus
cmake --build out/build/opus --target startup_stub_xapu startup_xapu_queue_check startup_xapu_opus_check
ctest --test-dir out/build/opus --no-tests=error -R '^startup_xapu_' -V
# startup_xapu_queue: Passed; startup_xapu_opus: "PASS: startup_xapu_opus (libopus 1.5.2)" (2/2)

libopus 1.5.2 was built from opus-1.5.2.tar.gz (SHA-256 65c1d2f7…7ce1) with -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Debug>:Debug>, because the stand-ins link the static runtime.

Negative controls: each mutant was built, then reverted.

  • Reporting the output capacity instead of the decoded count fails the 10 ms case: 6 failures.
  • Making close keep the decoder fails "decode after close matches a fresh decoder".
  • Decoding before the queue-room check fails "after the full-queue refusals slot 7 continues its stream".

Also passing: tests/docs_links_check.py, tests/bidi_controls_check.py, git diff --check. Not run: Release builds.

Local observation only

With a local libopus build of this decode path, I heard the decoded UI sounds through the desktop audio sink. This is a local observation only. It does not show that a title's audio works, and full-game startup remains unverified.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6604439142 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2326). Originally posted by [NiiRoZz](https://github.com/NiiRoZz) on 2026-11-25T12:26:06Z. --- Refs #1145, #1887 ## What Optional Opus packet decoding in the `xapu.dll` stand-in, as agreed in #1145. - New CMake option `X2W_WITH_OPUS`, **OFF by default**. When on, `find_package(Opus CONFIG REQUIRED)` links `Opus::opus` into `startup_stub_xapu`. The default configure and CI do not depend on libopus. - With `X2W_AUDIO_PERMISSIVE=1` and `X2W_XAPU_TYPED_COMPLETIONS=1`, a type 3 command decodes one Opus packet into the caller's output (interleaved float). The result carries the decoded byte count at `+0x34`. - Command fields: `+0x1C` rate (float), `+0x24` frames, `+0x28`/`+0x2C` packet bytes and pointer, `+0x34`/`+0x38` output bytes and pointer. - Each slot has its own decoder. Type 5 resets it; type 2 and `XApuDisconnect` free it. - Refusals queue no result, leave the output untouched and keep the slot's decoder: - `E_INVALIDARG` for a bad rate, channel count, frames, packet, output or slot; - `E_FAIL` for a packet libopus rejects; - `E_POINTER` for a command shorter than `0x40` bytes; - `E_OUTOFMEMORY` for a full queue, checked before any decode, reset or close. - Decoding goes through a scratch buffer, and the output is written only after the result is queued. - A command with no packet (null pointer and 0 bytes) completes with 0 bytes, as before. So `startup_xapu_queue` is unchanged and passes in both builds. - Without the option, behaviour is unchanged apart from the queue entry growing to 12 bytes (cap 192 KiB per connection; docs updated). - Tested with libopus 1.5.2. Its BSD-3-Clause notice is in `src/startup_stubs/THIRD_PARTY_NOTICES.md`. The repo contains no libopus source or binary. ## Tests `startup_xapu_opus` is registered only when `X2W_WITH_OPUS=ON`. It encodes generated 1 kHz tones with the same libopus; there are no binary fixtures. It checks: - exact byte counts: 3840 (48 kHz mono), 7680 (stereo), 1920 (a 10 ms packet in a 20 ms buffer), 1280 (16 kHz); - PCM bit-exact against a reference decoder, with guard floats past the decoded frames untouched; - a malformed packet, giving `E_FAIL` with the output and queue untouched; - each refusal, with the output untouched and nothing queued; - a full queue (16384 entries), after which the slot's stream continues; - reset and close, each separately starting a fresh decoder, with a control showing that a used decoder differs; - a rate change on a live slot, and a failed decode at a new rate that keeps the old decoder; - no decode when typed completions are off. Environment: Windows 11 (26300), MSVC 19.44, Ninja Debug, commit ccde4c1e (based on 31f46857; merges cleanly into current main). ```powershell cmake -S . -B out/build/def -G Ninja -DCMAKE_BUILD_TYPE=Debug cmake --build out/build/def --target startup_stub_xapu startup_xapu_queue_check ctest --test-dir out/build/def --no-tests=error -R '^startup_xapu_' # startup_xapu_queue: Passed (1/1) cmake -S . -B out/build/opus -G Ninja -DCMAKE_BUILD_TYPE=Debug -DX2W_WITH_OPUS=ON -DOpus_DIR=<opus 1.5.2 install>/lib/cmake/Opus cmake --build out/build/opus --target startup_stub_xapu startup_xapu_queue_check startup_xapu_opus_check ctest --test-dir out/build/opus --no-tests=error -R '^startup_xapu_' -V # startup_xapu_queue: Passed; startup_xapu_opus: "PASS: startup_xapu_opus (libopus 1.5.2)" (2/2) ``` libopus 1.5.2 was built from `opus-1.5.2.tar.gz` (SHA-256 `65c1d2f7…7ce1`) with `-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Debug>:Debug>`, because the stand-ins link the static runtime. Negative controls: each mutant was built, then reverted. - Reporting the output capacity instead of the decoded count fails the 10 ms case: 6 failures. - Making close keep the decoder fails "decode after close matches a fresh decoder". - Decoding before the queue-room check fails "after the full-queue refusals slot 7 continues its stream". Also passing: `tests/docs_links_check.py`, `tests/bidi_controls_check.py`, `git diff --check`. Not run: Release builds. ## Local observation only With a local libopus build of this decode path, I heard the decoded UI sounds through the desktop audio sink. This is a local observation only. It does not show that a title's audio works, and full-game startup remains unverified. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-25 13:00:49 +00:00
tedin7 commented 2026-11-25 13:00:49 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T12:28:32Z.

Review: COMMENTED


Review of head 6139a4b0a7f3f99087f2073003d1647c8fdde351 (xapu: optional Opus packet decode behind X2W_WITH_OPUS).

Scope read: git diff --stat origin/main...HEAD (5 files, +615/-6), full diff of src/startup_stubs/xapu.cpp and CMakeLists.txt, notices and test layout.

Commands executed locally (Linux):

  • python3 tests/docs_links_check.py -> Self-test passed. Checked 19 Markdown files and 452 links.
  • python3 tests/bidi_controls_check.py -> Checked 1613 tracked files: no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean (rc=0).
  • gh pr view -> MERGEABLE, title neutral. gh pr checks -> all jobs still pending at review time.

Not run: the C++ code includes windows.h and needs libopus. No libopus is installed on this Linux host, so I could not build or mutate startup_xapu_opus_check. No Win2025 run was queued, because the default CI configure does not enable X2W_WITH_OPUS. So the findings below come from reading the code.

What looks right:

  • Default build is unchanged in behavior. Without X2W_HAVE_OPUS, push_result is still used. The only new write is the zero +0x34 value, and the result buffer was already zeroed.
  • reserve_result before the decode means a full queue refuses the command before any decoder changes. place_result cannot fail after that.
  • Bounds hold. frames <= 5760 and channels in {1,2} keep output at or below 46080 bytes. scratch holds 5760*2 floats. out_bytes % (frames*4) cannot overflow at these sizes.
  • readable(packet) and writable(out) are checked before use, and the output is only written after a successful decode. The shared static scratch is guarded by connection_lock.
  • A new decoder only replaces the slot's decoder after a good decode. Disconnect frees all slot decoders.

Findings:

  • P1: none.
  • P2 (CMakeLists.txt:510-518, test gating): the whole Opus path, including the new test, only builds with X2W_WITH_OPUS=ON. Default CI never compiles #ifdef X2W_HAVE_OPUS code, so it can rot silently. Please add one CI or documented Win2025 job with libopus 1.5.2 that builds and runs startup_xapu_opus, and put the result in the PR body.
  • nit (src/startup_stubs/xapu.cpp:~162, header comment): "a refused decode ... leaves the output untouched / changes no decoder state" is true for output and for fresh decoders. For an existing slot decoder, a corrupt packet that fails inside opus_decode_float can still change libopus' internal state. Either soften the wording or decode into a cloned state. The first is enough.
  • nit (xapu.cpp comment): the cap comment changed from 128 KiB to 192 KiB, which matches the 12-byte entry (16384 x 12). Fine, just confirming it is intentional.

Mutation: not possible here for the reason above (no libopus, Windows-only sources). Please confirm on Windows that flipping the channels check or dropping reserve_result before the decode fails startup_xapu_opus.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6057597365 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2326#pullrequestreview-6057597365). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T12:28:32Z. **Review:** COMMENTED --- Review of head `6139a4b0a7f3f99087f2073003d1647c8fdde351` (xapu: optional Opus packet decode behind X2W_WITH_OPUS). Scope read: `git diff --stat origin/main...HEAD` (5 files, +615/-6), full diff of `src/startup_stubs/xapu.cpp` and `CMakeLists.txt`, notices and test layout. Commands executed locally (Linux): - `python3 tests/docs_links_check.py` -> Self-test passed. Checked 19 Markdown files and 452 links. - `python3 tests/bidi_controls_check.py` -> Checked 1613 tracked files: no bidirectional controls. - `git diff --check origin/main...HEAD` -> clean (rc=0). - `gh pr view` -> MERGEABLE, title neutral. `gh pr checks` -> all jobs still pending at review time. Not run: the C++ code includes `windows.h` and needs libopus. No libopus is installed on this Linux host, so I could not build or mutate `startup_xapu_opus_check`. No Win2025 run was queued, because the default CI configure does not enable `X2W_WITH_OPUS`. So the findings below come from reading the code. What looks right: - Default build is unchanged in behavior. Without `X2W_HAVE_OPUS`, `push_result` is still used. The only new write is the zero `+0x34` value, and the result buffer was already zeroed. - `reserve_result` before the decode means a full queue refuses the command before any decoder changes. `place_result` cannot fail after that. - Bounds hold. `frames <= 5760` and `channels` in {1,2} keep output at or below 46080 bytes. `scratch` holds 5760*2 floats. `out_bytes % (frames*4)` cannot overflow at these sizes. - `readable(packet)` and `writable(out)` are checked before use, and the output is only written after a successful decode. The shared static `scratch` is guarded by `connection_lock`. - A new decoder only replaces the slot's decoder after a good decode. Disconnect frees all slot decoders. Findings: - P1: none. - P2 (`CMakeLists.txt:510-518`, test gating): the whole Opus path, including the new test, only builds with `X2W_WITH_OPUS=ON`. Default CI never compiles `#ifdef X2W_HAVE_OPUS` code, so it can rot silently. Please add one CI or documented Win2025 job with libopus 1.5.2 that builds and runs `startup_xapu_opus`, and put the result in the PR body. - nit (`src/startup_stubs/xapu.cpp:~162`, header comment): "a refused decode ... leaves the output untouched / changes no decoder state" is true for output and for fresh decoders. For an existing slot decoder, a corrupt packet that fails inside `opus_decode_float` can still change libopus' internal state. Either soften the wording or decode into a cloned state. The first is enough. - nit (`xapu.cpp` comment): the cap comment changed from 128 KiB to 192 KiB, which matches the 12-byte entry (16384 x 12). Fine, just confirming it is intentional. Mutation: not possible here for the reason above (no libopus, Windows-only sources). Please confirm on Windows that flipping the `channels` check or dropping `reserve_result` before the decode fails `startup_xapu_opus`. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 13:00:49 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T12:46:06Z.

Review: COMMENTED


Review of head 3ee95fa5d7bc09556576cd8cb0ea92a4e538cac2 (comment only, no verdict).

Result: no P1, no P2. Two nits.

Commands executed locally (Linux worktree of the PR head):

  • git diff --stat origin/main...HEAD: 5 files, +620/-6
  • python3 tests/docs_links_check.py: self-test passed, 19 Markdown files, 452 links
  • python3 tests/bidi_controls_check.py: 1613 files, no bidirectional controls
  • git diff --check origin/main...HEAD: clean
  • gh pr checks 2326: Linux Python checks, gfx1013 fixtures and Windows build/test all pass; PR is MERGEABLE

Read through src/startup_stubs/xapu.cpp and CMakeLists.txt against the description:

  • Queue-room check (reserve_result) runs before any decode, reset or close, and place_result cannot fail afterwards. So a full queue never changes decoder state.
  • Decode goes to a static scratch buffer under connection_lock. Output is copied only after the result is queued. Size is bounded (max_frames * 2 floats, and out_bytes == frames * channels * 4 with channels 1 or 2), so the copy cannot overrun the scratch.
  • A new rate or channel count creates the new decoder first and swaps it in only after a good decode, so a failed decode keeps the old decoder.
  • Connection is created with new (std::nothrow) and every new member has a default initializer, so the 256 decoder slots start empty. XApuDisconnect frees them all.
  • Default build (X2W_WITH_OPUS OFF): the only change is the 12-byte QueueEntry; the comment and docs on the 192 KiB cap match.
  • Notice file and CMake option are consistent with "no libopus source or binary in the repo".

Findings:

  • nit, src/startup_stubs/xapu.cpp (decode_packet): opus_decoder_create failures of any kind return E_OUTOFMEMORY. Rate and channels are validated just above, so only a real allocation failure can reach it. Fine as is; a one-line comment would say so.
  • nit, CMakeLists.txt (X2W_WITH_OPUS block): the new decode path and startup_xapu_opus are not built by CI, because the option is OFF there. The PR's local results are therefore the only evidence for that path. Fine for an opt-in feature, but later edits to it can regress without a CI signal.

Not run by me:

  • The Opus build and startup_xapu_opus. They need an installed libopus built with the static MSVC runtime, and this Linux box has none. I also did not run the Windows VM (win2025) because the VM is not set up with libopus.
  • My own mutation of the new logic, for the same reason. The PR lists three negative controls (wrong byte count, close keeping the decoder, decode before the queue-room check). Each targets a distinct piece of the new logic, which looks sound by reading, but I did not re-run them.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6057856873 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2326#pullrequestreview-6057856873). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T12:46:06Z. **Review:** COMMENTED --- Review of head `3ee95fa5d7bc09556576cd8cb0ea92a4e538cac2` (comment only, no verdict). **Result: no P1, no P2. Two nits.** Commands executed locally (Linux worktree of the PR head): - `git diff --stat origin/main...HEAD`: 5 files, +620/-6 - `python3 tests/docs_links_check.py`: self-test passed, 19 Markdown files, 452 links - `python3 tests/bidi_controls_check.py`: 1613 files, no bidirectional controls - `git diff --check origin/main...HEAD`: clean - `gh pr checks 2326`: Linux Python checks, gfx1013 fixtures and Windows build/test all pass; PR is MERGEABLE Read through `src/startup_stubs/xapu.cpp` and `CMakeLists.txt` against the description: - Queue-room check (`reserve_result`) runs before any decode, reset or close, and `place_result` cannot fail afterwards. So a full queue never changes decoder state. - Decode goes to a static scratch buffer under `connection_lock`. Output is copied only after the result is queued. Size is bounded (`max_frames * 2` floats, and `out_bytes == frames * channels * 4` with channels 1 or 2), so the copy cannot overrun the scratch. - A new rate or channel count creates the new decoder first and swaps it in only after a good decode, so a failed decode keeps the old decoder. - `Connection` is created with `new (std::nothrow)` and every new member has a default initializer, so the 256 decoder slots start empty. `XApuDisconnect` frees them all. - Default build (`X2W_WITH_OPUS` OFF): the only change is the 12-byte `QueueEntry`; the comment and docs on the 192 KiB cap match. - Notice file and CMake option are consistent with "no libopus source or binary in the repo". Findings: - nit, `src/startup_stubs/xapu.cpp` (`decode_packet`): `opus_decoder_create` failures of any kind return `E_OUTOFMEMORY`. Rate and channels are validated just above, so only a real allocation failure can reach it. Fine as is; a one-line comment would say so. - nit, `CMakeLists.txt` (`X2W_WITH_OPUS` block): the new decode path and `startup_xapu_opus` are not built by CI, because the option is OFF there. The PR's local results are therefore the only evidence for that path. Fine for an opt-in feature, but later edits to it can regress without a CI signal. Not run by me: - The Opus build and `startup_xapu_opus`. They need an installed libopus built with the static MSVC runtime, and this Linux box has none. I also did not run the Windows VM (`win2025`) because the VM is not set up with libopus. - My own mutation of the new logic, for the same reason. The PR lists three negative controls (wrong byte count, close keeping the decoder, decode before the queue-room check). Each targets a distinct piece of the new logic, which looks sound by reading, but I did not re-run them. AI-assisted; commands listed were executed locally.
R0Y00 commented 2026-11-25 13:00:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T12:40:01Z.


I built upstream libopus 1.5.2 with the static MSVC runtime and independently verified Windows Release. With X2W_WITH_OPUS=ON, the queue and Opus checks passed (2/2). With the default option off, the queue check passed (1/1). I added checks for a malformed packet on an already active slot at the same rate and for a valid packet whose output capacity is too small. Both were rejected, the queue and output guards held, and the next packet still matched the independent reference stream. This is source-built coverage. The new head 3ee95fa5 also distinguishes retaining the decoder object from any provider-specific internal state after a decoder error.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7162687962 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2326#issuecomment-7162687962). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T12:40:01Z. --- I built upstream libopus 1.5.2 with the static MSVC runtime and independently verified Windows Release. With `X2W_WITH_OPUS=ON`, the queue and Opus checks passed (2/2). With the default option off, the queue check passed (1/1). I added checks for a malformed packet on an already active slot at the same rate and for a valid packet whose output capacity is too small. Both were rejected, the queue and output guards held, and the next packet still matched the independent reference stream. This is source-built coverage. The new head `3ee95fa5` also distinguishes retaining the decoder object from any provider-specific internal state after a decoder error.
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
R0Y00/Xbox2Windows!2318
No description provided.