diff --git a/core/libs/prx/libSceAjm.native/Export.cpp b/core/libs/prx/libSceAjm.native/Export.cpp index 62c0bc44e..4c3bbc33a 100644 --- a/core/libs/prx/libSceAjm.native/Export.cpp +++ b/core/libs/prx/libSceAjm.native/Export.cpp @@ -14,18 +14,6 @@ static void AjmStub(const char* name) { extern "C" { -int APS5_VABI sceAjmBatchJobControl(AjmBatchInfo* info, uint32_t instance, uint64_t flags, const void* sideband_input, size_t sideband_input_size, void* sideband_output, size_t sideband_output_size) { - (void)info; - (void)instance; - (void)flags; - (void)sideband_input; - (void)sideband_input_size; - (void)sideband_output; - (void)sideband_output_size; - AjmStub(__func__); - return 0; -} - int APS5_VABI sceAjmBatchJobDecodeSplit(AjmBatchInfo* info, uint32_t instance, const AjmBuffer* input_buffers, size_t input_buffers_num, const AjmBuffer* output_buffers, size_t output_buffers_num, void* result) { (void)info; (void)instance; diff --git a/core/libs/prx/libSceAjm.native/src/Ajm.cpp b/core/libs/prx/libSceAjm.native/src/Ajm.cpp index 9b5542631..93088b0f7 100644 --- a/core/libs/prx/libSceAjm.native/src/Ajm.cpp +++ b/core/libs/prx/libSceAjm.native/src/Ajm.cpp @@ -56,6 +56,8 @@ bool TraceEnabled() { constexpr std::uint64_t RUN_GET_CODEC_INFO = 1ull << 11; constexpr std::uint64_t RUN_MULTIPLE_FRAMES = 1ull << 12; +constexpr std::uint64_t CONTROL_RESET = 1ull << 13; +constexpr std::uint64_t CONTROL_INITIALIZE = 1ull << 14; constexpr std::uint64_t SIDEBAND_GAPLESS_DECODE = 1ull << 45; constexpr std::uint64_t SIDEBAND_FORMAT = 1ull << 46; constexpr std::uint64_t SIDEBAND_STREAM = 1ull << 47; @@ -139,6 +141,7 @@ enum class JobKind : std::uint32_t { SetGaplessDecode = 3, Run = 4, GetStatistics = 5, + Control = 6, }; struct JobHeader { @@ -844,6 +847,43 @@ void RunOpus(Instance& instance, const JobHeader& job, const AjmBuffer* inputs, WriteRunSideband(job, instance, result, consumed, pcmOutputs.produced, frames, CurrentFormat(instance)); } +void ClearContext(Instance& instance) { + instance.totalDecodedSamples = 0; + instance.gapless.skippedSamples = 0; + if (instance.decoder && instance.initialized) ResetDecoder(instance); + if (instance.mp3) avcodec_flush_buffers(instance.mp3); + if (instance.opus) avcodec_flush_buffers(instance.opus); + instance.opusPending.clear(); +} + +std::size_t ControlInitializeSize(std::uint32_t codec) { + switch (codec) { + case CODEC_MP3: return 0; + case CODEC_AT9: return 8; + case CODEC_OPUS: return 12; + default: NotImplemented_nid_no_patch("sceAjmBatchJobControl (INITIALIZE for a codec other than MP3, ATRAC9 and Opus)"); return 0; + } +} + +void Control(Instance& instance, const JobHeader& job, const AjmBuffer* inputs) { + const auto* input = job.inputCount ? static_cast(inputs[0].ptr) : nullptr; + const std::size_t inputSize = job.inputCount ? inputs[0].size : 0; + const std::size_t gaplessSize = (job.flags & SIDEBAND_GAPLESS_DECODE) ? sizeof(SidebandGaplessDecode) : 0; + const std::size_t initializeSize = (job.flags & CONTROL_INITIALIZE) ? ControlInitializeSize(instance.codec) : 0; + if (inputSize != gaplessSize + initializeSize) NotImplemented_nid_no_patch("sceAjmBatchJobControl (sideband input size other than the gapless decode and the codec's initialize parameters)"); + std::int32_t result = 0; + if (job.flags & CONTROL_RESET) ClearContext(instance); + if (job.flags & CONTROL_INITIALIZE) result = InitializeInstance(instance, input + gaplessSize, initializeSize); + if (gaplessSize) { + SidebandGaplessDecode gapless{}; + std::memcpy(&gapless, input, sizeof(gapless)); + instance.gapless.totalSamples = gapless.totalSamples; + instance.gapless.skipSamples = gapless.skipSamples; + } + AJM_TRACE("[ajm] instance %u control flags 0x%llx: %zu sideband input bytes -> result 0x%x, gapless total %u skip %u skipped %u\n", job.instance, static_cast(job.flags), inputSize, static_cast(result), instance.gapless.totalSamples, instance.gapless.skipSamples, instance.gapless.skippedSamples); + WriteResult(job.sideband, job.sidebandSize, result); +} + void Execute(const JobHeader& job, const AjmBuffer* inputs, const AjmBuffer* outputs) { if (job.kind == JobKind::GetStatistics) { AJM_TRACE("[ajm] statistics job: sideband %llu bytes\n", static_cast(job.sidebandSize)); @@ -866,12 +906,7 @@ void Execute(const JobHeader& job, const AjmBuffer* inputs, const AjmBuffer* out } case JobKind::ClearContext: AJM_TRACE("[ajm] instance %u clear context (sideband %llu bytes)\n", job.instance, static_cast(job.sidebandSize)); - instance->totalDecodedSamples = 0; - instance->gapless.skippedSamples = 0; - if (instance->decoder && instance->initialized) ResetDecoder(*instance); - if (instance->mp3) avcodec_flush_buffers(instance->mp3); - if (instance->opus) avcodec_flush_buffers(instance->opus); - instance->opusPending.clear(); + ClearContext(*instance); WriteResult(job.sideband, job.sidebandSize, 0); break; case JobKind::SetGaplessDecode: { @@ -884,6 +919,9 @@ void Execute(const JobHeader& job, const AjmBuffer* inputs, const AjmBuffer* out WriteResult(job.sideband, job.sidebandSize, 0); break; } + case JobKind::Control: + Control(*instance, job, inputs); + break; case JobKind::Run: if (!instance->initialized) { AJM_TRACE("[ajm] instance %u run before initialize\n", job.instance); @@ -1029,6 +1067,19 @@ int APS5_VABI sceAjmBatchJobSetGaplessDecode(AjmBatchInfo* info, uint32_t instan return Append(info, header, nullptr, nullptr); } +int APS5_VABI sceAjmBatchJobControl(AjmBatchInfo* info, uint32_t instance, uint64_t flags, const void* sideband_input, size_t sideband_input_size, void* sideband_output, size_t sideband_output_size) { + constexpr std::uint64_t supported = CONTROL_RESET | CONTROL_INITIALIZE | SIDEBAND_GAPLESS_DECODE; + if (flags == 0 || (flags & ~supported) != 0) NotImplemented_nid_no_patch("sceAjmBatchJobControl (flags other than a combination of RESET, INITIALIZE and SIDEBAND_GAPLESS_DECODE)"); + if ((flags & SIDEBAND_GAPLESS_DECODE) != 0 && (flags & CONTROL_RESET) == 0) NotImplemented_nid_no_patch("sceAjmBatchJobControl (SIDEBAND_GAPLESS_DECODE without RESET)"); + if (sideband_output_size != sizeof(SidebandResult)) NotImplemented_nid_no_patch("sceAjmBatchJobControl (sideband output other than the 8-byte result)"); + if (sideband_input_size != 0 && !sideband_input) NotImplemented_nid_no_patch("sceAjmBatchJobControl (null sideband input)"); + auto header = MakeHeader(JobKind::Control, instance, sideband_output, sideband_output_size); + header.flags = flags; + header.inputCount = sideband_input_size != 0 ? 1 : 0; + const AjmBuffer input{const_cast(sideband_input), sideband_input_size}; + return Append(info, header, &input, nullptr); +} + int APS5_VABI sceAjmBatchJobRunSplit(AjmBatchInfo* info, uint32_t instance, uint64_t flags, const AjmBuffer* input_buffers, size_t input_buffers_num, const AjmBuffer* output_buffers, size_t output_buffers_num, void* sideband_output, size_t sideband_output_size) { auto header = MakeHeader(JobKind::Run, instance, sideband_output, sideband_output_size); header.flags = flags; diff --git a/core/libs/tests/GuestAjm.cpp b/core/libs/tests/GuestAjm.cpp index 0e9bb2483..72d6bb691 100644 --- a/core/libs/tests/GuestAjm.cpp +++ b/core/libs/tests/GuestAjm.cpp @@ -21,6 +21,7 @@ int APS5_VABI sceAjmBatchJobDecode(AjmBatchInfo*, std::uint32_t, const void*, st int APS5_VABI sceAjmBatchJobDecodeSingle(AjmBatchInfo*, std::uint32_t, const void*, std::size_t, void*, std::size_t, void*); int APS5_VABI sceAjmBatchJobRun(AjmBatchInfo*, std::uint32_t, std::uint64_t, const void*, std::size_t, void*, std::size_t, void*, std::size_t); int APS5_VABI sceAjmBatchJobSetGaplessDecode(AjmBatchInfo*, std::uint32_t, const void*, int, void*); +int APS5_VABI sceAjmBatchJobControl(AjmBatchInfo*, std::uint32_t, std::uint64_t, const void*, std::size_t, void*, std::size_t); int APS5_VABI sceAjmBatchJobGetGaplessDecode(AjmBatchInfo*, std::uint32_t, void*); int APS5_VABI sceAjmBatchJobGetCodecInfo(AjmBatchInfo*, std::uint32_t, void*, std::size_t); int APS5_VABI sceAjmBatchStart(std::uint32_t, const AjmBatchInfo*, int, AjmBatchError*, std::uint32_t*); @@ -464,6 +465,157 @@ void TestBatchCancel(std::uint32_t context) { Require(sceAjmBatchCancel(context, id + 1) == invalidBatch); } +constexpr std::uint64_t CONTROL_RESET = 1ull << 13; +constexpr std::uint64_t CONTROL_INITIALIZE = 1ull << 14; +constexpr std::uint64_t SIDEBAND_GAPLESS_DECODE = 1ull << 45; +constexpr std::uint64_t CONTROL_START = CONTROL_RESET | CONTROL_INITIALIZE | SIDEBAND_GAPLESS_DECODE; + +struct At9Control { + GaplessDecode gapless; + std::uint8_t config[4]; + std::uint32_t reserved; +}; + +struct OpusControl { + GaplessDecode gapless; + std::uint32_t channels; + std::uint32_t sampleRate; + std::uint32_t third; +}; + +void RunControl(std::uint32_t context, std::uint32_t instance, std::uint64_t flags, const void* input, std::size_t inputSize, std::int32_t* result) { + std::vector batch(4096); + AjmBatchInfo info{}; + Require(sceAjmBatchInitialize(batch.data(), batch.size(), &info) == 0); + Require(sceAjmBatchJobControl(&info, instance, flags, input, inputSize, result, 2 * sizeof(std::int32_t)) == 0); + Submit(context, info); +} + +bool ControlThrows(std::uint32_t instance, std::uint64_t flags, const void* input, std::size_t inputSize, std::size_t outputSize) { + std::vector batch(4096); + AjmBatchInfo info{}; + std::int32_t result[4] = {}; + Require(sceAjmBatchInitialize(batch.data(), batch.size(), &info) == 0); + try { + static_cast(sceAjmBatchJobControl(&info, instance, flags, input, inputSize, result, outputSize)); + } catch (const std::runtime_error&) { + return info.offset == 0; + } + return false; +} + +bool ControlRefused(std::uint32_t context, std::uint32_t instance, std::uint64_t flags, const void* input, std::size_t inputSize) { + std::vector batch(4096); + AjmBatchInfo info{}; + std::int32_t result[2] = {-1, -1}; + Require(sceAjmBatchInitialize(batch.data(), batch.size(), &info) == 0); + Require(sceAjmBatchJobControl(&info, instance, flags, input, inputSize, result, sizeof(result)) == 0); + return Refused(context, info); +} + +void TestControlAt9(std::uint32_t context) { + std::uint32_t instance = 0; + Require(sceAjmInstanceCreate(context, 1, 0, &instance) == 0); + std::vector batch(4096); + const At9Control start{{2000, 100, 0}, {0xFE, 0x72, 0x1F, 0xF0}, 0xDEADBEEFu}; + std::int32_t result[4] = {-1, -1, -1, -1}; + At9CodecInfoSideband codec{-1, -1, 0, 0, 0, 0}; + GaplessSideband gapless{-1, -1, 0, 0, 0xffff}; + AjmBatchInfo info{}; + Require(sceAjmBatchInitialize(batch.data(), batch.size(), &info) == 0); + Require(sceAjmBatchJobControl(&info, instance, CONTROL_START, &start, sizeof(start), result, 2 * sizeof(std::int32_t)) == 0); + Require(sceAjmBatchJobGetCodecInfo(&info, instance, &codec, sizeof(codec)) == 0); + Require(sceAjmBatchJobGetGaplessDecode(&info, instance, &gapless) == 0); + Submit(context, info); + Require(result[0] == 0 && result[1] == 0 && result[2] == -1 && result[3] == -1); + Require(codec.result == 0 && codec.superframeSize == 1024 && codec.framesInSuperframe == 4 && codec.nextFrameSize == 1024 && codec.frameSamples == 256); + Require(gapless.result == 0 && gapless.totalSamples == 2000 && gapless.skipSamples == 100 && gapless.skippedSamples == 0); + + At9Control broken = start; + broken.config[0] = 0xFD; + At9CodecInfoSideband uninitialized{-1, -1, 0, 0, 0, 0}; + Require(sceAjmBatchInitialize(batch.data(), batch.size(), &info) == 0); + Require(sceAjmBatchJobControl(&info, instance, CONTROL_START, &broken, sizeof(broken), result, 2 * sizeof(std::int32_t)) == 0); + Require(sceAjmBatchJobGetCodecInfo(&info, instance, &uninitialized, sizeof(uninitialized)) == 0); + Submit(context, info); + Require(result[0] == 4 && uninitialized.result == 1); + + result[0] = -1; + RunControl(context, instance, CONTROL_INITIALIZE, start.config, sizeof(start) - sizeof(start.gapless), result); + Require(result[0] == 0); + + Require(ControlRefused(context, instance, CONTROL_INITIALIZE, &start, sizeof(start))); + Require(ControlRefused(context, instance, CONTROL_RESET | SIDEBAND_GAPLESS_DECODE, &start, sizeof(start))); + Require(ControlThrows(instance, CONTROL_INITIALIZE | SIDEBAND_GAPLESS_DECODE, &start, sizeof(start), 8)); + Require(ControlThrows(instance, CONTROL_START | (1ull << 15), &start, sizeof(start), 8)); + Require(ControlThrows(instance, CONTROL_START | (1ull << 46), &start, sizeof(start), 8)); + Require(ControlThrows(instance, CONTROL_START | (1ull << 12), &start, sizeof(start), 8)); + Require(ControlThrows(instance, 0, nullptr, 0, 8)); + Require(ControlThrows(instance, CONTROL_START, &start, sizeof(start), 16)); + Require(ControlThrows(instance, CONTROL_START, nullptr, sizeof(start), 8)); + Require(sceAjmInstanceDestroy(context, instance) == 0); + + result[0] = -1; + RunControl(context, instance, CONTROL_START, &start, sizeof(start), result); + Require(result[0] == 4); + + const std::uint32_t aac[4] = {0, 0, 1, 0}; + Require(sceAjmInstanceCreate(context, 2, 0, &instance) == 0); + Require(ControlRefused(context, instance, CONTROL_START, aac, sizeof(aac))); + Require(sceAjmInstanceDestroy(context, instance) == 0); +} + +void TestControlMp3(std::uint32_t context) { + std::uint32_t instance = 0; + Require(sceAjmInstanceCreate(context, 0, 0, &instance) == 0); + std::vector pcm(1152); + const std::size_t pcmBytes = pcm.size() * sizeof(std::int16_t); + DecodeSideband decoded{}; + RunDecode(context, instance, MP3_MONO, sizeof(MP3_MONO), pcm.data(), pcmBytes, decoded); + RunDecode(context, instance, MP3_MONO + 96, sizeof(MP3_MONO) - 96, pcm.data(), pcmBytes, decoded); + Require(decoded.result == 0 && decoded.totalDecodedSamples == 2 * 1152); + + const GaplessDecode skip{0, 100, 0}; + std::int32_t result[2] = {-1, -1}; + RunControl(context, instance, CONTROL_START, &skip, sizeof(skip), result); + RunDecode(context, instance, MP3_MONO, sizeof(MP3_MONO), pcm.data(), pcmBytes, decoded); + Require(result[0] == 0 && decoded.result == 0 && decoded.inputConsumed == 96 && decoded.outputWritten == (1152 - 100) * 2 && decoded.totalDecodedSamples == 1152 - 100); + + result[0] = -1; + RunControl(context, instance, CONTROL_RESET, nullptr, 0, result); + RunDecode(context, instance, MP3_MONO, sizeof(MP3_MONO), pcm.data(), pcmBytes, decoded); + Require(result[0] == 0 && decoded.result == 0 && decoded.outputWritten == (1152 - 100) * 2 && decoded.totalDecodedSamples == 1152 - 100); + + const GaplessDecode none{0, 0, 0}; + Require(ControlThrows(instance, SIDEBAND_GAPLESS_DECODE, &none, sizeof(none), 8)); + + const GaplessDecode longer{5000, 200, 0}; + result[0] = -1; + RunControl(context, instance, CONTROL_RESET | SIDEBAND_GAPLESS_DECODE, &longer, sizeof(longer), result); + RunDecode(context, instance, MP3_MONO, sizeof(MP3_MONO), pcm.data(), pcmBytes, decoded); + Require(result[0] == 0 && decoded.result == 0 && decoded.outputWritten == (1152 - 200) * 2 && decoded.totalDecodedSamples == 1152 - 200); + + const std::uint32_t parameters[2] = {1, 0}; + Require(ControlRefused(context, instance, CONTROL_INITIALIZE, parameters, sizeof(parameters))); + Require(sceAjmInstanceDestroy(context, instance) == 0); +} + +void TestControlOpus(std::uint32_t context) { + std::uint32_t instance = 0; + Require(sceAjmInstanceCreate(context, 24, 0, &instance) == 0); + const OpusControl start{{0, 0, 0}, 2, 48000, 0}; + std::int32_t result[2] = {-1, -1}; + RunControl(context, instance, CONTROL_START, &start, sizeof(start), result); + Require(result[0] == 0); + std::vector pcm(960 * 2); + DecodeSideband decoded{}; + RunDecode(context, instance, OPUS_STEREO, sizeof(OPUS_STEREO), pcm.data(), pcm.size() * sizeof(std::int16_t), decoded); + const std::size_t bytes = OPUS_STEREO[0] | (std::size_t{OPUS_STEREO[1]} << 8u); + Require(decoded.result == 0 && static_cast(decoded.inputConsumed) == 2 + bytes && decoded.outputWritten == 960 * 2 * 2); + Require(ControlRefused(context, instance, CONTROL_START, &start, sizeof(start) - sizeof(start.third))); + Require(sceAjmInstanceDestroy(context, instance) == 0); +} + } int main() { @@ -489,5 +641,8 @@ int main() { TestCodecInfo(context); TestBatchWaitRelease(context); TestBatchCancel(context); + TestControlAt9(context); + TestControlMp3(context); + TestControlOpus(context); Require(sceAjmFinalize(context) == 0); } diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 0fac8dd33..137685c65 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -108,6 +108,10 @@ Throughout the project, every function at every stage either **does exactly what - [sceAjmDecMp3ParseFrame](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - signature, result layout and rate tables taken from the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation of the PS4 library, not confirmed on a PS5 title: the layer field is not checked and MPEG-2.5 stops at 64 kbps; the original file length (`parse_ofl`) layout (Xing/Info + LAME tag, VBRI, Fraunhofer `0xB4` block with its CRC, encoder delay = samples per frame + LAME delay + 529) also comes from shadPS4 only, and the Fraunhofer block has no other public description - [sceAjmBatchJobGetCodecInfo, sceAjmBatchJobGetGaplessDecode](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - the `RUN_GET_CODEC_INFO` flag (0x800), the ATRAC9 codec info layout and its next frame size are taken from the [shadPS4](https://github.com/shadps4-emu/shadPS4) and [RPCSX](https://github.com/RPCSX/rpcsx) reimplementations, not confirmed on a PS5 title; a run job without buffers returns result 0, as in shadPS4. The gapless sideband reports the total and skip counts the title set and does not count them down: RPCSX does the same, shadPS4 counts them down and BryKytyPS5 returns its current counts - [AJM Opus decoder](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native, codec 24) - the initialize parameter layout (`u32 channels, u32 sample rate, u32`, seen as `2, 48000, 0`) and the little-endian `u16` byte count before each packet come from one title (The Smurfs Dreams); only 48000 Hz and a zero third word are accepted +- [sceAjmBatchJobControl](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - RESET (bit 13), INITIALIZE (bit 14) and the sideband input order (gapless decode, then initialize parameters) come from [shadPS4](https://github.com/shadps4-emu/shadPS4) (PS4); other flags throw +- [sceAjmBatchJobControl](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - RESET is assumed to clear the context as sceAjmBatchJobClearContext does +- [sceAjmBatchJobControl](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - initialize parameters are 8 bytes for ATRAC9, 12 for Opus and none for MP3, as one title passes them; other codecs and sizes, and an output other than the 8-byte result, throw +- [sceAjmBatchJobControl](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - SIDEBAND_GAPLESS_DECODE with RESET overwrites both counts as in [RPCSX](https://github.com/RPCSX/rpcsx); shadPS4 ignores a zero count when the other is nonzero. Without RESET they differ, so it throws - [NGS2 custom submixer rack](../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native, rack 0x4002) - structure layouts from [shadPS4](https://github.com/shadps4-emu/shadPS4)'s `ngs2_custom.h`; module id 0x1f for UserFx2, the voice parameter id `0x40001f00 | module index`, the process flags (1 on the first process call after setup, 2 after a parameter change, 0 otherwise) and the call order (setup per voice at rack creation, process after the inputs are mixed, cleanup at destruction) come only from [KytyPS5](https://github.com/KytyPS5/KytyPS5). Only UserFx2 modules on a single buffer are accepted: other module ids, more buffers, a module control handler, different input and output channel counts, setup flags and default rack options throw. Each module's state is a separate buffer, not the `state_offset` slice of the voice state, and `sceNgs2VoiceGetState` still throws for this rack. As in KytyPS5, process runs only on grains where an input produced samples, so an effect tail stops with the input and a pending flag 2 waits for the next audible grain - [sceHttpParseResponseHeader](../../core/libs/prx/libSceHttp/Export.cpp) (libSceHttp) - signature follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Net/Http.cs); parsing and error codes follow [shadPS4's reverse-engineered PS4 library](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/http.cpp), not confirmed on a PS5 title or hardware. Folded values retain their line breaks and whitespace; an empty first line starts the value scan after its line feed. A consumed byte count above `INT_MAX` throws instead of wrapping. - [sceHttpParseStatusLine](../../core/libs/prx/libSceHttp/src/Parse.cpp) (libSceHttp) - behaviour and error codes follow the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation of the PS4 library, not confirmed on a PS5 title; unlike it, no byte past `lineLen` is read