From 9382dab811428af7a3329deebc3449c6ab8b83c8 Mon Sep 17 00:00:00 2001 From: Torbjörn Andersson Date: Wed, 6 Jul 2016 20:51:28 +0200 Subject: AUDIO: Fix audio corruption in MS ADPCM decoder 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. --- audio/decoders/adpcm.cpp | 5 +++-- audio/decoders/adpcm_intern.h | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'audio') diff --git a/audio/decoders/adpcm.cpp b/audio/decoders/adpcm.cpp index fd97d5d109..ffb61a49c0 100644 --- a/audio/decoders/adpcm.cpp +++ b/audio/decoders/adpcm.cpp @@ -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--; } diff --git a/audio/decoders/adpcm_intern.h b/audio/decoders/adpcm_intern.h index 3a2af91c90..f4a708c3fc 100644 --- a/audio/decoders/adpcm_intern.h +++ b/audio/decoders/adpcm_intern.h @@ -209,6 +209,7 @@ public: 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); } @@ -220,6 +221,7 @@ protected: private: uint8 _decodedSampleCount; + uint8 _decodedSampleIndex; int16 _decodedSamples[4]; }; -- cgit v1.2.3