diff options
| author | BubsMcgee <alexbraden99@gmail.com> | 2026-11-25 14:11:53 -0500 |
|---|---|---|
| committer | BubsMcgee <alexbraden99@gmail.com> | 2026-11-25 14:11:53 -0500 |
| commit | a5dd540bccc6ce20971a6680f076beedc13f2a95 (patch) | |
| tree | 5b5c60d1027384485d63b57e35def6bd38e65ee6 /runtime/opus_decoder/src/lib.rs | |
| parent | 6ee7ef89b948a9942f5de0b61d1f6756f477d6d5 (diff) | |
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.
Diffstat (limited to 'runtime/opus_decoder/src/lib.rs')
| -rw-r--r-- | runtime/opus_decoder/src/lib.rs | 244 |
1 files changed, 244 insertions, 0 deletions
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 @@ | |||
| 1 | #![deny(unsafe_op_in_unsafe_fn)] | ||
| 2 | |||
| 3 | //! C ABI around the pure-Rust `opus-pure` decoder, for the console audio decoder (`xapu.dll`). | ||
| 4 | //! | ||
| 5 | //! The command protocol lives in `runtime/shims/xapu_chad.cpp`; this crate only turns one Opus | ||
| 6 | //! packet into interleaved f32 PCM, resets the decoder, and (for tests) encodes a synthetic tone. | ||
| 7 | //! `opus-pure` is BSD-3-Clause; this wrapper is GPL-3.0-or-later, like the rest of the runtime. | ||
| 8 | |||
| 9 | use std::panic::{catch_unwind, AssertUnwindSafe}; | ||
| 10 | use std::ptr; | ||
| 11 | use std::sync::Mutex; | ||
| 12 | |||
| 13 | use opus_pure::{Application, OpusDecoder, OpusEncoder}; | ||
| 14 | |||
| 15 | pub const LEONIDA_OPUS_OK: i32 = 0; | ||
| 16 | pub const LEONIDA_OPUS_INVALID_ARGUMENT: i32 = 1; | ||
| 17 | pub const LEONIDA_OPUS_INVALID_FORMAT: i32 = 2; | ||
| 18 | pub const LEONIDA_OPUS_DECODER_FAILURE: i32 = 3; | ||
| 19 | pub const LEONIDA_OPUS_OUT_OF_MEMORY: i32 = 4; | ||
| 20 | pub const LEONIDA_OPUS_PANIC: i32 = 5; | ||
| 21 | pub const LEONIDA_OPUS_BUFFER_TOO_SMALL: i32 = 6; | ||
| 22 | |||
| 23 | /// 120 ms at 48 kHz, the longest Opus packet, in samples per channel. | ||
| 24 | pub const LEONIDA_OPUS_MAX_FRAMES: usize = 5760; | ||
| 25 | |||
| 26 | #[repr(C)] | ||
| 27 | pub struct LeonidaOpusDecoder { | ||
| 28 | decoder: Mutex<OpusDecoder>, | ||
| 29 | channels: usize, | ||
| 30 | } | ||
| 31 | |||
| 32 | #[no_mangle] | ||
| 33 | pub unsafe extern "C" fn leonida_opus_decoder_channels(decoder: *const LeonidaOpusDecoder) -> u32 { | ||
| 34 | if decoder.is_null() { | ||
| 35 | return 0; | ||
| 36 | } | ||
| 37 | unsafe { (*decoder).channels as u32 } | ||
| 38 | } | ||
| 39 | |||
| 40 | /// Creates a decoder for one mono or stereo Opus stream at `sample_rate` (the title uses 48000). | ||
| 41 | /// | ||
| 42 | /// # Safety | ||
| 43 | /// `out` must be writable for one pointer. The returned decoder must be destroyed once, after | ||
| 44 | /// every call that uses it has finished. | ||
| 45 | #[no_mangle] | ||
| 46 | pub unsafe extern "C" fn leonida_opus_decoder_create( | ||
| 47 | sample_rate: u32, | ||
| 48 | channels: u32, | ||
| 49 | out: *mut *mut LeonidaOpusDecoder, | ||
| 50 | ) -> i32 { | ||
| 51 | if out.is_null() { | ||
| 52 | return LEONIDA_OPUS_INVALID_ARGUMENT; | ||
| 53 | } | ||
| 54 | unsafe { *out = ptr::null_mut() }; | ||
| 55 | if sample_rate == 0 || sample_rate > i32::MAX as u32 || !(1..=2).contains(&channels) { | ||
| 56 | return LEONIDA_OPUS_INVALID_FORMAT; | ||
| 57 | } | ||
| 58 | match catch_unwind(AssertUnwindSafe(|| { | ||
| 59 | OpusDecoder::new(sample_rate as i32, channels as usize).map(|decoder| { | ||
| 60 | Box::new(LeonidaOpusDecoder { | ||
| 61 | decoder: Mutex::new(decoder), | ||
| 62 | channels: channels as usize, | ||
| 63 | }) | ||
| 64 | }) | ||
| 65 | })) { | ||
| 66 | Ok(Ok(decoder)) => { | ||
| 67 | unsafe { *out = Box::into_raw(decoder) }; | ||
| 68 | LEONIDA_OPUS_OK | ||
| 69 | } | ||
| 70 | Ok(Err(_)) => LEONIDA_OPUS_INVALID_FORMAT, | ||
| 71 | Err(_) => LEONIDA_OPUS_PANIC, | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | /// Decodes one Opus packet into `out` as interleaved f32 frames. | ||
| 76 | /// | ||
| 77 | /// # Safety | ||
| 78 | /// `decoder` must come from `leonida_opus_decoder_create`. `input` must reference `input_len` | ||
| 79 | /// readable bytes; `out` must hold `out_capacity_frames * channels` floats; `out_frames` must be | ||
| 80 | /// writable. Do not destroy the decoder concurrently with this call. | ||
| 81 | #[no_mangle] | ||
| 82 | pub unsafe extern "C" fn leonida_opus_decode_packet( | ||
| 83 | decoder: *mut LeonidaOpusDecoder, | ||
| 84 | input: *const u8, | ||
| 85 | input_len: usize, | ||
| 86 | out: *mut f32, | ||
| 87 | out_capacity_frames: usize, | ||
| 88 | out_frames: *mut u32, | ||
| 89 | ) -> i32 { | ||
| 90 | if decoder.is_null() | ||
| 91 | || input.is_null() | ||
| 92 | || input_len == 0 | ||
| 93 | || out.is_null() | ||
| 94 | || out_frames.is_null() | ||
| 95 | { | ||
| 96 | return LEONIDA_OPUS_INVALID_ARGUMENT; | ||
| 97 | } | ||
| 98 | let decoder = unsafe { &*decoder }; | ||
| 99 | let channels = decoder.channels; | ||
| 100 | let Ok(mut guard) = decoder.decoder.lock() else { | ||
| 101 | return LEONIDA_OPUS_DECODER_FAILURE; | ||
| 102 | }; | ||
| 103 | let packet = unsafe { std::slice::from_raw_parts(input, input_len) }; | ||
| 104 | let mut pcm = vec![0f32; LEONIDA_OPUS_MAX_FRAMES * channels]; | ||
| 105 | match guard.decode(packet, LEONIDA_OPUS_MAX_FRAMES, &mut pcm) { | ||
| 106 | Ok(frames) => { | ||
| 107 | if frames > out_capacity_frames { | ||
| 108 | return LEONIDA_OPUS_BUFFER_TOO_SMALL; | ||
| 109 | } | ||
| 110 | let samples = frames * channels; | ||
| 111 | unsafe { | ||
| 112 | ptr::copy_nonoverlapping(pcm.as_ptr(), out, samples); | ||
| 113 | *out_frames = frames as u32; | ||
| 114 | } | ||
| 115 | LEONIDA_OPUS_OK | ||
| 116 | } | ||
| 117 | Err(_) => LEONIDA_OPUS_DECODER_FAILURE, | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | /// # Safety | ||
| 122 | /// `decoder` must come from `leonida_opus_decoder_create` and not be used concurrently. | ||
| 123 | #[no_mangle] | ||
| 124 | pub unsafe extern "C" fn leonida_opus_decoder_reset(decoder: *mut LeonidaOpusDecoder) -> i32 { | ||
| 125 | if decoder.is_null() { | ||
| 126 | return LEONIDA_OPUS_INVALID_ARGUMENT; | ||
| 127 | } | ||
| 128 | let decoder = unsafe { &*decoder }; | ||
| 129 | let Ok(mut guard) = decoder.decoder.lock() else { | ||
| 130 | return LEONIDA_OPUS_DECODER_FAILURE; | ||
| 131 | }; | ||
| 132 | let _ = guard.reset_state(); | ||
| 133 | LEONIDA_OPUS_OK | ||
| 134 | } | ||
| 135 | |||
| 136 | /// # Safety | ||
| 137 | /// `decoder` must be a live handle and must not be used after this call. | ||
| 138 | #[no_mangle] | ||
| 139 | pub unsafe extern "C" fn leonida_opus_decoder_destroy(decoder: *mut LeonidaOpusDecoder) { | ||
| 140 | if !decoder.is_null() { | ||
| 141 | unsafe { drop(Box::from_raw(decoder)) }; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | /// Test helper: encodes `frame_count` interleaved f32 frames to one Opus packet at 48 kHz. | ||
| 146 | /// | ||
| 147 | /// # Safety | ||
| 148 | /// `input` must hold `frame_count * channels` floats; `out` must hold `out_capacity` bytes; | ||
| 149 | /// `out_len` must be writable. | ||
| 150 | #[no_mangle] | ||
| 151 | pub unsafe extern "C" fn leonida_opus_encode_test( | ||
| 152 | input: *const f32, | ||
| 153 | frame_count: u32, | ||
| 154 | channels: u32, | ||
| 155 | out: *mut u8, | ||
| 156 | out_capacity: usize, | ||
| 157 | out_len: *mut usize, | ||
| 158 | ) -> i32 { | ||
| 159 | if input.is_null() | ||
| 160 | || out.is_null() | ||
| 161 | || out_len.is_null() | ||
| 162 | || frame_count == 0 | ||
| 163 | || !(1..=2).contains(&channels) | ||
| 164 | { | ||
| 165 | return LEONIDA_OPUS_INVALID_ARGUMENT; | ||
| 166 | } | ||
| 167 | let samples = frame_count as usize * channels as usize; | ||
| 168 | let input = unsafe { std::slice::from_raw_parts(input, samples) }; | ||
| 169 | let mut encoder = match OpusEncoder::new(48_000, channels as usize, Application::Audio) { | ||
| 170 | Ok(encoder) => encoder, | ||
| 171 | Err(_) => return LEONIDA_OPUS_INVALID_FORMAT, | ||
| 172 | }; | ||
| 173 | let mut packet = vec![0u8; out_capacity.max(4000)]; | ||
| 174 | match encoder.encode(input, frame_count as usize, &mut packet) { | ||
| 175 | Ok(bytes) if bytes <= out_capacity => { | ||
| 176 | unsafe { | ||
| 177 | ptr::copy_nonoverlapping(packet.as_ptr(), out, bytes); | ||
| 178 | *out_len = bytes; | ||
| 179 | } | ||
| 180 | LEONIDA_OPUS_OK | ||
| 181 | } | ||
| 182 | Ok(_) => LEONIDA_OPUS_BUFFER_TOO_SMALL, | ||
| 183 | Err(_) => LEONIDA_OPUS_DECODER_FAILURE, | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | #[cfg(test)] | ||
| 188 | mod tests { | ||
| 189 | use super::*; | ||
| 190 | |||
| 191 | #[test] | ||
| 192 | fn encodes_and_decodes_a_tone() { | ||
| 193 | const FRAMES: u32 = 960; | ||
| 194 | const CHANNELS: u32 = 2; | ||
| 195 | let mut tone = Vec::with_capacity((FRAMES * CHANNELS) as usize); | ||
| 196 | for i in 0..FRAMES { | ||
| 197 | let sample = ((i as f32) * 440.0 * std::f32::consts::TAU / 48_000.0).sin() * 0.5; | ||
| 198 | tone.push(sample); | ||
| 199 | tone.push(sample); | ||
| 200 | } | ||
| 201 | let mut packet = [0u8; 1500]; | ||
| 202 | let mut packet_len = 0usize; | ||
| 203 | // SAFETY: the buffers are owned by this test. | ||
| 204 | let encoded = unsafe { | ||
| 205 | leonida_opus_encode_test( | ||
| 206 | tone.as_ptr(), | ||
| 207 | FRAMES, | ||
| 208 | CHANNELS, | ||
| 209 | packet.as_mut_ptr(), | ||
| 210 | packet.len(), | ||
| 211 | &mut packet_len, | ||
| 212 | ) | ||
| 213 | }; | ||
| 214 | assert_eq!(encoded, LEONIDA_OPUS_OK); | ||
| 215 | assert!(packet_len > 0); | ||
| 216 | |||
| 217 | let mut decoder = ptr::null_mut(); | ||
| 218 | // SAFETY: `decoder` is writable for one pointer. | ||
| 219 | assert_eq!( | ||
| 220 | unsafe { leonida_opus_decoder_create(48_000, CHANNELS, &mut decoder) }, | ||
| 221 | LEONIDA_OPUS_OK | ||
| 222 | ); | ||
| 223 | let mut pcm = vec![0f32; LEONIDA_OPUS_MAX_FRAMES * CHANNELS as usize]; | ||
| 224 | let mut frames = 0u32; | ||
| 225 | // SAFETY: the packet and PCM buffers are owned by this test. | ||
| 226 | let decoded = unsafe { | ||
| 227 | leonida_opus_decode_packet( | ||
| 228 | decoder, | ||
| 229 | packet.as_ptr(), | ||
| 230 | packet_len, | ||
| 231 | pcm.as_mut_ptr(), | ||
| 232 | LEONIDA_OPUS_MAX_FRAMES, | ||
| 233 | &mut frames, | ||
| 234 | ) | ||
| 235 | }; | ||
| 236 | assert_eq!(decoded, LEONIDA_OPUS_OK); | ||
| 237 | assert_eq!(frames, FRAMES); | ||
| 238 | let energy: f32 = | ||
| 239 | pcm[..(frames * CHANNELS) as usize].iter().map(|s| s * s).sum::<f32>() / (frames * CHANNELS) as f32; | ||
| 240 | assert!(energy > 0.01, "decoded a tone, energy {energy}"); | ||
| 241 | // SAFETY: created above, destroyed once. | ||
| 242 | unsafe { leonida_opus_decoder_destroy(decoder) }; | ||
| 243 | } | ||
| 244 | } | ||