Skip to content

Commit

Permalink
Clamp the final sample count during decode.
Browse files Browse the repository at this point in the history
Some games depend on / expect this, or else they'll let important data get
overwritten.  This also handles GHA phase shifting skipped frames better.
  • Loading branch information
unknownbrackets committed Sep 23, 2014
1 parent 9b1eae6 commit 326421c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Core/HLE/sceAtrac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ u32 _AtracDecodeData(int atracID, u8 *outbuf, u32 outbufPtr, u32 *SamplesNum, u3
atrac->SeekToSample(atrac->currentSample == 0 ? 0 : atrac->currentSample + offsetSamples);
AVPacket packet;
av_init_packet(&packet);
int got_frame, avret;
int got_frame = 0, avret;
while (av_read_frame(atrac->pFormatCtx, &packet) >= 0) {
if (packet.stream_index != atrac->audio_stream_index) {
av_free_packet(&packet);
Expand Down Expand Up @@ -676,6 +676,9 @@ u32 _AtracDecodeData(int atracID, u8 *outbuf, u32 outbufPtr, u32 *SamplesNum, u3
skipSamples -= skipped;
numSamples = atrac->pFrame->nb_samples - skipped;

// If we're at the end, clamp to samples we want. It always returns a full chunk.
numSamples = std::min((u32)atrac->endSample - (u32)atrac->currentSample, numSamples);

if (skipped > 0 && numSamples == 0) {
// Wait for the next one.
got_frame = 0;
Expand Down Expand Up @@ -710,6 +713,15 @@ u32 _AtracDecodeData(int atracID, u8 *outbuf, u32 outbufPtr, u32 *SamplesNum, u3
break;
}
}

if (!got_frame && atrac->currentSample < atrac->endSample) {
// Never got a frame. We may have dropped a GHA frame or otherwise have a bug.
// For now, let's try to provide an extra "frame" if possible so games don't infinite loop.
numSamples = std::min((u32)atrac->endSample - (u32)atrac->currentSample, atracSamplesPerFrame);
u32 outBytes = numSamples * atrac->atracOutputChannels * sizeof(s16);
memset(outbuf, 0, outBytes);
CBreakPoints::ExecMemCheck(outbufPtr, true, outBytes, currentMIPS->pc);
}
}
#endif // USE_FFMPEG

Expand Down

0 comments on commit 326421c

Please sign in to comment.