Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HLE/AtracCtx2.cpp
3186 views
1
#include <algorithm>
2
3
#include "Common/Serialize/Serializer.h"
4
#include "Common/Serialize/SerializeFuncs.h"
5
#include "Common/Log.h"
6
#include "Core/MemMapHelpers.h"
7
#include "Core/HLE/HLE.h"
8
#include "Core/HLE/ErrorCodes.h"
9
#include "Core/HLE/FunctionWrappers.h"
10
#include "Core/System.h"
11
#include "Core/HLE/AtracCtx2.h"
12
#include "Core/Util/AtracTrack.h"
13
#include "Core/HW/Atrac3Standalone.h"
14
#include "Core/Config.h"
15
#include "Core/MemMap.h"
16
#include "Common/File/FileUtil.h"
17
18
// Convenient command line:
19
// Windows\x64\debug\PPSSPPHeadless.exe --root pspautotests/tests/../ -o --compare --new-atrac --timeout=30 --graphics=software pspautotests/tests/audio/atrac/stream.prx
20
//
21
// See the big comment in sceAtrac.cpp for an overview of the different modes of operation.
22
23
// To run on the real PSP, without gentest.py:
24
// cmd1> C:\dev\ppsspp\pspautotests\tests\audio\atrac> make
25
// cmd2> C:\dev\ppsspp\pspautotests> usbhostfs_pc -b 4000
26
// cmd3> C:\dev\ppsspp\pspautotests> pspsh -p 4000
27
// cmd3> > cd host0:/test/audio/atrac
28
// cmd3> stream.prx
29
// cmd1> C:\dev\ppsspp\pspautotests\tests\audio\atrac>copy /Y ..\..\..\__testoutput.txt stream.expected
30
// Then run the test, see above.
31
32
struct AT3BitrateMeta {
33
u16 sampleSize;
34
u8 dataByte;
35
u8 jointStereo; // I think?
36
};
37
38
static const AT3BitrateMeta g_at3BitrateMeta[5] = {
39
{0x180, 0x04, 0},
40
{0x130, 0x06, 0},
41
{0x0C0, 0x0B, 1},
42
{0x0C0, 0x0E, 0},
43
{0x098, 0x0F, 0},
44
};
45
46
// Needs to support negative numbers, and to handle non-powers-of-two.
47
static int RoundDownToMultiple(int size, int grain) {
48
return size - (size % grain);
49
}
50
51
static int RoundDownToMultipleWithOffset(int offset, int size, int grain) {
52
if (size > offset) {
53
return ((size - offset) / grain) * grain + offset;
54
} else {
55
return size;
56
}
57
}
58
59
static int ComputeSkipFrames(const SceAtracIdInfo &info, int seekPos) {
60
// No idea why this is the rule, but this is the rule.
61
return (seekPos & info.SamplesFrameMask()) < info.SkipSamples() ? 2 : 1;
62
}
63
64
static int ComputeFileOffset(const SceAtracIdInfo &info, int seekPos) {
65
int frameOffset = ((seekPos / info.SamplesPerFrame()) - 1) * info.sampleSize;
66
if ((seekPos & info.SamplesFrameMask()) < info.SkipSamples() && (frameOffset != 0)) {
67
frameOffset -= info.sampleSize;
68
}
69
return frameOffset + info.dataOff;
70
}
71
72
// Unlike the above, this one need to be inclusive.
73
static int ComputeLoopEndFileOffset(const SceAtracIdInfo &info, int seekPos) {
74
return (seekPos / info.SamplesPerFrame() + 1) * info.sampleSize + info.dataOff;
75
}
76
77
static int ComputeSpaceUsed(const SceAtracIdInfo &info) {
78
// The odd case: If streaming from the second buffer, and we're past the loop end (we're in the tail)...
79
if (info.decodePos > info.loopEnd && info.curBuffer == 1) {
80
int space = info.secondBufferByte;
81
if (info.secondStreamOff < space) {
82
space = RoundDownToMultipleWithOffset(info.secondStreamOff, info.secondBufferByte, info.sampleSize);
83
}
84
if ((info.secondStreamOff <= space) && (space - info.secondStreamOff < info.streamDataByte)) {
85
return info.streamDataByte - (space - info.secondStreamOff);
86
}
87
return 0;
88
}
89
90
// The normal case.
91
return info.streamDataByte;
92
}
93
94
static int ComputeRemainFrameStream(const SceAtracIdInfo &info) {
95
if (info.streamDataByte >= info.fileDataEnd - info.curFileOff) {
96
// Already done.
97
return PSP_ATRAC_NONLOOP_STREAM_DATA_IS_ON_MEMORY;
98
}
99
// Since we're streaming, the remaining frames are what's valid in the buffer.
100
return std::max(0, info.streamDataByte / info.sampleSize - (int)info.numSkipFrames);
101
}
102
103
// This got so complicated!
104
static int ComputeRemainFrameLooped(const SceAtracIdInfo &info) {
105
const int loopStartFileOffset = ComputeFileOffset(info, info.loopStart);
106
const int loopEndFileOffset = ComputeLoopEndFileOffset(info, info.loopEnd);
107
const int writeFileOff = info.curFileOff + info.streamDataByte;
108
const int leftToRead = writeFileOff - loopEndFileOffset;
109
110
int remainFrames;
111
if (writeFileOff <= loopEndFileOffset) {
112
// Simple case - just divide to find the number of frames remaining in the buffer.
113
remainFrames = info.streamDataByte / info.sampleSize;
114
} else {
115
// Darn, we need to take looping into account...
116
const int skipFramesAtLoopStart = ComputeSkipFrames(info, info.loopStart);
117
const int firstPartLength = loopEndFileOffset - loopStartFileOffset;
118
const int secondPartLength = leftToRead % firstPartLength;
119
// Sum up all the parts (the buffered, the space remaining before the loop point
120
// and the space after the loop point), each divided by sample size, need to take skipped frames into account.
121
remainFrames = (loopEndFileOffset - info.curFileOff) / info.sampleSize +
122
(leftToRead / firstPartLength) * (firstPartLength / info.sampleSize - skipFramesAtLoopStart);
123
if (secondPartLength > skipFramesAtLoopStart * info.sampleSize) {
124
remainFrames += secondPartLength / info.sampleSize - skipFramesAtLoopStart;
125
}
126
}
127
128
// Clamp to zero.
129
remainFrames = std::max(0, remainFrames - (int)info.numSkipFrames);
130
if (info.loopNum < 0) {
131
// Infinite looping while streaming, we never return that we're done reading data.
132
return remainFrames;
133
}
134
135
// Additional check for distance to end of playback if we're looping a finite amount of times.
136
const int streamBufferEndFileOffset = info.curFileOff + info.streamDataByte;
137
if (streamBufferEndFileOffset >= loopEndFileOffset) {
138
const int numBufferedLoops = (streamBufferEndFileOffset - loopEndFileOffset) / (loopEndFileOffset - loopStartFileOffset);
139
if (info.loopNum <= numBufferedLoops) {
140
return PSP_ATRAC_LOOP_STREAM_DATA_IS_ON_MEMORY;
141
}
142
}
143
return remainFrames;
144
}
145
146
static void InitLengthAndLoop(SceAtracIdInfo *ctx, int endSample, int waveDataSize, int firstSampleOffset, int loopBegin, int loopEnd) {
147
const int off = ctx->codec == 0x1001 ? 0x45 : 0x170;
148
const int blockShift = 0x100b - ctx->codec;
149
const int firstValidSample = firstSampleOffset + off;
150
int numSamplesInFile;
151
if (endSample == 0) {
152
numSamplesInFile = waveDataSize / ctx->sampleSize << (blockShift & 0x1f);
153
} else {
154
numSamplesInFile = endSample + firstValidSample;
155
}
156
ctx->decodePos = firstValidSample;
157
ctx->loopNum = 0;
158
ctx->endSample = numSamplesInFile - 1;
159
ctx->numSkipFrames = (char)(firstValidSample >> (blockShift & 0x1f));
160
if (-1 < loopBegin) {
161
ctx->loopEnd = loopEnd + off;
162
ctx->loopStart = loopBegin + off;
163
return;
164
}
165
ctx->loopEnd = 0;
166
ctx->loopStart = 0;
167
return;
168
}
169
170
static int ComputeAtracStateAndInitSecondBuffer(SceAtracIdInfo *info, u32 readSize, u32 bufferSize) {
171
AtracStatus state;
172
int loopEndFileOffset;
173
int loopEnd;
174
175
if (bufferSize < (u32)info->fileDataEnd) {
176
if (info->streamDataByte < (s32)info->sampleSize * 3) {
177
return 0x80630011;
178
}
179
loopEnd = info->loopEnd;
180
state = ATRAC_STATUS_STREAMED_WITHOUT_LOOP;
181
if ((loopEnd != 0) && (state = ATRAC_STATUS_STREAMED_LOOP_FROM_END, loopEnd != info->endSample)) {
182
loopEndFileOffset = ComputeLoopEndFileOffset(*info, loopEnd);
183
loopEnd = (loopEndFileOffset - info->dataOff) + 1;
184
info->state = ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER;
185
if (loopEnd < info->streamDataByte) {
186
info->streamDataByte = loopEnd;
187
}
188
info->secondStreamOff = 0;
189
info->secondBuffer = 0;
190
info->secondBufferByte = 0;
191
return 0;
192
}
193
} else {
194
state = ATRAC_STATUS_HALFWAY_BUFFER;
195
if (readSize >= (u32)info->fileDataEnd) {
196
state = ATRAC_STATUS_ALL_DATA_LOADED;
197
}
198
}
199
info->state = state;
200
return 0;
201
}
202
203
int InitContextFromTrackInfo(SceAtracContext *ctx, const TrackInfo *wave, u32 bufferAddr, int readSize, int bufferSize) {
204
(ctx->info).numChan = (char)wave->numChans;
205
const int extraSamples = (ctx->info).codec == 0x1001 ? 0x45 : 0x170;
206
(ctx->info).firstValidSample = extraSamples + wave->firstSampleOffset;
207
int endSample3 = wave->endSample;
208
(ctx->info).sampleSize = wave->blockAlign;
209
InitLengthAndLoop(&ctx->info, endSample3, wave->waveDataSize, wave->firstSampleOffset, wave->loopStart, wave->loopEnd);
210
const int dataOff = wave->dataOff;
211
const int endSample = (ctx->info).endSample;
212
(ctx->info).streamDataByte = readSize - dataOff;
213
(ctx->info).buffer = bufferAddr;
214
(ctx->info).curFileOff = dataOff;
215
(ctx->info).dataOff = dataOff;
216
(ctx->info).fileDataEnd = wave->waveDataSize + dataOff;
217
(ctx->info).curBuffer = 0;
218
(ctx->info).bufferByte = bufferSize;
219
(ctx->info).streamOff = dataOff;
220
if ((ctx->info).loopEnd > endSample) {
221
return SCE_ERROR_ATRAC_BAD_CODEC_PARAMS;
222
}
223
224
const int numChunks = (endSample >> (0x100b - (ctx->info).codec & 0x1f));
225
226
if ((numChunks * (u32)(ctx->info).sampleSize < (wave->waveDataSize + dataOff) - dataOff)) {
227
int retval = ComputeAtracStateAndInitSecondBuffer(&ctx->info, readSize, bufferSize);
228
if (retval < 0) {
229
return retval;
230
}
231
if ((ctx->info).codec != PSP_CODEC_AT3) {
232
// At3plus
233
// Configure the codec for the sample size, or whatever that data is.
234
(ctx->codec).unk40 = wave->sampleSizeMaybe;
235
(ctx->codec).unk48 = 0;
236
(ctx->codec).unk41 = wave->tailFlag;
237
return 0;
238
}
239
// At3. Set up the hardware codec (hopefully we can correctly support this in sceAudiocodec and thus sceAtrac LLE in the future)
240
// This is not actually necessary since we don't use the actual hardware codec.
241
for (int counter = 4; counter >= 0; counter--) {
242
if ((g_at3BitrateMeta[counter].sampleSize == (ctx->info).sampleSize) &&
243
((int)g_at3BitrateMeta[counter].dataByte == wave->sampleSizeMaybe)) {
244
(ctx->codec).unk40 = (char)g_at3BitrateMeta[counter].jointStereo;
245
(ctx->codec).unk41 = 0;
246
(ctx->codec).unk42 = 0;
247
(ctx->codec).unk43 = 0;
248
return 0;
249
}
250
}
251
return 0;
252
} else {
253
return SCE_ERROR_ATRAC_BAD_CODEC_PARAMS;
254
}
255
}
256
257
int Atrac2::RemainingFrames() const {
258
const SceAtracIdInfo &info = context_->info;
259
260
// Handle the easy cases first.
261
switch (info.state) {
262
case ATRAC_STATUS_UNINITIALIZED:
263
case ATRAC_STATUS_NO_DATA:
264
return 0;
265
case ATRAC_STATUS_ALL_DATA_LOADED:
266
return PSP_ATRAC_ALLDATA_IS_ON_MEMORY; // Not sure about no data.
267
case ATRAC_STATUS_HALFWAY_BUFFER:
268
{
269
// Pretty simple - compute the remaining space, and divide by the sample size, adjusting for frames-to-skip.
270
const int writeFileOff = info.dataOff + info.streamDataByte;
271
if (info.curFileOff < writeFileOff) {
272
return std::max(0, (writeFileOff - info.curFileOff) / info.sampleSize - (int)info.numSkipFrames);
273
}
274
return 0;
275
}
276
case ATRAC_STATUS_STREAMED_WITHOUT_LOOP:
277
return ComputeRemainFrameStream(info);
278
279
case ATRAC_STATUS_STREAMED_LOOP_FROM_END:
280
return ComputeRemainFrameLooped(info);
281
282
case ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER:
283
if (info.decodePos <= info.loopEnd) {
284
// If before the tail, just treat it as looped.
285
return ComputeRemainFrameLooped(info);
286
} else {
287
// If in tail, treat is as unlooped.
288
return ComputeRemainFrameStream(info);
289
}
290
break;
291
default:
292
return SCE_ERROR_ATRAC_BAD_ATRACID;
293
}
294
}
295
296
Atrac2::Atrac2(u32 contextAddr, int codecType) {
297
if (contextAddr) {
298
context_ = PSPPointer<SceAtracContext>::Create(contextAddr);
299
// First-time initialization. The rest is initialized in SetData.
300
SceAtracIdInfo &info = context_->info;
301
info.codec = codecType;
302
info.state = ATRAC_STATUS_NO_DATA;
303
info.curBuffer = 0;
304
305
sas_.streamOffset = 0;
306
sas_.bufPtr[0] = 0;
307
sas_.bufPtr[1] = 0;
308
} else {
309
// We're loading state, we'll restore the context in DoState.
310
}
311
}
312
313
Atrac2::~Atrac2() {
314
DumpBufferToFile();
315
// Nothing else to do here, the context is freed by the HLE.
316
}
317
318
void Atrac2::DumpBufferToFile() {
319
if (dumped_) {
320
// Already dumped, no need to dump again.
321
return;
322
}
323
if (!dumpBuffer_.empty()) {
324
std::string filename = StringFromFormat("atrac3_%08x_incomplete.at3", context_->info.endSample);
325
DumpFileIfEnabled(dumpBuffer_.data(), (u32)dumpBuffer_.size(), filename, DumpFileType::Atrac3);
326
dumpBuffer_.clear();
327
}
328
dumped_ = true;
329
}
330
331
void Atrac2::DoState(PointerWrap &p) {
332
auto s = p.Section("Atrac2", 1, 3);
333
if (!s)
334
return;
335
336
Do(p, outputChannels_);
337
// The only thing we need to save now is the outputChannels_ and the context pointer. And technically, not even that since
338
// it can be computed. Still, for future proofing, let's save it.
339
Do(p, context_);
340
341
// Actually, now we also need to save sas state. I guess this could also be saved on the Sas side, but this is easier.
342
if (s >= 2) {
343
Do(p, sas_.streamOffset);
344
Do(p, sas_.bufPtr[0]);
345
}
346
// Added support for streaming sas audio, need some more context state.
347
if (s >= 3) {
348
Do(p, sas_.bufPtr[1]);
349
Do(p, sas_.bufSize[0]);
350
Do(p, sas_.bufSize[1]);
351
Do(p, sas_.isStreaming);
352
Do(p, sas_.curBuffer);
353
Do(p, sas_.fileOffset);
354
}
355
356
const SceAtracIdInfo &info = context_->info;
357
if (p.mode == p.MODE_READ && info.state != ATRAC_STATUS_NO_DATA) {
358
CreateDecoder(info.codec, info.sampleSize, info.numChan);
359
}
360
}
361
362
bool Atrac2::HasSecondBuffer() const {
363
const SceAtracIdInfo &info = context_->info;
364
return info.secondBufferByte != 0;
365
}
366
367
int Atrac2::GetSoundSample(int *outEndSample, int *outLoopStartSample, int *outLoopEndSample) const {
368
const SceAtracIdInfo &info = context_->info;
369
*outEndSample = info.endSample - info.firstValidSample;
370
int loopEnd = -1;
371
if (info.loopEnd == 0) {
372
*outLoopStartSample = -1;
373
*outLoopEndSample = -1;
374
} else {
375
*outLoopStartSample = info.loopStart - info.firstValidSample;
376
*outLoopEndSample = info.loopEnd - info.firstValidSample;
377
}
378
return 0;
379
}
380
381
int Atrac2::ResetPlayPosition(int seekPos, int bytesWrittenFirstBuf, int bytesWrittenSecondBuf, bool *delay) {
382
*delay = false;
383
384
// This was mostly copied straight from the old impl.
385
SceAtracIdInfo &info = context_->info;
386
if (info.state == ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER && info.secondBufferByte == 0) {
387
return SCE_ERROR_ATRAC_SECOND_BUFFER_NEEDED;
388
}
389
390
seekPos += info.firstValidSample;
391
392
if ((u32)seekPos > (u32)info.endSample) {
393
return SCE_ERROR_ATRAC_BAD_SAMPLE;
394
}
395
396
int result = ResetPlayPositionInternal(seekPos, bytesWrittenFirstBuf, bytesWrittenSecondBuf);
397
if (result >= 0) {
398
int skipCount = 0;
399
result = SkipFrames(&skipCount);
400
if (skipCount) {
401
*delay = true;
402
}
403
}
404
return result;
405
}
406
407
u32 Atrac2::ResetPlayPositionInternal(int seekPos, int bytesWrittenFirstBuf, int bytesWrittenSecondBuf) {
408
// Redo the same calculation as before, for input validation.
409
410
AtracResetBufferInfo bufferInfo;
411
GetResetBufferInfoInternal(&bufferInfo, seekPos);
412
413
// Input validation.
414
if ((u32)bytesWrittenFirstBuf < bufferInfo.first.minWriteBytes || (u32)bytesWrittenFirstBuf > bufferInfo.first.writableBytes) {
415
return SCE_ERROR_ATRAC_BAD_FIRST_RESET_SIZE;
416
}
417
if ((u32)bytesWrittenSecondBuf < bufferInfo.second.minWriteBytes || (u32)bytesWrittenSecondBuf > bufferInfo.second.writableBytes) {
418
return SCE_ERROR_ATRAC_BAD_SECOND_RESET_SIZE;
419
}
420
421
SceAtracIdInfo &info = context_->info;
422
info.decodePos = seekPos;
423
info.numSkipFrames = ComputeSkipFrames(info, seekPos);
424
info.loopNum = 0;
425
info.curFileOff = ComputeFileOffset(info, seekPos);
426
427
context_->codec.err = 0x20b; // wtf? testing shows it.
428
429
switch (info.state) {
430
case ATRAC_STATUS_ALL_DATA_LOADED:
431
// We're done.
432
return 0;
433
434
case ATRAC_STATUS_HALFWAY_BUFFER:
435
info.streamDataByte += bytesWrittenFirstBuf;
436
if (info.dataOff + info.streamDataByte >= info.fileDataEnd) {
437
// Buffer full, we can transition to a full buffer here, if all the bytes were written. Let's do it.
438
info.state = ATRAC_STATUS_ALL_DATA_LOADED;
439
}
440
return 0;
441
442
case ATRAC_STATUS_STREAMED_WITHOUT_LOOP:
443
case ATRAC_STATUS_STREAMED_LOOP_FROM_END:
444
// We just adopt the bytes that were written as our stream data, no math needed.
445
info.streamDataByte = bytesWrittenFirstBuf;
446
info.curBuffer = 0;
447
info.streamOff = 0;
448
return 0;
449
450
case ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER:
451
{
452
// As usual with the second buffer and trailer, things get tricky here.
453
const int loopEndFileOffset = ComputeLoopEndFileOffset(info, info.loopEnd);
454
if (info.curFileOff >= loopEndFileOffset) {
455
const int secondBufferSizeRounded = RoundDownToMultiple(info.secondBufferByte, info.sampleSize);
456
if (info.curFileOff < loopEndFileOffset + secondBufferSizeRounded) {
457
info.streamDataByte = ((loopEndFileOffset + secondBufferSizeRounded) - info.curFileOff) + bytesWrittenFirstBuf;
458
info.curBuffer = 1;
459
info.secondStreamOff = info.curFileOff - loopEndFileOffset;
460
} else {
461
info.streamDataByte = bytesWrittenFirstBuf;
462
info.curBuffer = 2; // Temporary value! Will immediately switch back to 0.
463
info.streamOff = 0;
464
}
465
} else {
466
info.streamDataByte = bytesWrittenFirstBuf;
467
info.curBuffer = 0;
468
info.streamOff = 0;
469
}
470
return 0;
471
}
472
default:
473
_dbg_assert_(false);
474
return 0;
475
}
476
}
477
478
// This is basically sceAtracGetBufferInfoForResetting.
479
// NOTE: Not const! This can cause SkipFrames!
480
int Atrac2::GetBufferInfoForResetting(AtracResetBufferInfo *bufferInfo, int seekPos, bool *delay) {
481
const SceAtracIdInfo &info = context_->info;
482
483
if (info.state == ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER && info.secondBufferByte == 0) {
484
return SCE_ERROR_ATRAC_SECOND_BUFFER_NEEDED;
485
}
486
487
seekPos += info.firstValidSample;
488
489
if ((u32)seekPos > (u32)info.endSample) {
490
return SCE_ERROR_ATRAC_BAD_SAMPLE;
491
}
492
493
GetResetBufferInfoInternal(bufferInfo, seekPos);
494
// Yes, this happens here! If there are any frames to skip, they get skipped!
495
// Even though this looks like a function that shouldn't change the state.
496
int skipCount = 0;
497
int retval = SkipFrames(&skipCount);
498
if (skipCount > 0)
499
*delay = true;
500
return retval;
501
}
502
503
void Atrac2::GetResetBufferInfoInternal(AtracResetBufferInfo *bufferInfo, int seekPos) const {
504
const SceAtracIdInfo &info = context_->info;
505
506
switch (info.state) {
507
case ATRAC_STATUS_NO_DATA:
508
case ATRAC_STATUS_ALL_DATA_LOADED:
509
// Everything is loaded, so nothing needs to be read.
510
bufferInfo->first.writePosPtr = info.buffer;
511
bufferInfo->first.writableBytes = 0;
512
bufferInfo->first.minWriteBytes = 0;
513
bufferInfo->first.filePos = 0;
514
break;
515
case ATRAC_STATUS_HALFWAY_BUFFER:
516
{
517
// Not too hard, we just ask to fill up the missing part of the buffer.
518
const int streamPos = info.dataOff + info.streamDataByte;
519
const int fileOff = info.dataOff + (seekPos / info.SamplesPerFrame() + 1) * info.sampleSize;
520
bufferInfo->first.writePosPtr = info.buffer + streamPos;
521
bufferInfo->first.writableBytes = info.fileDataEnd - streamPos;
522
bufferInfo->first.filePos = streamPos;
523
bufferInfo->first.minWriteBytes = std::max(0, fileOff - streamPos);
524
break;
525
}
526
527
case ATRAC_STATUS_STREAMED_WITHOUT_LOOP:
528
case ATRAC_STATUS_STREAMED_LOOP_FROM_END:
529
{
530
// Relatively easy, just can't forget those skipped frames.
531
const int curFileOffset = ComputeFileOffset(info, seekPos);
532
const int bufferEnd = RoundDownToMultiple(info.bufferByte, info.sampleSize);
533
bufferInfo->first.writePosPtr = info.buffer;
534
bufferInfo->first.writableBytes = std::min(info.fileDataEnd - curFileOffset, bufferEnd);
535
bufferInfo->first.minWriteBytes = (ComputeSkipFrames(info, seekPos) + 1) * info.sampleSize;
536
bufferInfo->first.filePos = curFileOffset;
537
break;
538
}
539
case ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER:
540
{
541
// As usual, with the second buffer, things get crazy complicated...
542
const int seekFileOffset = ComputeFileOffset(info, seekPos);
543
const int loopEndFileOffset = ComputeLoopEndFileOffset(info, info.loopEnd) - 1;
544
const int bufferEnd = RoundDownToMultiple(info.bufferByte, info.sampleSize);
545
const int skipBytes = (ComputeSkipFrames(info, seekPos) + 1) * info.sampleSize;
546
const int secondBufferEnd = RoundDownToMultiple(info.secondBufferByte, info.sampleSize);
547
if (seekFileOffset < loopEndFileOffset) {
548
const int remainingBeforeLoop = (loopEndFileOffset - seekFileOffset) + 1;
549
bufferInfo->first.writePosPtr = info.buffer;
550
bufferInfo->first.writableBytes = std::min(bufferEnd, remainingBeforeLoop);
551
bufferInfo->first.minWriteBytes = std::min(skipBytes, remainingBeforeLoop);
552
bufferInfo->first.filePos = seekFileOffset;
553
} else if (loopEndFileOffset + secondBufferEnd <= seekFileOffset) {
554
bufferInfo->first.writePosPtr = info.buffer;
555
bufferInfo->first.writableBytes = std::min(info.fileDataEnd - seekFileOffset, bufferEnd);
556
bufferInfo->first.minWriteBytes = skipBytes;
557
bufferInfo->first.filePos = seekFileOffset;
558
} else if (loopEndFileOffset + (int)info.secondBufferByte + 1 < info.fileDataEnd) {
559
const int endOffset = loopEndFileOffset + secondBufferEnd + 1;
560
bufferInfo->first.writePosPtr = info.buffer;
561
bufferInfo->first.writableBytes = std::min(info.fileDataEnd - endOffset, bufferEnd);
562
bufferInfo->first.minWriteBytes = std::max(0, seekFileOffset + skipBytes - endOffset);
563
bufferInfo->first.filePos = endOffset;
564
} else {
565
bufferInfo->first.writePosPtr = info.buffer;
566
bufferInfo->first.writableBytes = 0;
567
bufferInfo->first.minWriteBytes = 0;
568
bufferInfo->first.filePos = 0;
569
}
570
break;
571
}
572
default:
573
_dbg_assert_(false);
574
break;
575
}
576
577
// Reset never needs a second buffer write, since the loop is in a fixed place.
578
// It seems like second.writePosPtr is always the same as the first buffer's pos, weirdly (doesn't make sense).
579
bufferInfo->second.writePosPtr = info.buffer;
580
bufferInfo->second.writableBytes = 0;
581
bufferInfo->second.minWriteBytes = 0;
582
bufferInfo->second.filePos = 0;
583
}
584
585
int Atrac2::SetLoopNum(int loopNum) {
586
SceAtracIdInfo &info = context_->info;
587
if (info.loopEnd <= 0) {
588
// File doesn't contain loop information, looping isn't allowed.
589
return SCE_ERROR_ATRAC_NO_LOOP_INFORMATION;
590
}
591
// Just override the current loop counter.
592
info.loopNum = loopNum;
593
return 0;
594
}
595
596
int Atrac2::LoopNum() const {
597
return context_->info.loopNum;
598
}
599
600
int Atrac2::LoopStatus() const {
601
const SceAtracIdInfo &info = context_->info;
602
if (info.loopEnd == 0) {
603
// No loop available.
604
return 0;
605
} else if (info.loopNum != 0) {
606
// We've got at least one loop to go.
607
return 1;
608
} else {
609
// Return 1 if we haven't passed the loop point.
610
return info.decodePos <= info.loopEnd ? 1 : 0;
611
}
612
}
613
614
u32 Atrac2::GetNextSamples() {
615
SceAtracIdInfo &info = context_->info;
616
// TODO: Need to reformulate this.
617
const int endOfCurrentFrame = info.decodePos | info.SamplesFrameMask(); // bit trick!
618
const int remainder = std::max(0, endOfCurrentFrame - info.endSample);
619
const int adjusted = (info.decodePos & info.SamplesFrameMask()) + remainder;
620
return std::max(0, info.SamplesPerFrame() - adjusted);
621
}
622
623
int Atrac2::GetNextDecodePosition(int *pos) const {
624
const SceAtracIdInfo &info = context_->info;
625
// Check if we reached the end.
626
if (info.decodePos > info.endSample) {
627
return SCE_ERROR_ATRAC_ALL_DATA_DECODED;
628
}
629
// Check if remaining data in the file is smaller than a frame.
630
if (info.fileDataEnd - info.curFileOff < info.sampleSize) {
631
return SCE_ERROR_ATRAC_ALL_DATA_DECODED;
632
}
633
*pos = info.decodePos - info.firstValidSample;
634
return 0;
635
}
636
637
int Atrac2::AddStreamData(u32 bytesToAdd) {
638
SceAtracIdInfo &info = context_->info;
639
640
// WARNING: bytesToAdd might not be sampleSize aligned, even though we return a sampleSize-aligned size
641
// in GetStreamDataInfo, so other parts of the code still has to handle unaligned data amounts.
642
if (info.state == ATRAC_STATUS_HALFWAY_BUFFER) {
643
const int newFileOffset = info.streamDataByte + info.dataOff + bytesToAdd;
644
if (newFileOffset == info.fileDataEnd) {
645
info.state = ATRAC_STATUS_ALL_DATA_LOADED;
646
} else if (newFileOffset > info.fileDataEnd) {
647
return SCE_ERROR_ATRAC_ADD_DATA_IS_TOO_BIG;
648
}
649
info.streamDataByte += bytesToAdd;
650
} else {
651
// TODO: Check for SCE_ERROR_ATRAC_ADD_DATA_IS_TOO_BIG in the other modes too.
652
info.streamDataByte += bytesToAdd;
653
}
654
return 0;
655
}
656
657
static int ComputeLoopedStreamWritableBytes(const SceAtracIdInfo &info, const int loopStartFileOffset, const u32 loopEndFileOffset) {
658
const u32 writeOffset = info.curFileOff + info.streamDataByte;
659
if (writeOffset >= loopEndFileOffset) {
660
const int loopLength = loopEndFileOffset - loopStartFileOffset;
661
return loopLength - ((info.curFileOff + info.streamDataByte) - loopEndFileOffset) % loopLength;
662
} else {
663
return loopEndFileOffset - writeOffset;
664
}
665
}
666
667
static int IncrementAndLoop(int curOffset, int increment, int loopStart, int loopEnd) {
668
const int sum = curOffset + increment;
669
if (sum >= loopEnd) {
670
return loopStart + (sum - loopEnd) % (loopEnd - loopStart);
671
} else {
672
return sum;
673
}
674
}
675
676
static int WrapAroundRoundedBufferSize(int offset, int bufferSize, int addend, int grainSize) {
677
bufferSize = RoundDownToMultipleWithOffset(offset, bufferSize, grainSize);
678
const int sum = offset + addend;
679
if (bufferSize <= sum) {
680
return sum - bufferSize;
681
} else {
682
return sum;
683
}
684
}
685
686
static void ComputeStreamBufferDataInfo(const SceAtracIdInfo &info, u32 *writePtr, u32 *bytesToWrite, u32 *readFileOffset) {
687
// Streaming data info
688
//
689
// This really is the core logic of sceAtrac. Initially I had it quite complicated, but boiled it
690
// all down to fairly simple logic. And then boiled it down further and fixed bugs.
691
// And then had to make it WAY complicated again to support looping... Sigh.
692
const u32 streamOff = info.curBuffer != 1 ? info.streamOff : 0;
693
const int spaceUsed = ComputeSpaceUsed(info);
694
const int spaceLeftAfterStreamOff = RoundDownToMultipleWithOffset(streamOff, info.bufferByte, info.sampleSize);
695
const int streamPos = streamOff + spaceUsed;
696
int spaceLeftInBuffer;
697
if (streamPos >= spaceLeftAfterStreamOff) {
698
spaceLeftInBuffer = spaceLeftAfterStreamOff - spaceUsed;
699
} else {
700
spaceLeftInBuffer = spaceLeftAfterStreamOff - streamPos;
701
}
702
const int loopStartFileOffset = ComputeFileOffset(info, info.loopStart);
703
const int loopEndFileOffset = ComputeLoopEndFileOffset(info, info.loopEnd);
704
705
if (spaceLeftInBuffer < 0) {
706
// Most likely, the file was truncated.
707
WARN_LOG(Log::Atrac, "File corruption detected: spaceLeftInBuffer < 0: %d. Stumbling along.", spaceLeftInBuffer);
708
spaceLeftInBuffer = 0;
709
}
710
711
switch (info.state) {
712
case ATRAC_STATUS_STREAMED_WITHOUT_LOOP:
713
{
714
*bytesToWrite = std::clamp(info.fileDataEnd - (info.curFileOff + info.streamDataByte), 0, spaceLeftInBuffer);
715
const int streamFileOff = info.curFileOff + info.streamDataByte;
716
if (streamFileOff < info.fileDataEnd) {
717
*readFileOffset = streamFileOff;
718
*writePtr = info.buffer + WrapAroundRoundedBufferSize(info.streamOff, info.bufferByte, info.streamDataByte, info.sampleSize);
719
} else {
720
*readFileOffset = 0;
721
*writePtr = info.buffer;
722
}
723
break;
724
}
725
726
case ATRAC_STATUS_STREAMED_LOOP_FROM_END:
727
*bytesToWrite = std::min(ComputeLoopedStreamWritableBytes(info, loopStartFileOffset, loopEndFileOffset), spaceLeftInBuffer);
728
*readFileOffset = IncrementAndLoop(info.curFileOff, info.streamDataByte, loopStartFileOffset, loopEndFileOffset);
729
*writePtr = info.buffer + WrapAroundRoundedBufferSize(info.streamOff, info.bufferByte, info.streamDataByte, info.sampleSize);
730
break;
731
732
case ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER:
733
{
734
// Behaves like WITHOUT_LOOP or STREAMED_LOOP_FROM_END depending on the decode position.
735
if (info.decodePos <= info.loopEnd) {
736
*bytesToWrite = std::min(ComputeLoopedStreamWritableBytes(info, loopStartFileOffset, loopEndFileOffset), spaceLeftInBuffer);
737
*readFileOffset = IncrementAndLoop(info.curFileOff, info.streamDataByte, loopStartFileOffset, loopEndFileOffset);
738
} else {
739
const int streamFileOff = info.curFileOff + info.streamDataByte;
740
*bytesToWrite = std::clamp(info.fileDataEnd - streamFileOff, 0, spaceLeftInBuffer);
741
if (streamFileOff < info.fileDataEnd) {
742
*readFileOffset = streamFileOff;
743
} else {
744
*readFileOffset = 0;
745
}
746
}
747
if (info.decodePos <= info.loopEnd || info.curBuffer != 1) {
748
*writePtr = info.buffer + WrapAroundRoundedBufferSize(info.streamOff, info.bufferByte, info.streamDataByte, info.sampleSize);
749
} else {
750
*writePtr = info.buffer + WrapAroundRoundedBufferSize(0, info.bufferByte, spaceUsed, info.sampleSize);
751
}
752
break;
753
}
754
755
default:
756
// unreachable
757
_dbg_assert_(false);
758
break;
759
}
760
}
761
762
void Atrac2::GetStreamDataInfo(u32 *writePtr, u32 *bytesToWrite, u32 *readFileOffset) {
763
const SceAtracIdInfo &info = context_->info;
764
765
switch (info.state) {
766
case ATRAC_STATUS_ALL_DATA_LOADED:
767
// Nothing to do, the whole track is loaded already.
768
*writePtr = info.buffer;
769
*bytesToWrite = 0;
770
*readFileOffset = 0;
771
break;
772
773
case ATRAC_STATUS_HALFWAY_BUFFER:
774
{
775
// This is both the file offset and the offset in the buffer, since it's direct mapped
776
// in this mode (no wrapping or any other trickery).
777
const int fileOffset = (int)info.dataOff + (int)info.streamDataByte;
778
const int bytesLeftInFile = (int)info.fileDataEnd - fileOffset;
779
// Just ask for the rest of the data. The game can supply as much of it as it wants at a time.
780
*writePtr = info.buffer + fileOffset;
781
*bytesToWrite = bytesLeftInFile;
782
*readFileOffset = fileOffset;
783
break;
784
}
785
786
default:
787
ComputeStreamBufferDataInfo(info, writePtr, bytesToWrite, readFileOffset);
788
break;
789
}
790
}
791
792
u32 Atrac2::DecodeData(u8 *outbuf, u32 outbufAddr, int *SamplesNum, int *finish, int *remains) {
793
SceAtracIdInfo &info = context_->info;
794
795
const int tries = info.numSkipFrames + 1;
796
for (int i = 0; i < tries; i++) {
797
u32 result = DecodeInternal(outbufAddr, SamplesNum, finish);
798
if (result != 0) {
799
*SamplesNum = 0;
800
return result;
801
}
802
}
803
804
*remains = RemainingFrames();
805
return 0;
806
}
807
808
u32 Atrac2::DecodeInternal(u32 outbufAddr, int *SamplesNum, int *finish) {
809
SceAtracIdInfo &info = context_->info;
810
811
// Check that there's enough data to decode.
812
813
// Check for end of file.
814
const int samplesToDecode = GetNextSamples();
815
const int nextFileOff = info.curFileOff + info.sampleSize;
816
if (nextFileOff > info.fileDataEnd || info.decodePos > info.endSample) {
817
*finish = 1;
818
return SCE_ERROR_ATRAC_ALL_DATA_DECODED;
819
}
820
821
DEBUG_LOG(Log::Atrac, "Decode(%08x): samplesToDecode: %d nextFileOff: %d", outbufAddr, samplesToDecode, nextFileOff);
822
823
// Check for streaming buffer run-out.
824
if (AtracStatusIsStreaming(info.state) && info.streamDataByte < info.sampleSize) {
825
*finish = 0;
826
return SCE_ERROR_ATRAC_BUFFER_IS_EMPTY;
827
}
828
829
// Check for halfway buffer end.
830
if (info.state == ATRAC_STATUS_HALFWAY_BUFFER && info.dataOff + info.streamDataByte < nextFileOff) {
831
*finish = 0;
832
return SCE_ERROR_ATRAC_BUFFER_IS_EMPTY;
833
}
834
835
if (info.state == ATRAC_STATUS_FOR_SCESAS) {
836
_dbg_assert_(false);
837
}
838
839
u32 streamOff;
840
u32 bufferPtr;
841
if (!AtracStatusIsStreaming(info.state)) {
842
bufferPtr = info.buffer;
843
streamOff = info.curFileOff;
844
} else {
845
const int bufferIndex = info.curBuffer & 1;
846
bufferPtr = bufferIndex == 0 ? info.buffer : info.secondBuffer;
847
streamOff = bufferIndex == 0 ? info.streamOff : info.secondStreamOff;
848
}
849
850
u32 inAddr = bufferPtr + streamOff;
851
int16_t *outPtr;
852
853
_dbg_assert_(samplesToDecode <= info.SamplesPerFrame());
854
if (samplesToDecode != info.SamplesPerFrame()) {
855
if (!decodeTemp_) {
856
decodeTemp_ = new int16_t[info.SamplesPerFrame() * outputChannels_];
857
}
858
outPtr = decodeTemp_;
859
} else {
860
outPtr = outbufAddr ? (int16_t *)Memory::GetPointer(outbufAddr) : 0; // outbufAddr can be 0 during skip!
861
}
862
863
context_->codec.inBuf = inAddr;
864
context_->codec.outBuf = outbufAddr;
865
866
if (!Memory::IsValidAddress(inAddr)) {
867
ERROR_LOG(Log::Atrac, "DecodeInternal: Bad inAddr %08x", inAddr);
868
return SCE_ERROR_ATRAC_API_FAIL;
869
}
870
871
int bytesConsumed = 0;
872
int outSamples = 0;
873
if (!decoder_->Decode(Memory::GetPointerUnchecked(inAddr), info.sampleSize, &bytesConsumed, outputChannels_, outPtr, &outSamples)) {
874
// Decode failed.
875
*finish = 0;
876
// TODO: The error code here varies based on what the problem is, but not sure of the right values.
877
// 0000020b and 0000020c have been observed for 0xFF and/or garbage data, there may be more codes.
878
context_->codec.err = 0x20b;
879
return SCE_ERROR_ATRAC_API_FAIL; // tested.
880
} else {
881
context_->codec.err = 0;
882
}
883
884
_dbg_assert_(bytesConsumed == info.sampleSize);
885
886
// Advance the file offset.
887
info.curFileOff += info.sampleSize;
888
889
if (info.numSkipFrames == 0) {
890
*SamplesNum = samplesToDecode;
891
if (info.endSample < info.decodePos + samplesToDecode) {
892
*finish = info.loopNum == 0;
893
} else {
894
*finish = 0;
895
}
896
u8 *outBuf = outbufAddr ? Memory::GetPointerWrite(outbufAddr) : nullptr;
897
if (samplesToDecode != info.SamplesPerFrame() && samplesToDecode != 0 && outBuf) {
898
memcpy(outBuf, decodeTemp_, samplesToDecode * outputChannels_ * sizeof(int16_t));
899
}
900
901
// Handle increments and looping.
902
info.decodePos += samplesToDecode;
903
if (info.loopEnd != 0 && info.loopNum != 0 && info.decodePos > info.loopEnd) {
904
info.curFileOff = ComputeFileOffset(info, info.loopStart);
905
info.numSkipFrames = ComputeSkipFrames(info, info.loopStart);
906
info.decodePos = info.loopStart;
907
if (info.loopNum > 0) {
908
info.loopNum--;
909
}
910
}
911
} else {
912
info.numSkipFrames--;
913
}
914
915
// Handle streaming special cases.
916
if (AtracStatusIsStreaming(info.state)) {
917
info.streamDataByte -= info.sampleSize;
918
if (info.curBuffer == 1) {
919
// If currently streaming from the second buffer...
920
int nextStreamOff = info.secondStreamOff + info.sampleSize;
921
if ((int)info.secondBufferByte < nextStreamOff + info.sampleSize) {
922
// Done/ran out
923
info.streamOff = 0;
924
info.secondStreamOff = 0;
925
info.curBuffer = 2;
926
} else {
927
info.secondStreamOff = nextStreamOff;
928
}
929
} else {
930
// Normal streaming from the main buffer. Let's first look at wrapping around the end...
931
const int nextStreamOff = info.streamOff + info.sampleSize;
932
if (nextStreamOff + info.sampleSize > (int)info.bufferByte) {
933
info.streamOff = 0;
934
} else {
935
info.streamOff = nextStreamOff;
936
}
937
938
// OK, that's the simple stuff done. Moving on to second buffer streaming...
939
// This is quite a condition!
940
// Basically, if we're in state LOOP_WITH_TRAILER and currently streaming from the main buffer,
941
// and either there's no loop or we're just done with the final loop and haven't reached the loop point yet...
942
if (info.state == ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER && info.curBuffer == 0 &&
943
(info.loopEnd == 0 || (info.loopNum == 0 && info.loopEnd < info.decodePos))) {
944
// If, at that point, our file streaming offset has indeed reached the loop point...
945
if (info.curFileOff >= ComputeLoopEndFileOffset(info, info.loopEnd)) {
946
// Then we switch to streaming from the secondary buffer, and also let's copy the last partial
947
// packet from the second buffer back to the start of the main buffer...
948
info.curBuffer = 1;
949
info.streamDataByte = info.secondBufferByte;
950
info.secondStreamOff = 0;
951
memcpy(Memory::GetPointerWrite(info.buffer),
952
Memory::GetPointer(info.secondBuffer + (info.secondBufferByte - info.secondBufferByte % info.sampleSize)),
953
info.secondBufferByte % info.sampleSize);
954
}
955
}
956
}
957
}
958
return 0;
959
}
960
961
int Atrac2::SetData(const Track &track, u32 bufferAddr, u32 readSize, u32 bufferSize, u32 fileSize, int outputChannels, bool isAA3) {
962
TrackInfo trackInfo{};
963
if (bufferAddr) {
964
if (!isAA3) {
965
int retval = ParseWaveAT3(Memory::GetPointerRange(bufferAddr, bufferSize), readSize, &trackInfo);
966
if (retval < 0) {
967
ERROR_LOG(Log::Atrac, "Atrac2::SetData: ParseWaveAT3 failed with %08x", retval);
968
return retval;
969
}
970
} else {
971
int retval = ParseAA3(Memory::GetPointerRange(bufferAddr, bufferSize), readSize, fileSize, &trackInfo);
972
if (retval < 0) {
973
ERROR_LOG(Log::Atrac, "Atrac2::SetData: ParseAA3 failed with %08x", retval);
974
return retval;
975
}
976
}
977
}
978
979
int retval = InitContextFromTrackInfo(context_, &trackInfo, bufferAddr, readSize, bufferSize);
980
if (retval < 0) {
981
ERROR_LOG(Log::Atrac, "Atrac2::SetData: InitContextFromTrackInfo failed with %08x", retval);
982
return retval;
983
}
984
985
SceAtracIdInfo &info = context_->info;
986
987
CreateDecoder(info.codec, info.sampleSize, info.numChan);
988
989
outputChannels_ = outputChannels;
990
991
INFO_LOG(Log::Atrac,
992
"Atrac: sampleSize: %d buffer: %08x bufferByte: %d firstValidSample: %d\n"
993
"endSample: %d loopStart: %d loopEnd: %d\n"
994
"dataOff: %d curFileOff: %d streamOff: %d streamDataByte: %d\n"
995
"fileDataEnd: %d decodePos: %d numSkipFrames: %d",
996
info.sampleSize, info.buffer, info.bufferByte, info.firstValidSample,
997
info.endSample, info.loopStart, info.loopEnd,
998
info.dataOff, info.curFileOff, info.streamOff, info.streamDataByte,
999
info.fileDataEnd, info.decodePos, info.numSkipFrames
1000
);
1001
1002
int skipCount = 0; // TODO: use for delay
1003
retval = SkipFrames(&skipCount);
1004
1005
// Seen in Mui Mui house. Things go very wrong after this..
1006
if (retval == SCE_ERROR_ATRAC_API_FAIL) {
1007
ERROR_LOG(Log::Atrac, "Bad frame during initial skip");
1008
} else if (retval != 0) {
1009
ERROR_LOG(Log::Atrac, "SkipFrames during InitContext returned an error: %08x", retval);
1010
}
1011
WrapLastPacket();
1012
return retval;
1013
}
1014
1015
void Atrac2::WrapLastPacket() {
1016
SceAtracIdInfo &info = context_->info;
1017
1018
// If streaming, we need to handle wrapping the overshot partial packet at the end. If not we don't.
1019
if (!AtracStatusIsStreaming(info.state)) {
1020
return;
1021
}
1022
1023
// This logic is similar to GetStreamDataInfo.
1024
int distanceToEnd = RoundDownToMultiple(info.bufferByte - info.streamOff, info.sampleSize);
1025
if (info.streamDataByte < distanceToEnd) {
1026
// There's space left without wrapping. Don't do anything.
1027
// INFO_LOG(Log::Atrac, "Packets fit into the buffer fully. %08x < %08x", readSize, bufferSize);
1028
// In this case, seems we need to zero some bytes. In one test, this seems to be 336.
1029
// Maybe there's a logical bug and the copy happens even when not needed, it's just that it'll
1030
// copy zeroes. Either way, let's just copy some bytes to make our sanity check hexdump pass.
1031
Memory::Memset(info.buffer, 0, 128);
1032
} else {
1033
// Wraps around.
1034
const int copyStart = info.streamOff + distanceToEnd;
1035
const int copyLen = info.bufferByte - copyStart;
1036
// Then, let's copy it.
1037
DEBUG_LOG(Log::Atrac, "Packets didn't fit evenly. Last packet got split into %d/%d (sum=%d). Copying to start of buffer.",
1038
copyLen, info.sampleSize - copyLen, info.sampleSize);
1039
Memory::Memcpy(info.buffer, info.buffer + copyStart, copyLen);
1040
}
1041
}
1042
1043
u32 Atrac2::SkipFrames(int *skipCount) {
1044
SceAtracIdInfo &info = context_->info;
1045
*skipCount = 0;
1046
int finishIgnored;
1047
while (true) {
1048
if (info.numSkipFrames == 0) {
1049
return 0;
1050
}
1051
u32 retval = DecodeInternal(0, 0, &finishIgnored);
1052
if (retval != 0) {
1053
if (retval == SCE_ERROR_ATRAC_API_FAIL) {
1054
WARN_LOG(Log::Atrac, "Failed during skip-frame, ignoring: %08x", retval);
1055
(*skipCount)++;
1056
}
1057
return retval;
1058
} else {
1059
DEBUG_LOG(Log::Atrac, "Frame correctly decoded during skip. numSkipFrames == %d", info.numSkipFrames);
1060
}
1061
(*skipCount)++;
1062
}
1063
return 0;
1064
}
1065
1066
// Where to read from to fill the second buffer.
1067
int Atrac2::GetSecondBufferInfo(u32 *fileOffset, u32 *readSize) const {
1068
const SceAtracIdInfo &info = context_->info;
1069
if (info.state != ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER) {
1070
// No second buffer needed in this state.
1071
*fileOffset = 0;
1072
*readSize = 0;
1073
return SCE_ERROR_ATRAC_SECOND_BUFFER_NOT_NEEDED;
1074
}
1075
1076
const int loopEndFileOffset = ComputeLoopEndFileOffset(info, info.loopEnd);
1077
*fileOffset = loopEndFileOffset;
1078
*readSize = info.fileDataEnd - loopEndFileOffset;
1079
return 0;
1080
}
1081
1082
int Atrac2::SetSecondBuffer(u32 secondBuffer, u32 secondBufferSize) {
1083
SceAtracIdInfo &info = context_->info;
1084
1085
u32 loopEndFileOffset = ComputeLoopEndFileOffset(info, info.loopEnd);
1086
if ((info.sampleSize * 3 <= (int)secondBufferSize ||
1087
(info.fileDataEnd - loopEndFileOffset) <= (int)secondBufferSize)) {
1088
if (info.state == ATRAC_STATUS_STREAMED_LOOP_WITH_TRAILER) {
1089
info.secondBuffer = secondBuffer;
1090
info.secondBufferByte = secondBufferSize;
1091
info.secondStreamOff = 0;
1092
return 0;
1093
} else {
1094
return SCE_ERROR_ATRAC_SECOND_BUFFER_NOT_NEEDED;
1095
}
1096
}
1097
return SCE_ERROR_ATRAC_SIZE_TOO_SMALL;
1098
}
1099
1100
u32 Atrac2::GetInternalCodecError() const {
1101
if (context_.IsValid()) {
1102
return context_->codec.err;
1103
} else {
1104
return 0;
1105
}
1106
}
1107
1108
int Atrac2::Bitrate() const {
1109
const SceAtracIdInfo &info = context_->info;
1110
return info.BitRate();
1111
}
1112
1113
void Atrac2::InitLowLevel(const Atrac3LowLevelParams &params, int codecType) {
1114
SceAtracIdInfo &info = context_->info;
1115
info.codec = codecType;
1116
info.numChan = params.encodedChannels;
1117
outputChannels_ = params.outputChannels;
1118
info.sampleSize = params.bytesPerFrame;
1119
info.dataOff = 0;
1120
info.decodePos = 0;
1121
info.state = ATRAC_STATUS_LOW_LEVEL;
1122
CreateDecoder(codecType, info.sampleSize, info.numChan);
1123
}
1124
1125
int Atrac2::DecodeLowLevel(const u8 *srcData, int *bytesConsumed, s16 *dstData, int *bytesWritten) {
1126
SceAtracIdInfo &info = context_->info;
1127
1128
const int channels = outputChannels_;
1129
int outSamples = 0;
1130
bool success = decoder_->Decode(srcData, info.sampleSize, bytesConsumed, channels, dstData, &outSamples);
1131
if (!success) {
1132
ERROR_LOG(Log::Atrac, "Low level decoding failed: sampleSize: %d bytesConsumed: %d", info.sampleSize, *bytesConsumed);
1133
// We proceed anyway, see issue #20452
1134
/*
1135
*bytesConsumed = 0;
1136
*bytesWritten = 0;
1137
return SCE_ERROR_ATRAC_API_FAIL; // need to check what return value we get here.
1138
*/
1139
}
1140
*bytesWritten = outSamples * channels * sizeof(int16_t);
1141
// TODO: Possibly return a decode error on bad data.
1142
return 0;
1143
}
1144
1145
void Atrac2::CheckForSas() {
1146
SceAtracIdInfo &info = context_->info;
1147
if (info.numChan != 1) {
1148
WARN_LOG(Log::Atrac, "Caller forgot to set channels to 1");
1149
}
1150
if (info.state != 0x10) {
1151
WARN_LOG(Log::Atrac, "Caller forgot to set state to 0x10");
1152
}
1153
sas_.isStreaming = info.fileDataEnd > (s32)info.bufferByte;
1154
if (sas_.isStreaming) {
1155
INFO_LOG(Log::Atrac, "SasAtrac stream mode");
1156
} else {
1157
INFO_LOG(Log::Atrac, "SasAtrac non-streaming mode");
1158
}
1159
}
1160
1161
int Atrac2::EnqueueForSas(u32 address, u32 ptr) {
1162
SceAtracIdInfo &info = context_->info;
1163
// Set the new buffer up to be adopted by the next call to Decode that needs more data.
1164
// Note: Can't call this if the decoder isn't asking for another buffer to be queued.
1165
if (info.secondBuffer != 0xFFFFFFFF) {
1166
return SCE_SAS_ERROR_ATRAC3_ALREADY_QUEUED;
1167
}
1168
1169
if (address == 0 && ptr == 0) {
1170
WARN_LOG(Log::Atrac, "Caller tries to send us a zero buffer. Something went wrong.");
1171
}
1172
1173
DEBUG_LOG(Log::Atrac, "EnqueueForSas: Second buffer updated to %08x, sz: %08x", address, ptr);
1174
info.secondBuffer = address;
1175
info.secondBufferByte = ptr;
1176
return 0;
1177
}
1178
1179
// Completely different streaming setup!
1180
void Atrac2::DecodeForSas(s16 *dstData, int *bytesWritten, int *finish) {
1181
SceAtracIdInfo &info = context_->info;
1182
*bytesWritten = 0;
1183
1184
// First frame handling. Not sure if accurate. Set up the initial buffer as the current streaming buffer.
1185
// Also works for the non-streaming case.
1186
if (info.buffer) {
1187
sas_.curBuffer = 0;
1188
sas_.bufPtr[0] = info.buffer;
1189
sas_.bufSize[0] = info.bufferByte - info.streamOff; // also equals info.streamDataByte
1190
sas_.streamOffset = 0;
1191
sas_.fileOffset = info.bufferByte; // Possibly should just set it to info.curFileOff
1192
info.buffer = 0; // yes, this happens.
1193
}
1194
1195
u8 assembly[1000];
1196
// Keep decoding from the current buffer until it runs out.
1197
if (sas_.streamOffset + (int)info.sampleSize <= (int)sas_.bufSize[sas_.curBuffer]) {
1198
// Just decode.
1199
const u8 *srcData = Memory::GetPointer(sas_.bufPtr[sas_.curBuffer] + sas_.streamOffset);
1200
int bytesConsumed = 0;
1201
bool decodeResult = decoder_->Decode(srcData, info.sampleSize, &bytesConsumed, 1, dstData, bytesWritten);
1202
if (!decodeResult) {
1203
ERROR_LOG(Log::Atrac, "SAS failed to decode regular packet");
1204
}
1205
sas_.streamOffset += bytesConsumed;
1206
} else if (sas_.isStreaming) {
1207
// TODO: Do we need special handling for the first buffer, since SetData will wrap around that packet? I think yes!
1208
DEBUG_LOG(Log::Atrac, "Streaming atrac through sas, and hit the end of buffer %d", sas_.curBuffer);
1209
1210
// Compute the part sizes using the current size.
1211
int part1Size = sas_.bufSize[sas_.curBuffer] - sas_.streamOffset;
1212
int part2Size = info.sampleSize - part1Size;
1213
_dbg_assert_(part1Size >= 0);
1214
if (part1Size >= 0) {
1215
// Grab the partial packet, before we switch over to the other buffer.
1216
Memory::Memcpy(assembly, sas_.bufPtr[sas_.curBuffer] + sas_.streamOffset, part1Size);
1217
}
1218
1219
// Check if we hit the end.
1220
if (sas_.fileOffset >= info.fileDataEnd) {
1221
DEBUG_LOG(Log::Atrac, "Streaming and hit the file end.");
1222
*bytesWritten = 0;
1223
*finish = 1;
1224
return;
1225
}
1226
1227
// Check that a new buffer actually exists
1228
if (info.secondBuffer == sas_.bufPtr[sas_.curBuffer]) {
1229
ERROR_LOG(Log::Atrac, "Can't enqueue the same buffer twice in a row!");
1230
*bytesWritten = 0;
1231
*finish = 1;
1232
return;
1233
}
1234
1235
if ((int)info.secondBuffer < 0) {
1236
ERROR_LOG(Log::Atrac, "AtracSas streaming ran out of data, no secondbuffer pending");
1237
*bytesWritten = 0;
1238
*finish = 1;
1239
return;
1240
}
1241
1242
// Switch to the other buffer.
1243
sas_.curBuffer ^= 1;
1244
1245
sas_.bufPtr[sas_.curBuffer] = info.secondBuffer;
1246
sas_.bufSize[sas_.curBuffer] = info.secondBufferByte;
1247
sas_.fileOffset += info.secondBufferByte;
1248
1249
sas_.streamOffset = part2Size;
1250
1251
// If we'll reach the end during this buffer, set second buffer to 0, signaling that we don't need more data.
1252
if (sas_.fileOffset >= info.fileDataEnd) {
1253
// We've reached the end.
1254
info.secondBuffer = 0;
1255
DEBUG_LOG(Log::Atrac, "%08x >= %08x: Reached the end.", sas_.fileOffset, info.fileDataEnd);
1256
} else {
1257
// Signal to the caller that we accept a new next buffer.
1258
info.secondBuffer = 0xFFFFFFFF;
1259
}
1260
1261
DEBUG_LOG(Log::Atrac, "Switching over to buffer %d, updating buffer to %08x, sz: %08x. %s", sas_.curBuffer, info.secondBuffer, info.secondBufferByte, info.secondBuffer == 0xFFFFFFFF ? "Signalling for more data." : "");
1262
1263
// Copy the second half (or if part1Size == 0, the whole packet) to the assembly buffer.
1264
Memory::Memcpy(assembly + part1Size, sas_.bufPtr[sas_.curBuffer], part2Size);
1265
// Decode the packet from the assembly, whether it's was assembled from two or one.
1266
const u8 *srcData = assembly;
1267
int bytesConsumed = 0;
1268
bool decodeResult = decoder_->Decode(srcData, info.sampleSize, &bytesConsumed, 1, dstData, bytesWritten);
1269
if (!decodeResult) {
1270
ERROR_LOG(Log::Atrac, "SAS failed to decode assembled packet");
1271
}
1272
}
1273
}
1274
1275