More prep for plugging in alternate audio decoders

This commit is contained in:
Henrik Rydgård 2024-04-10 12:14:58 +02:00
parent 32ca7ab59a
commit 1938d3b876
7 changed files with 31 additions and 28 deletions

View file

@ -77,29 +77,31 @@ void __AudioCodecShutdown() {
}
static int sceAudiocodecInit(u32 ctxPtr, int codec) {
if (IsValidCodec(codec)) {
PSPAudioType audioType = (PSPAudioType)codec;
if (IsValidCodec(audioType)) {
// Create audio decoder for given audio codec and push it into AudioList
if (removeDecoder(ctxPtr)) {
WARN_LOG_REPORT(HLE, "sceAudiocodecInit(%08x, %d): replacing existing context", ctxPtr, codec);
}
auto decoder = new SimpleAudio(codec);
auto decoder = CreateAudioDecoder(audioType);
decoder->SetCtxPtr(ctxPtr);
audioList[ctxPtr] = decoder;
INFO_LOG(ME, "sceAudiocodecInit(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
INFO_LOG(ME, "sceAudiocodecInit(%08x, %i (%s))", ctxPtr, codec, GetCodecName(audioType));
DEBUG_LOG(ME, "Number of playing sceAudioCodec audios : %d", (int)audioList.size());
return 0;
}
ERROR_LOG_REPORT(ME, "sceAudiocodecInit(%08x, %i (%s)): Unknown audio codec %i", ctxPtr, codec, GetCodecName(codec), codec);
ERROR_LOG_REPORT(ME, "sceAudiocodecInit(%08x, %i (%s)): Unknown audio codec %i", ctxPtr, codec, GetCodecName(audioType), codec);
return 0;
}
static int sceAudiocodecDecode(u32 ctxPtr, int codec) {
PSPAudioType audioType = (PSPAudioType)codec;
if (!ctxPtr){
ERROR_LOG_REPORT(ME, "sceAudiocodecDecode(%08x, %i (%s)) got NULL pointer", ctxPtr, codec, GetCodecName(codec));
ERROR_LOG_REPORT(ME, "sceAudiocodecDecode(%08x, %i (%s)) got NULL pointer", ctxPtr, codec, GetCodecName(audioType));
return -1;
}
if (IsValidCodec(codec)){
if (IsValidCodec(audioType)){
int outbytes = 0;
// find a decoder in audioList
auto decoder = findDecoder(ctxPtr);
@ -107,7 +109,7 @@ static int sceAudiocodecDecode(u32 ctxPtr, int codec) {
if (!decoder && oldStateLoaded) {
// We must have loaded an old state that did not have sceAudiocodec information.
// Fake it by creating the desired context.
decoder = new SimpleAudio(codec);
decoder = CreateAudioDecoder(audioType);
decoder->SetCtxPtr(ctxPtr);
audioList[ctxPtr] = decoder;
}
@ -194,7 +196,7 @@ void __sceAudiocodecDoState(PointerWrap &p){
DoArray(p, codec_, s >= 2 ? count : (int)ARRAY_SIZE(codec_));
DoArray(p, ctxPtr_, s >= 2 ? count : (int)ARRAY_SIZE(ctxPtr_));
for (int i = 0; i < count; i++) {
auto decoder = new SimpleAudio(codec_[i]);
auto decoder = CreateAudioDecoder((PSPAudioType)codec_[i]);
decoder->SetCtxPtr(ctxPtr_[i]);
audioList[ctxPtr_[i]] = decoder;
}

View file

@ -198,7 +198,7 @@ void __Mp3DoState(PointerWrap &p) {
mp3->MaxOutputSample = mp3_old->mp3MaxSamples;
mp3->SetReadPos(mp3_old->readPosition);
mp3->decoder = new SimpleAudio(PSP_CODEC_MP3);
mp3->decoder = CreateAudioDecoder(PSP_CODEC_MP3);
mp3Map[id] = mp3;
}
}
@ -304,7 +304,7 @@ static u32 sceMp3ReserveMp3Handle(u32 mp3Addr) {
}
Au->SetReadPos(Au->startPos);
Au->decoder = new SimpleAudio(PSP_CODEC_MP3);
Au->decoder = CreateAudioDecoder(PSP_CODEC_MP3);
int handle = (int)mp3Map.size();
mp3Map[handle] = Au;
@ -701,10 +701,10 @@ static u32 sceMp3ResetPlayPositionByFrame(u32 mp3, u32 frame) {
}
static u32 sceMp3LowLevelInit(u32 mp3, u32 unk) {
auto ctx = new AuCtx;
auto ctx = new AuCtx();
// create mp3 decoder
ctx->decoder = new SimpleAudio(PSP_CODEC_MP3);
ctx->decoder = CreateAudioDecoder(PSP_CODEC_MP3);
// close the audio if mp3 already exists.
if (mp3Map.find(mp3) != mp3Map.end()) {

View file

@ -255,7 +255,7 @@ static u32 sceAacInit(u32 id)
aac->SetReadPos((int)aac->startPos);
// create aac decoder
aac->decoder = new SimpleAudio(PSP_CODEC_AAC);
aac->decoder = CreateAudioDecoder(PSP_CODEC_AAC);
// close the audio if id already exist.
if (aacMap.find(id) != aacMap.end()) {

View file

@ -340,7 +340,7 @@ bool MediaEngine::openContext(bool keepReadPos) {
return false;
setVideoDim();
m_audioContext = new SimpleAudio(m_audioType, 44100, 2);
m_audioContext = CreateAudioDecoder((PSPAudioType)m_audioType);
m_isVideoEnd = false;
#endif // USE_FFMPEG
return true;

View file

@ -38,8 +38,8 @@ 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);
SimpleAudio *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz, int channels) {
return new SimpleAudio(audioType, sampleRateHz, channels);
}
int SimpleAudio::GetAudioCodecID(int audioType) {
@ -61,8 +61,8 @@ int SimpleAudio::GetAudioCodecID(int audioType) {
#endif // USE_FFMPEG
}
SimpleAudio::SimpleAudio(int audioType, int sample_rate, int channels)
: ctxPtr(0xFFFFFFFF), audioType(audioType), sample_rate_(sample_rate), channels_(channels),
SimpleAudio::SimpleAudio(PSPAudioType audioType, int sampleRateHz, int channels)
: ctxPtr(0xFFFFFFFF), audioType(audioType), sample_rate_(sampleRateHz), channels_(channels),
outSamples(0), srcPos(0),
frame_(0), codec_(0), codecCtx_(0), swrCtx_(0),
codecOpen_(false) {
@ -309,7 +309,7 @@ const char *GetCodecName(int codec) {
}
};
bool IsValidCodec(int codec){
bool IsValidCodec(PSPAudioType codec){
if (codec >= PSP_CODEC_AT3PLUS && codec <= PSP_CODEC_AAC) {
return true;
}
@ -535,7 +535,7 @@ void AuCtx::DoState(PointerWrap &p) {
Do(p, Channels);
Do(p, MaxOutputSample);
Do(p, readPos);
int audioType = decoder->GetAudioType();
int audioType = (int)decoder->GetAudioType();
Do(p, audioType);
Do(p, BitRate);
Do(p, SamplingRate);
@ -555,6 +555,6 @@ void AuCtx::DoState(PointerWrap &p) {
}
if (p.mode == p.MODE_READ) {
decoder = CreateAudioDecoder(audioType);
decoder = CreateAudioDecoder((PSPAudioType)audioType);
}
}

View file

@ -41,7 +41,7 @@ extern "C" {
// Based on http://ffmpeg.org/doxygen/trunk/doc_2examples_2decoding_encoding_8c-example.html#_a13
// audioType
enum {
enum PSPAudioType {
PSP_CODEC_AT3PLUS = 0x00001000,
PSP_CODEC_AT3 = 0x00001001,
PSP_CODEC_MP3 = 0x00001002,
@ -57,13 +57,13 @@ public:
virtual int GetOutSamples() const = 0;
virtual int GetSourcePos() const = 0;
virtual int GetAudioType() const = 0;
virtual PSPAudioType GetAudioType() const = 0;
};
// FFMPEG-based decoder
class SimpleAudio : public AudioDecoder {
public:
SimpleAudio(int audioType, int sample_rate = 44100, int channels = 2);
SimpleAudio(PSPAudioType audioType, int sampleRateHz = 44100, int channels = 2);
~SimpleAudio();
bool Decode(const uint8_t* inbuf, int inbytes, uint8_t *outbuf, int *outbytes) override;
@ -84,7 +84,7 @@ public:
void SetChannels(int channels);
// These two are only here because of save states.
int GetAudioType() const { return audioType; }
PSPAudioType GetAudioType() const { return audioType; }
// Just metadata.
void SetCtxPtr(u32 ptr) { ctxPtr = ptr; }
@ -94,7 +94,7 @@ private:
bool OpenCodec(int block_align);
u32 ctxPtr;
int audioType;
PSPAudioType audioType;
int sample_rate_;
int channels_;
int outSamples; // output samples per frame
@ -114,7 +114,8 @@ private:
void AudioClose(SimpleAudio **ctx);
void AudioClose(AudioDecoder **ctx);
const char *GetCodecName(int codec); // audioType
bool IsValidCodec(int codec);
bool IsValidCodec(PSPAudioType codec);
SimpleAudio *CreateAudioDecoder(PSPAudioType audioType, int sampleRateHz = 44100, int channels = 2);
class AuCtx {
public:

View file

@ -189,7 +189,7 @@ public:
wave_.Read(file_);
decoder_ = new SimpleAudio(wave_.codec, wave_.sample_rate, wave_.num_channels);
decoder_ = CreateAudioDecoder((PSPAudioType)wave_.codec, wave_.sample_rate, wave_.num_channels);
if (wave_.codec == PSP_CODEC_AT3) {
decoder_->SetExtraData(&wave_.at3_extradata[2], 14, wave_.raw_bytes_per_frame);
}