upleb.uk

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

summaryrefslogtreecommitdiff
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
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>
-rw-r--r--runtime/CMakeLists.txt45
-rw-r--r--runtime/shims/acphal.cpp155
-rw-r--r--runtime/src/xma2_decoder.cpp1094
-rw-r--r--runtime/src/xma2_decoder.h116
-rw-r--r--runtime/test/acphal_test.cpp93
-rw-r--r--runtime/test/xma2_decoder_test.cpp178
-rw-r--r--runtime/xma_decoder/README.md6
-rw-r--r--runtime/xma_decoder/leonida_xma2_decoder.h4
-rw-r--r--runtime/xma_decoder/src/lib.rs44
-rw-r--r--scripts/build_runtime.ps14
-rw-r--r--third_party/xma/NOTICE.md2
11 files changed, 266 insertions, 1475 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index e64748c1f..871c48c67 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -106,11 +106,39 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
106 MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" 106 MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
107 ) 107 )
108 108
109 # Rust XMA2 decoder (runtime/xma_decoder), built at configure time when cargo is on PATH.
110 # Without it (or if that build fails) the runtime still builds and XMA voices output silence.
111 find_program(LEONIDA_CARGO cargo)
112 if(LEONIDA_CARGO)
113 set(_xma_target_dir ${CMAKE_BINARY_DIR}/xma_decoder)
114 set(_xma_lib ${_xma_target_dir}/release/leonida_xma2_decoder.lib)
115 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
116 ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/Cargo.toml
117 ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/Cargo.lock
118 ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/src/lib.rs)
119 execute_process(
120 COMMAND ${LEONIDA_CARGO} build --release --locked
121 --manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder/Cargo.toml
122 --target-dir ${_xma_target_dir}
123 RESULT_VARIABLE _xma_result)
124 if(_xma_result EQUAL 0 AND EXISTS ${_xma_lib})
125 add_library(leonida_xma2_decoder STATIC IMPORTED)
126 set_target_properties(leonida_xma2_decoder PROPERTIES
127 IMPORTED_LOCATION ${_xma_lib}
128 INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/xma_decoder
129 INTERFACE_COMPILE_DEFINITIONS LEONIDA_HAVE_XMA2_DECODER=1
130 INTERFACE_LINK_LIBRARIES "ws2_32;userenv;ntdll")
131 else()
132 message(WARNING "cargo build of runtime/xma_decoder failed: XMA voices will be silent")
133 endif()
134 else()
135 message(STATUS "cargo not found: runtime/xma_decoder not built, XMA voices will be silent")
136 endif()
137
109 add_executable(gta6rt 138 add_executable(gta6rt
110 src/main.cpp 139 src/main.cpp
111 src/host_cpu.cpp 140 src/host_cpu.cpp
112 src/pe_loader.cpp 141 src/pe_loader.cpp
113 src/xma2_decoder.cpp
114 shims/registry.cpp 142 shims/registry.cpp
115 shims/file_system.cpp 143 shims/file_system.cpp
116 shims/winhttp_offline.cpp 144 shims/winhttp_offline.cpp
@@ -244,14 +272,18 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
244 add_executable(acphal_test 272 add_executable(acphal_test
245 test/acphal_test.cpp 273 test/acphal_test.cpp
246 shims/acphal.cpp 274 shims/acphal.cpp
247 src/xma2_decoder.cpp
248 shims/registry.cpp 275 shims/registry.cpp
249 ) 276 )
250 target_include_directories(acphal_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims ${CMAKE_CURRENT_SOURCE_DIR}/src) 277 target_include_directories(acphal_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims)
251 target_compile_definitions(acphal_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) 278 target_compile_definitions(acphal_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
252 target_compile_options(acphal_test PRIVATE /W4 /permissive- /utf-8) 279 target_compile_options(acphal_test PRIVATE /W4 /permissive- /utf-8)
253 target_link_options(acphal_test PRIVATE /OPT:NOREF) 280 target_link_options(acphal_test PRIVATE /OPT:NOREF)
254 281
282 if(TARGET leonida_xma2_decoder)
283 target_link_libraries(gta6rt PRIVATE leonida_xma2_decoder)
284 target_link_libraries(acphal_test PRIVATE leonida_xma2_decoder)
285 endif()
286
255 # The Zen 2 instructions a desktop CPU may lack (src/cpu_emulation.h): decoded, emulated on a CONTEXT, and run for 287 # The Zen 2 instructions a desktop CPU may lack (src/cpu_emulation.h): decoded, emulated on a CONTEXT, and run for
256 # real with the #UD handler installed, so an Intel host takes the emulated path and an AMD one the native path. 288 # real with the #UD handler installed, so an Intel host takes the emulated path and an AMD one the native path.
257 add_executable(cpu_emulation_test test/cpu_emulation_test.cpp) 289 add_executable(cpu_emulation_test test/cpu_emulation_test.cpp)
@@ -555,13 +587,6 @@ if(BUILD_TESTING)
555 add_test(NAME vulkan_device_filter COMMAND vulkan_device_filter_test) 587 add_test(NAME vulkan_device_filter COMMAND vulkan_device_filter_test)
556 add_executable(vk_presenter_capture_colour_test test/vk_presenter_capture_colour_test.cpp) 588 add_executable(vk_presenter_capture_colour_test test/vk_presenter_capture_colour_test.cpp)
557 add_test(NAME vk_presenter_capture_colour COMMAND vk_presenter_capture_colour_test) 589 add_test(NAME vk_presenter_capture_colour COMMAND vk_presenter_capture_colour_test)
558 # Portable XMA2 packet parser and WMA Pro frame decoder (src/xma2_decoder.cpp): pure stdlib, no GPU.
559 add_executable(xma2_decoder_test test/xma2_decoder_test.cpp src/xma2_decoder.cpp)
560 target_include_directories(xma2_decoder_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
561 if(MSVC)
562 target_compile_options(xma2_decoder_test PRIVATE /W4 /permissive- /utf-8)
563 endif()
564 add_test(NAME xma2_decoder COMMAND xma2_decoder_test)
565 if(TARGET gta6rt) 590 if(TARGET gta6rt)
566 if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") 591 if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
567 add_executable(cpp_unwind_test test/cpp_unwind_test.cpp) 592 add_executable(cpp_unwind_test test/cpp_unwind_test.cpp)
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)) {
diff --git a/runtime/src/xma2_decoder.cpp b/runtime/src/xma2_decoder.cpp
deleted file mode 100644
index a257c873a..000000000
--- a/runtime/src/xma2_decoder.cpp
+++ /dev/null
@@ -1,1094 +0,0 @@
1// Portable XMA2 packet parser and WMA Pro frame decoder.
2//
3// Canonical Huffman / run-level tables and decoding structure follow FFmpeg's libavcodec/wmaprodec.c and
4// libavcodec/wmaprodata.h (LGPL-2.1-or-later, used under GPL-3.0-or-later), specialized for Xbox XMA2 streams
5// (1–2 channels, 512-sample frames, 128/256/512 subframes, 2048-byte packets).
6#include "xma2_decoder.h"
7
8#include <algorithm>
9#include <bit>
10#include <cmath>
11#include <limits>
12#include <utility>
13
14namespace leonida::xma2 {
15namespace {
16
17constexpr std::uint32_t kLengthBits = 15;
18constexpr double kPi = 3.14159265358979323846264338327950288;
19
20struct SymLen {
21 std::uint16_t sym;
22 std::uint8_t len;
23};
24
25// Critical band cutoff frequencies (Hz) and canonical VLC tables from FFmpeg libavcodec/wmaprodata.h.
26constexpr std::uint16_t kCriticalFreq[28] = {
27 100, 200, 300, 400, 510, 630, 770, 920, 1080, 1270, 1480, 1720, 2000, 2320,
28 2700, 3150, 3700, 4400, 5300, 6400, 7700, 9500, 12000, 15500, 20675, 28575, 41375, 63875,
29};
30
31constexpr SymLen kScaleTable[121] = {
32 {58, 5}, {64, 6}, {66, 7}, {65, 7}, {62, 5}, {63, 6}, {68, 9}, {69, 10}, {54, 15}, {19, 19}, {20, 19},
33 {21, 19}, {22, 19}, {23, 19}, {24, 19}, {25, 19}, {26, 19}, {27, 19}, {28, 19}, {29, 19}, {30, 19}, {31, 19},
34 {32, 19}, {33, 19}, {34, 19}, {17, 19}, {36, 19}, {37, 19}, {38, 19}, {39, 19}, {40, 19}, {41, 19}, {42, 19},
35 {43, 19}, {44, 19}, {45, 19}, {46, 19}, {47, 19}, {48, 19}, {49, 19}, {50, 19}, {51, 19}, {52, 19}, {15, 19},
36 {16, 19}, {14, 19}, {13, 19}, {12, 19}, {11, 19}, {10, 19}, {0, 19}, {9, 19}, {8, 19}, {7, 19}, {6, 19},
37 {5, 19}, {4, 19}, {55, 13}, {70, 13}, {3, 19}, {2, 19}, {1, 19}, {35, 19}, {71, 19}, {72, 19}, {73, 19},
38 {74, 19}, {75, 19}, {76, 19}, {77, 19}, {78, 19}, {79, 19}, {80, 19}, {81, 19}, {82, 19}, {83, 19}, {84, 19},
39 {85, 19}, {86, 19}, {87, 19}, {88, 19}, {89, 19}, {90, 19}, {91, 19}, {92, 19}, {93, 19}, {94, 19}, {95, 19},
40 {96, 19}, {97, 19}, {98, 19}, {99, 19}, {100, 19},{101, 19},{102, 19},{103, 19},{104, 19},{105, 19},{106, 19},
41 {107, 19},{108, 19},{109, 19},{110, 19},{111, 19},{112, 19},{113, 19},{114, 19},{115, 19},{116, 19},{117, 19},
42 {118, 19},{119, 19},{120, 19},{18, 18}, {53, 16}, {56, 11}, {57, 8}, {67, 7}, {61, 3}, {59, 2}, {60, 1},
43};
44
45constexpr SymLen kScaleRlTable[120] = {
46 {103, 7}, {80, 11}, {60, 11}, {18, 10}, {56, 10}, {21, 12}, {90, 12}, {58, 11}, {27, 11}, {69, 12}, {84, 15},
47 {48, 15}, {86, 14}, {47, 13}, {19, 10}, {32, 9}, {78, 6}, {5, 5}, {28, 4}, {53, 5}, {9, 7}, {31, 8},
48 {38, 8}, {10, 7}, {88, 11}, {25, 12}, {105, 12},{118, 11},{23, 12}, {82, 14}, {98, 16}, {110, 16},{108, 15},
49 {93, 13}, {68, 10}, {72, 12}, {97, 12}, {81, 12}, {42, 12}, {64, 8}, {4, 4}, {1, 2}, {7, 6}, {14, 7},
50 {0, 9}, {55, 9}, {61, 9}, {117, 10},{24, 12}, {44, 12}, {67, 12}, {70, 16}, {99, 18}, {96, 21}, {95, 21},
51 {2, 21}, {77, 21}, {52, 21}, {111, 21},{102, 20},{101, 17},{46, 15}, {73, 15}, {109, 15},{51, 14}, {92, 14},
52 {30, 7}, {11, 7}, {66, 7}, {15, 8}, {16, 8}, {116, 9}, {65, 9}, {57, 10}, {59, 10}, {115, 9}, {12, 7},
53 {35, 9}, {17, 9}, {41, 9}, {20, 11}, {91, 11}, {26, 12}, {75, 15}, {45, 15}, {107, 14},{83, 14}, {100, 15},
54 {89, 15}, {43, 11}, {62, 9}, {37, 9}, {104, 8}, {6, 5}, {39, 8}, {40, 9}, {34, 9}, {79, 7}, {8, 6},
55 {63, 6}, {87, 12}, {94, 14}, {49, 14}, {50, 13}, {22, 11}, {119, 10},{33, 9}, {36, 9}, {113, 11},{106, 12},
56 {112, 13},{71, 15}, {85, 15}, {74, 14}, {76, 10}, {114, 7}, {29, 5}, {54, 6}, {13, 6}, {3, 2},
57};
58
59constexpr std::uint8_t kScaleRlRun[120] = {
60 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
61 21, 22, 23, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
62 20, 21, 22, 23, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
63 19, 20, 21, 22, 23, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
64 18, 19, 20, 21, 22, 23, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 0, 1, 0, 1,
65};
66
67constexpr std::uint8_t kScaleRlLevel[120] = {
68 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
69 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
70 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
71 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 8, 8, 9, 9,
72};
73
74constexpr std::uint8_t kCoef0Lens[272] = {
75 2, 9, 14, 14, 13, 12, 13, 14, 15, 15, 12, 10, 10, 10, 13, 14, 15, 15, 12, 11, 13, 14, 14, 13,
76 15, 15, 14, 12, 12, 8, 10, 10, 15, 15, 14, 13, 14, 14, 13, 15, 20, 20, 19, 21, 21, 20, 19, 17,
77 17, 18, 18, 15, 15, 13, 12, 14, 15, 15, 14, 15, 15, 12, 11, 6, 7, 8, 9, 13, 13, 13, 14, 14,
78 11, 10, 7, 8, 14, 14, 14, 14, 12, 13, 13, 12, 12, 12, 11, 9, 13, 14, 14, 12, 11, 11, 11, 9,
79 8, 7, 14, 15, 15, 14, 14, 12, 13, 15, 16, 17, 17, 14, 12, 12, 12, 15, 15, 14, 14, 14, 13, 13,
80 9, 9, 11, 11, 10, 7, 6, 13, 15, 15, 14, 14, 14, 13, 14, 15, 15, 13, 14, 14, 14, 14, 10, 9,
81 10, 10, 11, 11, 10, 8, 9, 13, 14, 14, 12, 11, 14, 15, 15, 13, 12, 14, 14, 14, 14, 13, 14, 14,
82 3, 5, 8, 10, 10, 15, 15, 14, 14, 16, 16, 15, 12, 11, 11, 11, 7, 8, 8, 9, 12, 13, 13, 12,
83 14, 15, 15, 13, 10, 11, 11, 13, 14, 14, 13, 14, 14, 11, 10, 13, 15, 15, 14, 12, 11, 4, 6, 6,
84 8, 12, 12, 12, 13, 13, 12, 13, 13, 14, 14, 13, 13, 13, 9, 7, 9, 11, 14, 14, 13, 14, 14, 13,
85 10, 8, 7, 5, 9, 12, 13, 14, 15, 15, 12, 12, 10, 14, 14, 13, 12, 13, 14, 14, 12, 13, 13, 12,
86 12, 12, 9, 7, 6, 3, 4, 4,
87};
88
89constexpr std::uint16_t kCoef0Syms[272] = {
90 2, 25, 111, 94, 69, 58, 87, 93, 136, 135, 59, 37, 34, 36, 82, 182, 120, 138, 195, 45, 168, 216, 178,
91 86, 140, 219, 186, 162, 239, 18, 156, 35, 127, 236, 109, 85, 180, 253, 88, 147, 268, 264, 256, 266, 270, 262,
92 260, 248, 246, 252, 258, 137, 189, 230, 64, 179, 146, 208, 101, 118, 238, 163, 46, 9, 153, 0, 26, 247, 169,
93 76, 202, 131, 194, 38, 13, 19, 132, 106, 191, 97, 65, 198, 77, 62, 66, 164, 48, 27, 81, 183, 102, 60,
94 47, 49, 159, 227, 20, 14, 112, 263, 144, 217, 104, 63, 79, 209, 269, 250, 254, 203, 241, 196, 61, 220, 148,
95 124, 185, 100, 80, 78, 193, 28, 50, 235, 41, 1, 10, 171, 226, 150, 103, 114, 115, 170, 105, 211, 149, 249,
96 108, 188, 107, 255, 231, 155, 42, 40, 55, 160, 39, 21, 29, 215, 234, 184, 228, 51, 116, 142, 145, 172, 165,
97 181, 130, 113, 117, 89, 128, 204, 3, 7, 154, 157, 43, 141, 265, 133, 225, 271, 244, 221, 74, 54, 56, 52,
98 15, 222, 22, 30, 83, 199, 173, 73, 123, 210, 143, 175, 44, 53, 237, 174, 139, 134, 110, 218, 129, 161, 213,
99 177, 267, 151, 125, 67, 223, 5, 11, 192, 23, 214, 243, 166, 200, 176, 68, 224, 187, 257, 261, 232, 96, 251,
100 31, 16, 32, 57, 207, 121, 91, 126, 119, 99, 158, 24, 212, 8, 33, 70, 92, 205, 240, 242, 75, 197, 233,
101 259, 190, 98, 71, 201, 122, 206, 72, 90, 95, 84, 167, 245, 229, 17, 12, 4, 152, 6,
102};
103
104constexpr SymLen kCoef1Table[244] = {
105 {2, 2}, {3, 3}, {102, 3}, {4, 4}, {148, 6}, {134, 9}, {171, 10},{18, 10}, {11, 8}, {159, 8}, {14, 9},
106 {156, 14},{235, 15},{61, 15}, {38, 13}, {153, 13},{48, 14}, {49, 14}, {23, 11}, {203, 13},{208, 19},{204, 19},
107 {129, 18},{94, 17}, {87, 16}, {62, 15}, {174, 15},{147, 15},{29, 12}, {191, 12},{64, 15}, {65, 15}, {146, 14},
108 {164, 13},{142, 5}, {132, 4}, {103, 5}, {154, 7}, {165, 9}, {181, 11},{109, 12},{30, 12}, {86, 16}, {92, 16},
109 {239, 15},{138, 14},{39, 13}, {50, 14}, {115, 15},{238, 21},{228, 21},{236, 21},{222, 21},{216, 20},{226, 20},
110 {196, 18},{192, 17},{120, 16},{221, 14},{51, 14}, {24, 11}, {143, 8}, {7, 6}, {9, 7}, {152, 10},{136, 12},
111 {160, 12},{241, 15},{66, 15}, {168, 14},{219, 14},{113, 14},{193, 12},{19, 10}, {173, 10},{105, 8}, {149, 9},
112 {15, 9}, {205, 13},{207, 13},{125, 17},{190, 17},{182, 16},{68, 15}, {70, 15}, {67, 15}, {137, 13},{31, 12},
113 {223, 14},{116, 15},{210, 19},{220, 19},{198, 18},{126, 17},{88, 16}, {41, 13}, {25, 11}, {40, 13}, {73, 15},
114 {243, 15},{53, 14}, {195, 12},{183, 11},{225, 14},{52, 14}, {71, 15}, {121, 16},{89, 16}, {170, 14},{55, 14},
115 {69, 15}, {83, 15}, {209, 13},{108, 11},{32, 12}, {54, 14}, {122, 16},{184, 16},{176, 15},{42, 13}, {12, 8},
116 {161, 8}, {6, 5}, {167, 9}, {106, 9}, {20, 10}, {145, 12},{111, 13},{43, 13}, {26, 11}, {175, 10},{107, 10},
117 {34, 12}, {33, 12}, {197, 12},{74, 15}, {128, 17},{232, 20},{212, 20},{224, 19},{202, 18},{90, 16}, {57, 14},
118 {227, 14},{97, 16}, {93, 16}, {140, 15},{185, 11},{27, 11}, {16, 9}, {158, 11},{211, 13},{56, 14}, {117, 15},
119 {72, 15}, {166, 13},{91, 16}, {95, 16}, {80, 15}, {101, 16},{194, 17},{127, 17},{82, 15}, {21, 10}, {144, 10},
120 {177, 10},{151, 6}, {10, 7}, {157, 7}, {8, 6}, {5, 4}, {13, 8}, {0, 9}, {213, 13},{46, 13}, {199, 12},
121 {35, 12}, {162, 12},{135, 10},{169, 9}, {45, 13}, {59, 14}, {114, 14},{44, 13}, {188, 16},{186, 16},{75, 15},
122 {79, 15}, {118, 15},{187, 11},{112, 13},{139, 14},{178, 15},{81, 15}, {110, 12},{28, 11}, {163, 8}, {133, 6},
123 {104, 6}, {17, 9}, {22, 10}, {229, 14},{172, 14},{217, 13},{201, 12},{36, 12}, {218, 20},{242, 22},{240, 22},
124 {234, 21},{230, 19},{206, 18},{200, 18},{214, 18},{130, 17},{131, 17},{141, 15},{84, 15}, {76, 15}, {215, 13},
125 {58, 14}, {231, 14},{233, 14},{180, 15},{77, 15}, {37, 12}, {189, 11},{179, 10},{155, 10},{47, 13}, {96, 16},
126 {99, 16}, {119, 15},{63, 14}, {237, 14},{78, 15}, {85, 15}, {60, 14}, {98, 16}, {100, 16},{124, 16},{123, 16},
127 {150, 11},{1, 7},
128};
129
130constexpr std::uint16_t kCoef0Run[272] = {
131 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
132 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
133 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
134 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
135 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
136 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
137 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 0, 1, 2, 3, 4, 5, 6, 7, 8,
138 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
139 32, 33, 34, 35, 36, 37, 38, 39, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
140 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 0, 1, 0,
141 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
142 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
143};
144
145constexpr float kCoef0Level[272] = {
146 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
147 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
148 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
149 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
150 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
151 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
152 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
153 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
154 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
155 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
156 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
157 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
158 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f,
159 3.0f, 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f, 5.0f, 5.0f,
160 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 7.0f, 7.0f, 8.0f, 8.0f, 9.0f, 9.0f, 10.0f, 10.0f, 11.0f, 11.0f, 12.0f,
161 12.0f, 13.0f, 13.0f, 14.0f, 14.0f, 15.0f, 15.0f, 16.0f, 16.0f, 17.0f, 17.0f, 18.0f, 18.0f, 19.0f, 19.0f, 20.0f,
162 20.0f, 21.0f, 21.0f, 22.0f, 22.0f, 23.0f, 23.0f, 24.0f, 24.0f, 25.0f, 25.0f, 26.0f, 26.0f, 27.0f, 27.0f, 28.0f,
163};
164
165constexpr std::uint16_t kCoef1Run[244] = {
166 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
167 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
168 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
169 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
170 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 0, 1, 2, 3, 4, 5, 6, 7,
171 8, 9, 0, 1, 2, 3, 4, 5, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
172 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
173 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
174 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0,
175};
176
177constexpr float kCoef1Level[244] = {
178 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
179 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
180 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
181 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
182 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
183 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
184 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
185 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
186 2.0f, 2.0f, 2.0f, 2.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 4.0f, 4.0f,
187 4.0f, 4.0f, 4.0f, 4.0f, 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f, 7.0f, 7.0f, 7.0f, 8.0f, 8.0f, 9.0f,
188 9.0f, 10.0f, 10.0f, 11.0f, 11.0f, 12.0f, 12.0f, 13.0f, 13.0f, 14.0f, 14.0f, 15.0f, 15.0f, 16.0f, 16.0f, 17.0f,
189 17.0f, 18.0f, 18.0f, 19.0f, 19.0f, 20.0f, 20.0f, 21.0f, 21.0f, 22.0f, 22.0f, 23.0f, 23.0f, 24.0f, 24.0f, 25.0f,
190 25.0f, 26.0f, 26.0f, 27.0f, 27.0f, 28.0f, 28.0f, 29.0f, 29.0f, 30.0f, 30.0f, 31.0f, 31.0f, 32.0f, 32.0f, 33.0f,
191 33.0f, 34.0f, 34.0f, 35.0f, 35.0f, 36.0f, 36.0f, 37.0f, 37.0f, 38.0f, 38.0f, 39.0f, 39.0f, 40.0f, 40.0f, 41.0f,
192 41.0f, 42.0f, 42.0f, 43.0f, 43.0f, 44.0f, 44.0f, 45.0f, 45.0f, 46.0f, 46.0f, 47.0f, 47.0f, 48.0f, 48.0f, 49.0f,
193 49.0f, 50.0f, 51.0f, 52.0f,
194};
195
196constexpr std::uint8_t kVec4Lens[127] = {
197 1, 6, 8, 10, 10, 10, 10, 8, 8, 10, 10, 9, 8, 8, 9, 12, 12, 11, 12, 12, 11, 9, 9, 8, 8, 9,
198 9, 8, 8, 9, 9, 12, 12, 12, 14, 14, 13, 11, 11, 9, 8, 9, 9, 11, 11, 10, 9, 8, 6, 6, 6, 6,
199 6, 6, 11, 11, 10, 11, 11, 10, 10, 11, 11, 9, 7, 6, 7, 7, 6, 6, 6, 5, 7, 11, 11, 10, 9, 8,
200 6, 9, 9, 10, 10, 9, 8, 8, 6, 6, 6, 8, 8, 9, 12, 12, 11, 10, 8, 8, 8, 10, 10, 10, 10, 9,
201 9, 8, 10, 11, 11, 9, 8, 8, 8, 9, 9, 10, 11, 12, 12, 9, 9, 9, 8, 8, 7, 7, 7,
202};
203
204constexpr std::uint16_t kVec4Syms[127] = {
205 0, 4370, 275, 8195, 4146, 12545, 8225, 290, 4625, 515, 20, 8706, 8210, 4355, 4131, 16385,
206 5121, 8961, 321, 1041, 51, 4641, 546, 4610, 530, 513, 8451, 4385, 4130, 33, 8211, 5,
207 66, 4161, 1281, 81, 6, 801, 8196, 8481, 8449, 4611, 531, 561, 769, 12290, 8226, 19,
208 4097, 2, 4369, 274, 4354, 4114, 12291, 16641, 12305, 49, 12321, 260, 4100, 516, 21, 12546,
209 8466, 4353, 4371, 4626, 257, 18, 17, 1, 4386, 8241, 771, 4865, 8705, 8194, 4098, 12561,
210 276, 50, 785, 4116, 8209, 4099, 273, 4113, 258, 259, 4609, 35, 1026, 1025, 16401, 305,
211 34, 529, 289, 770, 12289, 4, 4145, 4356, 12306, 8193, 12801, 261, 16386, 4881, 3, 514,
212 4129, 545, 306, 36, 4101, 65, 20481, 786, 4401, 4866, 8721, 291, 8450, 8465, 4115,
213};
214
215constexpr SymLen kVec2Table[137] = {
216 {19, 5}, {165, 10},{211, 11},{46, 11}, {75, 10}, {177, 11},{12, 11}, {86, 8}, {83, 7}, {38, 7}, {133, 9},
217 {178, 10},{28, 10}, {104, 9}, {73, 9}, {35, 5}, {52, 6}, {113, 9}, {8, 9}, {101, 8}, {69, 7}, {0, 3},
218 {71, 8}, {119, 9}, {91, 10}, {179, 10},{114, 8}, {166, 10},{10, 10}, {44, 10}, {145, 10},{66, 6}, {21, 6},
219 {24, 8}, {146, 9}, {26, 9}, {65, 7}, {5, 7}, {226, 11},{225, 12},{15, 12}, {180, 10},{147, 9}, {115, 8},
220 {40, 8}, {89, 9}, {134, 9}, {84, 7}, {54, 7}, {42, 9}, {60, 10}, {31, 11}, {193, 11},{181, 10},{76, 10},
221 {148, 9}, {37, 6}, {67, 6}, {33, 6}, {3, 6}, {17, 6}, {2, 6}, {102, 8}, {87, 8}, {116, 8}, {56, 8},
222 {50, 5}, {20, 5}, {120, 9}, {58, 9}, {29, 10}, {194, 10},{135, 9}, {97, 8}, {7, 8}, {105, 9}, {13, 11},
223 {241, 12},{16, 12}, {45, 10}, {149, 9}, {74, 9}, {98, 7}, {23, 7}, {85, 7}, {70, 7}, {195, 10},{161, 10},
224 {129, 9}, {72, 8}, {51, 5}, {36, 5}, {117, 8}, {61, 10}, {11, 10}, {162, 9}, {1, 7}, {4, 6}, {49, 6},
225 {68, 6}, {9, 9}, {27, 9}, {130, 8}, {39, 7}, {53, 6}, {99, 7}, {25, 8}, {150, 9}, {90, 9}, {103, 8},
226 {163, 9}, {196, 10},{210, 10},{136, 9}, {121, 9}, {41, 8}, {131, 8}, {43, 9}, {164, 9}, {118, 8}, {88, 8},
227 {81, 7}, {6, 7}, {55, 7}, {59, 9}, {30, 10}, {209, 11},{14, 11}, {151, 9}, {106, 9}, {82, 6}, {22, 6},
228 {100, 7}, {132, 8}, {57, 8}, {18, 4}, {34, 4},
229};
230
231constexpr SymLen kVec1Table[101] = {
232 {7, 5}, {32, 8}, {59, 10}, {60, 10}, {83, 11}, {82, 11}, {62, 10}, {33, 8}, {45, 9}, {61, 10}, {84, 11},
233 {85, 11}, {1, 6}, {13, 5}, {19, 6}, {25, 7}, {34, 8}, {46, 9}, {47, 9}, {14, 5}, {6, 5}, {64, 10},
234 {87, 11}, {86, 11}, {63, 10}, {88, 11}, {90, 11}, {35, 8}, {26, 7}, {0, 7}, {48, 9}, {65, 10}, {66, 10},
235 {36, 8}, {15, 5}, {20, 6}, {91, 11}, {89, 11}, {67, 10}, {49, 9}, {50, 9}, {69, 10}, {92, 11}, {93, 11},
236 {27, 7}, {5, 5}, {37, 8}, {68, 10}, {71, 10}, {51, 9}, {52, 9}, {70, 10}, {94, 11}, {96, 11}, {38, 8},
237 {21, 6}, {16, 5}, {4, 5}, {28, 7}, {53, 9}, {95, 11}, {97, 11}, {73, 10}, {39, 8}, {29, 7}, {72, 10},
238 {98, 11}, {99, 11}, {54, 9}, {40, 8}, {22, 6}, {30, 7}, {55, 9}, {74, 10}, {76, 10}, {56, 9}, {75, 10},
239 {77, 10}, {17, 5}, {3, 5}, {23, 6}, {41, 8}, {57, 9}, {78, 10}, {79, 10}, {31, 7}, {10, 4}, {9, 4},
240 {100, 5}, {2, 5}, {11, 4}, {8, 4}, {18, 5}, {42, 8}, {58, 9}, {80, 10}, {81, 10}, {43, 8}, {44, 8},
241 {24, 6}, {12, 4},
242};
243
244std::uint32_t floor_log2(std::size_t v) {
245 const std::size_t clamped = std::max<std::size_t>(v, 1u);
246 return static_cast<std::uint32_t>(std::bit_width(clamped) - 1u);
247}
248
249struct PacketCursor {
250 const std::uint8_t* data;
251 std::size_t size;
252 std::size_t pos;
253
254 bool read_bit(std::uint32_t& out) {
255 if ((pos % kPacketBits) == 0) {
256 const std::size_t prev = pos / kPacketBits;
257 if (prev > 0) {
258 const auto hdr = parse_packet(data, size, prev - 1);
259 if (!hdr) return false;
260 pos = (prev + static_cast<std::size_t>(hdr->skip)) * kPacketBits;
261 }
262 pos += kHeaderBits;
263 }
264 const std::size_t byte_idx = pos / 8;
265 if (byte_idx >= size) return false;
266 out = static_cast<std::uint32_t>((data[byte_idx] >> (7u - (pos % 8u))) & 1u);
267 ++pos;
268 return true;
269 }
270
271 bool read_bits(std::uint32_t n, std::uint32_t& out) {
272 std::uint32_t v = 0;
273 for (std::uint32_t i = 0; i < n; ++i) {
274 std::uint32_t b = 0;
275 if (!read_bit(b)) return false;
276 v = (v << 1) | b;
277 }
278 out = v;
279 return true;
280 }
281};
282
283std::optional<std::size_t> find_next_frame(const std::uint8_t* data, std::size_t size, std::size_t start,
284 std::size_t end) {
285 const std::size_t index = start / kPacketBits;
286 const auto header = parse_packet(data, size, index);
287 if (!header) return std::nullopt;
288
289 std::size_t at = index * kPacketBits + kHeaderBits + static_cast<std::size_t>(header->first_frame_bit);
290 std::uint32_t number = 0;
291 while (at < start) {
292 PacketCursor cur{data, size, at};
293 std::uint32_t bits = 0;
294 if (!cur.read_bits(kLengthBits, bits) || bits <= kLengthBits) return std::nullopt;
295 at += static_cast<std::size_t>(bits);
296 ++number;
297 }
298 const bool last = (number + 1u) >= header->frames;
299 if ((end % kPacketBits) == 0 || (last && (end / kPacketBits) == index)) {
300 const std::size_t end_pkt = (end - 1u) / kPacketBits;
301 const auto end_hdr = parse_packet(data, size, end_pkt);
302 if (!end_hdr) return std::nullopt;
303 const std::size_t next_pkt = end_pkt + 1u + static_cast<std::size_t>(end_hdr->skip);
304 const auto next_hdr = parse_packet(data, size, next_pkt);
305 if (!next_hdr) return std::nullopt;
306 end = next_pkt * kPacketBits + kHeaderBits + static_cast<std::size_t>(next_hdr->first_frame_bit);
307 }
308 if ((end / 8u) >= size) return std::nullopt;
309 return end;
310}
311
312struct VlcEntry {
313 std::uint32_t code;
314 std::uint8_t len;
315 std::int32_t symbol;
316};
317
318struct Vlc {
319 std::vector<VlcEntry> codes;
320
321 template <typename Fn>
322 static Vlc build(std::size_t count, Fn&& get_sym_len) {
323 Vlc vlc;
324 vlc.codes.reserve(count);
325 std::uint64_t code = 0;
326 for (std::size_t i = 0; i < count; ++i) {
327 const auto [sym, len] = get_sym_len(i);
328 vlc.codes.push_back({static_cast<std::uint32_t>(code), len, sym});
329 code += (1ull << (32u - len));
330 }
331 return vlc;
332 }
333
334 std::int32_t read(std::uint32_t peek32, std::size_t& pos) const {
335 const auto it = std::upper_bound(codes.begin(), codes.end(), peek32,
336 [](std::uint32_t val, const VlcEntry& e) { return val < e.code; });
337 const std::size_t idx = (it == codes.begin()) ? 0u : static_cast<std::size_t>((it - codes.begin()) - 1);
338 pos += codes[idx].len;
339 return codes[idx].symbol;
340 }
341};
342
343struct CodeTables {
344 Vlc scale;
345 Vlc scale_rl;
346 Vlc coef[2];
347 Vlc vec4;
348 Vlc vec2;
349 Vlc vec1;
350};
351
352const CodeTables& code_tables() {
353 static const CodeTables tables = [] {
354 CodeTables t;
355 t.scale = Vlc::build(std::size(kScaleTable), [](std::size_t i) {
356 return std::pair<std::int32_t, std::uint8_t>{static_cast<std::int32_t>(kScaleTable[i].sym) - 60,
357 kScaleTable[i].len};
358 });
359 t.scale_rl = Vlc::build(std::size(kScaleRlTable), [](std::size_t i) {
360 return std::pair<std::int32_t, std::uint8_t>{static_cast<std::int32_t>(kScaleRlTable[i].sym),
361 kScaleRlTable[i].len};
362 });
363 t.coef[0] = Vlc::build(std::size(kCoef0Syms), [](std::size_t i) {
364 return std::pair<std::int32_t, std::uint8_t>{static_cast<std::int32_t>(kCoef0Syms[i]), kCoef0Lens[i]};
365 });
366 t.coef[1] = Vlc::build(std::size(kCoef1Table), [](std::size_t i) {
367 return std::pair<std::int32_t, std::uint8_t>{static_cast<std::int32_t>(kCoef1Table[i].sym),
368 kCoef1Table[i].len};
369 });
370 t.vec4 = Vlc::build(std::size(kVec4Syms), [](std::size_t i) {
371 return std::pair<std::int32_t, std::uint8_t>{static_cast<std::int32_t>(kVec4Syms[i]) - 1, kVec4Lens[i]};
372 });
373 t.vec2 = Vlc::build(std::size(kVec2Table), [](std::size_t i) {
374 return std::pair<std::int32_t, std::uint8_t>{static_cast<std::int32_t>(kVec2Table[i].sym) - 1,
375 kVec2Table[i].len};
376 });
377 t.vec1 = Vlc::build(std::size(kVec1Table), [](std::size_t i) {
378 return std::pair<std::int32_t, std::uint8_t>{static_cast<std::int32_t>(kVec1Table[i].sym),
379 kVec1Table[i].len};
380 });
381 return t;
382 }();
383 return tables;
384}
385
386void fft_radix2(std::vector<double>& re, std::vector<double>& im) {
387 const std::size_t n = re.size();
388 std::size_t j = 0;
389 for (std::size_t i = 1; i < n; ++i) {
390 std::size_t bit = n >> 1;
391 while ((j & bit) != 0) {
392 j ^= bit;
393 bit >>= 1;
394 }
395 j |= bit;
396 if (i < j) {
397 std::swap(re[i], re[j]);
398 std::swap(im[i], im[j]);
399 }
400 }
401 for (std::size_t len = 2; len <= n; len <<= 1) {
402 const double ang = -2.0 * kPi / static_cast<double>(len);
403 const double wc = std::cos(ang);
404 const double ws = std::sin(ang);
405 for (std::size_t start = 0; start < n; start += len) {
406 double cr = 1.0;
407 double ci = 0.0;
408 for (std::size_t k = 0; k < len / 2; ++k) {
409 const std::size_t a = start + k;
410 const std::size_t b = a + len / 2;
411 const double tr = re[b] * cr - im[b] * ci;
412 const double ti = re[b] * ci + im[b] * cr;
413 re[b] = re[a] - tr;
414 im[b] = im[a] - ti;
415 re[a] += tr;
416 im[a] += ti;
417 const double next_cr = cr * wc - ci * ws;
418 const double next_ci = cr * ws + ci * wc;
419 cr = next_cr;
420 ci = next_ci;
421 }
422 }
423 }
424}
425
426std::vector<float> make_sine_window(std::size_t n) {
427 std::vector<float> w(n);
428 for (std::size_t i = 0; i < n; ++i) {
429 w[i] = static_cast<float>(std::sin((static_cast<double>(i) + 0.5) * kPi / (2.0 * static_cast<double>(n))));
430 }
431 return w;
432}
433
434std::int16_t float_to_i16(float v) {
435 const float scaled = std::round(v * 32768.0f);
436 const float clamped = std::clamp(scaled, -32768.0f, 32767.0f);
437 return static_cast<std::int16_t>(clamped);
438}
439
440} // namespace
441
442struct Decoder::Bits {
443 const std::uint8_t* data;
444 std::size_t byte_len;
445 std::size_t pos;
446 std::size_t end;
447
448 std::uint32_t peek32() const {
449 std::uint64_t v = 0;
450 const std::size_t byte = pos / 8u;
451 for (std::size_t i = 0; i < 5u; ++i) {
452 const std::uint8_t b = (byte + i < byte_len) ? data[byte + i] : 0u;
453 v = (v << 8) | static_cast<std::uint64_t>(b);
454 }
455 return static_cast<std::uint32_t>((v << (24u + (pos % 8u))) >> 32u);
456 }
457
458 std::uint32_t read(std::uint32_t n) {
459 if (n == 0) return 0;
460 const std::uint32_t v = peek32() >> (32u - n);
461 pos += static_cast<std::size_t>(n);
462 return v;
463 }
464
465 bool bit() { return read(1) != 0; }
466
467 std::int32_t read_signed(std::uint32_t n) {
468 const std::uint32_t u = read(n);
469 return static_cast<std::int32_t>(u << (32u - n)) >> static_cast<int>(32u - n);
470 }
471
472 std::int32_t read_vlc(const Vlc& vlc) { return vlc.read(peek32(), pos); }
473
474 std::uint32_t large_value() {
475 std::uint32_t n = 8;
476 if (bit()) {
477 n += 8;
478 if (bit()) {
479 n += 8;
480 if (bit()) n += 7;
481 }
482 }
483 return read(n);
484 }
485
486 bool overread() const { return pos > end; }
487};
488
489std::optional<PacketHeader> parse_packet(const std::uint8_t* data, std::size_t size, std::size_t packet_index) {
490 if (!data || packet_index > std::numeric_limits<std::size_t>::max() / kPacketBytes) return std::nullopt;
491 const std::size_t start = packet_index * kPacketBytes;
492 if (start > size || (size - start) < 4u) return std::nullopt;
493 const std::uint32_t h = (static_cast<std::uint32_t>(data[start]) << 24) |
494 (static_cast<std::uint32_t>(data[start + 1]) << 16) |
495 (static_cast<std::uint32_t>(data[start + 2]) << 8) |
496 static_cast<std::uint32_t>(data[start + 3]);
497 return PacketHeader{h >> 26, (h >> 11) & 0x7fffu, h & 0xffu};
498}
499
500std::optional<FrameBits> read_frame(const std::uint8_t* data, std::size_t size, std::size_t start_bit) {
501 if (!data || size == 0) return std::nullopt;
502 PacketCursor probe{data, size, start_bit};
503 std::uint32_t bits_u32 = 0;
504 if (!probe.read_bits(kLengthBits, bits_u32)) return std::nullopt;
505 const std::size_t bits = static_cast<std::size_t>(bits_u32);
506 if (bits <= kLengthBits || bits == 0x7fffu) return std::nullopt;
507
508 std::vector<std::uint8_t> out((bits + 7u) / 8u, 0u);
509 PacketCursor cur{data, size, start_bit};
510 for (std::size_t i = 0; i < bits; ++i) {
511 std::uint32_t b = 0;
512 if (!cur.read_bit(b)) return std::nullopt;
513 out[i / 8u] |= static_cast<std::uint8_t>(b << (7u - (i % 8u)));
514 }
515 const std::size_t end = cur.pos;
516 return FrameBits{std::move(out), bits, start_bit, find_next_frame(data, size, start_bit, end)};
517}
518
519void imdct_half(const float* input, float* output, std::size_t n, float scale) {
520 const std::size_t m = n / 2u;
521 std::vector<double> re(m, 0.0);
522 std::vector<double> im(m, 0.0);
523 for (std::size_t k = 0; k < m; ++k) {
524 const double a = static_cast<double>(input[2u * k]);
525 const double b = static_cast<double>(input[n - 1u - 2u * k]);
526 const double ang = -kPi * static_cast<double>(k) / static_cast<double>(n);
527 const double c = std::cos(ang);
528 const double s = std::sin(ang);
529 re[k] = a * c - b * s;
530 im[k] = a * s + b * c;
531 }
532 fft_radix2(re, im);
533 for (std::size_t k = 0; k < m; ++k) {
534 const double ang = -kPi * static_cast<double>(4u * k + 1u) / static_cast<double>(4u * n);
535 const double c = std::cos(ang);
536 const double s = std::sin(ang);
537 const double wr = re[k] * c - im[k] * s;
538 const double wi = re[k] * s + im[k] * c;
539 output[n - 1u - 2u * k] = static_cast<float>(wr) * scale;
540 output[2u * k] = -static_cast<float>(wi) * scale;
541 }
542}
543
544void window_overlap(float* block, const float* window, std::size_t block_len) {
545 const std::size_t half = block_len / 2u;
546 for (std::size_t k = 0; k < half; ++k) {
547 const std::size_t i = k;
548 const std::size_t j = 2u * half - 1u - k;
549 const float s0 = block[i];
550 const float s1 = block[j];
551 const float wi = window[i];
552 const float wj = window[j];
553 block[i] = s0 * wj - s1 * wi;
554 block[j] = s0 * wi + s1 * wj;
555 }
556}
557
558const std::vector<float>& sine_window(std::size_t n) {
559 static const std::vector<float> w128 = make_sine_window(128);
560 static const std::vector<float> w256 = make_sine_window(256);
561 static const std::vector<float> w512 = make_sine_window(512);
562 if (n == 128u) return w128;
563 if (n == 256u) return w256;
564 return w512;
565}
566
567Decoder::Decoder(std::size_t channels, std::uint32_t sample_rate) : channels_(std::clamp<std::size_t>(channels, 1u, 2u)) {
568 std::size_t rate = 24000u;
569 if (sample_rate > 44100u) rate = 48000u;
570 else if (sample_rate > 32000u) rate = 44100u;
571 else if (sample_rate > 24000u) rate = 32000u;
572
573 for (std::size_t i = 0; i < kBlockSizes; ++i) {
574 const std::size_t sflen = kFrameSamples >> i;
575 auto& offsets = sfb_offsets_[i];
576 offsets.fill(0);
577 std::size_t band = 1;
578 for (std::size_t f = 0; f < std::size(kCriticalFreq); ++f) {
579 if (offsets[band - 1u] >= sflen) break;
580 const std::size_t offset = (((sflen * 2u * static_cast<std::size_t>(kCriticalFreq[f])) / rate) + 2u) & ~std::size_t(3u);
581 if (offset > offsets[band - 1u]) {
582 offsets[band] = offset;
583 ++band;
584 }
585 if (offset >= sflen) break;
586 }
587 offsets[band - 1u] = sflen;
588 num_sfb_[i] = band - 1u;
589 }
590
591 for (std::size_t i = 0; i < kBlockSizes; ++i) {
592 for (std::size_t b = 0; b < num_sfb_[i]; ++b) {
593 const std::size_t offset = ((sfb_offsets_[i][b] + sfb_offsets_[i][b + 1u] - 1u) << i) >> 1u;
594 for (std::size_t x = 0; x < kBlockSizes; ++x) {
595 std::size_t v = 0;
596 while (v + 1u < kMaxBands && (sfb_offsets_[x][v + 1u] << x) < offset) ++v;
597 sf_offsets_[i][x][b] = v;
598 }
599 }
600 }
601 reset();
602}
603
604void Decoder::reset() { ch_ = {Channel{}, Channel{}}; }
605
606bool Decoder::decode(const std::uint8_t* data, std::size_t bits, std::vector<std::int16_t>& out, const char** error) {
607 const std::size_t byte_len = (bits + 7u) / 8u;
608 Bits b{data, byte_len, 0u, bits};
609 const std::size_t len = static_cast<std::size_t>(b.read(kLengthBits));
610 const char* err = nullptr;
611 const bool ok = decode_frame(b, len, err);
612 if (!ok) {
613 if (error) *error = err ? err : "decode error";
614 for (std::size_t c = 0; c < channels_; ++c) ch_[c].out.fill(0.0f);
615 } else if (error) {
616 *error = nullptr;
617 }
618 out.reserve(out.size() + kFrameSamples * channels_);
619 for (std::size_t i = 0; i < kFrameSamples; ++i) {
620 for (std::size_t c = 0; c < channels_; ++c) {
621 out.push_back(float_to_i16(ch_[c].out[i]));
622 }
623 }
624 for (std::size_t c = 0; c < channels_; ++c) {
625 std::copy(ch_[c].out.begin() + static_cast<std::ptrdiff_t>(kFrameSamples), ch_[c].out.end(), ch_[c].out.begin());
626 }
627 return ok;
628}
629
630void Decoder::flush(std::vector<std::int16_t>& out) {
631 out.reserve(out.size() + (kFrameSamples / 2u) * channels_);
632 for (std::size_t i = 0; i < kFrameSamples / 2u; ++i) {
633 for (std::size_t c = 0; c < channels_; ++c) {
634 out.push_back(float_to_i16(ch_[c].out[i]));
635 }
636 }
637 reset();
638}
639
640bool Decoder::decode_frame(Bits& b, std::size_t len, const char*& err) {
641 if (!decode_tile_header(b, err)) return false;
642 if (channels_ > 1u && b.bit() && b.bit()) {
643 b.read(static_cast<std::uint32_t>(channels_ * channels_ * 4u));
644 }
645 b.read(8); // dynamic range compression gain
646 if (b.bit()) {
647 if (b.bit()) b.read(floor_log2(kFrameSamples * 2u));
648 if (b.bit()) b.read(floor_log2(kFrameSamples * 2u));
649 }
650 parsed_all_subframes_ = false;
651 for (std::size_t c = 0; c < channels_; ++c) {
652 ch_[c].decoded_samples = 0;
653 ch_[c].cur_subframe = 0;
654 ch_[c].reuse_sf = false;
655 }
656 while (!parsed_all_subframes_) {
657 if (!decode_subframe(b, err)) return false;
658 }
659 if (b.overread() || b.pos > len) {
660 err = "frame overread";
661 return false;
662 }
663 return true;
664}
665
666bool Decoder::decode_subframe_length(Bits& b, std::size_t offset, std::size_t& out_len, const char*& err) const {
667 if (offset == kFrameSamples - kSubframeSamples) {
668 out_len = kSubframeSamples;
669 return true;
670 }
671 const std::uint32_t shift = b.bit() ? (1u + b.read(1)) : 0u;
672 const std::size_t len = kFrameSamples >> shift;
673 if (len < kSubframeSamples) {
674 err = "subframe length";
675 return false;
676 }
677 out_len = len;
678 return true;
679}
680
681bool Decoder::decode_tile_header(Bits& b, const char*& err) {
682 std::array<std::size_t, 2> num_samples{0, 0};
683 std::array<bool, 2> contains{false, false};
684 std::size_t cur_channels = channels_;
685 std::size_t min_channel_len = 0;
686 for (std::size_t c = 0; c < channels_; ++c) ch_[c].num_subframes = 0;
687 const bool fixed = b.bit();
688 for (;;) {
689 for (std::size_t c = 0; c < channels_; ++c) {
690 contains[c] = (num_samples[c] == min_channel_len) &&
691 (fixed || cur_channels == 1u || min_channel_len == kFrameSamples - kSubframeSamples || b.bit());
692 }
693 std::size_t sflen = 0;
694 if (!decode_subframe_length(b, min_channel_len, sflen, err)) return false;
695 min_channel_len += sflen;
696 for (std::size_t c = 0; c < channels_; ++c) {
697 auto& ch = ch_[c];
698 if (contains[c]) {
699 if (ch.num_subframes >= kMaxSubframes) {
700 err = "too many subframes";
701 return false;
702 }
703 ch.subframe_len[ch.num_subframes] = sflen;
704 num_samples[c] += sflen;
705 ++ch.num_subframes;
706 if (num_samples[c] > kFrameSamples) {
707 err = "channel longer than frame";
708 return false;
709 }
710 } else if (num_samples[c] <= min_channel_len) {
711 if (num_samples[c] < min_channel_len) {
712 cur_channels = 0;
713 min_channel_len = num_samples[c];
714 }
715 ++cur_channels;
716 }
717 }
718 if (min_channel_len >= kFrameSamples || b.overread()) break;
719 }
720 if (b.overread()) {
721 err = "tile header overread";
722 return false;
723 }
724 return true;
725}
726
727bool Decoder::decode_channel_transform(Bits& b, const char*& err) {
728 transform_ = false;
729 if (channels_ == 1u) return true;
730 if (b.bit()) {
731 err = "channel transform bit";
732 return false;
733 }
734 if (channels_for_cur_subframe_ == 2u) {
735 if (b.bit()) {
736 if (b.bit()) {
737 err = "channel transform type";
738 return false;
739 }
740 } else {
741 transform_ = true;
742 }
743 }
744 if (transform_) {
745 if (!b.bit()) {
746 for (std::size_t i = 0; i < num_bands_; ++i) transform_band_[i] = b.bit();
747 } else {
748 transform_band_.fill(true);
749 }
750 }
751 return true;
752}
753
754bool Decoder::decode_scale_factors(Bits& b, const char*& err) {
755 const auto& tables = code_tables();
756 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
757 const std::size_t c = channel_indexes_[i];
758 const auto& sf_off = sf_offsets_[table_idx_][ch_[c].table_idx];
759 auto& ch = ch_[c];
760 const std::size_t target = 1u - ch.scale_factor_idx;
761 ch.scale_factors = target;
762 if (ch.reuse_sf) {
763 const auto from = ch.saved_scale_factors[ch.scale_factor_idx];
764 for (std::size_t band = 0; band < num_bands_; ++band) {
765 ch.saved_scale_factors[target][band] = from[sf_off[band]];
766 }
767 }
768 if (ch.cur_subframe == 0u || b.bit()) {
769 if (!ch.reuse_sf) {
770 ch.scale_factor_step = static_cast<std::int32_t>(b.read(2)) + 1;
771 std::int32_t val = 45 / ch.scale_factor_step;
772 for (std::size_t band = 0; band < num_bands_; ++band) {
773 val += b.read_vlc(tables.scale);
774 ch.saved_scale_factors[target][band] = val;
775 }
776 } else {
777 std::size_t band = 0;
778 while (band < num_bands_) {
779 const std::int32_t idx = b.read_vlc(tables.scale_rl);
780 std::uint32_t skip = 0;
781 std::int32_t val = 0;
782 bool neg = false;
783 if (idx == 0) {
784 const std::uint32_t code = b.read(14);
785 skip = (code & 0x3fu) >> 1u;
786 val = static_cast<std::int32_t>(code >> 6u);
787 neg = (code & 1u) == 0u;
788 } else if (idx == 1) {
789 break;
790 } else {
791 skip = kScaleRlRun[static_cast<std::size_t>(idx)];
792 val = static_cast<std::int32_t>(kScaleRlLevel[static_cast<std::size_t>(idx)]);
793 neg = !b.bit();
794 }
795 band += static_cast<std::size_t>(skip);
796 if (band >= num_bands_) {
797 err = "scale factor run";
798 return false;
799 }
800 ch.saved_scale_factors[target][band] += neg ? -val : val;
801 ++band;
802 }
803 }
804 ch.scale_factor_idx = target;
805 ch.table_idx = table_idx_;
806 ch.reuse_sf = true;
807 }
808 std::int32_t max_sf = 0;
809 if (num_bands_ > 0u) {
810 max_sf = ch.saved_scale_factors[ch.scale_factors][0];
811 for (std::size_t band = 1; band < num_bands_; ++band) {
812 max_sf = std::max(max_sf, ch.saved_scale_factors[ch.scale_factors][band]);
813 }
814 }
815 ch.max_scale_factor = max_sf;
816 }
817 return true;
818}
819
820bool Decoder::decode_coefficients(Bits& b, std::size_t c, const char*& err) {
821 const auto& tables = code_tables();
822 const std::size_t table = b.bit() ? 1u : 0u;
823 const std::uint16_t* run = (table == 1u) ? kCoef1Run : kCoef0Run;
824 const float* level = (table == 1u) ? kCoef1Level : kCoef0Level;
825 auto& ch = ch_[c];
826
827 bool rl_mode = false;
828 std::size_t cur = 0;
829 std::size_t num_zeros = 0;
830 while ((transmit_num_vec_coeffs_ || !rl_mode) && cur + 3u < ch.num_vec_coeffs) {
831 std::array<std::uint32_t, 4> vals{};
832 const std::int32_t idx4 = b.read_vlc(tables.vec4);
833 if (idx4 < 0) {
834 for (std::size_t i = 0; i < 4u; i += 2u) {
835 const std::int32_t idx2 = b.read_vlc(tables.vec2);
836 if (idx2 < 0) {
837 for (std::size_t k = 0; k < 2u; ++k) {
838 std::uint32_t x = static_cast<std::uint32_t>(b.read_vlc(tables.vec1));
839 if (x == static_cast<std::uint32_t>(std::size(kVec1Table) - 1u)) {
840 x += b.large_value();
841 }
842 vals[i + k] = x;
843 }
844 } else {
845 vals[i] = static_cast<std::uint32_t>(idx2 >> 4);
846 vals[i + 1u] = static_cast<std::uint32_t>(idx2 & 0xf);
847 }
848 }
849 } else {
850 vals[0] = static_cast<std::uint32_t>((idx4 >> 12) & 0xf);
851 vals[1] = static_cast<std::uint32_t>((idx4 >> 8) & 0xf);
852 vals[2] = static_cast<std::uint32_t>((idx4 >> 4) & 0xf);
853 vals[3] = static_cast<std::uint32_t>(idx4 & 0xf);
854 }
855 for (std::uint32_t v : vals) {
856 if (v != 0u) {
857 const bool neg = !b.bit();
858 ch.coeffs[cur] = neg ? -static_cast<float>(v) : static_cast<float>(v);
859 num_zeros = 0;
860 } else {
861 ch.coeffs[cur] = 0.0f;
862 ++num_zeros;
863 if (num_zeros > (subframe_len_ >> 8u)) rl_mode = true;
864 }
865 ++cur;
866 }
867 if (b.overread()) {
868 err = "vector coefficients overread";
869 return false;
870 }
871 }
872
873 if (cur < subframe_len_) {
874 std::fill(ch.coeffs.begin() + static_cast<std::ptrdiff_t>(cur),
875 ch.coeffs.begin() + static_cast<std::ptrdiff_t>(subframe_len_), 0.0f);
876 const std::size_t mask = subframe_len_ - 1u;
877 std::size_t offset = cur;
878 while (offset < subframe_len_) {
879 const std::int32_t code = b.read_vlc(tables.coef[table]);
880 if (code > 1) {
881 offset += static_cast<std::size_t>(run[static_cast<std::size_t>(code)]);
882 const bool neg = !b.bit();
883 const float v = level[static_cast<std::size_t>(code)];
884 ch.coeffs[offset & mask] = neg ? -v : v;
885 } else if (code == 1) {
886 break;
887 } else {
888 const std::int32_t value = static_cast<std::int32_t>(b.large_value());
889 if (b.bit()) {
890 if (b.bit()) {
891 if (b.bit()) break; // escape terminator
892 offset += static_cast<std::size_t>(b.read(esc_len_)) + 4u;
893 } else {
894 offset += static_cast<std::size_t>(b.read(2)) + 1u;
895 }
896 }
897 const bool neg = !b.bit();
898 ch.coeffs[offset & mask] = static_cast<float>(neg ? -value : value);
899 }
900 if (b.overread()) {
901 err = "run-level coefficients overread";
902 return false;
903 }
904 ++offset;
905 }
906 }
907 return true;
908}
909
910void Decoder::inverse_channel_transform() {
911 if (!transform_ || channels_for_cur_subframe_ != 2u) return;
912 const auto& sfb = sfb_offsets_[table_idx_];
913 auto& a = ch_[0];
914 auto& b = ch_[1];
915 for (std::size_t band = 0; band < num_bands_; ++band) {
916 const std::size_t end = std::min(sfb[band + 1u], subframe_len_);
917 if (transform_band_[band]) {
918 for (std::size_t y = sfb[band]; y < end; ++y) {
919 const float l = a.coeffs[y];
920 const float r = b.coeffs[y];
921 a.coeffs[y] = l - r;
922 b.coeffs[y] = l + r;
923 }
924 } else {
925 for (std::size_t y = sfb[band]; y < end; ++y) {
926 a.coeffs[y] *= 181.0f / 128.0f;
927 b.coeffs[y] *= 181.0f / 128.0f;
928 }
929 }
930 }
931}
932
933bool Decoder::decode_subframe(Bits& b, const char*& err) {
934 std::size_t offset = kFrameSamples;
935 std::size_t sflen = kFrameSamples;
936 std::size_t total_samples = kFrameSamples * channels_;
937 for (std::size_t c = 0; c < channels_; ++c) {
938 const auto& ch = ch_[c];
939 if (offset > ch.decoded_samples) {
940 offset = ch.decoded_samples;
941 sflen = ch.subframe_len[std::min(ch.cur_subframe, kMaxSubframes - 1u)];
942 }
943 }
944 channels_for_cur_subframe_ = 0;
945 for (std::size_t c = 0; c < channels_; ++c) {
946 auto& ch = ch_[c];
947 total_samples -= ch.decoded_samples;
948 if (offset == ch.decoded_samples && ch.cur_subframe < ch.num_subframes &&
949 sflen == ch.subframe_len[ch.cur_subframe]) {
950 total_samples -= sflen;
951 ch.decoded_samples += sflen;
952 channel_indexes_[channels_for_cur_subframe_] = c;
953 ++channels_for_cur_subframe_;
954 }
955 }
956 if (channels_for_cur_subframe_ == 0u) {
957 err = "no channel in subframe";
958 return false;
959 }
960 if (total_samples == 0u) parsed_all_subframes_ = true;
961
962 table_idx_ = static_cast<std::size_t>(floor_log2(kFrameSamples / sflen));
963 num_bands_ = num_sfb_[table_idx_];
964 subframe_len_ = sflen;
965 esc_len_ = floor_log2(sflen - 1u) + 1u;
966
967 if (b.bit()) {
968 std::size_t fill = static_cast<std::size_t>(b.read(2));
969 if (fill == 0u) {
970 const std::uint32_t len = b.read(4);
971 fill = static_cast<std::size_t>(b.read(len)) + 1u;
972 }
973 b.pos += fill;
974 }
975 if (b.bit()) {
976 err = "reserved bit set";
977 return false;
978 }
979 if (!decode_channel_transform(b, err)) return false;
980
981 bool transmit_coeffs = false;
982 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
983 const std::size_t c = channel_indexes_[i];
984 ch_[c].transmit_coefs = b.bit();
985 transmit_coeffs = transmit_coeffs || ch_[c].transmit_coefs;
986 }
987 if (transmit_coeffs) {
988 transmit_num_vec_coeffs_ = b.bit();
989 if (transmit_num_vec_coeffs_) {
990 const std::uint32_t num_bits = floor_log2((sflen + 3u) / 4u) + 1u;
991 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
992 const std::size_t c = channel_indexes_[i];
993 const std::size_t n = static_cast<std::size_t>(b.read(num_bits)) << 2u;
994 if (n > sflen) {
995 err = "vector coefficient count";
996 return false;
997 }
998 ch_[c].num_vec_coeffs = n;
999 }
1000 } else {
1001 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
1002 ch_[channel_indexes_[i]].num_vec_coeffs = sflen;
1003 }
1004 }
1005 std::int32_t quant_step = 90;
1006 std::int32_t step = b.read_signed(6);
1007 quant_step += step;
1008 if (step == -32 || step == 31) {
1009 const bool negative = (step == -32);
1010 std::int32_t quant = 0;
1011 while (b.pos + 5u < b.end) {
1012 step = static_cast<std::int32_t>(b.read(5));
1013 if (step != 31) break;
1014 quant += 31;
1015 }
1016 quant_step += negative ? -(quant + step) : (quant + step);
1017 }
1018 if (channels_for_cur_subframe_ == 1u) {
1019 ch_[channel_indexes_[0]].quant_step = quant_step;
1020 } else {
1021 const std::uint32_t mod_len = b.read(3);
1022 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
1023 const std::size_t c = channel_indexes_[i];
1024 ch_[c].quant_step = quant_step;
1025 if (b.bit()) {
1026 ch_[c].quant_step += (mod_len > 0u) ? (static_cast<std::int32_t>(b.read(mod_len)) + 1) : 1;
1027 }
1028 }
1029 }
1030 if (!decode_scale_factors(b, err)) return false;
1031 }
1032
1033 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
1034 const std::size_t c = channel_indexes_[i];
1035 if (ch_[c].transmit_coefs && b.pos < b.end) {
1036 if (!decode_coefficients(b, c, err)) return false;
1037 } else {
1038 std::fill(ch_[c].coeffs.begin(), ch_[c].coeffs.begin() + static_cast<std::ptrdiff_t>(sflen), 0.0f);
1039 }
1040 }
1041
1042 const std::size_t at = offset + kFrameSamples / 2u;
1043 if (transmit_coeffs) {
1044 inverse_channel_transform();
1045 const float scale = 2.0f / static_cast<float>(sflen) / 32768.0f;
1046 const auto& sfb = sfb_offsets_[table_idx_];
1047 std::array<float, kFrameSamples> tmp{};
1048 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
1049 auto& ch = ch_[channel_indexes_[i]];
1050 const auto& sf = ch.saved_scale_factors[ch.scale_factors];
1051 for (std::size_t band = 0; band < num_bands_; ++band) {
1052 const std::size_t end = std::min(sfb[band + 1u], sflen);
1053 const std::int32_t exp = ch.quant_step - (ch.max_scale_factor - sf[band]) * ch.scale_factor_step;
1054 const float quant = std::pow(10.0f, static_cast<float>(exp) / 20.0f);
1055 for (std::size_t y = sfb[band]; y < end; ++y) tmp[y] = ch.coeffs[y] * quant;
1056 }
1057 imdct_half(tmp.data(), ch.out.data() + at, sflen, scale);
1058 }
1059 } else {
1060 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
1061 auto& out_buf = ch_[channel_indexes_[i]].out;
1062 std::fill(out_buf.begin() + static_cast<std::ptrdiff_t>(at),
1063 out_buf.begin() + static_cast<std::ptrdiff_t>(at + sflen), 0.0f);
1064 }
1065 }
1066
1067 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
1068 auto& ch = ch_[channel_indexes_[i]];
1069 std::size_t winlen = ch.prev_block_len;
1070 std::size_t start = at - winlen / 2u;
1071 if (sflen < winlen) {
1072 start += (winlen - sflen) / 2u;
1073 winlen = sflen;
1074 }
1075 window_overlap(ch.out.data() + start, sine_window(winlen).data(), winlen);
1076 ch.prev_block_len = sflen;
1077 }
1078
1079 for (std::size_t i = 0; i < channels_for_cur_subframe_; ++i) {
1080 auto& ch = ch_[channel_indexes_[i]];
1081 if (ch.cur_subframe >= ch.num_subframes) {
1082 err = "subframe overrun";
1083 return false;
1084 }
1085 ++ch.cur_subframe;
1086 }
1087 if (b.overread()) {
1088 err = "subframe overread";
1089 return false;
1090 }
1091 return true;
1092}
1093
1094} // namespace leonida::xma2
diff --git a/runtime/src/xma2_decoder.h b/runtime/src/xma2_decoder.h
deleted file mode 100644
index 3ee9fed17..000000000
--- a/runtime/src/xma2_decoder.h
+++ /dev/null
@@ -1,116 +0,0 @@
1// Portable XMA2 packet parser and WMA Pro frame decoder (1–2 channels, 512-sample frames, 128–512 subframes).
2//
3// Algorithm and canonical Huffman / quantization tables follow FFmpeg's libavcodec/wmaprodec.c and wmaprodata.h
4// (LGPL-2.1-or-later, used here under GPL-3.0-or-later), specialized for Xbox XMA2 streams (up to 2 channels, no LFE,
5// 2048-byte packets with 32-bit MSB-first packet headers).
6#pragma once
7
8#include <array>
9#include <cstddef>
10#include <cstdint>
11#include <optional>
12#include <vector>
13
14namespace leonida::xma2 {
15
16inline constexpr std::size_t kPacketBytes = 2048;
17inline constexpr std::size_t kPacketBits = kPacketBytes * 8;
18inline constexpr std::size_t kHeaderBits = 32;
19inline constexpr std::size_t kFrameSamples = 512;
20inline constexpr std::size_t kSubframeSamples = 128;
21
22struct PacketHeader {
23 std::uint32_t frames = 0;
24 std::uint32_t first_frame_bit = 0;
25 std::uint32_t skip = 0;
26};
27
28struct FrameBits {
29 std::vector<std::uint8_t> data;
30 std::size_t bits = 0;
31 std::size_t start = 0;
32 std::optional<std::size_t> next;
33};
34
35// Parses the 32-bit MSB-first header of packet `packet_index` inside `data[0..size)`.
36std::optional<PacketHeader> parse_packet(const std::uint8_t* data, std::size_t size, std::size_t packet_index);
37
38// Gathers one XMA2 frame starting at bit offset `start_bit` inside a buffer of 2048-byte packets, crossing packet
39// headers and skipped interleaved stream packets, and computes the bit offset of the next frame if present.
40std::optional<FrameBits> read_frame(const std::uint8_t* data, std::size_t size, std::size_t start_bit);
41
42// Half-length inverse MDCT (negated DCT-IV scaled by `scale`) and sine window overlap-add helpers.
43void imdct_half(const float* input, float* output, std::size_t n, float scale);
44void window_overlap(float* block, const float* window, std::size_t block_len);
45const std::vector<float>& sine_window(std::size_t n);
46
47class Decoder {
48public:
49 static constexpr std::size_t kBlockSizes = 3;
50 static constexpr std::size_t kMaxBands = 29;
51 static constexpr std::size_t kMaxSubframes = 32;
52
53 Decoder(std::size_t channels, std::uint32_t sample_rate);
54
55 std::size_t channels() const { return channels_; }
56 void reset();
57
58 // Decodes one frame (`data`, `bits`) and appends kFrameSamples interleaved 16-bit PCM samples per channel to `out`.
59 // Returns false and optionally sets `*error` on a bitstream error (still emitting timing-preserving silence).
60 bool decode(const std::uint8_t* data, std::size_t bits, std::vector<std::int16_t>& out,
61 const char** error = nullptr);
62
63 // Emits the remaining half-frame overlap tail (kFrameSamples / 2 samples per channel) and resets state.
64 void flush(std::vector<std::int16_t>& out);
65
66private:
67 struct Channel {
68 std::size_t prev_block_len = kFrameSamples;
69 bool transmit_coefs = false;
70 std::size_t num_subframes = 0;
71 std::array<std::size_t, kMaxSubframes> subframe_len{};
72 std::size_t cur_subframe = 0;
73 std::size_t decoded_samples = 0;
74 std::int32_t quant_step = 0;
75 bool reuse_sf = false;
76 std::int32_t scale_factor_step = 1;
77 std::int32_t max_scale_factor = 0;
78 std::array<std::array<std::int32_t, kMaxBands>, 2> saved_scale_factors{};
79 std::size_t scale_factor_idx = 0;
80 std::size_t scale_factors = 0;
81 std::size_t table_idx = 0;
82 std::size_t num_vec_coeffs = 0;
83 std::array<float, kFrameSamples> coeffs{};
84 std::array<float, kFrameSamples + kFrameSamples / 2> out{};
85 };
86
87 struct Bits;
88
89 bool decode_frame(Bits& b, std::size_t len, const char*& err);
90 bool decode_tile_header(Bits& b, const char*& err);
91 bool decode_subframe_length(Bits& b, std::size_t offset, std::size_t& out_len, const char*& err) const;
92 bool decode_subframe(Bits& b, const char*& err);
93 bool decode_channel_transform(Bits& b, const char*& err);
94 bool decode_scale_factors(Bits& b, const char*& err);
95 bool decode_coefficients(Bits& b, std::size_t c, const char*& err);
96 void inverse_channel_transform();
97
98 std::size_t channels_ = 1;
99 std::array<std::size_t, kBlockSizes> num_sfb_{};
100 std::array<std::array<std::size_t, kMaxBands + 1>, kBlockSizes> sfb_offsets_{};
101 std::array<std::array<std::array<std::size_t, kMaxBands>, kBlockSizes>, kBlockSizes> sf_offsets_{};
102 std::array<Channel, 2> ch_{};
103
104 std::size_t subframe_len_ = kFrameSamples;
105 std::size_t channels_for_cur_subframe_ = 0;
106 std::array<std::size_t, 2> channel_indexes_{0, 1};
107 std::size_t num_bands_ = 0;
108 std::size_t table_idx_ = 0;
109 std::uint32_t esc_len_ = 0;
110 bool transmit_num_vec_coeffs_ = false;
111 bool transform_ = false;
112 std::array<bool, kMaxBands> transform_band_{};
113 bool parsed_all_subframes_ = false;
114};
115
116} // namespace leonida::xma2
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
12namespace {
13
14constexpr double kPi = 3.14159265358979323846264338327950288;
15
16int g_checks = 0;
17int g_failures = 0;
18
19void 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
25void 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
33std::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
50int 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}
diff --git a/runtime/xma_decoder/README.md b/runtime/xma_decoder/README.md
index 4a6d5fb10..5ef66abbc 100644
--- a/runtime/xma_decoder/README.md
+++ b/runtime/xma_decoder/README.md
@@ -17,3 +17,9 @@ cargo build --manifest-path runtime/xma_decoder/Cargo.toml --locked
17``` 17```
18 18
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. 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.
20
21## Use in the runtime
22
23`runtime/CMakeLists.txt` builds this crate at configure time when `cargo` is on `PATH` (the first build fetches the pinned `xma` git dependency) and links it into `gta6rt` and `acphal_test` with `LEONIDA_HAVE_XMA2_DECODER` defined. `runtime/shims/acphal.cpp` calls `leonida_xma2_decoder_create_hardware` once per XMA context and decodes whole 2048-byte packets, so loop points are rounded to packets.
24
25Without `cargo`, when that build fails, or with the cl.exe fallback of `scripts/build_runtime.ps1`, XMA voices output silence.
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..cda2721fb 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,
@@ -233,6 +255,24 @@ mod tests {
233 } 255 }
234 256
235 #[test] 257 #[test]
258 fn create_hardware_accepts_stereo_and_rejects_three_channels() {
259 let mut decoder = ptr::null_mut();
260 assert_eq!(
261 unsafe { leonida_xma2_decoder_create_hardware(48_000, 2, &mut decoder) },
262 LEONIDA_XMA2_OK
263 );
264 assert!(!decoder.is_null());
265 unsafe { leonida_xma2_decoder_destroy(decoder) };
266
267 let mut decoder = ptr::dangling_mut();
268 assert_eq!(
269 unsafe { leonida_xma2_decoder_create_hardware(48_000, 3, &mut decoder) },
270 LEONIDA_XMA2_INVALID_FORMAT
271 );
272 assert!(decoder.is_null());
273 }
274
275 #[test]
236 fn pcm_handle_allocation_failure_clears_output_and_allows_retry() { 276 fn pcm_handle_allocation_failure_clears_output_and_allows_retry() {
237 let mut decoder = ptr::null_mut(); 277 let mut decoder = ptr::null_mut();
238 assert_eq!( 278 assert_eq!(
diff --git a/scripts/build_runtime.ps1 b/scripts/build_runtime.ps1
index baa8f8d75..d575b4200 100644
--- a/scripts/build_runtime.ps1
+++ b/scripts/build_runtime.ps1
@@ -78,10 +78,10 @@ $build || exit /b 1
78} else { 78} else {
79 # Keep this list in step with runtime\CMakeLists.txt. Optional parts that need a dependency are left out here: 79 # Keep this list in step with runtime\CMakeLists.txt. Optional parts that need a dependency are left out here:
80 # the GameInput shim (SDL3), so controllers go through the host's GameInput.dll, if any, as without the shim. 80 # the GameInput shim (SDL3), so controllers go through the host's GameInput.dll, if any, as without the shim.
81 Write-Warning "CMake and Ninja weren't found (neither VS C++ CMake tools nor native PATH tools): building with cl.exe directly, without the GameInput shim (SDL3). Install CMake + Ninja or VS's CMake tools component to get it." 81 Write-Warning "CMake and Ninja weren't found (neither VS C++ CMake tools nor native PATH tools): building with cl.exe directly, without the GameInput shim (SDL3) and without the Rust XMA2 decoder (XMA voices are silent). Install CMake + Ninja or VS's CMake tools component to get it."
82 $xtaskqueueDir = Join-Path $root "third_party\xtaskqueue" 82 $xtaskqueueDir = Join-Path $root "third_party\xtaskqueue"
83 $xtaskqueueFiles = "Source\Task\TaskQueue.cpp", "Source\Task\AsyncLib.cpp", "Source\Task\ThreadPool_win32.cpp", "Source\Task\WaitTimer_win32.cpp" | ForEach-Object { "`"$xtaskqueueDir\$_`"" } 83 $xtaskqueueFiles = "Source\Task\TaskQueue.cpp", "Source\Task\AsyncLib.cpp", "Source\Task\ThreadPool_win32.cpp", "Source\Task\WaitTimer_win32.cpp" | ForEach-Object { "`"$xtaskqueueDir\$_`"" }
84 $files = "src\main.cpp", "src\host_cpu.cpp", "src\pe_loader.cpp", "src\xma2_decoder.cpp", "shims\registry.cpp", "shims\file_system.cpp", "shims\xmem.cpp", "shims\xg_xs.cpp", "shims\acphal.cpp", "shims\d3d12_xs.cpp", "shims\d3d12_xs_device.cpp", "shims\d3d12_xs_rootsig.cpp", "shims\xgameruntime.cpp", "shims\xgameruntime_user.cpp", "shims\xgameruntime_gamesave.cpp", "shims\xgameruntime_store.cpp", "shims\xgameruntime_offline.cpp", "shims\winhttp_offline.cpp", "shims\library_loader.cpp", "shims\process.cpp", "shims\com_shim.cpp", "shims\bcrypt_rng.cpp", "shims\xapu_chad.cpp", "shims\dstorage.cpp" | ForEach-Object { "`"$src\$_`"" } 84 $files = "src\main.cpp", "src\host_cpu.cpp", "src\pe_loader.cpp", "shims\registry.cpp", "shims\file_system.cpp", "shims\xmem.cpp", "shims\xg_xs.cpp", "shims\acphal.cpp", "shims\d3d12_xs.cpp", "shims\d3d12_xs_device.cpp", "shims\d3d12_xs_rootsig.cpp", "shims\xgameruntime.cpp", "shims\xgameruntime_user.cpp", "shims\xgameruntime_gamesave.cpp", "shims\xgameruntime_store.cpp", "shims\xgameruntime_offline.cpp", "shims\winhttp_offline.cpp", "shims\library_loader.cpp", "shims\process.cpp", "shims\com_shim.cpp", "shims\bcrypt_rng.cpp", "shims\xapu_chad.cpp", "shims\dstorage.cpp" | ForEach-Object { "`"$src\$_`"" }
85 $includes = "/I `"$src\src`" /I `"$src\shims`" /I `"$src\shims\xtaskqueue`" /I `"$xtaskqueueDir\Include`" /I `"$xtaskqueueDir\Source\Task`" /I `"$xtaskqueueDir\Source\Common`"" 85 $includes = "/I `"$src\src`" /I `"$src\shims`" /I `"$src\shims\xtaskqueue`" /I `"$xtaskqueueDir\Include`" /I `"$xtaskqueueDir\Source\Task`" /I `"$xtaskqueueDir\Source\Common`""
86 $opt = if ($Debug) { "/Od /MDd" } else { "/O2 /MD" } 86 $opt = if ($Debug) { "/Od /MDd" } else { "/O2 /MD" }
87 $body = @" 87 $body = @"
diff --git a/third_party/xma/NOTICE.md b/third_party/xma/NOTICE.md
index 1e2176829..a7066b383 100644
--- a/third_party/xma/NOTICE.md
+++ b/third_party/xma/NOTICE.md
@@ -10,3 +10,5 @@ The upstream NOTICE identifies this package as a Rust port of FFmpeg's WMA Pro/X
10> This file is part of FFmpeg and is licensed under the GNU Lesser General Public License, version 2.1 or later. 10> This file is part of FFmpeg and is licensed under the GNU Lesser General Public License, version 2.1 or later.
11 11
12Leonida's C ABI wrapper is independently written and licensed under GPL-3.0-or-later. No upstream implementation source is copied or vendored. 12Leonida's C ABI wrapper is independently written and licensed under GPL-3.0-or-later. No upstream implementation source is copied or vendored.
13
14When `cargo` is found, `runtime/CMakeLists.txt` links this package statically into `gta6rt`; the runtime is GPL-3.0 and its full source is in this repository.