| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/numerics/safe_math.h" | 12 #include "base/numerics/safe_math.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "media/base/audio_bus.h" | 14 #include "media/base/audio_bus.h" |
| 15 #include "media/ffmpeg/ffmpeg_common.h" | 15 #include "media/ffmpeg/ffmpeg_common.h" |
| 16 | 16 |
| 17 namespace media { | 17 namespace media { |
| 18 | 18 |
| 19 // AAC(M4A) decoding specific constants. | 19 // AAC(M4A) decoding specific constants. |
| 20 static const int kAACPrimingFrameCount = 2112; | 20 static const int kAACPrimingFrameCount = 2112; |
| 21 static const int kAACRemainderFrameCount = 519; | 21 static const int kAACRemainderFrameCount = 519; |
| 22 | 22 |
| 23 AudioFileReader::AudioFileReader(FFmpegURLProtocol* protocol) | 23 AudioFileReader::AudioFileReader(FFmpegURLProtocol* protocol) |
| 24 : codec_context_(NULL), | 24 : stream_index_(0), |
| 25 stream_index_(0), | |
| 26 protocol_(protocol), | 25 protocol_(protocol), |
| 27 audio_codec_(kUnknownAudioCodec), | 26 audio_codec_(kUnknownAudioCodec), |
| 28 channels_(0), | 27 channels_(0), |
| 29 sample_rate_(0), | 28 sample_rate_(0), |
| 30 av_sample_format_(0) {} | 29 av_sample_format_(0) {} |
| 31 | 30 |
| 32 AudioFileReader::~AudioFileReader() { | 31 AudioFileReader::~AudioFileReader() { |
| 33 Close(); | 32 Close(); |
| 34 } | 33 } |
| 35 | 34 |
| 36 bool AudioFileReader::Open() { | 35 bool AudioFileReader::Open() { |
| 37 if (!OpenDemuxer()) | 36 if (!OpenDemuxer()) |
| 38 return false; | 37 return false; |
| 39 return OpenDecoder(); | 38 return OpenDecoder(); |
| 40 } | 39 } |
| 41 | 40 |
| 42 bool AudioFileReader::OpenDemuxer() { | 41 bool AudioFileReader::OpenDemuxer() { |
| 43 glue_.reset(new FFmpegGlue(protocol_)); | 42 glue_.reset(new FFmpegGlue(protocol_)); |
| 44 AVFormatContext* format_context = glue_->format_context(); | 43 AVFormatContext* format_context = glue_->format_context(); |
| 45 | 44 |
| 46 // Open FFmpeg AVFormatContext. | 45 // Open FFmpeg AVFormatContext. |
| 47 if (!glue_->OpenContext()) { | 46 if (!glue_->OpenContext()) { |
| 48 DLOG(WARNING) << "AudioFileReader::Open() : error in avformat_open_input()"; | 47 DLOG(WARNING) << "AudioFileReader::Open() : error in avformat_open_input()"; |
| 49 return false; | 48 return false; |
| 50 } | 49 } |
| 51 | 50 |
| 52 // Get the codec context. | 51 // Find the first audio stream, if any. |
| 53 codec_context_ = NULL; | 52 codec_context_.reset(); |
| 53 bool found_stream = false; |
| 54 for (size_t i = 0; i < format_context->nb_streams; ++i) { | 54 for (size_t i = 0; i < format_context->nb_streams; ++i) { |
| 55 AVCodecContext* c = format_context->streams[i]->codec; | 55 if (format_context->streams[i]->codecpar->codec_type == |
| 56 if (c->codec_type == AVMEDIA_TYPE_AUDIO) { | 56 AVMEDIA_TYPE_AUDIO) { |
| 57 codec_context_ = c; | |
| 58 stream_index_ = i; | 57 stream_index_ = i; |
| 58 found_stream = true; |
| 59 break; | 59 break; |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Get the codec. | 63 if (!found_stream) |
| 64 if (!codec_context_) | |
| 65 return false; | 64 return false; |
| 66 | 65 |
| 67 const int result = avformat_find_stream_info(format_context, NULL); | 66 const int result = avformat_find_stream_info(format_context, NULL); |
| 68 if (result < 0) { | 67 if (result < 0) { |
| 69 DLOG(WARNING) | 68 DLOG(WARNING) |
| 70 << "AudioFileReader::Open() : error in avformat_find_stream_info()"; | 69 << "AudioFileReader::Open() : error in avformat_find_stream_info()"; |
| 71 return false; | 70 return false; |
| 72 } | 71 } |
| 73 | 72 |
| 73 // Get the codec context. |
| 74 codec_context_ = |
| 75 AVStreamToAVCodecContext(format_context->streams[stream_index_]); |
| 76 if (!codec_context_) |
| 77 return false; |
| 78 |
| 79 DCHECK_EQ(codec_context_->codec_type, AVMEDIA_TYPE_AUDIO); |
| 74 return true; | 80 return true; |
| 75 } | 81 } |
| 76 | 82 |
| 77 bool AudioFileReader::OpenDecoder() { | 83 bool AudioFileReader::OpenDecoder() { |
| 78 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 84 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
| 79 if (codec) { | 85 if (codec) { |
| 80 // MP3 decodes to S16P which we don't support, tell it to use S16 instead. | 86 // MP3 decodes to S16P which we don't support, tell it to use S16 instead. |
| 81 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) | 87 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) |
| 82 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16; | 88 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16; |
| 83 | 89 |
| 84 const int result = avcodec_open2(codec_context_, codec, NULL); | 90 const int result = avcodec_open2(codec_context_.get(), codec, nullptr); |
| 85 if (result < 0) { | 91 if (result < 0) { |
| 86 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" | 92 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" |
| 87 << " result: " << result; | 93 << " result: " << result; |
| 88 return false; | 94 return false; |
| 89 } | 95 } |
| 90 | 96 |
| 91 // Ensure avcodec_open2() respected our format request. | 97 // Ensure avcodec_open2() respected our format request. |
| 92 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) { | 98 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) { |
| 93 DLOG(ERROR) << "AudioFileReader::Open() : unable to configure a" | 99 DLOG(ERROR) << "AudioFileReader::Open() : unable to configure a" |
| 94 << " supported sample format - " | 100 << " supported sample format - " |
| (...skipping 15 matching lines...) Expand all Loading... |
| 110 | 116 |
| 111 // Store initial values to guard against midstream configuration changes. | 117 // Store initial values to guard against midstream configuration changes. |
| 112 channels_ = codec_context_->channels; | 118 channels_ = codec_context_->channels; |
| 113 audio_codec_ = CodecIDToAudioCodec(codec_context_->codec_id); | 119 audio_codec_ = CodecIDToAudioCodec(codec_context_->codec_id); |
| 114 sample_rate_ = codec_context_->sample_rate; | 120 sample_rate_ = codec_context_->sample_rate; |
| 115 av_sample_format_ = codec_context_->sample_fmt; | 121 av_sample_format_ = codec_context_->sample_fmt; |
| 116 return true; | 122 return true; |
| 117 } | 123 } |
| 118 | 124 |
| 119 void AudioFileReader::Close() { | 125 void AudioFileReader::Close() { |
| 120 // |codec_context_| is a stream inside glue_->format_context(), so it is | 126 codec_context_.reset(); |
| 121 // closed when |glue_| is disposed. | |
| 122 glue_.reset(); | 127 glue_.reset(); |
| 123 codec_context_ = NULL; | |
| 124 } | 128 } |
| 125 | 129 |
| 126 int AudioFileReader::Read(AudioBus* audio_bus) { | 130 int AudioFileReader::Read(AudioBus* audio_bus) { |
| 127 DCHECK(glue_.get() && codec_context_) << | 131 DCHECK(glue_.get() && codec_context_) << |
| 128 "AudioFileReader::Read() : reader is not opened!"; | 132 "AudioFileReader::Read() : reader is not opened!"; |
| 129 | 133 |
| 130 DCHECK_EQ(audio_bus->channels(), channels()); | 134 DCHECK_EQ(audio_bus->channels(), channels()); |
| 131 if (audio_bus->channels() != channels()) | 135 if (audio_bus->channels() != channels()) |
| 132 return 0; | 136 return 0; |
| 133 | 137 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 144 while (current_frame < audio_bus->frames() && continue_decoding && | 148 while (current_frame < audio_bus->frames() && continue_decoding && |
| 145 ReadPacket(&packet)) { | 149 ReadPacket(&packet)) { |
| 146 // Make a shallow copy of packet so we can slide packet.data as frames are | 150 // Make a shallow copy of packet so we can slide packet.data as frames are |
| 147 // decoded from the packet; otherwise av_packet_unref() will corrupt memory. | 151 // decoded from the packet; otherwise av_packet_unref() will corrupt memory. |
| 148 AVPacket packet_temp = packet; | 152 AVPacket packet_temp = packet; |
| 149 do { | 153 do { |
| 150 // Reset frame to default values. | 154 // Reset frame to default values. |
| 151 av_frame_unref(av_frame.get()); | 155 av_frame_unref(av_frame.get()); |
| 152 | 156 |
| 153 int frame_decoded = 0; | 157 int frame_decoded = 0; |
| 154 int result = avcodec_decode_audio4( | 158 int result = avcodec_decode_audio4(codec_context_.get(), av_frame.get(), |
| 155 codec_context_, av_frame.get(), &frame_decoded, &packet_temp); | 159 &frame_decoded, &packet_temp); |
| 156 | 160 |
| 157 if (result < 0) { | 161 if (result < 0) { |
| 158 DLOG(WARNING) | 162 DLOG(WARNING) |
| 159 << "AudioFileReader::Read() : error in avcodec_decode_audio4() -" | 163 << "AudioFileReader::Read() : error in avcodec_decode_audio4() -" |
| 160 << result; | 164 << result; |
| 161 break; | 165 break; |
| 162 } | 166 } |
| 163 | 167 |
| 164 // Update packet size and data pointer in case we need to call the decoder | 168 // Update packet size and data pointer in case we need to call the decoder |
| 165 // with the remaining bytes from this packet. | 169 // with the remaining bytes from this packet. |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 if (output_packet->stream_index != stream_index_) { | 292 if (output_packet->stream_index != stream_index_) { |
| 289 av_packet_unref(output_packet); | 293 av_packet_unref(output_packet); |
| 290 continue; | 294 continue; |
| 291 } | 295 } |
| 292 return true; | 296 return true; |
| 293 } | 297 } |
| 294 return false; | 298 return false; |
| 295 } | 299 } |
| 296 | 300 |
| 297 bool AudioFileReader::SeekForTesting(base::TimeDelta seek_time) { | 301 bool AudioFileReader::SeekForTesting(base::TimeDelta seek_time) { |
| 298 return av_seek_frame(glue_->format_context(), | 302 // Use the AVStream's time_base, since |codec_context_| does not have |
| 299 stream_index_, | 303 // time_base populated until after OpenDecoder(). |
| 300 ConvertToTimeBase(codec_context_->time_base, seek_time), | 304 return av_seek_frame( |
| 301 AVSEEK_FLAG_BACKWARD) >= 0; | 305 glue_->format_context(), stream_index_, |
| 306 ConvertToTimeBase(GetAVStreamForTesting()->time_base, seek_time), |
| 307 AVSEEK_FLAG_BACKWARD) >= 0; |
| 302 } | 308 } |
| 303 | 309 |
| 304 const AVStream* AudioFileReader::GetAVStreamForTesting() const { | 310 const AVStream* AudioFileReader::GetAVStreamForTesting() const { |
| 305 return glue_->format_context()->streams[stream_index_]; | 311 return glue_->format_context()->streams[stream_index_]; |
| 306 } | 312 } |
| 307 | 313 |
| 308 } // namespace media | 314 } // namespace media |
| OLD | NEW |