CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HLE/sceAudiocodec.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Common/Serialize/Serializer.h"
19
20
#include "Common/Serialize/SerializeFuncs.h"
21
#include "Core/HLE/HLE.h"
22
#include "Core/HLE/FunctionWrappers.h"
23
#include "Core/HLE/sceAudiocodec.h"
24
#include "Core/MemMap.h"
25
#include "Core/Reporting.h"
26
#include "Core/HW/SimpleAudioDec.h"
27
28
// Following kaien_fr's sample code https://github.com/hrydgard/ppsspp/issues/5620#issuecomment-37086024
29
// Should probably store the EDRAM get/release status somewhere within here, etc.
30
struct AudioCodecContext {
31
u32_le unknown[6];
32
u32_le inDataPtr; // 6
33
u32_le inDataSize; // 7
34
u32_le outDataPtr; // 8
35
u32_le audioSamplesPerFrame; // 9
36
u32_le inDataSizeAgain; // 10 ??
37
};
38
39
// audioList is to store current playing audios.
40
static std::map<u32, AudioDecoder *> audioList;
41
42
static bool oldStateLoaded = false;
43
44
// find the audio decoder for corresponding ctxPtr in audioList
45
static AudioDecoder *findDecoder(u32 ctxPtr) {
46
auto it = audioList.find(ctxPtr);
47
if (it != audioList.end()) {
48
return it->second;
49
}
50
return NULL;
51
}
52
53
// remove decoder from audioList
54
static bool removeDecoder(u32 ctxPtr) {
55
auto it = audioList.find(ctxPtr);
56
if (it != audioList.end()) {
57
delete it->second;
58
audioList.erase(it);
59
return true;
60
}
61
return false;
62
}
63
64
static void clearDecoders() {
65
for (const auto &[_, decoder] : audioList) {
66
delete decoder;
67
}
68
audioList.clear();
69
}
70
71
void __AudioCodecInit() {
72
oldStateLoaded = false;
73
}
74
75
void __AudioCodecShutdown() {
76
// We need to kill off any still opened codecs to not leak memory.
77
clearDecoders();
78
}
79
80
static int sceAudiocodecInit(u32 ctxPtr, int codec) {
81
PSPAudioType audioType = (PSPAudioType)codec;
82
if (IsValidCodec(audioType)) {
83
// Create audio decoder for given audio codec and push it into AudioList
84
if (removeDecoder(ctxPtr)) {
85
WARN_LOG_REPORT(Log::HLE, "sceAudiocodecInit(%08x, %d): replacing existing context", ctxPtr, codec);
86
}
87
AudioDecoder *decoder = CreateAudioDecoder(audioType);
88
decoder->SetCtxPtr(ctxPtr);
89
audioList[ctxPtr] = decoder;
90
INFO_LOG(Log::ME, "sceAudiocodecInit(%08x, %i (%s))", ctxPtr, codec, GetCodecName(audioType));
91
DEBUG_LOG(Log::ME, "Number of playing sceAudioCodec audios : %d", (int)audioList.size());
92
return 0;
93
}
94
ERROR_LOG_REPORT(Log::ME, "sceAudiocodecInit(%08x, %i (%s)): Unknown audio codec %i", ctxPtr, codec, GetCodecName(audioType), codec);
95
return 0;
96
}
97
98
static int sceAudiocodecDecode(u32 ctxPtr, int codec) {
99
PSPAudioType audioType = (PSPAudioType)codec;
100
if (!ctxPtr){
101
ERROR_LOG_REPORT(Log::ME, "sceAudiocodecDecode(%08x, %i (%s)) got NULL pointer", ctxPtr, codec, GetCodecName(audioType));
102
return -1;
103
}
104
105
if (IsValidCodec(audioType)){
106
// find a decoder in audioList
107
auto decoder = findDecoder(ctxPtr);
108
109
if (!decoder && oldStateLoaded) {
110
// We must have loaded an old state that did not have sceAudiocodec information.
111
// Fake it by creating the desired context.
112
decoder = CreateAudioDecoder(audioType);
113
decoder->SetCtxPtr(ctxPtr);
114
audioList[ctxPtr] = decoder;
115
}
116
117
if (decoder != NULL) {
118
// Use SimpleAudioDec to decode audio
119
auto ctx = PSPPointer<AudioCodecContext>::Create(ctxPtr); // On stack, no need to allocate.
120
// Decode audio
121
int inDataConsumed = 0;
122
int outSamples = 0;
123
decoder->Decode(Memory::GetPointer(ctx->inDataPtr), ctx->inDataSize, &inDataConsumed, 2, (int16_t *)Memory::GetPointerWrite(ctx->outDataPtr), &outSamples);
124
}
125
DEBUG_LOG(Log::ME, "sceAudiocodecDec(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
126
return 0;
127
}
128
ERROR_LOG_REPORT(Log::ME, "UNIMPL sceAudiocodecDecode(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
129
return 0;
130
}
131
132
static int sceAudiocodecGetInfo(u32 ctxPtr, int codec) {
133
ERROR_LOG_REPORT(Log::ME, "UNIMPL sceAudiocodecGetInfo(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
134
return 0;
135
}
136
137
static int sceAudiocodecCheckNeedMem(u32 ctxPtr, int codec) {
138
WARN_LOG(Log::ME, "UNIMPL sceAudiocodecCheckNeedMem(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
139
return 0;
140
}
141
142
static int sceAudiocodecGetEDRAM(u32 ctxPtr, int codec) {
143
WARN_LOG(Log::ME, "UNIMPL sceAudiocodecGetEDRAM(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
144
return 0;
145
}
146
147
static int sceAudiocodecReleaseEDRAM(u32 ctxPtr, int id) {
148
if (removeDecoder(ctxPtr)){
149
INFO_LOG(Log::ME, "sceAudiocodecReleaseEDRAM(%08x, %i)", ctxPtr, id);
150
return 0;
151
}
152
WARN_LOG(Log::ME, "UNIMPL sceAudiocodecReleaseEDRAM(%08x, %i)", ctxPtr, id);
153
return 0;
154
}
155
156
const HLEFunction sceAudiocodec[] = {
157
{0X70A703F8, &WrapI_UI<sceAudiocodecDecode>, "sceAudiocodecDecode", 'i', "xi"},
158
{0X5B37EB1D, &WrapI_UI<sceAudiocodecInit>, "sceAudiocodecInit", 'i', "xi"},
159
{0X8ACA11D5, &WrapI_UI<sceAudiocodecGetInfo>, "sceAudiocodecGetInfo", 'i', "xi"},
160
{0X3A20A200, &WrapI_UI<sceAudiocodecGetEDRAM>, "sceAudiocodecGetEDRAM", 'i', "xi"},
161
{0X29681260, &WrapI_UI<sceAudiocodecReleaseEDRAM>, "sceAudiocodecReleaseEDRAM", 'i', "xi"},
162
{0X9D3F790C, &WrapI_UI<sceAudiocodecCheckNeedMem>, "sceAudiocodecCheckNeedMem", 'i', "xi"},
163
{0X59176A0F, nullptr, "sceAudiocodecAlcExtendParameter", '?', "" },
164
};
165
166
void Register_sceAudiocodec()
167
{
168
RegisterModule("sceAudiocodec", ARRAY_SIZE(sceAudiocodec), sceAudiocodec);
169
}
170
171
void __sceAudiocodecDoState(PointerWrap &p){
172
auto s = p.Section("AudioList", 0, 2);
173
if (!s) {
174
oldStateLoaded = true;
175
return;
176
}
177
178
int count = (int)audioList.size();
179
Do(p, count);
180
181
if (count > 0) {
182
if (p.mode == PointerWrap::MODE_READ) {
183
clearDecoders();
184
185
// loadstate if audioList is nonempty
186
auto codec_ = new int[count];
187
auto ctxPtr_ = new u32[count];
188
// These sizeof(pointers) are wrong, but kept to avoid breaking on old saves.
189
// They're not used in new savestates.
190
#ifdef __clang__
191
#pragma clang diagnostic push
192
#pragma clang diagnostic ignored "-Wunknown-warning-option"
193
#pragma clang diagnostic ignored "-Wsizeof-pointer-div"
194
#elif defined(__GNUC__)
195
#pragma GCC diagnostic push
196
#pragma GCC diagnostic ignored "-Wsizeof-pointer-div"
197
#endif
198
DoArray(p, codec_, s >= 2 ? count : (int)ARRAY_SIZE(codec_));
199
DoArray(p, ctxPtr_, s >= 2 ? count : (int)ARRAY_SIZE(ctxPtr_));
200
for (int i = 0; i < count; i++) {
201
auto decoder = CreateAudioDecoder((PSPAudioType)codec_[i]);
202
decoder->SetCtxPtr(ctxPtr_[i]);
203
audioList[ctxPtr_[i]] = decoder;
204
}
205
#ifdef __clang__
206
#pragma clang diagnostic pop
207
#elif defined(__GNUC__)
208
#pragma GCC diagnostic pop
209
#endif
210
delete[] codec_;
211
delete[] ctxPtr_;
212
}
213
else
214
{
215
// savestate if audioList is nonempty
216
// Some of this is only necessary in Write but won't really hurt Measure.
217
auto codec_ = new int[count];
218
auto ctxPtr_ = new u32[count];
219
int i = 0;
220
for (auto iter : audioList) {
221
const AudioDecoder *decoder = iter.second;
222
codec_[i] = decoder->GetAudioType();
223
ctxPtr_[i] = decoder->GetCtxPtr();
224
i++;
225
}
226
DoArray(p, codec_, count);
227
DoArray(p, ctxPtr_, count);
228
delete[] codec_;
229
delete[] ctxPtr_;
230
}
231
}
232
}
233
234