#include "Common/Serialize/Serializer.h"
#include "Common/Serialize/SerializeFuncs.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/FunctionWrappers.h"
#include "Core/HLE/sceAudiocodec.h"
#include "Core/HLE/ErrorCodes.h"
#include "Core/MemMap.h"
#include "Core/Reporting.h"
#include "Core/HW/SimpleAudioDec.h"
std::map<u32, AudioDecoder *> g_audioDecoderContexts;
static bool oldStateLoaded = false;
static_assert(sizeof(SceAudiocodecCodec) == 128);
void CalculateInputBytesAndChannelsAt3Plus(const SceAudiocodecCodec *ctx, int *inputBytes, int *channels) {
*inputBytes = 0;
*channels = 2;
int size = ctx->unk41 * 8 + 8;
if (ctx->unk40 & 8) {
*channels = 2;
} else {
*channels = 1;
}
switch (size) {
case 0x118:
case 0x178:
case 0x230:
case 0x2E8:
*inputBytes = size;
return;
default:
break;
}
}
static AudioDecoder *findDecoder(u32 ctxPtr) {
auto it = g_audioDecoderContexts.find(ctxPtr);
if (it != g_audioDecoderContexts.end()) {
return it->second;
}
return NULL;
}
static bool removeDecoder(u32 ctxPtr) {
auto it = g_audioDecoderContexts.find(ctxPtr);
if (it != g_audioDecoderContexts.end()) {
delete it->second;
g_audioDecoderContexts.erase(it);
return true;
}
return false;
}
static void clearDecoders() {
for (const auto &[_, decoder] : g_audioDecoderContexts) {
delete decoder;
}
g_audioDecoderContexts.clear();
}
void __AudioCodecInit() {
oldStateLoaded = false;
}
void __AudioCodecShutdown() {
clearDecoders();
}
static int __AudioCodecInitCommon(u32 ctxPtr, int codec, bool mono) {
const PSPAudioType audioType = (PSPAudioType)codec;
if (!IsValidCodec(audioType)) {
return hleLogError(Log::ME, SCE_KERNEL_ERROR_OUT_OF_RANGE, "Invalid codec");
}
if (removeDecoder(ctxPtr)) {
WARN_LOG_REPORT(Log::HLE, "sceAudiocodecInit(%08x, %d): replacing existing context", ctxPtr, codec);
}
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr);
ctx->unk_init = 0x5100601;
ctx->err = 0;
int bytesPerFrame = 0;
int channels = 2;
uint8_t extraData[14]{};
SceAudiocodecCodec *ptr = ctx;
switch (audioType) {
case PSP_CODEC_MP3:
ctx->mp3_9999 = 9999;
break;
case PSP_CODEC_AAC:
break;
case PSP_CODEC_AT3PLUS:
CalculateInputBytesAndChannelsAt3Plus(ctx, &bytesPerFrame, &channels);
break;
case PSP_CODEC_AT3:
{
bytesPerFrame = 384;
bool jointStereo = IsAtrac3StreamJointStereo(PSP_CODEC_AT3, bytesPerFrame, channels);
extraData[0] = 1;
extraData[3] = channels << 3;
extraData[6] = jointStereo;
extraData[8] = jointStereo;
extraData[10] = 1;
break;
}
default:
break;
}
INFO_LOG(Log::ME, "sceAudioDecoder: Creating codec with %04x frame size and %d channels, codec %04x", bytesPerFrame, channels, codec);
AudioDecoder *decoder = CreateAudioDecoder(audioType, 44100, channels, bytesPerFrame, extraData, sizeof(extraData));
decoder->SetCtxPtr(ctxPtr);
g_audioDecoderContexts[ctxPtr] = decoder;
return hleLogDebug(Log::ME, 0);
}
static int sceAudiocodecInit(u32 ctxPtr, int codec) {
return __AudioCodecInitCommon(ctxPtr, codec, false);
}
static int sceAudiocodecInitMono(u32 ctxPtr, int codec) {
return __AudioCodecInitCommon(ctxPtr, codec, true);
}
static int sceAudiocodecDecode(u32 ctxPtr, int codec) {
PSPAudioType audioType = (PSPAudioType)codec;
if (!ctxPtr) {
ERROR_LOG(Log::ME, "sceAudiocodecDecode(%08x, %i (%s)) got NULL pointer", ctxPtr, codec, GetCodecName(audioType));
return -1;
}
if (!IsValidCodec(audioType)) {
return hleLogError(Log::ME, 0, "UNIMPL sceAudiocodecDecode(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
}
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr);
int bytesPerFrame = 0;
int channels = 2;
int sampleRate = 0;
switch (codec) {
case PSP_CODEC_AT3PLUS:
CalculateInputBytesAndChannelsAt3Plus(ctx, &bytesPerFrame, &channels);
break;
case PSP_CODEC_MP3:
bytesPerFrame = ctx->srcBytesRead;
break;
case PSP_CODEC_AAC:
bytesPerFrame = ctx->srcBytesRead;
sampleRate = ctx->formatOutSamples;
break;
case PSP_CODEC_AT3:
bytesPerFrame = 384;
break;
}
auto decoder = findDecoder(ctxPtr);
if (!decoder && oldStateLoaded) {
decoder = CreateAudioDecoder(audioType, 44100, channels, bytesPerFrame);
decoder->SetCtxPtr(ctxPtr);
g_audioDecoderContexts[ctxPtr] = decoder;
}
if (decoder) {
int inDataConsumed = 0;
int outSamples = 0;
DEBUG_LOG(Log::ME, "decoder. in: %08x out: %08x unk40: %02x unk41: %02x", ctx->inBuf, ctx->outBuf, ctx->unk40, ctx->unk41);
int16_t *outBuf = (int16_t *)Memory::GetPointerWrite(ctx->outBuf);
bool result = decoder->Decode(Memory::GetPointer(ctx->inBuf), bytesPerFrame, &inDataConsumed, 2, outBuf, &outSamples);
if (!result) {
ctx->err = 0x20b;
ERROR_LOG(Log::ME, "AudioCodec decode failed. Setting error to %08x", ctx->err);
}
ctx->srcBytesRead = inDataConsumed;
ctx->dstSamplesWritten = outSamples;
}
return hleLogDebug(Log::ME, 0, "codec %s", GetCodecName(codec));
}
static int sceAudiocodecGetInfo(u32 ctxPtr, int codec) {
if (codec < 0x1000 || codec >= 0x1006) {
return hleLogError(Log::ME, SCE_KERNEL_ERROR_BAD_ARGUMENT, "invalid codec");
}
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr);
switch (codec) {
case PSP_CODEC_MP3:
ctx->mp3_3 = 3;
ctx->mp3_9 = 9;
ctx->mp3_0 = 0;
ctx->mp3_1 = 1;
ctx->mp3_1_first = 1;
break;
}
return hleLogInfo(Log::ME, 0, "codec=%s", GetCodecName(codec));
}
static int sceAudiocodecCheckNeedMem(u32 ctxPtr, int codec) {
if (codec < 0x1000 || codec >= 0x1006) {
return hleLogError(Log::ME, SCE_KERNEL_ERROR_BAD_ARGUMENT, "invalid codec");
}
if (!Memory::IsValidRange(ctxPtr, sizeof(SceAudiocodecCodec))) {
return hleLogError(Log::ME, 0, "Bad address");
}
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr);
switch (codec) {
case 0x1000:
ctx->neededMem = 0x7bc0;
if (ctx->unk40 != 0x28 || ctx->unk41 != 0x5c) {
ctx->err = 0x20f;
return hleLogError(Log::ME, SCE_AVCODEC_ERROR_INVALID_DATA, "Bad format values: %02x %02x", ctx->unk40, ctx->unk41);
}
break;
case 0x1001:
ctx->neededMem = 0x3de0;
break;
case 0x1002:
ctx->neededMem = 0x3b68;
break;
case 0x1003:
INFO_LOG(Log::ME, "CheckNeedMem for codec %04x: format %02x %02x", codec, ctx->unk40, ctx->unk41);
break;
}
ctx->err = 0;
ctx->unk_init = 0x5100601;
return hleLogWarning(Log::ME, 0, "%s", GetCodecName(codec));
}
static int sceAudiocodecGetEDRAM(u32 ctxPtr, int codec) {
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr);
switch (codec) {
case PSP_CODEC_MP3:
ctx->allocMem = 0x001B3124;
break;
case PSP_CODEC_AT3:
default:
ctx->allocMem = 0x0018ea90;
break;
}
ctx->edramAddr = (ctx->allocMem + 0x3f) & ~0x3f;
return hleLogInfo(Log::ME, 0, "edram address set to %08x", ctx->edramAddr);
}
static int sceAudiocodecReleaseEDRAM(u32 ctxPtr, int id) {
if (removeDecoder(ctxPtr)){
return hleLogInfo(Log::ME, 0);
}
return hleLogWarning(Log::ME, 0, "failed to remove decoder");
}
static int sceAudiocodecGetOutputBytes(u32 ctxPtr, int codec, u32 outBytesAddr) {
if (!Memory::IsValid4AlignedAddress(outBytesAddr)) {
return hleLogError(Log::ME, SCE_MP3_ERROR_BAD_ADDR);
}
int bytes = 0;
switch (codec) {
case PSP_CODEC_AT3PLUS: bytes = 0x2000; break;
case PSP_CODEC_AT3: bytes = 0x1000; break;
case PSP_CODEC_MP3: bytes = 0x1200; break;
default:
return hleLogWarning(Log::ME, 0, "Block size query not implemented for codec %04x", codec);
}
Memory::WriteUnchecked_U32(bytes, outBytesAddr);
return hleLogInfo(Log::ME, 0);
}
struct At3HeaderMap {
u16 bytes;
u16 channels;
u8 jointStereo;
bool Matches(int bytesPerFrame, int encodedChannels) const {
return this->bytes == bytesPerFrame && this->channels == encodedChannels;
}
};
static const At3HeaderMap at3HeaderMap[] = {
{ 0x00C0, 1, 0 },
{ 0x0098, 1, 0 },
{ 0x0180, 2, 0 },
{ 0x0130, 2, 0 },
{ 0x00C0, 2, 1 },
};
bool IsAtrac3StreamJointStereo(int codecType, int bytesPerFrame, int channels) {
if (codecType != PSP_CODEC_AT3) {
return false;
}
for (size_t i = 0; i < ARRAY_SIZE(at3HeaderMap); ++i) {
if (at3HeaderMap[i].Matches(bytesPerFrame, channels)) {
return at3HeaderMap[i].jointStereo;
}
}
return false;
}
const HLEFunction sceAudiocodec[] = {
{0X70A703F8, &WrapI_UI<sceAudiocodecDecode>, "sceAudiocodecDecode", 'i', "xx"},
{0X5B37EB1D, &WrapI_UI<sceAudiocodecInit>, "sceAudiocodecInit", 'i', "xx"},
{0X8ACA11D5, &WrapI_UI<sceAudiocodecGetInfo>, "sceAudiocodecGetInfo", 'i', "xx"},
{0X3A20A200, &WrapI_UI<sceAudiocodecGetEDRAM>, "sceAudiocodecGetEDRAM", 'i', "xx"},
{0X29681260, &WrapI_UI<sceAudiocodecReleaseEDRAM>, "sceAudiocodecReleaseEDRAM", 'i', "xx"},
{0X9D3F790C, &WrapI_UI<sceAudiocodecCheckNeedMem>, "sceAudiocodecCheckNeedMem", 'i', "xx"},
{0X59176A0F, &WrapI_UIU<sceAudiocodecGetOutputBytes>, "sceAudiocodecGetOutputBytes", 'i', "xxp" },
{0X3DD7EE1A, &WrapI_UI<sceAudiocodecInitMono>, "sceAudiocodecInitMono", 'i', "xx"},
};
void Register_sceAudiocodec() {
RegisterHLEModule("sceAudiocodec", ARRAY_SIZE(sceAudiocodec), sceAudiocodec);
}
void __sceAudiocodecDoState(PointerWrap &p){
auto s = p.Section("AudioList", 0, 2);
if (!s) {
oldStateLoaded = true;
return;
}
int count = (int)g_audioDecoderContexts.size();
Do(p, count);
if (count > 0) {
if (p.mode == PointerWrap::MODE_READ) {
clearDecoders();
auto codec_ = new int[count];
auto ctxPtr_ = new u32[count];
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunknown-warning-option"
#pragma clang diagnostic ignored "-Wsizeof-pointer-div"
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsizeof-pointer-div"
#endif
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 = CreateAudioDecoder((PSPAudioType)codec_[i]);
decoder->SetCtxPtr(ctxPtr_[i]);
g_audioDecoderContexts[ctxPtr_[i]] = decoder;
}
#ifdef __clang__
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
delete[] codec_;
delete[] ctxPtr_;
}
else
{
auto codec_ = new int[count];
auto ctxPtr_ = new u32[count];
int i = 0;
for (auto iter : g_audioDecoderContexts) {
const AudioDecoder *decoder = iter.second;
codec_[i] = decoder->GetAudioType();
ctxPtr_[i] = decoder->GetCtxPtr();
i++;
}
DoArray(p, codec_, count);
DoArray(p, ctxPtr_, count);
delete[] codec_;
delete[] ctxPtr_;
}
}
}