diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp index d7dd2e5457..f404d653a2 100644 --- a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp +++ b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp @@ -672,17 +672,7 @@ AVCodecDecoder::_DecodeNextAudioFrame() fRawDecodedAudio->data[0] = fDecodedData; while (fRawDecodedAudio->nb_samples < fOutputFrameCount) { - // Check conditions which would hint at broken code below. - if (fDecodedDataBufferSize < 0) { - fprintf(stderr, "Decoding read past the end of the decoded data " - "buffer! %ld\n", fDecodedDataBufferSize); - fDecodedDataBufferSize = 0; - } - if (fTempPacket.size < 0) { - fprintf(stderr, "Decoding read past the end of the temp packet! " - "%d\n", fTempPacket.size); - fTempPacket.size = 0; - } + _CheckAndFixConditionsThatHintAtBrokenAudioCodeBelow(); bool decodedDataBufferHasData = fDecodedDataBufferSize > 0; if (decodedDataBufferHasData) { @@ -700,7 +690,9 @@ AVCodecDecoder::_DecodeNextAudioFrame() if (decodingStatus != B_OK) { // Assume the audio decoded until now is broken so replace it with // some silence. - memset(fDecodedData, 0, fRawDecodedAudio->data[0] - fDecodedData); + size_t decodedDataSize + = fRawDecodedAudio->nb_samples * fOutputFrameSize; + memset(fDecodedData, 0, decodedDataSize); if (!fAudioDecodeError) { // Report failure if not done already @@ -730,6 +722,28 @@ AVCodecDecoder::_DecodeNextAudioFrame() } +/*! \brief Checks fDecodedDataBufferSize and fTempPacket for invalid values, + reports them and assigns valid values. + + Note: This method is intended to be called before any code is executed that + deals with moving, loading or decoding any audio frames. +*/ +void +AVCodecDecoder::_CheckAndFixConditionsThatHintAtBrokenAudioCodeBelow() +{ + if (fDecodedDataBufferSize < 0) { + fprintf(stderr, "Decoding read past the end of the decoded data " + "buffer! %ld\n", fDecodedDataBufferSize); + fDecodedDataBufferSize = 0; + } + if (fTempPacket.size < 0) { + fprintf(stderr, "Decoding read past the end of the temp packet! %d\n", + fTempPacket.size); + fTempPacket.size = 0; + } +} + + /*! \brief Moves audio frames from fDecodedDataBuffer to fRawDecodedAudio (and thus to fDecodedData) and updates the start times of fRawDecodedAudio, fDecodedDataBuffer and fTempPacket accordingly. diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.h b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.h index 8034104e2a..2dcc4eab3c 100644 --- a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.h +++ b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.h @@ -61,6 +61,7 @@ private: media_header* mediaHeader, media_decode_info* info); status_t _DecodeNextAudioFrame(); + void _CheckAndFixConditionsThatHintAtBrokenAudioCodeBelow(); void _MoveAudioFramesToRawDecodedAudioAndUpdateStartTimes(); status_t _LoadNextAudioChunkIfNeededAndAssignStartTime(); status_t _DecodeSomeAudioFramesIntoEmptyDecodedDataBuffer();