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 <cmath> | |
8 | |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/time/time.h" | 10 #include "base/time/time.h" |
9 #include "media/base/audio_bus.h" | 11 #include "media/base/audio_bus.h" |
10 #include "media/ffmpeg/ffmpeg_common.h" | 12 #include "media/ffmpeg/ffmpeg_common.h" |
11 #include "media/filters/ffmpeg_glue.h" | 13 #include "media/filters/ffmpeg_glue.h" |
12 | 14 |
13 namespace media { | 15 namespace media { |
14 | 16 |
15 AudioFileReader::AudioFileReader(FFmpegURLProtocol* protocol) | 17 AudioFileReader::AudioFileReader(FFmpegURLProtocol* protocol) |
16 : codec_context_(NULL), | 18 : codec_context_(NULL), |
17 stream_index_(0), | 19 stream_index_(0), |
18 protocol_(protocol), | 20 protocol_(protocol), |
19 channels_(0), | 21 channels_(0), |
20 sample_rate_(0), | 22 sample_rate_(0), |
21 av_sample_format_(0) { | 23 av_sample_format_(0) { |
22 } | 24 } |
23 | 25 |
24 AudioFileReader::~AudioFileReader() { | 26 AudioFileReader::~AudioFileReader() { |
25 Close(); | 27 Close(); |
26 } | 28 } |
27 | 29 |
28 base::TimeDelta AudioFileReader::duration() const { | |
29 const AVRational av_time_base = {1, AV_TIME_BASE}; | |
30 | |
31 // Add one microsecond to avoid rounding-down errors which can occur when | |
32 // |duration| has been calculated from an exact number of sample-frames. | |
33 // One microsecond is much less than the time of a single sample-frame | |
34 // at any real-world sample-rate. | |
35 return ConvertFromTimeBase( | |
36 av_time_base, glue_->format_context()->duration + 1); | |
37 } | |
38 | |
39 int64 AudioFileReader::number_of_frames() const { | |
40 return static_cast<int64>(duration().InSecondsF() * sample_rate()); | |
41 } | |
42 | |
43 bool AudioFileReader::Open() { | 30 bool AudioFileReader::Open() { |
44 glue_.reset(new FFmpegGlue(protocol_)); | 31 glue_.reset(new FFmpegGlue(protocol_)); |
45 AVFormatContext* format_context = glue_->format_context(); | 32 AVFormatContext* format_context = glue_->format_context(); |
46 | 33 |
47 // Open FFmpeg AVFormatContext. | 34 // Open FFmpeg AVFormatContext. |
48 if (!glue_->OpenContext()) { | 35 if (!glue_->OpenContext()) { |
49 DLOG(WARNING) << "AudioFileReader::Open() : error in avformat_open_input()"; | 36 DLOG(WARNING) << "AudioFileReader::Open() : error in avformat_open_input()"; |
50 return false; | 37 return false; |
51 } | 38 } |
52 | 39 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 << channels_ | 182 << channels_ |
196 << ", Sample Format: " << av_frame->format << " vs " | 183 << ", Sample Format: " << av_frame->format << " vs " |
197 << av_sample_format_; | 184 << av_sample_format_; |
198 | 185 |
199 // This is an unrecoverable error, so bail out. | 186 // This is an unrecoverable error, so bail out. |
200 continue_decoding = false; | 187 continue_decoding = false; |
201 break; | 188 break; |
202 } | 189 } |
203 | 190 |
204 // Truncate, if necessary, if the destination isn't big enough. | 191 // Truncate, if necessary, if the destination isn't big enough. |
205 if (current_frame + frames_read > audio_bus->frames()) | 192 if (current_frame + frames_read > audio_bus->frames()) { |
193 DLOG(ERROR) << "Truncating decoded data due to output size."; | |
206 frames_read = audio_bus->frames() - current_frame; | 194 frames_read = audio_bus->frames() - current_frame; |
195 } | |
207 | 196 |
208 // Deinterleave each channel and convert to 32bit floating-point with | 197 // Deinterleave each channel and convert to 32bit floating-point with |
209 // nominal range -1.0 -> +1.0. If the output is already in float planar | 198 // nominal range -1.0 -> +1.0. If the output is already in float planar |
210 // format, just copy it into the AudioBus. | 199 // format, just copy it into the AudioBus. |
211 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { | 200 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { |
212 float* decoded_audio_data = reinterpret_cast<float*>(av_frame->data[0]); | 201 float* decoded_audio_data = reinterpret_cast<float*>(av_frame->data[0]); |
213 int channels = audio_bus->channels(); | 202 int channels = audio_bus->channels(); |
214 for (int ch = 0; ch < channels; ++ch) { | 203 for (int ch = 0; ch < channels; ++ch) { |
215 float* bus_data = audio_bus->channel(ch) + current_frame; | 204 float* bus_data = audio_bus->channel(ch) + current_frame; |
216 for (int i = 0, offset = ch; i < frames_read; | 205 for (int i = 0, offset = ch; i < frames_read; |
(...skipping 18 matching lines...) Expand all Loading... | |
235 | 224 |
236 // Zero any remaining frames. | 225 // Zero any remaining frames. |
237 audio_bus->ZeroFramesPartial( | 226 audio_bus->ZeroFramesPartial( |
238 current_frame, audio_bus->frames() - current_frame); | 227 current_frame, audio_bus->frames() - current_frame); |
239 | 228 |
240 // Returns the actual number of sample-frames decoded. | 229 // Returns the actual number of sample-frames decoded. |
241 // Ideally this represents the "true" exact length of the file. | 230 // Ideally this represents the "true" exact length of the file. |
242 return current_frame; | 231 return current_frame; |
243 } | 232 } |
244 | 233 |
234 base::TimeDelta AudioFileReader::GetDuration() const { | |
Raymond Toy (Google)
2014/03/05 18:28:00
Why move the code around?
DaleCurtis
2014/03/05 19:27:35
http://google-styleguide.googlecode.com/svn/trunk/
| |
235 const AVRational av_time_base = {1, AV_TIME_BASE}; | |
236 | |
237 // Add one microsecond to avoid rounding-down errors which can occur when | |
238 // |duration| has been calculated from an exact number of sample-frames. | |
239 // One microsecond is much less than the time of a single sample-frame | |
240 // at any real-world sample-rate. | |
241 return ConvertFromTimeBase(av_time_base, | |
242 glue_->format_context()->duration + 1); | |
243 } | |
244 | |
245 int AudioFileReader::GetNumberOfFrames() const { | |
246 return static_cast<int>(ceil(GetDuration().InSecondsF() * sample_rate())); | |
247 } | |
248 | |
245 } // namespace media | 249 } // namespace media |
OLD | NEW |