diff --git a/Core/HW/Atrac3Standalone.cpp b/Core/HW/Atrac3Standalone.cpp index a195e02a49..dc73b46cc0 100644 --- a/Core/HW/Atrac3Standalone.cpp +++ b/Core/HW/Atrac3Standalone.cpp @@ -16,12 +16,25 @@ inline int16_t clamp16(float f) { // Test case for ATRAC3: Mega Man Maverick Hunter X, PSP menu sound class Atrac3Audio : public AudioDecoder { public: - Atrac3Audio(PSPAudioType audioType) : audioType_(audioType) { + Atrac3Audio(PSPAudioType audioType, int channels, size_t blockAlign, const uint8_t *extraData, size_t extraDataSize) : audioType_(audioType) { if (audioType == PSP_CODEC_AT3PLUS) { ctx_ = avcodec_alloc_context3(&ff_atrac3p_decoder); } else { ctx_ = avcodec_alloc_context3(&ff_atrac3_decoder); } + if (audioType_ == PSP_CODEC_AT3) { + _dbg_assert_(ctx_); + _dbg_assert_(!codecOpen_); + ctx_->extradata = (uint8_t *)av_mallocz(extraDataSize); + ctx_->extradata_size = (int)extraDataSize; + ctx_->block_align = (int)blockAlign; + codecOpen_ = false; + if (extraData != nullptr) { + memcpy(ctx_->extradata, extraData, extraDataSize); + } + } else { + ctx_->block_align = (int)blockAlign; + } for (int i = 0; i < 2; i++) { buffers_[i] = new float[4096]; } @@ -52,6 +65,10 @@ public: codecOpen_ = true; } + if (inbytes != ctx_->block_align) { + WARN_LOG(ME, "Atrac3Audio::Decode: Unexpected block align %d (expected %d)", inbytes, ctx_->block_align); + } + // We just call the decode function directly without going through the whole packet machinery. int got_frame = 0; int result; @@ -96,20 +113,6 @@ public: // Hmm. ignore for now. } - void SetExtraData(const uint8_t *data, int size, int wav_bytes_per_packet) override { - // if (audioType_ == PSP_CODEC_AT3PLUS) { - _dbg_assert_(ctx_); - _dbg_assert_(!codecOpen_); - ctx_->extradata = (uint8_t *)av_mallocz(size); - ctx_->extradata_size = size; - ctx_->block_align = wav_bytes_per_packet; - codecOpen_ = false; - if (data != nullptr) { - memcpy(ctx_->extradata, data, size); - } - //} - } - PSPAudioType GetAudioType() const override { return audioType_; } private: @@ -124,6 +127,9 @@ private: PSPAudioType audioType_; }; -AudioDecoder *CreateAtrac3Audio(PSPAudioType audioType) { - return new Atrac3Audio(audioType); +AudioDecoder *CreateAtrac3Audio(int channels, size_t blockAlign, const uint8_t *extraData, size_t extraDataSize) { + return new Atrac3Audio(PSP_CODEC_AT3, channels, blockAlign, extraData, extraDataSize); +} +AudioDecoder *CreateAtrac3PlusAudio(int channels, size_t blockAlign) { + return new Atrac3Audio(PSP_CODEC_AT3PLUS, channels, blockAlign, nullptr, 0); } diff --git a/Core/HW/Atrac3Standalone.h b/Core/HW/Atrac3Standalone.h index 713b1a21a2..a86fae1729 100644 --- a/Core/HW/Atrac3Standalone.h +++ b/Core/HW/Atrac3Standalone.h @@ -2,4 +2,5 @@ #include "SimpleAudioDec.h" -AudioDecoder *CreateAtrac3Audio(PSPAudioType audioType); +AudioDecoder *CreateAtrac3Audio(int channels, size_t blockAlign, const uint8_t *extraData, size_t extraDataSize); +AudioDecoder *CreateAtrac3PlusAudio(int channels, size_t blockAlign); diff --git a/Core/HW/SimpleAudioDec.cpp b/Core/HW/SimpleAudioDec.cpp index 0f72ae0469..83b79ec975 100644 --- a/Core/HW/SimpleAudioDec.cpp +++ b/Core/HW/SimpleAudioDec.cpp @@ -117,8 +117,6 @@ public: return srcPos; } - // Not save stated, only used by UI. Used for ATRAC3 (non+) files. - void SetExtraData(const uint8_t *data, int size, int wav_bytes_per_packet) override; void SetChannels(int channels) override; // These two are only here because of save states. @@ -144,13 +142,14 @@ private: bool codecOpen_; }; -AudioDecoder *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz, int channels) { +AudioDecoder *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz, int channels, size_t blockAlign, const uint8_t *extraData, size_t extraDataSize) { switch (audioType) { case PSP_CODEC_MP3: return new MiniMp3Audio(); case PSP_CODEC_AT3: + return CreateAtrac3Audio(channels, blockAlign, extraData, extraDataSize); case PSP_CODEC_AT3PLUS: - return CreateAtrac3Audio(audioType); + return CreateAtrac3PlusAudio(channels, blockAlign); default: // Only AAC falls back to FFMPEG now. return new SimpleAudio(audioType, sampleRateHz, channels); @@ -240,21 +239,6 @@ bool SimpleAudio::OpenCodec(int block_align) { #endif // USE_FFMPEG } -void SimpleAudio::SetExtraData(const uint8_t *data, int size, int wav_bytes_per_packet) { -#ifdef USE_FFMPEG - if (codecCtx_) { - codecCtx_->extradata = (uint8_t *)av_mallocz(size); - codecCtx_->extradata_size = size; - codecCtx_->block_align = wav_bytes_per_packet; - codecOpen_ = false; - - if (data != nullptr) { - memcpy(codecCtx_->extradata, data, size); - } - } -#endif -} - void SimpleAudio::SetChannels(int channels) { if (channels_ == channels) { // Do nothing, already set. diff --git a/Core/HW/SimpleAudioDec.h b/Core/HW/SimpleAudioDec.h index e4c2cf0116..8ee4e45aa1 100644 --- a/Core/HW/SimpleAudioDec.h +++ b/Core/HW/SimpleAudioDec.h @@ -47,7 +47,6 @@ public: virtual int GetSourcePos() const = 0; virtual void SetChannels(int channels) = 0; - virtual void SetExtraData(const uint8_t *data, int size, int wav_bytes_per_packet) {} // Just metadata. void SetCtxPtr(uint32_t ptr) { ctxPtr = ptr; } @@ -60,7 +59,7 @@ private: void AudioClose(AudioDecoder **ctx); const char *GetCodecName(int codec); // audioType bool IsValidCodec(PSPAudioType codec); -AudioDecoder *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz = 44100, int channels = 2); +AudioDecoder *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz = 44100, int channels = 2, size_t blockAlign = 0, const uint8_t *extraData = nullptr, size_t extraDataSize = 0); class AuCtx { public: diff --git a/UI/BackgroundAudio.cpp b/UI/BackgroundAudio.cpp index 471d245476..fc528274fa 100644 --- a/UI/BackgroundAudio.cpp +++ b/UI/BackgroundAudio.cpp @@ -189,10 +189,17 @@ public: wave_.Read(file_); - decoder_ = CreateAudioDecoder((PSPAudioType)wave_.codec, wave_.sample_rate, wave_.num_channels); + uint8_t *extraData = nullptr; + size_t extraDataSize = 0; + size_t blockSize = 0; if (wave_.codec == PSP_CODEC_AT3) { - decoder_->SetExtraData(&wave_.at3_extradata[2], 14, wave_.raw_bytes_per_frame); + extraData = &wave_.at3_extradata[2]; + extraDataSize = 14; + blockSize = wave_.raw_bytes_per_frame; + } else if (wave_.codec == PSP_CODEC_AT3PLUS) { + blockSize = wave_.raw_bytes_per_frame; } + decoder_ = CreateAudioDecoder((PSPAudioType)wave_.codec, wave_.sample_rate, wave_.num_channels, blockSize, extraData, extraDataSize); INFO_LOG(AUDIO, "read ATRAC, frames: %d, rate %d", wave_.numFrames, wave_.sample_rate); }