From de5488d4df4581ec3aa7cb691aca654d1a3a9407 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 25 Jan 2021 20:57:34 -0800 Subject: [PATCH 1/4] Mp3: Correct error handling for newer sdk versions. The 6.xx behavior might be important if a game relies on it to add data. --- Core/HLE/sceMp3.cpp | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/Core/HLE/sceMp3.cpp b/Core/HLE/sceMp3.cpp index c3e0bb92ba..0f4fb8a4b0 100644 --- a/Core/HLE/sceMp3.cpp +++ b/Core/HLE/sceMp3.cpp @@ -23,6 +23,7 @@ #include "Core/Config.h" #include "Core/HLE/HLE.h" #include "Core/HLE/FunctionWrappers.h" +#include "Core/HLE/sceKernelMemory.h" #include "Core/HLE/sceMp3.h" #include "Core/HW/MediaEngine.h" #include "Core/MemMap.h" @@ -33,6 +34,7 @@ static const u32 ERROR_MP3_INVALID_HANDLE = 0x80671001; static const u32 ERROR_MP3_UNRESERVED_HANDLE = 0x80671102; static const u32 ERROR_MP3_NOT_YET_INIT_HANDLE = 0x80671103; static const u32 ERROR_MP3_NO_RESOURCE_AVAIL = 0x80671201; +static const u32 ERROR_MP3_BAD_SAMPLE_RATE = 0x80671302; static const u32 ERROR_MP3_BAD_RESET_FRAME = 0x80671501; static const u32 ERROR_MP3_BAD_ADDR = 0x80671002; static const u32 ERROR_MP3_BAD_SIZE = 0x80671003; @@ -377,6 +379,7 @@ static int FindMp3Header(AuCtx *ctx, int &header, int end) { } static int sceMp3Init(u32 mp3) { + int sdkver = sceKernelGetCompiledSdkVersion(); AuCtx *ctx = getMp3Ctx(mp3); if (!ctx) { if (mp3 >= MP3_MAX_HANDLES) @@ -397,11 +400,8 @@ static int sceMp3Init(u32 mp3) { // Parse the Mp3 header int layerBits = (header >> 17) & 0x3; int versionBits = (header >> 19) & 0x3; - ctx->SamplingRate = __CalculateMp3SampleRates((header >> 10) & 0x3, versionBits); - ctx->Channels = __CalculateMp3Channels((header >> 6) & 0x3); - ctx->BitRate = __CalculateMp3Bitrates((header >> 12) & 0xF, versionBits, layerBits); - ctx->MaxOutputSample = CalculateMp3SamplesPerFrame(versionBits, layerBits); - ctx->freq = ctx->SamplingRate; + int bitrate = __CalculateMp3Bitrates((header >> 12) & 0xF, versionBits, layerBits); + int samplerate = __CalculateMp3SampleRates((header >> 10) & 0x3, versionBits);; DEBUG_LOG(ME, "sceMp3Init(): channels=%i, samplerate=%iHz, bitrate=%ikbps, layerBits=%d ,versionBits=%d,HEADER: %08x", ctx->Channels, ctx->SamplingRate, ctx->BitRate, layerBits, versionBits, header); @@ -409,18 +409,31 @@ static int sceMp3Init(u32 mp3) { // TODO: Should return ERROR_AVCODEC_INVALID_DATA. WARN_LOG_REPORT(ME, "sceMp3Init: invalid data: not layer 3"); } + if (bitrate == 0 || bitrate == -1) { + return hleDelayResult(hleReportError(ME, ERROR_AVCODEC_INVALID_DATA, "invalid bitrate v%d l%d rate %04x", versionBits, layerBits, (header >> 12) & 0xF), "mp3 init", PARSE_DELAY_MS); + } + if (samplerate == -1) { + return hleDelayResult(hleReportError(ME, ERROR_AVCODEC_INVALID_DATA, "invalid sample rate v%d l%d rate %02x", versionBits, layerBits, (header >> 10) & 0x3), "mp3 init", PARSE_DELAY_MS); + } + + // Before we allow init, newer SDK versions next require at least 156 bytes. + // That happens to be the size of the first frame header for VBR. + if (sdkver >= 0x06000000 && ctx->readPos < 156) { + return hleDelayResult(hleLogError(ME, SCE_KERNEL_ERROR_INVALID_VALUE, "insufficient mp3 data for init"), "mp3 init", PARSE_DELAY_MS); + } + + ctx->SamplingRate = samplerate; + ctx->Channels = __CalculateMp3Channels((header >> 6) & 0x3); + ctx->BitRate = bitrate; + ctx->MaxOutputSample = CalculateMp3SamplesPerFrame(versionBits, layerBits); + ctx->freq = ctx->SamplingRate; + if (versionBits != 3) { // TODO: Should return 0x80671301 (unsupported version?) WARN_LOG_REPORT(ME, "sceMp3Init: invalid data: not MPEG v1"); } - if (ctx->BitRate == 0 || ctx->BitRate == -1) { - return hleDelayResult(hleReportError(ME, ERROR_AVCODEC_INVALID_DATA, "invalid bitrate v%d l%d rate %04x", versionBits, layerBits, (header >> 12) & 0xF), "mp3 init", PARSE_DELAY_MS); - } - if (ctx->SamplingRate == -1) { - return hleDelayResult(hleReportError(ME, ERROR_AVCODEC_INVALID_DATA, "invalid sample rate v%d l%d rate %02x", versionBits, layerBits, (header >> 10) & 0x3), "mp3 init", PARSE_DELAY_MS); - } else if (ctx->SamplingRate != 44100) { - // TODO: Should return 0x80671302 (unsupported sample rate?) - WARN_LOG_REPORT(ME, "sceMp3Init: invalid data: not 44.1kHz"); + if (samplerate != 44100 && sdkver < 3090500) { + return hleDelayResult(hleLogError(ME, ERROR_MP3_BAD_SAMPLE_RATE, "invalid data: not 44.1kHz"), "mp3 init", PARSE_DELAY_MS); } // Based on bitrate, we can calculate the frame size in bytes. From 463fc3c792dfbcbe2d5704a1751668b87ebfa02e Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 25 Jan 2021 23:51:55 -0800 Subject: [PATCH 2/4] Mp3: Allow decode without pcm pointer. Just like other audio decoding, you're allowed to skip audio. Also prevents a crash if the mp3 is not yet inited. --- Core/HLE/sceMp3.cpp | 14 ++++++++------ Core/HW/SimpleAudioDec.cpp | 8 ++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Core/HLE/sceMp3.cpp b/Core/HLE/sceMp3.cpp index 0f4fb8a4b0..3c48549776 100644 --- a/Core/HLE/sceMp3.cpp +++ b/Core/HLE/sceMp3.cpp @@ -159,19 +159,21 @@ void __Mp3DoState(PointerWrap &p) { } static int sceMp3Decode(u32 mp3, u32 outPcmPtr) { - DEBUG_LOG(ME, "sceMp3Decode(%08x,%08x)", mp3, outPcmPtr); - AuCtx *ctx = getMp3Ctx(mp3); if (!ctx) { - ERROR_LOG(ME, "%s: bad mp3 handle %08x", __FUNCTION__, mp3); - return -1; + if (mp3 >= MP3_MAX_HANDLES) + return hleLogError(ME, ERROR_MP3_INVALID_HANDLE, "invalid handle"); + return hleLogError(ME, ERROR_MP3_NOT_YET_INIT_HANDLE, "unreserved handle"); + } else if (ctx->Version < 0 || ctx->AuBuf == 0) { + return hleLogError(ME, ERROR_MP3_NOT_YET_INIT_HANDLE, "not yet init"); } int pcmBytes = ctx->AuDecode(outPcmPtr); if (pcmBytes > 0) { // decode data successfully, delay thread - return hleDelayResult(pcmBytes, "mp3 decode", mp3DecodeDelay); + return hleDelayResult(hleLogSuccessI(ME, pcmBytes), "mp3 decode", mp3DecodeDelay); } + // Should already have logged. return pcmBytes; } @@ -719,7 +721,7 @@ const HLEFunction sceMp3[] = { {0X8AB81558, &WrapU_V, "sceMp3StartEntry", 'x', "" }, {0X8F450998, &WrapI_U, "sceMp3GetSamplingRate", 'i', "x" }, {0XA703FE0F, &WrapI_UUUU, "sceMp3GetInfoToAddStreamData", 'i', "xppp" }, - {0XD021C0FB, &WrapI_UU, "sceMp3Decode", 'i', "xx" }, + {0XD021C0FB, &WrapI_UU, "sceMp3Decode", 'i', "xp" }, {0XD0A56296, &WrapI_U, "sceMp3CheckStreamDataNeeded", 'i', "x" }, {0XD8F54A51, &WrapI_U, "sceMp3GetLoopNum", 'i', "x" }, {0XF5478233, &WrapI_U, "sceMp3ReleaseMp3Handle", 'i', "x" }, diff --git a/Core/HW/SimpleAudioDec.cpp b/Core/HW/SimpleAudioDec.cpp index 10159d7daa..2e9ac873d4 100644 --- a/Core/HW/SimpleAudioDec.cpp +++ b/Core/HW/SimpleAudioDec.cpp @@ -327,9 +327,8 @@ size_t AuCtx::FindNextMp3Sync() { // return output pcm size, <0 error u32 AuCtx::AuDecode(u32 pcmAddr) { - if (!Memory::IsValidAddress(pcmAddr)){ - ERROR_LOG(ME, "%s: output bufferAddress %08x is invalctx", __FUNCTION__, pcmAddr); - return -1; + if (!Memory::GetPointer(PCMBuf)) { + return hleLogError(ME, -1, "ctx output bufferAddress %08x is invalid", PCMBuf); } auto outbuf = Memory::GetPointer(PCMBuf); @@ -376,7 +375,8 @@ u32 AuCtx::AuDecode(u32 pcmAddr) { memset(outbuf + outpcmbufsize, 0, PCMBufSize - outpcmbufsize); } - Memory::Write_U32(PCMBuf, pcmAddr); + if (pcmAddr) + Memory::Write_U32(PCMBuf, pcmAddr); return outpcmbufsize; } From 4b4be0f34fb440aacae5c5a0c2b4e98eec1a035f Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 26 Jan 2021 23:50:54 -0800 Subject: [PATCH 3/4] Mp3: Always keep sample rate from original mp3. Our codec context is updated with the source sample rate, so this makes us not resample at all. Converting to stereo still seems correct. --- Core/HLE/sceMp3.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Core/HLE/sceMp3.cpp b/Core/HLE/sceMp3.cpp index 3c48549776..25ddbf0fee 100644 --- a/Core/HLE/sceMp3.cpp +++ b/Core/HLE/sceMp3.cpp @@ -447,11 +447,9 @@ static int sceMp3Init(u32 mp3) { ctx->Version = versionBits; - // for mp3, if required freq is 48000,32000, reset resampling Frequency to 48000,32000 seems get better sound quality (e.g. Miku Custom BGM,Hanayaka Nari Wa ga Ichizoku) - // TODO: Isn't this backwards? Woudln't we want to read as 48kHz and resample to 44.1kHz? - if (ctx->freq == 48000 || ctx->freq == 32000) { - ctx->decoder->SetResampleFrequency(ctx->freq); - } + // This tells us to resample to the same frequency it decodes to. + ctx->decoder->SetResampleFrequency(ctx->freq); + return hleDelayResult(hleLogSuccessI(ME, 0), "mp3 init", PARSE_DELAY_MS); } From 8a437b267ebd9b358b0ba88930cae01b6f0993aa Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 27 Jan 2021 07:07:14 -0800 Subject: [PATCH 4/4] Mp3: Correct logging for init. --- Core/HLE/sceMp3.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Core/HLE/sceMp3.cpp b/Core/HLE/sceMp3.cpp index 25ddbf0fee..9b1056e943 100644 --- a/Core/HLE/sceMp3.cpp +++ b/Core/HLE/sceMp3.cpp @@ -404,8 +404,9 @@ static int sceMp3Init(u32 mp3) { int versionBits = (header >> 19) & 0x3; int bitrate = __CalculateMp3Bitrates((header >> 12) & 0xF, versionBits, layerBits); int samplerate = __CalculateMp3SampleRates((header >> 10) & 0x3, versionBits);; + int channels = __CalculateMp3Channels((header >> 6) & 0x3); - DEBUG_LOG(ME, "sceMp3Init(): channels=%i, samplerate=%iHz, bitrate=%ikbps, layerBits=%d ,versionBits=%d,HEADER: %08x", ctx->Channels, ctx->SamplingRate, ctx->BitRate, layerBits, versionBits, header); + DEBUG_LOG(ME, "sceMp3Init(): channels=%i, samplerate=%iHz, bitrate=%ikbps, layerBits=%d ,versionBits=%d,HEADER: %08x", channels, samplerate, bitrate, layerBits, versionBits, header); if (layerBits != 1) { // TODO: Should return ERROR_AVCODEC_INVALID_DATA. @@ -425,7 +426,7 @@ static int sceMp3Init(u32 mp3) { } ctx->SamplingRate = samplerate; - ctx->Channels = __CalculateMp3Channels((header >> 6) & 0x3); + ctx->Channels = channels; ctx->BitRate = bitrate; ctx->MaxOutputSample = CalculateMp3SamplesPerFrame(versionBits, layerBits); ctx->freq = ctx->SamplingRate;