acphal: synchronize XMA context words and bound the output ring

This commit is contained in:
andretuta
2026-11-24 04:29:18 -03:00
parent a0327ee1af
commit e02ec36bf3
4 changed files with 114 additions and 77 deletions

View File

@ -139,24 +139,7 @@ struct SHAPE_DMA_CONTEXT {
static_assert(sizeof(SHAPE_DMA_CONTEXT) == 16, "SHAPE_DMA_CONTEXT is 16 bytes per GDK");
struct SHAPE_XMA_CONTEXT {
uint32_t sizeRead0 : 12;
uint32_t numLoops : 8;
uint32_t validBuffer : 2;
uint32_t sizeWrite : 5;
uint32_t offsetWrite : 5;
uint32_t sizeRead1 : 12;
uint32_t sequenceNumber : 12;
uint32_t stopSampleAddress : 8;
uint32_t subframeOffset : 4;
uint32_t reserved0 : 1;
uint32_t isLooping : 1;
uint32_t subframeSkip : 2;
uint32_t packetMetadata : 16;
uint32_t currentByteOffset : 26;
uint32_t reserved1 : 6;
uint32_t loopStartOffset : 26;
uint32_t loopEndOffset : 6;
uint32_t reserved[11]; // the GDK's 64-byte context: the published field list leaves one trailing word
uint32_t words[16]; // See the shared word layout above the XMA engine.
};
static_assert(sizeof(SHAPE_XMA_CONTEXT) == 64, "SHAPE_XMA_CONTEXT is 64 bytes per GDK");
@ -382,10 +365,9 @@ struct Xma2PcmFree {
#endif
struct XmaContextView {
volatile uint32_t* words = nullptr;
uint32_t* words = nullptr;
uint32_t get(std::size_t index) const { return words[index]; }
void set(std::size_t index, uint32_t value) const { words[index] = value; }
uint32_t get(std::size_t index) const { return std::atomic_ref(words[index]).load(); }
uint32_t field(std::size_t index, uint32_t shift, uint32_t bits) const {
const uint32_t mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u);
@ -395,7 +377,10 @@ struct XmaContextView {
void set_field(std::size_t index, uint32_t shift, uint32_t bits, uint32_t value) const {
const uint32_t raw_mask = (bits >= 32) ? 0xFFFFFFFFu : ((1u << bits) - 1u);
const uint32_t mask = raw_mask << shift;
set(index, (get(index) & ~mask) | ((value << shift) & mask));
const uint32_t field_value = (value << shift) & mask;
std::atomic_ref word(words[index]);
uint32_t previous = word.load();
while (!word.compare_exchange_weak(previous, (previous & ~mask) | field_value)) {}
}
std::size_t channels() const { return 1u + static_cast<std::size_t>(field(1, 29, 1)); }
@ -407,6 +392,7 @@ struct XmaVoice {
#endif
std::deque<int16_t> pcm;
std::size_t skip = 0; // samples per channel to drop (start/loop subframe skip)
std::optional<std::size_t> next_input_packet;
bool ended = false;
};
@ -422,7 +408,7 @@ public:
void bind(SHAPE_XMA_CONTEXT* array, uint32_t count) {
unbind();
std::unique_lock lock(table_mutex_);
array_ = reinterpret_cast<volatile uint32_t*>(array);
array_ = array;
voices_.clear();
voices_.reserve(count);
for (uint32_t i = 0; i < count; ++i) {
@ -522,8 +508,8 @@ public:
bool wake = false;
{
std::lock_guard slot_lock(voices_[index]->mutex);
const uint32_t raw_size = ctx.field(0, 22, 5);
const uint32_t size = raw_size ? raw_size : 1u;
const uint32_t size = ctx.field(0, 22, 5);
if (size == 0) return;
const uint32_t read = (ctx.field(9, 0, 5) + blocks % size) % size;
ctx.set_field(9, 0, 5, read);
if (blocks > 0) {
@ -537,7 +523,7 @@ public:
private:
XmaContextView context_locked(uint32_t index) const {
if (!array_ || index >= voices_.size()) return {};
return XmaContextView{array_ + static_cast<std::size_t>(index) * 16};
return XmaContextView{array_[index].words};
}
// Fills `pcm` (interleaved s16) for one 2048-byte packet. Without the Rust decoder, or when it
@ -559,7 +545,7 @@ private:
const std::unique_ptr<LeonidaXma2PcmBlock, Xma2PcmFree> block(raw);
const int16_t* samples = leonida_xma2_pcm_data(block.get());
const std::size_t count = leonida_xma2_pcm_sample_count(block.get());
pcm.assign(samples, samples + count);
if (count != 0) pcm.assign(samples, samples + count);
return;
}
voice.decoder.reset(); // Resume with a clean state after replacing a packet with silence.
@ -580,7 +566,7 @@ private:
uint32_t current = ctx.field(4, 31, 1);
const uint32_t valid = ctx.field(0, 20, 2);
if ((valid & (1u << current)) == 0) {
if (voice.next_input_packet || (valid & (1u << current)) == 0) {
const uint32_t other = 1u - current;
if ((valid & (1u << other)) == 0) {
voice.ended = true;
@ -590,7 +576,9 @@ private:
}
current = other;
ctx.set_field(4, 31, 1, current);
ctx.set_field(2, 0, 26, 0);
const auto next_bit = voice.next_input_packet.value_or(0) * kXmaPacketBits;
ctx.set_field(2, 0, 26, static_cast<uint32_t>(next_bit));
voice.next_input_packet.reset();
}
const uint32_t address = (current == 0) ? ctx.get(5) : ctx.get(6);
@ -599,6 +587,7 @@ private:
const uint8_t* data = apu_span(address & ~0x7FFu, packets * kXmaPacketBytes);
if (!data || packet >= packets) {
ctx.set_field(0, 20 + current, 1, 0);
voice.next_input_packet = 0;
ctx.set_field(2, 0, 26, 0);
return true;
}
@ -627,6 +616,7 @@ private:
}
if (next >= packets) {
ctx.set_field(0, 20 + current, 1, 0);
voice.next_input_packet = next - packets;
next = 0;
}
ctx.set_field(2, 0, 26, static_cast<uint32_t>(next * kXmaPacketBits));
@ -635,20 +625,18 @@ private:
static void pump_voice(XmaContextView ctx, XmaVoice& voice) {
const uint32_t size = ctx.field(0, 22, 5);
if (size == 0) return;
auto* ring = apu_span(ctx.get(7), size * kXmaBlockBytes);
if (!ring) return;
while (ctx.field(1, 31, 1) == 1) {
for (uint32_t blocks = 0; blocks < size && ctx.field(1, 31, 1) == 1; ++blocks) {
int decode_guard = 0;
while (voice.pcm.size() < kXmaSamplesPerBlock && decode_guard++ < 16) {
if (!decode_one_packet(ctx, voice)) break;
}
if (voice.pcm.empty()) return;
const uint32_t write = ctx.field(0, 27, 5);
const uint32_t out_addr =
ctx.get(7) + write * static_cast<uint32_t>(kXmaBlockBytes);
uint8_t* out = apu_span(out_addr, kXmaBlockBytes);
if (!out) return;
const uint32_t write = ctx.field(0, 27, 5) % size;
uint8_t* out = ring + write * kXmaBlockBytes;
for (std::size_t i = 0; i < kXmaSamplesPerBlock; ++i) {
int16_t sample = 0;
@ -663,7 +651,7 @@ private:
const uint32_t next_write = (write + 1u) % size;
ctx.set_field(0, 27, 5, next_write);
if (next_write == ctx.field(9, 0, 5)) {
if (next_write == ctx.field(9, 0, 5) % size) {
ctx.set_field(1, 31, 1, 0);
}
}
@ -728,7 +716,7 @@ private:
}
mutable std::shared_mutex table_mutex_;
volatile uint32_t* array_ = nullptr;
SHAPE_XMA_CONTEXT* array_ = nullptr;
std::vector<std::unique_ptr<XmaVoiceSlot>> voices_;
std::atomic<uint32_t> active_count_{0};

View File

@ -6,6 +6,7 @@
#include <windows.h>
#include <algorithm>
#include <atomic>
#include <cstddef>
#include <cstdint>
@ -69,6 +70,14 @@ static_assert(sizeof(TestShapeContexts) == 120, "TestShapeContexts is 120 bytes"
static_assert(offsetof(TestShapeContexts, xmaContextArray) == 0x38,
"xmaContextArray is at offset +0x38");
struct XmaWords {
uint32_t* data;
std::atomic_ref<uint32_t> operator[](std::size_t index) const {
return std::atomic_ref(data[index]);
}
};
} // namespace
int main() {
@ -348,7 +357,8 @@ int main() {
// 8. End-to-end XMA2 context ring decoding via background decoder thread
{
// Build a 2-packet XMA2 buffer with 3 silent mono frames per packet (6 frames total).
// Two synthetic XMA2 packets with three mono frames each. Each frame
// transmits one positive spectral coefficient so decoded PCM differs from fallback silence.
std::vector<uint8_t> stream(2 * 2048, 0xFFu);
auto put_bits = [&](size_t at, uint32_t value, size_t width) {
for (size_t i = 0; i < width; ++i) {
@ -361,12 +371,29 @@ int main() {
};
for (size_t packet = 0; packet < 2; ++packet) {
const size_t base = packet * 2048 * 8;
put_bits(base, 3u << 26, 32); // 3 frames, first_frame_bit = 0, skip = 0
put_bits(base, (3u << 26) | (1u << 8), 32);
constexpr size_t frame_bits = 82;
for (size_t f = 0; f < 3; ++f) {
const size_t at = base + 32 + f * 31;
put_bits(at, 31, 15); // frame length
put_bits(at + 15, 0b10u, 2); // fixed layout, one 512-sample subframe
put_bits(at + 17, 0, 14); // gain(8) + trim(1) + subframe_skip/empty(5)
size_t at = base + 32 + f * frame_bits;
auto put = [&](uint32_t value, size_t width) {
put_bits(at, value, width);
at += width;
};
put(frame_bits, 15);
put(0b10u, 2); // Fixed layout, one 512-sample subframe.
put(0, 11); // Gain, trim, fill and subframe error flag.
put(1, 1); // Coefficients are present.
put(1, 1); // Explicit vector coefficient count.
put(0, 8); // Use only run/level coding.
put(0, 6); // Quantizer adjustment.
put(0, 2); // Scale factor step.
put((1u << 23) - 1, 23); // One zero delta per 48 kHz scale band.
put(0, 1); // Run/level table 0.
put(0b00u, 2); // Run 0, level 1.
put(1, 1); // Positive sign.
put(0b0101011u, 7); // End of coefficients.
put(0, 1); // Frame padding bit.
put(f < 2 ? 1u : 0u, 1); // Another frame follows in this packet.
}
}
@ -389,7 +416,7 @@ int main() {
// Configure voice index 1: 2 packets in input 0, input 0 valid, 4 output blocks,
// writable = 1, 48 kHz mono, read_bit = 32.
auto* words = static_cast<volatile uint32_t*>(xma_ctx.xmaContextArray) + 16;
const XmaWords words{static_cast<uint32_t*>(xma_ctx.xmaContextArray) + 16};
words[0] = 2u | (1u << 20) | (4u << 22);
words[1] = (1u << 31) | (3u << 27);
words[2] = 32u;
@ -418,27 +445,28 @@ int main() {
for (int wait = 0; wait < 500 && ((words[1] >> 31) & 1u) == 1u; ++wait) {
Sleep(1);
}
check(((words[1] >> 31) & 1u) == 0u,
"Background XMA2 decoder fills ring and clears writable flag");
const bool ring_full = (words[1] >> 31) == 0u;
check(ring_full, "Background XMA2 decoder fills ring and clears writable flag");
check(((words[0] >> 27) & 0x1Fu) == 0u,
"Write block wrapped back to 0 after filling 4 blocks");
bool ring_zeroed = true;
const auto* ring_bytes = static_cast<const uint8_t*>(out_va);
for (size_t i = 0; i < 0x400; ++i) {
if (ring_bytes[i] != 0) {
ring_zeroed = false;
break;
}
}
check(ring_zeroed, "Decoded silent PCM overwrote sentinel bytes across all 4 ring blocks");
#if defined(LEONIDA_HAVE_XMA2_DECODER)
check(ring_full && std::any_of(ring_bytes, ring_bytes + 0x400,
[](uint8_t b) { return b != 0 && b != 0x55; }),
"The Rust decoder writes non-silent PCM");
#else
check(ring_full && std::all_of(ring_bytes, ring_bytes + 0x400, [](uint8_t b) { return b == 0; }),
"The fallback writes silence");
#endif
// Advance read pointer 4 blocks at a time until input exhausts and INPUT_INVALID fires.
const uint32_t advance_args[2] = {1u, 4u};
for (int step = 0; step < 20; ++step) {
check(fn_submit(x_acp, 18, 0xf2 + static_cast<uint64_t>(step),
0xffffffffu, advance_args, 0) == S_OK,
"ADVANCE_XMA_READ succeeds");
int written_blocks = ring_full ? 4 : 0;
bool advanced = true;
for (int step = 0; step < 6; ++step) {
advanced &= fn_submit(x_acp, 18, 0xf2 + static_cast<uint64_t>(step),
0xffffffffu, advance_args, 0) == S_OK;
// Wait until either ring fills again (writable == 0) or stream hits error bit.
for (int wait = 0; wait < 200; ++wait) {
const uint32_t w1 = words[1];
@ -449,7 +477,11 @@ int main() {
if (((words[2] >> 31) & 1u) == 1u && ((words[1] >> 31) & 1u) == 1u) {
break;
}
if ((words[1] >> 31) != 0u) break;
written_blocks += 4;
}
check(advanced, "ADVANCE_XMA_READ succeeds");
check(written_blocks == 24, "Six frames write exactly 24 blocks before input exhaustion");
check(((words[0] >> 20) & 3u) == 0u, "XMA input buffer valid bits cleared on exhaustion");
check(((words[2] >> 31) & 1u) == 1u, "XMA error flag set on exhaustion");
check(((words[2] >> 26) & 0x1Fu) == 4u, "XMA error status is INPUT_INVALID (4)");
@ -505,7 +537,7 @@ int main() {
"ApuAlloc 16-block loop output ring");
// Voice index 2.
auto* words = static_cast<volatile uint32_t*>(xma_ctx.xmaContextArray) + 32;
const XmaWords words{static_cast<uint32_t*>(xma_ctx.xmaContextArray) + 32};
using ConnectFn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t);
using SubmitFn = HRESULT(__stdcall*)(void*, uint32_t, uint64_t, uint32_t, const void*, uint32_t);
@ -519,23 +551,27 @@ int main() {
const auto fn_release = reinterpret_cast<ReleaseFn>(vt[5]);
check(fn_connect(x_acp, 0x1000, 0x1000) == S_OK, "Connect for packet loop test");
// Decodes the stream once on voice 2 with the given loop count and returns the number of
// blocks written when the error flag rose, or -1 on timeout.
auto run_blocks = [&](uint32_t loops, bool split_inputs = false) -> int {
auto configure_voice = [&](uint32_t loops, uint32_t input_0_packets, uint32_t ring_size,
uint32_t write, uint32_t read) {
std::memset(out_va, 0x55, 0x1000);
words[0] = (split_inputs ? 2u : 3u) | (loops << 12) |
((split_inputs ? 3u : 1u) << 20) | (16u << 22);
words[1] = (1u << 31) | (3u << 27) | (split_inputs ? 1u : 0u);
const uint32_t input_1_packets = 3u - input_0_packets;
words[0] = input_0_packets | (loops << 12) |
((input_1_packets != 0 ? 3u : 1u) << 20) | (ring_size << 22) | (write << 27);
words[1] = (1u << 31) | (3u << 27) | input_1_packets;
words[2] = 32u; // read at packet 0
words[3] = 32u; // loop start: packet 0
words[4] = 100u; // loop end: packet 0
words[5] = in_apu;
words[6] = in_apu + 0x1000u;
words[6] = in_apu + input_0_packets * 2048u;
words[7] = out_apu;
words[9] = 0u;
words[9] = read;
};
const uint32_t enable_arg = 2u;
check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &enable_arg, 0) == S_OK,
const uint32_t voice_index = 2u;
// Returns the number of blocks written before input exhaustion, or -1 on timeout.
auto run_blocks = [&](uint32_t loops, uint32_t input_0_packets = 3) -> int {
configure_voice(loops, input_0_packets, 16, 0, 0);
check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK,
"ENABLE_XMA voice 2 succeeds");
int wait = 0;
for (; wait < 1000 && ((words[2] >> 31) & 1u) == 0u; ++wait) {
@ -544,8 +580,7 @@ int main() {
const int blocks = ((words[2] >> 31) & 1u) != 0u
? static_cast<int>((words[0] >> 27) & 0x1Fu)
: -1;
const uint32_t disable_arg = 2u;
check(fn_submit(x_acp, 7, 0xf4, 0xffffffffu, &disable_arg, 0) == S_OK,
check(fn_submit(x_acp, 7, 0xf4, 0xffffffffu, &voice_index, 0) == S_OK,
"DISABLE_XMA voice 2 succeeds");
return blocks;
};
@ -553,8 +588,24 @@ int main() {
check(run_blocks(0) == 8, "Packets 0 and 2 write 8 blocks, skipping packet 1");
check(run_blocks(1) == 12, "One loop of packet 0 writes 12 blocks (packets 0, 0, 2)");
check(((words[0] >> 12) & 0xFFu) == 0u, "Loop count is consumed by the single loop");
check(run_blocks(0, true) == 8 && (words[4] >> 31) == 1u,
check(run_blocks(0, 2) == 8 && (words[4] >> 31) == 1u,
"A skip to the end of input 0 continues at input 1");
check(run_blocks(0, 1) == 8 && (words[4] >> 31) == 1u,
"An overshooting skip starts at packet 1 of input 1");
configure_voice(255, 3, 4, 7, 9);
check(fn_submit(x_acp, 5, 0xf3, 0xffffffffu, &voice_index, 0) == S_OK,
"Enable infinite loop with out-of-range ring indices");
for (int wait = 0; wait < 1000 && (words[1] >> 31) != 0u; ++wait) Sleep(1);
check((words[1] >> 31) == 0u && ((words[0] >> 27) & 0x1Fu) == 1u,
"Ring stops at the normalized read index");
check(fn_submit(x_acp, 7, 0xf4, 0xffffffffu, &voice_index, 0) == S_OK,
"Disable infinite loop after filling bounded ring");
check(((words[0] >> 12) & 0xFFu) == 255u, "Infinite loop does not consume its loop count");
const auto* ring_bytes = static_cast<const uint8_t*>(out_va);
check(std::all_of(ring_bytes + 0x400, ring_bytes + 0x1000,
[](uint8_t b) { return b == 0x55; }),
"Output never writes beyond the configured four-block ring");
words[0] = 3u << 22; // three ring blocks
words[9] = 2u;

View File

@ -6,8 +6,6 @@ Each decoder handle serializes decode calls. The returned PCM block owns its sam
The tests build packets in memory; they do not open or contain audio fixtures. The synthetic C++ smoke test verifies the exported ABI and ownership boundary.
This PR adds the decoder boundary only; the Windows XAudio2 engine/source-voice adapter is a follow-up stage.
Run:
```powershell
@ -22,6 +20,6 @@ The C++ smoke test is run by the xma2-decoder workflow with MSVC. It links ws2_3
`runtime/CMakeLists.txt` builds this crate for `x86_64-pc-windows-msvc` at configure time when `cargo` is on `PATH` and links it into `gta6rt` and `acphal_test` with `LEONIDA_HAVE_XMA2_DECODER` defined. The first build fetches the pinned `xma` git dependency. Set `CARGO_NET_OFFLINE=true` for a configure that uses only the local Cargo cache.
`runtime/shims/acphal.cpp` uses `leonida_xma2_decoder_create_hardware` and decodes whole 2048-byte packets, so loop points are rounded to packets.
`runtime/shims/acphal.cpp` uses `leonida_xma2_decoder_create_hardware` and decodes whole 2048-byte packets, so loop points are rounded to packets. The shim creates a fresh decoder after a loop rewind or a decoder error.
Without `cargo`, when that build fails, or with the cl.exe fallback of `scripts/build_runtime.ps1`, XMA voices output silence.

View File

@ -11,4 +11,4 @@ The upstream NOTICE identifies this package as a Rust port of FFmpeg's WMA Pro/X
Leonida's C ABI wrapper is independently written and licensed under GPL-3.0-or-later. No upstream implementation source is copied or vendored.
When `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.
When the Cargo build succeeds, `runtime/CMakeLists.txt` links the decoder and its `xma` dependency statically into `gta6rt` and `acphal_test`. The wrapper source is in `runtime/xma_decoder`. The pinned dependency source is available from the commit linked above.