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/ffmpeg_audio_decoder.h" | 5 #include "media/filters/ffmpeg_audio_decoder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
11 #include "media/base/audio_bus.h" | |
11 #include "media/base/audio_decoder_config.h" | 12 #include "media/base/audio_decoder_config.h" |
12 #include "media/base/data_buffer.h" | 13 #include "media/base/data_buffer.h" |
13 #include "media/base/decoder_buffer.h" | 14 #include "media/base/decoder_buffer.h" |
14 #include "media/base/demuxer.h" | 15 #include "media/base/demuxer.h" |
15 #include "media/base/pipeline.h" | 16 #include "media/base/pipeline.h" |
16 #include "media/ffmpeg/ffmpeg_common.h" | 17 #include "media/ffmpeg/ffmpeg_common.h" |
17 #include "media/filters/ffmpeg_glue.h" | 18 #include "media/filters/ffmpeg_glue.h" |
18 | 19 |
19 namespace media { | 20 namespace media { |
20 | 21 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 | 143 |
143 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 144 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
144 if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) { | 145 if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) { |
145 DLOG(ERROR) << "Could not initialize audio decoder: " | 146 DLOG(ERROR) << "Could not initialize audio decoder: " |
146 << codec_context_->codec_id; | 147 << codec_context_->codec_id; |
147 | 148 |
148 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED); | 149 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED); |
149 return; | 150 return; |
150 } | 151 } |
151 | 152 |
153 // Some codecs will only output float data, so we need to convert to integer | |
154 // before returning the decoded buffer. | |
155 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP || | |
156 codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { | |
157 DCHECK_EQ(static_cast<size_t>(config.bits_per_channel()), sizeof(float)); | |
158 | |
159 // Preallocate the AudioBus for float conversions. We can treat interleaved | |
160 // float data as a single planar channel since our output is expected in an | |
161 // interleaved format anyways. | |
162 int channels = codec_context_->channels; | |
163 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) | |
164 channels = 1; | |
165 converter_bus_ = AudioBus::CreateWrapper(channels); | |
166 } | |
167 | |
152 // Success! | 168 // Success! |
153 av_frame_ = avcodec_alloc_frame(); | 169 av_frame_ = avcodec_alloc_frame(); |
154 bits_per_channel_ = config.bits_per_channel(); | 170 bits_per_channel_ = config.bits_per_channel(); |
155 channel_layout_ = config.channel_layout(); | 171 channel_layout_ = config.channel_layout(); |
156 samples_per_second_ = config.samples_per_second(); | 172 samples_per_second_ = config.samples_per_second(); |
157 bytes_per_frame_ = codec_context_->channels * bits_per_channel_ / 8; | 173 bytes_per_frame_ = codec_context_->channels * bits_per_channel_ / 8; |
158 status_cb.Run(PIPELINE_OK); | 174 status_cb.Run(PIPELINE_OK); |
159 } | 175 } |
160 | 176 |
161 void FFmpegAudioDecoder::DoReset(const base::Closure& closure) { | 177 void FFmpegAudioDecoder::DoReset(const base::Closure& closure) { |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 if (output_bytes_to_drop_ > 0) { | 304 if (output_bytes_to_drop_ > 0) { |
289 // Currently Vorbis is the only codec that causes us to drop samples. | 305 // Currently Vorbis is the only codec that causes us to drop samples. |
290 // If we have to drop samples it always means the timeline starts at 0. | 306 // If we have to drop samples it always means the timeline starts at 0. |
291 DCHECK(is_vorbis); | 307 DCHECK(is_vorbis); |
292 output_timestamp_base_ = base::TimeDelta(); | 308 output_timestamp_base_ = base::TimeDelta(); |
293 } else { | 309 } else { |
294 output_timestamp_base_ = input->GetTimestamp(); | 310 output_timestamp_base_ = input->GetTimestamp(); |
295 } | 311 } |
296 } | 312 } |
297 | 313 |
298 const uint8* decoded_audio_data = NULL; | |
299 int decoded_audio_size = 0; | 314 int decoded_audio_size = 0; |
300 if (frame_decoded) { | 315 if (frame_decoded) { |
301 int output_sample_rate = av_frame_->sample_rate; | 316 int output_sample_rate = av_frame_->sample_rate; |
302 if (output_sample_rate != samples_per_second_) { | 317 if (output_sample_rate != samples_per_second_) { |
303 DLOG(ERROR) << "Output sample rate (" << output_sample_rate | 318 DLOG(ERROR) << "Output sample rate (" << output_sample_rate |
304 << ") doesn't match expected rate " << samples_per_second_; | 319 << ") doesn't match expected rate " << samples_per_second_; |
305 | 320 |
306 // This is an unrecoverable error, so bail out. | 321 // This is an unrecoverable error, so bail out. |
307 QueuedAudioBuffer queue_entry = { kDecodeError, NULL }; | 322 QueuedAudioBuffer queue_entry = { kDecodeError, NULL }; |
308 queued_audio_.push_back(queue_entry); | 323 queued_audio_.push_back(queue_entry); |
309 break; | 324 break; |
310 } | 325 } |
311 | 326 |
312 decoded_audio_data = av_frame_->data[0]; | |
313 decoded_audio_size = av_samples_get_buffer_size( | 327 decoded_audio_size = av_samples_get_buffer_size( |
314 NULL, codec_context_->channels, av_frame_->nb_samples, | 328 NULL, codec_context_->channels, av_frame_->nb_samples, |
315 codec_context_->sample_fmt, 1); | 329 codec_context_->sample_fmt, 1); |
316 } | 330 } |
317 | 331 |
318 scoped_refptr<DataBuffer> output; | 332 int start_sample = 0; |
333 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { | |
334 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) | |
335 << "Decoder didn't output full frames"; | |
319 | 336 |
320 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { | |
321 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); | 337 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); |
322 decoded_audio_data += dropped_size; | 338 start_sample = dropped_size / bytes_per_frame_; |
323 decoded_audio_size -= dropped_size; | 339 decoded_audio_size -= dropped_size; |
324 output_bytes_to_drop_ -= dropped_size; | 340 output_bytes_to_drop_ -= dropped_size; |
325 } | 341 } |
326 | 342 |
343 scoped_refptr<DataBuffer> output; | |
327 if (decoded_audio_size > 0) { | 344 if (decoded_audio_size > 0) { |
328 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) | 345 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) |
329 << "Decoder didn't output full frames"; | 346 << "Decoder didn't output full frames"; |
330 | 347 |
331 // Copy the audio samples into an output buffer. | 348 // Convert float data using an AudioBus. |
332 output = new DataBuffer(decoded_audio_data, decoded_audio_size); | 349 if (converter_bus_) { |
350 // Setup the AudioBus as a wrapper of the AVFrame data and then use | |
351 // AudioBus::ToInterleaved() to convert the data as necessary. | |
scherkus (not reviewing)
2012/12/06 17:25:32
don't we immediately call FromInterleaved() in ARI
DaleCurtis
2012/12/06 22:15:41
Not quite immediately, but yes. The data first go
| |
352 int skip_frames = start_sample; | |
353 int total_frames = av_frame_->nb_samples - start_sample; | |
354 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { | |
355 DCHECK_EQ(converter_bus_->channels(), 1); | |
356 total_frames *= codec_context_->channels; | |
357 skip_frames *= codec_context_->channels; | |
358 } | |
359 converter_bus_->set_frames(total_frames); | |
360 DCHECK_EQ(decoded_audio_size, | |
361 converter_bus_->frames() * bytes_per_frame_); | |
362 | |
363 for (int i = 0; i < converter_bus_->channels(); ++i) { | |
364 converter_bus_->SetChannelData(i, reinterpret_cast<float*>( | |
365 av_frame_->extended_data[i]) + skip_frames); | |
366 } | |
367 | |
368 output = new DataBuffer(decoded_audio_size); | |
369 output->SetDataSize(decoded_audio_size); | |
370 converter_bus_->ToInterleaved( | |
371 converter_bus_->frames(), bits_per_channel_ / 8, | |
372 output->GetWritableData()); | |
373 } else { | |
374 output = new DataBuffer( | |
375 av_frame_->extended_data[0] + start_sample * bytes_per_frame_, | |
376 decoded_audio_size); | |
377 } | |
333 | 378 |
334 base::TimeDelta timestamp = GetNextOutputTimestamp(); | 379 base::TimeDelta timestamp = GetNextOutputTimestamp(); |
335 total_frames_decoded_ += decoded_audio_size / bytes_per_frame_; | 380 total_frames_decoded_ += decoded_audio_size / bytes_per_frame_; |
336 | 381 |
337 output->SetTimestamp(timestamp); | 382 output->SetTimestamp(timestamp); |
338 output->SetDuration(GetNextOutputTimestamp() - timestamp); | 383 output->SetDuration(GetNextOutputTimestamp() - timestamp); |
339 } else if (IsEndOfStream(result, decoded_audio_size, input)) { | 384 } else if (IsEndOfStream(result, decoded_audio_size, input)) { |
340 DCHECK_EQ(packet.size, 0); | 385 DCHECK_EQ(packet.size, 0); |
341 // Create an end of stream output buffer. | 386 // Create an end of stream output buffer. |
342 output = new DataBuffer(0); | 387 output = new DataBuffer(0); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
375 } | 420 } |
376 | 421 |
377 base::TimeDelta FFmpegAudioDecoder::GetNextOutputTimestamp() const { | 422 base::TimeDelta FFmpegAudioDecoder::GetNextOutputTimestamp() const { |
378 DCHECK(output_timestamp_base_ != kNoTimestamp()); | 423 DCHECK(output_timestamp_base_ != kNoTimestamp()); |
379 double decoded_us = (total_frames_decoded_ / samples_per_second_) * | 424 double decoded_us = (total_frames_decoded_ / samples_per_second_) * |
380 base::Time::kMicrosecondsPerSecond; | 425 base::Time::kMicrosecondsPerSecond; |
381 return output_timestamp_base_ + base::TimeDelta::FromMicroseconds(decoded_us); | 426 return output_timestamp_base_ + base::TimeDelta::FromMicroseconds(decoded_us); |
382 } | 427 } |
383 | 428 |
384 } // namespace media | 429 } // namespace media |
OLD | NEW |