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/test | |
| 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/test')
| -rw-r--r-- | runtime/test/acphal_test.cpp | 93 | ||||
| -rw-r--r-- | runtime/test/xma2_decoder_test.cpp | 178 |
2 files changed, 93 insertions, 178 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 | { |
diff --git a/runtime/test/xma2_decoder_test.cpp b/runtime/test/xma2_decoder_test.cpp deleted file mode 100644 index b8d64e9bb..000000000 --- a/runtime/test/xma2_decoder_test.cpp +++ /dev/null | |||
| @@ -1,178 +0,0 @@ | |||
| 1 | // Portable unit tests for leonida::xma2 (runtime/src/xma2_decoder.h/.cpp): packet parsing, multi-packet frame walk, | ||
| 2 | // zero-length damaged packet guard, half-IMDCT accuracy, MDCT+IMDCT overlap-add signal reconstruction, and silent | ||
| 3 | // stream decoding. Needs no game data or Windows headers. | ||
| 4 | #include "../src/xma2_decoder.h" | ||
| 5 | |||
| 6 | #include <cmath> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <cstdio> | ||
| 9 | #include <limits> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | constexpr double kPi = 3.14159265358979323846264338327950288; | ||
| 15 | |||
| 16 | int g_checks = 0; | ||
| 17 | int g_failures = 0; | ||
| 18 | |||
| 19 | void check(bool condition, const char* what) { | ||
| 20 | ++g_checks; | ||
| 21 | std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); | ||
| 22 | if (!condition) ++g_failures; | ||
| 23 | } | ||
| 24 | |||
| 25 | void put_bits(std::vector<std::uint8_t>& data, std::size_t pos, std::uint32_t n, std::uint32_t value) { | ||
| 26 | for (std::size_t i = 0; i < n; ++i) { | ||
| 27 | const std::uint8_t bit = static_cast<std::uint8_t>((value >> (n - 1u - i)) & 1u); | ||
| 28 | const std::size_t p = pos + i; | ||
| 29 | data[p / 8u] = static_cast<std::uint8_t>((data[p / 8u] & ~(1u << (7u - (p % 8u)))) | (bit << (7u - (p % 8u)))); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | std::vector<float> imdct_reference(const std::vector<float>& input) { | ||
| 34 | const std::size_t n = input.size(); | ||
| 35 | std::vector<float> out(n, 0.0f); | ||
| 36 | for (std::size_t j = 0; j < n; ++j) { | ||
| 37 | const double t = static_cast<double>(j + n / 2u) + 0.5 + static_cast<double>(n) / 2.0; | ||
| 38 | double sum = 0.0; | ||
| 39 | for (std::size_t k = 0; k < n; ++k) { | ||
| 40 | sum += static_cast<double>(input[k]) * | ||
| 41 | std::cos(kPi / static_cast<double>(n) * t * (static_cast<double>(k) + 0.5)); | ||
| 42 | } | ||
| 43 | out[j] = -static_cast<float>(sum); | ||
| 44 | } | ||
| 45 | return out; | ||
| 46 | } | ||
| 47 | |||
| 48 | } // namespace | ||
| 49 | |||
| 50 | int main() { | ||
| 51 | using namespace leonida::xma2; | ||
| 52 | |||
| 53 | // 1. Out-of-range index and truncated packet header rejection. | ||
| 54 | { | ||
| 55 | const std::uint8_t tiny[4]{}; | ||
| 56 | check(!parse_packet(tiny, 4, std::numeric_limits<std::size_t>::max() / kPacketBytes + 1u).has_value(), | ||
| 57 | "parse_packet rejects overflow packet index"); | ||
| 58 | check(!parse_packet(tiny, 3, 0).has_value(), "parse_packet rejects truncated header"); | ||
| 59 | } | ||
| 60 | |||
| 61 | // 2. Zero-length damaged frame before start terminates search cleanly. | ||
| 62 | { | ||
| 63 | std::vector<std::uint8_t> data(kPacketBytes, 0u); | ||
| 64 | put_bits(data, 0, 32, 2u << 26); | ||
| 65 | const std::size_t start = kHeaderBits + 100u; | ||
| 66 | put_bits(data, start, 15, 200u); | ||
| 67 | const auto frame = read_frame(data.data(), data.size(), start); | ||
| 68 | check(frame.has_value() && frame->bits == 200u && !frame->next.has_value(), | ||
| 69 | "zero-length damaged frame before start ends search safely"); | ||
| 70 | } | ||
| 71 | |||
| 72 | // 3. Multi-packet frame walk (frame crossing boundary into next packet). | ||
| 73 | { | ||
| 74 | std::vector<std::uint8_t> data(2u * kPacketBytes, 0xffu); | ||
| 75 | auto hdr = [](std::uint32_t frames, std::uint32_t first) { return (frames << 26) | (first << 11); }; | ||
| 76 | put_bits(data, 0, 32, hdr(2, 0)); | ||
| 77 | put_bits(data, kPacketBits, 32, hdr(1, 48)); | ||
| 78 | put_bits(data, 32, 15, 100u); | ||
| 79 | put_bits(data, 132, 15, 16300u); | ||
| 80 | const std::size_t c_start = kPacketBits + 32u + 48u; | ||
| 81 | put_bits(data, c_start, 15, 60u); | ||
| 82 | |||
| 83 | const auto a = read_frame(data.data(), data.size(), 32u); | ||
| 84 | const auto b = read_frame(data.data(), data.size(), 132u); | ||
| 85 | const auto c = read_frame(data.data(), data.size(), c_start); | ||
| 86 | check(a && a->bits == 100u && a->next == 132u, "frame A walks to frame B"); | ||
| 87 | check(b && b->bits == 16300u && b->next == c_start && b->data.size() == (16300u + 7u) / 8u, | ||
| 88 | "frame B crosses packet header into packet 1"); | ||
| 89 | check(c && c->bits == 60u && !c->next.has_value(), "frame C terminates at end of buffer"); | ||
| 90 | } | ||
| 91 | |||
| 92 | // 4. Fast half-IMDCT matches direct cosine sum for n = 128, 256, 512. | ||
| 93 | { | ||
| 94 | bool all_close = true; | ||
| 95 | for (const std::size_t n : {std::size_t(128), std::size_t(256), std::size_t(512)}) { | ||
| 96 | std::vector<float> input(n); | ||
| 97 | for (std::size_t i = 0; i < n; ++i) input[i] = static_cast<float>((i * 7919u) % 97u) - 48.0f; | ||
| 98 | std::vector<float> fast(n, 0.0f); | ||
| 99 | imdct_half(input.data(), fast.data(), n, 1.0f); | ||
| 100 | const auto ref = imdct_reference(input); | ||
| 101 | for (std::size_t i = 0; i < n; ++i) { | ||
| 102 | if (std::fabs(fast[i] - ref[i]) >= 1e-2f * (1.0f + std::fabs(ref[i]))) all_close = false; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | check(all_close, "fast imdct_half matches direct cosine sum for 128/256/512"); | ||
| 106 | } | ||
| 107 | |||
| 108 | // 5. MDCT + imdct_half + window_overlap reconstructs a continuous sine wave. | ||
| 109 | { | ||
| 110 | constexpr std::size_t n = 256; | ||
| 111 | std::vector<float> signal(n * 6u); | ||
| 112 | for (std::size_t i = 0; i < signal.size(); ++i) { | ||
| 113 | signal[i] = std::sin(static_cast<float>(i) * 0.05f) * 0.3f; | ||
| 114 | } | ||
| 115 | auto win = [&](std::size_t i) { | ||
| 116 | return std::sin((static_cast<double>(i) + 0.5) * kPi / (2.0 * static_cast<double>(n))); | ||
| 117 | }; | ||
| 118 | std::vector<float> out(n * 7u, 0.0f); | ||
| 119 | for (std::size_t block = 0; block < 5u; ++block) { | ||
| 120 | const std::size_t base = block * n; | ||
| 121 | std::vector<float> coeffs(n, 0.0f); | ||
| 122 | for (std::size_t k = 0; k < n; ++k) { | ||
| 123 | double sum = 0.0; | ||
| 124 | for (std::size_t i = 0; i < 2u * n; ++i) { | ||
| 125 | const double t = static_cast<double>(i) + 0.5 + static_cast<double>(n) / 2.0; | ||
| 126 | sum += -static_cast<double>(signal[base + i]) * win(i) * | ||
| 127 | std::cos(kPi / static_cast<double>(n) * t * (static_cast<double>(k) + 0.5)); | ||
| 128 | } | ||
| 129 | coeffs[k] = static_cast<float>(sum); | ||
| 130 | } | ||
| 131 | const std::size_t at = base + n / 2u; | ||
| 132 | imdct_half(coeffs.data(), out.data() + at, n, 2.0f / static_cast<float>(n)); | ||
| 133 | if (block > 0u) window_overlap(out.data() + (at - n / 2u), sine_window(n).data(), n); | ||
| 134 | } | ||
| 135 | bool exact = true; | ||
| 136 | for (std::size_t i = n; i < 5u * n; ++i) { | ||
| 137 | if (std::fabs(out[i] - signal[i]) >= 1e-3f) exact = false; | ||
| 138 | } | ||
| 139 | check(exact, "windowed MDCT + half-IMDCT overlap-add reconstructs sine wave within 1e-3"); | ||
| 140 | } | ||
| 141 | |||
| 142 | // 6. Decode 2 packets of 3 silent mono frames each + overlap flush. | ||
| 143 | { | ||
| 144 | std::vector<std::uint8_t> data(2u * kPacketBytes, 0xffu); | ||
| 145 | for (std::size_t pkt = 0; pkt < 2u; ++pkt) { | ||
| 146 | const std::size_t base = pkt * kPacketBits; | ||
| 147 | put_bits(data, base, 32, 3u << 26); | ||
| 148 | for (std::size_t f = 0; f < 3u; ++f) { | ||
| 149 | const std::size_t at = base + 32u + f * 31u; | ||
| 150 | put_bits(data, at, 15, 31u); | ||
| 151 | put_bits(data, at + 15u, 2, 0b10u); // fixed layout, 1x 512-sample subframe | ||
| 152 | put_bits(data, at + 17u, 14, 0u); // gain, no trim, empty subframe | ||
| 153 | } | ||
| 154 | } | ||
| 155 | Decoder dec(1, 48000); | ||
| 156 | std::vector<std::int16_t> pcm; | ||
| 157 | std::optional<std::size_t> cursor = 32u; | ||
| 158 | int frames = 0; | ||
| 159 | int errors = 0; | ||
| 160 | while (cursor) { | ||
| 161 | const auto frame = read_frame(data.data(), data.size(), *cursor); | ||
| 162 | if (!frame) break; | ||
| 163 | if (!dec.decode(frame->data.data(), frame->bits, pcm)) ++errors; | ||
| 164 | ++frames; | ||
| 165 | cursor = frame->next; | ||
| 166 | } | ||
| 167 | dec.flush(pcm); | ||
| 168 | bool all_zero = true; | ||
| 169 | for (std::int16_t s : pcm) { | ||
| 170 | if (s != 0) all_zero = false; | ||
| 171 | } | ||
| 172 | check(frames == 6 && errors == 0 && pcm.size() == 6u * kFrameSamples + kFrameSamples / 2u && all_zero, | ||
| 173 | "6 silent mono frames + flush produce 3328 zero PCM samples with 0 errors"); | ||
| 174 | } | ||
| 175 | |||
| 176 | std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures); | ||
| 177 | return g_failures ? 1 : 0; | ||
| 178 | } | ||