From e4d271289733847ee21c6940cba36a6784dd853d Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 23 Apr 2019 20:49:45 -0700 Subject: [PATCH] Mp3: Apply offset to stream read buffer. Handle errors for sceMp3CheckStreamDataNeeded as well. --- Core/HLE/sceMp3.cpp | 19 +++++++++++-------- Core/HW/SimpleAudioDec.cpp | 18 ++++++++++++------ Core/HW/SimpleAudioDec.h | 1 + 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Core/HLE/sceMp3.cpp b/Core/HLE/sceMp3.cpp index a474d91ec9..7ffb6e4711 100644 --- a/Core/HLE/sceMp3.cpp +++ b/Core/HLE/sceMp3.cpp @@ -181,15 +181,16 @@ static int sceMp3ResetPlayPosition(u32 mp3) { } static int sceMp3CheckStreamDataNeeded(u32 mp3) { - DEBUG_LOG(ME, "sceMp3CheckStreamDataNeeded(%08x)", mp3); - 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_UNRESERVED_HANDLE, "unreserved handle"); + } else if (ctx->AuBuf == 0) { + return hleLogError(ME, ERROR_MP3_UNRESERVED_HANDLE, "incorrect handle type"); } - return ctx->AuCheckStreamDataNeeded(); + return hleLogSuccessI(ME, ctx->AuCheckStreamDataNeeded()); } static u32 sceMp3ReserveMp3Handle(u32 mp3Addr) { @@ -344,15 +345,17 @@ static int __CalculateMp3Bitrates(int bitval, int mp3version, int mp3layer) { } static int __ParseMp3Header(AuCtx *ctx, bool *isID3) { - int header = bswap32(Memory::Read_U32(ctx->AuBuf)); + u32 ptr = ctx->AuBuf + ctx->AuStreamWorkareaSize(); + int header = bswap32(Memory::Read_U32(ptr)); // ID3 tag , can be seen in Hanayaka Nari Wa ga Ichizoku. static const int ID3 = 0x49443300; if ((header & 0xFFFFFF00) == ID3) { *isID3 = true; - int size = bswap32(Memory::Read_U32(ctx->AuBuf + ctx->startPos + 6)); + // TODO: Can we count on startPos being read already? What if it's past the buffer size? + int size = bswap32(Memory::Read_U32(ptr + ctx->startPos + 6)); // Highest bit of each byte has to be ignored (format: 0x7F7F7F7F) size = (size & 0x7F) | ((size & 0x7F00) >> 1) | ((size & 0x7F0000) >> 2) | ((size & 0x7F000000) >> 3); - header = bswap32(Memory::Read_U32(ctx->AuBuf + ctx->startPos + 10 + size)); + header = bswap32(Memory::Read_U32(ptr + ctx->startPos + 10 + size)); } return header; } diff --git a/Core/HW/SimpleAudioDec.cpp b/Core/HW/SimpleAudioDec.cpp index 4a26c01999..b8062ca56e 100644 --- a/Core/HW/SimpleAudioDec.cpp +++ b/Core/HW/SimpleAudioDec.cpp @@ -397,7 +397,7 @@ int AuCtx::AuStreamBytesNeeded() { if (readPos >= endPos) return 0; // Account for the workarea. - int offset = 0x05c0; + int offset = AuStreamWorkareaSize(); return (int)AuBufSize - AuBufAvailable - offset; } @@ -405,8 +405,17 @@ int AuCtx::AuStreamBytesNeeded() { return std::min((int)AuBufSize - AuBufAvailable, (int)endPos - readPos); } +int AuCtx::AuStreamWorkareaSize() { + // Note that this is 31 bytes more than the max layer 3 frame size. + if (audioType == PSP_CODEC_MP3) + return 0x05c0; + return 0; +} + // check how many bytes we have read from source file u32 AuCtx::AuNotifyAddStreamData(int size) { + int offset = AuStreamWorkareaSize(); + if (askedReadSize != 0) { // Old save state, numbers already adjusted. int diffsize = size - askedReadSize; @@ -422,7 +431,7 @@ u32 AuCtx::AuNotifyAddStreamData(int size) { } if (Memory::IsValidRange(AuBuf, size)) { - sourcebuff.append((const char *)Memory::GetPointer(AuBuf), size); + sourcebuff.append((const char *)Memory::GetPointer(AuBuf + offset), size); } if (readPos >= (int)endPos && LoopNum != 0) { @@ -441,10 +450,7 @@ u32 AuCtx::AuNotifyAddStreamData(int size) { // buff, size and srcPos are all pointers u32 AuCtx::AuGetInfoToAddStreamData(u32 bufPtr, u32 sizePtr, u32 srcPosPtr) { int readsize = AuStreamBytesNeeded(); - int offset = 0; - if (audioType == PSP_CODEC_MP3) { - offset = 0x05c0; - } + int offset = AuStreamWorkareaSize(); // we can recharge AuBuf from its beginning if (readsize != 0) { diff --git a/Core/HW/SimpleAudioDec.h b/Core/HW/SimpleAudioDec.h index 182d2dc7bf..c41707c3a5 100644 --- a/Core/HW/SimpleAudioDec.h +++ b/Core/HW/SimpleAudioDec.h @@ -100,6 +100,7 @@ public: u32 AuNotifyAddStreamData(int size); int AuCheckStreamDataNeeded(); int AuStreamBytesNeeded(); + int AuStreamWorkareaSize(); u32 AuResetPlayPosition(); u32 AuResetPlayPositionByFrame(int position);