From 32ca7ab59a522b0360f826dd22d77a0f678dc672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 10 Apr 2024 12:07:03 +0200 Subject: [PATCH] Minor code cleanups --- Core/HLE/sceKernel.cpp | 1 + Core/HLE/sceMp3.cpp | 13 +++++++------ Core/HLE/sceMp3.h | 3 ++- Core/HLE/sceMp4.cpp | 3 +-- Core/HW/SimpleAudioDec.cpp | 31 +++++++++++++++++++++---------- Core/HW/SimpleAudioDec.h | 8 +++----- 6 files changed, 35 insertions(+), 24 deletions(-) diff --git a/Core/HLE/sceKernel.cpp b/Core/HLE/sceKernel.cpp index 73aea78a7d..28d6a60a0b 100644 --- a/Core/HLE/sceKernel.cpp +++ b/Core/HLE/sceKernel.cpp @@ -124,6 +124,7 @@ void __KernelInit() __IoInit(); __JpegInit(); __AudioInit(); + __Mp3Init(); __SasInit(); __AtracInit(); __CccInit(); diff --git a/Core/HLE/sceMp3.cpp b/Core/HLE/sceMp3.cpp index f93f6c0e9c..2d824bae5b 100644 --- a/Core/HLE/sceMp3.cpp +++ b/Core/HLE/sceMp3.cpp @@ -157,6 +157,10 @@ static AuCtx *getMp3Ctx(u32 mp3) { return mp3Map[mp3]; } +void __Mp3Init() { + resourceInited = false; +} + void __Mp3Shutdown() { for (auto it = mp3Map.begin(), end = mp3Map.end(); it != end; ++it) { delete it->second; @@ -194,8 +198,7 @@ void __Mp3DoState(PointerWrap &p) { mp3->MaxOutputSample = mp3_old->mp3MaxSamples; mp3->SetReadPos(mp3_old->readPosition); - mp3->audioType = PSP_CODEC_MP3; - mp3->decoder = new SimpleAudio(mp3->audioType); + mp3->decoder = new SimpleAudio(PSP_CODEC_MP3); mp3Map[id] = mp3; } } @@ -300,9 +303,8 @@ static u32 sceMp3ReserveMp3Handle(u32 mp3Addr) { Au->PCMBufSize = 0; } - Au->audioType = PSP_CODEC_MP3; Au->SetReadPos(Au->startPos); - Au->decoder = new SimpleAudio(Au->audioType); + Au->decoder = new SimpleAudio(PSP_CODEC_MP3); int handle = (int)mp3Map.size(); mp3Map[handle] = Au; @@ -701,9 +703,8 @@ static u32 sceMp3ResetPlayPositionByFrame(u32 mp3, u32 frame) { static u32 sceMp3LowLevelInit(u32 mp3, u32 unk) { auto ctx = new AuCtx; - ctx->audioType = PSP_CODEC_MP3; // create mp3 decoder - ctx->decoder = new SimpleAudio(ctx->audioType); + ctx->decoder = new SimpleAudio(PSP_CODEC_MP3); // close the audio if mp3 already exists. if (mp3Map.find(mp3) != mp3Map.end()) { diff --git a/Core/HLE/sceMp3.h b/Core/HLE/sceMp3.h index 84ff082dfb..d1aa4fa3b5 100644 --- a/Core/HLE/sceMp3.h +++ b/Core/HLE/sceMp3.h @@ -21,5 +21,6 @@ class PointerWrap; void Register_sceMp3(); +void __Mp3Init(); void __Mp3Shutdown(); -void __Mp3DoState(PointerWrap &p); \ No newline at end of file +void __Mp3DoState(PointerWrap &p); diff --git a/Core/HLE/sceMp4.cpp b/Core/HLE/sceMp4.cpp index 21a70e4999..e1c555e8a3 100644 --- a/Core/HLE/sceMp4.cpp +++ b/Core/HLE/sceMp4.cpp @@ -253,10 +253,9 @@ static u32 sceAacInit(u32 id) aac->Channels = 2; aac->MaxOutputSample = aac->PCMBufSize / 4; aac->SetReadPos((int)aac->startPos); - aac->audioType = PSP_CODEC_AAC; // create aac decoder - aac->decoder = new SimpleAudio(aac->audioType); + aac->decoder = new SimpleAudio(PSP_CODEC_AAC); // close the audio if id already exist. if (aacMap.find(id) != aacMap.end()) { diff --git a/Core/HW/SimpleAudioDec.cpp b/Core/HW/SimpleAudioDec.cpp index c833ed4dd0..4ee43a3d96 100644 --- a/Core/HW/SimpleAudioDec.cpp +++ b/Core/HW/SimpleAudioDec.cpp @@ -37,6 +37,11 @@ extern "C" { #endif // USE_FFMPEG +// TODO: This should also be able to create other types of decoders. +SimpleAudio *CreateAudioDecoder(int audioType) { + return new SimpleAudio(audioType); +} + int SimpleAudio::GetAudioCodecID(int audioType) { #ifdef USE_FFMPEG switch (audioType) { @@ -57,13 +62,11 @@ int SimpleAudio::GetAudioCodecID(int audioType) { } SimpleAudio::SimpleAudio(int audioType, int sample_rate, int channels) -: ctxPtr(0xFFFFFFFF), audioType(audioType), sample_rate_(sample_rate), channels_(channels), - outSamples(0), srcPos(0), frame_(0), codec_(0), codecCtx_(0), swrCtx_(0), - codecOpen_(false) { - Init(); -} + : ctxPtr(0xFFFFFFFF), audioType(audioType), sample_rate_(sample_rate), channels_(channels), + outSamples(0), srcPos(0), + frame_(0), codec_(0), codecCtx_(0), swrCtx_(0), + codecOpen_(false) { -void SimpleAudio::Init() { #ifdef USE_FFMPEG #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 18, 100) avcodec_register_all(); @@ -179,6 +182,7 @@ bool SimpleAudio::IsOK() const { #endif } +// Decodes a single input frame. bool SimpleAudio::Decode(const uint8_t *inbuf, int inbytes, uint8_t *outbuf, int *outbytes) { #ifdef USE_FFMPEG if (!codecOpen_) { @@ -286,6 +290,12 @@ void AudioClose(AudioDecoder **ctx) { #endif // USE_FFMPEG } +void AudioClose(SimpleAudio **ctx) { +#ifdef USE_FFMPEG + delete *ctx; + *ctx = 0; +#endif // USE_FFMPEG +} static const char *const codecNames[4] = { "AT3+", "AT3", "MP3", "AAC", @@ -320,7 +330,7 @@ AuCtx::~AuCtx() { } size_t AuCtx::FindNextMp3Sync() { - if (audioType != PSP_CODEC_MP3) { + if (decoder->GetAudioType() != PSP_CODEC_MP3) { return 0; } @@ -413,7 +423,7 @@ int AuCtx::AuCheckStreamDataNeeded() { } int AuCtx::AuStreamBytesNeeded() { - if (audioType == PSP_CODEC_MP3) { + if (decoder->GetAudioType() == PSP_CODEC_MP3) { // The endPos and readPos are not considered, except when you've read to the end. if (readPos >= endPos) return 0; @@ -428,7 +438,7 @@ int AuCtx::AuStreamBytesNeeded() { int AuCtx::AuStreamWorkareaSize() { // Note that this is 31 bytes more than the max layer 3 frame size. - if (audioType == PSP_CODEC_MP3) + if (decoder->GetAudioType() == PSP_CODEC_MP3) return 0x05c0; return 0; } @@ -525,6 +535,7 @@ void AuCtx::DoState(PointerWrap &p) { Do(p, Channels); Do(p, MaxOutputSample); Do(p, readPos); + int audioType = decoder->GetAudioType(); Do(p, audioType); Do(p, BitRate); Do(p, SamplingRate); @@ -544,6 +555,6 @@ void AuCtx::DoState(PointerWrap &p) { } if (p.mode == p.MODE_READ) { - decoder = new SimpleAudio(audioType); + decoder = CreateAudioDecoder(audioType); } } diff --git a/Core/HW/SimpleAudioDec.h b/Core/HW/SimpleAudioDec.h index fb25a5fb0d..02ba8db3b8 100644 --- a/Core/HW/SimpleAudioDec.h +++ b/Core/HW/SimpleAudioDec.h @@ -57,6 +57,7 @@ public: virtual int GetOutSamples() const = 0; virtual int GetSourcePos() const = 0; + virtual int GetAudioType() const = 0; }; // FFMPEG-based decoder @@ -90,7 +91,6 @@ public: u32 GetCtxPtr() const { return ctxPtr; } private: - void Init(); bool OpenCodec(int block_align); u32 ctxPtr; @@ -111,6 +111,7 @@ private: bool codecOpen_; }; +void AudioClose(SimpleAudio **ctx); void AudioClose(AudioDecoder **ctx); const char *GetCodecName(int codec); // audioType bool IsValidCodec(int codec); @@ -170,9 +171,6 @@ public: // Au decoder AudioDecoder *decoder = nullptr; - // Au type - int audioType = 0; - private: size_t FindNextMp3Sync(); @@ -180,7 +178,7 @@ private: // buffers informations int AuBufAvailable = 0; // the available buffer of AuBuf to be able to recharge data - int readPos; // read position in audio source file + int readPos = 0; // read position in audio source file int askedReadSize = 0; // the size of data requied to be read from file by the game int nextOutputHalf = 0; };