Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: media/filters/audio_file_reader.cc

Issue 2497603003: Roll src/third_party/ffmpeg/ 3c7a09882..cdf4accee (3188 commits). (Closed)
Patch Set: Rebase (liberato@'s pipeline_integration_test_base conflicted; using liberato@'s now here) Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_.reset(avcodec_alloc_context3(NULL));
DaleCurtis 2016/11/14 17:58:21 Ditto on helper function.
wolenetz 2016/11/15 02:15:35 Done.
75 AVCodecParameters* cp = format_context->streams[stream_index_]->codecpar;
76 if (!codec_context_ ||
77 avcodec_parameters_to_context(codec_context_.get(), cp) < 0) {
78 return false;
79 }
80
81 DCHECK_EQ(codec_context_->codec_type, AVMEDIA_TYPE_AUDIO);
74 return true; 82 return true;
75 } 83 }
76 84
77 bool AudioFileReader::OpenDecoder() { 85 bool AudioFileReader::OpenDecoder() {
78 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); 86 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
79 if (codec) { 87 if (codec) {
80 // MP3 decodes to S16P which we don't support, tell it to use S16 instead. 88 // 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) 89 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P)
82 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16; 90 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16;
83 91
84 const int result = avcodec_open2(codec_context_, codec, NULL); 92 const int result = avcodec_open2(codec_context_.get(), codec, NULL);
85 if (result < 0) { 93 if (result < 0) {
86 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -" 94 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -"
87 << " result: " << result; 95 << " result: " << result;
88 return false; 96 return false;
89 } 97 }
90 98
91 // Ensure avcodec_open2() respected our format request. 99 // Ensure avcodec_open2() respected our format request.
92 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) { 100 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) {
93 DLOG(ERROR) << "AudioFileReader::Open() : unable to configure a" 101 DLOG(ERROR) << "AudioFileReader::Open() : unable to configure a"
94 << " supported sample format - " 102 << " supported sample format - "
(...skipping 15 matching lines...) Expand all
110 118
111 // Store initial values to guard against midstream configuration changes. 119 // Store initial values to guard against midstream configuration changes.
112 channels_ = codec_context_->channels; 120 channels_ = codec_context_->channels;
113 audio_codec_ = CodecIDToAudioCodec(codec_context_->codec_id); 121 audio_codec_ = CodecIDToAudioCodec(codec_context_->codec_id);
114 sample_rate_ = codec_context_->sample_rate; 122 sample_rate_ = codec_context_->sample_rate;
115 av_sample_format_ = codec_context_->sample_fmt; 123 av_sample_format_ = codec_context_->sample_fmt;
116 return true; 124 return true;
117 } 125 }
118 126
119 void AudioFileReader::Close() { 127 void AudioFileReader::Close() {
120 // |codec_context_| is a stream inside glue_->format_context(), so it is 128 codec_context_.reset();
121 // closed when |glue_| is disposed.
122 glue_.reset(); 129 glue_.reset();
123 codec_context_ = NULL;
124 } 130 }
125 131
126 int AudioFileReader::Read(AudioBus* audio_bus) { 132 int AudioFileReader::Read(AudioBus* audio_bus) {
127 DCHECK(glue_.get() && codec_context_) << 133 DCHECK(glue_.get() && codec_context_) <<
128 "AudioFileReader::Read() : reader is not opened!"; 134 "AudioFileReader::Read() : reader is not opened!";
129 135
130 DCHECK_EQ(audio_bus->channels(), channels()); 136 DCHECK_EQ(audio_bus->channels(), channels());
131 if (audio_bus->channels() != channels()) 137 if (audio_bus->channels() != channels())
132 return 0; 138 return 0;
133 139
(...skipping 10 matching lines...) Expand all
144 while (current_frame < audio_bus->frames() && continue_decoding && 150 while (current_frame < audio_bus->frames() && continue_decoding &&
145 ReadPacket(&packet)) { 151 ReadPacket(&packet)) {
146 // Make a shallow copy of packet so we can slide packet.data as frames are 152 // 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. 153 // decoded from the packet; otherwise av_packet_unref() will corrupt memory.
148 AVPacket packet_temp = packet; 154 AVPacket packet_temp = packet;
149 do { 155 do {
150 // Reset frame to default values. 156 // Reset frame to default values.
151 av_frame_unref(av_frame.get()); 157 av_frame_unref(av_frame.get());
152 158
153 int frame_decoded = 0; 159 int frame_decoded = 0;
154 int result = avcodec_decode_audio4( 160 int result = avcodec_decode_audio4(codec_context_.get(), av_frame.get(),
155 codec_context_, av_frame.get(), &frame_decoded, &packet_temp); 161 &frame_decoded, &packet_temp);
156 162
157 if (result < 0) { 163 if (result < 0) {
158 DLOG(WARNING) 164 DLOG(WARNING)
159 << "AudioFileReader::Read() : error in avcodec_decode_audio4() -" 165 << "AudioFileReader::Read() : error in avcodec_decode_audio4() -"
160 << result; 166 << result;
161 break; 167 break;
162 } 168 }
163 169
164 // Update packet size and data pointer in case we need to call the decoder 170 // Update packet size and data pointer in case we need to call the decoder
165 // with the remaining bytes from this packet. 171 // with the remaining bytes from this packet.
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 if (output_packet->stream_index != stream_index_) { 294 if (output_packet->stream_index != stream_index_) {
289 av_packet_unref(output_packet); 295 av_packet_unref(output_packet);
290 continue; 296 continue;
291 } 297 }
292 return true; 298 return true;
293 } 299 }
294 return false; 300 return false;
295 } 301 }
296 302
297 bool AudioFileReader::SeekForTesting(base::TimeDelta seek_time) { 303 bool AudioFileReader::SeekForTesting(base::TimeDelta seek_time) {
298 return av_seek_frame(glue_->format_context(), 304 // Use the AVStream's time_base, since |codec_context_| does not have
299 stream_index_, 305 // time_base populated until after OpenDecoder().
300 ConvertToTimeBase(codec_context_->time_base, seek_time), 306 return av_seek_frame(
301 AVSEEK_FLAG_BACKWARD) >= 0; 307 glue_->format_context(), stream_index_,
308 ConvertToTimeBase(GetAVStreamForTesting()->time_base, seek_time),
309 AVSEEK_FLAG_BACKWARD) >= 0;
302 } 310 }
303 311
304 const AVStream* AudioFileReader::GetAVStreamForTesting() const { 312 const AVStream* AudioFileReader::GetAVStreamForTesting() const {
305 return glue_->format_context()->streams[stream_index_]; 313 return glue_->format_context()->streams[stream_index_];
306 } 314 }
307 315
308 } // namespace media 316 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698