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.
This commit is contained in:
Colin Günther 2014-08-22 20:36:53 +02:00
parent ca5c686d93
commit 1b8bbb509a
2 changed files with 27 additions and 12 deletions

View File

@ -672,17 +672,7 @@ AVCodecDecoder::_DecodeNextAudioFrame()
fRawDecodedAudio->data[0] = fDecodedData; fRawDecodedAudio->data[0] = fDecodedData;
while (fRawDecodedAudio->nb_samples < fOutputFrameCount) { while (fRawDecodedAudio->nb_samples < fOutputFrameCount) {
// Check conditions which would hint at broken code below. _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;
}
bool decodedDataBufferHasData = fDecodedDataBufferSize > 0; bool decodedDataBufferHasData = fDecodedDataBufferSize > 0;
if (decodedDataBufferHasData) { if (decodedDataBufferHasData) {
@ -700,7 +690,9 @@ AVCodecDecoder::_DecodeNextAudioFrame()
if (decodingStatus != B_OK) { if (decodingStatus != B_OK) {
// Assume the audio decoded until now is broken so replace it with // Assume the audio decoded until now is broken so replace it with
// some silence. // some silence.
memset(fDecodedData, 0, fRawDecodedAudio->data[0] - fDecodedData); size_t decodedDataSize
= fRawDecodedAudio->nb_samples * fOutputFrameSize;
memset(fDecodedData, 0, decodedDataSize);
if (!fAudioDecodeError) { if (!fAudioDecodeError) {
// Report failure if not done already // 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 /*! \brief Moves audio frames from fDecodedDataBuffer to fRawDecodedAudio (and
thus to fDecodedData) and updates the start times of fRawDecodedAudio, thus to fDecodedData) and updates the start times of fRawDecodedAudio,
fDecodedDataBuffer and fTempPacket accordingly. fDecodedDataBuffer and fTempPacket accordingly.

View File

@ -61,6 +61,7 @@ private:
media_header* mediaHeader, media_header* mediaHeader,
media_decode_info* info); media_decode_info* info);
status_t _DecodeNextAudioFrame(); status_t _DecodeNextAudioFrame();
void _CheckAndFixConditionsThatHintAtBrokenAudioCodeBelow();
void _MoveAudioFramesToRawDecodedAudioAndUpdateStartTimes(); void _MoveAudioFramesToRawDecodedAudioAndUpdateStartTimes();
status_t _LoadNextAudioChunkIfNeededAndAssignStartTime(); status_t _LoadNextAudioChunkIfNeededAndAssignStartTime();
status_t _DecodeSomeAudioFramesIntoEmptyDecodedDataBuffer(); status_t _DecodeSomeAudioFramesIntoEmptyDecodedDataBuffer();