diff options
| author | BubsMcgee <alexbraden99@gmail.com> | 2026-11-25 14:11:53 -0500 |
|---|---|---|
| committer | BubsMcgee <alexbraden99@gmail.com> | 2026-11-25 14:11:53 -0500 |
| commit | a5dd540bccc6ce20971a6680f076beedc13f2a95 (patch) | |
| tree | 5b5c60d1027384485d63b57e35def6bd38e65ee6 /runtime/shims/xapu_queue.cpp | |
| parent | 6ee7ef89b948a9942f5de0b61d1f6756f477d6d5 (diff) | |
xapu: decode Opus packets through runtime/opus_decoder (RT-XAPU-OPUS-DECODE)
Builds on #1144 (RT-XAPU-CONNECT-LAYOUTS): the queue and layouts stay there;
fill() decodes each process packet through a Rust C-ABI crate over BSD-3
opus-pure when cargo builds it, and keeps the table-of-contents silence path
otherwise. Adds xapu_opus_test (a generated 440 Hz stereo packet decoded to
960 frames) and the CMake wiring; xapu_chad_test keeps covering the layouts
and the silent path. No game data.
Diffstat (limited to 'runtime/shims/xapu_queue.cpp')
| -rw-r--r-- | runtime/shims/xapu_queue.cpp | 99 |
1 files changed, 88 insertions, 11 deletions
diff --git a/runtime/shims/xapu_queue.cpp b/runtime/shims/xapu_queue.cpp index 3e69ce43c..e754376ba 100644 --- a/runtime/shims/xapu_queue.cpp +++ b/runtime/shims/xapu_queue.cpp | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | // xapu.dll: the console's audio processing unit (APU) command queue. No PC has one, so this keeps the queue the title | 1 | // xapu.dll: the console's audio processing unit (APU) command queue. No PC has one, so this keeps the queue the title |
| 2 | // talks to - one result per accepted command, in command order, with the console's error codes - and decodes nothing: | 2 | // talks to - one result per accepted command, in command order, with the console's error codes. With |
| 3 | // an Opus packet's result carries the frame count its table-of-contents byte gives and silence in its output. | 3 | // runtime/opus_decoder built (LEONIDA_HAVE_OPUS_DECODER) a process command decodes its Opus packet to PCM; without |
| 4 | // it the result carries the frame count its table-of-contents byte gives and silence in its output. | ||
| 4 | // | 5 | // |
| 5 | // Read from the executable's call sites (counts in docs/runtime_imports.md, shapes in docs/audio_hal_exe_calls.md), | 6 | // Read from the executable's call sites (counts in docs/runtime_imports.md, shapes in docs/audio_hal_exe_calls.md), |
| 6 | // statically, never run: | 7 | // statically, never run: |
| @@ -35,10 +36,10 @@ | |||
| 35 | // stream out of range, or the stream already holding `commands_per_stream` results not dequeued) gives no result; | 36 | // stream out of range, or the stream already holding `commands_per_stream` results not dequeued) gives no result; |
| 36 | // every other command gives one, with its error, if any, in the result. | 37 | // every other command gives one, with its error, if any, in the result. |
| 37 | // | 38 | // |
| 38 | // What this does not do: decode. A process command's frame count comes from the Opus packet's table-of-contents byte | 39 | // Without runtime/opus_decoder a process command's frame count comes from the Opus packet's table-of-contents byte |
| 39 | // (RFC 6716, section 3.1), clipped from first_frame, to frame_count and to what output_bytes holds, and that output | 40 | // (RFC 6716, section 3.1), clipped from first_frame, to frame_count and to what output_bytes holds, and that output |
| 40 | // range is written with zeros. So the title sees streams advance at the right rate and its error checks pass, and the | 41 | // range is written with zeros, so the title sees streams advance at the right rate and the audio stays silent. With |
| 41 | // audio stays silent until a decoder is wired in at fill_silence(). | 42 | // the crate, fill() decodes the packet and writes its PCM instead. |
| 42 | #include <windows.h> | 43 | #include <windows.h> |
| 43 | #include <intrin.h> | 44 | #include <intrin.h> |
| 44 | 45 | ||
| @@ -53,6 +54,10 @@ | |||
| 53 | #include <string> | 54 | #include <string> |
| 54 | #include <vector> | 55 | #include <vector> |
| 55 | 56 | ||
| 57 | #if defined(LEONIDA_HAVE_OPUS_DECODER) | ||
| 58 | #include "leonida_opus_decoder.h" | ||
| 59 | #endif | ||
| 60 | |||
| 56 | #include "shims.h" | 61 | #include "shims.h" |
| 57 | #include "startup_trace.h" | 62 | #include "startup_trace.h" |
| 58 | 63 | ||
| @@ -75,7 +80,8 @@ constexpr uint32_t kReset = 5; // 4 is notify; neither carries anything to do w | |||
| 75 | 80 | ||
| 76 | constexpr uint32_t kSignalWhenDone = 0x1; // CommandId::options: set the result event when the result is ready | 81 | constexpr uint32_t kSignalWhenDone = 0x1; // CommandId::options: set the result event when the result is ready |
| 77 | constexpr uint32_t kResultEvent = 0x8; // ConnectInput::options: create an auto-reset event and hand it out | 82 | constexpr uint32_t kResultEvent = 0x8; // ConnectInput::options: create an auto-reset event and hand it out |
| 78 | // ConnectInput::options 0x4 asks for deinterleaved output; silence is the same zeros either way. | 83 | constexpr uint32_t kDeinterleavedOutput = 0x4; // ConnectInput::options: split stereo into two planes |
| 84 | constexpr uint32_t kOpusSampleRate = 48000; | ||
| 79 | 85 | ||
| 80 | constexpr uint32_t kMaxStreams = 500; | 86 | constexpr uint32_t kMaxStreams = 500; |
| 81 | constexpr uint32_t kMaxCommandsPerStream = 16; | 87 | constexpr uint32_t kMaxCommandsPerStream = 16; |
| @@ -150,12 +156,16 @@ struct Stream { | |||
| 150 | bool active = false; | 156 | bool active = false; |
| 151 | uint32_t channels = 0; | 157 | uint32_t channels = 0; |
| 152 | uint32_t pending = 0; // results queued and not dequeued | 158 | uint32_t pending = 0; // results queued and not dequeued |
| 159 | #if defined(LEONIDA_HAVE_OPUS_DECODER) | ||
| 160 | LeonidaOpusDecoder* decoder = nullptr; | ||
| 161 | #endif | ||
| 153 | }; | 162 | }; |
| 154 | 163 | ||
| 155 | struct Connection { | 164 | struct Connection { |
| 156 | void* memory = nullptr; | 165 | void* memory = nullptr; |
| 157 | HANDLE event = nullptr; | 166 | HANDLE event = nullptr; |
| 158 | uint32_t commands_per_stream = 0; | 167 | uint32_t commands_per_stream = 0; |
| 168 | bool deinterleaved = false; | ||
| 159 | std::mutex mutex; | 169 | std::mutex mutex; |
| 160 | std::vector<Stream> streams; | 170 | std::vector<Stream> streams; |
| 161 | std::deque<Result> results; | 171 | std::deque<Result> results; |
| @@ -180,7 +190,7 @@ void note(const char* api, uint64_t value, const void* caller) { | |||
| 180 | } | 190 | } |
| 181 | 191 | ||
| 182 | // Samples per channel at 48 kHz in one Opus packet, from its table-of-contents byte and frame count code (RFC 6716, | 192 | // Samples per channel at 48 kHz in one Opus packet, from its table-of-contents byte and frame count code (RFC 6716, |
| 183 | // section 3.1); 0 for a packet that is not valid. | 193 | // section 3.1); 0 for a packet that is not valid. The no-decoder path counts frames this way. |
| 184 | uint32_t opus_packet_frames(const uint8_t* packet, uint32_t bytes) { | 194 | uint32_t opus_packet_frames(const uint8_t* packet, uint32_t bytes) { |
| 185 | if (!packet || bytes == 0) return 0; | 195 | if (!packet || bytes == 0) return 0; |
| 186 | static constexpr uint32_t kSilk[4] = {480, 960, 1920, 2880}; // 10, 20, 40, 60 ms | 196 | static constexpr uint32_t kSilk[4] = {480, 960, 1920, 2880}; // 10, 20, 40, 60 ms |
| @@ -202,8 +212,9 @@ uint32_t opus_packet_frames(const uint8_t* packet, uint32_t bytes) { | |||
| 202 | return count * per_frame; | 212 | return count * per_frame; |
| 203 | } | 213 | } |
| 204 | 214 | ||
| 205 | // Fills a process command's result. The decoder belongs here: today the frames are counted, not decoded. | 215 | // Fills a process command's result: the packet's PCM when runtime/opus_decoder is built, silence (with the frame |
| 206 | void fill_silence(const Stream& stream, const ProcessCommand& c, Result& r) { | 216 | // count from the table-of-contents byte) otherwise. |
| 217 | void fill(Connection& connection, Stream& stream, const ProcessCommand& c, Result& r) { | ||
| 207 | r.input = c.input; | 218 | r.input = c.input; |
| 208 | r.input_bytes = c.input_bytes; | 219 | r.input_bytes = c.input_bytes; |
| 209 | r.output = c.output; | 220 | r.output = c.output; |
| @@ -211,6 +222,46 @@ void fill_silence(const Stream& stream, const ProcessCommand& c, Result& r) { | |||
| 211 | r.error = E_INVALIDARG; | 222 | r.error = E_INVALIDARG; |
| 212 | return; | 223 | return; |
| 213 | } | 224 | } |
| 225 | #if defined(LEONIDA_HAVE_OPUS_DECODER) | ||
| 226 | if (stream.decoder) { | ||
| 227 | const uint32_t channels = leonida_opus_decoder_channels(stream.decoder); | ||
| 228 | if (channels == 0) { | ||
| 229 | r.error = kInvalidStream; | ||
| 230 | return; | ||
| 231 | } | ||
| 232 | std::vector<float> pcm; | ||
| 233 | try { | ||
| 234 | pcm.resize(size_t(kMaxFrames) * channels); | ||
| 235 | } catch (...) { | ||
| 236 | r.error = E_OUTOFMEMORY; | ||
| 237 | return; | ||
| 238 | } | ||
| 239 | uint32_t decoded = 0; | ||
| 240 | if (leonida_opus_decode_packet(stream.decoder, c.input, c.input_bytes, pcm.data(), kMaxFrames, &decoded) != | ||
| 241 | LEONIDA_OPUS_OK) { | ||
| 242 | r.error = kBadPacket; | ||
| 243 | return; | ||
| 244 | } | ||
| 245 | const uint32_t first = c.first_frame < decoded ? c.first_frame : decoded; | ||
| 246 | uint32_t frames = decoded - first; | ||
| 247 | if (frames > c.frame_count) frames = c.frame_count; | ||
| 248 | const uint32_t room = c.output_bytes / (4 * channels); | ||
| 249 | if (frames > room) frames = room; | ||
| 250 | const uint32_t bytes = frames * channels * 4; | ||
| 251 | if (connection.deinterleaved && channels == 2) { | ||
| 252 | float* out = reinterpret_cast<float*>(c.output); | ||
| 253 | for (uint32_t i = 0; i < frames; ++i) { | ||
| 254 | out[i] = pcm[(first + i) * 2]; | ||
| 255 | out[frames + i] = pcm[(first + i) * 2 + 1]; | ||
| 256 | } | ||
| 257 | } else if (bytes != 0) { | ||
| 258 | std::memcpy(c.output, pcm.data() + first * channels, bytes); | ||
| 259 | } | ||
| 260 | r.frames = frames; | ||
| 261 | r.output_bytes = bytes; | ||
| 262 | return; | ||
| 263 | } | ||
| 264 | #endif | ||
| 214 | const uint32_t decoded = opus_packet_frames(c.input, c.input_bytes); | 265 | const uint32_t decoded = opus_packet_frames(c.input, c.input_bytes); |
| 215 | if (decoded == 0) { | 266 | if (decoded == 0) { |
| 216 | r.error = kBadPacket; | 267 | r.error = kBadPacket; |
| @@ -266,17 +317,26 @@ HRESULT __stdcall XApuConnect(const ConnectInput* input, ConnectOutput* output, | |||
| 266 | } | 317 | } |
| 267 | } | 318 | } |
| 268 | c->commands_per_stream = in.commands_per_stream; | 319 | c->commands_per_stream = in.commands_per_stream; |
| 320 | c->deinterleaved = (in.options & kDeinterleavedOutput) != 0; | ||
| 269 | const ConnectOutput o{c->memory, c->event}; | 321 | const ConnectOutput o{c->memory, c->event}; |
| 270 | std::memcpy(output, &o, sizeof(o)); | 322 | std::memcpy(output, &o, sizeof(o)); |
| 271 | *out = c; | 323 | *out = c; |
| 272 | std::fprintf(stderr, "[rt] xapu: connected for Opus decoding, %u streams; packets are counted, not decoded\n", | 324 | #if defined(LEONIDA_HAVE_OPUS_DECODER) |
| 325 | std::fprintf(stderr, "[rt] xapu: connected for Opus decoding, %u streams; packets decoded\n", in.streams); | ||
| 326 | #else | ||
| 327 | std::fprintf(stderr, "[rt] xapu: connected for Opus decoding, %u streams; packets counted, not decoded\n", | ||
| 273 | in.streams); | 328 | in.streams); |
| 329 | #endif | ||
| 274 | return S_OK; | 330 | return S_OK; |
| 275 | } | 331 | } |
| 276 | 332 | ||
| 277 | HRESULT __stdcall XApuDisconnect(Connection* c, uint32_t /*0x20 at the measured site*/) { | 333 | HRESULT __stdcall XApuDisconnect(Connection* c, uint32_t /*0x20 at the measured site*/) { |
| 278 | note("XApuDisconnect", 0, _ReturnAddress()); | 334 | note("XApuDisconnect", 0, _ReturnAddress()); |
| 279 | if (!c) return E_INVALIDARG; | 335 | if (!c) return E_INVALIDARG; |
| 336 | #if defined(LEONIDA_HAVE_OPUS_DECODER) | ||
| 337 | for (Stream& s : c->streams) | ||
| 338 | if (s.decoder) leonida_opus_decoder_destroy(s.decoder); | ||
| 339 | #endif | ||
| 280 | if (c->event) CloseHandle(c->event); | 340 | if (c->event) CloseHandle(c->event); |
| 281 | VirtualFree(c->memory, 0, MEM_RELEASE); | 341 | VirtualFree(c->memory, 0, MEM_RELEASE); |
| 282 | delete c; | 342 | delete c; |
| @@ -308,14 +368,31 @@ HRESULT __stdcall XApuEnqueueCommand(Connection* c, const void* command, uint32_ | |||
| 308 | } else { | 368 | } else { |
| 309 | stream.active = true; | 369 | stream.active = true; |
| 310 | stream.channels = a.channels; | 370 | stream.channels = a.channels; |
| 371 | #if defined(LEONIDA_HAVE_OPUS_DECODER) | ||
| 372 | if (leonida_opus_decoder_create(kOpusSampleRate, a.channels, &stream.decoder) != LEONIDA_OPUS_OK) { | ||
| 373 | stream.active = false; | ||
| 374 | stream.channels = 0; | ||
| 375 | r.error = E_INVALIDARG; | ||
| 376 | } | ||
| 377 | #endif | ||
| 311 | } | 378 | } |
| 312 | } else if (id.kind == kDeactivate) { | 379 | } else if (id.kind == kDeactivate) { |
| 313 | stream.active = false; | 380 | stream.active = false; |
| 381 | #if defined(LEONIDA_HAVE_OPUS_DECODER) | ||
| 382 | if (stream.decoder) { | ||
| 383 | leonida_opus_decoder_destroy(stream.decoder); | ||
| 384 | stream.decoder = nullptr; | ||
| 385 | } | ||
| 386 | #endif | ||
| 387 | } else if (id.kind == kReset) { | ||
| 388 | #if defined(LEONIDA_HAVE_OPUS_DECODER) | ||
| 389 | if (stream.decoder) leonida_opus_decoder_reset(stream.decoder); | ||
| 390 | #endif | ||
| 314 | } else if (id.kind == kProcess) { | 391 | } else if (id.kind == kProcess) { |
| 315 | ProcessCommand p; | 392 | ProcessCommand p; |
| 316 | std::memcpy(&p, command, sizeof(p)); | 393 | std::memcpy(&p, command, sizeof(p)); |
| 317 | if (!stream.active) r.error = kInvalidStream; | 394 | if (!stream.active) r.error = kInvalidStream; |
| 318 | else fill_silence(stream, p, r); | 395 | else fill(*c, stream, p, r); |
| 319 | } | 396 | } |
| 320 | try { | 397 | try { |
| 321 | c->results.push_back(r); | 398 | c->results.push_back(r); |