| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/filters/audio_file_reader.h" | 5 #include "media/filters/audio_file_reader.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "media/base/audio_bus.h" | 9 #include "media/base/audio_bus.h" |
| 10 #include "media/ffmpeg/ffmpeg_common.h" | 10 #include "media/ffmpeg/ffmpeg_common.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 72 |
| 73 int result = avformat_find_stream_info(format_context, NULL); | 73 int result = avformat_find_stream_info(format_context, NULL); |
| 74 if (result < 0) { | 74 if (result < 0) { |
| 75 DLOG(WARNING) | 75 DLOG(WARNING) |
| 76 << "AudioFileReader::Open() : error in avformat_find_stream_info()"; | 76 << "AudioFileReader::Open() : error in avformat_find_stream_info()"; |
| 77 return false; | 77 return false; |
| 78 } | 78 } |
| 79 | 79 |
| 80 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 80 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
| 81 if (codec) { | 81 if (codec) { |
| 82 // MP3 decodes to S16P which we don't support, tell it to use S16 instead. |
| 83 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) |
| 84 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16; |
| 85 |
| 82 if ((result = avcodec_open2(codec_context_, codec, NULL)) < 0) { | 86 if ((result = avcodec_open2(codec_context_, codec, NULL)) < 0) { |
| 83 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" | 87 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" |
| 84 << " result: " << result; | 88 << " result: " << result; |
| 89 return false; |
| 90 } |
| 91 |
| 92 // Ensure avcodec_open2() respected our format request. |
| 93 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) { |
| 94 DLOG(ERROR) << "AudioFileReader::Open() : unable to configure a" |
| 95 << " supported sample format - " |
| 96 << codec_context_->sample_fmt; |
| 85 return false; | 97 return false; |
| 86 } | 98 } |
| 87 } else { | 99 } else { |
| 88 DLOG(WARNING) << "AudioFileReader::Open() : could not find codec -" | 100 DLOG(WARNING) << "AudioFileReader::Open() : could not find codec -" |
| 89 << " result: " << result; | 101 << " result: " << result; |
| 90 return false; | 102 return false; |
| 91 } | 103 } |
| 92 | 104 |
| 93 return true; | 105 return true; |
| 94 } | 106 } |
| 95 | 107 |
| 96 void AudioFileReader::Close() { | 108 void AudioFileReader::Close() { |
| 97 if (codec_context_) { | 109 if (codec_context_) { |
| 98 avcodec_close(codec_context_); | 110 avcodec_close(codec_context_); |
| 99 codec_context_ = NULL; | 111 codec_context_ = NULL; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 int frames_read = av_frame->nb_samples; | 168 int frames_read = av_frame->nb_samples; |
| 157 if (frames_read < 0) { | 169 if (frames_read < 0) { |
| 158 continue_decoding = false; | 170 continue_decoding = false; |
| 159 break; | 171 break; |
| 160 } | 172 } |
| 161 | 173 |
| 162 // Truncate, if necessary, if the destination isn't big enough. | 174 // Truncate, if necessary, if the destination isn't big enough. |
| 163 if (current_frame + frames_read > audio_bus->frames()) | 175 if (current_frame + frames_read > audio_bus->frames()) |
| 164 frames_read = audio_bus->frames() - current_frame; | 176 frames_read = audio_bus->frames() - current_frame; |
| 165 | 177 |
| 166 // Deinterleave each channel and convert to 32bit floating-point | 178 // Deinterleave each channel and convert to 32bit floating-point with |
| 167 // with nominal range -1.0 -> +1.0. | 179 // nominal range -1.0 -> +1.0. If the output is already in float planar |
| 168 audio_bus->FromInterleavedPartial( | 180 // format, just copy it into the AudioBus. |
| 169 av_frame->data[0], current_frame, frames_read, bytes_per_sample); | 181 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { |
| 182 float* decoded_audio_data = reinterpret_cast<float*>(av_frame->data[0]); |
| 183 int channels = audio_bus->channels(); |
| 184 for (int ch = 0; ch < channels; ++ch) { |
| 185 float* bus_data = audio_bus->channel(ch) + current_frame; |
| 186 for (int i = 0, offset = ch; i < frames_read; |
| 187 ++i, offset += channels) { |
| 188 bus_data[i] = decoded_audio_data[offset]; |
| 189 } |
| 190 } |
| 191 } else if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP) { |
| 192 for (int ch = 0; ch < audio_bus->channels(); ++ch) { |
| 193 memcpy(audio_bus->channel(ch) + current_frame, |
| 194 av_frame->extended_data[ch], sizeof(float) * frames_read); |
| 195 } |
| 196 } else { |
| 197 audio_bus->FromInterleavedPartial( |
| 198 av_frame->data[0], current_frame, frames_read, bytes_per_sample); |
| 199 } |
| 170 | 200 |
| 171 current_frame += frames_read; | 201 current_frame += frames_read; |
| 172 } while (packet_temp.size > 0); | 202 } while (packet_temp.size > 0); |
| 173 av_free_packet(&packet); | 203 av_free_packet(&packet); |
| 174 } | 204 } |
| 175 | 205 |
| 176 // Zero any remaining frames. | 206 // Zero any remaining frames. |
| 177 audio_bus->ZeroFramesPartial( | 207 audio_bus->ZeroFramesPartial( |
| 178 current_frame, audio_bus->frames() - current_frame); | 208 current_frame, audio_bus->frames() - current_frame); |
| 179 | 209 |
| 180 // Returns the actual number of sample-frames decoded. | 210 // Returns the actual number of sample-frames decoded. |
| 181 // Ideally this represents the "true" exact length of the file. | 211 // Ideally this represents the "true" exact length of the file. |
| 182 return current_frame; | 212 return current_frame; |
| 183 } | 213 } |
| 184 | 214 |
| 185 } // namespace media | 215 } // namespace media |
| OLD | NEW |