Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HLE/sceAudiocodec.cpp
3186 views
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/HLE/ErrorCodes.h"
25
#include "Core/MemMap.h"
26
#include "Core/Reporting.h"
27
#include "Core/HW/SimpleAudioDec.h"
28
29
// Following kaien_fr's sample code https://github.com/hrydgard/ppsspp/issues/5620#issuecomment-37086024
30
// Should probably store the EDRAM get/release status somewhere within here, etc.
31
32
// g_audioDecoderContexts is to store current playing audios.
33
std::map<u32, AudioDecoder *> g_audioDecoderContexts;
34
35
static bool oldStateLoaded = false;
36
37
static_assert(sizeof(SceAudiocodecCodec) == 128);
38
39
// Atrac3+ (0x1000) frame sizes, and control bytes
40
//
41
// Bitrate Frame Size Byte 1 Byte 2 Channels
42
// -----------------------------------------------------
43
// 48kbps 0x118 0x24 0x22 1? // This hits "Frame data doesn't match channel configuration".
44
// 64kbps 0x178
45
// 96kbps? 0x230 0x28 0x45 2
46
// 128kbps 0x2E8 0x28 0x5c 2
47
//
48
// Seems like maybe the frame size is equal to "Byte 2" * 8 + 8
49
//
50
// Known byte values.
51
52
// Atrac3 (0x1001)
53
//
54
// Frame size data byte JointStereo?
55
// -------------------------------------------------
56
// 0x180 0x04 0
57
// 0x130 0x06 0
58
// 0x0C0 0x0B 1
59
// 0x0C0 0x0E 0
60
// 0x098 0x0F 0
61
62
// AAC (0x1003)
63
// ------------------------------------------------
64
// Sample rate is at offset 0x28.
65
// srcBytesConsumed can be very small the first frames.
66
// 0x1000 is always the frame size.
67
68
// MP3 (0x1002)
69
// ------------------------------------------------
70
71
72
void CalculateInputBytesAndChannelsAt3Plus(const SceAudiocodecCodec *ctx, int *inputBytes, int *channels) {
73
*inputBytes = 0;
74
*channels = 2;
75
76
int size = ctx->unk41 * 8 + 8;
77
// No idea if this is accurate, this is just a guess...
78
if (ctx->unk40 & 8) {
79
*channels = 2;
80
} else {
81
*channels = 1;
82
}
83
switch (size) {
84
case 0x118:
85
case 0x178:
86
case 0x230:
87
case 0x2E8:
88
// These have been seen before, let's return it.
89
*inputBytes = size;
90
return;
91
default:
92
break;
93
}
94
}
95
96
// find the audio decoder for corresponding ctxPtr in audioList
97
static AudioDecoder *findDecoder(u32 ctxPtr) {
98
auto it = g_audioDecoderContexts.find(ctxPtr);
99
if (it != g_audioDecoderContexts.end()) {
100
return it->second;
101
}
102
return NULL;
103
}
104
105
// remove decoder from audioList
106
static bool removeDecoder(u32 ctxPtr) {
107
auto it = g_audioDecoderContexts.find(ctxPtr);
108
if (it != g_audioDecoderContexts.end()) {
109
delete it->second;
110
g_audioDecoderContexts.erase(it);
111
return true;
112
}
113
return false;
114
}
115
116
static void clearDecoders() {
117
for (const auto &[_, decoder] : g_audioDecoderContexts) {
118
delete decoder;
119
}
120
g_audioDecoderContexts.clear();
121
}
122
123
void __AudioCodecInit() {
124
oldStateLoaded = false;
125
}
126
127
void __AudioCodecShutdown() {
128
// We need to kill off any still opened codecs to not leak memory.
129
clearDecoders();
130
}
131
132
// TODO: Actually support mono output.
133
static int __AudioCodecInitCommon(u32 ctxPtr, int codec, bool mono) {
134
const PSPAudioType audioType = (PSPAudioType)codec;
135
if (!IsValidCodec(audioType)) {
136
return hleLogError(Log::ME, SCE_KERNEL_ERROR_OUT_OF_RANGE, "Invalid codec");
137
}
138
139
if (removeDecoder(ctxPtr)) {
140
WARN_LOG_REPORT(Log::HLE, "sceAudiocodecInit(%08x, %d): replacing existing context", ctxPtr, codec);
141
}
142
143
// Initialize the codec memory.
144
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr);
145
ctx->unk_init = 0x5100601; // Firmware version indicator?
146
ctx->err = 0;
147
148
int bytesPerFrame = 0;
149
int channels = 2;
150
151
uint8_t extraData[14]{};
152
153
SceAudiocodecCodec *ptr = ctx;
154
// Special actions for some codecs.
155
switch (audioType) {
156
case PSP_CODEC_MP3:
157
// Not seeing inited in Kurok (homebrew)
158
// _dbg_assert_(ctx->inited == 1);
159
ctx->mp3_9999 = 9999;
160
break;
161
case PSP_CODEC_AAC:
162
// AAC / mp4
163
// offsets 40-42 are a 24-bit LE number specifying the sample rate. It's 32000, 44100 or 48000.
164
// neededMem has been set to 0x18f20.
165
break;
166
case PSP_CODEC_AT3PLUS:
167
CalculateInputBytesAndChannelsAt3Plus(ctx, &bytesPerFrame, &channels);
168
break;
169
case PSP_CODEC_AT3:
170
{
171
// See AtracBase::CreateDecoder. Need to properly understand this one day..
172
//
173
// TODO: How do we get the bytesPerFrame?
174
bytesPerFrame = 384; // TODO: Calculate from params.
175
bool jointStereo = IsAtrac3StreamJointStereo(PSP_CODEC_AT3, bytesPerFrame, channels);
176
// The only thing that changes are the jointStereo_ values.
177
extraData[0] = 1;
178
extraData[3] = channels << 3;
179
extraData[6] = jointStereo;
180
extraData[8] = jointStereo;
181
extraData[10] = 1;
182
break;
183
}
184
default:
185
break;
186
}
187
188
// Create audio decoder for given audio codec and push it into AudioList
189
INFO_LOG(Log::ME, "sceAudioDecoder: Creating codec with %04x frame size and %d channels, codec %04x", bytesPerFrame, channels, codec);
190
// We send in extra data with all codec, most ignore it.
191
AudioDecoder *decoder = CreateAudioDecoder(audioType, 44100, channels, bytesPerFrame, extraData, sizeof(extraData));
192
decoder->SetCtxPtr(ctxPtr);
193
g_audioDecoderContexts[ctxPtr] = decoder;
194
return hleLogDebug(Log::ME, 0);
195
}
196
197
static int sceAudiocodecInit(u32 ctxPtr, int codec) {
198
return __AudioCodecInitCommon(ctxPtr, codec, false);
199
}
200
201
static int sceAudiocodecInitMono(u32 ctxPtr, int codec) {
202
return __AudioCodecInitCommon(ctxPtr, codec, true);
203
}
204
205
static int sceAudiocodecDecode(u32 ctxPtr, int codec) {
206
PSPAudioType audioType = (PSPAudioType)codec;
207
if (!ctxPtr) {
208
ERROR_LOG(Log::ME, "sceAudiocodecDecode(%08x, %i (%s)) got NULL pointer", ctxPtr, codec, GetCodecName(audioType));
209
return -1;
210
}
211
212
if (!IsValidCodec(audioType)) {
213
return hleLogError(Log::ME, 0, "UNIMPL sceAudiocodecDecode(%08x, %i (%s))", ctxPtr, codec, GetCodecName(codec));
214
}
215
216
// TODO: Should check that codec corresponds to the currently used codec in the context, I guess..
217
218
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr); // On game-owned heap, no need to allocate.
219
220
int bytesPerFrame = 0;
221
int channels = 2;
222
int sampleRate = 0;
223
224
switch (codec) {
225
case PSP_CODEC_AT3PLUS:
226
CalculateInputBytesAndChannelsAt3Plus(ctx, &bytesPerFrame, &channels);
227
break;
228
case PSP_CODEC_MP3:
229
bytesPerFrame = ctx->srcBytesRead;
230
break;
231
case PSP_CODEC_AAC:
232
bytesPerFrame = ctx->srcBytesRead;
233
sampleRate = ctx->formatOutSamples;
234
break;
235
case PSP_CODEC_AT3:
236
bytesPerFrame = 384;
237
break;
238
}
239
240
// find a decoder in audioList
241
auto decoder = findDecoder(ctxPtr);
242
243
if (!decoder && oldStateLoaded) {
244
// We must have loaded an old state that did not have sceAudiocodec information.
245
// Fake it by creating the desired context.
246
decoder = CreateAudioDecoder(audioType, 44100, channels, bytesPerFrame);
247
decoder->SetCtxPtr(ctxPtr);
248
g_audioDecoderContexts[ctxPtr] = decoder;
249
}
250
251
if (decoder) {
252
// Use SimpleAudioDec to decode audio
253
// Decode audio
254
int inDataConsumed = 0;
255
int outSamples = 0;
256
257
DEBUG_LOG(Log::ME, "decoder. in: %08x out: %08x unk40: %02x unk41: %02x", ctx->inBuf, ctx->outBuf, ctx->unk40, ctx->unk41);
258
259
int16_t *outBuf = (int16_t *)Memory::GetPointerWrite(ctx->outBuf);
260
261
bool result = decoder->Decode(Memory::GetPointer(ctx->inBuf), bytesPerFrame, &inDataConsumed, 2, outBuf, &outSamples);
262
if (!result) {
263
ctx->err = 0x20b;
264
ERROR_LOG(Log::ME, "AudioCodec decode failed. Setting error to %08x", ctx->err);
265
}
266
267
ctx->srcBytesRead = inDataConsumed;
268
ctx->dstSamplesWritten = outSamples;
269
}
270
return hleLogDebug(Log::ME, 0, "codec %s", GetCodecName(codec));
271
}
272
273
// This is used by sceMp3, in Beats.
274
// Is the return value the only output?
275
static int sceAudiocodecGetInfo(u32 ctxPtr, int codec) {
276
if (codec < 0x1000 || codec >= 0x1006) {
277
return hleLogError(Log::ME, SCE_KERNEL_ERROR_BAD_ARGUMENT, "invalid codec");
278
}
279
280
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr); // On game-owned heap, no need to allocate.
281
282
// Write some expected values.
283
switch (codec) {
284
case PSP_CODEC_MP3:
285
// When this is called, the caller has written:
286
// * inptr
287
// * outptr
288
// * formatOutSamples = 0x5A1
289
// Our response is written to a bunch of fields, but I really don't know much
290
// about what the values are - this is handled internally in the ME.
291
ctx->mp3_3 = 3;
292
ctx->mp3_9 = 9;
293
ctx->mp3_0 = 0;
294
ctx->mp3_1 = 1;
295
ctx->mp3_1_first = 1;
296
break;
297
}
298
299
return hleLogInfo(Log::ME, 0, "codec=%s", GetCodecName(codec));
300
}
301
302
static int sceAudiocodecCheckNeedMem(u32 ctxPtr, int codec) {
303
if (codec < 0x1000 || codec >= 0x1006) {
304
return hleLogError(Log::ME, SCE_KERNEL_ERROR_BAD_ARGUMENT, "invalid codec");
305
}
306
307
if (!Memory::IsValidRange(ctxPtr, sizeof(SceAudiocodecCodec))) {
308
return hleLogError(Log::ME, 0, "Bad address");
309
}
310
311
// Check for expected values.
312
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr); // On game-owned heap, no need to allocate.
313
314
switch (codec) {
315
case 0x1000:
316
ctx->neededMem = 0x7bc0;
317
if (ctx->unk40 != 0x28 || ctx->unk41 != 0x5c) {
318
ctx->err = 0x20f;
319
return hleLogError(Log::ME, SCE_AVCODEC_ERROR_INVALID_DATA, "Bad format values: %02x %02x", ctx->unk40, ctx->unk41);
320
}
321
break;
322
case 0x1001:
323
ctx->neededMem = 0x3de0;
324
break;
325
case 0x1002:
326
ctx->neededMem = 0x3b68;
327
break;
328
case 0x1003:
329
// Kosmodrones uses sceAudiocodec directly (no intermediate library).
330
INFO_LOG(Log::ME, "CheckNeedMem for codec %04x: format %02x %02x", codec, ctx->unk40, ctx->unk41);
331
break;
332
}
333
334
ctx->err = 0;
335
ctx->unk_init = 0x5100601;
336
337
return hleLogWarning(Log::ME, 0, "%s", GetCodecName(codec));
338
}
339
340
static int sceAudiocodecGetEDRAM(u32 ctxPtr, int codec) {
341
auto ctx = PSPPointer<SceAudiocodecCodec>::Create(ctxPtr); // On game-owned heap, no need to allocate.
342
// TODO: Set this a bit more dynamically. No idea what the allocation algorithm is...
343
switch (codec) {
344
case PSP_CODEC_MP3:
345
ctx->allocMem = 0x001B3124;
346
break;
347
case PSP_CODEC_AT3:
348
default:
349
ctx->allocMem = 0x0018ea90;
350
break;
351
}
352
ctx->edramAddr = (ctx->allocMem + 0x3f) & ~0x3f; // round up to 64 bytes.
353
return hleLogInfo(Log::ME, 0, "edram address set to %08x", ctx->edramAddr);
354
}
355
356
static int sceAudiocodecReleaseEDRAM(u32 ctxPtr, int id) {
357
if (removeDecoder(ctxPtr)){
358
return hleLogInfo(Log::ME, 0);
359
}
360
return hleLogWarning(Log::ME, 0, "failed to remove decoder");
361
}
362
363
static int sceAudiocodecGetOutputBytes(u32 ctxPtr, int codec, u32 outBytesAddr) {
364
if (!Memory::IsValid4AlignedAddress(outBytesAddr)) {
365
// Not tested
366
return hleLogError(Log::ME, SCE_MP3_ERROR_BAD_ADDR);
367
}
368
369
int bytes = 0;
370
switch (codec) {
371
case PSP_CODEC_AT3PLUS: bytes = 0x2000; break;
372
case PSP_CODEC_AT3: bytes = 0x1000; break; // Atrac3
373
case PSP_CODEC_MP3: bytes = 0x1200; break;
374
default:
375
return hleLogWarning(Log::ME, 0, "Block size query not implemented for codec %04x", codec);
376
}
377
378
Memory::WriteUnchecked_U32(bytes, outBytesAddr);
379
return hleLogInfo(Log::ME, 0);
380
}
381
382
struct At3HeaderMap {
383
u16 bytes;
384
u16 channels;
385
u8 jointStereo;
386
387
bool Matches(int bytesPerFrame, int encodedChannels) const {
388
return this->bytes == bytesPerFrame && this->channels == encodedChannels;
389
}
390
};
391
392
// These should represent all possible supported bitrates (66, 104, and 132 for stereo.)
393
static const At3HeaderMap at3HeaderMap[] = {
394
{ 0x00C0, 1, 0 }, // 132/2 (66) kbps mono
395
{ 0x0098, 1, 0 }, // 105/2 (52.5) kbps mono
396
{ 0x0180, 2, 0 }, // 132 kbps stereo
397
{ 0x0130, 2, 0 }, // 105 kbps stereo
398
// At this size, stereo can only use joint stereo.
399
{ 0x00C0, 2, 1 }, // 66 kbps stereo
400
};
401
402
bool IsAtrac3StreamJointStereo(int codecType, int bytesPerFrame, int channels) {
403
if (codecType != PSP_CODEC_AT3) {
404
// Well, might actually be, but it's not used in codec setup.
405
return false;
406
}
407
408
for (size_t i = 0; i < ARRAY_SIZE(at3HeaderMap); ++i) {
409
if (at3HeaderMap[i].Matches(bytesPerFrame, channels)) {
410
return at3HeaderMap[i].jointStereo;
411
}
412
}
413
414
// Not found? Should we log?
415
return false;
416
}
417
418
419
const HLEFunction sceAudiocodec[] = {
420
{0X70A703F8, &WrapI_UI<sceAudiocodecDecode>, "sceAudiocodecDecode", 'i', "xx"},
421
{0X5B37EB1D, &WrapI_UI<sceAudiocodecInit>, "sceAudiocodecInit", 'i', "xx"},
422
{0X8ACA11D5, &WrapI_UI<sceAudiocodecGetInfo>, "sceAudiocodecGetInfo", 'i', "xx"},
423
{0X3A20A200, &WrapI_UI<sceAudiocodecGetEDRAM>, "sceAudiocodecGetEDRAM", 'i', "xx"},
424
{0X29681260, &WrapI_UI<sceAudiocodecReleaseEDRAM>, "sceAudiocodecReleaseEDRAM", 'i', "xx"},
425
{0X9D3F790C, &WrapI_UI<sceAudiocodecCheckNeedMem>, "sceAudiocodecCheckNeedMem", 'i', "xx"},
426
{0X59176A0F, &WrapI_UIU<sceAudiocodecGetOutputBytes>, "sceAudiocodecGetOutputBytes", 'i', "xxp" }, // params are context, codec, outptr
427
{0X3DD7EE1A, &WrapI_UI<sceAudiocodecInitMono>, "sceAudiocodecInitMono", 'i', "xx"}, // Used by sceAtrac for MOut* functions.
428
};
429
430
void Register_sceAudiocodec() {
431
RegisterHLEModule("sceAudiocodec", ARRAY_SIZE(sceAudiocodec), sceAudiocodec);
432
}
433
434
void __sceAudiocodecDoState(PointerWrap &p){
435
auto s = p.Section("AudioList", 0, 2);
436
if (!s) {
437
oldStateLoaded = true;
438
return;
439
}
440
441
int count = (int)g_audioDecoderContexts.size();
442
Do(p, count);
443
444
if (count > 0) {
445
if (p.mode == PointerWrap::MODE_READ) {
446
clearDecoders();
447
448
// loadstate if audioList is nonempty
449
auto codec_ = new int[count];
450
auto ctxPtr_ = new u32[count];
451
// These sizeof(pointers) are wrong, but kept to avoid breaking on old saves.
452
// They're not used in new savestates.
453
#ifdef __clang__
454
#pragma clang diagnostic push
455
#pragma clang diagnostic ignored "-Wunknown-warning-option"
456
#pragma clang diagnostic ignored "-Wsizeof-pointer-div"
457
#elif defined(__GNUC__)
458
#pragma GCC diagnostic push
459
#pragma GCC diagnostic ignored "-Wsizeof-pointer-div"
460
#endif
461
DoArray(p, codec_, s >= 2 ? count : (int)ARRAY_SIZE(codec_));
462
DoArray(p, ctxPtr_, s >= 2 ? count : (int)ARRAY_SIZE(ctxPtr_));
463
for (int i = 0; i < count; i++) {
464
auto decoder = CreateAudioDecoder((PSPAudioType)codec_[i]);
465
decoder->SetCtxPtr(ctxPtr_[i]);
466
g_audioDecoderContexts[ctxPtr_[i]] = decoder;
467
}
468
#ifdef __clang__
469
#pragma clang diagnostic pop
470
#elif defined(__GNUC__)
471
#pragma GCC diagnostic pop
472
#endif
473
delete[] codec_;
474
delete[] ctxPtr_;
475
}
476
else
477
{
478
// savestate if audioList is nonempty
479
// Some of this is only necessary in Write but won't really hurt Measure.
480
auto codec_ = new int[count];
481
auto ctxPtr_ = new u32[count];
482
int i = 0;
483
for (auto iter : g_audioDecoderContexts) {
484
const AudioDecoder *decoder = iter.second;
485
codec_[i] = decoder->GetAudioType();
486
ctxPtr_[i] = decoder->GetCtxPtr();
487
i++;
488
}
489
DoArray(p, codec_, count);
490
DoArray(p, ctxPtr_, count);
491
delete[] codec_;
492
delete[] ctxPtr_;
493
}
494
}
495
}
496
497