Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions core/libs/prx/libSceAjm.native/src/Ajm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ struct Instance {
std::uint32_t superframeRemaining = 0;
std::uint32_t frameInSuperframe = 0;
std::uint64_t totalDecodedSamples = 0;
std::uint64_t segmentSamples = 0;
SidebandGaplessDecode gapless{};
bool flagsReported = false;
AVCodecContext* mp3 = nullptr;
Expand Down Expand Up @@ -375,6 +376,7 @@ std::int32_t InitializeInstance(Instance& instance, const std::uint8_t* paramete
instance.opusPending.clear();
instance.initialized = true;
instance.totalDecodedSamples = 0;
instance.segmentSamples = 0;
instance.gapless = {};
return 0;
}
Expand All @@ -392,6 +394,7 @@ std::int32_t InitializeInstance(Instance& instance, const std::uint8_t* paramete
}
instance.initialized = true;
instance.totalDecodedSamples = 0;
instance.segmentSamples = 0;
instance.gapless = {};
return 0;
}
Expand Down Expand Up @@ -545,9 +548,10 @@ void EmitFrame(Instance& instance, PcmOutputs& outputs, const std::uint8_t* pcm,
count -= skip;
}
if (instance.gapless.totalSamples != 0) {
const std::uint64_t remaining = instance.gapless.totalSamples > instance.totalDecodedSamples ? instance.gapless.totalSamples - instance.totalDecodedSamples : 0;
const std::uint64_t remaining = instance.gapless.totalSamples > instance.segmentSamples ? instance.gapless.totalSamples - instance.segmentSamples : 0;
count = static_cast<std::size_t>(std::min<std::uint64_t>(count, remaining));
}
instance.segmentSamples += count;
instance.totalDecodedSamples += count;
if (!Resampling(instance)) {
outputs.Emit(pcm + first * channels * sampleBytes, count * channels * sampleBytes);
Expand Down Expand Up @@ -627,7 +631,9 @@ void RunAt9(Instance& instance, const JobHeader& job, const AjmBuffer* inputs, c
const auto superframeSize = static_cast<std::size_t>(instance.info.superframeSize);
const auto framesInSuperframe = static_cast<std::uint32_t>(std::max(1, instance.info.framesInSuperframe));
if (Resampling(instance)) ResamplerProduce(instance, pcmOutputs, channels, encoding, sampleBytes);
const auto segmentEnded = [&] { return instance.gapless.totalSamples != 0 && instance.segmentSamples >= instance.gapless.totalSamples; };
for (;;) {
if (segmentEnded()) break;
if (instance.superframeRemaining == 0) consumed += RiffDataOffset(input.data() + consumed, input.size() - consumed);
const std::size_t needed = instance.superframeRemaining == 0 ? superframeSize : instance.superframeRemaining;
if (input.size() - consumed < needed) {
Expand Down Expand Up @@ -659,8 +665,8 @@ void RunAt9(Instance& instance, const JobHeader& job, const AjmBuffer* inputs, c
++frames;

EmitFrame(instance, pcmOutputs, pcm.data(), frameSamples, channels, sampleBytes);
if ((job.flags & RUN_MULTIPLE_FRAMES) == 0) break;
}
if (segmentEnded()) instance.segmentSamples = 0;

if (TraceEnabled()) {
std::fprintf(stderr, "[ajm] instance %u run flags 0x%llx: %zu input bytes in %u buffers, %zu output bytes in %u buffers, sideband %llu bytes; superframe %d (%d frames), %d channels, %d samples/frame, %d Hz, config %02x %02x %02x %02x -> result 0x%x, decoder status 0x%x, %u frames, consumed %zu, produced %zu, total samples %llu, gapless total %u skip %u skipped %u, input",
Expand Down Expand Up @@ -1059,6 +1065,7 @@ void RunOpus(Instance& instance, const JobHeader& job, const AjmBuffer* inputs,

void ClearContext(Instance& instance) {
instance.totalDecodedSamples = 0;
instance.segmentSamples = 0;
instance.gapless.skippedSamples = 0;
if (HasAt9Decoder(instance) && instance.initialized) ResetDecoder(instance);
if (instance.mp3) avcodec_flush_buffers(instance.mp3);
Expand Down Expand Up @@ -1133,7 +1140,10 @@ void Execute(const JobHeader& job, const AjmBuffer* inputs, const AjmBuffer* out
AJM_TRACE("[ajm] instance %u set gapless decode: total %u, skip %u, reset %llu (sideband %llu bytes)\n", job.instance, gapless.totalSamples, gapless.skipSamples, static_cast<unsigned long long>(job.flags), static_cast<unsigned long long>(job.sidebandSize));
instance->gapless.totalSamples = gapless.totalSamples;
instance->gapless.skipSamples = gapless.skipSamples;
if (job.flags) instance->gapless.skippedSamples = 0;
if (job.flags) {
instance->gapless.skippedSamples = 0;
instance->segmentSamples = 0;
}
WriteResult(job.sideband, job.sidebandSize, 0);
break;
}
Expand Down
57 changes: 57 additions & 0 deletions core/libs/tests/GuestAjm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,61 @@ void TestResampleAt9(std::uint32_t context) {
for (const std::uint32_t instance : {plain, faster, slower}) Require(sceAjmInstanceDestroy(context, instance) == 0);
}

struct At9Run {
DecodeSideband stream;
std::uint32_t frames;
std::uint32_t reserved;
};

At9Run RunAt9Job(std::uint32_t context, std::uint32_t instance, std::uint64_t flags, const std::uint8_t* input, std::size_t inputSize) {
constexpr std::uint64_t runMultipleFrames = 1ull << 12;
constexpr std::uint64_t sidebandStream = 1ull << 47;
std::vector<std::uint8_t> batch(4096);
std::vector<std::int16_t> pcm(1024);
At9Run decoded{{-1, -1, 0, 0, 0}, 0, 0};
AjmBatchInfo info{};
Require(sceAjmBatchInitialize(batch.data(), batch.size(), &info) == 0);
const std::size_t sidebandSize = (flags & runMultipleFrames) != 0 ? sizeof(decoded) : sizeof(decoded.stream);
Require(sceAjmBatchJobRun(&info, instance, sidebandStream | flags, input, inputSize, pcm.data(), pcm.size() * sizeof(std::int16_t), &decoded, sidebandSize) == 0);
Submit(context, info);
Require(decoded.stream.result == 0);
return decoded;
}

void TestAt9GaplessSegments(std::uint32_t context) {
constexpr std::uint64_t runMultipleFrames = 1ull << 12;
std::vector<std::uint8_t> stream(1024, 0);
std::memcpy(stream.data(), AT9_MONO_SILENT_SUPERFRAME, sizeof(AT9_MONO_SILENT_SUPERFRAME));
std::uint32_t instance = 0;
Require(sceAjmInstanceCreate(context, 1, 0, &instance) == 0);
const At9Control start{{512, 0, 0}, {0xFE, 0x70, 0x1F, 0xF0}, 0};
std::int32_t result[2] = {-1, -1};
RunControl(context, instance, CONTROL_START, &start, sizeof(start), result);
Require(result[0] == 0);
const auto first = RunAt9Job(context, instance, runMultipleFrames, stream.data(), stream.size());
Require(first.stream.outputWritten == 512 * 2 && first.frames == 2);
Require(first.stream.inputConsumed > 0 && static_cast<std::size_t>(first.stream.inputConsumed) < stream.size());
const auto consumed = static_cast<std::size_t>(first.stream.inputConsumed);
const auto second = RunAt9Job(context, instance, runMultipleFrames, stream.data() + consumed, stream.size() - consumed);
Require(second.stream.outputWritten == 512 * 2 && second.frames == 2);
Require(consumed + static_cast<std::size_t>(second.stream.inputConsumed) == stream.size());
Require(sceAjmInstanceDestroy(context, instance) == 0);
}

void TestAt9RunDecodesWholeInput(std::uint32_t context) {
std::vector<std::uint8_t> stream(1024, 0);
std::memcpy(stream.data(), AT9_MONO_SILENT_SUPERFRAME, sizeof(AT9_MONO_SILENT_SUPERFRAME));
std::uint32_t instance = 0;
Require(sceAjmInstanceCreate(context, 1, 0, &instance) == 0);
const At9Control start{{0, 0, 0}, {0xFE, 0x70, 0x1F, 0xF0}, 0};
std::int32_t result[2] = {-1, -1};
RunControl(context, instance, CONTROL_START, &start, sizeof(start), result);
Require(result[0] == 0);
const auto run = RunAt9Job(context, instance, 0, stream.data(), stream.size());
Require(static_cast<std::size_t>(run.stream.inputConsumed) == stream.size() && run.stream.outputWritten == 1024 * 2);
Require(sceAjmInstanceDestroy(context, instance) == 0);
}

}

int main() {
Expand Down Expand Up @@ -988,5 +1043,7 @@ int main() {
TestResampleOpus(context);
TestResampleMp3(context);
TestResampleAt9(context);
TestAt9GaplessSegments(context);
TestAt9RunDecodesWholeInput(context);
Require(sceAjmFinalize(context) == 0);
}
1 change: 1 addition & 0 deletions docs/dev/TechnicalDebt.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ Throughout the project, every function at every stage either **does exactly what
- [AudioOut2 contexts](../../core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) (libSceAudioOut) with an SDL device read a grain's port buffers at the next push, at least 100 us after the grain was pushed, and play the mix behind a 40 ms cushion that the queue level leaves out. When the hardware reads the buffers is not known. The timing follows a title whose mixer fills a buffer after pushing it, reuses it two pushes later, and may still be writing it when the next push follows within microseconds. The last grain pushed before a title stops pushing is never read or played.
- [libSceAudiodec](../../core/libs/prx/libSceAudiodec/Export.cpp) throws for the 24-bit PCM word size (`iBwPcm` 0): its sample layout is unknown. ATRAC9 decoding is covered only by configuration and error tests, as no ATRAC9 encoder is available for a fixture.
- [sceAjmDecMp3ParseFrame](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) throws when the original file length is requested (`parse_ofl` non-zero) for a layer other than III, for a CRC-protected frame, or for a VBRI header outside MPEG-1 stereo: shadPS4 looks for the tag after the CRC and after the side information, while LAME and FFmpeg place it without the CRC and VBRI always at byte 36.
- [ATRAC9 gapless segments](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - a run job stops after the gapless total of output samples counted from the start of the segment, leaving the rest of its input unconsumed, and the next run starts the next segment with the decoder as it is and without a second skip. FMOD in PPSA29141 sets a total shorter than a sound's frames and reads the frames after it on later runs; with the output held at the total its stream reader waited for them forever and the last buffer repeated. The console's handling of the frames after the total is not documented; a reset (`SetGaplessDecode` with the reset flag, `ClearContext`, initialization) starts a new segment. An ATRAC9 run job without `RUN_MULTIPLE_FRAMES` decodes its whole input as one with it: FMOD in PPSA29141 puts two run jobs in one batch with the second's input right after the whole of the first's. Whether the console stops after one frame there is not verified; MP3 and Opus run jobs still stop after one frame
- The [output modifier](../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) (`mul:2`, `mul:4`, `div:2`) follows the shader's float mode as measured on RDNA2 (gfx1035): the hardware applies it only with IEEE mode off and output denormals of the result's precision flushed (bit 1 of the `FLOAT_MODE` denormal field clear), and ignores it otherwise. On f32 results it is applied as the hardware does with f32 denormals flushed; a shader without a known float mode is translated with IEEE mode off and f32 denormals flushed, the mode titles use. On f16 and f64 results it is ignored, and it throws in the modes where the hardware applies it.
- The [clamp modifier](../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) follows `DX10_CLAMP`, measured on RDNA2 (gfx1035) for f32, f16, packed f16 and f64 results at IEEE=0 and IEEE=1: with `DX10_CLAMP`=1 (and in shaders without a known float mode) a NaN result clamps to +0; with `DX10_CLAMP`=0 a NaN result is written unchanged and every other value clamps to [0, 1] as before. f64 results follow the same rule on hardware, but a clamped f64 result with `DX10_CLAMP`=0 throws until it has an execution test. With `DX10_CLAMP`=0 the NaN's bits are those the translation already produces for the instruction without clamp, so where those are left to the driver (f32 and scalar f16 arithmetic) the payload is the driver's, not the hardware's input propagation. `v_dot2_f32_f16` and the integer clamps are unchanged.
- [Comparison sampling of a color texture](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (`image_sample_c`/`image_sample_c_lz` on a texture that is not R32 float or R16 unorm) is emulated in the shader, since Vulkan compares only depth formats: the red channel is fetched, compared and, for bilinear filtering, blended (compare, then filter). Only 2D and 2D array views, float/unorm/snorm formats, wrap, clamp-to-edge and clamp-to-border addressing (border red 0 for the black borders, 1 for opaque white) and point or bilinear filtering at one level are implemented, with texel offsets added to the texel indices; LOD biases and clamps are accepted, as they cannot select another level of a single-level sample. Half-border addressing, the border color table, gradients, gathers, other dimensions, anisotropy and mip filtering throw. Under bilinear filtering, taps past a clamp-to-border edge compare the border value and blend with the texel taps, so results near the edge depend on the filter weights. AMD documentation does not say how a color format's reference is handled: it is clamped to [0, 1] for unorm and [-1, 1] for snorm, as Vulkan does for unorm depth, and not clamped for float. Bilinear weights are full float, not the hardware's fixed-point subtexel weights
Expand Down
Loading