XAudio2: real Opus decoder via Windows MSOpusDecoder MFT #2

Merged
raylitebull merged 1 commit from upstream-pr/3 into main 2026-11-21 16:38:48 +00:00
raylitebull commented 2026-11-21 16:29:33 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by raylitebull on 2026-11-21T16:29:33Z.


Implements real Opus audio decoding for XAudio2 on top of current main using Windows' built-in MSOpusDecoder.dll Media Foundation transform (as noted in HANDOFF.md), with no additional third-party crates.

Summary of changes

  • crates/xcl/src/hle/opus.rs
    • RFC 6716 TOC & packet parser: exact Table 2 frame durations for all 32 configs (SILK 10/20/40/60 ms, Hybrid 10/20 ms, CELT 2.5/5/10/20 ms), codes 0–3 (CBR/VBR/padding), and the 120 ms (5760-sample) packet limit.
    • RFC 7845 OpusHead builder & parser: supports 1–8 channels (mapping family 0 for mono/stereo, Vorbis mapping family 1 for 3–8 ch), pre_skip, and output_gain.
    • Packet framing extraction: supports Ogg Opus (OggS), single- and multi-block [u16 LE len][packet]... with optional trailing zero padding (matching the 2048 B AWC blocks), and raw RFC 6716 packets.
    • Stateful Decoder: dynamically loads mfplat.dll and MSOpusDecoder.dll via DllGetClassObject (no static Media Foundation dependency; GPL-2.0 system-library compatible). Decodes 1–2 ch single-stream directly and 3–8 ch multistream (5.1/7.1) by splitting RFC 6716 Appendix B self-delimited sub-packets across stream_count MFT instances and interleaving via the channel map.
  • crates/xcl/src/hle/xaudio2.rs
    • Intercepts WAVE_FORMAT_OPUS (0x704F) and WAVE_FORMAT_EXTENSIBLE with MFAudioFormat_Opus, creating a PCM16 source voice backed by opus::Decoder (with RFC 6716-sized silence fallback if MSOpusDecoder.dll is unavailable).
    • Wraps IXAudio2VoiceCallback to keep decoded PCM16 buffers alive at stable heap addresses until OnBufferEnd (or DestroyVoice) while forwarding the title's original pBufferContext.
    • Patches FlushSourceBuffers (#22) to flush decoder state and GetState (#25) to map internal buffer tokens back to the title's pCurrentBufferContext.
    • Adds opus_voices, opus_buffers_decoded, and opus_decode_errors to AudioStats (report.json -> audio).
  • Tests & docs
    • 7 unit tests in hle::opus covering all 32 RFC 6716 configs, codes 0–3, framing detection, real libopus packets across SILK/Hybrid/CELT/2.5–60 ms, 440/880 Hz tone verification, 5.1 surround channel isolation, pre_skip, 24 kHz output, and flush.
    • 2 new unit tests in hle::xaudio2 covering format detection, region clamping, live Opus voice playback, GetState context mapping, callback forwarding, and zero decoded-buffer leaks.
    • Added an Opus 0x704F voice (2048 B AWC-framed block) to tests/testtitle/src/audio.rs and checked its report.json stats in crates/xcl/tests/e2e.rs.
    • Updated README.md and THIRD_PARTY.md.

Testing

cargo build --workspace && cargo test --workspace -- --test-threads=4:

  • 147 passed, 0 failed (unit tests)
  • 1 passed, 0 failed (e2e – XMA2 and Opus 0.1 s voices both finish in ~96 ms)
  • 12 passed (console_dlls_e2e), 6 passed (gpu_pso_e2e)
<!-- greyfinch-sync:ivall/xcl:issue:6531437900 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3). Originally posted by [raylitebull](https://github.com/raylitebull) on 2026-11-21T16:29:33Z. --- Implements real Opus audio decoding for XAudio2 on top of current `main` using Windows' built-in `MSOpusDecoder.dll` Media Foundation transform (as noted in `HANDOFF.md`), with no additional third-party crates. ### Summary of changes - **`crates/xcl/src/hle/opus.rs`** - **RFC 6716 TOC & packet parser:** exact Table 2 frame durations for all 32 configs (SILK 10/20/40/60 ms, Hybrid 10/20 ms, CELT 2.5/5/10/20 ms), codes 0–3 (CBR/VBR/padding), and the 120 ms (5760-sample) packet limit. - **RFC 7845 `OpusHead` builder & parser:** supports 1–8 channels (mapping family 0 for mono/stereo, Vorbis mapping family 1 for 3–8 ch), `pre_skip`, and `output_gain`. - **Packet framing extraction:** supports Ogg Opus (`OggS`), single- and multi-block `[u16 LE len][packet]...` with optional trailing zero padding (matching the 2048 B AWC blocks), and raw RFC 6716 packets. - **Stateful `Decoder`:** dynamically loads `mfplat.dll` and `MSOpusDecoder.dll` via `DllGetClassObject` (no static Media Foundation dependency; GPL-2.0 system-library compatible). Decodes 1–2 ch single-stream directly and 3–8 ch multistream (5.1/7.1) by splitting RFC 6716 Appendix B self-delimited sub-packets across `stream_count` MFT instances and interleaving via the channel map. - **`crates/xcl/src/hle/xaudio2.rs`** - Intercepts `WAVE_FORMAT_OPUS` (`0x704F`) and `WAVE_FORMAT_EXTENSIBLE` with `MFAudioFormat_Opus`, creating a PCM16 source voice backed by `opus::Decoder` (with RFC 6716-sized silence fallback if `MSOpusDecoder.dll` is unavailable). - Wraps `IXAudio2VoiceCallback` to keep decoded PCM16 buffers alive at stable heap addresses until `OnBufferEnd` (or `DestroyVoice`) while forwarding the title's original `pBufferContext`. - Patches `FlushSourceBuffers` (#22) to flush decoder state and `GetState` (#25) to map internal buffer tokens back to the title's `pCurrentBufferContext`. - Adds `opus_voices`, `opus_buffers_decoded`, and `opus_decode_errors` to `AudioStats` (`report.json -> audio`). - **Tests & docs** - 7 unit tests in `hle::opus` covering all 32 RFC 6716 configs, codes 0–3, framing detection, real libopus packets across SILK/Hybrid/CELT/2.5–60 ms, 440/880 Hz tone verification, 5.1 surround channel isolation, `pre_skip`, 24 kHz output, and `flush`. - 2 new unit tests in `hle::xaudio2` covering format detection, region clamping, live Opus voice playback, `GetState` context mapping, callback forwarding, and zero decoded-buffer leaks. - Added an Opus `0x704F` voice (2048 B AWC-framed block) to `tests/testtitle/src/audio.rs` and checked its `report.json` stats in `crates/xcl/tests/e2e.rs`. - Updated `README.md` and `THIRD_PARTY.md`. ### Testing `cargo build --workspace && cargo test --workspace -- --test-threads=4`: - 147 passed, 0 failed (unit tests) - 1 passed, 0 failed (`e2e` – XMA2 and Opus 0.1 s voices both finish in ~96 ms) - 12 passed (`console_dlls_e2e`), 6 passed (`gpu_pso_e2e`)
upstream-sync closed this pull request 2026-11-24 06:56:41 +00:00
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
xcl-dev/xcl!2
No description provided.