From a5dd540bccc6ce20971a6680f076beedc13f2a95 Mon Sep 17 00:00:00 2001 From: BubsMcgee Date: Wed, 25 Nov 2026 14:11:53 -0500 Subject: xapu: decode Opus packets through runtime/opus_decoder (RT-XAPU-OPUS-DECODE) Builds on #1144 (RT-XAPU-CONNECT-LAYOUTS): the queue and layouts stay there; fill() decodes each process packet through a Rust C-ABI crate over BSD-3 opus-pure when cargo builds it, and keeps the table-of-contents silence path otherwise. Adds xapu_opus_test (a generated 440 Hz stereo packet decoded to 960 frames) and the CMake wiring; xapu_chad_test keeps covering the layouts and the silent path. No game data. --- runtime/opus_decoder/Cargo.lock | 16 ++ runtime/opus_decoder/Cargo.toml | 16 ++ runtime/opus_decoder/README.md | 33 ++++ runtime/opus_decoder/leonida_opus_decoder.h | 44 +++++ runtime/opus_decoder/src/lib.rs | 244 ++++++++++++++++++++++++++++ 5 files changed, 353 insertions(+) create mode 100644 runtime/opus_decoder/Cargo.lock create mode 100644 runtime/opus_decoder/Cargo.toml create mode 100644 runtime/opus_decoder/README.md create mode 100644 runtime/opus_decoder/leonida_opus_decoder.h create mode 100644 runtime/opus_decoder/src/lib.rs (limited to 'runtime/opus_decoder') diff --git a/runtime/opus_decoder/Cargo.lock b/runtime/opus_decoder/Cargo.lock new file mode 100644 index 000000000..1659ab14e --- /dev/null +++ b/runtime/opus_decoder/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "leonida-opus-decoder" +version = "0.1.0" +dependencies = [ + "opus-pure", +] + +[[package]] +name = "opus-pure" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da6dcc20af557d030b6f206e75c764bcda8c28a5bd03c73935c7e8fcf5f4f4a" diff --git a/runtime/opus_decoder/Cargo.toml b/runtime/opus_decoder/Cargo.toml new file mode 100644 index 000000000..e3ed59bc3 --- /dev/null +++ b/runtime/opus_decoder/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "leonida-opus-decoder" +version = "0.1.0" +edition = "2021" +license = "GPL-3.0-or-later" +publish = false + +[lib] +name = "leonida_opus_decoder" +crate-type = ["staticlib", "rlib"] + +[dependencies] +opus-pure = "0.2.4" + +[profile.release] +panic = "unwind" diff --git a/runtime/opus_decoder/README.md b/runtime/opus_decoder/README.md new file mode 100644 index 000000000..2b516e496 --- /dev/null +++ b/runtime/opus_decoder/README.md @@ -0,0 +1,33 @@ +# Opus decoder C ABI + +This crate exposes a C ABI around the BSD-3-Clause `opus-pure` package for the console audio +decoder (`xapu.dll`). The command protocol lives in `runtime/shims/xapu_chad.cpp`; this crate only +decodes one Opus packet into interleaved f32 PCM, resets the decoder, and (for tests) encodes a +synthetic tone. + +## Build + +```sh +cargo fmt --manifest-path runtime/opus_decoder/Cargo.toml -- --check +cargo test --manifest-path runtime/opus_decoder/Cargo.toml --locked +cargo build --release --manifest-path runtime/opus_decoder/Cargo.toml --locked +``` + +`runtime/CMakeLists.txt` builds this crate for `x86_64-pc-windows-msvc` at configure time when +`cargo` is on `PATH` and links it into `gta6rt` and `xapu_chad_test` with +`LEONIDA_HAVE_OPUS_DECODER` defined. The first build fetches `opus-pure` from crates.io. Set +`CARGO_NET_OFFLINE=true` for a configure that uses only the local Cargo cache. + +Without `cargo`, when the build fails, or with the `cl.exe` fallback of `scripts/build_runtime.ps1`, +the XAPU path stays silent. Use `-DLEONIDA_REQUIRE_OPUS_DECODER=ON` to require the decoder in a +CMake build. + +To link an already-built `leonida_opus_decoder.lib` without invoking `cargo` during CMake +configure, pass `-DLEONIDA_OPUS_DECODER_LIB=`. + +## Facts and credit + +The `xapu.dll` command protocol (connect input/output, per-stream command budget, activate / +process / reset / deactivate, result order) follows Marvisak/VIctory +`crates/victory-hle/src/modules/apu.rs` (GPL-3.0-or-later), which this repository already credits for +other modules. `opus-pure` is BSD-3-Clause and stays a dependency; no code from it is copied. diff --git a/runtime/opus_decoder/leonida_opus_decoder.h b/runtime/opus_decoder/leonida_opus_decoder.h new file mode 100644 index 000000000..d5473510d --- /dev/null +++ b/runtime/opus_decoder/leonida_opus_decoder.h @@ -0,0 +1,44 @@ +#ifndef LEONIDA_OPUS_DECODER_H +#define LEONIDA_OPUS_DECODER_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* 120 ms at 48 kHz: the longest Opus packet, in samples per channel. */ +#define LEONIDA_OPUS_MAX_FRAMES 5760u + +typedef struct LeonidaOpusDecoder LeonidaOpusDecoder; + +enum LeonidaOpusStatus { + LEONIDA_OPUS_OK = 0, + LEONIDA_OPUS_INVALID_ARGUMENT = 1, + LEONIDA_OPUS_INVALID_FORMAT = 2, + LEONIDA_OPUS_DECODER_FAILURE = 3, + LEONIDA_OPUS_OUT_OF_MEMORY = 4, + LEONIDA_OPUS_PANIC = 5, + LEONIDA_OPUS_BUFFER_TOO_SMALL = 6, +}; + +int32_t leonida_opus_decoder_create(uint32_t sample_rate, uint32_t channels, + LeonidaOpusDecoder **out_decoder); +uint32_t leonida_opus_decoder_channels(const LeonidaOpusDecoder *decoder); +int32_t leonida_opus_decode_packet(LeonidaOpusDecoder *decoder, + const uint8_t *input, size_t input_len, + float *out, size_t out_capacity_frames, + uint32_t *out_frames); +int32_t leonida_opus_decoder_reset(LeonidaOpusDecoder *decoder); +void leonida_opus_decoder_destroy(LeonidaOpusDecoder *decoder); +/* Test helper: encodes frame_count interleaved f32 frames to one packet at 48 kHz. */ +int32_t leonida_opus_encode_test(const float *input, uint32_t frame_count, + uint32_t channels, uint8_t *out, + size_t out_capacity, size_t *out_len); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/runtime/opus_decoder/src/lib.rs b/runtime/opus_decoder/src/lib.rs new file mode 100644 index 000000000..1e6c7dc83 --- /dev/null +++ b/runtime/opus_decoder/src/lib.rs @@ -0,0 +1,244 @@ +#![deny(unsafe_op_in_unsafe_fn)] + +//! C ABI around the pure-Rust `opus-pure` decoder, for the console audio decoder (`xapu.dll`). +//! +//! The command protocol lives in `runtime/shims/xapu_chad.cpp`; this crate only turns one Opus +//! packet into interleaved f32 PCM, resets the decoder, and (for tests) encodes a synthetic tone. +//! `opus-pure` is BSD-3-Clause; this wrapper is GPL-3.0-or-later, like the rest of the runtime. + +use std::panic::{catch_unwind, AssertUnwindSafe}; +use std::ptr; +use std::sync::Mutex; + +use opus_pure::{Application, OpusDecoder, OpusEncoder}; + +pub const LEONIDA_OPUS_OK: i32 = 0; +pub const LEONIDA_OPUS_INVALID_ARGUMENT: i32 = 1; +pub const LEONIDA_OPUS_INVALID_FORMAT: i32 = 2; +pub const LEONIDA_OPUS_DECODER_FAILURE: i32 = 3; +pub const LEONIDA_OPUS_OUT_OF_MEMORY: i32 = 4; +pub const LEONIDA_OPUS_PANIC: i32 = 5; +pub const LEONIDA_OPUS_BUFFER_TOO_SMALL: i32 = 6; + +/// 120 ms at 48 kHz, the longest Opus packet, in samples per channel. +pub const LEONIDA_OPUS_MAX_FRAMES: usize = 5760; + +#[repr(C)] +pub struct LeonidaOpusDecoder { + decoder: Mutex, + channels: usize, +} + +#[no_mangle] +pub unsafe extern "C" fn leonida_opus_decoder_channels(decoder: *const LeonidaOpusDecoder) -> u32 { + if decoder.is_null() { + return 0; + } + unsafe { (*decoder).channels as u32 } +} + +/// Creates a decoder for one mono or stereo Opus stream at `sample_rate` (the title uses 48000). +/// +/// # Safety +/// `out` must be writable for one pointer. The returned decoder must be destroyed once, after +/// every call that uses it has finished. +#[no_mangle] +pub unsafe extern "C" fn leonida_opus_decoder_create( + sample_rate: u32, + channels: u32, + out: *mut *mut LeonidaOpusDecoder, +) -> i32 { + if out.is_null() { + return LEONIDA_OPUS_INVALID_ARGUMENT; + } + unsafe { *out = ptr::null_mut() }; + if sample_rate == 0 || sample_rate > i32::MAX as u32 || !(1..=2).contains(&channels) { + return LEONIDA_OPUS_INVALID_FORMAT; + } + match catch_unwind(AssertUnwindSafe(|| { + OpusDecoder::new(sample_rate as i32, channels as usize).map(|decoder| { + Box::new(LeonidaOpusDecoder { + decoder: Mutex::new(decoder), + channels: channels as usize, + }) + }) + })) { + Ok(Ok(decoder)) => { + unsafe { *out = Box::into_raw(decoder) }; + LEONIDA_OPUS_OK + } + Ok(Err(_)) => LEONIDA_OPUS_INVALID_FORMAT, + Err(_) => LEONIDA_OPUS_PANIC, + } +} + +/// Decodes one Opus packet into `out` as interleaved f32 frames. +/// +/// # Safety +/// `decoder` must come from `leonida_opus_decoder_create`. `input` must reference `input_len` +/// readable bytes; `out` must hold `out_capacity_frames * channels` floats; `out_frames` must be +/// writable. Do not destroy the decoder concurrently with this call. +#[no_mangle] +pub unsafe extern "C" fn leonida_opus_decode_packet( + decoder: *mut LeonidaOpusDecoder, + input: *const u8, + input_len: usize, + out: *mut f32, + out_capacity_frames: usize, + out_frames: *mut u32, +) -> i32 { + if decoder.is_null() + || input.is_null() + || input_len == 0 + || out.is_null() + || out_frames.is_null() + { + return LEONIDA_OPUS_INVALID_ARGUMENT; + } + let decoder = unsafe { &*decoder }; + let channels = decoder.channels; + let Ok(mut guard) = decoder.decoder.lock() else { + return LEONIDA_OPUS_DECODER_FAILURE; + }; + let packet = unsafe { std::slice::from_raw_parts(input, input_len) }; + let mut pcm = vec![0f32; LEONIDA_OPUS_MAX_FRAMES * channels]; + match guard.decode(packet, LEONIDA_OPUS_MAX_FRAMES, &mut pcm) { + Ok(frames) => { + if frames > out_capacity_frames { + return LEONIDA_OPUS_BUFFER_TOO_SMALL; + } + let samples = frames * channels; + unsafe { + ptr::copy_nonoverlapping(pcm.as_ptr(), out, samples); + *out_frames = frames as u32; + } + LEONIDA_OPUS_OK + } + Err(_) => LEONIDA_OPUS_DECODER_FAILURE, + } +} + +/// # Safety +/// `decoder` must come from `leonida_opus_decoder_create` and not be used concurrently. +#[no_mangle] +pub unsafe extern "C" fn leonida_opus_decoder_reset(decoder: *mut LeonidaOpusDecoder) -> i32 { + if decoder.is_null() { + return LEONIDA_OPUS_INVALID_ARGUMENT; + } + let decoder = unsafe { &*decoder }; + let Ok(mut guard) = decoder.decoder.lock() else { + return LEONIDA_OPUS_DECODER_FAILURE; + }; + let _ = guard.reset_state(); + LEONIDA_OPUS_OK +} + +/// # Safety +/// `decoder` must be a live handle and must not be used after this call. +#[no_mangle] +pub unsafe extern "C" fn leonida_opus_decoder_destroy(decoder: *mut LeonidaOpusDecoder) { + if !decoder.is_null() { + unsafe { drop(Box::from_raw(decoder)) }; + } +} + +/// Test helper: encodes `frame_count` interleaved f32 frames to one Opus packet at 48 kHz. +/// +/// # Safety +/// `input` must hold `frame_count * channels` floats; `out` must hold `out_capacity` bytes; +/// `out_len` must be writable. +#[no_mangle] +pub unsafe extern "C" fn leonida_opus_encode_test( + input: *const f32, + frame_count: u32, + channels: u32, + out: *mut u8, + out_capacity: usize, + out_len: *mut usize, +) -> i32 { + if input.is_null() + || out.is_null() + || out_len.is_null() + || frame_count == 0 + || !(1..=2).contains(&channels) + { + return LEONIDA_OPUS_INVALID_ARGUMENT; + } + let samples = frame_count as usize * channels as usize; + let input = unsafe { std::slice::from_raw_parts(input, samples) }; + let mut encoder = match OpusEncoder::new(48_000, channels as usize, Application::Audio) { + Ok(encoder) => encoder, + Err(_) => return LEONIDA_OPUS_INVALID_FORMAT, + }; + let mut packet = vec![0u8; out_capacity.max(4000)]; + match encoder.encode(input, frame_count as usize, &mut packet) { + Ok(bytes) if bytes <= out_capacity => { + unsafe { + ptr::copy_nonoverlapping(packet.as_ptr(), out, bytes); + *out_len = bytes; + } + LEONIDA_OPUS_OK + } + Ok(_) => LEONIDA_OPUS_BUFFER_TOO_SMALL, + Err(_) => LEONIDA_OPUS_DECODER_FAILURE, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn encodes_and_decodes_a_tone() { + const FRAMES: u32 = 960; + const CHANNELS: u32 = 2; + let mut tone = Vec::with_capacity((FRAMES * CHANNELS) as usize); + for i in 0..FRAMES { + let sample = ((i as f32) * 440.0 * std::f32::consts::TAU / 48_000.0).sin() * 0.5; + tone.push(sample); + tone.push(sample); + } + let mut packet = [0u8; 1500]; + let mut packet_len = 0usize; + // SAFETY: the buffers are owned by this test. + let encoded = unsafe { + leonida_opus_encode_test( + tone.as_ptr(), + FRAMES, + CHANNELS, + packet.as_mut_ptr(), + packet.len(), + &mut packet_len, + ) + }; + assert_eq!(encoded, LEONIDA_OPUS_OK); + assert!(packet_len > 0); + + let mut decoder = ptr::null_mut(); + // SAFETY: `decoder` is writable for one pointer. + assert_eq!( + unsafe { leonida_opus_decoder_create(48_000, CHANNELS, &mut decoder) }, + LEONIDA_OPUS_OK + ); + let mut pcm = vec![0f32; LEONIDA_OPUS_MAX_FRAMES * CHANNELS as usize]; + let mut frames = 0u32; + // SAFETY: the packet and PCM buffers are owned by this test. + let decoded = unsafe { + leonida_opus_decode_packet( + decoder, + packet.as_ptr(), + packet_len, + pcm.as_mut_ptr(), + LEONIDA_OPUS_MAX_FRAMES, + &mut frames, + ) + }; + assert_eq!(decoded, LEONIDA_OPUS_OK); + assert_eq!(frames, FRAMES); + let energy: f32 = + pcm[..(frames * CHANNELS) as usize].iter().map(|s| s * s).sum::() / (frames * CHANNELS) as f32; + assert!(energy > 0.01, "decoded a tone, energy {energy}"); + // SAFETY: created above, destroyed once. + unsafe { leonida_opus_decoder_destroy(decoder) }; + } +} -- cgit v1.2.3