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.md15
-rw-r--r--runtime/xma_decoder/leonida_xma2_decoder.h4
-rw-r--r--runtime/xma_decoder/src/lib.rs51
-rw-r--r--runtime/xma_decoder/tests/ffi_contract.rs22
4 files changed, 84 insertions, 8 deletions
diff --git a/runtime/xma_decoder/README.md b/runtime/xma_decoder/README.md
index 4a6d5fb10..67341e60a 100644
--- a/runtime/xma_decoder/README.md
+++ b/runtime/xma_decoder/README.md
@@ -6,8 +6,6 @@ Each decoder handle serializes decode calls. The returned PCM block owns its sam
6 6
7The tests build packets in memory; they do not open or contain audio fixtures. The synthetic C++ smoke test verifies the exported ABI and ownership boundary. 7The tests build packets in memory; they do not open or contain audio fixtures. The synthetic C++ smoke test verifies the exported ABI and ownership boundary.
8 8
9This PR adds the decoder boundary only; the Windows XAudio2 engine/source-voice adapter is a follow-up stage.
10
11Run: 9Run:
12 10
13```powershell 11```powershell
@@ -16,4 +14,15 @@ cargo test --manifest-path runtime/xma_decoder/Cargo.toml --locked
16cargo build --manifest-path runtime/xma_decoder/Cargo.toml --locked 14cargo build --manifest-path runtime/xma_decoder/Cargo.toml --locked
17``` 15```
18 16
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. 17The `xma2-decoder` workflow builds and runs the C++ smoke test with MSVC. It links `ws2_32.lib`, `userenv.lib`,
18`ntdll.lib`, and `synchronization.lib` for Rust's Windows standard library. `synchronization.lib` resolves the
19`WaitOnAddress` and `WakeByAddress*` imports in the Rust static library.
20
21## Use in the runtime
22
23`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 `acphal_test` with `LEONIDA_HAVE_XMA2_DECODER` defined. The first build fetches the pinned `xma` git dependency. Set `CARGO_NET_OFFLINE=true` for a configure that uses only the local Cargo cache.
24
25`runtime/shims/acphal.cpp` uses `leonida_xma2_decoder_create_hardware` and decodes whole 2048-byte packets, so loop points are rounded to packets. The shim creates a fresh decoder after a loop rewind or a decoder error.
26
27Without `cargo`, when that build fails, or with the cl.exe fallback of `scripts/build_runtime.ps1`, XMA voices output silence.
28Use `-DLEONIDA_REQUIRE_XMA2_DECODER=ON` to require the decoder in a CMake build, as the Windows runtime CI does.
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..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!(
diff --git a/runtime/xma_decoder/tests/ffi_contract.rs b/runtime/xma_decoder/tests/ffi_contract.rs
index 0eb8e5cf4..6e6185c39 100644
--- a/runtime/xma_decoder/tests/ffi_contract.rs
+++ b/runtime/xma_decoder/tests/ffi_contract.rs
@@ -52,6 +52,28 @@ fn make_decoder(rate: u32, channels: u32) -> *mut LeonidaXma2Decoder {
52} 52}
53 53
54#[test] 54#[test]
55fn hardware_mode_keeps_all_samples_of_the_first_stereo_frame() {
56 let mut packet = synthetic_silent_packet(2);
57 packet[..4].copy_from_slice(&[4, 0, 1, 0]); // One frame, no continuation from a previous packet.
58 let mut decoder = ptr::null_mut();
59 let mut block = ptr::null_mut();
60 unsafe {
61 assert_eq!(
62 leonida_xma2_decoder_create_hardware(48_000, 2, &mut decoder),
63 LEONIDA_XMA2_OK
64 );
65 assert_eq!(
66 leonida_xma2_decode_buffer(decoder, packet.as_ptr(), packet.len(), &mut block),
67 LEONIDA_XMA2_OK
68 );
69 let count = leonida_xma2_pcm_sample_count(block);
70 leonida_xma2_pcm_destroy(block);
71 leonida_xma2_decoder_destroy(decoder);
72 assert_eq!(count, 512 * 2);
73 }
74}
75
76#[test]
55fn creation_rejects_invalid_outputs_and_formats() { 77fn creation_rejects_invalid_outputs_and_formats() {
56 assert_eq!( 78 assert_eq!(
57 unsafe { leonida_xma2_decoder_create(48_000, 2, ptr::null_mut()) }, 79 unsafe { leonida_xma2_decoder_create(48_000, 2, ptr::null_mut()) },