XAudio2: real Opus decoder via Windows MSOpusDecoder MFT #3

Manually merged
github merged 1 commit from gh-pr/3/head into gh-pr/3/base 2026-11-26 04:03:26 +00:00
Owner

@⁠raylitebull on GitHub · 2026-11-21 16:29 UTC · original
raylitebull:opus-decoder → main · +2266 −44 · 8 files · 1 commits · merged 2026-11-21 16:38 UTC

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)
**@⁠raylitebull** on GitHub · 2026-11-21 16:29 UTC · [original](https://github.com/xcl-dev/xcl/pull/3) `raylitebull:opus-decoder` → `main` · +2266 −44 · 8 files · 1 commits · merged 2026-11-21 16:38 UTC 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`)
- Add hle::opus: RFC 6716 TOC/packet parser (exact Table 2 frame durations for
  all 32 configs, codes 0-3, 120 ms limit), RFC 7845 OpusHead parser/builder,
  framing extractor (Ogg Opus, [u16 LE len][pkt] blocks with zero padding, raw
  packets), and stateful Decoder backed by dynamically loaded mfplat.dll +
  MSOpusDecoder.dll (1-2 ch single-stream and 3-8 ch multistream via RFC 6716
  Appendix B self-delimited splitting).
- Wire Opus voices (WAVE_FORMAT_OPUS 0x704F and WAVE_FORMAT_EXTENSIBLE with
  MFAudioFormat_Opus) in hle::xaudio2 to decode to PCM16, wrap
  IXAudio2VoiceCallback to manage decoded buffer lifetimes and forward the
  title's original pBufferContext, flush decoder state on FlushSourceBuffers,
  and map tokens back in GetState.
- Add AudioStats counters (opus_voices, opus_buffers_decoded,
  opus_decode_errors), unit tests covering SILK/Hybrid/CELT/5.1/framing/lifetime,
  and an Opus voice check in tests/testtitle + e2e.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:03:25 +00:00
github manually merged commit 2bd8fccb73 into gh-pr/3/base 2026-11-26 04:03:26 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.