XAudio2: pure-Rust XMA1/XMA2 decoder

This commit is contained in:
raylitebull
2026-11-21 22:30:28 +01:00
parent 18cb400400
commit 6da1947ece
9 changed files with 3164 additions and 125 deletions
+4 -4
View File
@@ -43,7 +43,7 @@ measurements and next steps:
|---|---|---|---|
| **XGameRuntime** | ✅ | the GDK thunk protocol real titles use, laid out from a table of all 396 GDK runtime functions; offline XUser, XGameSave, persistent local storage, XPackage, XGame (title id), XSystem – the title sees an Xbox Series X / S; task queues, async, networking and dialogs through the PC's XGameRuntime | the game's libHttpClient initialises; users, saves and storage through the real call path; slot layout cross-checked against the game's own thunks |
| **Input** | ✅ | GameInput → the system GameInput | – |
| **Audio** | 🟡 | XAudio2 → system XAudio2.9 (PCM / float voices; Opus 0x704F decoded via `MSOpusDecoder.dll`) | unit + end-to-end tests; XMA2 voices are silent for now |
| **Audio** | ✅ | XAudio2 → system XAudio2.9 (PCM / float voices; Opus 0x704F decoded via `MSOpusDecoder.dll`; XMA1 / XMA2 decoded in pure Rust) | unit + end-to-end tests |
### Graphics
@@ -68,9 +68,9 @@ measurements and next steps:
| Needs a game executable | Not done yet | Not planned |
|---|---|---|
| D3D12.X draw records and `PresentX` / frame pacing | XMA2 audio decoding | Xbox Live, Store licensing (offline by design) |
| root arguments → shader registers for graphics | performance work (GPU buffers live in system memory) | decrypting XVC packages or any DRM |
| texture creation in title memory (binding the detiler), MSAA tiled textures | hull / domain / amplification shaders (none in the sample game) | console SDK headers / binaries in the repository |
| D3D12.X draw records and `PresentX` / frame pacing | performance work (GPU buffers live in system memory) | Xbox Live, Store licensing (offline by design) |
| root arguments → shader registers for graphics | hull / domain / amplification shaders (none in the sample game) | decrypting XVC packages or any DRM |
| texture creation in title memory (binding the detiler), MSAA tiled textures | | console SDK headers / binaries in the repository |
| Bink GPU decoding, rehydrating prebuilt BVHs | | |
Every run writes `report.json` (called stubs, unknown D3D12.X slots and interfaces, XGameRuntime
+1
View File
@@ -7,6 +7,7 @@ carry a note in their header about its origin and the date of changes (GPL-2.0
| Source | License | What in XCL | How |
|---|---|---|---|
| [AnyPS5](https://github.com/boykopovar/AnyPS5) | GPL-2.0-only | recompiler (`crates/xcl/src/gpu/recomp/`) – portions of logic marked in the files | port to Rust/HLSL with annotation |
| [FFmpeg](https://ffmpeg.org/) | LGPL-2.1+ | XMA1/XMA2 decoder and tables (`crates/xcl/src/hle/xma2/`) – after `libavcodec/wmaprodec.c`, `wmaprodata.h`, `wma.c`, `sinewin_tablegen.h`, `libavutil/float_dsp.c`, `tx_template.c` | port to safe Rust with annotation |
| [AMD Machine-Readable GPU ISA](https://gpuopen.com/machine-readable-isa/) | MIT | `crates/xcl/src/gpu/rdna2_spec.rs` (generated by `scripts/gen_rdna2.py`) | encoding and instruction tables |
| [AMD addrlib](https://gitlab.freedesktop.org/mesa/mesa/-/tree/main/src/amd/addrlib) (Mesa) | MIT | `crates/xcl/src/gpu/tiling_tables.rs` (generated by `scripts/gen_tiling.py` from `gfx10SwizzlePattern.h`); GFX10 mip chain / mip tail layout in `gpu/tiling.rs` (after `Gfx10Lib::ComputeSurfaceInfoMacroTiled`) | swizzle tables; layout logic ported with annotation |
| [LLVM](https://github.com/llvm/llvm-project) | Apache-2.0 WITH LLVM-exception | `tests/data/llvm-amdgpu/*` (test data, not part of the program); GFX10 buffer format values (`UfmtSymbolicGFX10`) in `gpu/recomp/emit.rs` | test vectors; hardware facts |
+1
View File
@@ -17,6 +17,7 @@ pub mod dxcompiler;
pub mod kernelx;
pub mod opus;
pub mod xaudio2;
pub mod xma2;
pub mod xg;
pub mod xgameruntime;
+389 -110
View File
@@ -9,20 +9,22 @@
//! * IXAudio2 slot 5 `CreateSourceVoice` – Opus formats (`WAVE_FORMAT_OPUS` 0x704F or
//! `WAVE_FORMAT_EXTENSIBLE` with `MFAudioFormat_Opus`) become PCM16 voices backed by a
//! real RFC 6716 / RFC 7845 decoder (`hle::opus`, using Windows' `MSOpusDecoder.dll`);
//! XMA/XMA2 and any other format Windows rejects become silent PCM16 voices of matching
//! length,
//! XMA/XMA2 (`0x165` / `0x166`) become PCM16 voices backed by the pure-Rust XMA decoder
//! (`hle::xma2`); any other format Windows rejects becomes a silent PCM16 voice of
//! matching length,
//! * IXAudio2 slot 7 `CreateMasteringVoice` – console device ids are ignored (default
//! device); on failure retried with default channels/rate and the game-effects category,
//! * IXAudio2SourceVoice slot 21 `SubmitSourceBuffer` – Opus voices decode the submitted
//! packets to PCM16 and keep each decoded buffer alive until `OnBufferEnd` (or voice
//! destruction); silent voices submit silence of matching length, preserving callbacks,
//! * IXAudio2SourceVoice slot 22 `FlushSourceBuffers` – flushes the Opus decoder state,
//! * IXAudio2SourceVoice slot 21 `SubmitSourceBuffer` – Opus and XMA/XMA2 voices decode
//! the submitted packets to PCM16 and keep each decoded buffer alive until `OnBufferEnd`
//! (or voice destruction); silent voices submit silence of matching length, preserving
//! callbacks,
//! * IXAudio2SourceVoice slot 22 `FlushSourceBuffers` – flushes the Opus / XMA decoder
//! state,
//! * IXAudio2SourceVoice slot 25 `GetState` – maps internal buffer tokens back to the
//! title's original `pBufferContext`,
//! * IXAudio2SourceVoice slot 18 `DestroyVoice` – tears down the voice, then releases any
//! remaining decoded buffers and callback wrapper.
//!
//! Next step: a real XMA2 decoder (FFmpeg libavcodec, LGPL-2.1+).
//! Exports we do not implement (X3DAudio*, CreateFX, CreateAudioReverb…) fall back to the
//! system xaudio2_9.dll (see `hle::build`).
@@ -35,7 +37,7 @@ use std::sync::{Arc, Mutex, OnceLock};
use serde::{Deserialize, Serialize};
use crate::hle::opus;
use crate::hle::{opus, xma2};
use crate::util::{E_FAIL, E_POINTER, HRESULT, S_OK};
pub const WAVE_FORMAT_PCM: u16 = 1;
@@ -176,8 +178,12 @@ pub struct AudioStats {
pub mastering_retries: u64,
pub mastering_errors: u64,
pub source_voices: u64,
/// XMA/XMA2 voices replaced by PCM16 voices (no decoder yet → silence)
/// XMA/XMA2 voices decoded to PCM16 via `hle::xma2`
pub xma_voices: u64,
/// XMA/XMA2 buffers decoded to PCM16
pub xma_buffers_decoded: u64,
/// XMA/XMA2 buffers that failed decoding (fell back to silence)
pub xma_decode_errors: u64,
/// Opus voices decoded to PCM16 via MSOpusDecoder
pub opus_voices: u64,
/// Opus buffers decoded to PCM16
@@ -200,6 +206,8 @@ static MASTERING_RETRIES: AtomicU64 = AtomicU64::new(0);
static MASTERING_ERRORS: AtomicU64 = AtomicU64::new(0);
static SOURCE_VOICES: AtomicU64 = AtomicU64::new(0);
static XMA_VOICES: AtomicU64 = AtomicU64::new(0);
static XMA_BUFFERS_DECODED: AtomicU64 = AtomicU64::new(0);
static XMA_DECODE_ERRORS: AtomicU64 = AtomicU64::new(0);
static OPUS_VOICES_CREATED: AtomicU64 = AtomicU64::new(0);
static OPUS_BUFFERS_DECODED: AtomicU64 = AtomicU64::new(0);
static OPUS_DECODE_ERRORS: AtomicU64 = AtomicU64::new(0);
@@ -218,6 +226,8 @@ pub fn stats() -> AudioStats {
mastering_errors: MASTERING_ERRORS.load(Ordering::Relaxed),
source_voices: SOURCE_VOICES.load(Ordering::Relaxed),
xma_voices: XMA_VOICES.load(Ordering::Relaxed),
xma_buffers_decoded: XMA_BUFFERS_DECODED.load(Ordering::Relaxed),
xma_decode_errors: XMA_DECODE_ERRORS.load(Ordering::Relaxed),
opus_voices: OPUS_VOICES_CREATED.load(Ordering::Relaxed),
opus_buffers_decoded: OPUS_BUFFERS_DECODED.load(Ordering::Relaxed),
opus_decode_errors: OPUS_DECODE_ERRORS.load(Ordering::Relaxed),
@@ -312,6 +322,27 @@ pub unsafe fn opus_format<'a>(fmt: *const WaveFormatEx) -> Option<OpusFormat<'a>
None
}
/// Parse an XMA1 (`0x0165`) or XMA2 (`0x0166`) `WAVEFORMATEX` + extradata into a decoder config.
///
/// # Safety
/// `fmt` must point to a `WAVEFORMATEX` followed by `cbSize` readable bytes.
pub unsafe fn xma_stream_config(fmt: *const WaveFormatEx) -> Option<Result<xma2::StreamConfig, String>> {
let w = fmt.read_unaligned();
if !is_xma(w.tag) {
return None;
}
let extra = if w.cb_size > 0 {
std::slice::from_raw_parts((fmt as *const u8).add(std::mem::size_of::<WaveFormatEx>()), w.cb_size as usize)
} else {
&[]
};
Some(if w.tag == WAVE_FORMAT_XMA2 {
xma2::StreamConfig::from_xma2_wfx(w.channels, w.rate, extra)
} else {
xma2::StreamConfig::from_xma1_wfx(w.channels, w.rate, extra)
})
}
/// Voice info from a format (XMA2WAVEFORMATEX fields only when cbSize says they are there).
///
/// # Safety
@@ -433,7 +464,7 @@ pub fn silent_buffer(i: &XmaInfo, b: &XAudio2Buffer, silence: *const u8, max_byt
}
// ---------------------------------------------------------------------------------------
// Opus voice state & IXAudio2VoiceCallback wrapper (decoded PCM buffer lifetime manager)
// Decoded voice state (Opus & XMA1/XMA2) & IXAudio2VoiceCallback wrapper
// ---------------------------------------------------------------------------------------
/// Owned decoded PCM16 buffer kept alive at a stable heap address until `OnBufferEnd`
@@ -443,8 +474,22 @@ struct DecodedBuffer {
_pcm: Box<[u8]>,
}
struct OpusVoiceState {
decoder: Mutex<opus::Decoder>,
enum VoiceDecoder {
Opus(opus::Decoder),
Xma { decoder: xma2::Decoder, info: XmaInfo },
}
impl VoiceDecoder {
fn flush(&mut self) {
match self {
VoiceDecoder::Opus(d) => d.flush(),
VoiceDecoder::Xma { decoder, .. } => decoder.flush(),
}
}
}
struct DecodedVoiceState {
decoder: Mutex<VoiceDecoder>,
block_align: u16,
channels: u16,
rate: u32,
@@ -454,8 +499,8 @@ struct OpusVoiceState {
live_buffers: Mutex<HashMap<usize, Box<DecodedBuffer>>>,
}
struct OpusVoiceHandle {
state: Arc<OpusVoiceState>,
struct DecodedVoiceHandle {
state: Arc<DecodedVoiceState>,
cb_ptr: usize,
}
@@ -472,9 +517,9 @@ struct VoiceCallbackVtbl {
}
#[repr(C)]
struct OpusCallback {
struct DecodedCallback {
vtbl: &'static VoiceCallbackVtbl,
state: Arc<OpusVoiceState>,
state: Arc<DecodedVoiceState>,
}
unsafe fn title_cb_vtbl(cb: usize) -> Option<(*mut c_void, *const VoiceCallbackVtbl)> {
@@ -489,29 +534,29 @@ unsafe fn title_cb_vtbl(cb: usize) -> Option<(*mut c_void, *const VoiceCallbackV
Some((obj, vtbl))
}
unsafe extern "system" fn opus_cb_pass_start(this: *mut c_void, bytes: u32) {
let cb = &*(this as *const OpusCallback);
unsafe extern "system" fn decoded_cb_pass_start(this: *mut c_void, bytes: u32) {
let cb = &*(this as *const DecodedCallback);
if let Some((obj, vt)) = title_cb_vtbl(cb.state.title_cb) {
((*vt).pass_start)(obj, bytes);
}
}
unsafe extern "system" fn opus_cb_pass_end(this: *mut c_void) {
let cb = &*(this as *const OpusCallback);
unsafe extern "system" fn decoded_cb_pass_end(this: *mut c_void) {
let cb = &*(this as *const DecodedCallback);
if let Some((obj, vt)) = title_cb_vtbl(cb.state.title_cb) {
((*vt).pass_end)(obj);
}
}
unsafe extern "system" fn opus_cb_stream_end(this: *mut c_void) {
let cb = &*(this as *const OpusCallback);
unsafe extern "system" fn decoded_cb_stream_end(this: *mut c_void) {
let cb = &*(this as *const DecodedCallback);
if let Some((obj, vt)) = title_cb_vtbl(cb.state.title_cb) {
((*vt).stream_end)(obj);
}
}
unsafe extern "system" fn opus_cb_buffer_start(this: *mut c_void, ctx: *mut c_void) {
let cb = &*(this as *const OpusCallback);
unsafe extern "system" fn decoded_cb_buffer_start(this: *mut c_void, ctx: *mut c_void) {
let cb = &*(this as *const DecodedCallback);
let orig = cb
.state
.live_buffers
@@ -525,8 +570,8 @@ unsafe extern "system" fn opus_cb_buffer_start(this: *mut c_void, ctx: *mut c_vo
}
}
unsafe extern "system" fn opus_cb_buffer_end(this: *mut c_void, ctx: *mut c_void) {
let cb = &*(this as *const OpusCallback);
unsafe extern "system" fn decoded_cb_buffer_end(this: *mut c_void, ctx: *mut c_void) {
let cb = &*(this as *const DecodedCallback);
// Remove and drop the decoded PCM buffer before invoking the title's callback so
// the lock is released and no double-free or deadlock can occur.
let removed = cb.state.live_buffers.lock().unwrap().remove(&(ctx as usize));
@@ -537,8 +582,8 @@ unsafe extern "system" fn opus_cb_buffer_end(this: *mut c_void, ctx: *mut c_void
}
}
unsafe extern "system" fn opus_cb_loop_end(this: *mut c_void, ctx: *mut c_void) {
let cb = &*(this as *const OpusCallback);
unsafe extern "system" fn decoded_cb_loop_end(this: *mut c_void, ctx: *mut c_void) {
let cb = &*(this as *const DecodedCallback);
let orig = cb
.state
.live_buffers
@@ -552,8 +597,8 @@ unsafe extern "system" fn opus_cb_loop_end(this: *mut c_void, ctx: *mut c_void)
}
}
unsafe extern "system" fn opus_cb_voice_error(this: *mut c_void, ctx: *mut c_void, hr: HRESULT) {
let cb = &*(this as *const OpusCallback);
unsafe extern "system" fn decoded_cb_voice_error(this: *mut c_void, ctx: *mut c_void, hr: HRESULT) {
let cb = &*(this as *const DecodedCallback);
let orig = cb
.state
.live_buffers
@@ -567,14 +612,14 @@ unsafe extern "system" fn opus_cb_voice_error(this: *mut c_void, ctx: *mut c_voi
}
}
static OPUS_CALLBACK_VTBL: VoiceCallbackVtbl = VoiceCallbackVtbl {
pass_start: opus_cb_pass_start,
pass_end: opus_cb_pass_end,
stream_end: opus_cb_stream_end,
buffer_start: opus_cb_buffer_start,
buffer_end: opus_cb_buffer_end,
loop_end: opus_cb_loop_end,
voice_error: opus_cb_voice_error,
static DECODED_CALLBACK_VTBL: VoiceCallbackVtbl = VoiceCallbackVtbl {
pass_start: decoded_cb_pass_start,
pass_end: decoded_cb_pass_end,
stream_end: decoded_cb_stream_end,
buffer_start: decoded_cb_buffer_start,
buffer_end: decoded_cb_buffer_end,
loop_end: decoded_cb_loop_end,
voice_error: decoded_cb_voice_error,
};
// ---------------------------------------------------------------------------------------
@@ -585,10 +630,10 @@ static OPUS_CALLBACK_VTBL: VoiceCallbackVtbl = VoiceCallbackVtbl {
static ORIGINALS: Mutex<Vec<(usize, usize, usize)>> = Mutex::new(Vec::new());
/// Live silent voices (voice pointer → info).
static XMA: Mutex<Option<HashMap<usize, XmaInfo>>> = Mutex::new(None);
/// Live decoded Opus voices (voice pointer → handle).
static OPUS_VOICES: Mutex<Option<HashMap<usize, OpusVoiceHandle>>> = Mutex::new(None);
/// Count of live Opus voices (fast path for non-Opus voices).
static OPUS_LIVE_COUNT: AtomicUsize = AtomicUsize::new(0);
/// Live decoded voices (Opus & XMA1/XMA2: voice pointer → handle).
static DECODED_VOICES: Mutex<Option<HashMap<usize, DecodedVoiceHandle>>> = Mutex::new(None);
/// Count of live decoded voices (fast path for plain PCM voices).
static DECODED_LIVE_COUNT: AtomicUsize = AtomicUsize::new(0);
unsafe fn vtable(obj: *mut c_void) -> *mut usize {
*(obj as *const *mut usize)
@@ -677,6 +722,55 @@ pub unsafe extern "system" fn XAudio2CreateWithVersionInfo(pp: *mut *mut c_void,
create_engine(pp, flags, processor)
}
unsafe fn create_decoded_voice(
orig: CreateSourceVoiceFn,
this: *mut c_void,
pp: *mut *mut c_void,
flags: u32,
max_ratio: f32,
cb: *mut c_void,
sends: *const c_void,
chain: *const c_void,
decoder: VoiceDecoder,
channels: u16,
rate: u32,
block_align: u16,
avg_bytes: u32,
label: &str,
) -> HRESULT {
let pcm = pcm16_for_rate(channels, rate);
let state = Arc::new(DecodedVoiceState {
decoder: Mutex::new(decoder),
block_align,
channels: pcm.channels,
rate: pcm.rate,
avg_bytes,
title_cb: cb as usize,
live_buffers: Mutex::new(HashMap::new()),
});
let wrapped_cb = Box::into_raw(Box::new(DecodedCallback {
vtbl: &DECODED_CALLBACK_VTBL,
state: Arc::clone(&state),
}));
let hr = orig(this, pp, &pcm, flags, max_ratio, wrapped_cb as *mut c_void, sends, chain);
if hr < 0 || (*pp).is_null() {
drop(Box::from_raw(wrapped_cb));
SOURCE_ERRORS.fetch_add(1, Ordering::Relaxed);
crate::xlog_warn!("XAudio2: CreateSourceVoice({label}→PCM16) failed ({:#x})", hr as u32);
return hr;
}
let voice = *pp;
patch_source_voice(voice);
DECODED_VOICES
.lock()
.unwrap()
.get_or_insert_with(HashMap::new)
.insert(voice as usize, DecodedVoiceHandle { state, cb_ptr: wrapped_cb as usize });
DECODED_LIVE_COUNT.fetch_add(1, Ordering::Release);
SOURCE_VOICES.fetch_add(1, Ordering::Relaxed);
hr
}
unsafe extern "system" fn create_source_voice(
this: *mut c_void,
pp: *mut *mut c_void,
@@ -702,37 +796,23 @@ unsafe extern "system" fn create_source_voice(
Ok(dec) => {
let pcm_ch = dec.channels();
let pcm_rate = dec.output_rate();
let pcm = pcm16_for_rate(pcm_ch, pcm_rate);
let state = Arc::new(OpusVoiceState {
decoder: Mutex::new(dec),
block_align: of.block_align,
channels: pcm_ch,
rate: pcm_rate,
avg_bytes: of.avg_bytes,
title_cb: cb as usize,
live_buffers: Mutex::new(HashMap::new()),
});
let wrapped_cb = Box::into_raw(Box::new(OpusCallback {
vtbl: &OPUS_CALLBACK_VTBL,
state: Arc::clone(&state),
}));
let hr = orig(this, pp, &pcm, flags, max_ratio, wrapped_cb as *mut c_void, sends, chain);
if hr < 0 || (*pp).is_null() {
drop(Box::from_raw(wrapped_cb));
SOURCE_ERRORS.fetch_add(1, Ordering::Relaxed);
crate::xlog_warn!("XAudio2: CreateSourceVoice(Opus→PCM16) failed ({:#x})", hr as u32);
return hr;
}
let voice = *pp;
patch_source_voice(voice);
OPUS_VOICES
.lock()
.unwrap()
.get_or_insert_with(HashMap::new)
.insert(voice as usize, OpusVoiceHandle { state, cb_ptr: wrapped_cb as usize });
OPUS_LIVE_COUNT.fetch_add(1, Ordering::Release);
SOURCE_VOICES.fetch_add(1, Ordering::Relaxed);
if OPUS_VOICES_CREATED.fetch_add(1, Ordering::Relaxed) == 0 {
let hr = create_decoded_voice(
orig,
this,
pp,
flags,
max_ratio,
cb,
sends,
chain,
VoiceDecoder::Opus(dec),
pcm_ch,
pcm_rate,
of.block_align,
of.avg_bytes,
"Opus",
);
if hr >= 0 && OPUS_VOICES_CREATED.fetch_add(1, Ordering::Relaxed) == 0 {
crate::xlog_info!("XAudio2: Opus voice ({pcm_ch} ch., {pcm_rate} Hz) → real PCM16 decoding via MSOpusDecoder");
}
return hr;
@@ -743,7 +823,42 @@ unsafe extern "system" fn create_source_voice(
}
}
// 2. XMA/XMA2 or other formats (including Opus fallback if MSOpusDecoder is missing)
// 2. XMA / XMA2 voice (WAVE_FORMAT_XMA 0x0165 or WAVE_FORMAT_XMA2 0x0166)
if let Some(cfg_res) = xma_stream_config(fmt) {
let info = xma_info(fmt);
match cfg_res.and_then(xma2::Decoder::new) {
Ok(dec) => {
let pcm_ch = dec.channels();
let pcm_rate = dec.sample_rate();
let label = if tag == WAVE_FORMAT_XMA2 { "XMA2" } else { "XMA" };
let hr = create_decoded_voice(
orig,
this,
pp,
flags,
max_ratio,
cb,
sends,
chain,
VoiceDecoder::Xma { decoder: dec, info },
pcm_ch,
pcm_rate,
info.block_align,
info.avg_bytes,
label,
);
if hr >= 0 && XMA_VOICES.fetch_add(1, Ordering::Relaxed) == 0 {
crate::xlog_info!("XAudio2: {label} voice ({pcm_ch} ch., {pcm_rate} Hz) → real PCM16 decoding via hle::xma2");
}
return hr;
}
Err(e) => {
crate::xlog_warn!("XAudio2: XMA decoder init failed ({e}) – falling back to silence");
}
}
}
// 3. Fallback for unknown compressed formats or unavailable decoder
let mut silent = (is_xma(tag) || opus_format(fmt).is_some()).then(|| xma_info(fmt));
let mut hr = match &silent {
Some(i) => orig(this, pp, &pcm16_for(i), flags, max_ratio, cb, sends, chain),
@@ -849,10 +964,10 @@ fn silence() -> *const u8 {
*P.get_or_init(|| unsafe { VirtualAlloc(std::ptr::null(), SILENCE_BYTES, MEM_COMMIT | MEM_RESERVE, PAGE_READONLY) as usize }) as *const u8
}
unsafe fn submit_opus_buffer(
unsafe fn submit_decoded_buffer(
this: *mut c_void,
orig: SubmitSourceBufferFn,
state: &OpusVoiceState,
state: &DecodedVoiceState,
orig_buf: XAudio2Buffer,
) -> HRESULT {
let data = if !orig_buf.audio_data.is_null() && orig_buf.audio_bytes > 0 {
@@ -860,14 +975,31 @@ unsafe fn submit_opus_buffer(
} else {
&[]
};
let eos = (orig_buf.flags & XAUDIO2_END_OF_STREAM) != 0;
let decoded_pcm = {
let (decoded_pcm, fallback_info, is_xma_voice) = {
let mut dec = state.decoder.lock().unwrap();
let res = dec.decode_buffer(data, state.block_align);
if (orig_buf.flags & XAUDIO2_END_OF_STREAM) != 0 {
dec.flush();
match &mut *dec {
VoiceDecoder::Opus(d) => {
let res = d.decode_buffer(data, state.block_align);
if eos {
d.flush();
}
let info = XmaInfo {
channels: state.channels,
rate: state.rate,
avg_bytes: state.avg_bytes,
block_align: state.block_align,
is_opus: true,
..Default::default()
};
(res, info, false)
}
VoiceDecoder::Xma { decoder, info } => {
let res = decoder.decode_buffer_i16(data, eos);
(res, *info, true)
}
}
res
};
let mut nb = orig_buf;
@@ -879,26 +1011,24 @@ unsafe fn submit_opus_buffer(
for s in samples {
bytes.extend_from_slice(&s.to_le_bytes());
}
OPUS_BUFFERS_DECODED.fetch_add(1, Ordering::Relaxed);
if is_xma_voice {
XMA_BUFFERS_DECODED.fetch_add(1, Ordering::Relaxed);
} else {
OPUS_BUFFERS_DECODED.fetch_add(1, Ordering::Relaxed);
}
bytes.into_boxed_slice()
}
res => {
if let Err(e) = res {
if OPUS_DECODE_ERRORS.fetch_add(1, Ordering::Relaxed) == 0 {
crate::xlog_warn!("XAudio2: Opus decode error ({e}) – substituting silence");
let err_counter = if is_xma_voice { &XMA_DECODE_ERRORS } else { &OPUS_DECODE_ERRORS };
let codec_name = if is_xma_voice { "XMA" } else { "Opus" };
let prev = err_counter.fetch_add(1, Ordering::Relaxed);
if prev == 0 {
match res {
Err(e) => crate::xlog_warn!("XAudio2: {codec_name} decode error ({e}) – substituting silence"),
Ok(_) => crate::xlog_warn!("XAudio2: {codec_name} buffer decoded 0 samples – substituting silence"),
}
} else {
OPUS_DECODE_ERRORS.fetch_add(1, Ordering::Relaxed);
}
let info = XmaInfo {
channels: state.channels,
rate: state.rate,
avg_bytes: state.avg_bytes,
block_align: state.block_align,
is_opus: true,
..Default::default()
};
let sb = silent_buffer(&info, &orig_buf, std::ptr::null(), SILENCE_BYTES);
let sb = silent_buffer(&fallback_info, &orig_buf, std::ptr::null(), SILENCE_BYTES);
nb = sb;
SILENCED.fetch_add(1, Ordering::Relaxed);
vec![0u8; sb.audio_bytes as usize].into_boxed_slice()
@@ -925,14 +1055,14 @@ unsafe fn submit_opus_buffer(
unsafe extern "system" fn submit_source_buffer(this: *mut c_void, buf: *const XAudio2Buffer, wma: *const c_void) -> HRESULT {
let Some(orig) = original(this, VOICE_SUBMIT_SOURCE_BUFFER) else { return E_FAIL };
let orig: SubmitSourceBufferFn = std::mem::transmute(orig);
if !buf.is_null() && OPUS_LIVE_COUNT.load(Ordering::Acquire) != 0 {
let state = OPUS_VOICES
if !buf.is_null() && DECODED_LIVE_COUNT.load(Ordering::Acquire) != 0 {
let state = DECODED_VOICES
.lock()
.unwrap()
.as_ref()
.and_then(|m| m.get(&(this as usize)).map(|h| Arc::clone(&h.state)));
if let Some(state) = state {
return submit_opus_buffer(this, orig, &state, buf.read_unaligned());
return submit_decoded_buffer(this, orig, &state, buf.read_unaligned());
}
}
let info = XMA.lock().unwrap().as_ref().and_then(|m| m.get(&(this as usize)).copied());
@@ -954,8 +1084,8 @@ unsafe extern "system" fn flush_source_buffers(this: *mut c_void) -> HRESULT {
let Some(orig) = original(this, VOICE_FLUSH_SOURCE_BUFFERS) else { return E_FAIL };
let orig: FlushSourceBuffersFn = std::mem::transmute(orig);
let hr = orig(this);
if hr >= 0 && OPUS_LIVE_COUNT.load(Ordering::Acquire) != 0 {
let state = OPUS_VOICES
if hr >= 0 && DECODED_LIVE_COUNT.load(Ordering::Acquire) != 0 {
let state = DECODED_VOICES
.lock()
.unwrap()
.as_ref()
@@ -971,8 +1101,8 @@ unsafe extern "system" fn get_state(this: *mut c_void, state_out: *mut XAudio2Vo
let Some(orig) = original(this, VOICE_GET_STATE) else { return };
let orig: GetStateFn = std::mem::transmute(orig);
orig(this, state_out, flags);
if !state_out.is_null() && OPUS_LIVE_COUNT.load(Ordering::Acquire) != 0 {
let voice_state = OPUS_VOICES
if !state_out.is_null() && DECODED_LIVE_COUNT.load(Ordering::Acquire) != 0 {
let voice_state = DECODED_VOICES
.lock()
.unwrap()
.as_ref()
@@ -994,20 +1124,20 @@ unsafe extern "system" fn destroy_voice(this: *mut c_void) {
if let Some(m) = XMA.lock().unwrap().as_mut() {
m.remove(&(this as usize));
}
let opus_handle = if OPUS_LIVE_COUNT.load(Ordering::Acquire) != 0 {
OPUS_VOICES.lock().unwrap().as_mut().and_then(|m| m.remove(&(this as usize)))
let decoded_handle = if DECODED_LIVE_COUNT.load(Ordering::Acquire) != 0 {
DECODED_VOICES.lock().unwrap().as_mut().and_then(|m| m.remove(&(this as usize)))
} else {
None
};
if opus_handle.is_some() {
OPUS_LIVE_COUNT.fetch_sub(1, Ordering::Release);
if decoded_handle.is_some() {
DECODED_LIVE_COUNT.fetch_sub(1, Ordering::Release);
}
// Destroy the Windows voice first so its audio thread stops using the callback and any
// queued PCM buffers before we free them.
orig(this);
if let Some(h) = opus_handle {
if let Some(h) = decoded_handle {
h.state.live_buffers.lock().unwrap().clear();
drop(Box::from_raw(h.cb_ptr as *mut OpusCallback));
drop(Box::from_raw(h.cb_ptr as *mut DecodedCallback));
}
}
@@ -1275,7 +1405,7 @@ mod tests {
assert!(STREAM_END.load(Ordering::SeqCst) >= 1);
// All decoded buffers must have been freed on OnBufferEnd!
let remaining = OPUS_VOICES
let remaining = DECODED_VOICES
.lock()
.unwrap()
.as_ref()
@@ -1295,4 +1425,153 @@ mod tests {
release(eng);
}
}
#[test]
fn xma2_source_voice_decodes_and_manages_lifetime() {
use std::sync::atomic::AtomicUsize;
static ENDED: AtomicUsize = AtomicUsize::new(0);
static STARTED: AtomicUsize = AtomicUsize::new(0);
static STREAM_END: AtomicUsize = AtomicUsize::new(0);
unsafe extern "system" fn on_pass_start(_: *mut c_void, _: u32) {}
unsafe extern "system" fn on_nothing(_: *mut c_void) {}
unsafe extern "system" fn on_stream_end(_: *mut c_void) {
STREAM_END.fetch_add(1, Ordering::SeqCst);
}
unsafe extern "system" fn on_buf_start(_: *mut c_void, ctx: *mut c_void) {
STARTED.fetch_add(ctx as usize, Ordering::SeqCst);
}
unsafe extern "system" fn on_buf_end(_: *mut c_void, ctx: *mut c_void) {
ENDED.fetch_add(ctx as usize, Ordering::SeqCst);
}
unsafe extern "system" fn on_loop(_: *mut c_void, _: *mut c_void) {}
unsafe extern "system" fn on_err(_: *mut c_void, _: *mut c_void, _: HRESULT) {}
static TEST_VTBL: VoiceCallbackVtbl = VoiceCallbackVtbl {
pass_start: on_pass_start,
pass_end: on_nothing,
stream_end: on_stream_end,
buffer_start: on_buf_start,
buffer_end: on_buf_end,
loop_end: on_loop,
voice_error: on_err,
};
#[repr(C)]
struct TestCb {
vtbl: &'static VoiceCallbackVtbl,
}
static TEST_CB: TestCb = TestCb { vtbl: &TEST_VTBL };
unsafe {
let mut eng: *mut c_void = std::ptr::null_mut();
assert_eq!(XAudio2Create(&mut eng, 0, 1), S_OK);
let vt_eng = vtable(eng);
let create_master: CreateMasteringVoiceFn = std::mem::transmute(vt_eng.add(ENGINE_CREATE_MASTERING_VOICE).read());
let mut master: *mut c_void = std::ptr::null_mut();
if create_master(eng, &mut master, 0, 0, 0, std::ptr::null(), std::ptr::null(), AUDIO_CATEGORY_GAME_EFFECTS) < 0 {
let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(vt_eng.add(2).read());
release(eng);
eprintln!("skipping: no audio device");
return;
}
let create_src: CreateSourceVoiceFn = std::mem::transmute(vt_eng.add(ENGINE_CREATE_SOURCE_VOICE).read());
let mut xma_fmt = xma2(1, 48_000, 6080, 2048, 1);
xma_fmt.num_streams = 1;
let mut voice: *mut c_void = std::ptr::null_mut();
let hr = create_src(
eng,
&mut voice,
&xma_fmt as *const Xma2WaveFormatEx as *const WaveFormatEx,
0,
2.0,
&TEST_CB as *const _ as *mut c_void,
std::ptr::null(),
std::ptr::null(),
);
assert_eq!(hr, S_OK, "CreateSourceVoice(XMA2) → {:#x}", hr as u32);
assert!(!voice.is_null());
let pkt = xma2::tests::pad_packet(xma2::tests::MONO_48K_PREFIX);
let bad_pkt = vec![0x5Au8; xma2::PACKET_SIZE];
let vt_v = vtable(voice);
let submit: SubmitSourceBufferFn = std::mem::transmute(vt_v.add(VOICE_SUBMIT_SOURCE_BUFFER).read());
let get_st: GetStateFn = std::mem::transmute(vt_v.add(VOICE_GET_STATE).read());
let start: unsafe extern "system" fn(*mut c_void, u32, u32) -> HRESULT = std::mem::transmute(vt_v.add(19).read());
let stop: unsafe extern "system" fn(*mut c_void, u32, u32) -> HRESULT = std::mem::transmute(vt_v.add(20).read());
let flush: FlushSourceBuffersFn = std::mem::transmute(vt_v.add(VOICE_FLUSH_SOURCE_BUFFERS).read());
let destroy: DestroyVoiceFn = std::mem::transmute(vt_v.add(VOICE_DESTROY).read());
const CTX_VALID: usize = 0x3003;
const CTX_BAD: usize = 0x4004;
let b_valid = XAudio2Buffer {
flags: XAUDIO2_END_OF_STREAM,
audio_bytes: pkt.len() as u32,
audio_data: pkt.as_ptr(),
play_begin: 0,
play_length: 2400, // 50 ms slice of the 6080 decoded samples
loop_begin: 0,
loop_length: 0,
loop_count: 0,
context: CTX_VALID as *mut c_void,
};
// Corrupt buffer must gracefully fall back to silence and increment xma_decode_errors.
let b_bad = XAudio2Buffer {
flags: XAUDIO2_END_OF_STREAM,
audio_bytes: bad_pkt.len() as u32,
audio_data: bad_pkt.as_ptr(),
play_begin: 0,
play_length: 480,
loop_begin: 0,
loop_length: 0,
loop_count: 0,
context: CTX_BAD as *mut c_void,
};
let before_decoded = stats().xma_buffers_decoded;
let before_errors = stats().xma_decode_errors;
assert_eq!(submit(voice, &b_valid, std::ptr::null()), S_OK);
assert_eq!(submit(voice, &b_bad, std::ptr::null()), S_OK);
assert_eq!(stats().xma_buffers_decoded, before_decoded + 1);
assert_eq!(stats().xma_decode_errors, before_errors + 1);
let mut st = XAudio2VoiceState { current_buffer_context: std::ptr::null_mut(), buffers_queued: 0, samples_played: 0 };
get_st(voice, &mut st, 0);
assert_eq!({ st.buffers_queued }, 2);
assert_eq!({ st.current_buffer_context } as usize, CTX_VALID);
let set_vol: unsafe extern "system" fn(*mut c_void, f32, u32) -> HRESULT = std::mem::transmute(vt_v.add(12).read());
let _ = set_vol(voice, 0.0, 0);
assert_eq!(start(voice, 0, 0), S_OK);
let t0 = std::time::Instant::now();
while (ENDED.load(Ordering::SeqCst) != CTX_VALID + CTX_BAD || STREAM_END.load(Ordering::SeqCst) < 2)
&& t0.elapsed() < std::time::Duration::from_secs(3)
{
std::thread::sleep(std::time::Duration::from_millis(10));
}
assert_eq!(STARTED.load(Ordering::SeqCst), CTX_VALID + CTX_BAD);
assert_eq!(ENDED.load(Ordering::SeqCst), CTX_VALID + CTX_BAD);
assert!(STREAM_END.load(Ordering::SeqCst) >= 2);
let remaining = DECODED_VOICES
.lock()
.unwrap()
.as_ref()
.and_then(|m| m.get(&(voice as usize)).map(|h| h.state.live_buffers.lock().unwrap().len()))
.unwrap_or(999);
assert_eq!(remaining, 0, "decoded XMA2 PCM buffers leaked after OnBufferEnd");
let _ = stop(voice, 0, 0);
assert_eq!(submit(voice, &b_valid, std::ptr::null()), S_OK);
assert_eq!(flush(voice), S_OK);
destroy(voice);
destroy(master);
let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(vt_eng.add(2).read());
release(eng);
}
}
}
File diff suppressed because it is too large Load Diff
+391
View File
@@ -0,0 +1,391 @@
//! Static Huffman, critical-frequency, and decorrelation tables for XMA1/XMA2 decoding.
//!
//! Ported from FFmpeg `libavcodec/wmaprodata.h` (LGPL-2.1+, Copyright (c) 2007-2009
//! Baptiste Coudurier, Benjamin Larsson, Ulion, Sascha Sommer), adapted to Rust for XCL
//! (GPL-2.0-only) on 2026-11-21.
/// Critical frequencies (Hz) dividing the spectrum into scale factor bands.
pub const CRITICAL_FREQ: [u16; 28] = [
100, 200, 300, 400, 510, 630, 770, 920,
1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150,
3700, 4400, 5300, 6400, 7700, 9500, 12000, 15500,
20675, 28575, 41375, 63875,
];
/// DPCM-coded scale factor Huffman table: `(symbol, bit_length)` (applied with offset `-60`).
pub const HUFF_SCALE_MAXBITS: usize = 19;
pub const SCALE_TABLE: [(u8, u8); 121] = [
( 58, 5), ( 64, 6), ( 66, 7), ( 65, 7), ( 62, 5), ( 63, 6),
( 68, 9), ( 69, 10), ( 54, 15), ( 19, 19), ( 20, 19), ( 21, 19),
( 22, 19), ( 23, 19), ( 24, 19), ( 25, 19), ( 26, 19), ( 27, 19),
( 28, 19), ( 29, 19), ( 30, 19), ( 31, 19), ( 32, 19), ( 33, 19),
( 34, 19), ( 17, 19), ( 36, 19), ( 37, 19), ( 38, 19), ( 39, 19),
( 40, 19), ( 41, 19), ( 42, 19), ( 43, 19), ( 44, 19), ( 45, 19),
( 46, 19), ( 47, 19), ( 48, 19), ( 49, 19), ( 50, 19), ( 51, 19),
( 52, 19), ( 15, 19), ( 16, 19), ( 14, 19), ( 13, 19), ( 12, 19),
( 11, 19), ( 10, 19), ( 0, 19), ( 9, 19), ( 8, 19), ( 7, 19),
( 6, 19), ( 5, 19), ( 4, 19), ( 55, 13), ( 70, 13), ( 3, 19),
( 2, 19), ( 1, 19), ( 35, 19), ( 71, 19), ( 72, 19), ( 73, 19),
( 74, 19), ( 75, 19), ( 76, 19), ( 77, 19), ( 78, 19), ( 79, 19),
( 80, 19), ( 81, 19), ( 82, 19), ( 83, 19), ( 84, 19), ( 85, 19),
( 86, 19), ( 87, 19), ( 88, 19), ( 89, 19), ( 90, 19), ( 91, 19),
( 92, 19), ( 93, 19), ( 94, 19), ( 95, 19), ( 96, 19), ( 97, 19),
( 98, 19), ( 99, 19), (100, 19), (101, 19), (102, 19), (103, 19),
(104, 19), (105, 19), (106, 19), (107, 19), (108, 19), (109, 19),
(110, 19), (111, 19), (112, 19), (113, 19), (114, 19), (115, 19),
(116, 19), (117, 19), (118, 19), (119, 19), (120, 19), ( 18, 18),
( 53, 16), ( 56, 11), ( 57, 8), ( 67, 7), ( 61, 3), ( 59, 2),
( 60, 1),
];
/// Run-length delta scale factor Huffman table: `(symbol, bit_length)`.
pub const HUFF_SCALE_RL_MAXBITS: usize = 21;
pub const SCALE_RL_TABLE: [(u8, u8); 120] = [
(103, 7), ( 80, 11), ( 60, 11), ( 18, 10), ( 56, 10), ( 21, 12),
( 90, 12), ( 58, 11), ( 27, 11), ( 69, 12), ( 84, 15), ( 48, 15),
( 86, 14), ( 47, 13), ( 19, 10), ( 32, 9), ( 78, 6), ( 5, 5),
( 28, 4), ( 53, 5), ( 9, 7), ( 31, 8), ( 38, 8), ( 10, 7),
( 88, 11), ( 25, 12), (105, 12), (118, 11), ( 23, 12), ( 82, 14),
( 98, 16), (110, 16), (108, 15), ( 93, 13), ( 68, 10), ( 72, 12),
( 97, 12), ( 81, 12), ( 42, 12), ( 64, 8), ( 4, 4), ( 1, 2),
( 7, 6), ( 14, 7), ( 0, 9), ( 55, 9), ( 61, 9), (117, 10),
( 24, 12), ( 44, 12), ( 67, 12), ( 70, 16), ( 99, 18), ( 96, 21),
( 95, 21), ( 2, 21), ( 77, 21), ( 52, 21), (111, 21), (102, 20),
(101, 17), ( 46, 15), ( 73, 15), (109, 15), ( 51, 14), ( 92, 14),
( 30, 7), ( 11, 7), ( 66, 7), ( 15, 8), ( 16, 8), (116, 9),
( 65, 9), ( 57, 10), ( 59, 10), (115, 9), ( 12, 7), ( 35, 9),
( 17, 9), ( 41, 9), ( 20, 11), ( 91, 11), ( 26, 12), ( 75, 15),
( 45, 15), (107, 14), ( 83, 14), (100, 15), ( 89, 15), ( 43, 11),
( 62, 9), ( 37, 9), (104, 8), ( 6, 5), ( 39, 8), ( 40, 9),
( 34, 9), ( 79, 7), ( 8, 6), ( 63, 6), ( 87, 12), ( 94, 14),
( 49, 14), ( 50, 13), ( 22, 11), (119, 10), ( 33, 9), ( 36, 9),
(113, 11), (106, 12), (112, 13), ( 71, 15), ( 85, 15), ( 74, 14),
( 76, 10), (114, 7), ( 29, 5), ( 54, 6), ( 13, 6), ( 3, 2),
];
pub const SCALE_RL_RUN: [u8; 120] = [
0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 0, 1, 0, 1, 0, 1,
];
pub const SCALE_RL_LEVEL: [u8; 120] = [
0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 7, 7, 8, 8, 9, 9,
];
/// Coefficient run-length Huffman table 0.
pub const HUFF_COEF0_MAXBITS: usize = 21;
pub const COEF0_LENS: [u8; 272] = [
2, 9, 14, 14, 13, 12, 13, 14, 15, 15, 12, 10, 10, 10, 13, 14,
15, 15, 12, 11, 13, 14, 14, 13, 15, 15, 14, 12, 12, 8, 10, 10,
15, 15, 14, 13, 14, 14, 13, 15, 20, 20, 19, 21, 21, 20, 19, 17,
17, 18, 18, 15, 15, 13, 12, 14, 15, 15, 14, 15, 15, 12, 11, 6,
7, 8, 9, 13, 13, 13, 14, 14, 11, 10, 7, 8, 14, 14, 14, 14,
12, 13, 13, 12, 12, 12, 11, 9, 13, 14, 14, 12, 11, 11, 11, 9,
8, 7, 14, 15, 15, 14, 14, 12, 13, 15, 16, 17, 17, 14, 12, 12,
12, 15, 15, 14, 14, 14, 13, 13, 9, 9, 11, 11, 10, 7, 6, 13,
15, 15, 14, 14, 14, 13, 14, 15, 15, 13, 14, 14, 14, 14, 10, 9,
10, 10, 11, 11, 10, 8, 9, 13, 14, 14, 12, 11, 14, 15, 15, 13,
12, 14, 14, 14, 14, 13, 14, 14, 3, 5, 8, 10, 10, 15, 15, 14,
14, 16, 16, 15, 12, 11, 11, 11, 7, 8, 8, 9, 12, 13, 13, 12,
14, 15, 15, 13, 10, 11, 11, 13, 14, 14, 13, 14, 14, 11, 10, 13,
15, 15, 14, 12, 11, 4, 6, 6, 8, 12, 12, 12, 13, 13, 12, 13,
13, 14, 14, 13, 13, 13, 9, 7, 9, 11, 14, 14, 13, 14, 14, 13,
10, 8, 7, 5, 9, 12, 13, 14, 15, 15, 12, 12, 10, 14, 14, 13,
12, 13, 14, 14, 12, 13, 13, 12, 12, 12, 9, 7, 6, 3, 4, 4,
];
pub const COEF0_SYMS: [u16; 272] = [
2, 25, 111, 94, 69, 58, 87, 93, 136, 135, 59, 37,
34, 36, 82, 182, 120, 138, 195, 45, 168, 216, 178, 86,
140, 219, 186, 162, 239, 18, 156, 35, 127, 236, 109, 85,
180, 253, 88, 147, 268, 264, 256, 266, 270, 262, 260, 248,
246, 252, 258, 137, 189, 230, 64, 179, 146, 208, 101, 118,
238, 163, 46, 9, 153, 0, 26, 247, 169, 76, 202, 131,
194, 38, 13, 19, 132, 106, 191, 97, 65, 198, 77, 62,
66, 164, 48, 27, 81, 183, 102, 60, 47, 49, 159, 227,
20, 14, 112, 263, 144, 217, 104, 63, 79, 209, 269, 250,
254, 203, 241, 196, 61, 220, 148, 124, 185, 100, 80, 78,
193, 28, 50, 235, 41, 1, 10, 171, 226, 150, 103, 114,
115, 170, 105, 211, 149, 249, 108, 188, 107, 255, 231, 155,
42, 40, 55, 160, 39, 21, 29, 215, 234, 184, 228, 51,
116, 142, 145, 172, 165, 181, 130, 113, 117, 89, 128, 204,
3, 7, 154, 157, 43, 141, 265, 133, 225, 271, 244, 221,
74, 54, 56, 52, 15, 222, 22, 30, 83, 199, 173, 73,
123, 210, 143, 175, 44, 53, 237, 174, 139, 134, 110, 218,
129, 161, 213, 177, 267, 151, 125, 67, 223, 5, 11, 192,
23, 214, 243, 166, 200, 176, 68, 224, 187, 257, 261, 232,
96, 251, 31, 16, 32, 57, 207, 121, 91, 126, 119, 99,
158, 24, 212, 8, 33, 70, 92, 205, 240, 242, 75, 197,
233, 259, 190, 98, 71, 201, 122, 206, 72, 90, 95, 84,
167, 245, 229, 17, 12, 4, 152, 6,
];
/// Coefficient run-length Huffman table 1: `(symbol, bit_length)`.
pub const HUFF_COEF1_MAXBITS: usize = 22;
pub const COEF1_TABLE: [(u8, u8); 244] = [
( 2, 2), ( 3, 3), (102, 3), ( 4, 4), (148, 6), (134, 9),
(171, 10), ( 18, 10), ( 11, 8), (159, 8), ( 14, 9), (156, 14),
(235, 15), ( 61, 15), ( 38, 13), (153, 13), ( 48, 14), ( 49, 14),
( 23, 11), (203, 13), (208, 19), (204, 19), (129, 18), ( 94, 17),
( 87, 16), ( 62, 15), (174, 15), (147, 15), ( 29, 12), (191, 12),
( 64, 15), ( 65, 15), (146, 14), (164, 13), (142, 5), (132, 4),
(103, 5), (154, 7), (165, 9), (181, 11), (109, 12), ( 30, 12),
( 86, 16), ( 92, 16), (239, 15), (138, 14), ( 39, 13), ( 50, 14),
(115, 15), (238, 21), (228, 21), (236, 21), (222, 21), (216, 20),
(226, 20), (196, 18), (192, 17), (120, 16), (221, 14), ( 51, 14),
( 24, 11), (143, 8), ( 7, 6), ( 9, 7), (152, 10), (136, 12),
(160, 12), (241, 15), ( 66, 15), (168, 14), (219, 14), (113, 14),
(193, 12), ( 19, 10), (173, 10), (105, 8), (149, 9), ( 15, 9),
(205, 13), (207, 13), (125, 17), (190, 17), (182, 16), ( 68, 15),
( 70, 15), ( 67, 15), (137, 13), ( 31, 12), (223, 14), (116, 15),
(210, 19), (220, 19), (198, 18), (126, 17), ( 88, 16), ( 41, 13),
( 25, 11), ( 40, 13), ( 73, 15), (243, 15), ( 53, 14), (195, 12),
(183, 11), (225, 14), ( 52, 14), ( 71, 15), (121, 16), ( 89, 16),
(170, 14), ( 55, 14), ( 69, 15), ( 83, 15), (209, 13), (108, 11),
( 32, 12), ( 54, 14), (122, 16), (184, 16), (176, 15), ( 42, 13),
( 12, 8), (161, 8), ( 6, 5), (167, 9), (106, 9), ( 20, 10),
(145, 12), (111, 13), ( 43, 13), ( 26, 11), (175, 10), (107, 10),
( 34, 12), ( 33, 12), (197, 12), ( 74, 15), (128, 17), (232, 20),
(212, 20), (224, 19), (202, 18), ( 90, 16), ( 57, 14), (227, 14),
( 97, 16), ( 93, 16), (140, 15), (185, 11), ( 27, 11), ( 16, 9),
(158, 11), (211, 13), ( 56, 14), (117, 15), ( 72, 15), (166, 13),
( 91, 16), ( 95, 16), ( 80, 15), (101, 16), (194, 17), (127, 17),
( 82, 15), ( 21, 10), (144, 10), (177, 10), (151, 6), ( 10, 7),
(157, 7), ( 8, 6), ( 5, 4), ( 13, 8), ( 0, 9), (213, 13),
( 46, 13), (199, 12), ( 35, 12), (162, 12), (135, 10), (169, 9),
( 45, 13), ( 59, 14), (114, 14), ( 44, 13), (188, 16), (186, 16),
( 75, 15), ( 79, 15), (118, 15), (187, 11), (112, 13), (139, 14),
(178, 15), ( 81, 15), (110, 12), ( 28, 11), (163, 8), (133, 6),
(104, 6), ( 17, 9), ( 22, 10), (229, 14), (172, 14), (217, 13),
(201, 12), ( 36, 12), (218, 20), (242, 22), (240, 22), (234, 21),
(230, 19), (206, 18), (200, 18), (214, 18), (130, 17), (131, 17),
(141, 15), ( 84, 15), ( 76, 15), (215, 13), ( 58, 14), (231, 14),
(233, 14), (180, 15), ( 77, 15), ( 37, 12), (189, 11), (179, 10),
(155, 10), ( 47, 13), ( 96, 16), ( 99, 16), (119, 15), ( 63, 14),
(237, 14), ( 78, 15), ( 85, 15), ( 60, 14), ( 98, 16), (100, 16),
(124, 16), (123, 16), (150, 11), ( 1, 7),
];
pub const COEF0_RUN: [u16; 272] = [
0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
142, 143, 144, 145, 146, 147, 148, 149, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0,
];
pub const COEF0_LEVEL: [f32; 272] = [
0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0,
3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0,
3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0,
4.0, 4.0, 5.0, 5.0, 5.0, 5.0, 5.0, 6.0, 6.0, 7.0,
7.0, 8.0, 8.0, 9.0, 9.0, 10.0, 10.0, 11.0, 11.0, 12.0,
12.0, 13.0, 13.0, 14.0, 14.0, 15.0, 15.0, 16.0, 16.0, 17.0,
17.0, 18.0, 18.0, 19.0, 19.0, 20.0, 20.0, 21.0, 21.0, 22.0,
22.0, 23.0, 23.0, 24.0, 24.0, 25.0, 25.0, 26.0, 26.0, 27.0,
27.0, 28.0,
];
pub const COEF1_RUN: [u16; 244] = [
0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
2, 3, 4, 5, 0, 1, 2, 0, 1, 2, 0, 1,
2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 0, 0,
];
pub const COEF1_LEVEL: [f32; 244] = [
0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0,
3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 5.0, 5.0,
5.0, 6.0, 6.0, 6.0, 7.0, 7.0, 7.0, 8.0, 8.0, 9.0,
9.0, 10.0, 10.0, 11.0, 11.0, 12.0, 12.0, 13.0, 13.0, 14.0,
14.0, 15.0, 15.0, 16.0, 16.0, 17.0, 17.0, 18.0, 18.0, 19.0,
19.0, 20.0, 20.0, 21.0, 21.0, 22.0, 22.0, 23.0, 23.0, 24.0,
24.0, 25.0, 25.0, 26.0, 26.0, 27.0, 27.0, 28.0, 28.0, 29.0,
29.0, 30.0, 30.0, 31.0, 31.0, 32.0, 32.0, 33.0, 33.0, 34.0,
34.0, 35.0, 35.0, 36.0, 36.0, 37.0, 37.0, 38.0, 38.0, 39.0,
39.0, 40.0, 40.0, 41.0, 41.0, 42.0, 42.0, 43.0, 43.0, 44.0,
44.0, 45.0, 45.0, 46.0, 46.0, 47.0, 47.0, 48.0, 48.0, 49.0,
49.0, 50.0, 51.0, 52.0,
];
/// 4-element vector coefficient Huffman table (applied with offset `-1`).
pub const HUFF_VEC4_MAXBITS: usize = 14;
pub const VEC4_LENS: [u8; 127] = [
1, 6, 8, 10, 10, 10, 10, 8, 8, 10, 10, 9, 8, 8, 9, 12,
12, 11, 12, 12, 11, 9, 9, 8, 8, 9, 9, 8, 8, 9, 9, 12,
12, 12, 14, 14, 13, 11, 11, 9, 8, 9, 9, 11, 11, 10, 9, 8,
6, 6, 6, 6, 6, 6, 11, 11, 10, 11, 11, 10, 10, 11, 11, 9,
7, 6, 7, 7, 6, 6, 6, 5, 7, 11, 11, 10, 9, 8, 6, 9,
9, 10, 10, 9, 8, 8, 6, 6, 6, 8, 8, 9, 12, 12, 11, 10,
8, 8, 8, 10, 10, 10, 10, 9, 9, 8, 10, 11, 11, 9, 8, 8,
8, 9, 9, 10, 11, 12, 12, 9, 9, 9, 8, 8, 7, 7, 7,
];
pub const VEC4_SYMS: [u16; 127] = [
0x0000, 0x1112, 0x0113, 0x2003, 0x1032, 0x3101, 0x2021, 0x0122,
0x1211, 0x0203, 0x0014, 0x2202, 0x2012, 0x1103, 0x1023, 0x4001,
0x1401, 0x2301, 0x0141, 0x0411, 0x0033, 0x1221, 0x0222, 0x1202,
0x0212, 0x0201, 0x2103, 0x1121, 0x1022, 0x0021, 0x2013, 0x0005,
0x0042, 0x1041, 0x0501, 0x0051, 0x0006, 0x0321, 0x2004, 0x2121,
0x2101, 0x1203, 0x0213, 0x0231, 0x0301, 0x3002, 0x2022, 0x0013,
0x1001, 0x0002, 0x1111, 0x0112, 0x1102, 0x1012, 0x3003, 0x4101,
0x3011, 0x0031, 0x3021, 0x0104, 0x1004, 0x0204, 0x0015, 0x3102,
0x2112, 0x1101, 0x1113, 0x1212, 0x0101, 0x0012, 0x0011, 0x0001,
0x1122, 0x2031, 0x0303, 0x1301, 0x2201, 0x2002, 0x1002, 0x3111,
0x0114, 0x0032, 0x0311, 0x1014, 0x2011, 0x1003, 0x0111, 0x1011,
0x0102, 0x0103, 0x1201, 0x0023, 0x0402, 0x0401, 0x4011, 0x0131,
0x0022, 0x0211, 0x0121, 0x0302, 0x3001, 0x0004, 0x1031, 0x1104,
0x3012, 0x2001, 0x3201, 0x0105, 0x4002, 0x1311, 0x0003, 0x0202,
0x1021, 0x0221, 0x0132, 0x0024, 0x1005, 0x0041, 0x5001, 0x0312,
0x1131, 0x1302, 0x2211, 0x0123, 0x2102, 0x2111, 0x1013,
];
/// 2-element vector coefficient Huffman table: `(symbol, bit_length)` (applied with offset `-1`).
pub const HUFF_VEC2_MAXBITS: usize = 12;
pub const VEC2_TABLE: [(u8, u8); 137] = [
( 19, 5), (165, 10), (211, 11), ( 46, 11), ( 75, 10), (177, 11),
( 12, 11), ( 86, 8), ( 83, 7), ( 38, 7), (133, 9), (178, 10),
( 28, 10), (104, 9), ( 73, 9), ( 35, 5), ( 52, 6), (113, 9),
( 8, 9), (101, 8), ( 69, 7), ( 0, 3), ( 71, 8), (119, 9),
( 91, 10), (179, 10), (114, 8), (166, 10), ( 10, 10), ( 44, 10),
(145, 10), ( 66, 6), ( 21, 6), ( 24, 8), (146, 9), ( 26, 9),
( 65, 7), ( 5, 7), (226, 11), (225, 12), ( 15, 12), (180, 10),
(147, 9), (115, 8), ( 40, 8), ( 89, 9), (134, 9), ( 84, 7),
( 54, 7), ( 42, 9), ( 60, 10), ( 31, 11), (193, 11), (181, 10),
( 76, 10), (148, 9), ( 37, 6), ( 67, 6), ( 33, 6), ( 3, 6),
( 17, 6), ( 2, 6), (102, 8), ( 87, 8), (116, 8), ( 56, 8),
( 50, 5), ( 20, 5), (120, 9), ( 58, 9), ( 29, 10), (194, 10),
(135, 9), ( 97, 8), ( 7, 8), (105, 9), ( 13, 11), (241, 12),
( 16, 12), ( 45, 10), (149, 9), ( 74, 9), ( 98, 7), ( 23, 7),
( 85, 7), ( 70, 7), (195, 10), (161, 10), (129, 9), ( 72, 8),
( 51, 5), ( 36, 5), (117, 8), ( 61, 10), ( 11, 10), (162, 9),
( 1, 7), ( 4, 6), ( 49, 6), ( 68, 6), ( 9, 9), ( 27, 9),
(130, 8), ( 39, 7), ( 53, 6), ( 99, 7), ( 25, 8), (150, 9),
( 90, 9), (103, 8), (163, 9), (196, 10), (210, 10), (136, 9),
(121, 9), ( 41, 8), (131, 8), ( 43, 9), (164, 9), (118, 8),
( 88, 8), ( 81, 7), ( 6, 7), ( 55, 7), ( 59, 9), ( 30, 10),
(209, 11), ( 14, 11), (151, 9), (106, 9), ( 82, 6), ( 22, 6),
(100, 7), (132, 8), ( 57, 8), ( 18, 4), ( 34, 4),
];
/// 1-element vector coefficient Huffman table: `(symbol, bit_length)`.
pub const HUFF_VEC1_SIZE: usize = 101;
pub const HUFF_VEC1_MAXBITS: usize = 11;
pub const VEC1_TABLE: [(u8, u8); 101] = [
( 7, 5), ( 32, 8), ( 59, 10), ( 60, 10), ( 83, 11), ( 82, 11),
( 62, 10), ( 33, 8), ( 45, 9), ( 61, 10), ( 84, 11), ( 85, 11),
( 1, 6), ( 13, 5), ( 19, 6), ( 25, 7), ( 34, 8), ( 46, 9),
( 47, 9), ( 14, 5), ( 6, 5), ( 64, 10), ( 87, 11), ( 86, 11),
( 63, 10), ( 88, 11), ( 90, 11), ( 35, 8), ( 26, 7), ( 0, 7),
( 48, 9), ( 65, 10), ( 66, 10), ( 36, 8), ( 15, 5), ( 20, 6),
( 91, 11), ( 89, 11), ( 67, 10), ( 49, 9), ( 50, 9), ( 69, 10),
( 92, 11), ( 93, 11), ( 27, 7), ( 5, 5), ( 37, 8), ( 68, 10),
( 71, 10), ( 51, 9), ( 52, 9), ( 70, 10), ( 94, 11), ( 96, 11),
( 38, 8), ( 21, 6), ( 16, 5), ( 4, 5), ( 28, 7), ( 53, 9),
( 95, 11), ( 97, 11), ( 73, 10), ( 39, 8), ( 29, 7), ( 72, 10),
( 98, 11), ( 99, 11), ( 54, 9), ( 40, 8), ( 22, 6), ( 30, 7),
( 55, 9), ( 74, 10), ( 76, 10), ( 56, 9), ( 75, 10), ( 77, 10),
( 17, 5), ( 3, 5), ( 23, 6), ( 41, 8), ( 57, 9), ( 78, 10),
( 79, 10), ( 31, 7), ( 10, 4), ( 9, 4), (100, 5), ( 2, 5),
( 11, 4), ( 8, 4), ( 18, 5), ( 42, 8), ( 58, 9), ( 80, 10),
( 81, 10), ( 43, 8), ( 44, 8), ( 24, 6), ( 12, 4),
];
/// Default multichannel decorrelation matrices (sizes 1..=6 packed sequentially).
pub const DEFAULT_DECORRELATION_MATRICES: [f32; 91] = [
1.000000, 0.707031, -0.707031, 0.707031, 0.707031, 0.578125, 0.707031, 0.410156,
0.578125, -0.707031, 0.410156, 0.578125, 0.000000, -0.816406, 0.500000, 0.652344,
0.500000, 0.269531, 0.500000, 0.269531, -0.500000, -0.652344, 0.500000, -0.269531,
-0.500000, 0.652344, 0.500000, -0.652344, 0.500000, -0.269531, 0.445312, 0.601562,
0.511719, 0.371094, 0.195312, 0.445312, 0.371094, -0.195312, -0.601562, -0.511719,
0.445312, 0.000000, -0.632812, 0.000000, 0.632812, 0.445312, -0.371094, -0.195312,
0.601562, -0.511719, 0.445312, -0.601562, 0.511719, -0.371094, 0.195312, 0.410156,
0.558594, 0.500000, 0.410156, 0.289062, 0.148438, 0.410156, 0.410156, 0.000000,
-0.410156, -0.578125, -0.410156, 0.410156, 0.148438, -0.500000, -0.410156, 0.289062,
0.558594, 0.410156, -0.148438, -0.500000, 0.410156, 0.289062, -0.558594, 0.410156,
-0.410156, 0.000000, 0.410156, -0.578125, 0.410156, 0.410156, -0.558594, 0.500000,
-0.410156, 0.289062, -0.148438,
];
/// Starting offset in `DEFAULT_DECORRELATION_MATRICES` for `num_channels` in `1..=6`.
pub const DEFAULT_DECORRELATION_OFFSETS: [usize; 7] = [0, 0, 1, 5, 14, 30, 55];
+5 -3
View File
@@ -153,19 +153,21 @@ fn test_title_runs_and_game_folder_is_untouched() {
assert!(d3["uma_gpu_va"].as_str().is_some_and(|v| v.starts_with("0x")), "d3d12: {d3}");
assert_eq!(dumps.iter().filter(|n| n.ends_with(".fssm0.p0.hlsl")).count(), 1, "{dumps:?}");
// (5b) XAudio2: genuine Windows engine; XMA2 voice replaced, console flags dropped
// (5b) XAudio2: genuine Windows engine; XMA2 and Opus voices decoded to PCM16, console flags dropped
let a = &report["audio"];
assert_eq!(a["engines"], 1, "audio: {a}");
assert_eq!(a["engine_errors"], 0, "audio: {a}");
assert!(a["dropped_flags"].as_array().unwrap().iter().any(|f| f == "XAudio2Create:0x41"), "audio: {a}");
if a["mastering_voices"] == 1 {
assert_eq!(a["xma_voices"], 1, "audio: {a}");
assert_eq!(a["xma_buffers_decoded"], 1, "audio: {a}");
assert_eq!(a["xma_decode_errors"], 0, "audio: {a}");
assert_eq!(a["opus_voices"], 1, "audio: {a}");
assert_eq!(a["opus_buffers_decoded"], 1, "audio: {a}");
assert_eq!(a["opus_decode_errors"], 0, "audio: {a}");
assert_eq!(a["silent_voices"], 1, "audio: {a}");
assert_eq!(a["silent_voices"], 0, "audio: {a}");
assert_eq!(a["source_voices"], 3, "audio: {a}");
assert_eq!(a["buffers_silenced"], 1, "audio: {a}");
assert_eq!(a["buffers_silenced"], 0, "audio: {a}");
assert_eq!(a["source_voice_errors"], 0, "audio: {a}");
} else {
eprintln!("e2e: no audio device – XAudio2 voices not checked ({a})");
+34 -7
View File
@@ -1,7 +1,7 @@
//! XAudio2 the way a console title uses it: engine with console-only flags, mastering
//! voice with a console device id, an XMA2 voice (no decoder on PC – XCL plays silence of
//! the same length) and a plain PCM voice. Buffer callbacks must come back with the
//! title's contexts and the stream must end on time.
//! voice with a console device id, an XMA2 voice (decoded to PCM16 by XCL), an Opus voice
//! (decoded via MSOpusDecoder) and a plain PCM voice. Buffer callbacks must come back with
//! the title's contexts and the streams must end on time.
use std::ffi::c_void;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
@@ -77,6 +77,22 @@ const XMA_CONTEXT: usize = 0x100;
const PCM_CONTEXT: usize = 0x2_0000;
const OPUS_CONTEXT: usize = 0x4000;
// Real 48 kHz mono XMA2 packet prefix (zero-padded to 2048 B; 12 frames = 6080 samples).
const XMA2_MONO_48K_PREFIX: &[u8] = &[
0x30, 0x00, 0x00, 0x00, 0x00, 0xe7, 0x00, 0x0c, 0x13, 0xe3, 0xff, 0xff, 0xfb, 0x39, 0x46, 0x44,
0xf6, 0x55, 0xa0, 0x1c, 0xe0, 0x01, 0x82, 0x7c, 0x7f, 0xff, 0xff, 0x67, 0x28, 0xc8, 0x9d, 0xca,
0xb4, 0x03, 0x9c, 0x00, 0x30, 0x4f, 0x8f, 0xff, 0xff, 0xec, 0xe5, 0x19, 0x13, 0xd9, 0x56, 0x80,
0x73, 0x80, 0x06, 0x09, 0xf1, 0xff, 0xff, 0xfd, 0x9c, 0xa3, 0x22, 0x77, 0x2a, 0xd0, 0x0e, 0x70,
0x00, 0xc1, 0x3e, 0x3f, 0xff, 0xff, 0xb3, 0x94, 0x64, 0x4f, 0x65, 0x5a, 0x01, 0xce, 0x00, 0x18,
0x27, 0xc7, 0xff, 0xff, 0xf6, 0x72, 0x8c, 0x89, 0xdc, 0xab, 0x40, 0x39, 0xc0, 0x03, 0x04, 0xf8,
0xff, 0xff, 0xfe, 0xce, 0x51, 0x91, 0x3d, 0x95, 0x68, 0x07, 0x38, 0x00, 0x60, 0x9f, 0x1f, 0xff,
0xff, 0xd9, 0xca, 0x32, 0x27, 0x72, 0xad, 0x00, 0xe7, 0x00, 0x0c, 0x13, 0xe3, 0xff, 0xff, 0xfb,
0x39, 0x46, 0x44, 0xf6, 0x55, 0xa0, 0x1c, 0xe0, 0x01, 0x82, 0x7c, 0x7f, 0xff, 0xff, 0x67, 0x28,
0xc8, 0x9d, 0xca, 0xb4, 0x03, 0x9c, 0x00, 0x30, 0x4f, 0x8f, 0xff, 0xff, 0xec, 0xe5, 0x19, 0x13,
0xd9, 0x56, 0x80, 0x73, 0x80, 0x06, 0x09, 0xf1, 0xff, 0xff, 0xfd, 0x9c, 0xa3, 0x22, 0x77, 0x2a,
0xc0,
];
// Five real 20 ms (960-sample) Hybrid FB mono Opus packets (100 ms total at 48 kHz).
const OPUS_PACKETS: &[&[u8]] = &[
&[0x78, 0x81, 0x7b, 0xc6, 0x11, 0x76, 0xf4, 0x75, 0x00, 0x00, 0x07, 0x8e, 0xca, 0x69, 0xdb, 0xbb, 0xcc, 0x48, 0xdc, 0x16, 0xbd, 0x9b, 0xd3, 0x58, 0x27, 0x78, 0x93, 0xbc, 0x37, 0x44, 0x5e, 0xcf, 0x2d, 0x97, 0x00, 0x69, 0x80, 0x11, 0x80, 0x2c, 0x03, 0x38, 0xab, 0x45, 0x61, 0xfd, 0x89, 0xb7, 0x7e, 0x0d, 0x17, 0xbf, 0x20, 0xa5, 0x2b, 0xb5, 0xea, 0x2b, 0x5e, 0xd3, 0xa7, 0x48, 0xd1, 0x18, 0xf2, 0x0b, 0x5c, 0x01, 0x4f, 0x5f, 0xa1, 0x96, 0x21, 0xae, 0xde, 0xe0, 0x53, 0x00, 0xd6, 0x65, 0x65, 0x3f, 0x1f, 0xa6, 0xcb, 0xb9, 0x77, 0xbf, 0x97, 0x62, 0xc1, 0x12, 0xcf, 0x8d, 0xc2, 0x7d, 0xfd, 0x00, 0x80, 0xff, 0xfa, 0xc6, 0x88, 0xb6, 0xde, 0x10, 0xb8, 0x4f, 0xc6, 0x23, 0x6b, 0xea, 0x87, 0xc9, 0x05],
@@ -180,12 +196,12 @@ pub fn xaudio2_console_voices() -> Result<(), String> {
return Ok(());
}
// XMA2 mono 48 kHz – Windows XAudio2 cannot play it
// XMA2 mono 48 kHz – decoded to PCM16 by XCL (Windows XAudio2 cannot play it natively)
let xma = Xma2Format {
wfx: WaveFormatEx { tag: 0x166, channels: 1, rate: 48_000, avg_bytes: 6000, block_align: 1, bits: 16, cb_size: 34 },
num_streams: 1,
channel_mask: 4,
samples_encoded: 9600,
samples_encoded: 6080,
bytes_per_block: 2048,
encoder_version: 4,
block_count: 1,
@@ -204,13 +220,15 @@ pub fn xaudio2_console_voices() -> Result<(), String> {
if (ch, rate) != (1, 48_000) {
return Err(format!("XMA2 voice: {ch} ch., {rate} Hz"));
}
let packet = vec![0x5au8; 2048];
let _ = method::<SetVolume>(xv, 12)(xv, 0.0, 0); // stay silent during automated tests
let mut packet = vec![0u8; 2048];
packet[..XMA2_MONO_48K_PREFIX.len()].copy_from_slice(XMA2_MONO_48K_PREFIX);
let xb = Buffer {
flags: END_OF_STREAM,
audio_bytes: 2048,
audio_data: packet.as_ptr(),
play_begin: 0,
play_length: 4800, // 0.1 s
play_length: 4800, // 0.1 s slice of the 6080 decoded samples
loop_begin: 0,
loop_length: 0,
loop_count: 0,
@@ -220,6 +238,15 @@ pub fn xaudio2_console_voices() -> Result<(), String> {
if hr != 0 {
return Err(format!("SubmitSourceBuffer(XMA2) → {:#x}", hr as u32));
}
let mut xst = VoiceState::default();
method::<GetState>(xv, 25)(xv, &mut xst, 0);
if { xst.current_buffer_context } as usize != XMA_CONTEXT || { xst.buffers_queued } != 1 {
return Err(format!(
"XMA2 GetState: ctx={:#x}, queued={}",
{ xst.current_buffer_context } as usize,
{ xst.buffers_queued }
));
}
// plain PCM16 voice must pass through untouched
let pcm = WaveFormatEx { tag: 1, channels: 1, rate: 48_000, avg_bytes: 96_000, block_align: 2, bits: 16, cb_size: 0 };
+1 -1
View File
@@ -505,7 +505,7 @@ fn main() {
check("DirectStorage (xcrdapi): a file read into memory, status + D3D12 fence", gpu::direct_storage);
check("D3D12.X: a texture in title memory (XG layout, #44) follows CPU writes", gpu::title_texture);
check("D3D12.X: a texture in a streaming pool gets its texels once pages are mapped in", gpu::streamed_texture);
check("XAudio2: console flags/device, XMA2 voice plays silence, PCM passes, callbacks", audio::xaudio2_console_voices);
check("XAudio2: console flags/device, XMA2 & Opus voices decoded, PCM passes, callbacks", audio::xaudio2_console_voices);
let failures = FAILURES.load(Ordering::SeqCst);
println!("xcl_testtitle: done, failures: {failures}");