upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/xma_decoder/src
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/xma_decoder/src')
-rw-r--r--runtime/xma_decoder/src/lib.rs51
1 files changed, 46 insertions, 5 deletions
diff --git a/runtime/xma_decoder/src/lib.rs b/runtime/xma_decoder/src/lib.rs
index 1b435a0ce..69fedf49e 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,
@@ -87,7 +109,8 @@ pub unsafe extern "C" fn leonida_xma2_decoder_create(
87/// Decodes a buffer containing one or more complete 2048-byte XMA2 packets. 109/// Decodes a buffer containing one or more complete 2048-byte XMA2 packets.
88/// 110///
89/// # Safety 111/// # Safety
90/// `decoder` must be a live handle returned by `leonida_xma2_decoder_create`. 112/// `decoder` must be a live handle returned by `leonida_xma2_decoder_create`
113/// or `leonida_xma2_decoder_create_hardware`.
91/// `input` must reference `input_len` readable bytes and `out_block` must be writable. 114/// `input` must reference `input_len` readable bytes and `out_block` must be writable.
92/// Do not destroy a decoder concurrently with this call. The returned PCM block remains valid 115/// Do not destroy a decoder concurrently with this call. The returned PCM block remains valid
93/// until `leonida_xma2_pcm_destroy` is called. 116/// until `leonida_xma2_pcm_destroy` is called.
@@ -160,8 +183,8 @@ pub unsafe extern "C" fn leonida_xma2_decode_buffer(
160/// Destroys a decoder. Passing null is a no-op. 183/// Destroys a decoder. Passing null is a no-op.
161/// 184///
162/// # Safety 185/// # Safety
163/// A non-null pointer must be a live handle returned by `leonida_xma2_decoder_create`, 186/// A non-null pointer must be a live handle returned by `leonida_xma2_decoder_create`
164/// and no calls may be using it concurrently. 187/// or `leonida_xma2_decoder_create_hardware`. No calls may be using it concurrently.
165#[no_mangle] 188#[no_mangle]
166pub unsafe extern "C" fn leonida_xma2_decoder_destroy(decoder: *mut LeonidaXma2Decoder) { 189pub unsafe extern "C" fn leonida_xma2_decoder_destroy(decoder: *mut LeonidaXma2Decoder) {
167 if !decoder.is_null() { 190 if !decoder.is_null() {
@@ -233,6 +256,24 @@ mod tests {
233 } 256 }
234 257
235 #[test] 258 #[test]
259 fn create_hardware_accepts_stereo_and_rejects_three_channels() {
260 let mut decoder = ptr::null_mut();
261 assert_eq!(
262 unsafe { leonida_xma2_decoder_create_hardware(48_000, 2, &mut decoder) },
263 LEONIDA_XMA2_OK
264 );
265 assert!(!decoder.is_null());
266 unsafe { leonida_xma2_decoder_destroy(decoder) };
267
268 let mut decoder = ptr::dangling_mut();
269 assert_eq!(
270 unsafe { leonida_xma2_decoder_create_hardware(48_000, 3, &mut decoder) },
271 LEONIDA_XMA2_INVALID_FORMAT
272 );
273 assert!(decoder.is_null());
274 }
275
276 #[test]
236 fn pcm_handle_allocation_failure_clears_output_and_allows_retry() { 277 fn pcm_handle_allocation_failure_clears_output_and_allows_retry() {
237 let mut decoder = ptr::null_mut(); 278 let mut decoder = ptr::null_mut();
238 assert_eq!( 279 assert_eq!(