From 1b8bbb509aacb9b7a200bfdcf82e4ea0bb66a7d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20G=C3=BCnther?= Date: Fri, 22 Aug 2014 20:36:53 +0200 Subject: [PATCH] FFMPEG Plugin: Refactor out checking of invalid conditions in audio path. - Main reason for this refactoring is to increase readability and thus make audio decode path more comprehensible. - Added documentation for the new method accordingly. - Small change in calculating the decoded data size to clear when error occurs during decoding. This way it is more readable and more consistent with calculations of decoded data size on other locations. - No functional change intended. --- .../media/plugins/ffmpeg/AVCodecDecoder.cpp | 38 +++++++++++++------ .../media/plugins/ffmpeg/AVCodecDecoder.h | 1 + 2 files changed, 27 insertions(+), 12 deletions(-) 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();