2014-03-15 13:44:47 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#include "Core/HLE/HLE.h"
|
2014-03-15 18:22:19 +00:00
|
|
|
#include "Core/HLE/FunctionWrappers.h"
|
|
|
|
#include "Core/HLE/sceAudiocodec.h"
|
|
|
|
#include "Core/MemMap.h"
|
2014-03-15 13:44:47 +00:00
|
|
|
#include "Core/Reporting.h"
|
2014-03-23 18:45:08 +00:00
|
|
|
#include "Core/HW/SimpleAudioDec.h"
|
|
|
|
#include "Common/ChunkFile.h"
|
2014-03-15 13:44:47 +00:00
|
|
|
|
|
|
|
// Following kaien_fr's sample code https://github.com/hrydgard/ppsspp/issues/5620#issuecomment-37086024
|
|
|
|
// Should probably store the EDRAM get/release status somewhere within here, etc.
|
|
|
|
struct AudioCodecContext {
|
|
|
|
u32_le unknown[6];
|
|
|
|
u32_le inDataPtr; // 6
|
|
|
|
u32_le inDataSize; // 7
|
|
|
|
u32_le outDataPtr; // 8
|
|
|
|
u32_le audioSamplesPerFrame; // 9
|
|
|
|
u32_le inDataSizeAgain; // 10 ??
|
2014-03-23 18:45:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// audioList is to store current playing audios.
|
2014-05-29 15:16:47 +00:00
|
|
|
static std::map<u32, SimpleAudio *> audioList;
|
2014-03-23 18:45:08 +00:00
|
|
|
|
2014-03-27 21:53:57 +00:00
|
|
|
static bool oldStateLoaded = false;
|
|
|
|
|
2014-03-23 18:45:08 +00:00
|
|
|
// find the audio decoder for corresponding ctxPtr in audioList
|
2014-05-29 15:11:00 +00:00
|
|
|
static SimpleAudio *findDecoder(u32 ctxPtr) {
|
2014-05-29 15:16:47 +00:00
|
|
|
auto it = audioList.find(ctxPtr);
|
|
|
|
if (it != audioList.end()) {
|
|
|
|
return it->second;
|
2014-03-23 18:45:08 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove decoder from audioList
|
2014-05-29 15:11:00 +00:00
|
|
|
static bool removeDecoder(u32 ctxPtr) {
|
2014-05-29 15:16:47 +00:00
|
|
|
auto it = audioList.find(ctxPtr);
|
|
|
|
if (it != audioList.end()) {
|
|
|
|
delete it->second;
|
|
|
|
audioList.erase(it);
|
|
|
|
return true;
|
2014-03-23 18:45:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-15 13:44:47 +00:00
|
|
|
|
2014-05-29 15:11:00 +00:00
|
|
|
static void clearDecoders() {
|
2014-05-29 15:16:47 +00:00
|
|
|
for (auto it = audioList.begin(), end = audioList.end(); it != end; it++) {
|
|
|
|
delete it->second;
|
2014-05-29 15:11:00 +00:00
|
|
|
}
|
|
|
|
audioList.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-27 21:53:57 +00:00
|
|
|
void __AudioCodecInit() {
|
|
|
|
oldStateLoaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __AudioCodecShutdown() {
|
|
|
|
// We need to kill off any still opened codecs to not leak memory.
|
2014-05-29 15:11:00 +00:00
|
|
|
clearDecoders();
|
2014-03-27 21:53:57 +00:00
|
|
|
}
|
2014-03-15 13:44:47 +00:00
|
|
|
|
|
|
|
int sceAudiocodecInit(u32 ctxPtr, int codec) {
|
2014-06-22 12:01:23 +00:00
|
|
|
if (IsValidCodec(codec)) {
|
2014-03-23 18:45:08 +00:00
|
|
|
// Create audio decoder for given audio codec and push it into AudioList
|
2014-05-29 15:16:47 +00:00
|
|
|
if (removeDecoder(ctxPtr)) {
|
|
|
|
WARN_LOG_REPORT(HLE, "sceAudiocodecInit(%08x, %d): replacing existing context", ctxPtr, codec);
|
|
|
|
}
|
2014-06-22 15:02:04 +00:00
|
|
|
auto decoder = new SimpleAudio(codec);
|
|
|
|
decoder->SetCtxPtr(ctxPtr);
|
2014-05-29 15:16:47 +00:00
|
|
|
audioList[ctxPtr] = decoder;
|
2014-03-23 18:45:08 +00:00
|
|
|
INFO_LOG(ME, "sceAudiocodecInit(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
|
2014-05-21 14:59:58 +00:00
|
|
|
DEBUG_LOG(ME, "Number of playing sceAudioCodec audios : %d", (int)audioList.size());
|
2014-03-15 13:44:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-23 18:45:08 +00:00
|
|
|
ERROR_LOG_REPORT(ME, "sceAudiocodecInit(%08x, %i (%s)): Unknown audio codec %i", ctxPtr, codec, GetCodecName(codec), codec);
|
2014-03-15 13:44:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceAudiocodecDecode(u32 ctxPtr, int codec) {
|
|
|
|
if (!ctxPtr){
|
|
|
|
ERROR_LOG_REPORT(ME, "sceAudiocodecDecode(%08x, %i (%s)) got NULL pointer", ctxPtr, codec, GetCodecName(codec));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-27 21:53:57 +00:00
|
|
|
|
2014-06-22 12:01:23 +00:00
|
|
|
if (IsValidCodec(codec)){
|
2014-03-23 18:45:08 +00:00
|
|
|
// Use SimpleAudioDec to decode audio
|
2014-05-29 15:19:26 +00:00
|
|
|
auto ctx = PSPPointer<AudioCodecContext>::Create(ctxPtr); // On stack, no need to allocate.
|
2014-03-15 13:44:47 +00:00
|
|
|
int outbytes = 0;
|
2014-03-23 18:45:08 +00:00
|
|
|
// find a decoder in audioList
|
|
|
|
auto decoder = findDecoder(ctxPtr);
|
2014-03-27 21:53:57 +00:00
|
|
|
|
|
|
|
if (!decoder && oldStateLoaded) {
|
2014-05-29 15:16:47 +00:00
|
|
|
// We must have loaded an old state that did not have sceAudiocodec information.
|
2014-03-27 21:53:57 +00:00
|
|
|
// Fake it by creating the desired context.
|
2014-06-22 15:02:04 +00:00
|
|
|
decoder = new SimpleAudio(codec);
|
|
|
|
decoder->SetCtxPtr(ctxPtr);
|
2014-05-29 15:16:47 +00:00
|
|
|
audioList[ctxPtr] = decoder;
|
2014-03-27 21:53:57 +00:00
|
|
|
}
|
|
|
|
|
2014-05-29 15:16:47 +00:00
|
|
|
if (decoder != NULL) {
|
2014-03-27 14:10:53 +00:00
|
|
|
// Decode audio
|
2014-05-29 15:19:26 +00:00
|
|
|
decoder->Decode(Memory::GetPointer(ctx->inDataPtr), ctx->inDataSize, Memory::GetPointer(ctx->outDataPtr), &outbytes);
|
2014-03-23 18:45:08 +00:00
|
|
|
}
|
2014-05-29 15:16:47 +00:00
|
|
|
DEBUG_LOG(ME, "sceAudiocodecDec(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
|
2014-03-15 13:44:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ERROR_LOG_REPORT(ME, "UNIMPL sceAudiocodecDecode(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceAudiocodecGetInfo(u32 ctxPtr, int codec) {
|
|
|
|
ERROR_LOG_REPORT(ME, "UNIMPL sceAudiocodecGetInfo(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceAudiocodecCheckNeedMem(u32 ctxPtr, int codec) {
|
|
|
|
WARN_LOG(ME, "UNIMPL sceAudiocodecCheckNeedMem(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sceAudiocodecGetEDRAM(u32 ctxPtr, int codec) {
|
|
|
|
WARN_LOG(ME, "UNIMPL sceAudiocodecGetEDRAM(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-23 18:45:08 +00:00
|
|
|
int sceAudiocodecReleaseEDRAM(u32 ctxPtr, int id) {
|
|
|
|
if (removeDecoder(ctxPtr)){
|
|
|
|
INFO_LOG(ME, "sceAudiocodecReleaseEDRAM(%08x, %i)", ctxPtr, id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
WARN_LOG(ME, "UNIMPL sceAudiocodecReleaseEDRAM(%08x, %i)", ctxPtr, id);
|
2014-03-15 13:44:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const HLEFunction sceAudiocodec[] = {
|
2014-03-23 18:45:08 +00:00
|
|
|
{ 0x70A703F8, WrapI_UI<sceAudiocodecDecode>, "sceAudiocodecDecode" },
|
|
|
|
{ 0x5B37EB1D, WrapI_UI<sceAudiocodecInit>, "sceAudiocodecInit" },
|
|
|
|
{ 0x8ACA11D5, WrapI_UI<sceAudiocodecGetInfo>, "sceAudiocodecGetInfo" },
|
|
|
|
{ 0x3A20A200, WrapI_UI<sceAudiocodecGetEDRAM>, "sceAudiocodecGetEDRAM" },
|
|
|
|
{ 0x29681260, WrapI_UI<sceAudiocodecReleaseEDRAM>, "sceAudiocodecReleaseEDRAM" },
|
|
|
|
{ 0x9D3F790C, WrapI_UI<sceAudiocodecCheckNeedMem>, "sceAudiocodecCheckNeedMem" },
|
|
|
|
{ 0x59176a0f, 0, "sceAudiocodec_59176A0F" },
|
2014-03-15 13:44:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void Register_sceAudiocodec()
|
|
|
|
{
|
|
|
|
RegisterModule("sceAudiocodec", ARRAY_SIZE(sceAudiocodec), sceAudiocodec);
|
2014-03-23 18:45:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 14:10:53 +00:00
|
|
|
void __sceAudiocodecDoState(PointerWrap &p){
|
2014-05-29 05:45:51 +00:00
|
|
|
auto s = p.Section("AudioList", 0, 2);
|
2014-03-27 21:53:57 +00:00
|
|
|
if (!s) {
|
2014-05-29 05:45:51 +00:00
|
|
|
oldStateLoaded = true;
|
2014-03-27 14:10:53 +00:00
|
|
|
return;
|
2014-03-27 21:53:57 +00:00
|
|
|
}
|
2014-03-27 14:10:53 +00:00
|
|
|
|
2014-05-29 05:45:51 +00:00
|
|
|
int count = (int)audioList.size();
|
2014-03-27 14:10:53 +00:00
|
|
|
p.Do(count);
|
2014-05-29 05:45:51 +00:00
|
|
|
|
2014-03-27 21:53:57 +00:00
|
|
|
if (count > 0) {
|
2014-05-29 05:45:51 +00:00
|
|
|
if (p.mode == PointerWrap::MODE_READ) {
|
2014-05-29 15:11:00 +00:00
|
|
|
clearDecoders();
|
|
|
|
|
2014-03-27 14:10:53 +00:00
|
|
|
// loadstate if audioList is nonempty
|
|
|
|
auto codec_ = new int[count];
|
|
|
|
auto ctxPtr_ = new u32[count];
|
2014-05-29 05:45:51 +00:00
|
|
|
p.DoArray(codec_, s >= 2 ? count : (int)ARRAY_SIZE(codec_));
|
|
|
|
p.DoArray(ctxPtr_, s >= 2 ? count : (int)ARRAY_SIZE(ctxPtr_));
|
|
|
|
for (int i = 0; i < count; i++) {
|
2014-06-22 15:02:04 +00:00
|
|
|
auto decoder = new SimpleAudio(codec_[i]);
|
|
|
|
decoder->SetCtxPtr(ctxPtr_[i]);
|
2014-05-29 15:16:47 +00:00
|
|
|
audioList[ctxPtr_[i]] = decoder;
|
2014-03-27 14:10:53 +00:00
|
|
|
}
|
|
|
|
delete[] codec_;
|
|
|
|
delete[] ctxPtr_;
|
|
|
|
}
|
2014-03-27 21:53:57 +00:00
|
|
|
else
|
|
|
|
{
|
2014-03-27 14:10:53 +00:00
|
|
|
// savestate if audioList is nonempty
|
2014-03-27 21:53:57 +00:00
|
|
|
// Some of this is only necessary in Write but won't really hurt Measure.
|
2014-03-27 14:10:53 +00:00
|
|
|
auto codec_ = new int[count];
|
|
|
|
auto ctxPtr_ = new u32[count];
|
|
|
|
int i = 0;
|
2014-05-29 15:16:47 +00:00
|
|
|
for (auto it = audioList.begin(), end = audioList.end(); it != end; it++) {
|
|
|
|
const SimpleAudio *decoder = it->second;
|
2014-06-22 12:01:23 +00:00
|
|
|
codec_[i] = decoder->GetAudioType();
|
|
|
|
ctxPtr_[i] = decoder->GetCtxPtr();
|
2014-03-27 14:10:53 +00:00
|
|
|
i++;
|
|
|
|
}
|
2014-05-29 05:49:49 +00:00
|
|
|
p.DoArray(codec_, count);
|
|
|
|
p.DoArray(ctxPtr_, count);
|
2014-03-27 14:10:53 +00:00
|
|
|
delete[] codec_;
|
|
|
|
delete[] ctxPtr_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|