upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/acphal.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/shims/acphal.cpp')
-rw-r--r--runtime/shims/acphal.cpp155
1 files changed, 82 insertions, 73 deletions
diff --git a/runtime/shims/acphal.cpp b/runtime/shims/acphal.cpp
index 3741a0c83..7f4de8fd8 100644
--- a/runtime/shims/acphal.cpp
+++ b/runtime/shims/acphal.cpp
@@ -41,7 +41,9 @@
41 41
42#include "shims.h" 42#include "shims.h"
43#include "startup_trace.h" 43#include "startup_trace.h"
44#include "../src/xma2_decoder.h" 44#if defined(LEONIDA_HAVE_XMA2_DECODER)
45#include "leonida_xma2_decoder.h"
46#endif
45 47
46namespace { 48namespace {
47 49
@@ -355,11 +357,29 @@ constexpr uint64_t kAudioFramesPerSecond = 375;
355// Word 7: output ring APU address. 357// Word 7: output ring APU address.
356// Word 8: overlap buffer APU address. 358// Word 8: overlap buffer APU address.
357// Word 9: output read block index (0..4). 359// Word 9: output read block index (0..4).
360//
361// Packets are decoded by the Rust decoder in runtime/xma_decoder when CMake found cargo (LEONIDA_HAVE_XMA2_DECODER);
362// otherwise each packet becomes silence for its frame count. The read offset (word 2) and the loop points (words 3
363// and 4) are used per 2048-byte packet, so the loop-end subframe (word 1 bits 12..13) is not applied.
358 364
359constexpr std::size_t kXmaBlockBytes = 256; 365constexpr std::size_t kXmaBlockBytes = 256;
360constexpr std::size_t kXmaSamplesPerBlock = kXmaBlockBytes / 2; 366constexpr std::size_t kXmaSamplesPerBlock = kXmaBlockBytes / 2;
367constexpr std::size_t kXmaPacketBytes = 2048;
368constexpr std::size_t kXmaPacketBits = kXmaPacketBytes * 8;
369constexpr std::size_t kXmaSubframeSamples = 128;
370constexpr std::size_t kXmaFrameSamples = 512;
361constexpr uint32_t kXmaErrorInputInvalid = 4; 371constexpr uint32_t kXmaErrorInputInvalid = 4;
362 372
373#if defined(LEONIDA_HAVE_XMA2_DECODER)
374constexpr uint32_t kXmaRates[4] = {24000, 32000, 44100, 48000};
375struct Xma2DecoderFree {
376 void operator()(LeonidaXma2Decoder* d) const noexcept { leonida_xma2_decoder_destroy(d); }
377};
378struct Xma2PcmFree {
379 void operator()(LeonidaXma2PcmBlock* b) const noexcept { leonida_xma2_pcm_destroy(b); }
380};
381#endif
382
363struct XmaContextView { 383struct XmaContextView {
364 volatile uint32_t* words = nullptr; 384 volatile uint32_t* words = nullptr;
365 385
@@ -381,14 +401,12 @@ struct XmaContextView {
381}; 401};
382 402
383struct XmaVoice { 403struct XmaVoice {
384 std::optional<leonida::xma2::Decoder> decoder; 404#if defined(LEONIDA_HAVE_XMA2_DECODER)
405 std::unique_ptr<LeonidaXma2Decoder, Xma2DecoderFree> decoder;
406#endif
385 std::deque<int16_t> pcm; 407 std::deque<int16_t> pcm;
386 std::size_t skip = 0; // samples per channel to drop (start/loop subframe skip) 408 std::size_t skip = 0; // samples per channel to drop (start/loop subframe skip)
387 bool ended = false; 409 bool ended = false;
388 std::size_t produced = 0;
389 uint32_t frames = 0;
390 uint32_t bad_frames = 0;
391 int16_t peak = 0;
392}; 410};
393 411
394struct XmaVoiceSlot { 412struct XmaVoiceSlot {
@@ -474,8 +492,7 @@ public:
474 ctx.set_field(2, 26, 6, 0); 492 ctx.set_field(2, 26, 6, 0);
475 493
476 XmaVoice voice; 494 XmaVoice voice;
477 voice.skip = static_cast<std::size_t>(ctx.field(1, 24, 3)) * 495 voice.skip = static_cast<std::size_t>(ctx.field(1, 24, 3)) * kXmaSubframeSamples;
478 leonida::xma2::kSubframeSamples;
479 voices_[index]->voice.emplace(std::move(voice)); 496 voices_[index]->voice.emplace(std::move(voice));
480 if (!was_active) { 497 if (!was_active) {
481 active_count_.fetch_add(1, std::memory_order_relaxed); 498 active_count_.fetch_add(1, std::memory_order_relaxed);
@@ -520,7 +537,39 @@ private:
520 return XmaContextView{array_ + static_cast<std::size_t>(index) * 16}; 537 return XmaContextView{array_ + static_cast<std::size_t>(index) * 16};
521 } 538 }
522 539
523 static bool decode_one_frame(XmaContextView ctx, XmaVoice& voice) { 540 // Fills `pcm` (interleaved s16) for one 2048-byte packet. Without the Rust decoder, or when it
541 // fails, the packet becomes silence for its frame count (header bits 26..31) so timing holds.
542 static void decode_packet(XmaContextView ctx, XmaVoice& voice, const uint8_t* bytes,
543 std::vector<int16_t>& pcm) {
544 const std::size_t channels = ctx.channels();
545#if defined(LEONIDA_HAVE_XMA2_DECODER)
546 if (!voice.decoder) {
547 LeonidaXma2Decoder* raw = nullptr;
548 if (leonida_xma2_decoder_create_hardware(kXmaRates[ctx.field(1, 27, 2) & 3u],
549 static_cast<uint32_t>(channels), &raw) == LEONIDA_XMA2_OK) {
550 voice.decoder.reset(raw);
551 }
552 }
553 if (voice.decoder) {
554 LeonidaXma2PcmBlock* raw = nullptr;
555 if (leonida_xma2_decode_buffer(voice.decoder.get(), bytes, kXmaPacketBytes, &raw) == LEONIDA_XMA2_OK) {
556 const std::unique_ptr<LeonidaXma2PcmBlock, Xma2PcmFree> block(raw);
557 const int16_t* samples = leonida_xma2_pcm_data(block.get());
558 const std::size_t count = leonida_xma2_pcm_sample_count(block.get());
559 pcm.assign(samples, samples + count);
560 return;
561 }
562 }
563#else
564 (void)voice;
565#endif
566 pcm.assign(static_cast<std::size_t>(bytes[0] >> 2) * kXmaFrameSamples * channels, 0);
567 }
568
569 // Decodes the packet at the read offset of the current input buffer and queues its PCM.
570 // Offsets and loop points are used per packet: a loop plays its end packet in full and
571 // restarts at the start of the packet holding the loop start.
572 static bool decode_one_packet(XmaContextView ctx, XmaVoice& voice) {
524 if (voice.ended) return false; 573 if (voice.ended) return false;
525 574
526 const std::size_t channels = ctx.channels(); 575 const std::size_t channels = ctx.channels();
@@ -530,12 +579,6 @@ private:
530 if ((valid & (1u << current)) == 0) { 579 if ((valid & (1u << current)) == 0) {
531 const uint32_t other = 1u - current; 580 const uint32_t other = 1u - current;
532 if ((valid & (1u << other)) == 0) { 581 if ((valid & (1u << other)) == 0) {
533 // Both inputs exhausted: flush remaining overlap tail, then signal INPUT_INVALID.
534 if (voice.decoder.has_value()) {
535 std::vector<int16_t> tail;
536 voice.decoder->flush(tail);
537 voice.pcm.insert(voice.pcm.end(), tail.begin(), tail.end());
538 }
539 voice.ended = true; 582 voice.ended = true;
540 ctx.set_field(2, 26, 5, kXmaErrorInputInvalid); 583 ctx.set_field(2, 26, 5, kXmaErrorInputInvalid);
541 ctx.set_field(2, 31, 1, 1); 584 ctx.set_field(2, 31, 1, 1);
@@ -547,69 +590,39 @@ private:
547 } 590 }
548 591
549 const uint32_t address = (current == 0) ? ctx.get(5) : ctx.get(6); 592 const uint32_t address = (current == 0) ? ctx.get(5) : ctx.get(6);
550 const uint32_t packets = (current == 0) ? ctx.field(0, 0, 12) : ctx.field(1, 0, 12); 593 const std::size_t packets = (current == 0) ? ctx.field(0, 0, 12) : ctx.field(1, 0, 12);
551 const std::size_t input_bytes = 594 const std::size_t packet = ctx.field(2, 0, 26) / kXmaPacketBits;
552 static_cast<std::size_t>(packets) * leonida::xma2::kPacketBytes; 595 const uint8_t* data = apu_span(address & ~0x7FFu, packets * kXmaPacketBytes);
553 const uint8_t* data = apu_span(address & ~0x7FFu, input_bytes); 596 if (!data || packet >= packets) {
554 if (!data) {
555 ctx.set_field(0, 20 + current, 1, 0);
556 return true;
557 }
558
559 std::size_t at = static_cast<std::size_t>(ctx.field(2, 0, 26));
560 if (at < leonida::xma2::kHeaderBits) {
561 const auto pkt = leonida::xma2::parse_packet(data, input_bytes, 0);
562 at = leonida::xma2::kHeaderBits + (pkt.has_value() ? pkt->first_frame_bit : 0u);
563 }
564
565 const auto frame = leonida::xma2::read_frame(data, input_bytes, at);
566 if (!frame.has_value()) {
567 ctx.set_field(0, 20 + current, 1, 0); 597 ctx.set_field(0, 20 + current, 1, 0);
598 ctx.set_field(2, 0, 26, 0);
568 return true; 599 return true;
569 } 600 }
570 601
571 if (!voice.decoder.has_value()) {
572 static constexpr uint32_t kRates[4] = {24000, 32000, 44100, 48000};
573 const uint32_t rate_idx = ctx.field(1, 27, 2) & 3u;
574 voice.decoder.emplace(channels, kRates[rate_idx]);
575 }
576
577 std::vector<int16_t> pcm; 602 std::vector<int16_t> pcm;
578 pcm.reserve(leonida::xma2::kFrameSamples * channels); 603 decode_packet(ctx, voice, data + packet * kXmaPacketBytes, pcm);
579 ++voice.frames;
580 if (!voice.decoder->decode(frame->data.data(), frame->bits, pcm)) {
581 ++voice.bad_frames;
582 }
583
584 std::size_t next = frame->next.value_or(0);
585 const uint32_t loops = ctx.field(0, 12, 8);
586 const std::size_t loop_end = static_cast<std::size_t>(ctx.field(4, 0, 26));
587 const bool ends_loop =
588 loop_end != 0 &&
589 (frame->start == loop_end ||
590 (frame->start < loop_end && (!frame->next.has_value() || loop_end < *frame->next)));
591 if (loops > 0 && ends_loop) {
592 const std::size_t keep =
593 (static_cast<std::size_t>(ctx.field(1, 12, 2)) + 1u) *
594 leonida::xma2::kSubframeSamples * channels;
595 if (keep < pcm.size()) pcm.resize(keep);
596 next = static_cast<std::size_t>(ctx.field(3, 0, 26));
597 voice.skip = static_cast<std::size_t>(ctx.field(1, 17, 3)) *
598 leonida::xma2::kSubframeSamples;
599 if (loops != 255u) {
600 ctx.set_field(0, 12, 8, loops - 1u);
601 }
602 }
603
604 const std::size_t skip_samples = std::min(voice.skip * channels, pcm.size()); 604 const std::size_t skip_samples = std::min(voice.skip * channels, pcm.size());
605 voice.skip -= skip_samples / channels; 605 voice.skip -= skip_samples / channels;
606 voice.pcm.insert(voice.pcm.end(), pcm.begin() + static_cast<std::ptrdiff_t>(skip_samples), 606 voice.pcm.insert(voice.pcm.end(), pcm.begin() + static_cast<std::ptrdiff_t>(skip_samples), pcm.end());
607 pcm.end());
608 607
609 if (next == 0) { 608 std::size_t next = packet + 1;
609 const uint32_t loops = ctx.field(0, 12, 8);
610 const std::size_t loop_end = ctx.field(4, 0, 26);
611 if (loops > 0 && loop_end != 0 && packet == loop_end / kXmaPacketBits) {
612 next = ctx.field(3, 0, 26) / kXmaPacketBits;
613 voice.skip = static_cast<std::size_t>(ctx.field(1, 17, 3)) * kXmaSubframeSamples;
614#if defined(LEONIDA_HAVE_XMA2_DECODER)
615 // A frame spilling out of the loop-end packet does not continue in the loop-start packet:
616 // start a fresh decoder there rather than decode the spliced bits.
617 voice.decoder.reset();
618#endif
619 if (loops != 255u) ctx.set_field(0, 12, 8, loops - 1u);
620 }
621 if (next >= packets) {
610 ctx.set_field(0, 20 + current, 1, 0); 622 ctx.set_field(0, 20 + current, 1, 0);
623 next = 0;
611 } 624 }
612 ctx.set_field(2, 0, 26, static_cast<uint32_t>(next)); 625 ctx.set_field(2, 0, 26, static_cast<uint32_t>(next * kXmaPacketBits));
613 return true; 626 return true;
614 } 627 }
615 628
@@ -620,7 +633,7 @@ private:
620 while (ctx.field(1, 31, 1) == 1) { 633 while (ctx.field(1, 31, 1) == 1) {
621 int decode_guard = 0; 634 int decode_guard = 0;
622 while (voice.pcm.size() < kXmaSamplesPerBlock && decode_guard++ < 16) { 635 while (voice.pcm.size() < kXmaSamplesPerBlock && decode_guard++ < 16) {
623 if (!decode_one_frame(ctx, voice)) break; 636 if (!decode_one_packet(ctx, voice)) break;
624 } 637 }
625 if (voice.pcm.empty()) return; 638 if (voice.pcm.empty()) return;
626 639
@@ -636,15 +649,11 @@ private:
636 sample = voice.pcm.front(); 649 sample = voice.pcm.front();
637 voice.pcm.pop_front(); 650 voice.pcm.pop_front();
638 } 651 }
639 const int32_t mag =
640 (sample == INT16_MIN) ? INT16_MAX : std::abs(static_cast<int32_t>(sample));
641 if (mag > voice.peak) voice.peak = static_cast<int16_t>(mag);
642 const uint16_t raw = static_cast<uint16_t>(sample); 652 const uint16_t raw = static_cast<uint16_t>(sample);
643 out[2 * i] = static_cast<uint8_t>(raw & 0xFFu); 653 out[2 * i] = static_cast<uint8_t>(raw & 0xFFu);
644 out[2 * i + 1] = static_cast<uint8_t>((raw >> 8) & 0xFFu); 654 out[2 * i + 1] = static_cast<uint8_t>((raw >> 8) & 0xFFu);
645 } 655 }
646 656
647 voice.produced += kXmaSamplesPerBlock / ctx.channels();
648 const uint32_t next_write = (write + 1u) % size; 657 const uint32_t next_write = (write + 1u) % size;
649 ctx.set_field(0, 27, 5, next_write); 658 ctx.set_field(0, 27, 5, next_write);
650 if (next_write == ctx.field(9, 0, 5)) { 659 if (next_write == ctx.field(9, 0, 5)) {