Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / filters / audio_file_reader.cc
blob70b60d757ef381e4fd8e1b779879c8df41ac8d76
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
3 // found in the LICENSE file.
5 #include "media/filters/audio_file_reader.h"
7 #include <cmath>
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "media/base/audio_bus.h"
12 #include "media/ffmpeg/ffmpeg_common.h"
14 namespace media {
16 AudioFileReader::AudioFileReader(FFmpegURLProtocol* protocol)
17 : codec_context_(NULL),
18 stream_index_(0),
19 protocol_(protocol),
20 channels_(0),
21 sample_rate_(0),
22 av_sample_format_(0) {
25 AudioFileReader::~AudioFileReader() {
26 Close();
29 bool AudioFileReader::Open() {
30 if (!OpenDemuxer())
31 return false;
32 return OpenDecoder();
35 bool AudioFileReader::OpenDemuxer() {
36 glue_.reset(new FFmpegGlue(protocol_));
37 AVFormatContext* format_context = glue_->format_context();
39 // Open FFmpeg AVFormatContext.
40 if (!glue_->OpenContext()) {
41 DLOG(WARNING) << "AudioFileReader::Open() : error in avformat_open_input()";
42 return false;
45 // Get the codec context.
46 codec_context_ = NULL;
47 for (size_t i = 0; i < format_context->nb_streams; ++i) {
48 AVCodecContext* c = format_context->streams[i]->codec;
49 if (c->codec_type == AVMEDIA_TYPE_AUDIO) {
50 codec_context_ = c;
51 stream_index_ = i;
52 break;
56 // Get the codec.
57 if (!codec_context_)
58 return false;
60 const int result = avformat_find_stream_info(format_context, NULL);
61 if (result < 0) {
62 DLOG(WARNING)
63 << "AudioFileReader::Open() : error in avformat_find_stream_info()";
64 return false;
67 return true;
70 bool AudioFileReader::OpenDecoder() {
71 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
72 if (codec) {
73 // MP3 decodes to S16P which we don't support, tell it to use S16 instead.
74 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P)
75 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16;
77 const int result = avcodec_open2(codec_context_, codec, NULL);
78 if (result < 0) {
79 DLOG(WARNING) << "AudioFileReader::Open() : could not open codec -"
80 << " result: " << result;
81 return false;
84 // Ensure avcodec_open2() respected our format request.
85 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) {
86 DLOG(ERROR) << "AudioFileReader::Open() : unable to configure a"
87 << " supported sample format - "
88 << codec_context_->sample_fmt;
89 return false;
91 } else {
92 DLOG(WARNING) << "AudioFileReader::Open() : could not find codec.";
93 return false;
96 // Verify the channel layout is supported by Chrome. Acts as a sanity check
97 // against invalid files. See http://crbug.com/171962
98 if (ChannelLayoutToChromeChannelLayout(
99 codec_context_->channel_layout, codec_context_->channels) ==
100 CHANNEL_LAYOUT_UNSUPPORTED) {
101 return false;
104 // Store initial values to guard against midstream configuration changes.
105 channels_ = codec_context_->channels;
106 sample_rate_ = codec_context_->sample_rate;
107 av_sample_format_ = codec_context_->sample_fmt;
108 return true;
111 void AudioFileReader::Close() {
112 // |codec_context_| is a stream inside glue_->format_context(), so it is
113 // closed when |glue_| is disposed.
114 glue_.reset();
115 codec_context_ = NULL;
118 int AudioFileReader::Read(AudioBus* audio_bus) {
119 DCHECK(glue_.get() && codec_context_) <<
120 "AudioFileReader::Read() : reader is not opened!";
122 DCHECK_EQ(audio_bus->channels(), channels());
123 if (audio_bus->channels() != channels())
124 return 0;
126 size_t bytes_per_sample = av_get_bytes_per_sample(codec_context_->sample_fmt);
128 // Holds decoded audio.
129 scoped_ptr<AVFrame, ScopedPtrAVFreeFrame> av_frame(av_frame_alloc());
131 // Read until we hit EOF or we've read the requested number of frames.
132 AVPacket packet;
133 int current_frame = 0;
134 bool continue_decoding = true;
136 while (current_frame < audio_bus->frames() && continue_decoding &&
137 ReadPacket(&packet)) {
138 // Make a shallow copy of packet so we can slide packet.data as frames are
139 // decoded from the packet; otherwise av_free_packet() will corrupt memory.
140 AVPacket packet_temp = packet;
141 do {
142 // Reset frame to default values.
143 av_frame_unref(av_frame.get());
145 int frame_decoded = 0;
146 int result = avcodec_decode_audio4(
147 codec_context_, av_frame.get(), &frame_decoded, &packet_temp);
149 if (result < 0) {
150 DLOG(WARNING)
151 << "AudioFileReader::Read() : error in avcodec_decode_audio4() -"
152 << result;
153 break;
156 // Update packet size and data pointer in case we need to call the decoder
157 // with the remaining bytes from this packet.
158 packet_temp.size -= result;
159 packet_temp.data += result;
161 if (!frame_decoded)
162 continue;
164 // Determine the number of sample-frames we just decoded. Check overflow.
165 int frames_read = av_frame->nb_samples;
166 if (frames_read < 0) {
167 continue_decoding = false;
168 break;
171 #ifdef CHROMIUM_NO_AVFRAME_CHANNELS
172 int channels = av_get_channel_layout_nb_channels(
173 av_frame->channel_layout);
174 #else
175 int channels = av_frame->channels;
176 #endif
177 if (av_frame->sample_rate != sample_rate_ ||
178 channels != channels_ ||
179 av_frame->format != av_sample_format_) {
180 DLOG(ERROR) << "Unsupported midstream configuration change!"
181 << " Sample Rate: " << av_frame->sample_rate << " vs "
182 << sample_rate_
183 << ", Channels: " << channels << " vs "
184 << channels_
185 << ", Sample Format: " << av_frame->format << " vs "
186 << av_sample_format_;
188 // This is an unrecoverable error, so bail out.
189 continue_decoding = false;
190 break;
193 // Truncate, if necessary, if the destination isn't big enough.
194 if (current_frame + frames_read > audio_bus->frames()) {
195 DLOG(ERROR) << "Truncating decoded data due to output size.";
196 frames_read = audio_bus->frames() - current_frame;
199 // Deinterleave each channel and convert to 32bit floating-point with
200 // nominal range -1.0 -> +1.0. If the output is already in float planar
201 // format, just copy it into the AudioBus.
202 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) {
203 float* decoded_audio_data = reinterpret_cast<float*>(av_frame->data[0]);
204 int channels = audio_bus->channels();
205 for (int ch = 0; ch < channels; ++ch) {
206 float* bus_data = audio_bus->channel(ch) + current_frame;
207 for (int i = 0, offset = ch; i < frames_read;
208 ++i, offset += channels) {
209 bus_data[i] = decoded_audio_data[offset];
212 } else if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP) {
213 for (int ch = 0; ch < audio_bus->channels(); ++ch) {
214 memcpy(audio_bus->channel(ch) + current_frame,
215 av_frame->extended_data[ch], sizeof(float) * frames_read);
217 } else {
218 audio_bus->FromInterleavedPartial(
219 av_frame->data[0], current_frame, frames_read, bytes_per_sample);
222 current_frame += frames_read;
223 } while (packet_temp.size > 0);
224 av_free_packet(&packet);
227 // Zero any remaining frames.
228 audio_bus->ZeroFramesPartial(
229 current_frame, audio_bus->frames() - current_frame);
231 // Returns the actual number of sample-frames decoded.
232 // Ideally this represents the "true" exact length of the file.
233 return current_frame;
236 base::TimeDelta AudioFileReader::GetDuration() const {
237 const AVRational av_time_base = {1, AV_TIME_BASE};
239 // Add one microsecond to avoid rounding-down errors which can occur when
240 // |duration| has been calculated from an exact number of sample-frames.
241 // One microsecond is much less than the time of a single sample-frame
242 // at any real-world sample-rate.
243 return ConvertFromTimeBase(av_time_base,
244 glue_->format_context()->duration + 1);
247 int AudioFileReader::GetNumberOfFrames() const {
248 return static_cast<int>(ceil(GetDuration().InSecondsF() * sample_rate()));
251 bool AudioFileReader::OpenDemuxerForTesting() {
252 return OpenDemuxer();
255 bool AudioFileReader::ReadPacketForTesting(AVPacket* output_packet) {
256 return ReadPacket(output_packet);
259 bool AudioFileReader::ReadPacket(AVPacket* output_packet) {
260 while (av_read_frame(glue_->format_context(), output_packet) >= 0 &&
261 av_dup_packet(output_packet) >= 0) {
262 // Skip packets from other streams.
263 if (output_packet->stream_index != stream_index_) {
264 av_free_packet(output_packet);
265 continue;
267 return true;
269 return false;
272 bool AudioFileReader::SeekForTesting(base::TimeDelta seek_time) {
273 return av_seek_frame(glue_->format_context(),
274 stream_index_,
275 ConvertToTimeBase(codec_context_->time_base, seek_time),
276 AVSEEK_FLAG_BACKWARD) >= 0;
279 const AVStream* AudioFileReader::GetAVStreamForTesting() const {
280 return glue_->format_context()->streams[stream_index_];
283 } // namespace media