//! The console's audio decoder (`xapu.dll`): Opus packets in, float PCM out. //! //! A title connects once for one kind of processing, gets a block of memory //! for its packets and output, and then queues per-stream commands //! (activate, process, reset, deactivate) and dequeues one result per //! command. We decode each command when it is queued, so its result is ready //! at once; the order of results is the order of commands. #![allow(non_snake_case)] #![allow(clippy::not_unsafe_ptr_arg_deref)] use std::alloc::Layout; use std::collections::VecDeque; use std::ffi::c_void; use std::sync::Mutex; use opus_pure::OpusDecoder; use windows_sys::Win32::Foundation::{CloseHandle, HANDLE}; use windows_sys::Win32::System::Threading::{CreateEventW, SetEvent}; use super::xasync::E_INVALIDARG; use super::xgameruntime::{HRESULT, S_OK}; use crate::{HleModule, hle_export}; const E_NOTIMPL: HRESULT = 0x8000_4001_u32 as i32; const E_OUTOFMEMORY: HRESULT = 0x8007_000E_u32 as i32; const QUEUE_FULL: HRESULT = 0x8AC8_1000_u32 as i32; /// No result yet, which titles poll for. pub(crate) const PENDING_RESULTS: HRESULT = 0x8AC8_1001_u32 as i32; const INVALID_COMMAND: HRESULT = 0x8AC8_1002_u32 as i32; const INVALID_STREAM: HRESULT = 0x8AC8_1003_u32 as i32; const ALREADY_ACTIVE: HRESULT = 0x8AC8_1005_u32 as i32; const BAD_PACKET: HRESULT = 0x8AC8_1008_u32 as i32; /// Processing kinds; only plain decoding is implemented. const DECODE_OPUS: u32 = 1; const ACTIVATE: u32 = 1; const DEACTIVATE: u32 = 2; const PROCESS: u32 = 3; const NOTIFY: u32 = 4; const RESET: u32 = 5; /// Command option: signal the connection's event when the result is ready. const SIGNAL: u32 = 1; /// Connect options. const DEINTERLEAVED_OUTPUT: u32 = 4; const RESULT_EVENT: u32 = 8; /// The longest Opus packet duration at 48 kHz (120 ms). const MAX_FRAMES: usize = 5760; #[repr(C)] pub struct ConnectInput { process: u32, streams: u32, commands_per_stream: u32, memory: u32, frames_per_output: u32, options: u32, } #[repr(C)] pub struct ConnectOutput { memory: *mut c_void, result_event: HANDLE, } /// What every command starts with, and what its result echoes. #[repr(C, packed)] #[derive(Clone, Copy)] pub struct CommandId { kind: u32, stream: u32, sequence: u32, options: u32, context: u64, } #[repr(C, packed)] struct Activate { id: CommandId, channels: u32, init_length: u32, init: *const c_void, start_pitch: f32, processing_length: u32, processing: *mut c_void, } #[repr(C, packed)] #[derive(Clone, Copy)] struct Process { id: CommandId, pitch_ramp: f32, target_pitch: f32, first_frame: u32, frame_count: u32, input_length: u32, input: *const u8, output_length: u32, output: *mut u8, } #[repr(C, packed)] #[derive(Clone, Copy)] pub struct ApuResult { id: CommandId, state: u32, state_data: u64, usec: u64, error: HRESULT, frames: u32, output_length: u32, output: *mut u8, pitch: f32, input_length: u32, input: *const u8, } // SAFETY: the raw pointers are title addresses carried through, never // dereferenced after the result is made. unsafe impl Send for ApuResult {} pub struct Connection { memory: *mut u8, layout: Layout, event: HANDLE, options: u32, per_stream: u32, state: Mutex, } struct State { streams: Vec>, /// Results not yet dequeued, per stream. queued: Vec, results: VecDeque, commands: u32, dequeued: u32, } fn lock(c: &Connection) -> std::sync::MutexGuard<'_, State> { c.state.lock().unwrap_or_else(|e| e.into_inner()) } pub extern "system" fn XApuConnect( input: *const ConnectInput, output: *mut ConnectOutput, handle: *mut *mut Connection, ) -> HRESULT { if input.is_null() || output.is_null() || handle.is_null() { return E_INVALIDARG; } // SAFETY: checked non-null. let input = unsafe { &*input }; if !(1..=500).contains(&input.streams) || !(1..=16).contains(&input.commands_per_stream) || input.memory == 0 { return E_INVALIDARG; } if input.process != DECODE_OPUS { // shortcut: rate conversion (decode-convert, PCM convert) is not // implemented; add a resampler if a title connects for it. tracing::warn!( process = input.process, "audio decoder: processing kind not implemented" ); return E_NOTIMPL; } if input.frames_per_output != 0 { return E_INVALIDARG; } let Ok(layout) = Layout::from_size_align(input.memory as usize, 16) else { return E_INVALIDARG; }; // SAFETY: non-zero size. let memory = unsafe { std::alloc::alloc_zeroed(layout) }; if memory.is_null() { return E_OUTOFMEMORY; } let event = if input.options & RESULT_EVENT != 0 { // SAFETY: plain auto-reset event, no name. let event = unsafe { CreateEventW(std::ptr::null(), 0, 0, std::ptr::null()) }; if event.is_null() { // SAFETY: allocated above with `layout`. unsafe { std::alloc::dealloc(memory, layout) }; return E_OUTOFMEMORY; } event } else { std::ptr::null_mut() }; let streams = input.streams as usize; tracing::info!( streams, memory = input.memory, options = input.options, "audio decoder connected" ); let connection = Box::new(Connection { memory, layout, event, options: input.options, per_stream: input.commands_per_stream, state: Mutex::new(State { streams: (0..streams).map(|_| None).collect(), queued: vec![0; streams], results: VecDeque::new(), commands: 0, dequeued: 0, }), }); // SAFETY: checked non-null. unsafe { output.write(ConnectOutput { memory: memory.cast(), result_event: event, }); handle.write(Box::into_raw(connection)); } S_OK } pub extern "system" fn XApuDisconnect(handle: *mut Connection) -> HRESULT { if handle.is_null() { return E_INVALIDARG; } // SAFETY: made by `XApuConnect`; disconnected once. let connection = unsafe { Box::from_raw(handle) }; if !connection.event.is_null() { // SAFETY: our event. unsafe { CloseHandle(connection.event) }; } // SAFETY: allocated in `XApuConnect` with this layout. unsafe { std::alloc::dealloc(connection.memory, connection.layout) }; S_OK } pub extern "system" fn XApuEnqueueCommand( handle: *mut Connection, command: *const CommandId, total: *mut u32, ) -> HRESULT { if handle.is_null() || command.is_null() { return E_INVALIDARG; } // SAFETY: a connection from `XApuConnect`; every command starts with an id. let (connection, id) = unsafe { (&*handle, command.read_unaligned()) }; let mut state = lock(connection); let stream = id.stream as usize; if stream >= state.streams.len() { return INVALID_STREAM; } if state.queued[stream] >= connection.per_stream { return QUEUE_FULL; } let mut result = ApuResult { id, state: 0, state_data: 0, usec: 0, error: S_OK, frames: 0, output_length: 0, output: std::ptr::null_mut(), pitch: 1.0, input_length: 0, input: std::ptr::null(), }; let decoder = &mut state.streams[stream]; match id.kind { ACTIVATE => { // SAFETY: an activate command is this struct. let channels = unsafe { command.cast::().read_unaligned() }.channels; tracing::debug!(stream, channels, "Opus stream activated"); result.error = if decoder.is_some() { ALREADY_ACTIVE } else if !(1..=2).contains(&channels) { E_INVALIDARG } else { match OpusDecoder::new(48_000, channels as usize) { Ok(d) => { *decoder = Some(d); S_OK } Err(_) => E_INVALIDARG, } }; } DEACTIVATE => *decoder = None, RESET => { if let Some(d) = decoder { let _ = d.reset_state(); } } NOTIFY => {} PROCESS => { // 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), None => result.error = INVALID_STREAM, } } _ => return INVALID_COMMAND, } state.queued[stream] += 1; state.commands = state.commands.wrapping_add(1); state.results.push_back(result); if !total.is_null() { // SAFETY: optional out parameter. unsafe { total.write(state.commands) }; } if id.options & SIGNAL != 0 && !connection.event.is_null() { // SAFETY: our event. unsafe { SetEvent(connection.event) }; } S_OK } /// 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) { let channels = decoder.channels(); let (input, input_length) = (command.input, command.input_length); let (output, output_length) = (command.output, command.output_length); result.input = input; result.input_length = input_length; result.output = output; if input.is_null() || output.is_null() || input_length == 0 { result.error = E_INVALIDARG; return; } // SAFETY: the title's packet in its connection memory. let packet = unsafe { std::slice::from_raw_parts(input, input_length as usize) }; let mut pcm = vec![0f32; MAX_FRAMES * channels]; let decoded = match decoder.decode(packet, MAX_FRAMES, &mut pcm) { Ok(n) => n, Err(_) => { result.error = BAD_PACKET; return; } }; let first = (command.first_frame as usize).min(decoded); let frames = (decoded - first) .min(command.frame_count as usize) .min(output_length as usize / (4 * channels)); let kept = &pcm[first * channels..(first + frames) * channels]; let bytes = frames * channels * 4; if bytes > 0 { // SAFETY: `output` is non-null and holds at least `bytes <= output_length` bytes; // writing raw bytes avoids requiring 4-byte alignment on the title's `*mut u8`. let out = unsafe { std::slice::from_raw_parts_mut(output, bytes) }; if options & DEINTERLEAVED_OUTPUT != 0 && channels == 2 { let (left, right) = out.split_at_mut(frames * 4); let (left, _) = left.as_chunks_mut::<4>(); let (right, _) = right.as_chunks_mut::<4>(); for (i, frame) in kept.as_chunks::<2>().0.iter().enumerate() { left[i] = frame[0].to_le_bytes(); right[i] = frame[1].to_le_bytes(); } } else { for (dst, &sample) in out.as_chunks_mut::<4>().0.iter_mut().zip(kept) { *dst = sample.to_le_bytes(); } } } result.frames = frames as u32; result.output_length = bytes as u32; } pub extern "system" fn XApuDequeueResult(handle: *mut Connection, out: *mut ApuResult) -> HRESULT { if handle.is_null() || out.is_null() { return E_INVALIDARG; } // SAFETY: a connection from `XApuConnect`. let mut state = lock(unsafe { &*handle }); let Some(result) = state.results.pop_front() else { return PENDING_RESULTS; }; let stream = result.id.stream as usize; state.queued[stream] -= 1; state.dequeued = state.dequeued.wrapping_add(1); // SAFETY: the title passes room for one result. unsafe { out.write_unaligned(result) }; S_OK } pub extern "system" fn XApuGetResultCount(handle: *mut Connection, total: *mut u32) -> u32 { if handle.is_null() { return 0; } // SAFETY: a connection from `XApuConnect`. let state = lock(unsafe { &*handle }); if !total.is_null() { // SAFETY: optional out parameter. unsafe { total.write(state.dequeued) }; } state.results.len() as u32 } pub(crate) fn module() -> HleModule { HleModule { name: "apu", dlls: &["xapu.dll"], exports: vec![ hle_export!(XApuConnect), hle_export!(XApuDisconnect), hle_export!(XApuEnqueueCommand), hle_export!(XApuDequeueResult), hle_export!(XApuGetResultCount), ], } } #[cfg(test)] mod tests { use super::*; use opus_pure::{Application, OpusEncoder}; fn id(kind: u32, stream: u32) -> CommandId { CommandId { kind, stream, sequence: 7, options: 0, context: 0x1234, } } #[test] fn decodes_opus_as_the_title_drives_it() { assert_eq!(size_of::(), 0x50); // 256 mono/stereo streams, two commands each, as the title connects. let input = ConnectInput { process: DECODE_OPUS, streams: 256, commands_per_stream: 2, memory: 0x10_4000, frames_per_output: 0, options: 1, }; let mut output = ConnectOutput { memory: std::ptr::null_mut(), result_event: std::ptr::null_mut(), }; let mut handle = std::ptr::null_mut(); assert_eq!(XApuConnect(&input, &mut output, &mut handle), S_OK); let base = output.memory.cast::(); let activate = Activate { id: id(ACTIVATE, 3), channels: 2, init_length: 0, init: std::ptr::null(), start_pitch: 1.0, processing_length: 0, processing: std::ptr::null_mut(), }; assert_eq!( XApuEnqueueCommand(handle, (&raw const activate).cast(), std::ptr::null_mut()), S_OK ); // A 20 ms stereo packet of a 440 Hz tone. let tone: Vec = (0..960 * 2) .map(|i| ((i / 2) as f32 * 440.0 * std::f32::consts::TAU / 48_000.0).sin() * 0.5) .collect(); let mut encoder = OpusEncoder::new(48_000, 2, Application::Audio).unwrap(); let mut packet = [0u8; 1500]; let n = encoder.encode(&tone, 960, &mut packet).unwrap(); // SAFETY: inside the connection memory. unsafe { std::ptr::copy_nonoverlapping(packet.as_ptr(), base, n) }; let process = Process { id: id(PROCESS, 3), pitch_ramp: 0.0, target_pitch: 1.0, first_frame: 0, frame_count: 960, input_length: n as u32, input: base, output_length: 0xf00 * 2, output: base.wrapping_add(0x14000), }; let mut total = 0; assert_eq!( XApuEnqueueCommand(handle, (&raw const process).cast(), &mut total), S_OK ); assert_eq!(total, 2); // Two results queued for stream 3: its queue is full. assert_eq!( XApuEnqueueCommand(handle, (&raw const process).cast(), &mut total), QUEUE_FULL ); assert_eq!(XApuGetResultCount(handle, std::ptr::null_mut()), 2); let mut result = std::mem::MaybeUninit::::zeroed(); assert_eq!(XApuDequeueResult(handle, result.as_mut_ptr()), S_OK); // SAFETY: written above. let first = unsafe { result.assume_init() }; let (kind, error, context) = (first.id.kind, first.error, first.id.context); assert_eq!((kind, error, context), (ACTIVATE, S_OK, 0x1234)); assert_eq!(XApuDequeueResult(handle, result.as_mut_ptr()), S_OK); // SAFETY: written above. let decoded = unsafe { result.assume_init() }; let (kind, error, frames, length) = ( decoded.id.kind, decoded.error, decoded.frames, decoded.output_length, ); assert_eq!((kind, error, frames, length), (PROCESS, S_OK, 960, 960 * 8)); // SAFETY: the decoder wrote 960 stereo frames there. let pcm = unsafe { std::slice::from_raw_parts(base.wrapping_add(0x14000).cast::(), 1920) }; let energy: f32 = pcm.iter().map(|s| s * s).sum::() / 1920.0; assert!(energy > 0.01, "decoded a tone, energy {energy}"); assert_eq!( XApuDequeueResult(handle, result.as_mut_ptr()), PENDING_RESULTS ); let mut dequeued = 0; assert_eq!(XApuGetResultCount(handle, &mut dequeued), 0); assert_eq!(dequeued, 2); // A process on a stream that was never activated fails in its result. let mut stray = process; stray.id.stream = 4; assert_eq!( XApuEnqueueCommand(handle, (&raw const stray).cast(), &mut total), S_OK ); assert_eq!(XApuDequeueResult(handle, result.as_mut_ptr()), S_OK); // SAFETY: written above. let error = unsafe { result.assume_init() }.error; assert_eq!(error, INVALID_STREAM); assert_eq!(XApuDisconnect(handle), S_OK); } #[test] fn decodes_into_unaligned_output_interleaved_and_deinterleaved() { let tone: Vec = (0..960 * 2) .map(|i| { let phase = (i / 2) as f32 * std::f32::consts::TAU / 48_000.0; if i % 2 == 0 { (phase * 440.0).sin() * 0.5 } else { (phase * 880.0).sin() * 0.25 } }) .collect(); let mut encoder = OpusEncoder::new(48_000, 2, Application::Audio).unwrap(); let mut packet = [0u8; 1500]; let n = encoder.encode(&tone, 960, &mut packet).unwrap(); for (options, deinterleaved) in [(0u32, false), (DEINTERLEAVED_OUTPUT, true)] { let input = ConnectInput { process: DECODE_OPUS, streams: 4, commands_per_stream: 4, memory: 0x4_0000, frames_per_output: 0, options, }; let mut output = ConnectOutput { memory: std::ptr::null_mut(), result_event: std::ptr::null_mut(), }; let mut handle = std::ptr::null_mut(); assert_eq!(XApuConnect(&input, &mut output, &mut handle), S_OK); let base = output.memory.cast::(); // Invalid channel counts fail cleanly without activating the stream. for bad_channels in [0u32, 3, u32::MAX] { let bad = Activate { id: id(ACTIVATE, 0), channels: bad_channels, init_length: 0, init: std::ptr::null(), start_pitch: 1.0, processing_length: 0, processing: std::ptr::null_mut(), }; assert_eq!( XApuEnqueueCommand(handle, (&raw const bad).cast(), std::ptr::null_mut()), S_OK ); let mut res = std::mem::MaybeUninit::::zeroed(); assert_eq!(XApuDequeueResult(handle, res.as_mut_ptr()), S_OK); // SAFETY: written by `XApuDequeueResult`. let err = unsafe { res.assume_init() }.error; assert_eq!(err, E_INVALIDARG, "channels={bad_channels}"); } // Activate two identical stereo decoders so aligned and unaligned outputs match bit-for-bit. for stream in [0u32, 1] { let activate = Activate { id: id(ACTIVATE, stream), channels: 2, init_length: 0, init: std::ptr::null(), start_pitch: 1.0, processing_length: 0, processing: std::ptr::null_mut(), }; assert_eq!( XApuEnqueueCommand(handle, (&raw const activate).cast(), std::ptr::null_mut()), S_OK ); let mut res = std::mem::MaybeUninit::::zeroed(); assert_eq!(XApuDequeueResult(handle, res.as_mut_ptr()), S_OK); // SAFETY: written by `XApuDequeueResult`. let err = unsafe { res.assume_init() }.error; assert_eq!(err, S_OK); } // SAFETY: inside the connection memory. unsafe { std::ptr::copy_nonoverlapping(packet.as_ptr(), base, n) }; let aligned_out = base.wrapping_add(0x10000); let unaligned_out = base.wrapping_add(0x20001); // 1 mod 4: misaligned for f32 for (stream, out_ptr, frame_count) in [ (0u32, aligned_out, 960u32), (1u32, unaligned_out, 960u32), (1u32, base.wrapping_add(0x30003), 0u32), // zero frames at misaligned ptr ] { let proc_cmd = Process { id: id(PROCESS, stream), pitch_ramp: 0.0, target_pitch: 1.0, first_frame: 0, frame_count, input_length: n as u32, input: base, output_length: 960 * 8, output: out_ptr, }; assert_eq!( XApuEnqueueCommand(handle, (&raw const proc_cmd).cast(), std::ptr::null_mut()), S_OK ); let mut res = std::mem::MaybeUninit::::zeroed(); assert_eq!(XApuDequeueResult(handle, res.as_mut_ptr()), S_OK); // SAFETY: written by `XApuDequeueResult`. let decoded = unsafe { res.assume_init() }; let (err, frames, len) = (decoded.error, decoded.frames, decoded.output_length); assert_eq!((err, frames, len), (S_OK, frame_count, frame_count * 8)); } // SAFETY: both output regions are inside connection memory and hold 960 * 8 bytes. let (aligned_bytes, unaligned_bytes) = unsafe { ( std::slice::from_raw_parts(aligned_out, 960 * 8), std::slice::from_raw_parts(unaligned_out, 960 * 8), ) }; assert_eq!( unaligned_bytes, aligned_bytes, "unaligned output must match aligned output (deinterleaved={deinterleaved})" ); let samples: Vec = unaligned_bytes .as_chunks::<4>() .0 .iter() .map(|c| f32::from_le_bytes(*c)) .collect(); let energy: f32 = samples.iter().map(|s| s * s).sum::() / samples.len() as f32; assert!( energy > 0.01, "expected decoded tone (deinterleaved={deinterleaved}), energy={energy}" ); assert_eq!(XApuDisconnect(handle), S_OK); } } }