Skip to content

Commit

Permalink
AUDIO: Fix audio corruption in MS ADPCM decoder
Browse files Browse the repository at this point in the history
Since _decodedSamples[] is filled with either 2 or 4 samples, we
can't use 1 - (count - 1) to "ensure that acts as a FIFO". When
we have 4 samples, that index will become negative, putting
uninitialized data into the buffer.

We could still use a similar trick, but I think it's much clearer
to use an index variable like this. We need an addition variable
either way.
  • Loading branch information
Torbjörn Andersson committed Jul 6, 2016
1 parent 20ccd3a commit 9382dab
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions audio/decoders/adpcm.cpp
Expand Up @@ -320,10 +320,11 @@ int MS_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
_decodedSamples[_decodedSampleCount++] = decodeMS(&_status.ch[0], (data >> 4) & 0x0f);
_decodedSamples[_decodedSampleCount++] = decodeMS(&_status.ch[_channels - 1], data & 0x0f);
}
_decodedSampleIndex = 0;
}

// (1 - (count - 1)) ensures that _decodedSamples acts as a FIFO of depth 2
buffer[samples] = _decodedSamples[1 - (_decodedSampleCount - 1)];
// _decodedSamples acts as a FIFO of depth 2 or 4;
buffer[samples] = _decodedSamples[_decodedSampleIndex++];
_decodedSampleCount--;
}

Expand Down
2 changes: 2 additions & 0 deletions audio/decoders/adpcm_intern.h
Expand Up @@ -209,6 +209,7 @@ class MS_ADPCMStream : public ADPCMStream {
error("MS_ADPCMStream(): blockAlign isn't specified for MS ADPCM");
memset(&_status, 0, sizeof(_status));
_decodedSampleCount = 0;
_decodedSampleIndex = 0;
}

virtual bool endOfData() const { return (_stream->eos() || _stream->pos() >= _endpos) && (_decodedSampleCount == 0); }
Expand All @@ -220,6 +221,7 @@ class MS_ADPCMStream : public ADPCMStream {

private:
uint8 _decodedSampleCount;
uint8 _decodedSampleIndex;
int16 _decodedSamples[4];
};

Expand Down

0 comments on commit 9382dab

Please sign in to comment.