Skip to content

XAudio2: pure-Rust XMA1/XMA2 audio decoder - #20

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

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

Conversation

@raylitebull

Copy link
Copy Markdown
Member

Implements real XMA1 (0x0165) and XMA2 (0x0166) audio decoding for XAudio2 on top of current main in pure safe Rust — closing out the remaining audio gap listed in README.md and crates/xcl/src/hle/xaudio2.rs with zero external runtime DLLs and zero new Cargo dependencies.

The decoder and static Huffman/DSP tables are cleanly ported from FFmpeg's LGPL-2.1+ wmaprodec.c / wmaprodata.h / wma.c / sinewin_tablegen.h / float_dsp.c / tx_template.c (GPL-2.0-only compatible under LGPL-2.1 §3, with GPL-2.0 §2a provenance headers in the files and THIRD_PARTY.md).

Summary of changes

  • crates/xcl/src/hle/xma2/ (mod.rs, tables.rs)
    • Format & stream config parser (StreamConfig): supports XMA2WAVEFORMATEX (cbSize == 34), legacy XMA2WAVEFORMAT (rev 3/4), and XMAWAVEFORMAT (0x0165), up to 8 interleaved 1–2 ch streams (16 channels total).
    • Packet & multi-stream framer (Decoder): decodes fixed 2048-byte XMA1/XMA2 packets with cross-packet bit-reservoir frame assembly (num_bits_prev_frame), multi-stream skip_packets routing, 0xFFFFFFFF padding-packet skipping, 64-sample initial encoder-delay skip, trim_start/trim_end trimming, and overlap-add EOF flushing.
    • WMA Pro subframe codec: variable subframe tiling (512/256/128/64 samples, uniform and non-uniform channel layouts), DPCM + resampled run-length delta scale factors, 4/2/1-element vector + run-level + escape coefficient decoding across all 7 canonical Huffman tables, M/S stereo channel decorrelation (all-band and per-band), inverse quantization, radix-2 FFT imdct_half, and sine window overlap-add.
    • Cross-validated against FFmpeg's xma2 / xma1 decoders: all 7 synthetic test vectors (mono 48 kHz tone, stereo M/S transform, non-uniform 256/128 subframes + RL scale factors, all vector/RLE/quant-escape paths at 32 kHz, cross-packet frames + trim_start/trim_end, 5.1 6-channel 3-stream interleaving, and XMA1 24 kHz stereo) match FFmpeg's output to < 1.5e-7 float32 (0–1 LSB PCM16).
  • crates/xcl/src/hle/xaudio2.rs
    • Generalizes the decoded-voice callback wrapper (DecodedVoiceState / DecodedCallback) to back both Opus (hle::opus) and XMA1/XMA2 (hle::xma2) voices without code duplication.
    • Intercepts WAVE_FORMAT_XMA (0x0165) and WAVE_FORMAT_XMA2 (0x0166) in CreateSourceVoice, decoding submitted buffers to PCM16, clamping play/loop regions, managing decoded PCM buffer lifetimes until OnBufferEnd / DestroyVoice, mapping contexts back in GetState, and flushing decoder state on FlushSourceBuffers / XAUDIO2_END_OF_STREAM.
    • Falls back cleanly to length-matched silence if a buffer is corrupt or yields zero samples, tracking xma_buffers_decoded and xma_decode_errors in AudioStats (report.json -> audio).
  • Tests & docs
    • 7 unit tests in hle::xma2 covering fast IMDCT vs naive reference, all 7 Huffman tables round-tripping every symbol, XMA1/XMA2 config parsing, all 7 FFmpeg-verified bitstreams, streaming chunk-by-chunk parity with single-shot decode, 5.1 channel isolation, 0xFFFFFFFF padding packets, and corrupt/empty input safety.
    • 1 new live XAudio2 unit test in hle::xaudio2 (xma2_source_voice_decodes_and_manages_lifetime) covering valid XMA2 playback, corrupt-buffer silence fallback, GetState context mapping, FlushSourceBuffers, callback forwarding, and zero decoded-buffer leaks.
    • Updated tests/testtitle/src/audio.rs, tests/testtitle/src/main.rs, and crates/xcl/tests/e2e.rs to submit a real 48 kHz mono XMA2 packet and assert xma_voices == 1, xma_buffers_decoded == 1, xma_decode_errors == 0, silent_voices == 0, and buffers_silenced == 0.
    • Updated README.md and THIRD_PARTY.md.

Testing

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

  • 248 passed, 0 failed (unit tests)
  • 1 passed, 0 failed (e2e – XMA2 and Opus 0.1 s voices both decode and finish in ~96 ms)
  • 12 passed (console_dlls_e2e), 1 passed (console_shaders), 6 passed (gpu_pso_e2e), 3 passed (xg_oracle)

@ivall

ivall commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Reviewed and merged, thanks. FFmpeg LGPL-2.1+ provenance is in the file header and THIRD_PARTY.md (compatible with GPL-2.0-only); the decoder is safe Rust, the unwraps are test-only and the bit reader reads zeros past the end, so corrupt input cannot panic the audio path. Clippy nits for later: a 14-argument function, two manual is_multiple_of. Checked together with #17, #18, #19 and #20 on top of current main: full workspace tests pass (271 lib tests, e2e, console_dlls_e2e, gpu_pso_e2e, rpf_e2e, the console-library tests), no new build warnings.

@ivall
ivall merged commit 0f16f59 into xcl-dev:main Nov 21, 2026
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 27, 2026
…wports/scissors/topo) into ListDrawRecord

List slots xcl-dev#34 (SetGraphicsRootSignature), xcl-dev#50 (OMSetRenderTargets),
xcl-dev#47 (IASetIndexBuffer), plus PC-side xcl-dev#20/xcl-dev#21/xcl-dev#22 (topology, viewports,
scissors) store into ListDrawRecord. SetPipelineState (xcl-dev#25) now merges
instead of overwriting so PSO-first or state-first order both keep the
full record. Store-only: no draw, no validation, no execute; triangle/
bias FAILs stay failing.

Evidence: new draw_s2_list_state_recorded_pre_draw fails pre-change
(S2 symbols absent at HEAD: grep count 0) and passes post; full lib
806 passed, 0 failed, 1 ignored; both cargo checks clean; title null
4 FAILs identical to HEAD-baseline worktree run (triangle, present,
bias-xcl-dev#94, all D3D12.X GPU-only, + pre-existing files:delete also on
HEAD), delta 0; Vulkan 3 (triangle, bias-xcl-dev#94 + same files:delete);
retail HOLDS (exit 139 @ exe+0x2237320, SIGSEGV fault 0, 20 stubs,
xgr XAsync/XNetworking/XUser, 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