Chromium Code Reviews| Index: media/filters/ffmpeg_audio_decoder.cc |
| diff --git a/media/filters/ffmpeg_audio_decoder.cc b/media/filters/ffmpeg_audio_decoder.cc |
| index 5384d63c48874a70c01b8c00aece2eebe5392d5b..1040127fe2e46b3b2cc0e02f2b7c4364715cd296 100644 |
| --- a/media/filters/ffmpeg_audio_decoder.cc |
| +++ b/media/filters/ffmpeg_audio_decoder.cc |
| @@ -8,6 +8,7 @@ |
| #include "base/callback_helpers.h" |
| #include "base/location.h" |
| #include "base/message_loop_proxy.h" |
| +#include "media/base/audio_bus.h" |
| #include "media/base/audio_decoder_config.h" |
| #include "media/base/audio_timestamp_helper.h" |
| #include "media/base/data_buffer.h" |
| @@ -283,6 +284,11 @@ bool FFmpegAudioDecoder::ConfigureDecoder() { |
| codec_context_ = avcodec_alloc_context3(NULL); |
| AudioDecoderConfigToAVCodecContext(config, codec_context_); |
| + // MP3 decodes to S16P which we don't support, tell it to use S16 instead. |
| + bool request_s16_format = codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P; |
|
scherkus (not reviewing)
2012/12/13 22:40:11
ditto
|
| + if (request_s16_format) |
| + codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16; |
| + |
| AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
| if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) { |
| DLOG(ERROR) << "Could not initialize audio decoder: " |
| @@ -290,6 +296,26 @@ bool FFmpegAudioDecoder::ConfigureDecoder() { |
| return false; |
| } |
| + // Ensure avcodec_open2() respected our format request. |
| + if (request_s16_format && codec_context_->sample_fmt != AV_SAMPLE_FMT_S16) { |
| + DLOG(ERROR) << "Unable to configure a supported sample format: " |
| + << codec_context_->sample_fmt; |
| + return false; |
| + } |
| + |
| + // Some codecs will only output float data, so we need to convert to integer |
| + // before returning the decoded buffer. |
| + if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP || |
| + codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { |
| + // Preallocate the AudioBus for float conversions. We can treat interleaved |
| + // float data as a single planar channel since our output is expected in an |
| + // interleaved format anyways. |
| + int channels = codec_context_->channels; |
| + if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) |
| + channels = 1; |
| + converter_bus_ = AudioBus::CreateWrapper(channels); |
| + } |
| + |
| // Success! |
| av_frame_ = avcodec_alloc_frame(); |
| bits_per_channel_ = config.bits_per_channel(); |
| @@ -297,6 +323,7 @@ bool FFmpegAudioDecoder::ConfigureDecoder() { |
| samples_per_second_ = config.samples_per_second(); |
| output_timestamp_helper_.reset(new AudioTimestampHelper( |
| config.bytes_per_frame(), config.samples_per_second())); |
| + bytes_per_frame_ = config.bytes_per_frame(); |
| return true; |
| } |
| @@ -374,7 +401,6 @@ void FFmpegAudioDecoder::RunDecodeLoop( |
| } |
| } |
| - const uint8* decoded_audio_data = NULL; |
| int decoded_audio_size = 0; |
| if (frame_decoded) { |
| int output_sample_rate = av_frame_->sample_rate; |
| @@ -388,24 +414,64 @@ void FFmpegAudioDecoder::RunDecodeLoop( |
| break; |
| } |
| - decoded_audio_data = av_frame_->data[0]; |
| decoded_audio_size = av_samples_get_buffer_size( |
| NULL, codec_context_->channels, av_frame_->nb_samples, |
| codec_context_->sample_fmt, 1); |
| + // If we're decoding into float, adjust audio size. |
| + if (converter_bus_ && bits_per_channel_ / 8 != sizeof(float)) { |
| + DCHECK(codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT || |
| + codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP); |
| + decoded_audio_size *= |
| + static_cast<float>(bits_per_channel_ / 8) / sizeof(float); |
| + } |
| } |
| - scoped_refptr<DataBuffer> output; |
| - |
| + int start_sample = 0; |
| if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { |
| + DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) |
| + << "Decoder didn't output full frames"; |
| + |
| int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); |
| - decoded_audio_data += dropped_size; |
| + start_sample = dropped_size / bytes_per_frame_; |
| decoded_audio_size -= dropped_size; |
| output_bytes_to_drop_ -= dropped_size; |
| } |
| + scoped_refptr<DataBuffer> output; |
| if (decoded_audio_size > 0) { |
| - // Copy the audio samples into an output buffer. |
| - output = new DataBuffer(decoded_audio_data, decoded_audio_size); |
| + DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) |
| + << "Decoder didn't output full frames"; |
| + |
| + // Convert float data using an AudioBus. |
| + if (converter_bus_) { |
| + // Setup the AudioBus as a wrapper of the AVFrame data and then use |
| + // AudioBus::ToInterleaved() to convert the data as necessary. |
| + int skip_frames = start_sample; |
| + int total_frames = av_frame_->nb_samples - start_sample; |
| + if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { |
| + DCHECK_EQ(converter_bus_->channels(), 1); |
| + total_frames *= codec_context_->channels; |
| + skip_frames *= codec_context_->channels; |
| + } |
| + converter_bus_->set_frames(total_frames); |
| + DCHECK_EQ(decoded_audio_size, |
| + converter_bus_->frames() * bytes_per_frame_); |
| + |
| + for (int i = 0; i < converter_bus_->channels(); ++i) { |
| + converter_bus_->SetChannelData(i, reinterpret_cast<float*>( |
| + av_frame_->extended_data[i]) + skip_frames); |
| + } |
| + |
| + output = new DataBuffer(decoded_audio_size); |
| + output->SetDataSize(decoded_audio_size); |
| + converter_bus_->ToInterleaved( |
| + converter_bus_->frames(), bits_per_channel_ / 8, |
| + output->GetWritableData()); |
| + } else { |
| + output = new DataBuffer( |
| + av_frame_->extended_data[0] + start_sample * bytes_per_frame_, |
| + decoded_audio_size); |
| + } |
| output->SetTimestamp(output_timestamp_helper_->GetTimestamp()); |
| output->SetDuration( |
| output_timestamp_helper_->GetDuration(decoded_audio_size)); |