upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/xma_decoder
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/xma_decoder')
-rw-r--r--runtime/xma_decoder/README.md6
-rw-r--r--runtime/xma_decoder/leonida_xma2_decoder.h4
-rw-r--r--runtime/xma_decoder/src/lib.rs44
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
19The 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. 19The 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
25Without `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
26int32_t leonida_xma2_decoder_create(uint32_t sample_rate, uint32_t channels, 26int32_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. */
30int32_t leonida_xma2_decoder_create_hardware(uint32_t sample_rate, uint32_t channels,
31 LeonidaXma2Decoder **out_decoder);
28int32_t leonida_xma2_decode_buffer(LeonidaXma2Decoder *decoder, 32int32_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]
70pub 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
78unsafe 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!(