Chromium Code Reviews| Index: media/filters/audio_file_reader.cc |
| diff --git a/media/filters/audio_file_reader.cc b/media/filters/audio_file_reader.cc |
| index cf295b6f4467f5c0b93da91f4103636f90f6d14b..3e1b507a2a93ed074937fd0808f683544ff6aaff 100644 |
| --- a/media/filters/audio_file_reader.cc |
| +++ b/media/filters/audio_file_reader.cc |
| @@ -79,14 +79,27 @@ bool AudioFileReader::Open() { |
| AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
| if (codec) { |
| + // MP3 decodes to S16P which we don't support, tell it to use S16 instead. |
|
scherkus (not reviewing)
2012/12/13 22:40:11
should we also be checking the codec id?
DaleCurtis
2012/12/13 22:51:28
We could but, but this is more generic and the che
|
| + bool request_s16_format = codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P; |
|
scherkus (not reviewing)
2012/12/13 22:40:11
how about instead of a bool (which I found a bit t
DaleCurtis
2012/12/13 22:51:28
Sounds good. Will do so in next patch set.
DaleCurtis
2012/12/14 20:48:32
Done.
|
| + if (request_s16_format) |
| + codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16; |
| + |
| if ((result = avcodec_open2(codec_context_, codec, NULL)) < 0) { |
| DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" |
| - << " result: " << result; |
| + << " result: " << result; |
| + return false; |
| + } |
| + |
| + // Ensure avcodec_open2() respected our format request. |
| + if (request_s16_format && codec_context_->sample_fmt != AV_SAMPLE_FMT_S16) { |
| + DLOG(ERROR) << "AudioFileReader::Open() : unable to configure a" |
| + << " supported sample format - " |
| + << codec_context_->sample_fmt; |
| return false; |
| } |
| } else { |
| DLOG(WARNING) << "AudioFileReader::Open() : could not find codec -" |
| - << " result: " << result; |
| + << " result: " << result; |
| return false; |
| } |
| @@ -163,10 +176,28 @@ int AudioFileReader::Read(AudioBus* audio_bus) { |
| if (current_frame + frames_read > audio_bus->frames()) |
| frames_read = audio_bus->frames() - current_frame; |
| - // Deinterleave each channel and convert to 32bit floating-point |
| - // with nominal range -1.0 -> +1.0. |
| - audio_bus->FromInterleavedPartial( |
| - av_frame->data[0], current_frame, frames_read, bytes_per_sample); |
| + // Deinterleave each channel and convert to 32bit floating-point with |
| + // nominal range -1.0 -> +1.0. If the output is already in float planar |
| + // format, just copy it into the AudioBus. |
| + if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { |
| + float* decoded_audio_data = reinterpret_cast<float*>(av_frame->data[0]); |
| + int channels = audio_bus->channels(); |
| + for (int ch = 0; ch < channels; ++ch) { |
| + float* bus_data = audio_bus->channel(ch) + current_frame; |
| + for (int i = 0, offset = ch; i < frames_read; |
| + ++i, offset += channels) { |
| + bus_data[i] = decoded_audio_data[offset]; |
| + } |
| + } |
| + } else if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP) { |
| + for (int ch = 0; ch < audio_bus->channels(); ++ch) { |
| + memcpy(audio_bus->channel(ch) + current_frame, |
| + av_frame->extended_data[ch], sizeof(float) * frames_read); |
| + } |
| + } else { |
| + audio_bus->FromInterleavedPartial( |
| + av_frame->data[0], current_frame, frames_read, bytes_per_sample); |
| + } |
| current_frame += frames_read; |
| } while (packet_temp.size > 0); |