upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/acphal_test.cpp
diff options
context:
space:
mode:
authorandretuta <andreviniciusmerlia@gmail.com>2026-11-24 03:53:37 -0300
committerandretuta <andreviniciusmerlia@gmail.com>2026-11-24 03:53:37 -0300
commitcfcedb58e2802c5cd38afd1cbde877ae9d71c2a5 (patch)
treeaa73abcbd3f2fb1685127392ad8716717831c725 /runtime/test/acphal_test.cpp
parent12536c2ab33072b8f49a8d012c970093c90c0866 (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/test/acphal_test.cpp')
-rw-r--r--runtime/test/acphal_test.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/runtime/test/acphal_test.cpp b/runtime/test/acphal_test.cpp
index 276d5815a..94ff8fa96 100644
--- a/runtime/test/acphal_test.cpp
+++ b/runtime/test/acphal_test.cpp
@@ -464,6 +464,99 @@ int main() {
464 check(release_ctx(&xma_ctx) == S_OK, "Release XMA SHAPE contexts joins decoder cleanly"); 464 check(release_ctx(&xma_ctx) == S_OK, "Release XMA SHAPE contexts joins decoder cleanly");
465 } 465 }
466 466
467 // 8b. Loops are rounded to whole packets: the loop end packet plays in full and the read
468 // restarts at the packet holding the loop start. One 1-frame packet = 512 samples = 4 blocks.
469 {
470 // Two packets with one silent mono frame each.
471 std::vector<uint8_t> stream(2 * 2048, 0xFFu);
472 auto put_bits = [&](size_t at, uint32_t value, size_t width) {
473 for (size_t i = 0; i < width; ++i) {
474 const size_t p = at + i;
475 const uint32_t bit = (value >> (width - 1 - i)) & 1u;
476 stream[p / 8] = static_cast<uint8_t>(
477 (stream[p / 8] & ~(0x80u >> (p % 8))) |
478 (bit << (7 - (p % 8))));
479 }
480 };
481 for (size_t packet = 0; packet < 2; ++packet) {
482 const size_t base = packet * 2048 * 8;
483 put_bits(base, 1u << 26, 32); // 1 frame, first_frame_bit = 0
484 const size_t at = base + 32;
485 put_bits(at, 31, 15); // frame length
486 put_bits(at + 15, 0b10u, 2); // fixed layout, one 512-sample subframe
487 put_bits(at + 17, 0, 14); // gain(8) + trim(1) + subframe_skip/empty(5)
488 }
489
490 TestShapeContexts xma_ctx{};
491 xma_ctx.numXmaContexts = 4;
492 check(alloc_ctx(&xma_ctx) == S_OK && xma_ctx.xmaContextArray != nullptr,
493 "Allocate 4 XMA SHAPE contexts for the packet loop test");
494
495 void* in_va = nullptr;
496 uint32_t in_apu = 0;
497 check(alloc(&in_va, &in_apu, 0x1000, 0x800, 1) == S_OK && in_va != nullptr,
498 "ApuAlloc 2-packet loop input buffer");
499 std::memcpy(in_va, stream.data(), stream.size());
500
501 void* out_va = nullptr;
502 uint32_t out_apu = 0;
503 check(alloc(&out_va, &out_apu, 0x1000, 0x100, 2) == S_OK && out_va != nullptr,
504 "ApuAlloc 16-block loop output ring");
505
506 // Voice index 2.
507 auto* words = static_cast<volatile uint32_t*>(xma_ctx.xmaContextArray) + 32;
508
509 using ConnectFn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t);
510 using SubmitFn = HRESULT(__stdcall*)(void*, uint32_t, uint64_t, uint32_t, const void*, uint32_t);
511 using ReleaseFn = HRESULT(__stdcall*)(void*);
512
513 void* x_acp = nullptr;
514 check(create(&x_acp) == S_OK && x_acp != nullptr, "AcpHalCreate for packet loop test");
515 auto vt = *static_cast<void* const* const*>(x_acp);
516 const auto fn_connect = reinterpret_cast<ConnectFn>(vt[0]);
517 const auto fn_submit = reinterpret_cast<SubmitFn>(vt[2]);
518 const auto fn_release = reinterpret_cast<ReleaseFn>(vt[5]);
519 check(fn_connect(x_acp, 0x1000, 0x1000) == S_OK, "Connect for packet loop test");
520
521 // Decodes the stream once on voice 2 with the given loop count and returns the number of
522 // blocks written when the error flag rose, or -1 on timeout.
523 auto run_blocks = [&](uint32_t loops) -> int {
524 std::memset(out_va, 0x55, 0x1000);
525 words[0] = 2u | (loops << 12) | (1u << 20) | (16u << 22);
526 words[1] = (1u << 31) | (3u << 27); // 48 kHz, mono, writable
527 words[2] = 32u; // read at packet 0
528 words[3] = 32u; // loop start: packet 0
529 words[4] = 100u; // loop end: packet 0
530 words[5] = in_apu;
531 words[7] = out_apu;
532 words[9] = 0u;
533
534 const uint32_t enable_arg = 2u;
535 check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &enable_arg, 0) == S_OK,
536 "ENABLE_XMA voice 2 succeeds");
537 int wait = 0;
538 for (; wait < 1000 && ((words[2] >> 31) & 1u) == 0u; ++wait) {
539 Sleep(1);
540 }
541 const int blocks = ((words[2] >> 31) & 1u) != 0u
542 ? static_cast<int>((words[0] >> 27) & 0x1Fu)
543 : -1;
544 const uint32_t disable_arg = 2u;
545 check(fn_submit(x_acp, 7, 0xf4, 0xffffffffu, &disable_arg, 0) == S_OK,
546 "DISABLE_XMA voice 2 succeeds");
547 return blocks;
548 };
549
550 check(run_blocks(0) == 8, "Two 1-frame packets without a loop write 8 blocks (1024 samples)");
551 check(run_blocks(1) == 12, "One loop of packet 0 writes 12 blocks (packets 0, 0, 1)");
552 check(((words[0] >> 12) & 0xFFu) == 0u, "Loop count is consumed by the single loop");
553
554 check(fn_release(x_acp) == S_OK, "Release packet loop AcpHal succeeds");
555 free_mem(in_va);
556 free_mem(out_va);
557 check(release_ctx(&xma_ctx) == S_OK, "Release packet loop SHAPE contexts");
558 }
559
467 // 9. ENABLE_XMA (starts/wakes the decoder worker) racing bind/release (stops it). 560 // 9. ENABLE_XMA (starts/wakes the decoder worker) racing bind/release (stops it).
468 // Every release must return; a lost stop would hang this block. 561 // Every release must return; a lost stop would hang this block.
469 { 562 {