Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HLE/sceAtrac.cpp
3187 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 <algorithm>
19
20
#include "Common/Serialize/Serializer.h"
21
#include "Common/Serialize/SerializeFuncs.h"
22
#include "Core/HLE/HLE.h"
23
#include "Core/HLE/ErrorCodes.h"
24
#include "Core/HLE/FunctionWrappers.h"
25
#include "Core/MIPS/MIPS.h"
26
#include "Core/CoreTiming.h"
27
#include "Core/MemMapHelpers.h"
28
#include "Core/Reporting.h"
29
#include "Core/Config.h"
30
#include "Core/Debugger/MemBlockInfo.h"
31
#include "Core/HW/BufferQueue.h"
32
33
#include "Core/HLE/sceKernel.h"
34
#include "Core/HLE/sceUtility.h"
35
#include "Core/HLE/sceKernelMemory.h"
36
#include "Core/HLE/sceAtrac.h"
37
#include "Core/HLE/AtracCtx.h"
38
#include "Core/HLE/AtracCtx2.h"
39
#include "Core/System.h"
40
41
// Notes about sceAtrac buffer management
42
//
43
// sceAtrac decodes from a buffer the game fills, where this buffer has a status, one of:
44
//
45
// * Not yet initialized (state NO_DATA = 1)
46
// * The entire size of the audio data, and filled with audio data (state ALL_DATA_LOADED = 2)
47
// * The entire size, but only partially filled so far (state HALFWAY_BUFFER = 3)
48
// * Smaller than the audio, sliding without any loop (state STREAMED_WITHOUT_LOOP = 4)
49
// * Smaller than the audio, sliding with a loop at the end (state STREAMED_WITH_LOOP_AT_END = 5)
50
// * Smaller with a second buffer to help with a loop in the middle (state STREAMED_WITH_SECOND_BUF = 6)
51
// * Not managed, decoding using "low level" manual looping etc. (LOW_LEVEL = 8)
52
// * Not managed, reserved externally - possibly by sceSas - through low level (RESERVED = 16)
53
//
54
// When streaming (modes 3-6), a game will call sceAtracGetStreamDataInfo to figure out what data
55
// to read and where to place it, and after doing that it'll call sceAtracAddStreamData with the amount
56
// of data it actually read. This will move the various pointers forward.
57
// Similarly, for a game to seek, it'll call sceAtracGetBufferInfoForResetting with a sample offset,
58
// and read that data into the buffer.
59
//
60
// State 6 indicates a second buffer is needed. This buffer is used to manage looping correctly.
61
// To determine how to fill it, the game will call sceAtracGetSecondBufferInfo, then after filling
62
// the buffer it will call sceAtracSetSecondBuffer.
63
//
64
// The second buffer will just contain the data for the end of loop. The "first" buffer may manage
65
// only the looped portion, or some of the part after the loop (depending on second buf size.)
66
//
67
// TODO: What games use this?
68
//
69
// Most files will be in RIFF format. It's also possible to load in an OMA/AA3 format file, but
70
// ultimately this works the same, just the loading process is a little different.
71
//
72
// Low level decoding doesn't use the buffer, and decodes only a single packet at a time.
73
//
74
// Lastly, sceSas has some integration with sceAtrac, which allows setting an Atrac id as
75
// a voice for an SAS core. In this mode, the game will directly modify some of the context,
76
// but will largely only interact using sceSas.
77
//
78
// Note that this buffer is THE view of the audio stream. On a PSP, the firmware does not manage
79
// any cache or separate version of the buffer - at most it manages decode state from earlier in
80
// the buffer. Also, our Atrac2 context implementation works like this.
81
82
// TODO: We should add checks that the utility module is loaded.
83
84
static const int atracDecodeDelay = 2300;
85
86
static bool atracInited = true;
87
static AtracBase *atracContexts[PSP_MAX_ATRAC_IDS];
88
static u32 atracContextTypes[PSP_MAX_ATRAC_IDS];
89
static int atracLibVersion = 0;
90
static u32 atracLibCrc = 0;
91
static int g_atracMaxContexts = 6;
92
static int g_atracBSS = 0;
93
94
static bool g_muteFlag[PSP_MAX_ATRAC_IDS]{}; // Not saved, just for debugging.
95
96
bool *__AtracMuteFlag(int atracID) {
97
if (atracID < 0 || atracID >= PSP_MAX_ATRAC_IDS) {
98
return nullptr;
99
}
100
return &g_muteFlag[atracID];
101
}
102
103
// For debugger only.
104
const AtracBase *__AtracGetCtx(int i, u32 *type) {
105
if (type) {
106
*type = atracContextTypes[i];
107
}
108
return atracContexts[i];
109
}
110
111
void __AtracInit() {
112
_assert_(sizeof(SceAtracContext) == 256);
113
114
atracLibVersion = 0;
115
atracLibCrc = 0;
116
g_atracMaxContexts = 6;
117
g_atracBSS = 0;
118
119
atracInited = true; // TODO: This should probably only happen in __AtracNotifyLoadModule.
120
121
memset(atracContexts, 0, sizeof(atracContexts));
122
memset(g_muteFlag, 0, sizeof(g_muteFlag));
123
124
// Start with 2 of each in this order.
125
atracContextTypes[0] = PSP_CODEC_AT3PLUS;
126
atracContextTypes[1] = PSP_CODEC_AT3PLUS;
127
atracContextTypes[2] = PSP_CODEC_AT3;
128
atracContextTypes[3] = PSP_CODEC_AT3;
129
atracContextTypes[4] = 0;
130
atracContextTypes[5] = 0;
131
}
132
133
void __AtracShutdown() {
134
for (size_t i = 0; i < ARRAY_SIZE(atracContexts); ++i) {
135
delete atracContexts[i];
136
atracContexts[i] = nullptr;
137
}
138
}
139
140
int __AtracMaxContexts() {
141
return g_atracMaxContexts;
142
}
143
144
void __AtracNotifyLoadModule(int version, u32 crc, u32 bssAddr, int bssSize) {
145
atracLibVersion = version;
146
atracLibCrc = crc;
147
INFO_LOG(Log::Atrac, "Atrac module loaded: atracLibVersion 0x%0x, atracLibcrc %x, bss: %x (%x bytes)", atracLibVersion, atracLibCrc, bssAddr, bssSize);
148
g_atracBSS = bssAddr;
149
g_atracMaxContexts = atracLibVersion <= 0x101 ? 4 : 6; // Need to figure out where the cutoff is.
150
_dbg_assert_(bssSize >= g_atracMaxContexts * sizeof(SceAtracContext));
151
Memory::Memset(g_atracBSS, 0, g_atracMaxContexts * sizeof(SceAtracContext));
152
NotifyMemInfo(MemBlockFlags::ALLOC, g_atracBSS, g_atracMaxContexts * sizeof(SceAtracContext), "AtracContext");
153
}
154
155
void __AtracNotifyUnloadModule() {
156
atracLibVersion = 0;
157
atracLibCrc = 0;
158
INFO_LOG(Log::Atrac, "Atrac module unloaded.");
159
g_atracBSS = 0;
160
g_atracMaxContexts = 6; // TODO: We should make this zero here.
161
NotifyMemInfo(MemBlockFlags::FREE, g_atracBSS, g_atracMaxContexts * sizeof(SceAtracContext), "AtracContext");
162
}
163
164
static u32 GetAtracContextAddress(int atracID) {
165
return g_atracBSS + atracID * sizeof(SceAtracContext);
166
}
167
168
void __AtracDoState(PointerWrap &p) {
169
auto s = p.Section("sceAtrac", 1, 4);
170
if (!s)
171
return;
172
173
Do(p, atracInited);
174
if (s >= 4) {
175
Do(p, g_atracBSS);
176
Do(p, g_atracMaxContexts);
177
if (g_atracMaxContexts > PSP_MAX_ATRAC_IDS) // paranoia
178
g_atracMaxContexts = PSP_MAX_ATRAC_IDS;
179
} else {
180
g_atracBSS = 0;
181
g_atracMaxContexts = 6;
182
}
183
for (int i = 0; i < PSP_MAX_ATRAC_IDS; ++i) {
184
bool valid = atracContexts[i] != nullptr;
185
Do(p, valid);
186
if (valid) {
187
int version = atracContexts[i] ? atracContexts[i]->GetContextVersion() : 0;
188
if (s >= 4) {
189
Do(p, version);
190
_dbg_assert_(version != 0);
191
} else {
192
// Old versions only support old contexts.
193
version = 1;
194
}
195
switch (version) {
196
case 1:
197
DoSubClass<AtracBase, Atrac>(p, atracContexts[i], i);
198
break;
199
case 2:
200
DoSubClass<AtracBase, Atrac2>(p, atracContexts[i]);
201
break;
202
}
203
} else {
204
delete atracContexts[i];
205
atracContexts[i] = nullptr;
206
}
207
}
208
DoArray(p, atracContextTypes, PSP_MAX_ATRAC_IDS);
209
if (s >= 2) {
210
Do(p, atracLibVersion);
211
Do(p, atracLibCrc);
212
}
213
else {
214
atracLibVersion = 0;
215
atracLibCrc = 0;
216
}
217
}
218
219
static AtracBase *getAtrac(int atracID) {
220
if (atracID < 0 || atracID >= PSP_MAX_ATRAC_IDS) {
221
return nullptr;
222
}
223
AtracBase *atrac = atracContexts[atracID];
224
if (atrac) {
225
atrac->UpdateContextFromPSPMem();
226
}
227
return atrac;
228
}
229
230
static int AllocAndRegisterAtrac(int codecType) {
231
for (int i = 0; i < g_atracMaxContexts; ++i) {
232
if (atracContextTypes[i] == codecType && atracContexts[i] == 0) {
233
if (!g_Config.bUseOldAtrac && g_atracBSS != 0) {
234
atracContexts[i] = new Atrac2(GetAtracContextAddress(i), codecType);
235
} else {
236
atracContexts[i] = new Atrac(i, codecType);
237
}
238
return i;
239
}
240
}
241
return SCE_ERROR_ATRAC_NO_ATRACID;
242
}
243
244
static int UnregisterAndDeleteAtrac(int atracID) {
245
if (atracID >= 0 && atracID < PSP_MAX_ATRAC_IDS) {
246
if (atracContexts[atracID] != nullptr) {
247
delete atracContexts[atracID];
248
atracContexts[atracID] = nullptr;
249
return 0;
250
}
251
}
252
return SCE_ERROR_ATRAC_BAD_ATRACID;
253
}
254
255
// Really, allocate an Atrac context of a specific codec type.
256
// Useful to initialize a context for low level decode.
257
static u32 sceAtracGetAtracID(int codecType) {
258
if (codecType != PSP_CODEC_AT3 && codecType != PSP_CODEC_AT3PLUS) {
259
return hleReportError(Log::Atrac, SCE_ERROR_ATRAC_INVALID_CODECTYPE, "invalid codecType");
260
}
261
262
int atracID = AllocAndRegisterAtrac(codecType);
263
if (atracID < 0) {
264
return hleLogError(Log::Atrac, atracID, "no free ID");
265
}
266
267
return hleLogInfo(Log::Atrac, atracID);
268
}
269
270
static int AtracValidateData(const AtracBase *atrac) {
271
if (!atrac) {
272
return SCE_ERROR_ATRAC_BAD_ATRACID;
273
} else if (atrac->BufferState() == ATRAC_STATUS_NO_DATA) {
274
return SCE_ERROR_ATRAC_NO_DATA;
275
} else {
276
return 0;
277
}
278
}
279
280
static int AtracValidateManaged(const AtracBase *atrac) {
281
if (!atrac) {
282
return SCE_ERROR_ATRAC_BAD_ATRACID;
283
} else if (atrac->BufferState() == ATRAC_STATUS_NO_DATA) {
284
return SCE_ERROR_ATRAC_NO_DATA;
285
} else if (atrac->BufferState() == ATRAC_STATUS_LOW_LEVEL) {
286
return SCE_ERROR_ATRAC_IS_LOW_LEVEL;
287
} else if (atrac->BufferState() == ATRAC_STATUS_FOR_SCESAS) {
288
return SCE_ERROR_ATRAC_IS_FOR_SCESAS;
289
} else {
290
return 0;
291
}
292
}
293
294
// Notifies that more data is (OR will be very soon) available in the buffer.
295
// This implies it has been added to whatever position sceAtracGetStreamDataInfo would indicate.
296
//
297
// The total size of the buffer is atrac->bufferMaxSize_.
298
static u32 sceAtracAddStreamData(int atracID, u32 bytesToAdd) {
299
AtracBase *atrac = getAtrac(atracID);
300
int err = AtracValidateManaged(atrac);
301
if (err != 0) {
302
return hleLogError(Log::Atrac, err);
303
}
304
305
if (atrac->BufferState() == ATRAC_STATUS_ALL_DATA_LOADED) {
306
// Let's avoid spurious warnings. Some games call this with 0 which is pretty harmless.
307
if (bytesToAdd == 0)
308
return hleLogDebug(Log::Atrac, SCE_ERROR_ATRAC_ALL_DATA_LOADED, "stream entirely loaded");
309
return hleLogWarning(Log::Atrac, SCE_ERROR_ATRAC_ALL_DATA_LOADED, "stream entirely loaded");
310
}
311
312
int ret = atrac->AddStreamData(bytesToAdd);
313
return hleLogDebugOrError(Log::Atrac, ret);
314
}
315
316
// Note that outAddr being null is completely valid here, used to skip data.
317
static u32 sceAtracDecodeData(int atracID, u32 outAddr, u32 numSamplesAddr, u32 finishFlagAddr, u32 remainAddr) {
318
AtracBase *atrac = getAtrac(atracID);
319
u32 err = AtracValidateData(atrac);
320
if (err != 0) {
321
return hleLogError(Log::Atrac, err);
322
}
323
324
if (outAddr & 1) {
325
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_BAD_ALIGNMENT);
326
}
327
328
int numSamplesWritten = 0;
329
int finish = 0;
330
int remains = 0;
331
if (outAddr != 0 && !Memory::IsValidAddress(outAddr)) {
332
// Dunno what error code to return here, but we have to bail in this case to avoid crashing PPSSPP.
333
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_SIZE_TOO_SMALL);
334
}
335
336
u8 *outPtr = outAddr ? Memory::GetPointerWrite(outAddr) : nullptr;
337
338
int ret = atrac->DecodeData(outPtr, outAddr, &numSamplesWritten, &finish, &remains);
339
if (ret != (int)SCE_ERROR_ATRAC_BAD_ATRACID && ret != (int)SCE_ERROR_ATRAC_NO_DATA) {
340
if (Memory::IsValidAddress(numSamplesAddr))
341
Memory::WriteUnchecked_U32(numSamplesWritten, numSamplesAddr);
342
if (Memory::IsValidAddress(finishFlagAddr))
343
Memory::WriteUnchecked_U32(finish, finishFlagAddr);
344
// On error, no remaining frame value is written.
345
if (ret == 0 && Memory::IsValidAddress(remainAddr))
346
Memory::WriteUnchecked_U32(remains, remainAddr);
347
}
348
DEBUG_LOG(Log::Atrac, "%08x=sceAtracDecodeData(%i, %08x, %08x[%08x], %08x[%08x], %08x[%d])", ret, atracID, outAddr,
349
numSamplesAddr, numSamplesWritten,
350
finishFlagAddr, finish,
351
remainAddr, remains);
352
353
if (outPtr && g_muteFlag[atracID]) {
354
memset(outPtr, 0, atrac->GetOutputChannels() * 2 * numSamplesWritten);
355
}
356
357
if (ret == 0 || ret == SCE_ERROR_ATRAC_API_FAIL) {
358
// Decoded or at least attempted to decode data, delay thread
359
return hleDelayResult(hleNoLog(ret), "atrac decode data", atracDecodeDelay);
360
}
361
362
return hleNoLog(ret);
363
}
364
365
static u32 sceAtracReleaseResources() {
366
// In the real implementation, this just calls sceAudioCodecReleaseEDRAM if any has been allocated.
367
return hleLogDebug(Log::Atrac, 0);
368
}
369
370
// Obtains information about what needs to be in the buffer to seek (or "reset")
371
// Generally called by games right before calling sceAtracResetPlayPosition().
372
static u32 sceAtracGetBufferInfoForResetting(int atracID, int sample, u32 bufferInfoAddr) {
373
auto bufferInfo = PSPPointer<AtracResetBufferInfo>::Create(bufferInfoAddr);
374
375
AtracBase *atrac = getAtrac(atracID);
376
u32 err = AtracValidateManaged(atrac);
377
if (err != 0) {
378
return hleLogError(Log::Atrac, err);
379
}
380
381
if (!bufferInfo.IsValid()) {
382
return hleLogError(Log::Atrac, SCE_KERNEL_ERROR_ILLEGAL_ADDR, "invalid buffer, should crash");
383
}
384
385
// Note: If we error here, it's because of the internal SkipFrames.
386
// We delayresult if we skip frames, which indeed can happen.
387
bool delay = false;
388
int ret = atrac->GetBufferInfoForResetting(bufferInfo, sample, &delay);
389
if (delay) {
390
return hleDelayResult(hleLogDebugOrError(Log::Atrac, ret), "getreset_frameskip", 300);
391
} else {
392
return hleLogDebugOrError(Log::Atrac, ret);
393
}
394
}
395
396
static u32 sceAtracGetBitrate(int atracID, u32 outBitrateAddr) {
397
AtracBase *atrac = getAtrac(atracID);
398
u32 err = AtracValidateData(atrac);
399
if (err != 0) {
400
return hleLogError(Log::Atrac, err);
401
}
402
403
int bitrate = atrac->Bitrate();
404
if (Memory::IsValidAddress(outBitrateAddr)) {
405
Memory::WriteUnchecked_U32(atrac->Bitrate(), outBitrateAddr);
406
return hleLogDebug(Log::Atrac, 0);
407
} else {
408
return hleLogError(Log::Atrac, 0, "invalid address");
409
}
410
}
411
412
static u32 sceAtracGetChannel(int atracID, u32 channelAddr) {
413
AtracBase *atrac = getAtrac(atracID);
414
u32 err = AtracValidateData(atrac);
415
if (err != 0) {
416
return hleLogError(Log::Atrac, err);
417
}
418
419
if (Memory::IsValidAddress(channelAddr)){
420
Memory::WriteUnchecked_U32(atrac->Channels(), channelAddr);
421
return hleLogDebug(Log::Atrac, 0);
422
} else {
423
return hleLogError(Log::Atrac, 0, "invalid address");
424
}
425
}
426
427
static u32 sceAtracGetLoopStatus(int atracID, u32 loopNumAddr, u32 statusAddr) {
428
AtracBase *atrac = getAtrac(atracID);
429
u32 err = AtracValidateData(atrac);
430
if (err != 0) {
431
return hleLogError(Log::Atrac, err);
432
}
433
434
if (Memory::IsValidAddress(loopNumAddr)) {
435
Memory::WriteUnchecked_U32(atrac->LoopNum(), loopNumAddr);
436
}
437
438
if (Memory::IsValidAddress(statusAddr)) {
439
const int loopStatus = atrac->LoopStatus();
440
Memory::WriteUnchecked_U32(loopStatus, statusAddr);
441
return hleLogDebug(Log::Atrac, 0);
442
} else {
443
return hleLogError(Log::Atrac, 0, "invalid address");
444
}
445
}
446
447
static u32 sceAtracGetInternalErrorInfo(int atracID, u32 errorAddr) {
448
AtracBase *atrac = getAtrac(atracID);
449
u32 err = AtracValidateData(atrac);
450
if (err != 0) {
451
return hleLogError(Log::Atrac, err);
452
}
453
454
const u32 errorCode = atrac->GetInternalCodecError();
455
if (Memory::IsValidAddress(errorAddr)) {
456
Memory::WriteUnchecked_U32(errorCode, errorAddr);
457
}
458
459
if (errorCode) {
460
return hleLogWarning(Log::Atrac, 0, "code: %08x", errorCode);
461
} else {
462
return hleLogDebug(Log::Atrac, 0);
463
}
464
}
465
466
static u32 sceAtracGetMaxSample(int atracID, u32 maxSamplesAddr) {
467
AtracBase *atrac = getAtrac(atracID);
468
u32 err = AtracValidateData(atrac);
469
if (err != 0) {
470
return hleLogError(Log::Atrac, err);
471
}
472
473
if (Memory::IsValidAddress(maxSamplesAddr)) {
474
Memory::WriteUnchecked_U32(atrac->SamplesPerFrame(), maxSamplesAddr);
475
return hleLogDebug(Log::Atrac, 0);
476
} else {
477
return hleLogError(Log::Atrac, 0, "invalid address");
478
}
479
}
480
481
static u32 sceAtracGetNextDecodePosition(int atracID, u32 outposAddr) {
482
AtracBase *atrac = getAtrac(atracID);
483
u32 err = AtracValidateData(atrac);
484
if (err != 0) {
485
return hleLogError(Log::Atrac, err);
486
}
487
488
if (!Memory::IsValidAddress(outposAddr)) {
489
return hleLogError(Log::Atrac, 0, "invalid address");
490
}
491
492
int pos = 0;
493
int ret = atrac->GetNextDecodePosition(&pos);
494
if (ret < 0) {
495
if (ret == SCE_ERROR_ATRAC_ALL_DATA_DECODED) {
496
// Benign.
497
return hleLogDebug(Log::Atrac, ret);
498
} else {
499
return hleLogError(Log::Atrac, ret);
500
}
501
}
502
503
Memory::WriteUnchecked_U32(pos, outposAddr);
504
return hleLogDebug(Log::Atrac, 0);
505
}
506
507
static u32 sceAtracGetNextSample(int atracID, u32 outNAddr) {
508
AtracBase *atrac = getAtrac(atracID);
509
u32 err = AtracValidateData(atrac);
510
if (err != 0) {
511
return hleLogError(Log::Atrac, err);
512
}
513
514
int numSamples = atrac->GetNextSamples();
515
if (Memory::IsValidAddress(outNAddr)) {
516
Memory::WriteUnchecked_U32(numSamples, outNAddr);
517
}
518
return hleLogDebug(Log::Atrac, 0, "%d samples left", numSamples);
519
}
520
521
// Obtains the number of frames remaining in the buffer which can be decoded.
522
// When no more data would be needed, this returns a negative number.
523
static u32 sceAtracGetRemainFrame(int atracID, u32 remainAddr) {
524
AtracBase *atrac = getAtrac(atracID);
525
u32 err = AtracValidateManaged(atrac);
526
if (err != 0) {
527
return hleLogError(Log::Atrac, err);
528
}
529
530
if (!Memory::IsValidAddress(remainAddr)) {
531
// Would crash.
532
return hleReportError(Log::Atrac, SCE_KERNEL_ERROR_ILLEGAL_ADDR, "invalid remainingFrames pointer");
533
}
534
535
u32 remaining = atrac->RemainingFrames();
536
Memory::WriteUnchecked_U32(remaining, remainAddr);
537
return hleLogDebug(Log::Atrac, 0);
538
}
539
540
static u32 sceAtracGetSecondBufferInfo(int atracID, u32 fileOffsetAddr, u32 desiredSizeAddr) {
541
auto fileOffset = PSPPointer<u32_le>::Create(fileOffsetAddr);
542
auto desiredSize = PSPPointer<u32_le>::Create(desiredSizeAddr);
543
544
AtracBase *atrac = getAtrac(atracID);
545
u32 err = AtracValidateManaged(atrac);
546
if (err != 0) {
547
return hleLogError(Log::Atrac, err);
548
}
549
550
if (!fileOffset.IsValid() || !desiredSize.IsValid()) {
551
// Would crash.
552
return hleReportError(Log::Atrac, SCE_KERNEL_ERROR_ILLEGAL_ADDR, "invalid addresses");
553
}
554
555
int result = atrac->GetSecondBufferInfo(fileOffset, desiredSize);
556
switch (result) {
557
case (int)SCE_ERROR_ATRAC_SECOND_BUFFER_NOT_NEEDED:
558
return hleLogDebug(Log::Atrac, result);
559
default:
560
return hleLogDebugOrError(Log::Atrac, result);
561
}
562
}
563
564
static u32 sceAtracGetSoundSample(int atracID, u32 outEndSampleAddr, u32 outLoopStartSampleAddr, u32 outLoopEndSampleAddr) {
565
AtracBase *atrac = getAtrac(atracID);
566
u32 err = AtracValidateManaged(atrac);
567
if (err != 0) {
568
return hleLogError(Log::Atrac, err);
569
}
570
571
int endSample = -1;
572
int loopStart = -1;
573
int loopEnd = -1;
574
int retval = atrac->GetSoundSample(&endSample, &loopStart, &loopEnd);
575
if (retval < 0) {
576
return hleLogError(Log::Atrac, retval);
577
}
578
if (Memory::IsValidAddress(outEndSampleAddr)) {
579
Memory::WriteUnchecked_U32(endSample, outEndSampleAddr);
580
}
581
if (Memory::IsValidAddress(outLoopStartSampleAddr)) {
582
Memory::WriteUnchecked_U32(loopStart, outLoopStartSampleAddr);
583
}
584
if (Memory::IsValidAddress(outLoopEndSampleAddr)) {
585
Memory::WriteUnchecked_U32(loopEnd, outLoopEndSampleAddr);
586
}
587
return hleLogDebug(Log::Atrac, retval);
588
}
589
590
// Games call this function to get some info for add more stream data,
591
// such as where the data read from, where the data add to,
592
// and how many bytes are allowed to add.
593
static u32 sceAtracGetStreamDataInfo(int atracID, u32 writePtrAddr, u32 writableBytesAddr, u32 readOffsetAddr) {
594
AtracBase *atrac = getAtrac(atracID);
595
u32 err = AtracValidateManaged(atrac);
596
if (err != 0) {
597
return hleLogError(Log::Atrac, err);
598
}
599
600
u32 writePtr;
601
u32 writableBytes;
602
u32 readOffset;
603
atrac->GetStreamDataInfo(&writePtr, &writableBytes, &readOffset);
604
605
if (Memory::IsValidAddress(writePtrAddr))
606
Memory::WriteUnchecked_U32(writePtr, writePtrAddr);
607
if (Memory::IsValidAddress(writableBytesAddr))
608
Memory::WriteUnchecked_U32(writableBytes, writableBytesAddr);
609
if (Memory::IsValidAddress(readOffsetAddr))
610
Memory::WriteUnchecked_U32(readOffset, readOffsetAddr);
611
612
return hleLogDebug(Log::Atrac, 0);
613
}
614
615
static u32 sceAtracReleaseAtracID(int atracID) {
616
int result = UnregisterAndDeleteAtrac(atracID);
617
if (result < 0) {
618
if (atracID >= 0) {
619
return hleLogError(Log::Atrac, result, "did not exist");
620
} else {
621
return hleLogWarning(Log::Atrac, result, "did not exist");
622
}
623
}
624
return hleLogDebug(Log::Atrac, result);
625
}
626
627
// This is called when a game wants to seek (or "reset") to a specific position in the audio data.
628
// Normally, sceAtracGetBufferInfoForResetting() is called to determine how to buffer.
629
// The game must add sufficient packets to the buffer in order to complete the seek.
630
static u32 sceAtracResetPlayPosition(int atracID, int sample, int bytesWrittenFirstBuf, int bytesWrittenSecondBuf) {
631
AtracBase *atrac = getAtrac(atracID);
632
u32 err = AtracValidateManaged(atrac);
633
if (err != 0) {
634
return hleLogError(Log::Atrac, err);
635
}
636
637
bool delay = false;
638
int res = atrac->ResetPlayPosition(sample, bytesWrittenFirstBuf, bytesWrittenSecondBuf, &delay);
639
if (res < 0) {
640
if (delay) {
641
return hleDelayResult(hleLogError(Log::Atrac, res), "reset play pos", 200);
642
} else {
643
return hleLogError(Log::Atrac, res);
644
}
645
}
646
647
return hleDelayResult(res, "reset play pos", 3000);
648
}
649
650
static u32 sceAtracSetHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u32 bufferSize) {
651
AtracBase *atrac = getAtrac(atracID);
652
// Don't use AtracValidateManaged here.
653
if (!atrac) {
654
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_BAD_ATRACID, "invalid atrac ID");
655
}
656
657
if (readSize > bufferSize) {
658
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large");
659
}
660
661
Track track;
662
std::string error;
663
int ret = AnalyzeAtracTrack(Memory::GetPointerOrNull(buffer), readSize, &track, &error);
664
if (ret < 0) {
665
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
666
}
667
if (track.codecType != atracContextTypes[atracID]) {
668
// TODO: Should this not change the buffer size?
669
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_WRONG_CODECTYPE, "atracID uses different codec type than data");
670
}
671
672
ret = atrac->SetData(track, buffer, readSize, bufferSize, 0, 2, false);
673
if (ret < 0) {
674
// Must not delay.
675
return hleLogError(Log::Atrac, ret);
676
}
677
678
// not sure the real delay time
679
return hleDelayResult(hleLogDebug(Log::Atrac, ret), "atrac set data", 100);
680
}
681
682
static u32 sceAtracSetSecondBuffer(int atracID, u32 secondBuffer, u32 secondBufferSize) {
683
AtracBase *atrac = getAtrac(atracID);
684
u32 err = AtracValidateManaged(atrac);
685
if (err != 0) {
686
return hleLogError(Log::Atrac, err);
687
}
688
return hleLogDebugOrError(Log::Atrac, atrac->SetSecondBuffer(secondBuffer, secondBufferSize));
689
}
690
691
static u32 sceAtracSetData(int atracID, u32 buffer, u32 bufferSize) {
692
AtracBase *atrac = getAtrac(atracID);
693
if (!atrac) {
694
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_BAD_ATRACID, "bad atrac ID");
695
}
696
697
Track track;
698
std::string error;
699
int ret = AnalyzeAtracTrack(Memory::GetPointerOrNull(buffer), bufferSize, &track, &error);
700
if (ret < 0) {
701
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
702
}
703
if (track.codecType != atracContextTypes[atracID]) {
704
// TODO: Should this not change the buffer size?
705
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_WRONG_CODECTYPE, "atracID uses different codec type than data");
706
}
707
708
ret = atrac->SetData(track, buffer, bufferSize, bufferSize, 0, 2, false);
709
if (ret < 0) {
710
// Must not delay.
711
return hleLogError(Log::Atrac, ret);
712
}
713
714
return hleDelayResult(hleLogDebug(Log::Atrac, ret), "atrac set data", 100);
715
}
716
717
static int sceAtracSetDataAndGetID(u32 buffer, int bufferSize) {
718
// A large value happens in Tales of VS, and isn't handled somewhere properly as a u32.
719
// It's impossible for it to be that big anyway, so cap it.
720
if (bufferSize < 0) {
721
WARN_LOG(Log::Atrac, "sceAtracSetDataAndGetID(%08x, %08x): negative bufferSize", buffer, bufferSize);
722
bufferSize = 0x10000000;
723
}
724
725
Track track;
726
std::string error;
727
// We let zero-pointer through as zeroes, so the small size check can kick in before it's accessed.
728
// This is needed in Tomb Raider Legend (#20145).
729
int ret = AnalyzeAtracTrack(Memory::GetPointerOrNull(buffer), bufferSize, &track, &error);
730
if (ret < 0) {
731
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
732
}
733
734
int atracID = AllocAndRegisterAtrac(track.codecType);
735
if (atracID < 0) {
736
return hleLogError(Log::Atrac, atracID, "no free ID");
737
}
738
739
ret = atracContexts[atracID]->SetData(track, buffer, bufferSize, bufferSize, 0, 2, false);
740
if (ret < 0) {
741
UnregisterAndDeleteAtrac(atracID);
742
return hleLogError(Log::Atrac, ret);
743
}
744
745
return hleDelayResult(hleLogDebug(Log::Atrac, atracID), "atrac set data", 100);
746
}
747
748
static int sceAtracSetHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bufferSize) {
749
if (readSize > bufferSize) {
750
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large");
751
}
752
753
Track track;
754
std::string error;
755
int ret = AnalyzeAtracTrack(Memory::GetPointerOrNull(buffer), readSize, &track, &error);
756
if (ret < 0) {
757
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
758
}
759
760
int atracID = AllocAndRegisterAtrac(track.codecType);
761
if (atracID < 0) {
762
return hleLogError(Log::Atrac, atracID, "no free ID");
763
}
764
765
ret = atracContexts[atracID]->SetData(track, buffer, readSize, bufferSize, 0, 2, false);
766
if (ret < 0) {
767
UnregisterAndDeleteAtrac(atracID);
768
return hleLogError(Log::Atrac, ret);
769
}
770
771
return hleDelayResult(hleLogDebug(Log::Atrac, atracID), "atrac set data", 100);
772
}
773
774
static u32 sceAtracStartEntry() {
775
ERROR_LOG_REPORT(Log::Atrac, "UNIMPL sceAtracStartEntry()");
776
return 0;
777
}
778
779
static u32 sceAtracSetLoopNum(int atracID, int loopNum) {
780
AtracBase *atrac = getAtrac(atracID);
781
u32 err = AtracValidateData(atrac);
782
if (err != 0) {
783
return hleLogError(Log::Atrac, err);
784
}
785
786
int ret = atrac->SetLoopNum(loopNum);
787
if (ret == SCE_ERROR_ATRAC_NO_LOOP_INFORMATION && (loopNum == -1 || loopNum == 0)) {
788
// Not really an issue
789
return hleLogDebug(Log::Atrac, ret);
790
}
791
return hleLogDebugOrError(Log::Atrac, ret);
792
}
793
794
static int sceAtracReinit(int at3Count, int at3plusCount) {
795
for (int i = 0; i < PSP_MAX_ATRAC_IDS; ++i) {
796
if (atracContexts[i] != nullptr) {
797
return hleReportError(Log::Atrac, SCE_KERNEL_ERROR_BUSY, "cannot reinit while IDs in use");
798
}
799
}
800
801
memset(atracContextTypes, 0, sizeof(atracContextTypes));
802
int next = 0;
803
int space = g_atracMaxContexts;
804
805
// This seems to deinit things. Mostly, it cause a reschedule on next deinit (but -1, -1 does not.)
806
if (at3Count == 0 && at3plusCount == 0) {
807
atracInited = false;
808
return hleDelayResult(hleLogInfo(Log::Atrac, 0, "deinit"), "atrac reinit", 200);
809
}
810
811
// First, ATRAC3+. These IDs seem to cost double (probably memory.)
812
// Intentionally signed. 9999 tries to allocate, -1 does not.
813
for (int i = 0; i < at3plusCount; ++i) {
814
space -= 2;
815
if (space >= 0) {
816
atracContextTypes[next++] = PSP_CODEC_AT3PLUS;
817
}
818
}
819
for (int i = 0; i < at3Count; ++i) {
820
space -= 1;
821
if (space >= 0) {
822
atracContextTypes[next++] = PSP_CODEC_AT3;
823
}
824
}
825
826
// If we ran out of space, we still initialize some, but return an error.
827
int result = space >= 0 ? 0 : (int)SCE_KERNEL_ERROR_OUT_OF_MEMORY;
828
if (atracInited || next == 0) {
829
atracInited = true;
830
return hleLogInfo(Log::Atrac, result);
831
} else {
832
atracInited = true;
833
return hleDelayResult(hleLogInfo(Log::Atrac, result), "atrac reinit", 400);
834
}
835
}
836
837
static int sceAtracGetOutputChannel(int atracID, u32 outputChanPtr) {
838
AtracBase *atrac = getAtrac(atracID);
839
u32 err = AtracValidateData(atrac);
840
if (err != 0) {
841
return hleLogError(Log::Atrac, err);
842
}
843
if (Memory::IsValidAddress(outputChanPtr)) {
844
Memory::WriteUnchecked_U32(atrac->GetOutputChannels(), outputChanPtr);
845
return hleLogDebug(Log::Atrac, 0);
846
} else {
847
return hleLogError(Log::Atrac, 0, "invalid address");
848
}
849
}
850
851
static int sceAtracIsSecondBufferNeeded(int atracID) {
852
AtracBase *atrac = getAtrac(atracID);
853
u32 err = AtracValidateManaged(atrac);
854
if (err != 0) {
855
return hleLogError(Log::Atrac, err);
856
}
857
858
// Note that this returns true whether the buffer is already set or not.
859
int needed = atrac->BufferState() == ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER ? 1 : 0;
860
return hleLogDebug(Log::Atrac, needed);
861
}
862
863
static int sceAtracSetMOutHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u32 bufferSize) {
864
AtracBase *atrac = getAtrac(atracID);
865
// Don't use AtracValidate* here.
866
if (!atrac) {
867
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_BAD_ATRACID, "bad atrac ID");
868
}
869
if (readSize > bufferSize) {
870
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large");
871
}
872
873
Track track;
874
std::string error;
875
int ret = AnalyzeAtracTrack(Memory::GetPointerOrNull(buffer), readSize, &track, &error);
876
if (ret < 0) {
877
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
878
}
879
880
ret = atrac->SetData(track, buffer, readSize, bufferSize, 1, 0, false);
881
if (ret < 0 && ret != SCE_ERROR_ATRAC_NOT_MONO) {
882
// Must not delay.
883
return hleLogError(Log::Atrac, ret);
884
}
885
return hleDelayResult(hleLogDebugOrError(Log::Atrac, ret), "atrac set data mono", 100);
886
}
887
888
// Note: This doesn't seem to be part of any available libatrac3plus library.
889
// So we should probably remove it?
890
// HalfwayBuffer can fully replace it though, of course (just set readSize == bufferSize).
891
static u32 sceAtracSetMOutData(int atracID, u32 buffer, u32 bufferSize) {
892
AtracBase *atrac = getAtrac(atracID);
893
// Don't use AtracValidate* here.
894
if (!atrac) {
895
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_BAD_ATRACID, "bad atrac ID");
896
}
897
898
Track track;
899
std::string error;
900
int ret = AnalyzeAtracTrack(Memory::GetPointerOrNull(buffer), bufferSize, &track, &error);
901
if (ret < 0) {
902
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
903
}
904
905
ret = atrac->SetData(track, buffer, bufferSize, bufferSize, 1, 0, false);
906
if (ret < 0 && ret != SCE_ERROR_ATRAC_NOT_MONO) {
907
// Must not delay.
908
return hleLogError(Log::Atrac, ret);
909
}
910
// It's OK if this fails, at least with NO_MONO...
911
return hleDelayResult(hleLogDebugOrError(Log::Atrac, ret), "atrac set data mono", 100);
912
}
913
914
// Note: This doesn't seem to be part of any available libatrac3plus library.
915
// See note in above function.
916
static int sceAtracSetMOutDataAndGetID(u32 buffer, u32 bufferSize) {
917
Track track;
918
std::string error;
919
int ret = AnalyzeAtracTrack(Memory::GetPointerOrNull(buffer), bufferSize, &track, &error);
920
if (ret < 0) {
921
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
922
}
923
924
if (track.channels != 1) {
925
return hleReportError(Log::Atrac, SCE_ERROR_ATRAC_NOT_MONO, "not mono data");
926
}
927
928
int atracID = AllocAndRegisterAtrac(track.codecType);
929
if (atracID < 0) {
930
return hleLogError(Log::Atrac, atracID, "no free ID");
931
}
932
933
ret = atracContexts[atracID]->SetData(track, buffer, bufferSize, bufferSize, 0, 1, false);
934
if (ret < 0 && ret != SCE_ERROR_ATRAC_NOT_MONO) {
935
UnregisterAndDeleteAtrac(atracID);
936
return hleLogError(Log::Atrac, ret);
937
}
938
return hleDelayResult(hleLogDebugOrError(Log::Atrac, atracID), "atrac set data", 100);
939
}
940
941
static int sceAtracSetMOutHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bufferSize) {
942
if (readSize > bufferSize) {
943
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large");
944
}
945
946
Track track;
947
std::string error;
948
int ret = AnalyzeAtracTrack(Memory::GetPointerOrNull(buffer), readSize, &track, &error);
949
if (ret < 0) {
950
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
951
}
952
953
if (track.channels != 1) {
954
return hleReportError(Log::Atrac, SCE_ERROR_ATRAC_NOT_MONO, "not mono data");
955
}
956
957
int atracID = AllocAndRegisterAtrac(track.codecType);
958
if (atracID < 0) {
959
return hleLogError(Log::Atrac, atracID, "no free ID");
960
}
961
962
ret = atracContexts[atracID]->SetData(track, buffer, readSize, bufferSize, 0, 1, false);
963
if (ret < 0 && ret != SCE_ERROR_ATRAC_NOT_MONO) {
964
UnregisterAndDeleteAtrac(atracID);
965
return hleLogError(Log::Atrac, ret);
966
}
967
return hleDelayResult(hleLogDebug(Log::Atrac, atracID), "atrac set data", 100);
968
}
969
970
static int sceAtracSetAA3DataAndGetID(u32 buffer, u32 bufferSize, u32 fileSize, u32 metadataSizeAddr) {
971
Track track;
972
std::string error;
973
int ret = AnalyzeAA3Track(Memory::GetPointerOrNull(buffer), bufferSize, fileSize, &track, &error);
974
if (ret < 0) {
975
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
976
}
977
978
int atracID = AllocAndRegisterAtrac(track.codecType);
979
if (atracID < 0) {
980
return hleLogError(Log::Atrac, atracID, "no free ID");
981
}
982
983
ret = atracContexts[atracID]->SetData(track, buffer, bufferSize, bufferSize, fileSize, 2, true);
984
if (ret < 0) {
985
UnregisterAndDeleteAtrac(atracID);
986
return hleLogError(Log::Atrac, ret);
987
}
988
989
return hleDelayResult(hleLogDebug(Log::Atrac, atracID), "atrac set aa3 data", 100);
990
}
991
992
static int sceAtracSetAA3HalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bufferSize, u32 fileSize) {
993
if (readSize > bufferSize) {
994
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large");
995
}
996
997
Track track;
998
std::string error;
999
int ret = AnalyzeAA3Track(Memory::GetPointerOrNull(buffer), readSize, fileSize, &track, &error);
1000
if (ret < 0) {
1001
return hleLogError(Log::Atrac, ret, "%s", error.c_str());
1002
}
1003
1004
int atracID = AllocAndRegisterAtrac(track.codecType);
1005
if (atracID < 0) {
1006
return hleLogError(Log::Atrac, atracID, "no free ID");
1007
}
1008
1009
ret = atracContexts[atracID]->SetData(track, buffer, readSize, bufferSize, fileSize, 2, true);
1010
if (ret < 0) {
1011
UnregisterAndDeleteAtrac(atracID);
1012
return hleLogError(Log::Atrac, ret);
1013
}
1014
1015
return hleDelayResult(hleLogDebug(Log::Atrac, atracID), "atrac set data", 100);
1016
}
1017
1018
// TODO: Should see if these are stored contiguously in memory somewhere, or if there really are
1019
// individual allocations being used.
1020
static u32 _sceAtracGetContextAddress(int atracID) {
1021
AtracBase *atrac = getAtrac(atracID);
1022
if (!atrac) {
1023
return hleLogError(Log::Atrac, 0, "bad atrac id");
1024
}
1025
1026
// Only the old context needs this. The new one will always have a context pointer.
1027
atrac->NotifyGetContextAddress();
1028
return hleLogDebug(Log::Atrac, atrac->context_.ptr);
1029
}
1030
1031
static int sceAtracLowLevelInitDecoder(int atracID, u32 paramsAddr) {
1032
AtracBase *atrac = getAtrac(atracID);
1033
if (!atrac) {
1034
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_BAD_ATRACID, "bad atrac ID");
1035
}
1036
1037
if (!Memory::IsValidRange(paramsAddr, 12)) {
1038
// The real library will probably just crash here.
1039
return hleReportError(Log::Atrac, 0, "invalid pointers");
1040
}
1041
1042
auto params = PSPPointer<Atrac3LowLevelParams>::Create(paramsAddr);
1043
const int codecType = atracContextTypes[atracID];
1044
1045
atrac->InitLowLevel(*params, codecType);
1046
1047
const char *codecName = codecType == PSP_CODEC_AT3 ? "atrac3" : "atrac3+";
1048
const char *encodedChannelName = params->encodedChannels == 1 ? "mono" : "stereo";
1049
const char *outputChannelName = params->outputChannels == 1 ? "mono" : "stereo";
1050
return hleLogInfo(Log::Atrac, 0, "%s %s->%s audio", codecName, encodedChannelName, outputChannelName);
1051
}
1052
1053
static int sceAtracLowLevelDecode(int atracID, u32 sourceAddr, u32 sourceBytesConsumedAddr, u32 samplesAddr, u32 sampleBytesAddr) {
1054
auto srcp = PSPPointer<u8>::Create(sourceAddr);
1055
auto srcConsumed = PSPPointer<u32_le>::Create(sourceBytesConsumedAddr);
1056
auto outp = PSPPointer<s16>::Create(samplesAddr);
1057
auto outWritten = PSPPointer<u32_le>::Create(sampleBytesAddr);
1058
1059
AtracBase *atrac = getAtrac(atracID);
1060
if (!atrac) {
1061
return hleLogError(Log::Atrac, SCE_ERROR_ATRAC_BAD_ATRACID, "bad atrac ID");
1062
}
1063
1064
if (!srcp.IsValid() || !srcConsumed.IsValid() || !outp.IsValid() || !outWritten.IsValid()) {
1065
// TODO: Returning zero as code was before. Needs testing.
1066
return hleReportError(Log::Atrac, 0, "invalid pointers");
1067
}
1068
1069
int bytesConsumed = 0;
1070
int bytesWritten = 0;
1071
1072
int retval = atrac->DecodeLowLevel(srcp, &bytesConsumed, outp, &bytesWritten);
1073
*srcConsumed = bytesConsumed;
1074
*outWritten = bytesWritten;
1075
1076
if (g_muteFlag[atracID]) {
1077
memset(outp, 0, bytesWritten);
1078
}
1079
1080
NotifyMemInfo(MemBlockFlags::WRITE, samplesAddr, bytesWritten, "AtracLowLevelDecode");
1081
return hleDelayResult(hleLogDebug(Log::Atrac, retval), "low level atrac decode data", atracDecodeDelay);
1082
}
1083
1084
// These three are the external interface used by sceSas' AT3 integration.
1085
1086
// NOTE: There are special rules.
1087
1088
1089
// This is __sceSasConcatenateATRAC3.
1090
// The context's fileOff is incremented by the caller. No other pointers are bumped, except
1091
// internally in sceSas on the real hardware.
1092
u32 AtracSasAddStreamData(int atracID, u32 bufPtr, u32 bytesToAdd) {
1093
AtracBase *atrac = getAtrac(atracID);
1094
if (!atrac) {
1095
WARN_LOG(Log::Atrac, "bad atrac ID");
1096
}
1097
return atrac->EnqueueForSas(bufPtr, bytesToAdd);
1098
}
1099
1100
void AtracSasDecodeData(int atracID, u8* outbuf, int *SamplesNum, int *finish) {
1101
AtracBase *atrac = getAtrac(atracID);
1102
if (!atrac) {
1103
WARN_LOG(Log::Atrac, "bad atrac ID");
1104
}
1105
// NOTE: If streaming, this will write 0xFFFFFFFF to secondBuffer in the context when close to running
1106
// out of data. It'll then expect __sceSasConcatenateATRAC3 to concatenate a new buffer, which cannot
1107
// be at the same address as the old one. I think you need to double buffer, and we in fact continouously
1108
// read out of these.
1109
atrac->DecodeForSas((s16 *)outbuf, SamplesNum, finish);
1110
}
1111
1112
int AtracSasBindContextAndGetID(u32 contextAddr) {
1113
// Ugly hack, but needed to support both old and new contexts.
1114
int atracID = (int)Memory::Read_U32(contextAddr + 0xfc);
1115
if (atracID < PSP_MAX_ATRAC_IDS && atracContexts[atracID] && atracContexts[atracID]->GetContextVersion() == 1) {
1116
// We can assume the old atracID hack was used, and atracID is valid.
1117
} else {
1118
// Let's just loop around the contexts and find it by address.
1119
atracID = -1;
1120
for (int i = 0; i < PSP_MAX_ATRAC_IDS; i++) {
1121
if (!atracContexts[i]) {
1122
continue;
1123
}
1124
if (atracContexts[i]->GetContextVersion() == 2 && atracContexts[i]->context_.Equals(contextAddr)) {
1125
atracID = i;
1126
break;
1127
}
1128
}
1129
_dbg_assert_(atracID != -1);
1130
}
1131
1132
AtracBase *atrac = getAtrac(atracID);
1133
atrac->CheckForSas();
1134
return atracID;
1135
}
1136
1137
const char *AtracStatusToString(AtracStatus status) {
1138
switch (status) {
1139
case ATRAC_STATUS_NO_DATA: return "NO_DATA";
1140
case ATRAC_STATUS_ALL_DATA_LOADED: return "ALL_DATA_LOADED";
1141
case ATRAC_STATUS_HALFWAY_BUFFER: return "HALFWAY_BUFFER";
1142
case ATRAC_STATUS_STREAMED_WITHOUT_LOOP: return "STREAMED_WITHOUT_LOOP";
1143
case ATRAC_STATUS_STREAMED_LOOP_FROM_END: return "STREAMED_LOOP_FROM_END";
1144
case ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER: return "STREAMED_LOOP_WITH_TRAILER";
1145
case ATRAC_STATUS_LOW_LEVEL: return "LOW_LEVEL";
1146
case ATRAC_STATUS_FOR_SCESAS: return "FOR_SCESAS";
1147
default: return "(unknown!)";
1148
}
1149
}
1150
1151
const HLEFunction sceAtrac3plus[] = {
1152
{0X7DB31251, &WrapU_IU<sceAtracAddStreamData>, "sceAtracAddStreamData", 'x', "ix" },
1153
{0X6A8C3CD5, &WrapU_IUUUU<sceAtracDecodeData>, "sceAtracDecodeData", 'x', "ixppp"},
1154
{0XD5C28CC0, &WrapU_V<sceAtracReleaseResources>, "sceAtracReleaseResources", 'x', "" }, // previously had the wrong name sceAtracEndEntry. The new name is a guess, but accurate in meaning.
1155
{0X780F88D1, &WrapU_I<sceAtracGetAtracID>, "sceAtracGetAtracID", 'i', "x" },
1156
{0XCA3CA3D2, &WrapU_IIU<sceAtracGetBufferInfoForResetting>, "sceAtracGetBufferInfoForReseting", 'x', "iix" },
1157
{0XA554A158, &WrapU_IU<sceAtracGetBitrate>, "sceAtracGetBitrate", 'x', "ip" },
1158
{0X31668BAA, &WrapU_IU<sceAtracGetChannel>, "sceAtracGetChannel", 'x', "ip" },
1159
{0XFAA4F89B, &WrapU_IUU<sceAtracGetLoopStatus>, "sceAtracGetLoopStatus", 'x', "ipp" },
1160
{0XE88F759B, &WrapU_IU<sceAtracGetInternalErrorInfo>, "sceAtracGetInternalErrorInfo", 'x', "ip" },
1161
{0XD6A5F2F7, &WrapU_IU<sceAtracGetMaxSample>, "sceAtracGetMaxSample", 'x', "ip" },
1162
{0XE23E3A35, &WrapU_IU<sceAtracGetNextDecodePosition>, "sceAtracGetNextDecodePosition", 'x', "ip" },
1163
{0X36FAABFB, &WrapU_IU<sceAtracGetNextSample>, "sceAtracGetNextSample", 'x', "ip" },
1164
{0X9AE849A7, &WrapU_IU<sceAtracGetRemainFrame>, "sceAtracGetRemainFrame", 'x', "ip" },
1165
{0X83E85EA0, &WrapU_IUU<sceAtracGetSecondBufferInfo>, "sceAtracGetSecondBufferInfo", 'x', "ipp" },
1166
{0XA2BBA8BE, &WrapU_IUUU<sceAtracGetSoundSample>, "sceAtracGetSoundSample", 'x', "ippp" },
1167
{0X5D268707, &WrapU_IUUU<sceAtracGetStreamDataInfo>, "sceAtracGetStreamDataInfo", 'x', "ippp" },
1168
{0X61EB33F5, &WrapU_I<sceAtracReleaseAtracID>, "sceAtracReleaseAtracID", 'x', "i" },
1169
{0X644E5607, &WrapU_IIII<sceAtracResetPlayPosition>, "sceAtracResetPlayPosition", 'x', "iiii" },
1170
{0X3F6E26B5, &WrapU_IUUU<sceAtracSetHalfwayBuffer>, "sceAtracSetHalfwayBuffer", 'x', "ixxx" },
1171
{0X83BF7AFD, &WrapU_IUU<sceAtracSetSecondBuffer>, "sceAtracSetSecondBuffer", 'x', "ixx" },
1172
{0X0E2A73AB, &WrapU_IUU<sceAtracSetData>, "sceAtracSetData", 'x', "ixx" },
1173
{0X7A20E7AF, &WrapI_UI<sceAtracSetDataAndGetID>, "sceAtracSetDataAndGetID", 'i', "xx" },
1174
{0XD1F59FDB, &WrapU_V<sceAtracStartEntry>, "sceAtracStartEntry", 'x', "" },
1175
{0X868120B5, &WrapU_II<sceAtracSetLoopNum>, "sceAtracSetLoopNum", 'x', "ii" },
1176
{0X132F1ECA, &WrapI_II<sceAtracReinit>, "sceAtracReinit", 'x', "ii" },
1177
{0XECA32A99, &WrapI_I<sceAtracIsSecondBufferNeeded>, "sceAtracIsSecondBufferNeeded", 'i', "i" },
1178
{0X0FAE370E, &WrapI_UUU<sceAtracSetHalfwayBufferAndGetID>, "sceAtracSetHalfwayBufferAndGetID", 'i', "xxx" },
1179
{0X2DD3E298, &WrapU_IIU<sceAtracGetBufferInfoForResetting>, "sceAtracGetBufferInfoForResetting", 'x', "iix" },
1180
{0X5CF9D852, &WrapI_IUUU<sceAtracSetMOutHalfwayBuffer>, "sceAtracSetMOutHalfwayBuffer", 'x', "ixxx" },
1181
{0XF6837A1A, &WrapU_IUU<sceAtracSetMOutData>, "sceAtracSetMOutData", 'x', "ixx" },
1182
{0X472E3825, &WrapI_UU<sceAtracSetMOutDataAndGetID>, "sceAtracSetMOutDataAndGetID", 'i', "xx" },
1183
{0X9CD7DE03, &WrapI_UUU<sceAtracSetMOutHalfwayBufferAndGetID>, "sceAtracSetMOutHalfwayBufferAndGetID", 'i', "xxx" },
1184
{0XB3B5D042, &WrapI_IU<sceAtracGetOutputChannel>, "sceAtracGetOutputChannel", 'x', "ip" },
1185
{0X5622B7C1, &WrapI_UUUU<sceAtracSetAA3DataAndGetID>, "sceAtracSetAA3DataAndGetID", 'i', "xxxp" },
1186
{0X5DD66588, &WrapI_UUUU<sceAtracSetAA3HalfwayBufferAndGetID>, "sceAtracSetAA3HalfwayBufferAndGetID", 'i', "xxxx" },
1187
{0X231FC6B7, &WrapU_I<_sceAtracGetContextAddress>, "_sceAtracGetContextAddress", 'x', "i" },
1188
{0X1575D64B, &WrapI_IU<sceAtracLowLevelInitDecoder>, "sceAtracLowLevelInitDecoder", 'x', "ix" },
1189
{0X0C116E1B, &WrapI_IUUUU<sceAtracLowLevelDecode>, "sceAtracLowLevelDecode", 'x', "ixpxp"},
1190
};
1191
1192
void Register_sceAtrac3plus() {
1193
// Two names
1194
RegisterHLEModule("sceATRAC3plus_Library", ARRAY_SIZE(sceAtrac3plus), sceAtrac3plus);
1195
RegisterHLEModule("sceAtrac3plus", ARRAY_SIZE(sceAtrac3plus), sceAtrac3plus);
1196
}
1197
1198