Fixed a couple edge case bugs in Stream and FfmpegDecoder.

This commit is contained in:
casey langen 2018-12-26 23:36:31 -08:00
parent cc134775ef
commit 205e3e301d
2 changed files with 25 additions and 15 deletions

View File

@ -235,6 +235,10 @@ void Stream::RefillInternalBuffers() {
break;
}
if (this->decoderBuffer->Samples() == 0) {
continue;
}
this->decoderSamplesRemain = this->decoderBuffer->Samples();
this->decoderSampleOffset = 0;
}
@ -269,20 +273,21 @@ void Stream::RefillInternalBuffers() {
targetSamplesRemain = this->samplesPerBuffer - targetSampleOffset;
if (targetSamplesRemain > 0) {
long samplesToCopy = std::min(this->decoderSamplesRemain, targetSamplesRemain);
if (samplesToCopy > 0) {
float* src = this->decoderBuffer->BufferPointer() + this->decoderSampleOffset;
target->Copy(src, samplesToCopy, targetSampleOffset);
float* src = this->decoderBuffer->BufferPointer() + this->decoderSampleOffset;
target->Copy(src, samplesToCopy, targetSampleOffset);
this->decoderPosition += samplesToCopy;
this->decoderSampleOffset += samplesToCopy;
this->decoderSamplesRemain -= samplesToCopy;
this->decoderPosition += samplesToCopy;
this->decoderSampleOffset += samplesToCopy;
this->decoderSamplesRemain -= samplesToCopy;
targetSampleOffset += samplesToCopy;
targetSampleOffset += samplesToCopy;
if (targetSampleOffset == this->samplesPerBuffer) {
targetSampleOffset = 0;
target = nullptr;
--count; /* target buffer has been filled. */
if (targetSampleOffset == this->samplesPerBuffer) {
targetSampleOffset = 0;
target = nullptr;
--count; /* target buffer has been filled. */
}
}
}
}

View File

@ -142,6 +142,7 @@ bool FfmpegDecoder::GetBuffer(IBuffer *buffer) {
if (this->ioContext) {
buffer->SetSampleRate((long) this->rate);
buffer->SetChannels((long) this->channels);
buffer->SetSamples(0);
if (!av_read_frame(this->formatContext, &this->packet)) {
int frameDecoded = 0;
@ -164,6 +165,7 @@ bool FfmpegDecoder::GetBuffer(IBuffer *buffer) {
nullptr, channels, samples, inFormat, 1);
if (decodedSize > 0) {
/* preferred buffer size based on input data */
buffer->SetSamples(samples * channels);
if (!this->resampler) {
@ -185,10 +187,13 @@ bool FfmpegDecoder::GetBuffer(IBuffer *buffer) {
uint8_t* outData = (uint8_t*) buffer->BufferPointer();
const uint8_t** inData = (const uint8_t**) this->decodedFrame->extended_data;
swr_convert(this->resampler, &outData, samples, inData, samples);
}
else {
buffer->SetSamples(0);
int convertedSamplesPerChannel = swr_convert(
this->resampler, &outData, samples, inData, samples);
/* actual buffer size, based on resampler output. should be the same
as the preferred size... */
buffer->SetSamples(convertedSamplesPerChannel * this->channels);
}
}