From 6dc23e4ece9dec5ea755acc63981f57d8ffa6f17 Mon Sep 17 00:00:00 2001 From: PrivacyOnion Date: Wed, 25 Nov 2026 07:14:18 +0100 Subject: [PATCH 1/2] hle, docs: add XTP_APU_DUMP and Windows Opus MFT parity test --- README.md | 1 + crates/xtp-hle/src/modules/apu.rs | 141 ++++- crates/xtp-hle/tests/opus_reference.rs | 751 +++++++++++++++++++++++++ 3 files changed, 887 insertions(+), 6 deletions(-) create mode 100644 crates/xtp-hle/tests/opus_reference.rs diff --git a/README.md b/README.md index 593340af..58ef8905 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ cargo build --profile diag -p xtp --features diagnostics # target\diag\xtp.exe | `XTP_DXIL_CENSUS=` | (every build) appends one line per translated shader module to the file: its instructions by kind as translated and as written, and whether the clean-up passes were kept | | `XTP_AUDIO_CAPTURE=` | records what the title plays (our process only) | | `XTP_XMA_DUMP=` | saves the first 32 XMA voices' context and input | +| `XTP_APU_DUMP=` | saves the first 32 Opus streams' packets (up to 256 packets per stream) as `.opuscap` files outside the repository for reference comparison via `XTP_SAMPLE_OPUS` | | `XTP_DSP_DUMP=` | captures the first 32 DSP streams per process outside the repository. Each numbered stream directory holds `params.txt`, `ir.f32le` (omitted if unreadable or over 4 MiB), and the first four commands' buffers and gains as raw little-endian floats. On Windows, compare `.output-before.f32le` with `.output.f32le` to see how the command changed its destination. Disconnect waits for pending writes. | | `XTP_WATCHDOG_HOOK=1` | every build: keeps the title's own watchdog from ending it after about 30 s without progress (slow world frames), and logs a warning (at most every 30 s) when it would have; a real deadlock then hangs for ever, with no `crashes.log` entry; known builds only, otherwise a warning and nothing patched | | `XTP_TITLE_ASSERTS=continue` | every build: where the title's own assertion would end it because an entry of its archive store could not be made (`int3` at title+0x23d8f03; the entry's header was not read, docs/analysis/archive-null.md), it takes the title's own path for a null object instead, and logs how many times (at once, then at most every 10 s) and a census of that store; hides the failure, it does not fix it; known builds only, otherwise a warning and nothing patched | diff --git a/crates/xtp-hle/src/modules/apu.rs b/crates/xtp-hle/src/modules/apu.rs index 0668f0de..8d4741d3 100644 --- a/crates/xtp-hle/src/modules/apu.rs +++ b/crates/xtp-hle/src/modules/apu.rs @@ -130,8 +130,128 @@ pub struct Connection { state: Mutex, } +/// Maximum number of Opus streams captured under `XTP_APU_DUMP`. +#[cfg(feature = "diagnostics")] +const MAX_DUMP_STREAMS: u32 = 32; + +/// Maximum number of Opus packets appended per captured stream. +#[cfg(feature = "diagnostics")] +const MAX_DUMP_PACKETS: u32 = 256; + +/// Magic header for `.opuscap` packet capture files (`"XOPU"` + `u16` channels + `u16` 0). +pub const CAPTURE_MAGIC: [u8; 4] = *b"XOPU"; + +/// Appends one length-prefixed Opus packet to `path`, writing the 8-byte +/// `XOPU` header if the file is new. +pub fn append_capture_packet( + path: &std::path::Path, + channels: u16, + packet: &[u8], +) -> std::io::Result<()> { + use std::io::Write; + let is_new = !path.exists(); + let mut file = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(path)?; + if is_new { + file.write_all(&CAPTURE_MAGIC)?; + file.write_all(&channels.to_le_bytes())?; + file.write_all(&0u16.to_le_bytes())?; + } + let len = u32::try_from(packet.len()) + .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "packet too large"))?; + file.write_all(&len.to_le_bytes())?; + file.write_all(packet)?; + Ok(()) +} + +/// Parses a `.opuscap` file written by [`append_capture_packet`] into +/// `(channels, packets)`. +pub fn read_capture_file(path: &std::path::Path) -> std::io::Result<(u16, Vec>)> { + let data = std::fs::read(path)?; + if data.len() < 8 || data[..4] != CAPTURE_MAGIC { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "not an XOPU capture file", + )); + } + let channels = u16::from_le_bytes([data[4], data[5]]); + if !(1..=2).contains(&channels) { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!("unsupported channel count {channels}"), + )); + } + let mut packets = Vec::new(); + let mut pos = 8; + while pos + 4 <= data.len() { + let len = u32::from_le_bytes(data[pos..pos + 4].try_into().unwrap()) as usize; + pos += 4; + if pos + len > data.len() { + return Err(std::io::Error::new( + std::io::ErrorKind::UnexpectedEof, + "truncated Opus packet in capture file", + )); + } + packets.push(data[pos..pos + len].to_vec()); + pos += len; + } + Ok((channels, packets)) +} + +/// `XTP_APU_DUMP=`: saves the Opus packets queued on the first 32 +/// streams (up to 256 packets per stream) into `-s-ch.opuscap`, +/// for offline comparison against Windows' Opus MFT. +#[cfg(feature = "diagnostics")] +struct StreamDump { + path: std::path::PathBuf, + channels: u16, + written: u32, +} + +#[cfg(feature = "diagnostics")] +impl StreamDump { + fn open(stream: usize, channels: u16) -> Option { + static COUNT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + let dir = std::path::PathBuf::from(std::env::var_os("XTP_APU_DUMP")?); + if let Err(e) = xtp_core::ensure_outside_repo(&dir) { + tracing::warn!(%e, "XTP_APU_DUMP refuses to write inside the repository"); + return None; + } + let n = COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + if n >= MAX_DUMP_STREAMS { + return None; + } + if std::fs::create_dir_all(&dir).is_err() { + return None; + } + let path = dir.join(format!("{n:02}-s{stream}-{channels}ch.opuscap")); + Some(Self { + path, + channels, + written: 0, + }) + } + + fn record(&mut self, packet: &[u8]) { + if self.written >= MAX_DUMP_PACKETS { + return; + } + if append_capture_packet(&self.path, self.channels, packet).is_ok() { + self.written += 1; + } + } +} + +struct StreamSlot { + decoder: OpusDecoder, + #[cfg(feature = "diagnostics")] + dump: Option, +} + struct State { - streams: Vec>, + streams: Vec>, /// Results not yet dequeued, per stream. queued: Vec, results: VecDeque, @@ -274,7 +394,11 @@ pub extern "system" fn XApuEnqueueCommand( } else { match OpusDecoder::new(48_000, channels as usize) { Ok(d) if (1..=2).contains(&channels) => { - *decoder = Some(d); + *decoder = Some(StreamSlot { + decoder: d, + #[cfg(feature = "diagnostics")] + dump: StreamDump::open(stream, channels as u16), + }); S_OK } _ => E_INVALIDARG, @@ -283,8 +407,8 @@ pub extern "system" fn XApuEnqueueCommand( } DEACTIVATE => *decoder = None, RESET => { - if let Some(d) = decoder { - let _ = d.reset_state(); + if let Some(slot) = decoder { + let _ = slot.decoder.reset_state(); } } NOTIFY => {} @@ -292,7 +416,7 @@ pub extern "system" fn XApuEnqueueCommand( // SAFETY: a process command is this struct. let command = unsafe { command.cast::().read_unaligned() }; match decoder { - Some(d) => process(d, connection.options, &command, &mut result), + Some(slot) => process(slot, connection.options, &command, &mut result), None => result.error = INVALID_STREAM, } } @@ -314,7 +438,8 @@ pub extern "system" fn XApuEnqueueCommand( /// Decodes one packet into the command's output buffer, keeping the frames /// from `first_frame` on, at most `frame_count` of them. -fn process(decoder: &mut OpusDecoder, options: u32, command: &Process, result: &mut ApuResult) { +fn process(slot: &mut StreamSlot, options: u32, command: &Process, result: &mut ApuResult) { + let decoder = &mut slot.decoder; let channels = decoder.channels(); let (input, input_length) = (command.input, command.input_length); let (output, output_length) = (command.output, command.output_length); @@ -327,6 +452,10 @@ fn process(decoder: &mut OpusDecoder, options: u32, command: &Process, result: & } // SAFETY: the title's packet in its connection memory. let packet = unsafe { std::slice::from_raw_parts(input, input_length as usize) }; + #[cfg(feature = "diagnostics")] + if let Some(dump) = &mut slot.dump { + dump.record(packet); + } let mut pcm = vec![0f32; MAX_FRAMES * channels]; let decoded = match decoder.decode(packet, MAX_FRAMES, &mut pcm) { Ok(n) => n, diff --git a/crates/xtp-hle/tests/opus_reference.rs b/crates/xtp-hle/tests/opus_reference.rs new file mode 100644 index 00000000..d2163757 --- /dev/null +++ b/crates/xtp-hle/tests/opus_reference.rs @@ -0,0 +1,751 @@ +//! Reference comparison of `opus-pure` against Windows' built-in Opus MFT +//! (loaded dynamically from `mfplat.dll`, test-only). +//! +//! - `opus_pure_matches_windows_mft_on_synthetic_streams`: generates mono and +//! stereo 48 kHz Opus streams with `opus_pure::OpusEncoder`, decodes them +//! with both `opus_pure::OpusDecoder` and Windows' Opus MFT, and verifies +//! SNR >= 60 dB. +//! - `opus_pure_matches_windows_mft_on_sample_captures`: opt-in test reading +//! `.opuscap` captures produced by `XTP_APU_DUMP` from `XTP_SAMPLE_OPUS=`. +//! Skips cleanly via `xtp_testkit::skip` when unset. +#![cfg(windows)] + +use std::ffi::c_void; +use std::path::PathBuf; + +use opus_pure::{Application, OpusDecoder, OpusEncoder}; +use windows_sys::Win32::Foundation::{FreeLibrary, HMODULE}; +use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA}; +use xtp_hle::modules::apu; + +const MIN_SNR_DB: f64 = 60.0; +const SAMPLE_RATE: u32 = 48_000; +const MAX_FRAMES: usize = 5760; + +#[repr(C)] +#[derive(Clone, Copy)] +struct Guid { + d1: u32, + d2: u16, + d3: u16, + d4: [u8; 8], +} + +impl Guid { + const fn new(d1: u32, d2: u16, d3: u16, d4: [u8; 8]) -> Self { + Self { d1, d2, d3, d4 } + } +} + +#[repr(C)] +struct MftRegisterTypeInfo { + major_type: Guid, + subtype: Guid, +} + +#[repr(C)] +struct MftOutputDataBuffer { + stream_id: u32, + sample: *mut c_void, + status: u32, + events: *mut c_void, +} + +const MFT_CATEGORY_AUDIO_DECODER: Guid = Guid::new( + 0x9ea7_3fb4, + 0xef7a, + 0x4559, + [0x8d, 0x5d, 0x71, 0x9d, 0x8f, 0x04, 0x26, 0xc7], +); +const MF_MEDIA_TYPE_AUDIO: Guid = Guid::new( + 0x7364_7561, + 0x0000, + 0x0010, + [0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71], +); +const MF_AUDIO_FORMAT_OPUS: Guid = Guid::new( + 0x0000_704f, + 0x0000, + 0x0010, + [0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71], +); +const MF_AUDIO_FORMAT_FLOAT: Guid = Guid::new( + 0x0000_0003, + 0x0000, + 0x0010, + [0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71], +); +const MF_MT_MAJOR_TYPE: Guid = Guid::new( + 0x48eb_a18e, + 0xf8c9, + 0x4687, + [0xbf, 0x11, 0x0a, 0x74, 0xc9, 0xf9, 0x6a, 0x8f], +); +const MF_MT_SUBTYPE: Guid = Guid::new( + 0xf7e3_4c9a, + 0x42e8, + 0x4714, + [0xb7, 0x4b, 0xcb, 0x29, 0xd7, 0x2c, 0x35, 0xe5], +); +const MF_MT_AUDIO_NUM_CHANNELS: Guid = Guid::new( + 0x37e4_8bf5, + 0x645e, + 0x4c5b, + [0x89, 0xde, 0xad, 0xa9, 0xe2, 0x9b, 0x69, 0x6a], +); +const MF_MT_AUDIO_SAMPLES_PER_SECOND: Guid = Guid::new( + 0x5fae_eae7, + 0x0290, + 0x4c31, + [0x9e, 0x8a, 0xc5, 0x34, 0xf6, 0x8d, 0x9d, 0xba], +); +const MF_MT_AUDIO_BITS_PER_SAMPLE: Guid = Guid::new( + 0xf2de_b57f, + 0x40fa, + 0x4764, + [0xaa, 0x33, 0xed, 0x4f, 0x2d, 0x1f, 0xf6, 0x69], +); +const MF_MT_AUDIO_BLOCK_ALIGNMENT: Guid = Guid::new( + 0x322d_e230, + 0x9eeb, + 0x43bd, + [0xab, 0x7a, 0xff, 0x41, 0x22, 0x51, 0x54, 0x1d], +); +const MF_MT_AUDIO_AVG_BYTES_PER_SECOND: Guid = Guid::new( + 0x1aab_75c8, + 0xcfef, + 0x451c, + [0xab, 0x95, 0xac, 0x03, 0x4b, 0x8e, 0x17, 0x31], +); +const MF_MT_USER_DATA: Guid = Guid::new( + 0xb6bc_765f, + 0x4c3b, + 0x40a4, + [0xbd, 0x51, 0x25, 0x35, 0xb6, 0x6f, 0xe0, 0x9d], +); +const IID_IMF_TRANSFORM: Guid = Guid::new( + 0xbf94_c121, + 0x5b05, + 0x4e6f, + [0x80, 0x00, 0xba, 0x59, 0x89, 0x61, 0x41, 0x4d], +); + +const MF_VERSION: u32 = (0x0002 << 16) | 0x0070; +const MFT_ENUM_FLAG_ALL: u32 = 0x0000_003f; +const MFT_ENUM_FLAG_SORTANDFILTER: u32 = 0x0000_0040; +const MFT_MESSAGE_NOTIFY_BEGIN_STREAMING: u32 = 0x1000_0000; +const MFT_MESSAGE_NOTIFY_START_OF_STREAM: u32 = 0x1000_0003; +const MF_E_TRANSFORM_NEED_MORE_INPUT: i32 = 0xC00D_6D72_u32 as i32; + +type FnMfStartup = unsafe extern "system" fn(u32, u32) -> i32; +type FnMfShutdown = unsafe extern "system" fn() -> i32; +type FnMftEnumEx = unsafe extern "system" fn( + Guid, + u32, + *const MftRegisterTypeInfo, + *const MftRegisterTypeInfo, + *mut *mut *mut c_void, + *mut u32, +) -> i32; +type FnMfCreateMediaType = unsafe extern "system" fn(*mut *mut c_void) -> i32; +type FnMfCreateSample = unsafe extern "system" fn(*mut *mut c_void) -> i32; +type FnMfCreateMemoryBuffer = unsafe extern "system" fn(u32, *mut *mut c_void) -> i32; +type FnCoTaskMemFree = unsafe extern "system" fn(*mut c_void); +type FnCoInitializeEx = unsafe extern "system" fn(*mut c_void, u32) -> i32; + +struct MfApi { + mfplat: HMODULE, + ole32: HMODULE, + mf_shutdown: FnMfShutdown, + mft_enum_ex: FnMftEnumEx, + create_media_type: FnMfCreateMediaType, + create_sample: FnMfCreateSample, + create_memory_buffer: FnMfCreateMemoryBuffer, + co_task_mem_free: FnCoTaskMemFree, +} + +impl MfApi { + fn load() -> Option { + unsafe fn sym(module: HMODULE, name: &[u8]) -> Option { + // SAFETY: `module` is a valid loaded DLL and `name` is NUL-terminated. + let p = unsafe { GetProcAddress(module, name.as_ptr().cast::()) }?; + // SAFETY: caller supplies the exact Win32 function pointer type `T`. + Some(unsafe { std::mem::transmute_copy(&p) }) + } + + // SAFETY: NUL-terminated system DLL names. + let mfplat = unsafe { LoadLibraryA(c"mfplat.dll".as_ptr().cast()) }; + if mfplat.is_null() { + return None; + } + // SAFETY: NUL-terminated system DLL name. + let ole32 = unsafe { LoadLibraryA(c"ole32.dll".as_ptr().cast()) }; + if ole32.is_null() { + // SAFETY: `mfplat` was loaded above. + unsafe { FreeLibrary(mfplat) }; + return None; + } + // SAFETY: loading standard Media Foundation / COM entry points. + let loaded = unsafe { + let startup: FnMfStartup = sym(mfplat, b"MFStartup\0")?; + let mf_shutdown: FnMfShutdown = sym(mfplat, b"MFShutdown\0")?; + let mft_enum_ex: FnMftEnumEx = sym(mfplat, b"MFTEnumEx\0")?; + let create_media_type: FnMfCreateMediaType = sym(mfplat, b"MFCreateMediaType\0")?; + let create_sample: FnMfCreateSample = sym(mfplat, b"MFCreateSample\0")?; + let create_memory_buffer: FnMfCreateMemoryBuffer = + sym(mfplat, b"MFCreateMemoryBuffer\0")?; + let co_task_mem_free: FnCoTaskMemFree = sym(ole32, b"CoTaskMemFree\0")?; + let co_init: FnCoInitializeEx = sym(ole32, b"CoInitializeEx\0")?; + let _ = co_init(std::ptr::null_mut(), 0); // COINIT_MULTITHREADED + if startup(MF_VERSION, 0) < 0 { + FreeLibrary(ole32); + FreeLibrary(mfplat); + return None; + } + Self { + mfplat, + ole32, + mf_shutdown, + mft_enum_ex, + create_media_type, + create_sample, + create_memory_buffer, + co_task_mem_free, + } + }; + Some(loaded) + } +} + +impl Drop for MfApi { + fn drop(&mut self) { + // SAFETY: paired with `MFStartup` and `LoadLibraryA` in `load()`. + unsafe { + (self.mf_shutdown)(); + FreeLibrary(self.ole32); + FreeLibrary(self.mfplat); + } + } +} + +/// Call COM vtable slot `index` on `obj`. +unsafe fn vtable_slot(obj: *mut c_void, index: usize) -> T { + // SAFETY: `obj` is a valid COM interface pointer whose first word is its vtable. + unsafe { + let vt = *(obj as *const *const *const ()); + std::mem::transmute_copy(&*vt.add(index)) + } +} + +unsafe fn com_release(obj: *mut c_void) { + if !obj.is_null() { + // SAFETY: `IUnknown::Release` is always slot 2. + let release: unsafe extern "system" fn(*mut c_void) -> u32 = unsafe { vtable_slot(obj, 2) }; + // SAFETY: releasing a valid COM reference. + unsafe { release(obj) }; + } +} + +/// Wraps a Windows Media Foundation Opus decoder `IMFTransform`. +struct WindowsOpusMft { + mf: MfApi, + transform: *mut c_void, + channels: usize, +} + +impl WindowsOpusMft { + fn new(channels: usize) -> Option { + let mf = MfApi::load()?; + let input_filter = MftRegisterTypeInfo { + major_type: MF_MEDIA_TYPE_AUDIO, + subtype: MF_AUDIO_FORMAT_OPUS, + }; + let mut activates: *mut *mut c_void = std::ptr::null_mut(); + let mut count = 0u32; + // SAFETY: `MFTEnumEx` allocates `activates` of length `count`. + let hr = unsafe { + (mf.mft_enum_ex)( + MFT_CATEGORY_AUDIO_DECODER, + MFT_ENUM_FLAG_ALL | MFT_ENUM_FLAG_SORTANDFILTER, + &input_filter, + std::ptr::null(), + &mut activates, + &mut count, + ) + }; + if hr < 0 || count == 0 || activates.is_null() { + eprintln!("opus MFT enum: hr={hr:#x} count={count}"); + return None; + } + + let mut transform: *mut c_void = std::ptr::null_mut(); + // SAFETY: `activates[0]` is an `IMFActivate`; slot 33 is `ActivateObject`. + unsafe { + let first = *activates; + type ActivateObject = + unsafe extern "system" fn(*mut c_void, *const Guid, *mut *mut c_void) -> i32; + let activate: ActivateObject = vtable_slot(first, 33); + let _ = activate(first, &IID_IMF_TRANSFORM, &mut transform); + for i in 0..count as usize { + com_release(*activates.add(i)); + } + (mf.co_task_mem_free)(activates.cast()); + } + if transform.is_null() { + return None; + } + + let decoder = Self { + mf, + transform, + channels, + }; + if !decoder.configure() { + return None; + } + Some(decoder) + } + + fn set_attr_guid(&self, mt: *mut c_void, key: &Guid, val: &Guid) -> bool { + // SAFETY: `IMFAttributes::SetGUID` is slot 24. + unsafe { + let f: unsafe extern "system" fn(*mut c_void, *const Guid, *const Guid) -> i32 = + vtable_slot(mt, 24); + f(mt, key, val) >= 0 + } + } + + fn set_attr_u32(&self, mt: *mut c_void, key: &Guid, val: u32) -> bool { + // SAFETY: `IMFAttributes::SetUINT32` is slot 21. + unsafe { + let f: unsafe extern "system" fn(*mut c_void, *const Guid, u32) -> i32 = + vtable_slot(mt, 21); + f(mt, key, val) >= 0 + } + } + + fn set_attr_blob(&self, mt: *mut c_void, key: &Guid, data: &[u8]) -> bool { + // SAFETY: `IMFAttributes::SetBlob` is slot 26. + unsafe { + let f: unsafe extern "system" fn(*mut c_void, *const Guid, *const u8, u32) -> i32 = + vtable_slot(mt, 26); + f(mt, key, data.as_ptr(), data.len() as u32) >= 0 + } + } + + fn configure(&self) -> bool { + // Input type: 48 kHz Opus with `self.channels` and an RFC 7845 OpusHead. + let mut in_mt = std::ptr::null_mut(); + // SAFETY: Media Foundation factory call. + if unsafe { (self.mf.create_media_type)(&mut in_mt) } < 0 { + return false; + } + let mut opus_head = [0u8; 19]; + opus_head[..8].copy_from_slice(b"OpusHead"); + opus_head[8] = 1; // version + opus_head[9] = self.channels as u8; + opus_head[12..16].copy_from_slice(&SAMPLE_RATE.to_le_bytes()); + let ok_in = self.set_attr_guid(in_mt, &MF_MT_MAJOR_TYPE, &MF_MEDIA_TYPE_AUDIO) + && self.set_attr_guid(in_mt, &MF_MT_SUBTYPE, &MF_AUDIO_FORMAT_OPUS) + && self.set_attr_u32(in_mt, &MF_MT_AUDIO_NUM_CHANNELS, self.channels as u32) + && self.set_attr_u32(in_mt, &MF_MT_AUDIO_SAMPLES_PER_SECOND, SAMPLE_RATE) + && self.set_attr_blob(in_mt, &MF_MT_USER_DATA, &opus_head); + let hr_in = if ok_in { + // SAFETY: `IMFTransform::SetInputType` is slot 15. + unsafe { + let set_in: unsafe extern "system" fn(*mut c_void, u32, *mut c_void, u32) -> i32 = + vtable_slot(self.transform, 15); + set_in(self.transform, 0, in_mt, 0) + } + } else { + -1 + }; + // SAFETY: releasing temporary media type. + unsafe { com_release(in_mt) }; + if hr_in < 0 { + eprintln!("opus MFT SetInputType failed: hr={hr_in:#x}"); + return false; + } + + // Output type: query available types for IEEE float PCM, else construct one. + let mut selected_out = std::ptr::null_mut(); + for idx in 0..16u32 { + let mut avail = std::ptr::null_mut(); + // SAFETY: `IMFTransform::GetOutputAvailableType` is slot 14. + let hr = unsafe { + let get_out: unsafe extern "system" fn( + *mut c_void, + u32, + u32, + *mut *mut c_void, + ) -> i32 = vtable_slot(self.transform, 14); + get_out(self.transform, 0, idx, &mut avail) + }; + if hr < 0 || avail.is_null() { + break; + } + let mut subtype = Guid::new(0, 0, 0, [0; 8]); + // SAFETY: `IMFAttributes::GetGUID` is slot 10. + let got = unsafe { + let get_guid: unsafe extern "system" fn( + *mut c_void, + *const Guid, + *mut Guid, + ) -> i32 = vtable_slot(avail, 10); + get_guid(avail, &MF_MT_SUBTYPE, &mut subtype) >= 0 + }; + if got + && subtype.d1 == MF_AUDIO_FORMAT_FLOAT.d1 + && subtype.d2 == MF_AUDIO_FORMAT_FLOAT.d2 + && subtype.d3 == MF_AUDIO_FORMAT_FLOAT.d3 + && subtype.d4 == MF_AUDIO_FORMAT_FLOAT.d4 + { + selected_out = avail; + break; + } + // SAFETY: releasing non-matching available output type. + unsafe { com_release(avail) }; + } + if selected_out.is_null() { + // SAFETY: Media Foundation factory call. + if unsafe { (self.mf.create_media_type)(&mut selected_out) } < 0 { + return false; + } + let block_align = (self.channels * 4) as u32; + let _ = self.set_attr_guid(selected_out, &MF_MT_MAJOR_TYPE, &MF_MEDIA_TYPE_AUDIO) + && self.set_attr_guid(selected_out, &MF_MT_SUBTYPE, &MF_AUDIO_FORMAT_FLOAT) + && self.set_attr_u32( + selected_out, + &MF_MT_AUDIO_NUM_CHANNELS, + self.channels as u32, + ) + && self.set_attr_u32(selected_out, &MF_MT_AUDIO_SAMPLES_PER_SECOND, SAMPLE_RATE) + && self.set_attr_u32(selected_out, &MF_MT_AUDIO_BITS_PER_SAMPLE, 32) + && self.set_attr_u32(selected_out, &MF_MT_AUDIO_BLOCK_ALIGNMENT, block_align) + && self.set_attr_u32( + selected_out, + &MF_MT_AUDIO_AVG_BYTES_PER_SECOND, + SAMPLE_RATE * block_align, + ); + } + // SAFETY: `IMFTransform::SetOutputType` is slot 16. + let hr_out = unsafe { + let set_out: unsafe extern "system" fn(*mut c_void, u32, *mut c_void, u32) -> i32 = + vtable_slot(self.transform, 16); + set_out(self.transform, 0, selected_out, 0) + }; + // SAFETY: releasing temporary media type. + unsafe { com_release(selected_out) }; + if hr_out < 0 { + eprintln!("opus MFT SetOutputType failed: hr={hr_out:#x}"); + return false; + } + + // SAFETY: `IMFTransform::ProcessMessage` is slot 23. + unsafe { + let msg: unsafe extern "system" fn(*mut c_void, u32, usize) -> i32 = + vtable_slot(self.transform, 23); + let _ = msg(self.transform, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0); + let _ = msg(self.transform, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0); + } + true + } + + fn decode_packet(&self, packet: &[u8]) -> Result, i32> { + let mut in_buf = std::ptr::null_mut(); + let mut in_sample = std::ptr::null_mut(); + // SAFETY: creating MF memory buffer & sample for the input packet. + unsafe { + let hr = (self.mf.create_memory_buffer)(packet.len() as u32, &mut in_buf); + if hr < 0 { + return Err(hr); + } + let lock: unsafe extern "system" fn( + *mut c_void, + *mut *mut u8, + *mut u32, + *mut u32, + ) -> i32 = vtable_slot(in_buf, 3); + let unlock: unsafe extern "system" fn(*mut c_void) -> i32 = vtable_slot(in_buf, 4); + let set_len: unsafe extern "system" fn(*mut c_void, u32) -> i32 = + vtable_slot(in_buf, 6); + let mut ptr = std::ptr::null_mut(); + let hr = lock(in_buf, &mut ptr, std::ptr::null_mut(), std::ptr::null_mut()); + if hr < 0 { + com_release(in_buf); + return Err(hr); + } + std::ptr::copy_nonoverlapping(packet.as_ptr(), ptr, packet.len()); + let _ = unlock(in_buf); + let _ = set_len(in_buf, packet.len() as u32); + + let hr = (self.mf.create_sample)(&mut in_sample); + if hr < 0 { + com_release(in_buf); + return Err(hr); + } + // `IMFSample::AddBuffer` is slot 42. + let add_buf: unsafe extern "system" fn(*mut c_void, *mut c_void) -> i32 = + vtable_slot(in_sample, 42); + let _ = add_buf(in_sample, in_buf); + com_release(in_buf); + + // `IMFTransform::ProcessInput` is slot 24. + let process_in: unsafe extern "system" fn(*mut c_void, u32, *mut c_void, u32) -> i32 = + vtable_slot(self.transform, 24); + let hr = process_in(self.transform, 0, in_sample, 0); + com_release(in_sample); + if hr < 0 { + return Err(hr); + } + } + + // Query whether the MFT allocates its own output samples. + #[repr(C)] + struct MftOutputStreamInfo { + flags: u32, + cb_size: u32, + cb_alignment: u32, + } + let mut osi = MftOutputStreamInfo { + flags: 0, + cb_size: 0, + cb_alignment: 0, + }; + // SAFETY: `IMFTransform::GetOutputStreamInfo` is slot 7. + let mft_provides_samples = unsafe { + let get_osi: unsafe extern "system" fn( + *mut c_void, + u32, + *mut MftOutputStreamInfo, + ) -> i32 = vtable_slot(self.transform, 7); + get_osi(self.transform, 0, &mut osi) >= 0 && (osi.flags & 0x300) != 0 + }; + + let mut samples = Vec::new(); + loop { + let mut caller_buf = std::ptr::null_mut(); + let mut caller_sample = std::ptr::null_mut(); + if !mft_provides_samples { + let out_bytes = osi.cb_size.max((MAX_FRAMES * self.channels * 4) as u32); + // SAFETY: allocating output buffer/sample when the MFT expects caller-owned storage. + unsafe { + let hr = (self.mf.create_memory_buffer)(out_bytes, &mut caller_buf); + if hr < 0 { + return Err(hr); + } + let hr = (self.mf.create_sample)(&mut caller_sample); + if hr < 0 { + com_release(caller_buf); + return Err(hr); + } + let add_buf: unsafe extern "system" fn(*mut c_void, *mut c_void) -> i32 = + vtable_slot(caller_sample, 42); + let _ = add_buf(caller_sample, caller_buf); + } + } + + let mut odb = MftOutputDataBuffer { + stream_id: 0, + sample: caller_sample, + status: 0, + events: std::ptr::null_mut(), + }; + let mut status = 0u32; + // SAFETY: `IMFTransform::ProcessOutput` is slot 25. + let hr = unsafe { + let process_out: unsafe extern "system" fn( + *mut c_void, + u32, + u32, + *mut MftOutputDataBuffer, + *mut u32, + ) -> i32 = vtable_slot(self.transform, 25); + process_out(self.transform, 0, 1, &mut odb, &mut status) + }; + // SAFETY: releasing any events / samples returned or allocated above. + unsafe { + if !odb.events.is_null() { + com_release(odb.events); + } + if hr == MF_E_TRANSFORM_NEED_MORE_INPUT { + com_release(caller_sample); + com_release(caller_buf); + break; + } + if hr < 0 { + com_release(caller_sample); + com_release(caller_buf); + return Err(hr); + } + let out_sample = odb.sample; + if !out_sample.is_null() { + let mut cont_buf = std::ptr::null_mut(); + // `IMFSample::ConvertToContiguousBuffer` is slot 41. + let to_buf: unsafe extern "system" fn(*mut c_void, *mut *mut c_void) -> i32 = + vtable_slot(out_sample, 41); + if to_buf(out_sample, &mut cont_buf) >= 0 && !cont_buf.is_null() { + let lock: unsafe extern "system" fn( + *mut c_void, + *mut *mut u8, + *mut u32, + *mut u32, + ) -> i32 = vtable_slot(cont_buf, 3); + let unlock: unsafe extern "system" fn(*mut c_void) -> i32 = + vtable_slot(cont_buf, 4); + let mut ptr = std::ptr::null_mut(); + let mut cur_len = 0u32; + if lock(cont_buf, &mut ptr, std::ptr::null_mut(), &mut cur_len) >= 0 + && cur_len >= 4 + { + let bytes = std::slice::from_raw_parts(ptr, cur_len as usize); + for &chunk in bytes.as_chunks::<4>().0 { + samples.push(f32::from_le_bytes(chunk)); + } + let _ = unlock(cont_buf); + } + com_release(cont_buf); + } + com_release(out_sample); + } + com_release(caller_buf); + } + } + Ok(samples) + } +} + +impl Drop for WindowsOpusMft { + fn drop(&mut self) { + // SAFETY: releasing the activated `IMFTransform`. + unsafe { com_release(self.transform) }; + } +} + +/// Signal-to-noise ratio in dB between `reference` and `candidate`. +fn snr_db(reference: &[f32], candidate: &[f32]) -> f64 { + let n = reference.len().min(candidate.len()); + assert!(n > 0, "empty PCM comparison"); + let mut sig = 0.0f64; + let mut err = 0.0f64; + for (&r, &c) in reference[..n].iter().zip(&candidate[..n]) { + let rf = f64::from(r); + let df = rf - f64::from(c); + sig += rf * rf; + err += df * df; + } + if err == 0.0 { + return f64::INFINITY; + } + 10.0 * (sig / err).log10() +} + +fn compare_stream(channels: usize, packets: &[Vec], label: &str) { + let Some(mft) = WindowsOpusMft::new(channels) else { + xtp_testkit::skip("no-opus-mft"); + return; + }; + let mut pure = OpusDecoder::new(SAMPLE_RATE as i32, channels).expect("opus-pure decoder"); + let mut pure_pcm = Vec::new(); + let mut mft_pcm = Vec::new(); + let mut scratch = vec![0f32; MAX_FRAMES * channels]; + + for pkt in packets { + let frames = pure + .decode(pkt, MAX_FRAMES, &mut scratch) + .expect("opus-pure decode"); + pure_pcm.extend_from_slice(&scratch[..frames * channels]); + let decoded_mft = mft.decode_packet(pkt).expect("Windows Opus MFT decode"); + mft_pcm.extend_from_slice(&decoded_mft); + } + + assert_eq!( + pure_pcm.len(), + mft_pcm.len(), + "{label}: sample count mismatch (opus-pure={} vs MFT={})", + pure_pcm.len(), + mft_pcm.len() + ); + let snr = snr_db(&mft_pcm, &pure_pcm); + assert!( + snr >= MIN_SNR_DB, + "{label}: SNR {snr:.2} dB < {MIN_SNR_DB} dB" + ); +} + +fn synth_packets(channels: usize, count: usize) -> Vec> { + let mut enc = OpusEncoder::new(SAMPLE_RATE as i32, channels, Application::Audio).unwrap(); + let frame_samples = 960; // 20 ms at 48 kHz + let mut packets = Vec::with_capacity(count); + let mut buf = [0u8; 1500]; + for p in 0..count { + let mut pcm = vec![0f32; frame_samples * channels]; + for i in 0..frame_samples { + let t = (p * frame_samples + i) as f32 / SAMPLE_RATE as f32; + let left = (t * 440.0 * std::f32::consts::TAU).sin() * 0.4; + if channels == 1 { + pcm[i] = left; + } else { + let right = (t * 660.0 * std::f32::consts::TAU).sin() * 0.4; + pcm[i * 2] = left; + pcm[i * 2 + 1] = right; + } + } + let n = enc.encode(&pcm, frame_samples, &mut buf).unwrap(); + packets.push(buf[..n].to_vec()); + } + packets +} + +#[test] +fn opus_pure_matches_windows_mft_on_synthetic_streams() { + // Also exercise the `.opuscap` capture round-trip on synthetic packets. + let dir = tempfile::tempdir().unwrap(); + for channels in [1usize, 2] { + let packets = synth_packets(channels, 10); + let path = dir.path().join(format!("synth-{channels}ch.opuscap")); + for pkt in &packets { + apu::append_capture_packet(&path, channels as u16, pkt).unwrap(); + } + let (read_ch, read_pkts) = apu::read_capture_file(&path).unwrap(); + assert_eq!(read_ch as usize, channels); + assert_eq!(read_pkts, packets); + compare_stream(channels, &read_pkts, &format!("synthetic {channels}ch")); + } +} + +/// Opt-in parity test on real captures produced by `XTP_APU_DUMP=`. +/// Set `XTP_SAMPLE_OPUS=` to a folder of `.opuscap` files outside the +/// repository. +#[test] +fn opus_pure_matches_windows_mft_on_sample_captures() { + let Some(dir) = std::env::var_os("XTP_SAMPLE_OPUS") else { + xtp_testkit::skip("no-sample XTP_SAMPLE_OPUS"); + return; + }; + let dir = PathBuf::from(dir); + let mut files: Vec = std::fs::read_dir(&dir) + .unwrap_or_else(|e| panic!("XTP_SAMPLE_OPUS={} unreadable: {e}", dir.display())) + .filter_map(Result::ok) + .map(|e| e.path()) + .filter(|p| p.extension().is_some_and(|e| e == "opuscap")) + .collect(); + files.sort(); + assert!( + !files.is_empty(), + "XTP_SAMPLE_OPUS={} contains no .opuscap files", + dir.display() + ); + for file in files { + let (channels, packets) = apu::read_capture_file(&file) + .unwrap_or_else(|e| panic!("reading {}: {e}", file.display())); + if packets.is_empty() { + continue; + } + let name = file + .file_name() + .and_then(|n| n.to_str()) + .unwrap_or("capture"); + compare_stream(channels as usize, &packets, name); + } +} From d13672982a981cfa8ccff8caf127f65f58a2c4c8 Mon Sep 17 00:00:00 2001 From: PrivacyOnion Date: Wed, 25 Nov 2026 07:26:15 +0100 Subject: [PATCH 2/2] hle: truncate capture on open, fix MFT unlock and hardening --- crates/xtp-hle/src/modules/apu.rs | 36 +++--- crates/xtp-hle/tests/opus_reference.rs | 166 ++++++++++++++++--------- 2 files changed, 131 insertions(+), 71 deletions(-) diff --git a/crates/xtp-hle/src/modules/apu.rs b/crates/xtp-hle/src/modules/apu.rs index 8d4741d3..1734f6f0 100644 --- a/crates/xtp-hle/src/modules/apu.rs +++ b/crates/xtp-hle/src/modules/apu.rs @@ -141,24 +141,29 @@ const MAX_DUMP_PACKETS: u32 = 256; /// Magic header for `.opuscap` packet capture files (`"XOPU"` + `u16` channels + `u16` 0). pub const CAPTURE_MAGIC: [u8; 4] = *b"XOPU"; -/// Appends one length-prefixed Opus packet to `path`, writing the 8-byte -/// `XOPU` header if the file is new. +/// Creates (or truncates) `path` and writes the 8-byte `"XOPU"` header for +/// `channels`. +pub fn create_capture_file(path: &std::path::Path, channels: u16) -> std::io::Result<()> { + use std::io::Write; + let mut file = std::fs::File::create(path)?; + file.write_all(&CAPTURE_MAGIC)?; + file.write_all(&channels.to_le_bytes())?; + file.write_all(&0u16.to_le_bytes())?; + Ok(()) +} + +/// Appends one length-prefixed Opus packet to `path`, creating the 8-byte +/// `XOPU` header first if the file does not exist yet. pub fn append_capture_packet( path: &std::path::Path, channels: u16, packet: &[u8], ) -> std::io::Result<()> { use std::io::Write; - let is_new = !path.exists(); - let mut file = std::fs::OpenOptions::new() - .create(true) - .append(true) - .open(path)?; - if is_new { - file.write_all(&CAPTURE_MAGIC)?; - file.write_all(&channels.to_le_bytes())?; - file.write_all(&0u16.to_le_bytes())?; + if !path.exists() { + create_capture_file(path, channels)?; } + let mut file = std::fs::OpenOptions::new().append(true).open(path)?; let len = u32::try_from(packet.len()) .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidInput, "packet too large"))?; file.write_all(&len.to_le_bytes())?; @@ -215,18 +220,19 @@ impl StreamDump { fn open(stream: usize, channels: u16) -> Option { static COUNT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); let dir = std::path::PathBuf::from(std::env::var_os("XTP_APU_DUMP")?); - if let Err(e) = xtp_core::ensure_outside_repo(&dir) { - tracing::warn!(%e, "XTP_APU_DUMP refuses to write inside the repository"); - return None; - } let n = COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); if n >= MAX_DUMP_STREAMS { return None; } + if let Err(e) = xtp_core::ensure_outside_repo(&dir) { + tracing::warn!(%e, "XTP_APU_DUMP refuses to write inside the repository"); + return None; + } if std::fs::create_dir_all(&dir).is_err() { return None; } let path = dir.join(format!("{n:02}-s{stream}-{channels}ch.opuscap")); + create_capture_file(&path, channels).ok()?; Some(Self { path, channels, diff --git a/crates/xtp-hle/tests/opus_reference.rs b/crates/xtp-hle/tests/opus_reference.rs index d2163757..ebf41b39 100644 --- a/crates/xtp-hle/tests/opus_reference.rs +++ b/crates/xtp-hle/tests/opus_reference.rs @@ -15,7 +15,9 @@ use std::path::PathBuf; use opus_pure::{Application, OpusDecoder, OpusEncoder}; use windows_sys::Win32::Foundation::{FreeLibrary, HMODULE}; -use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA}; +use windows_sys::Win32::System::LibraryLoader::{ + GetProcAddress, LOAD_LIBRARY_SEARCH_SYSTEM32, LoadLibraryExA, +}; use xtp_hle::modules::apu; const MIN_SNR_DB: f64 = 60.0; @@ -131,10 +133,13 @@ const IID_IMF_TRANSFORM: Guid = Guid::new( ); const MF_VERSION: u32 = (0x0002 << 16) | 0x0070; -const MFT_ENUM_FLAG_ALL: u32 = 0x0000_003f; +const MFT_ENUM_FLAG_SYNCMFT: u32 = 0x0000_0001; +const MFT_ENUM_FLAG_LOCALMFT: u32 = 0x0000_0010; const MFT_ENUM_FLAG_SORTANDFILTER: u32 = 0x0000_0040; +const MFT_MESSAGE_COMMAND_DRAIN: u32 = 0x0000_0001; const MFT_MESSAGE_NOTIFY_BEGIN_STREAMING: u32 = 0x1000_0000; const MFT_MESSAGE_NOTIFY_START_OF_STREAM: u32 = 0x1000_0003; +const MF_E_TRANSFORM_STREAM_CHANGE: i32 = 0xC00D_6D61_u32 as i32; const MF_E_TRANSFORM_NEED_MORE_INPUT: i32 = 0xC00D_6D72_u32 as i32; type FnMfStartup = unsafe extern "system" fn(u32, u32) -> i32; @@ -153,9 +158,37 @@ type FnMfCreateMemoryBuffer = unsafe extern "system" fn(u32, *mut *mut c_void) - type FnCoTaskMemFree = unsafe extern "system" fn(*mut c_void); type FnCoInitializeEx = unsafe extern "system" fn(*mut c_void, u32) -> i32; +struct DllGuard(HMODULE); + +impl DllGuard { + fn load_system(name: &std::ffi::CStr) -> Option { + // SAFETY: `name` is a NUL-terminated system DLL filename; restricting + // search to System32 avoids loading untrusted DLLs from CWD. + let handle = unsafe { + LoadLibraryExA( + name.as_ptr().cast(), + std::ptr::null_mut(), + LOAD_LIBRARY_SEARCH_SYSTEM32, + ) + }; + if handle.is_null() { + None + } else { + Some(Self(handle)) + } + } +} + +impl Drop for DllGuard { + fn drop(&mut self) { + // SAFETY: `self.0` is a valid non-null module handle from `LoadLibraryExA`. + unsafe { FreeLibrary(self.0) }; + } +} + struct MfApi { - mfplat: HMODULE, - ole32: HMODULE, + _ole32: DllGuard, + _mfplat: DllGuard, mf_shutdown: FnMfShutdown, mft_enum_ex: FnMftEnumEx, create_media_type: FnMfCreateMediaType, @@ -173,38 +206,27 @@ impl MfApi { Some(unsafe { std::mem::transmute_copy(&p) }) } - // SAFETY: NUL-terminated system DLL names. - let mfplat = unsafe { LoadLibraryA(c"mfplat.dll".as_ptr().cast()) }; - if mfplat.is_null() { - return None; - } - // SAFETY: NUL-terminated system DLL name. - let ole32 = unsafe { LoadLibraryA(c"ole32.dll".as_ptr().cast()) }; - if ole32.is_null() { - // SAFETY: `mfplat` was loaded above. - unsafe { FreeLibrary(mfplat) }; - return None; - } - // SAFETY: loading standard Media Foundation / COM entry points. + let mfplat = DllGuard::load_system(c"mfplat.dll")?; + let ole32 = DllGuard::load_system(c"ole32.dll")?; + // SAFETY: loading standard Media Foundation / COM entry points; `DllGuard` + // frees both module handles on any early return. let loaded = unsafe { - let startup: FnMfStartup = sym(mfplat, b"MFStartup\0")?; - let mf_shutdown: FnMfShutdown = sym(mfplat, b"MFShutdown\0")?; - let mft_enum_ex: FnMftEnumEx = sym(mfplat, b"MFTEnumEx\0")?; - let create_media_type: FnMfCreateMediaType = sym(mfplat, b"MFCreateMediaType\0")?; - let create_sample: FnMfCreateSample = sym(mfplat, b"MFCreateSample\0")?; + let startup: FnMfStartup = sym(mfplat.0, b"MFStartup\0")?; + let mf_shutdown: FnMfShutdown = sym(mfplat.0, b"MFShutdown\0")?; + let mft_enum_ex: FnMftEnumEx = sym(mfplat.0, b"MFTEnumEx\0")?; + let create_media_type: FnMfCreateMediaType = sym(mfplat.0, b"MFCreateMediaType\0")?; + let create_sample: FnMfCreateSample = sym(mfplat.0, b"MFCreateSample\0")?; let create_memory_buffer: FnMfCreateMemoryBuffer = - sym(mfplat, b"MFCreateMemoryBuffer\0")?; - let co_task_mem_free: FnCoTaskMemFree = sym(ole32, b"CoTaskMemFree\0")?; - let co_init: FnCoInitializeEx = sym(ole32, b"CoInitializeEx\0")?; + sym(mfplat.0, b"MFCreateMemoryBuffer\0")?; + let co_task_mem_free: FnCoTaskMemFree = sym(ole32.0, b"CoTaskMemFree\0")?; + let co_init: FnCoInitializeEx = sym(ole32.0, b"CoInitializeEx\0")?; let _ = co_init(std::ptr::null_mut(), 0); // COINIT_MULTITHREADED if startup(MF_VERSION, 0) < 0 { - FreeLibrary(ole32); - FreeLibrary(mfplat); return None; } Self { - mfplat, - ole32, + _ole32: ole32, + _mfplat: mfplat, mf_shutdown, mft_enum_ex, create_media_type, @@ -219,11 +241,10 @@ impl MfApi { impl Drop for MfApi { fn drop(&mut self) { - // SAFETY: paired with `MFStartup` and `LoadLibraryA` in `load()`. + // SAFETY: paired with `MFStartup` in `load()`; runs before `_ole32` and + // `_mfplat` fields are dropped. unsafe { (self.mf_shutdown)(); - FreeLibrary(self.ole32); - FreeLibrary(self.mfplat); } } } @@ -266,7 +287,7 @@ impl WindowsOpusMft { let hr = unsafe { (mf.mft_enum_ex)( MFT_CATEGORY_AUDIO_DECODER, - MFT_ENUM_FLAG_ALL | MFT_ENUM_FLAG_SORTANDFILTER, + MFT_ENUM_FLAG_SYNCMFT | MFT_ENUM_FLAG_LOCALMFT | MFT_ENUM_FLAG_SORTANDFILTER, &input_filter, std::ptr::null(), &mut activates, @@ -274,7 +295,6 @@ impl WindowsOpusMft { ) }; if hr < 0 || count == 0 || activates.is_null() { - eprintln!("opus MFT enum: hr={hr:#x} count={count}"); return None; } @@ -363,10 +383,23 @@ impl WindowsOpusMft { // SAFETY: releasing temporary media type. unsafe { com_release(in_mt) }; if hr_in < 0 { - eprintln!("opus MFT SetInputType failed: hr={hr_in:#x}"); return false; } + if !self.select_output_type() { + return false; + } + + // SAFETY: `IMFTransform::ProcessMessage` is slot 23. + unsafe { + let msg: unsafe extern "system" fn(*mut c_void, u32, usize) -> i32 = + vtable_slot(self.transform, 23); + let _ = msg(self.transform, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0); + let _ = msg(self.transform, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0); + } + true + } + fn select_output_type(&self) -> bool { // Output type: query available types for IEEE float PCM, else construct one. let mut selected_out = std::ptr::null_mut(); for idx in 0..16u32 { @@ -436,19 +469,7 @@ impl WindowsOpusMft { }; // SAFETY: releasing temporary media type. unsafe { com_release(selected_out) }; - if hr_out < 0 { - eprintln!("opus MFT SetOutputType failed: hr={hr_out:#x}"); - return false; - } - - // SAFETY: `IMFTransform::ProcessMessage` is slot 23. - unsafe { - let msg: unsafe extern "system" fn(*mut c_void, u32, usize) -> i32 = - vtable_slot(self.transform, 23); - let _ = msg(self.transform, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0); - let _ = msg(self.transform, MFT_MESSAGE_NOTIFY_START_OF_STREAM, 0); - } - true + hr_out >= 0 } fn decode_packet(&self, packet: &[u8]) -> Result, i32> { @@ -500,6 +521,24 @@ impl WindowsOpusMft { } } + let mut samples = Vec::new(); + self.drain_output(&mut samples)?; + Ok(samples) + } + + fn drain(&self) -> Result, i32> { + // SAFETY: `IMFTransform::ProcessMessage` is slot 23. + unsafe { + let msg: unsafe extern "system" fn(*mut c_void, u32, usize) -> i32 = + vtable_slot(self.transform, 23); + let _ = msg(self.transform, MFT_MESSAGE_COMMAND_DRAIN, 0); + } + let mut samples = Vec::new(); + self.drain_output(&mut samples)?; + Ok(samples) + } + + fn drain_output(&self, samples: &mut Vec) -> Result<(), i32> { // Query whether the MFT allocates its own output samples. #[repr(C)] struct MftOutputStreamInfo { @@ -522,7 +561,6 @@ impl WindowsOpusMft { get_osi(self.transform, 0, &mut osi) >= 0 && (osi.flags & 0x300) != 0 }; - let mut samples = Vec::new(); loop { let mut caller_buf = std::ptr::null_mut(); let mut caller_sample = std::ptr::null_mut(); @@ -568,6 +606,14 @@ impl WindowsOpusMft { if !odb.events.is_null() { com_release(odb.events); } + if hr == MF_E_TRANSFORM_STREAM_CHANGE { + com_release(caller_sample); + com_release(caller_buf); + if !self.select_output_type() { + return Err(hr); + } + continue; + } if hr == MF_E_TRANSFORM_NEED_MORE_INPUT { com_release(caller_sample); com_release(caller_buf); @@ -595,12 +641,12 @@ impl WindowsOpusMft { vtable_slot(cont_buf, 4); let mut ptr = std::ptr::null_mut(); let mut cur_len = 0u32; - if lock(cont_buf, &mut ptr, std::ptr::null_mut(), &mut cur_len) >= 0 - && cur_len >= 4 - { - let bytes = std::slice::from_raw_parts(ptr, cur_len as usize); - for &chunk in bytes.as_chunks::<4>().0 { - samples.push(f32::from_le_bytes(chunk)); + if lock(cont_buf, &mut ptr, std::ptr::null_mut(), &mut cur_len) >= 0 { + if cur_len >= 4 { + let bytes = std::slice::from_raw_parts(ptr, cur_len as usize); + for &chunk in bytes.as_chunks::<4>().0 { + samples.push(f32::from_le_bytes(chunk)); + } } let _ = unlock(cont_buf); } @@ -611,7 +657,7 @@ impl WindowsOpusMft { com_release(caller_buf); } } - Ok(samples) + Ok(()) } } @@ -658,6 +704,8 @@ fn compare_stream(channels: usize, packets: &[Vec], label: &str) { let decoded_mft = mft.decode_packet(pkt).expect("Windows Opus MFT decode"); mft_pcm.extend_from_slice(&decoded_mft); } + let drained = mft.drain().expect("Windows Opus MFT drain"); + mft_pcm.extend_from_slice(&drained); assert_eq!( pure_pcm.len(), @@ -704,6 +752,12 @@ fn opus_pure_matches_windows_mft_on_synthetic_streams() { for channels in [1usize, 2] { let packets = synth_packets(channels, 10); let path = dir.path().join(format!("synth-{channels}ch.opuscap")); + // Seed a stale capture with a different channel count to verify + // `create_capture_file` truncates instead of concatenating runs. + apu::create_capture_file(&path, if channels == 1 { 2 } else { 1 }).unwrap(); + apu::append_capture_packet(&path, if channels == 1 { 2 } else { 1 }, &[0xAA, 0xBB]) + .unwrap(); + apu::create_capture_file(&path, channels as u16).unwrap(); for pkt in &packets { apu::append_capture_packet(&path, channels as u16, pkt).unwrap(); }