diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp index f1c33ddd9e..762c566539 100644 --- a/runtime/shims/acphal.cpp +++ b/runtime/shims/acphal.cpp @@ -359,17 +359,84 @@ constexpr uint64_t kAudioFramesPerSecond = 375; // Word 9: output read block index (0..4). // // Packets are decoded by the Rust decoder in runtime/xma_decoder when CMake found cargo (LEONIDA_HAVE_XMA2_DECODER); -// otherwise each packet becomes silence for its frame count. The read offset (word 2) and the loop points (words 3 -// and 4) are used per 2048-byte packet, so the loop-end subframe (word 1 bits 12..13) is not applied. +// otherwise each packet becomes silence for its frame count. The loop points name frame bit positions within +// the input buffers. On a pass that rewinds, the loop-end frame plays through the selected 128-sample subframe. constexpr std::size_t kXmaBlockBytes = 256; constexpr std::size_t kXmaSamplesPerBlock = kXmaBlockBytes / 2; constexpr std::size_t kXmaPacketBytes = 2048; constexpr std::size_t kXmaPacketBits = kXmaPacketBytes * 8; +constexpr std::size_t kXmaHeaderBits = 32; +constexpr std::size_t kXmaFrameLengthBits = 15; constexpr std::size_t kXmaSubframeSamples = 128; constexpr std::size_t kXmaFrameSamples = 512; constexpr uint32_t kXmaErrorInputInvalid = 4; +// XMA2's big-endian packet header has the count of frames that start in this packet, then the bit count of +// the preceding frame's continuation. Each newly started frame has a 15-bit length, including the prefix. +struct XmaPacketFrames { + std::array starts{}; + std::size_t count = 0; + std::size_t complete = 0; + std::size_t leading = 0; + + std::optional index(std::size_t bit) const { + const auto end = starts.begin() + static_cast(count); + const auto found = std::find(starts.begin(), end, bit); + if (found == end) return std::nullopt; + return static_cast(found - starts.begin()); + } +}; + +std::size_t xma_bits(const uint8_t* packet, std::size_t bit, std::size_t count) { + std::size_t value = 0; + for (std::size_t i = 0; i < count; ++i, ++bit) { + value = (value << 1) | ((packet[bit / 8] >> (7 - bit % 8)) & 1u); + } + return value; +} + +std::size_t xma_leading_bits(const uint8_t* packet) { + return ((packet[0] & 3u) << 13) | (packet[1] << 5) | (packet[2] >> 3); +} + +std::optional xma_packet_frames(const uint8_t* packet) { + XmaPacketFrames frames; + frames.count = packet[0] >> 2; + frames.leading = xma_leading_bits(packet); + std::size_t bit = kXmaHeaderBits + frames.leading; + for (std::size_t i = 0; i < frames.count; ++i) { + if (bit >= kXmaPacketBits) return std::nullopt; + frames.starts[i] = bit; + const std::size_t remaining = kXmaPacketBits - bit; + if (remaining < kXmaFrameLengthBits) break; + const std::size_t length = xma_bits(packet, bit, kXmaFrameLengthBits); + if (length <= kXmaFrameLengthBits) return std::nullopt; + if (length > remaining) break; + ++frames.complete; + bit += length; + } + if (frames.count - frames.complete > 1) return std::nullopt; + return frames; +} + +bool xma_finishes_split_frame(const uint8_t* packet, std::size_t start, const uint8_t* next) { + const std::size_t head = kXmaPacketBits - start; + const std::size_t tail = xma_leading_bits(next); + if (tail == 0 || tail > kXmaPacketBits - kXmaHeaderBits) return false; + const std::size_t prefix = std::min(head, kXmaFrameLengthBits); + const std::size_t missing = kXmaFrameLengthBits - prefix; + if (tail < missing) return false; + const std::size_t length = (xma_bits(packet, start, prefix) << missing) | xma_bits(next, kXmaHeaderBits, missing); + return length == head + tail; +} + +void xma_start_at_frame(uint8_t* packet, std::size_t bit, std::size_t remaining_frames) { + const auto header = static_cast((remaining_frames << 26) | ((bit - kXmaHeaderBits) << 11) | + ((packet[2] & 7u) << 8) | packet[3]); + for (std::size_t i = 0; i < 4; ++i) packet[i] = static_cast(header >> (24 - 8 * i)); +} + #if defined(LEONIDA_HAVE_XMA2_DECODER) constexpr uint32_t kXmaRates[4] = {24000, 32000, 44100, 48000}; struct Xma2DecoderFree { @@ -414,6 +481,7 @@ struct XmaVoice { std::size_t pcm_read = 0; std::size_t loop_skip = 0; // samples per channel to drop after a rewind uint32_t format; // SHAPE word 1 bits 27..29, rate and channel layout + bool loop_restart = false; bool ended = false; }; @@ -508,6 +576,7 @@ class XmaEngine { voice->pcm.clear(); voice->pcm_read = 0; voice->loop_skip = 0; + voice->loop_restart = false; voice->format = format; #if defined(LEONIDA_HAVE_XMA2_DECODER) voice->decoder.reset(); @@ -556,7 +625,7 @@ class XmaEngine { // Appends interleaved s16 for one 2048-byte packet. Without the Rust decoder, or when it // fails, the packet becomes silence for its frame count (header bits 26..31) so timing holds. - static void decode_packet(XmaVoice& voice, const uint8_t* bytes) { + static bool decode_packet(XmaVoice& voice, const uint8_t* bytes) { const std::size_t channels = voice.channels(); #if defined(LEONIDA_HAVE_XMA2_DECODER) if (!voice.decoder) { @@ -573,17 +642,46 @@ class XmaEngine { const int16_t* samples = leonida_xma2_pcm_data(block.get()); const std::size_t count = leonida_xma2_pcm_sample_count(block.get()); if (count != 0) voice.pcm.insert(voice.pcm.end(), samples, samples + count); - return; + return true; } voice.decoder.reset(); // Resume with a clean state after replacing a packet with silence. } #endif voice.pcm.resize(voice.pcm.size() + static_cast(bytes[0] >> 2) * kXmaFrameSamples * channels); + return false; + } + + // Keep only PCM through the selected loop-end subframe. The Rust decoder can prepend one frame finished from + // the previous packet. Compare its returned count with the packet's completed new frames to locate this one. + static bool trim_loop_end(XmaVoice& voice, std::size_t packet_start, bool decoded, const XmaPacketFrames& frames, + std::size_t end, std::size_t subframe, const uint8_t* continuation) { + const std::size_t frame_samples = kXmaFrameSamples * voice.channels(); + const std::size_t keep_tail = (subframe + 1) * kXmaSubframeSamples * voice.channels(); + if (!decoded) { + voice.pcm.resize(packet_start + end * frame_samples + keep_tail); + return true; + } + + const std::size_t before_continuation = voice.pcm.size(); + const std::size_t current_samples = before_continuation - packet_start; + const std::size_t complete_samples = frames.complete * frame_samples; + const bool has_previous = frames.leading != 0 && current_samples == complete_samples + frame_samples; + if (!has_previous && current_samples != complete_samples) return false; + if (end == frames.complete) { + if (!continuation) return false; + const auto next = xma_packet_frames(continuation); + if (!next || !decode_packet(voice, continuation) || + voice.pcm.size() - before_continuation != (next->complete + 1) * frame_samples) { + voice.pcm.resize(before_continuation); + return false; + } + } + const std::size_t prefix = has_previous ? frame_samples : 0; + voice.pcm.resize(packet_start + prefix + end * frame_samples + keep_tail); + return true; } // Decodes the packet at the read offset of the current input buffer and queues its PCM. - // Offsets and loop points are used per packet: a loop plays its end packet in full and - // restarts at the start of the packet holding the loop start. static bool decode_one_packet(XmaContextView ctx, XmaVoice& voice) { if (voice.ended) return false; @@ -603,7 +701,8 @@ class XmaEngine { const uint32_t address = (current == 0) ? ctx.get(5) : ctx.get(6); const std::size_t packets = (current == 0) ? ctx.field(0, 0, 12) : ctx.field(1, 0, 12); - const std::size_t packet = ctx.field(2, 0, 26) / kXmaPacketBits; + const std::size_t offset = ctx.field(2, 0, 26); + const std::size_t packet = offset / kXmaPacketBits; const uint8_t* data = apu_span(address & ~0x7FFu, packets * kXmaPacketBytes); if (!data || packet >= packets) { finish_input(ctx, current, packet >= packets ? packet - packets : 0); @@ -612,12 +711,60 @@ class XmaEngine { const uint8_t* packet_data = data + packet * kXmaPacketBytes; const uint8_t packet_skip = packet_data[3]; + // XMA2 packet headers link packets from the same stream; 0xff ends the chain. + std::size_t next = packet_skip == 0xFFu ? packets : packet + 1 + packet_skip; + const uint32_t loops = ctx.field(0, 12, 8); + const std::size_t loop_start = ctx.field(3, 0, 26); + const std::size_t loop_end = ctx.field(4, 0, 26); + const bool loop_packet = loops > 0 && loop_end != 0 && packet == loop_end / kXmaPacketBits; + const std::size_t start_bit = offset % kXmaPacketBits; + auto frames = (loop_packet || voice.loop_restart) ? xma_packet_frames(packet_data) : std::nullopt; + // When resuming at a later frame, present its prefix as continuation data to the freshly reset codec. + // It skips the prefix on its first packet without decoding earlier PCM into the overlap state. + std::array start_packet; + if (frames && voice.loop_restart) { + if (const auto start = frames->index(start_bit); start && *start > 0) { + std::memcpy(start_packet.data(), packet_data, start_packet.size()); + xma_start_at_frame(start_packet.data(), start_bit, frames->count - *start); + packet_data = start_packet.data(); + frames = xma_packet_frames(packet_data); + } + } + const auto end = loop_packet && frames ? frames->index(loop_end % kXmaPacketBits) : std::nullopt; + const uint8_t* continuation = nullptr; + if (end && *end == frames->complete) { + if (next < packets) { + continuation = data + next * kXmaPacketBytes; + } else if (packet_skip != 0xFFu) { + const uint32_t other = 1u - current; + const std::size_t other_packets = ctx.field(other, 0, 12); + const std::size_t follow = next - packets; + if (follow < other_packets) { + // A declared continuation may arrive after the current input. Keep that input for the rewind. + if ((valid & (1u << other)) == 0) return false; + const auto* other_data = apu_span(ctx.get(5 + other) & ~0x7FFu, other_packets * kXmaPacketBytes); + if (other_data) continuation = other_data + follow * kXmaPacketBytes; + } + } + if (continuation && !xma_finishes_split_frame(packet_data, frames->starts[*end], continuation)) { + continuation = nullptr; + } + } // Decoding is only requested when less than a ring block remains. Keep that // tail and reuse the packet storage as the voice consumes subsequent packets. voice.pcm.erase(voice.pcm.begin(), voice.pcm.begin() + static_cast(voice.pcm_read)); voice.pcm_read = 0; + voice.loop_restart = false; const std::size_t packet_start = voice.pcm.size(); - decode_packet(voice, packet_data); + const bool decoded = decode_packet(voice, packet_data); + const std::size_t loop_start_packet = loop_start / kXmaPacketBits; + if (end && (*end < frames->complete || continuation) && loop_start_packet < packets) { + const auto rewind = xma_packet_frames(data + loop_start_packet * kXmaPacketBytes); + if (rewind && rewind->index(loop_start % kXmaPacketBits)) { + voice.loop_restart = trim_loop_end(voice, packet_start, decoded, *frames, *end, + ctx.field(1, 12, 2), continuation); + } + } const std::size_t packet_samples = voice.pcm.size() - packet_start; const std::size_t subframe_samples = kXmaSubframeSamples * channels; const std::size_t initial_skip = ctx.field(1, 24, 3); @@ -635,16 +782,14 @@ class XmaEngine { } voice.loop_skip -= loop_samples / channels; - // XMA2 packet headers link packets from the same stream; 0xff ends the chain. - std::size_t next = packet_skip == 0xFFu ? packets : packet + 1 + packet_skip; - const uint32_t loops = ctx.field(0, 12, 8); - const std::size_t loop_end = ctx.field(4, 0, 26); - if (loops > 0 && loop_end != 0 && packet == loop_end / kXmaPacketBits) { - next = ctx.field(3, 0, 26) / kXmaPacketBits; + std::size_t next_offset = 0; + if (loop_packet) { + next = loop_start_packet; + if (voice.loop_restart) next_offset = loop_start % kXmaPacketBits; voice.loop_skip = static_cast(ctx.field(1, 17, 3)) * kXmaSubframeSamples; #if defined(LEONIDA_HAVE_XMA2_DECODER) - // A frame spilling out of the loop-end packet does not continue in the loop-start packet: - // start a fresh decoder there rather than decode the spliced bits. + // A rewind starts a different packet sequence. Discard the old overlap and any state advanced by + // inspecting a continuation, including when its PCM count required a packet-level fallback. voice.decoder.reset(); #endif if (loops != 255u) ctx.set_field(0, 12, 8, loops - 1u); @@ -652,7 +797,7 @@ class XmaEngine { if (next >= packets) { finish_input(ctx, current, next - packets); } else { - ctx.set_field(2, 0, 26, static_cast(next * kXmaPacketBits)); + ctx.set_field(2, 0, 26, static_cast(next * kXmaPacketBits + next_offset)); } return true; } diff --git a/runtime/test/acphal_test.cpp b/runtime/test/acphal_test.cpp index be2c6f1e1a..75bc6337f2 100644 --- a/runtime/test/acphal_test.cpp +++ b/runtime/test/acphal_test.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -148,6 +149,56 @@ void write_xma_packet(uint8_t* packet, uint32_t frames, uint8_t packet_skip, } } +constexpr uint32_t kTestXmaPacketBits = 2048 * 8; +constexpr uint32_t kTestXmaHeaderBits = 32; + +uint32_t first_xma_frame_length(const uint8_t* packet) { + return (static_cast(packet[4]) << 7) | (packet[5] >> 1); +} + +void copy_xma_bits(uint8_t* dest, uint32_t dest_bit, const uint8_t* source, uint32_t source_bit, uint32_t count) { + for (uint32_t i = 0; i < count; ++i, ++dest_bit, ++source_bit) { + const uint8_t mask = static_cast(0x80u >> (dest_bit % 8)); + const bool bit = (source[source_bit / 8] & (0x80u >> (source_bit % 8))) != 0; + dest[dest_bit / 8] = static_cast((dest[dest_bit / 8] & ~mask) | (bit ? mask : 0u)); + } +} + +void write_xma_header(uint8_t* packet, uint32_t frames, uint32_t leading_bits, uint8_t packet_skip) { + const uint32_t header = (frames << 26) | (leading_bits << 11) | (1u << 8) | packet_skip; + for (uint32_t i = 0; i < 4; ++i) { + packet[i] = static_cast(header >> (24 - 8 * i)); + } +} + +struct SpanningFrame { + uint32_t start; + uint32_t next_frame; +}; + +// Split one compressed frame between two linked packets, then put a complete +// frame after the continuation. Input starts at the split frame in packet 0. +SpanningFrame write_spanning_xma_frame(uint8_t* packet, uint8_t* next_packet, + uint32_t first_piece_bits = 32, bool following_frame = true) { + const uint32_t start = kTestXmaPacketBits - first_piece_bits; + std::array source{}; + write_xma_packet(source.data(), 1, 0); + const uint32_t frame_bits = first_xma_frame_length(source.data()); + const uint32_t continuation_bits = frame_bits - first_piece_bits; + std::memset(packet, 0xFF, source.size()); + std::memset(next_packet, 0xFF, source.size()); + write_xma_header(packet, 1, start - kTestXmaHeaderBits, 1); + write_xma_header(next_packet, following_frame ? 1 : 0, continuation_bits, 0); + copy_xma_bits(packet, start, source.data(), kTestXmaHeaderBits, first_piece_bits); + copy_xma_bits(next_packet, kTestXmaHeaderBits, source.data(), + kTestXmaHeaderBits + first_piece_bits, continuation_bits); + if (following_frame) { + copy_xma_bits(next_packet, kTestXmaHeaderBits + continuation_bits, source.data(), + kTestXmaHeaderBits, frame_bits); + } + return {start, kTestXmaHeaderBits + continuation_bits}; +} + } // namespace int main(int argc, char** argv) { @@ -589,8 +640,7 @@ int main(int argc, char** argv) { check(release_ctx(&xma_ctx) == S_OK, "Release XMA SHAPE contexts joins decoder cleanly"); } - // 8b. Loops are rounded to whole packets: the loop end packet plays in full and the read - // restarts at the packet holding the loop start. One 1-frame packet = 512 samples = 4 blocks. + // 8b. Loop positions address frames and subframes. A mono frame is four 128-sample ring blocks. { TestShapeContexts xma_ctx{}; xma_ctx.numXmaContexts = 4; @@ -609,8 +659,8 @@ int main(int argc, char** argv) { void* out_va = nullptr; uint32_t out_apu = 0; - check(alloc(&out_va, &out_apu, 0x1000, 0x100, 2) == S_OK && out_va != nullptr, - "ApuAlloc 16-block loop output ring"); + check(alloc(&out_va, &out_apu, 0x2000, 0x100, 2) == S_OK && out_va != nullptr, + "ApuAlloc output storage for the packet loop tests"); // Voice index 2. const XmaWords words{static_cast(xma_ctx.xmaContextArray) + 32}; @@ -629,14 +679,14 @@ int main(int argc, char** argv) { auto configure_voice = [&](uint32_t loops, uint32_t input_0_packets, uint32_t ring_size, uint32_t write, uint32_t read) { - std::memset(out_va, 0x55, 0x1000); + std::memset(out_va, 0x55, 0x2000); const uint32_t input_1_packets = 3u - input_0_packets; words[0] = input_0_packets | (loops << 12) | ((input_1_packets != 0 ? 3u : 1u) << 20) | (ring_size << 22) | (write << 27); words[1] = (1u << 31) | (3u << 27) | input_1_packets; words[2] = 32u; // read at packet 0 words[3] = 32u; // loop start: packet 0 - words[4] = 100u; // loop end: packet 0 + words[4] = 32u; // loop end: first frame of packet 0 words[5] = in_apu; words[6] = in_apu + input_0_packets * 2048u; words[7] = out_apu; @@ -644,11 +694,21 @@ int main(int argc, char** argv) { }; const uint32_t voice_index = 2u; + struct LoopBounds { + uint32_t read = kTestXmaHeaderBits; + uint32_t start = kTestXmaHeaderBits; + uint32_t end = kTestXmaHeaderBits; + uint32_t end_subframe = 3; + }; // Returns the number of blocks written before input exhaustion, or -1 on timeout. auto run_blocks = [&](uint32_t loops, uint32_t input_0_packets = 3, - uint32_t initial_skip = 0, uint32_t loop_skip = 0) -> int { - configure_voice(loops, input_0_packets, 16, 0, 0); - words[1].fetch_or((initial_skip << 24) | (loop_skip << 17)); + uint32_t initial_skip = 0, uint32_t loop_skip = 0, + LoopBounds bounds = {}) -> int { + configure_voice(loops, input_0_packets, 31, 0, 0); + words[1].fetch_or((initial_skip << 24) | (loop_skip << 17) | (bounds.end_subframe << 12)); + words[2] = bounds.read; + words[3] = bounds.start; + words[4] = bounds.end; check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK, "ENABLE_XMA voice 2 succeeds"); int wait = 0; @@ -687,6 +747,63 @@ int main(int argc, char** argv) { "Rust produces signal on both sides of the buffer switch"); #endif + for (uint32_t end = 0; end < 4; ++end) { + const int expected = 9 + static_cast(end); + const int blocks = run_blocks(1, 3, 0, 0, {.end_subframe = end}); + std::printf("XMA loop end subframe %u: %d blocks, expected %d\n", end, blocks, expected); + check(blocks == expected, "Only the repeated pass is truncated at the loop end subframe"); + } + + write_xma_packet(packets, 2, 1); + const uint32_t second_frame = kTestXmaHeaderBits + first_xma_frame_length(packets); + const int multi_frame = run_blocks(1, 3, 0, 0, {.start = second_frame, .end = second_frame, .end_subframe = 1}); + std::printf("XMA two frames in one loop packet: %d blocks, expected 14\n", multi_frame); + check(multi_frame == 14, "Loop start and end select the second frame without repeating the first"); + + const auto spanning = write_spanning_xma_frame(packets, packets + 0x1000); + const LoopBounds split_bounds{ + .read = spanning.start, .start = spanning.start, .end = spanning.start, .end_subframe = 1}; + const int across_packet = run_blocks(1, 3, 0, 0, split_bounds); + std::printf("XMA loop end frame spanning linked packets: %d blocks, expected 10\n", across_packet); + check(across_packet == 10, "Finish the spanning frame before rewinding without playing the next frame early"); + const int across_input = run_blocks(1, 1, 0, 0, split_bounds); + std::printf("XMA loop end frame spanning two input buffers: %d blocks, expected 10\n", across_input); + check(across_input == 10, "Keep the loop-end input until its continuation in the other input is decoded"); +#if defined(LEONIDA_HAVE_XMA2_DECODER) + constexpr std::size_t two_blocks = 2 * 256; + check(has_signal(ring_bytes) && std::memcmp(ring_bytes, ring_bytes + two_blocks, two_blocks) == 0, + "Rust PCM after a spanning rewind starts with the same two decoded subframes"); +#endif + + const uint32_t next_frame = 2 * kTestXmaPacketBits + spanning.next_frame; + const int followed_split = run_blocks(1, 3, 0, 0, + {.read = spanning.start, .start = next_frame, .end = next_frame, .end_subframe = 1}); + std::printf("XMA loop end after a pending continuation: %d blocks, expected 10\n", followed_split); + check(followed_split == 10, "Keep the carried frame before the loop-end frame's two subframes"); + + const auto length_split = write_spanning_xma_frame(packets, packets + 0x1000, 9, false); + const int split_prefix = run_blocks(1, 3, 0, 0, + {.read = length_split.start, .start = length_split.start, .end = length_split.start, .end_subframe = 1}); + std::printf("XMA frame length split across packets: %d blocks, expected 6\n", split_prefix); + check(split_prefix == 6, "A continuation-only packet can finish a frame whose length prefix is split"); +#if defined(LEONIDA_HAVE_XMA2_DECODER) + const auto rejected = write_spanning_xma_frame(packets, packets + 0x1000); + // The next frame still has a valid length. Its subframe error bit makes the codec emit only the + // carried frame, so the continuation probe cannot use the returned PCM count for precise looping. + const uint32_t error_bit = rejected.next_frame + 27u; + packets[0x1000 + error_bit / 8] |= static_cast(0x80u >> (error_bit % 8)); + const int reference_blocks = run_blocks(0, 3, 0, 0, {.read = rejected.start}); + std::array reference_pcm{}; + std::memcpy(reference_pcm.data(), ring_bytes, reference_pcm.size()); + const int fallback_blocks = run_blocks(1, 3, 0, 0, + {.read = rejected.start, .start = rejected.start, .end = rejected.start, .end_subframe = 1}); + check(reference_blocks == 4 && fallback_blocks == reference_blocks && has_signal(ring_bytes) && + std::memcmp(ring_bytes, reference_pcm.data(), reference_pcm.size()) == 0, + "A rejected continuation probe does not change fallback playback after the rewind"); +#endif + write_xma_packet(packets, 1, 1); + write_xma_packet(packets + 0x1000, 1, 0); + configure_voice(255, 3, 4, 7, 9); check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK, "Enable infinite loop with out-of-range ring indices"); diff --git a/runtime/xma_decoder/README.md b/runtime/xma_decoder/README.md index f02bdcb0cb..be99a72919 100644 --- a/runtime/xma_decoder/README.md +++ b/runtime/xma_decoder/README.md @@ -22,7 +22,7 @@ The `xma2-decoder` workflow builds and runs the C++ smoke test with MSVC. It lin `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. -`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. +`runtime/shims/acphal.cpp` uses `leonida_xma2_decoder_create_hardware` and decodes 2048-byte packets. The shim uses the frame bit positions in the packet to keep PCM through the selected 128-sample loop-end subframe on a repeating pass. If that frame continues in the next linked packet, the shim decodes the continuation before rewinding, including when the linked packet comes from the other input buffer. The shim starts the next pass at the selected loop-start frame with a fresh decoder. Unmatched frame metadata retains packet-level handling. Decoder errors still start a fresh decoder. Without `cargo`, when that build fails, or with the cl.exe fallback of `scripts/build_runtime.ps1`, XMA voices output silence. Use `-DLEONIDA_REQUIRE_XMA2_DECODER=ON` to require the decoder in a CMake build, as the Windows runtime CI does.