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

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

@⁠raylitebull on GitHub · 2026-11-21 21:31 UTC · original
raylitebull:xma2-decoder → main · +3164 −125 · 9 files · 1 commits · merged 2026-11-21 22:50 UTC

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)
**@⁠raylitebull** on GitHub · 2026-11-21 21:31 UTC · [original](https://github.com/xcl-dev/xcl/pull/20) `raylitebull:xma2-decoder` → `main` · +3164 −125 · 9 files · 1 commits · merged 2026-11-21 22:50 UTC 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`)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:04:33 +00:00
github manually merged commit 6da1947ece into gh-pr/20/base 2026-11-26 04:04:34 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-21 22:50 UTC · original

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** on GitHub · 2026-11-21 22:50 UTC · [original](https://github.com/xcl-dev/xcl/pull/20#issuecomment-7070465527) 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.