mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(ngs2): decode ATRAC9 across streamed waveform blocks
Keep logical sample and skip counts across byte-only continuation buffers, assemble split superframes, and allow callbacks to refill, pause or reset the stream. Preserve decoded audio while waiting for input and reject truncated closed streams. Validated with seven NGS2 tests, an ASan/UBSan streaming test build, and PPSA03747 repeatedly supplying continuation buffers during playback. Flag semantics follow the pinned KytyPS5 source documented in TechnicalDebt.md. Written with AI assistance.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
@@ -67,42 +68,134 @@ void Ngs2RestartAtrac9(Ngs2Voice& voice) {
|
||||
atrac9.decoder.reset(Atrac9GetHandle());
|
||||
if (atrac9.decoder == nullptr || Atrac9InitDecoder(atrac9.decoder.get(), atrac9.config) != 0) throw std::runtime_error("NGS2: ATRAC9 decoder init failed");
|
||||
atrac9.window.clear();
|
||||
atrac9.windowStart = 0;
|
||||
atrac9.windowCursor = 0;
|
||||
atrac9.input.clear();
|
||||
atrac9.remainingSamples = 0;
|
||||
atrac9.skipSamples = 0;
|
||||
atrac9.finishOnDrain = false;
|
||||
}
|
||||
|
||||
static void DecodeSuperframe(Ngs2Voice& voice, Ngs2Block& block) {
|
||||
static std::size_t BufferedFrames(const Ngs2Voice& voice) {
|
||||
return voice.atrac9.window.size() / voice.channels - voice.atrac9.windowCursor;
|
||||
}
|
||||
|
||||
static bool GatherSuperframe(Ngs2Voice& voice) {
|
||||
auto& atrac9 = voice.atrac9;
|
||||
if (block.dataCursor + atrac9.superframeBytes > block.info.data_size) throw std::invalid_argument("NGS2: the ATRAC9 block ends inside a superframe");
|
||||
while (atrac9.input.size() < atrac9.superframeBytes) {
|
||||
if (voice.blocks.empty()) {
|
||||
if (!voice.acceptsBlocks && atrac9.remainingSamples != 0)
|
||||
throw std::invalid_argument("NGS2: the ATRAC9 stream ends before its declared samples");
|
||||
return false;
|
||||
}
|
||||
auto& block = voice.blocks.front();
|
||||
if (!block.started) {
|
||||
if (block.info.num_samples != 0) {
|
||||
if (atrac9.remainingSamples != 0 || !atrac9.input.empty())
|
||||
throw std::invalid_argument("NGS2: a new ATRAC9 waveform interrupts an incomplete stream");
|
||||
atrac9.remainingSamples = block.info.num_samples;
|
||||
atrac9.skipSamples = block.info.num_skip_samples;
|
||||
} else if (atrac9.remainingSamples == 0) {
|
||||
throw std::invalid_argument("NGS2: an ATRAC9 data-only block has no waveform to continue");
|
||||
}
|
||||
block.started = true;
|
||||
}
|
||||
const auto count = std::min<std::size_t>(atrac9.superframeBytes - atrac9.input.size(), block.info.data_size - block.dataCursor);
|
||||
atrac9.input.insert(atrac9.input.end(), block.data + block.dataCursor, block.data + block.dataCursor + count);
|
||||
block.dataCursor += count;
|
||||
voice.decodedBytes += count;
|
||||
if (atrac9.input.size() == atrac9.superframeBytes) return true;
|
||||
if (!Ngs2FinishBlock(voice)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool DecodeSuperframe(Ngs2Voice& voice) {
|
||||
if (!GatherSuperframe(voice)) return false;
|
||||
auto& atrac9 = voice.atrac9;
|
||||
if (atrac9.windowCursor != 0) {
|
||||
atrac9.window.erase(atrac9.window.begin(), atrac9.window.begin() + static_cast<std::ptrdiff_t>(atrac9.windowCursor) * voice.channels);
|
||||
atrac9.windowCursor = 0;
|
||||
}
|
||||
const std::size_t frameValues = static_cast<std::size_t>(atrac9.frameSamples) * voice.channels;
|
||||
const std::size_t superframeValues = frameValues * atrac9.framesInSuperframe;
|
||||
if (atrac9.window.size() >= 2 * superframeValues) {
|
||||
atrac9.window.erase(atrac9.window.begin(), atrac9.window.begin() + static_cast<std::ptrdiff_t>(superframeValues));
|
||||
atrac9.windowStart += atrac9.frameSamples * atrac9.framesInSuperframe;
|
||||
}
|
||||
std::size_t end = atrac9.window.size();
|
||||
atrac9.window.resize(end + superframeValues);
|
||||
const auto* superframe = block.data + block.dataCursor;
|
||||
const auto superframeSamples = atrac9.frameSamples * atrac9.framesInSuperframe;
|
||||
const auto end = atrac9.window.size();
|
||||
atrac9.window.resize(end + static_cast<std::size_t>(superframeSamples) * voice.channels);
|
||||
std::size_t consumed = 0;
|
||||
for (std::uint32_t frame = 0; frame < atrac9.framesInSuperframe; frame++, end += frameValues) {
|
||||
for (std::uint32_t frame = 0; frame < atrac9.framesInSuperframe; ++frame) {
|
||||
int used = 0;
|
||||
const int status = Atrac9DecodeF32(atrac9.decoder.get(), superframe + consumed, static_cast<int>(atrac9.superframeBytes - consumed), atrac9.window.data() + end, &used, 0);
|
||||
consumed += static_cast<std::size_t>(std::max(used, 0));
|
||||
if (status != 0 || consumed > atrac9.superframeBytes) throw std::runtime_error("NGS2: ATRAC9 decode failed with " + Ngs2Hex(static_cast<std::uint32_t>(status)));
|
||||
const int status = Atrac9DecodeF32(atrac9.decoder.get(), atrac9.input.data() + consumed,
|
||||
static_cast<int>(atrac9.superframeBytes - consumed), atrac9.window.data() + end + frame * frameValues, &used, 0);
|
||||
if (status != 0 || used <= 0 || static_cast<std::size_t>(used) > atrac9.superframeBytes - consumed)
|
||||
throw std::runtime_error("NGS2: ATRAC9 decode failed with " + Ngs2Hex(static_cast<std::uint32_t>(status)));
|
||||
consumed += static_cast<std::size_t>(used);
|
||||
}
|
||||
block.dataCursor += atrac9.superframeBytes;
|
||||
voice.decodedBytes += atrac9.superframeBytes;
|
||||
atrac9.input.clear();
|
||||
const auto skipped = std::min(atrac9.skipSamples, superframeSamples);
|
||||
const auto samples = std::min(atrac9.remainingSamples, superframeSamples - skipped);
|
||||
atrac9.skipSamples -= skipped;
|
||||
atrac9.remainingSamples -= samples;
|
||||
std::memmove(atrac9.window.data() + end, atrac9.window.data() + end + static_cast<std::size_t>(skipped) * voice.channels,
|
||||
static_cast<std::size_t>(samples) * voice.channels * sizeof(float));
|
||||
atrac9.window.resize(end + static_cast<std::size_t>(samples) * voice.channels);
|
||||
if (atrac9.remainingSamples == 0) {
|
||||
atrac9.finishOnDrain = true;
|
||||
} else if (voice.blocks.front().dataCursor == voice.blocks.front().info.data_size) {
|
||||
if (!Ngs2FinishBlock(voice)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const float* Ngs2Atrac9Frame(Ngs2Voice& voice, Ngs2Block& block, std::uint32_t frame) {
|
||||
static bool EnsureFrames(Ngs2Voice& voice, std::size_t count) {
|
||||
auto& atrac9 = voice.atrac9;
|
||||
const std::uint32_t position = block.info.num_skip_samples + frame;
|
||||
if (block.dataCursor == 0) {
|
||||
atrac9.window.clear();
|
||||
atrac9.windowStart = 0;
|
||||
while (voice.state == Ngs2PlayState::Playing && BufferedFrames(voice) < count) {
|
||||
if (atrac9.finishOnDrain) {
|
||||
if (BufferedFrames(voice) != 0) return false;
|
||||
atrac9.finishOnDrain = false;
|
||||
if (!Ngs2FinishBlock(voice)) return false;
|
||||
}
|
||||
if (!DecodeSuperframe(voice)) return false;
|
||||
}
|
||||
if (position < atrac9.windowStart) throw std::logic_error("NGS2: ATRAC9 frames are read backwards");
|
||||
while (position >= atrac9.windowStart + atrac9.window.size() / voice.channels) DecodeSuperframe(voice, block);
|
||||
return atrac9.window.data() + static_cast<std::size_t>(position - atrac9.windowStart) * voice.channels;
|
||||
return voice.state == Ngs2PlayState::Playing;
|
||||
}
|
||||
|
||||
static bool AdvanceAtrac9(Ngs2Voice& voice) {
|
||||
while ((voice.phase >> 32) != 0 && voice.state == Ngs2PlayState::Playing) {
|
||||
if (!EnsureFrames(voice, 1)) return false;
|
||||
const auto count = std::min<std::uint64_t>(voice.phase >> 32, BufferedFrames(voice));
|
||||
voice.atrac9.windowCursor += static_cast<std::uint32_t>(count);
|
||||
voice.decodedSamples += count;
|
||||
voice.phase -= count << 32;
|
||||
if (BufferedFrames(voice) == 0 && voice.atrac9.finishOnDrain) {
|
||||
voice.atrac9.finishOnDrain = false;
|
||||
if (!Ngs2FinishBlock(voice)) return false;
|
||||
}
|
||||
}
|
||||
return voice.state == Ngs2PlayState::Playing;
|
||||
}
|
||||
|
||||
void Ngs2ConsumeAtrac9(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t systemRate) {
|
||||
constexpr double phaseOne = 4294967296.0;
|
||||
const auto initialRevision = voice.waveformRevision;
|
||||
const auto step = static_cast<std::uint64_t>(std::llround(voice.sampleRate * static_cast<double>(voice.pitch) / systemRate * phaseOne));
|
||||
std::uint32_t written = 0;
|
||||
for (; written < grain; ++written) {
|
||||
if (!AdvanceAtrac9(voice) || !EnsureFrames(voice, 1)) break;
|
||||
const auto revision = voice.waveformRevision;
|
||||
const auto fraction = static_cast<float>(voice.phase / phaseOne);
|
||||
if (fraction != 0.0f) EnsureFrames(voice, 2);
|
||||
if (voice.state != Ngs2PlayState::Playing || voice.waveformRevision != revision) break;
|
||||
const auto* current = voice.atrac9.window.data() + static_cast<std::size_t>(voice.atrac9.windowCursor) * voice.channels;
|
||||
const auto* next = BufferedFrames(voice) > 1 ? current + voice.channels : current;
|
||||
for (std::uint32_t channel = 0; channel < voice.channels; ++channel)
|
||||
voice.samples[static_cast<std::size_t>(channel) * grain + written] = std::lerp(current[channel], next[channel], fraction);
|
||||
voice.phase += step;
|
||||
}
|
||||
voice.hasSamples = written != 0;
|
||||
if (voice.state != Ngs2PlayState::Playing || voice.waveformRevision != initialRevision) return;
|
||||
AdvanceAtrac9(voice);
|
||||
if (voice.state != Ngs2PlayState::Playing || voice.waveformRevision != initialRevision) return;
|
||||
if (!voice.acceptsBlocks && voice.blocks.empty() && BufferedFrames(voice) == 0 && voice.atrac9.remainingSamples != 0)
|
||||
throw std::invalid_argument("NGS2: the ATRAC9 stream ends before its declared samples");
|
||||
}
|
||||
|
||||
static std::uint16_t ReadLe16(const std::uint8_t* data) {
|
||||
|
||||
@@ -41,6 +41,7 @@ struct Ngs2Block {
|
||||
bool streaming = false;
|
||||
std::uint64_t availableFrames = 0;
|
||||
std::vector<Ngs2Piece> pieces;
|
||||
bool started = false;
|
||||
};
|
||||
|
||||
struct Ngs2Atrac9DecoderDeleter {
|
||||
@@ -54,7 +55,11 @@ struct Ngs2Atrac9 {
|
||||
std::uint32_t framesInSuperframe = 0;
|
||||
std::uint32_t superframeBytes = 0;
|
||||
std::vector<float> window;
|
||||
std::uint32_t windowStart = 0;
|
||||
std::uint32_t windowCursor = 0;
|
||||
std::vector<std::uint8_t> input;
|
||||
std::uint32_t remainingSamples = 0;
|
||||
std::uint32_t skipSamples = 0;
|
||||
bool finishOnDrain = false;
|
||||
};
|
||||
|
||||
struct Ngs2ReverbState;
|
||||
@@ -111,6 +116,7 @@ struct Ngs2Voice {
|
||||
bool acceptsBlocks = true;
|
||||
std::uint64_t decodedSamples = 0;
|
||||
std::uint64_t decodedBytes = 0;
|
||||
std::uint64_t waveformRevision = 0;
|
||||
const std::uint8_t* waveformEnd = nullptr;
|
||||
Ngs2VoiceCallbackHandler callback = nullptr;
|
||||
std::uintptr_t callbackData = 0;
|
||||
@@ -181,7 +187,8 @@ int Ngs2ReleaseBuffer(const Ngs2BufferAllocator& allocator, Ngs2ContextBufferInf
|
||||
void Ngs2SetupAtrac9(Ngs2Voice& voice, const Ngs2WaveformFormat& format);
|
||||
std::size_t Ngs2Atrac9BlockBytes(const Ngs2Voice& voice, const Ngs2WaveformBlock& block);
|
||||
void Ngs2RestartAtrac9(Ngs2Voice& voice);
|
||||
const float* Ngs2Atrac9Frame(Ngs2Voice& voice, Ngs2Block& block, std::uint32_t frame);
|
||||
void Ngs2ConsumeAtrac9(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t systemRate);
|
||||
bool Ngs2FinishBlock(Ngs2Voice& voice);
|
||||
void Ngs2CheckCustomRack(const Ngs2CustomRackOption& option);
|
||||
void Ngs2SetupUserFx(Ngs2Rack& rack, const Ngs2CustomRackOption& option);
|
||||
void Ngs2CleanupUserFx(Ngs2Rack& rack);
|
||||
|
||||
@@ -38,12 +38,6 @@ static void ReadPcm(const Ngs2Voice& voice, const std::uint8_t* frame, float* ou
|
||||
|
||||
static void ReadFrames(Ngs2Voice& voice, float* current, float* next) {
|
||||
auto& block = voice.blocks.front();
|
||||
if (voice.waveformType == SCE_NGS2_WAVEFORM_TYPE_ATRAC9) {
|
||||
std::memcpy(current, Ngs2Atrac9Frame(voice, block, block.cursor), voice.channels * sizeof(float));
|
||||
if (block.cursor + 1 < block.info.num_samples) std::memcpy(next, Ngs2Atrac9Frame(voice, block, block.cursor + 1), voice.channels * sizeof(float));
|
||||
else std::memcpy(next, current, voice.channels * sizeof(float));
|
||||
return;
|
||||
}
|
||||
ReadPcm(voice, FrameAt(voice, block, block.cursor), current);
|
||||
if (block.streaming && block.cursor + 1 >= block.availableFrames) std::memcpy(next, current, voice.channels * sizeof(float));
|
||||
else if (block.cursor + 1 < block.info.num_samples) ReadPcm(voice, FrameAt(voice, block, block.cursor + 1), next);
|
||||
@@ -52,13 +46,14 @@ static void ReadFrames(Ngs2Voice& voice, float* current, float* next) {
|
||||
else std::memcpy(next, current, voice.channels * sizeof(float));
|
||||
}
|
||||
|
||||
static void FinishBlock(Ngs2Voice& voice) {
|
||||
bool Ngs2FinishBlock(Ngs2Voice& voice) {
|
||||
auto& block = voice.blocks.front();
|
||||
const bool repeat = block.info.num_repeats != 0;
|
||||
if (repeat) {
|
||||
if (block.info.num_repeats != UINT32_MAX) block.info.num_repeats--;
|
||||
block.numRepeated++;
|
||||
block.cursor = 0;
|
||||
block.started = false;
|
||||
if (voice.waveformType == SCE_NGS2_WAVEFORM_TYPE_ATRAC9) {
|
||||
block.dataCursor = 0;
|
||||
Ngs2RestartAtrac9(voice);
|
||||
@@ -80,7 +75,9 @@ static void FinishBlock(Ngs2Voice& voice) {
|
||||
voice.waveformEnd = voice.waveformType == SCE_NGS2_WAVEFORM_TYPE_ATRAC9 ? block.data + block.dataCursor : block.streaming ? Ngs2StreamEnd(voice, block) : FrameAt(voice, block, block.info.num_samples);
|
||||
voice.blocks.pop_front();
|
||||
}
|
||||
const auto revision = voice.waveformRevision;
|
||||
if (voice.callback != nullptr && (voice.callbackFlags & info.flag) != 0) voice.callback(&info);
|
||||
return voice.waveformRevision == revision && voice.state == Ngs2PlayState::Playing;
|
||||
}
|
||||
|
||||
static void Advance(Ngs2Voice& voice, std::uint64_t frames) {
|
||||
@@ -96,7 +93,7 @@ static void Advance(Ngs2Voice& voice, std::uint64_t frames) {
|
||||
if (block.streaming) {
|
||||
while (block.pieces.size() > 1 && block.cursor >= block.pieces.front().firstFrame + block.pieces.front().frames) block.pieces.erase(block.pieces.begin());
|
||||
}
|
||||
if (block.cursor == block.info.num_samples || (block.streaming && !voice.acceptsBlocks && block.cursor == block.availableFrames)) FinishBlock(voice);
|
||||
if (block.cursor == block.info.num_samples || (block.streaming && !voice.acceptsBlocks && block.cursor == block.availableFrames)) Ngs2FinishBlock(voice);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,13 +144,15 @@ static void ApplyFilter(Ngs2Voice& voice, Ngs2Filter& filter, std::uint32_t grai
|
||||
}
|
||||
|
||||
static void RenderSampler(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t systemRate) {
|
||||
ConsumeSamples(voice, grain, systemRate);
|
||||
if (voice.waveformType == SCE_NGS2_WAVEFORM_TYPE_ATRAC9) Ngs2ConsumeAtrac9(voice, grain, systemRate);
|
||||
else ConsumeSamples(voice, grain, systemRate);
|
||||
for (auto& filter : voice.filters) {
|
||||
if (!filter.enabled || (!voice.hasSamples && !HasHistory(filter))) continue;
|
||||
ApplyFilter(voice, filter, grain);
|
||||
voice.hasSamples = true;
|
||||
}
|
||||
if (voice.state == Ngs2PlayState::Playing && voice.blocks.empty() && !voice.acceptsBlocks && std::none_of(voice.filters.begin(), voice.filters.end(), HasHistory)) {
|
||||
const bool buffered = voice.waveformType == SCE_NGS2_WAVEFORM_TYPE_ATRAC9 && voice.atrac9.windowCursor * voice.channels < voice.atrac9.window.size();
|
||||
if (voice.state == Ngs2PlayState::Playing && voice.blocks.empty() && !buffered && !voice.acceptsBlocks && std::none_of(voice.filters.begin(), voice.filters.end(), HasHistory)) {
|
||||
voice.state = Ngs2PlayState::Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ void Ngs2Voice::SetEvent(std::uint32_t eventId) {
|
||||
}
|
||||
|
||||
void Ngs2Voice::ResetSetup() {
|
||||
++waveformRevision;
|
||||
SetEvent(SCE_NGS2_VOICE_EVENT_STOP_IMM);
|
||||
std::fill(ports.begin(), ports.end(), Ngs2Port{});
|
||||
for (auto& matrix : matrices) matrix.clear();
|
||||
@@ -170,6 +171,20 @@ static void SetupSampler(Ngs2Voice& voice, const Ngs2WaveformFormat& format) {
|
||||
}
|
||||
|
||||
static void AppendStreamData(Ngs2Voice& voice, const Ngs2SamplerVoiceWaveformBlocksParam& param, std::size_t frameBytes) {
|
||||
if (voice.waveformType == SCE_NGS2_WAVEFORM_TYPE_ATRAC9) {
|
||||
const bool pendingWaveform = voice.atrac9.remainingSamples != 0 ||
|
||||
std::any_of(voice.blocks.begin(), voice.blocks.end(), [](const Ngs2Block& block) { return block.info.num_samples != 0; });
|
||||
if (!pendingWaveform) throw std::invalid_argument("NGS2: appending compressed data requires a waveform");
|
||||
for (std::uint32_t i = 0; i < param.num_blocks; ++i) {
|
||||
auto block = param.blocks[i];
|
||||
if (block.data_size == 0) continue;
|
||||
block.num_samples = 0;
|
||||
block.num_skip_samples = 0;
|
||||
block.num_repeats = 0;
|
||||
voice.blocks.push_back({static_cast<const std::uint8_t*>(param.data) + block.data_offset, block});
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (voice.blocks.empty() || !voice.blocks.back().streaming) throw std::runtime_error("NGS2: appending waveform data to a voice without a streaming waveform is not implemented");
|
||||
auto& stream = voice.blocks.back();
|
||||
for (std::uint32_t i = 0; i < param.num_blocks; i++) {
|
||||
@@ -190,6 +205,7 @@ static void AddWaveformBlocks(Ngs2Voice& voice, const Ngs2SamplerVoiceWaveformBl
|
||||
const bool reset = (param.flags & SCE_NGS2_WAVEFORM_BLOCKS_FLAG_RESET) != 0;
|
||||
if (!voice.acceptsBlocks && !reset) throw std::invalid_argument("NGS2: the voice waveform was already closed");
|
||||
if (reset) {
|
||||
++voice.waveformRevision;
|
||||
voice.blocks.clear();
|
||||
voice.phase = 0;
|
||||
voice.waveformEnd = nullptr;
|
||||
@@ -197,7 +213,6 @@ static void AddWaveformBlocks(Ngs2Voice& voice, const Ngs2SamplerVoiceWaveformBl
|
||||
}
|
||||
const std::size_t frameBytes = voice.channels * sizeof(std::int16_t);
|
||||
if ((param.flags & SCE_NGS2_WAVEFORM_BLOCKS_FLAG_APPEND) != 0) {
|
||||
if (voice.waveformType != SCE_NGS2_WAVEFORM_TYPE_PCM_I16L) throw std::runtime_error("NGS2: appending waveform data to a voice that is not 16-bit PCM is not implemented");
|
||||
AppendStreamData(voice, param, frameBytes);
|
||||
voice.acceptsBlocks = (param.flags & SCE_NGS2_WAVEFORM_BLOCKS_FLAG_CONTINUE) != 0;
|
||||
return;
|
||||
@@ -210,7 +225,8 @@ static void AddWaveformBlocks(Ngs2Voice& voice, const Ngs2SamplerVoiceWaveformBl
|
||||
: (static_cast<std::uint64_t>(block.num_skip_samples) + block.num_samples) * frameBytes;
|
||||
const bool streaming = block.num_samples != 0 && bytes > block.data_size && voice.waveformType == SCE_NGS2_WAVEFORM_TYPE_PCM_I16L &&
|
||||
block.num_skip_samples == 0 && block.num_repeats == 0 && block.data_size >= frameBytes && block.data_size % frameBytes == 0;
|
||||
if (!streaming && (block.num_samples == 0 || bytes > block.data_size)) {
|
||||
const bool compressedStream = voice.waveformType == SCE_NGS2_WAVEFORM_TYPE_ATRAC9 && voice.acceptsBlocks && block.num_repeats == 0;
|
||||
if (block.num_samples == 0 || (bytes > block.data_size && !streaming && !compressedStream)) {
|
||||
throw std::invalid_argument("NGS2: waveform block " + std::to_string(i) + " does not fit its data");
|
||||
}
|
||||
voice.blocks.push_back({static_cast<const std::uint8_t*>(param.data) + block.data_offset, block});
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
#include "libatrac9.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <stdexcept>
|
||||
@@ -334,6 +336,116 @@ static void TestCorruptSuperframeAtPageEnd() {
|
||||
Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK);
|
||||
}
|
||||
|
||||
static uintptr_t EmptySampler(uintptr_t system) {
|
||||
const auto voice = Voice(CreateRack(system, SCE_NGS2_RACK_ID_SAMPLER));
|
||||
Control(voice, SCE_NGS2_SAMPLER_VOICE_PARAM_SETUP, Ngs2SamplerVoiceSetupParam{{}, {SCE_NGS2_WAVEFORM_TYPE_ATRAC9, 1, 48000, Config, 0, 0}});
|
||||
Patch(voice, Mastering(system, 1));
|
||||
Event(voice, SCE_NGS2_VOICE_EVENT_PLAY);
|
||||
return voice;
|
||||
}
|
||||
|
||||
static void Queue(uintptr_t voice, const void* data, std::uint32_t bytes, std::uint32_t flags, std::uint32_t samples = 0, std::uint32_t skip = 0) {
|
||||
const Ngs2WaveformBlock block{0, bytes, 0, skip, samples, 0, bytes};
|
||||
Control(voice, SCE_NGS2_SAMPLER_VOICE_PARAM_ADD_WAVEFORM_BLOCKS, Ngs2SamplerVoiceWaveformBlocksParam{{}, data, flags, 1, &block});
|
||||
}
|
||||
|
||||
static void TestStreamPartitions() {
|
||||
const auto reference = Reference(2);
|
||||
std::vector<std::uint8_t> data(Superframe, Superframe + SuperframeBytes);
|
||||
data.insert(data.end(), Superframe, Superframe + SuperframeBytes);
|
||||
for (const auto skip : {100u, 1100u}) {
|
||||
for (const float pitch : {0.5f, 1.0f, 1.5f}) {
|
||||
const auto system = CreateSystem();
|
||||
const auto voice = EmptySampler(system);
|
||||
Control(voice, SCE_NGS2_VOICE_PARAM_CALLBACK,
|
||||
Ngs2VoiceCallbackParam{{}, OnBlock, 0, SCE_NGS2_VOICE_CALLBACK_FLAG_BLOCK_END, 0});
|
||||
Control(voice, SCE_NGS2_SAMPLER_VOICE_PARAM_PITCH, Ngs2SamplerVoicePitchParam{{}, pitch});
|
||||
callbackFlags.clear();
|
||||
const std::uint32_t sizes[]{1, 17, 61, 93, 340};
|
||||
std::uint32_t offset = 0;
|
||||
for (const auto size : sizes) {
|
||||
const auto flags = offset == 0 ? SCE_NGS2_WAVEFORM_BLOCKS_FLAG_CONTINUE
|
||||
: SCE_NGS2_WAVEFORM_BLOCKS_FLAG_APPEND | (offset + size == data.size() ? 0 : SCE_NGS2_WAVEFORM_BLOCKS_FLAG_CONTINUE);
|
||||
Queue(voice, data.data() + offset, size, flags, 800, skip);
|
||||
offset += size;
|
||||
}
|
||||
const auto output = Render(system, 1664);
|
||||
for (std::size_t i = 0; i < output.size(); ++i) {
|
||||
const float position = i * pitch;
|
||||
float expected = 0.0f;
|
||||
if (position < 800) {
|
||||
const auto index = static_cast<std::uint32_t>(position);
|
||||
const auto next = std::min(index + 1, 799u);
|
||||
const auto fraction = position - index;
|
||||
expected = reference[skip + index] * (1 - fraction) + reference[skip + next] * fraction;
|
||||
}
|
||||
Require(std::abs(output[i] - expected) < 1e-6f);
|
||||
}
|
||||
Require(callbackFlags.size() == 5 && Flags(voice) == 0);
|
||||
Ngs2SamplerVoiceState state{};
|
||||
Require(sceNgs2VoiceGetState(voice, &state.voice_state, sizeof(state)) == SCE_NGS2_OK);
|
||||
Require(state.num_decoded_samples == 800);
|
||||
Require(sceNgs2SystemDestroy(system, nullptr) == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RefillState { int calls = 0; bool pause = false; bool reset = false; };
|
||||
|
||||
static void APS5_VABI Refill(const Ngs2VoiceCallbackInfo* info) {
|
||||
auto& state = *reinterpret_cast<RefillState*>(info->callback_data);
|
||||
++state.calls;
|
||||
Require(info->flag == SCE_NGS2_VOICE_CALLBACK_FLAG_BLOCK_END);
|
||||
Require(info->user_data == info->block_size);
|
||||
if (state.calls != 1) return;
|
||||
Require(info->block_size == 13 && info->block_data == Superframe);
|
||||
if (state.reset) Queue(info->voice_handle, Superframe, SuperframeBytes, SCE_NGS2_WAVEFORM_BLOCKS_FLAG_RESET, 500, 100);
|
||||
else Queue(info->voice_handle, Superframe + 13, SuperframeBytes - 13, SCE_NGS2_WAVEFORM_BLOCKS_FLAG_APPEND, 0xffffffffu, 0xffffffffu);
|
||||
if (state.pause) Event(info->voice_handle, SCE_NGS2_VOICE_EVENT_PAUSE);
|
||||
}
|
||||
|
||||
static void TestRefill(const std::vector<float>& reference) {
|
||||
for (int mode = 0; mode < 3; ++mode) {
|
||||
const auto system = CreateSystem();
|
||||
const auto voice = EmptySampler(system);
|
||||
RefillState state{0, mode == 1, mode == 2};
|
||||
Control(voice, SCE_NGS2_VOICE_PARAM_CALLBACK,
|
||||
Ngs2VoiceCallbackParam{{}, Refill, reinterpret_cast<std::uintptr_t>(&state), SCE_NGS2_VOICE_CALLBACK_FLAG_BLOCK_END, 0});
|
||||
Queue(voice, Superframe, 13, SCE_NGS2_WAVEFORM_BLOCKS_FLAG_CONTINUE, 500, 100);
|
||||
if (mode != 0) {
|
||||
for (float value : Render(system, Grain)) Require(value == 0.0f);
|
||||
Require(state.calls == 1);
|
||||
if (state.pause) Event(voice, SCE_NGS2_VOICE_EVENT_RESUME);
|
||||
}
|
||||
const auto output = Render(system, 512);
|
||||
for (std::size_t i = 0; i < output.size(); ++i) Require(output[i] == (i < 500 ? reference[100 + i] : 0.0f));
|
||||
Require(state.calls == 2 && Flags(voice) == 0);
|
||||
Require(sceNgs2SystemDestroy(system, nullptr) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void TestStarvationAndTruncation(const std::vector<float>& reference) {
|
||||
for (bool truncate : {false, true}) {
|
||||
const auto system = CreateSystem();
|
||||
const auto voice = EmptySampler(system);
|
||||
Queue(voice, Superframe, 13, SCE_NGS2_WAVEFORM_BLOCKS_FLAG_CONTINUE, 500, 100);
|
||||
for (float value : Render(system, Grain * 3)) Require(value == 0.0f);
|
||||
Require((Flags(voice) & SCE_NGS2_VOICE_STATE_FLAG_PLAYING) != 0);
|
||||
if (truncate) {
|
||||
Control(voice, SCE_NGS2_SAMPLER_VOICE_PARAM_ADD_WAVEFORM_BLOCKS, Ngs2SamplerVoiceWaveformBlocksParam{{}, nullptr, 0, 0, nullptr});
|
||||
bool rejected = false;
|
||||
try { Render(system, Grain); } catch (const std::invalid_argument&) { rejected = true; }
|
||||
Require(rejected);
|
||||
} else {
|
||||
Queue(voice, Superframe + 13, SuperframeBytes - 13, SCE_NGS2_WAVEFORM_BLOCKS_FLAG_APPEND);
|
||||
const auto output = Render(system, 512);
|
||||
for (std::size_t i = 0; i < output.size(); ++i) Require(output[i] == (i < 500 ? reference[100 + i] : 0.0f));
|
||||
Require(Flags(voice) == 0);
|
||||
}
|
||||
Require(sceNgs2SystemDestroy(system, nullptr) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
const auto reference = Reference();
|
||||
TestSkipAndBlockEnd(reference);
|
||||
@@ -343,5 +455,8 @@ int main() {
|
||||
TestMalformedLoops();
|
||||
TestCalcBlock();
|
||||
TestCorruptSuperframeAtPageEnd();
|
||||
TestStreamPartitions();
|
||||
TestRefill(reference);
|
||||
TestStarvationAndTruncation(reference);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
- [libSceMbus](../../core/libs/prx/libSceMbus/Export.cpp) (`libSceDeviceService`) - signatures taken from PPSA01317, which calls `sceDeviceServiceInitialize(3, 0)`, polls `sceDeviceServiceGetEventState(1)` and queries again when bit 0 or 1 is set, and calls `sceDeviceServiceQueryDeviceInfo_(0x7001, 0, 0, infos, 1, &count, &unknown, 0x70)`, treating a negative result as an error and `count` as the number of entries. The host has no devices: the event state is 0 and a query returns 0 with `count` 0
|
||||
- [NGS2 ATRAC9 waveform loops](../../core/libs/prx/libSceNgs2.native/src/Atrac9.cpp) accepts one integer forward `smpl` loop. Inclusive RIFF endpoints and encoder-delay adjustment follow [vgmstream riff.c](https://github.com/vgmstream/vgmstream/blob/7dc938fa2f210943b37c7b6511852b516ef432ab/src/meta/riff.c). PPSA03747 supplies a 60-byte sampler chunk with one loop, `samplerData` 24, start 256, inclusive end 299224 and delay 256 for 298969 audible samples. The chunk size and loop count bound the records; `samplerData` is not interpreted. Output positions are assumed to use audible samples and an exclusive end. Blocks split into an optional intro, a repeated range and an optional tail; zero play count means infinite repeats, positive play count includes the first pass. ATRAC9 ranges include a preceding codec frame for decoder history. The output block layout, repeat-count mapping and error codes are unverified on hardware. Multiple, reverse, alternating and fractional loops throw.
|
||||
- [NGS2 submixer UserFx](../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (control `0x20000004`) invokes the title's callback after input mixing on every playing grain, including silence. The 40-byte control and 48-byte process context follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/945dbc3cc3eee80ac3e053b438502ed936fa6bb2/src/core/libraries/ngs2/ngs2_submixer.h). PPSA03747 installs a peak-meter callback; its code confirms the channel pointers, user data 0, channel count, grain size and sample-rate offsets and returns 0. Other user data words are preserved. Flags are assumed to be 1 after registration/restart and 0 thereafter, following UserFx2's first-process convention; this callback does not inspect them. Null handlers disable the effect, setup clears it, and nonzero callback results throw. These lifecycle and error semantics are unverified on console hardware.
|
||||
- [NGS2 ATRAC9 streaming](../../core/libs/prx/libSceNgs2.native/src/Atrac9.cpp) keeps waveform duration and skip counts across compressed input blocks. The shared `APPEND` flag (2), called `ONLY_DATA` in the compressed-data reference, ignores an ATRAC9 continuation block's sample, skip and repeat counts, following [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/bb98f26433fa9e711c219aac1711fe8ecba5bf3a/src/libs/ngs2.cpp); PPSA03747 confirms a 262044-byte initial buffer declaring 11310532 samples, followed by 16384-byte buffers with flags 3. Incomplete superframes are retained until more input arrives. Input-block completion callbacks allow refills; the final callback of a logical waveform waits for its buffered PCM to drain, preserving the existing complete-block repeat behaviour. This buffering, callback timing, decoded-byte accounting and starvation silence are unverified on hardware. A closed ATRAC9 stream ending before its declared samples throws; fragmented ATRAC9 waveforms with repeats are unsupported. PCM streaming retains its existing append validation and early-close behaviour.
|
||||
- PPSA01341 imports declared without parameters, signatures unknown: [sceAgcSetSemaphoreMemory](../../core/libs/prx/libSceAgc/Unimplemented.cpp), [sceAgcDriverRegisterMultipleResources](../../core/libs/prx/libSceAgcDriver/Unimplemented.cpp). Names from the shadPS4 aerolib NID list
|
||||
- [sceAppContentDownloadDataGetAvailableSpaceKb](../../core/libs/prx/libSceAppContent/Export.cpp) (libSceAppContent) - a title whose `param.json` declares `downloadDataSize` 0 gets 0 KB and OK; the console's behaviour for a title without a download data area is unverified (it may return an error instead). KytyPS5 does not special-case it, and shadPS4 stubs 1048576 KB
|
||||
- [sceNpEAAccessInitialize, sceNpEAAccessTerminate](../../core/libs/prx/libSceNpPartner001/Export.cpp) (libSceNpPartner001) - names and the no-argument signatures follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/np/np_partner.cpp); imported by PPSA23566, not confirmed on a PS5 title
|
||||
|
||||
Reference in New Issue
Block a user