Skip to content

XAudio2: real Opus decoder via Windows MSOpusDecoder MFT - #3

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
raylitebull:opus-decoder
Nov 21, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
raylitebull:opus-decoder

Conversation

@raylitebull

Copy link
Copy Markdown
Member

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 (HLE: implement mfplat.dll!MFResetDXGIDeviceManagerX with system fallback #22) to flush decoder state and GetState (test: accept all valid Windows MMDevice endpoint ID formats in audio test #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.
ivall added a commit that referenced this pull request Nov 21, 2026
@ivall
ivall merged commit aa8280f into xcl-dev:main Nov 21, 2026
ivall added a commit that referenced this pull request Nov 21, 2026
…io2 part, superseded by PR #3)

# Conflicts:
#	crates/xcl/src/hle/xaudio2.rs
@raylitebull
raylitebull deleted the opus-decoder branch November 21, 2026 17:05
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 27, 2026
The null device's plain lists ran the console memory slots (xcl-dev#60/xcl-dev#61/
xcl-dev#69-xcl-dev#72/xcl-dev#89/xcl-dev#93) as logging stubs, so the title's console_list_methods
check read back 0x0 on the null backend while the identical immediate
host writes already passed on Vulkan lists. The vtable now wires the
same VulList_* functions into Kind::CommandList; GetDevice (xcl-dev#7),
the UAV barrier (xcl-dev#146) and AllocateUploadMemory (xcl-dev#148) stay
Vulkan-only with their slices (barrier/upload ride with raytracing,
which still fails on null at the OPTIONS5 tail-zero assert).

Failing-first: the new null_list_shares_the_console_memory_methods
test fails pre-route (slot 69 stub writes nothing) and passes now
(write32/fill/copy/timestamp land, pre-satisfied wait returns,
kickoff is a silent no-op), mirroring the title's asserts.
Gates: check -p xcl --tests EXIT 0; check --workspace EXIT 0; lib 797
passed/0 failed/1 ignored (--test-threads=4). Title e2e, fresh roots:
null 12->11 FAILs (Write32 fixed, byte-identical remainder), Vulkan 9
unchanged with the real present (640x360 token 0x1 on Intel Arc).
Retail HOLDS (SIGSEGV exe+0x2237320, exit 139, called_stubs 20,
0 d3d12).
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 27, 2026
…ev#148 (XS)

The null device left slot xcl-dev#18 a logging stub, so the title's
raytracing_surface leg 2 read heap zeros ([0 x4]) instead of the RDNA2
BVH words; legs 3 (xcl-dev#148 upload, xcl-dev#146 barrier) were Vulkan-list-only, so
the default title list had stubs there too. All three are pure
host-memory operations (descriptor-word write, aligned zeroed alloc, a
coherent single-queue no-op barrier), so the null objects share the
Vulkan implementations with no GPU needed — same precedent as the xcl-dev#3
Execute leg sharing the console memory methods.

Failing-first: new null_device_serves_the_raytracing_surface test fails
pre-fix ([0 x4] vs the BVH words) and passes now; other view dimensions
and nulls still pass through untouched (existing Vulkan helper test).
Gates: check -p xcl --tests EXIT 0; check --workspace EXIT 0; lib 799
passed/0 failed/1 ignored (--test-threads=4). Title e2e, fresh roots:
null 12->11 FAILs (raytracing_surface now PASSES; every other FAIL
byte-identical, 0 new non-GPU), Vulkan 10 total incl. files (9 D3D12.X,
raytracing absent — paths untouched). Retail HOLDS (SIGSEGV
exe+0x2237320, exit 139, called_stubs 20, 0 d3d12).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants