diff options
| author | andretuta <andreviniciusmerlia@gmail.com> | 2026-11-24 03:53:37 -0300 |
|---|---|---|
| committer | andretuta <andreviniciusmerlia@gmail.com> | 2026-11-24 03:53:37 -0300 |
| commit | cfcedb58e2802c5cd38afd1cbde877ae9d71c2a5 (patch) | |
| tree | aa73abcbd3f2fb1685127392ad8716717831c725 /runtime/xma_decoder | |
| parent | 12536c2ab33072b8f49a8d012c970093c90c0866 (diff) | |
acphal: decode XMA contexts with runtime/xma_decoder when cargo is available
Replace the hand-written C++ XMA2/WMA Pro decoder with the Rust decoder
already on main. runtime/CMakeLists.txt builds it at configure time when
cargo is on PATH and links it into gta6rt and acphal_test
(LEONIDA_HAVE_XMA2_DECODER); without cargo, if that build fails, or with
the cl.exe fallback, XMA voices output silence for each packet's frame
count.
The ring engine decodes whole 2048-byte packets: the read offset and the
loop points are used per packet, so a loop plays its end packet in full
and restarts with a fresh decoder at the packet holding the loop start.
The C ABI gains leonida_xma2_decoder_create_hardware (no first-frame drop,
no 64-sample start skip), since the context carries its own skip.
Tested: acphal_test 129 checks (silence) and 125 checks (Rust decoder,
gnullvm build), 0 failures, 3 runs each, clang 23 / mingw x64; cargo fmt
and cargo test (gnullvm); pytest build/docs suites 204 passed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/xma_decoder')
| -rw-r--r-- | runtime/xma_decoder/README.md | 6 | ||||
| -rw-r--r-- | runtime/xma_decoder/leonida_xma2_decoder.h | 4 | ||||
| -rw-r--r-- | runtime/xma_decoder/src/lib.rs | 44 |
3 files changed, 52 insertions, 2 deletions
diff --git a/runtime/xma_decoder/README.md b/runtime/xma_decoder/README.md index 4a6d5fb10..5ef66abbc 100644 --- a/runtime/xma_decoder/README.md +++ b/runtime/xma_decoder/README.md | |||
| @@ -17,3 +17,9 @@ cargo build --manifest-path runtime/xma_decoder/Cargo.toml --locked | |||
| 17 | ``` | 17 | ``` |
| 18 | 18 | ||
| 19 | The C++ smoke test is run by the xma2-decoder workflow with MSVC. It links ws2_32, userenv, and ntdll for Rust's Windows standard library. | 19 | The C++ smoke test is run by the xma2-decoder workflow with MSVC. It links ws2_32, userenv, and ntdll for Rust's Windows standard library. |
| 20 | |||
| 21 | ## Use in the runtime | ||
| 22 | |||
| 23 | `runtime/CMakeLists.txt` builds this crate at configure time when `cargo` is on `PATH` (the first build fetches the pinned `xma` git dependency) and links it into `gta6rt` and `acphal_test` with `LEONIDA_HAVE_XMA2_DECODER` defined. `runtime/shims/acphal.cpp` calls `leonida_xma2_decoder_create_hardware` once per XMA context and decodes whole 2048-byte packets, so loop points are rounded to packets. | ||
| 24 | |||
| 25 | Without `cargo`, when that build fails, or with the cl.exe fallback of `scripts/build_runtime.ps1`, XMA voices output silence. | ||
diff --git a/runtime/xma_decoder/leonida_xma2_decoder.h b/runtime/xma_decoder/leonida_xma2_decoder.h index 98dd5c49c..a4c5b4534 100644 --- a/runtime/xma_decoder/leonida_xma2_decoder.h +++ b/runtime/xma_decoder/leonida_xma2_decoder.h | |||
| @@ -25,6 +25,10 @@ enum LeonidaXma2Status { | |||
| 25 | 25 | ||
| 26 | int32_t leonida_xma2_decoder_create(uint32_t sample_rate, uint32_t channels, | 26 | int32_t leonida_xma2_decoder_create(uint32_t sample_rate, uint32_t channels, |
| 27 | LeonidaXma2Decoder **out_decoder); | 27 | LeonidaXma2Decoder **out_decoder); |
| 28 | /* Like leonida_xma2_decoder_create, but decodes like the console's XMA hardware: the first | ||
| 29 | frame is kept and there is no 64-sample start skip; the caller applies the context's skip. */ | ||
| 30 | int32_t leonida_xma2_decoder_create_hardware(uint32_t sample_rate, uint32_t channels, | ||
| 31 | LeonidaXma2Decoder **out_decoder); | ||
| 28 | int32_t leonida_xma2_decode_buffer(LeonidaXma2Decoder *decoder, | 32 | int32_t leonida_xma2_decode_buffer(LeonidaXma2Decoder *decoder, |
| 29 | const uint8_t *input, size_t input_len, | 33 | const uint8_t *input, size_t input_len, |
| 30 | LeonidaXma2PcmBlock **out_block); | 34 | LeonidaXma2PcmBlock **out_block); |
diff --git a/runtime/xma_decoder/src/lib.rs b/runtime/xma_decoder/src/lib.rs index 1b435a0ce..cda2721fb 100644 --- a/runtime/xma_decoder/src/lib.rs +++ b/runtime/xma_decoder/src/lib.rs | |||
| @@ -59,6 +59,28 @@ pub unsafe extern "C" fn leonida_xma2_decoder_create( | |||
| 59 | channels: u32, | 59 | channels: u32, |
| 60 | out_decoder: *mut *mut LeonidaXma2Decoder, | 60 | out_decoder: *mut *mut LeonidaXma2Decoder, |
| 61 | ) -> i32 { | 61 | ) -> i32 { |
| 62 | unsafe { create_with(sample_rate, channels, out_decoder, Xma2Stream::new) } | ||
| 63 | } | ||
| 64 | |||
| 65 | /// Like `leonida_xma2_decoder_create`, but decodes like the console's XMA hardware: the first | ||
| 66 | /// frame is kept and there is no 64-sample start skip; the caller applies the context's skip. | ||
| 67 | /// # Safety | ||
| 68 | /// Same as `leonida_xma2_decoder_create`. | ||
| 69 | #[no_mangle] | ||
| 70 | pub unsafe extern "C" fn leonida_xma2_decoder_create_hardware( | ||
| 71 | sample_rate: u32, | ||
| 72 | channels: u32, | ||
| 73 | out_decoder: *mut *mut LeonidaXma2Decoder, | ||
| 74 | ) -> i32 { | ||
| 75 | unsafe { create_with(sample_rate, channels, out_decoder, Xma2Stream::new_hardware) } | ||
| 76 | } | ||
| 77 | |||
| 78 | unsafe fn create_with<E>( | ||
| 79 | sample_rate: u32, | ||
| 80 | channels: u32, | ||
| 81 | out_decoder: *mut *mut LeonidaXma2Decoder, | ||
| 82 | make: impl FnOnce(u32, usize) -> Result<Xma2Stream, E>, | ||
| 83 | ) -> i32 { | ||
| 62 | if out_decoder.is_null() { | 84 | if out_decoder.is_null() { |
| 63 | return LEONIDA_XMA2_INVALID_ARGUMENT; | 85 | return LEONIDA_XMA2_INVALID_ARGUMENT; |
| 64 | } | 86 | } |
| @@ -68,8 +90,8 @@ pub unsafe extern "C" fn leonida_xma2_decoder_create( | |||
| 68 | } | 90 | } |
| 69 | 91 | ||
| 70 | match catch_unwind(AssertUnwindSafe(|| { | 92 | match catch_unwind(AssertUnwindSafe(|| { |
| 71 | let stream = Xma2Stream::new(sample_rate, channels as usize) | 93 | let stream = |
| 72 | .map_err(|_| LEONIDA_XMA2_INVALID_FORMAT)?; | 94 | make(sample_rate, channels as usize).map_err(|_| LEONIDA_XMA2_INVALID_FORMAT)?; |
| 73 | try_new_handle(LeonidaXma2Decoder { | 95 | try_new_handle(LeonidaXma2Decoder { |
| 74 | stream: Mutex::new(stream), | 96 | stream: Mutex::new(stream), |
| 75 | channels: channels as usize, | 97 | channels: channels as usize, |
| @@ -233,6 +255,24 @@ mod tests { | |||
| 233 | } | 255 | } |
| 234 | 256 | ||
| 235 | #[test] | 257 | #[test] |
| 258 | fn create_hardware_accepts_stereo_and_rejects_three_channels() { | ||
| 259 | let mut decoder = ptr::null_mut(); | ||
| 260 | assert_eq!( | ||
| 261 | unsafe { leonida_xma2_decoder_create_hardware(48_000, 2, &mut decoder) }, | ||
| 262 | LEONIDA_XMA2_OK | ||
| 263 | ); | ||
| 264 | assert!(!decoder.is_null()); | ||
| 265 | unsafe { leonida_xma2_decoder_destroy(decoder) }; | ||
| 266 | |||
| 267 | let mut decoder = ptr::dangling_mut(); | ||
| 268 | assert_eq!( | ||
| 269 | unsafe { leonida_xma2_decoder_create_hardware(48_000, 3, &mut decoder) }, | ||
| 270 | LEONIDA_XMA2_INVALID_FORMAT | ||
| 271 | ); | ||
| 272 | assert!(decoder.is_null()); | ||
| 273 | } | ||
| 274 | |||
| 275 | #[test] | ||
| 236 | fn pcm_handle_allocation_failure_clears_output_and_allows_retry() { | 276 | fn pcm_handle_allocation_failure_clears_output_and_allows_retry() { |
| 237 | let mut decoder = ptr::null_mut(); | 277 | let mut decoder = ptr::null_mut(); |
| 238 | assert_eq!( | 278 | assert_eq!( |