From f1eaf9dc0e5bd43cb3e273fc5639d173c2f444f0 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 23 Apr 2019 20:18:16 -0700 Subject: [PATCH] Mp3: Don't change buffer accounting until add. If we just ask what we should add, that is meant to stay static until we do actually add it. This also reduces the max we ask for at a time, which better matches correct behavior and might impact game behavior. --- Core/HLE/sceMp3.cpp | 1 - Core/HW/SimpleAudioDec.cpp | 78 ++++++++++++++++++++++++-------------- Core/HW/SimpleAudioDec.h | 3 +- 3 files changed, 51 insertions(+), 31 deletions(-) diff --git a/Core/HLE/sceMp3.cpp b/Core/HLE/sceMp3.cpp index b53b6f1d2d..a474d91ec9 100644 --- a/Core/HLE/sceMp3.cpp +++ b/Core/HLE/sceMp3.cpp @@ -136,7 +136,6 @@ void __Mp3DoState(PointerWrap &p) { mp3->readPos = mp3_old->readPosition; mp3->AuBufAvailable = 0; // reset to read from file mp3->askedReadSize = 0; - mp3->realReadSize = 0; mp3->audioType = PSP_CODEC_MP3; mp3->decoder = new SimpleAudio(mp3->audioType); diff --git a/Core/HW/SimpleAudioDec.cpp b/Core/HW/SimpleAudioDec.cpp index 05d36541f8..6f9c039104 100644 --- a/Core/HW/SimpleAudioDec.cpp +++ b/Core/HW/SimpleAudioDec.cpp @@ -306,7 +306,6 @@ AuCtx::AuCtx() { SumDecodedSamples = 0; MaxOutputSample = 0; askedReadSize = 0; - realReadSize = 0; audioType = 0; FrameNum = 0; }; @@ -394,20 +393,26 @@ int AuCtx::AuCheckStreamDataNeeded() } // check how many bytes we have read from source file -u32 AuCtx::AuNotifyAddStreamData(int size) -{ - realReadSize = size; - int diffsize = realReadSize - askedReadSize; - // Notify the real read size - if (diffsize != 0){ - readPos += diffsize; - AuBufAvailable += diffsize; +u32 AuCtx::AuNotifyAddStreamData(int size) { + if (askedReadSize != 0) { + // Old save state, numbers already adjusted. + int diffsize = size - askedReadSize; + // Notify the real read size + if (diffsize != 0) { + readPos += diffsize; + AuBufAvailable += diffsize; + } + askedReadSize = 0; + } else { + readPos += size; + AuBufAvailable += size; } - // append AuBuf into sourcebuff - sourcebuff.append((const char*)Memory::GetPointer(AuBuf), size); + if (Memory::IsValidRange(AuBuf, size)) { + sourcebuff.append((const char *)Memory::GetPointer(AuBuf), size); + } - if (readPos >= (int)endPos && LoopNum != 0){ + if (readPos >= (int)endPos && LoopNum != 0) { // if we need loop, reset readPos readPos = startPos; // reset LoopNum @@ -421,24 +426,40 @@ u32 AuCtx::AuNotifyAddStreamData(int size) // read from stream position srcPos of size bytes into buff // buff, size and srcPos are all pointers -u32 AuCtx::AuGetInfoToAddStreamData(u32 buff, u32 size, u32 srcPos) -{ - // you can not read beyond file size and the buffer size - int readsize = std::min((int)AuBufSize - AuBufAvailable, (int)endPos - readPos); +u32 AuCtx::AuGetInfoToAddStreamData(u32 bufPtr, u32 sizePtr, u32 srcPosPtr) { + int readsize; + int offset = 0; + if (audioType == PSP_CODEC_MP3) { + // Account for the workarea. + offset = 0x05c0; + // The endPos and readPos are not considered, except when you've read to the end. + readsize = (int)AuBufSize - AuBufAvailable - offset; + if (readPos >= endPos) + readsize = 0; + } else { + // TODO: Untested. Maybe similar to MP3. + readsize = std::min((int)AuBufSize - AuBufAvailable, (int)endPos - readPos); + } // we can recharge AuBuf from its beginning - if (Memory::IsValidAddress(buff)) - Memory::Write_U32(AuBuf, buff); - if (Memory::IsValidAddress(size)) - Memory::Write_U32(readsize, size); - if (Memory::IsValidAddress(srcPos)) - Memory::Write_U32(readPos, srcPos); - - // preset the readPos and available size, they will be notified later in NotifyAddStreamData. - askedReadSize = readsize; - readPos += askedReadSize; - AuBufAvailable += askedReadSize; + if (readsize != 0) { + if (Memory::IsValidAddress(bufPtr)) + Memory::Write_U32(AuBuf, bufPtr); + if (Memory::IsValidAddress(sizePtr)) + Memory::Write_U32(readsize, sizePtr); + if (Memory::IsValidAddress(srcPosPtr)) + Memory::Write_U32(readPos, srcPosPtr); + } else { + if (Memory::IsValidAddress(bufPtr)) + Memory::Write_U32(0, bufPtr); + if (Memory::IsValidAddress(sizePtr)) + Memory::Write_U32(0, sizePtr); + if (Memory::IsValidAddress(srcPosPtr)) + Memory::Write_U32(0, srcPosPtr); + } + // Just for old save states. + askedReadSize = 0; return 0; } @@ -473,7 +494,8 @@ void AuCtx::DoState(PointerWrap &p) { p.Do(BitRate); p.Do(SamplingRate); p.Do(askedReadSize); - p.Do(realReadSize); + int dummy = 0; + p.Do(dummy); p.Do(FrameNum); if (p.mode == p.MODE_READ) { diff --git a/Core/HW/SimpleAudioDec.h b/Core/HW/SimpleAudioDec.h index 22ec180a85..63c7c181a1 100644 --- a/Core/HW/SimpleAudioDec.h +++ b/Core/HW/SimpleAudioDec.h @@ -105,7 +105,7 @@ public: u32 AuSetLoopNum(int loop); u32 AuGetLoopNum(); - u32 AuGetInfoToAddStreamData(u32 buff, u32 size, u32 srcPos); + u32 AuGetInfoToAddStreamData(u32 bufPtr, u32 sizePtr, u32 srcPosPtr); u32 AuGetMaxOutputSample() const { return MaxOutputSample; } u32 AuGetSumDecodedSample() const { return SumDecodedSamples; } int AuGetChannelNum() const { return Channels; } @@ -149,7 +149,6 @@ public: int AuBufAvailable; // the available buffer of AuBuf to be able to recharge data int readPos; // read position in audio source file int askedReadSize; // the size of data requied to be read from file by the game - int realReadSize; // the really read size from file private: std::string sourcebuff; // source buffer