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/audio_timestamp_helper.h" | 13 #include "media/base/audio_timestamp_helper.h" |
13 #include "media/base/data_buffer.h" | 14 #include "media/base/data_buffer.h" |
14 #include "media/base/decoder_buffer.h" | 15 #include "media/base/decoder_buffer.h" |
15 #include "media/base/demuxer.h" | 16 #include "media/base/demuxer.h" |
16 #include "media/base/pipeline.h" | 17 #include "media/base/pipeline.h" |
17 #include "media/ffmpeg/ffmpeg_common.h" | 18 #include "media/ffmpeg/ffmpeg_common.h" |
18 #include "media/filters/ffmpeg_glue.h" | 19 #include "media/filters/ffmpeg_glue.h" |
19 | 20 |
20 namespace media { | 21 namespace media { |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 codec_context_ = avcodec_alloc_context3(NULL); | 284 codec_context_ = avcodec_alloc_context3(NULL); |
284 AudioDecoderConfigToAVCodecContext(config, codec_context_); | 285 AudioDecoderConfigToAVCodecContext(config, codec_context_); |
285 | 286 |
286 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 287 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
287 if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) { | 288 if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) { |
288 DLOG(ERROR) << "Could not initialize audio decoder: " | 289 DLOG(ERROR) << "Could not initialize audio decoder: " |
289 << codec_context_->codec_id; | 290 << codec_context_->codec_id; |
290 return false; | 291 return false; |
291 } | 292 } |
292 | 293 |
294 // Some codecs will only output float data, so we need to convert to integer | |
295 // before returning the decoded buffer. | |
296 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP || | |
297 codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { | |
298 DCHECK_EQ(static_cast<size_t>(config.bits_per_channel() / 8), | |
DaleCurtis
2012/12/13 02:39:38
Fails since MediaSource configures S16 in the deco
| |
299 sizeof(float)); | |
300 | |
301 // Preallocate the AudioBus for float conversions. We can treat interleaved | |
302 // float data as a single planar channel since our output is expected in an | |
303 // interleaved format anyways. | |
304 int channels = codec_context_->channels; | |
305 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) | |
306 channels = 1; | |
307 converter_bus_ = AudioBus::CreateWrapper(channels); | |
308 } | |
309 | |
293 // Success! | 310 // Success! |
294 av_frame_ = avcodec_alloc_frame(); | 311 av_frame_ = avcodec_alloc_frame(); |
295 bits_per_channel_ = config.bits_per_channel(); | 312 bits_per_channel_ = config.bits_per_channel(); |
296 channel_layout_ = config.channel_layout(); | 313 channel_layout_ = config.channel_layout(); |
297 samples_per_second_ = config.samples_per_second(); | 314 samples_per_second_ = config.samples_per_second(); |
298 output_timestamp_helper_.reset(new AudioTimestampHelper( | 315 output_timestamp_helper_.reset(new AudioTimestampHelper( |
299 config.bytes_per_frame(), config.samples_per_second())); | 316 config.bytes_per_frame(), config.samples_per_second())); |
300 return true; | 317 return true; |
301 } | 318 } |
302 | 319 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 if (output_bytes_to_drop_ > 0) { | 384 if (output_bytes_to_drop_ > 0) { |
368 // Currently Vorbis is the only codec that causes us to drop samples. | 385 // Currently Vorbis is the only codec that causes us to drop samples. |
369 // If we have to drop samples it always means the timeline starts at 0. | 386 // If we have to drop samples it always means the timeline starts at 0. |
370 DCHECK_EQ(codec_context_->codec_id, CODEC_ID_VORBIS); | 387 DCHECK_EQ(codec_context_->codec_id, CODEC_ID_VORBIS); |
371 output_timestamp_helper_->SetBaseTimestamp(base::TimeDelta()); | 388 output_timestamp_helper_->SetBaseTimestamp(base::TimeDelta()); |
372 } else { | 389 } else { |
373 output_timestamp_helper_->SetBaseTimestamp(input->GetTimestamp()); | 390 output_timestamp_helper_->SetBaseTimestamp(input->GetTimestamp()); |
374 } | 391 } |
375 } | 392 } |
376 | 393 |
377 const uint8* decoded_audio_data = NULL; | |
378 int decoded_audio_size = 0; | 394 int decoded_audio_size = 0; |
379 if (frame_decoded) { | 395 if (frame_decoded) { |
380 int output_sample_rate = av_frame_->sample_rate; | 396 int output_sample_rate = av_frame_->sample_rate; |
381 if (output_sample_rate != samples_per_second_) { | 397 if (output_sample_rate != samples_per_second_) { |
382 DLOG(ERROR) << "Output sample rate (" << output_sample_rate | 398 DLOG(ERROR) << "Output sample rate (" << output_sample_rate |
383 << ") doesn't match expected rate " << samples_per_second_; | 399 << ") doesn't match expected rate " << samples_per_second_; |
384 | 400 |
385 // This is an unrecoverable error, so bail out. | 401 // This is an unrecoverable error, so bail out. |
386 QueuedAudioBuffer queue_entry = { kDecodeError, NULL }; | 402 QueuedAudioBuffer queue_entry = { kDecodeError, NULL }; |
387 queued_audio_.push_back(queue_entry); | 403 queued_audio_.push_back(queue_entry); |
388 break; | 404 break; |
389 } | 405 } |
390 | 406 |
391 decoded_audio_data = av_frame_->data[0]; | |
392 decoded_audio_size = av_samples_get_buffer_size( | 407 decoded_audio_size = av_samples_get_buffer_size( |
393 NULL, codec_context_->channels, av_frame_->nb_samples, | 408 NULL, codec_context_->channels, av_frame_->nb_samples, |
394 codec_context_->sample_fmt, 1); | 409 codec_context_->sample_fmt, 1); |
395 } | 410 } |
396 | 411 |
397 scoped_refptr<DataBuffer> output; | 412 int start_sample = 0; |
413 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { | |
414 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) | |
415 << "Decoder didn't output full frames"; | |
398 | 416 |
399 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { | |
400 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); | 417 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); |
401 decoded_audio_data += dropped_size; | 418 start_sample = dropped_size / bytes_per_frame_; |
402 decoded_audio_size -= dropped_size; | 419 decoded_audio_size -= dropped_size; |
403 output_bytes_to_drop_ -= dropped_size; | 420 output_bytes_to_drop_ -= dropped_size; |
404 } | 421 } |
405 | 422 |
423 scoped_refptr<DataBuffer> output; | |
406 if (decoded_audio_size > 0) { | 424 if (decoded_audio_size > 0) { |
407 // Copy the audio samples into an output buffer. | 425 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) |
408 output = new DataBuffer(decoded_audio_data, decoded_audio_size); | 426 << "Decoder didn't output full frames"; |
427 | |
428 // Convert float data using an AudioBus. | |
429 if (converter_bus_) { | |
430 // Setup the AudioBus as a wrapper of the AVFrame data and then use | |
431 // AudioBus::ToInterleaved() to convert the data as necessary. | |
432 int skip_frames = start_sample; | |
433 int total_frames = av_frame_->nb_samples - start_sample; | |
434 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { | |
435 DCHECK_EQ(converter_bus_->channels(), 1); | |
436 total_frames *= codec_context_->channels; | |
437 skip_frames *= codec_context_->channels; | |
438 } | |
439 converter_bus_->set_frames(total_frames); | |
440 DCHECK_EQ(decoded_audio_size, | |
441 converter_bus_->frames() * bytes_per_frame_); | |
442 | |
443 for (int i = 0; i < converter_bus_->channels(); ++i) { | |
444 converter_bus_->SetChannelData(i, reinterpret_cast<float*>( | |
445 av_frame_->extended_data[i]) + skip_frames); | |
446 } | |
447 | |
448 output = new DataBuffer(decoded_audio_size); | |
449 output->SetDataSize(decoded_audio_size); | |
450 converter_bus_->ToInterleaved( | |
451 converter_bus_->frames(), bits_per_channel_ / 8, | |
452 output->GetWritableData()); | |
453 } else { | |
454 output = new DataBuffer( | |
455 av_frame_->extended_data[0] + start_sample * bytes_per_frame_, | |
456 decoded_audio_size); | |
457 } | |
409 output->SetTimestamp(output_timestamp_helper_->GetTimestamp()); | 458 output->SetTimestamp(output_timestamp_helper_->GetTimestamp()); |
410 output->SetDuration( | 459 output->SetDuration( |
411 output_timestamp_helper_->GetDuration(decoded_audio_size)); | 460 output_timestamp_helper_->GetDuration(decoded_audio_size)); |
412 output_timestamp_helper_->AddBytes(decoded_audio_size); | 461 output_timestamp_helper_->AddBytes(decoded_audio_size); |
413 } else if (IsEndOfStream(result, decoded_audio_size, input) && | 462 } else if (IsEndOfStream(result, decoded_audio_size, input) && |
414 !skip_eos_append) { | 463 !skip_eos_append) { |
415 DCHECK_EQ(packet.size, 0); | 464 DCHECK_EQ(packet.size, 0); |
416 // Create an end of stream output buffer. | 465 // Create an end of stream output buffer. |
417 output = new DataBuffer(0); | 466 output = new DataBuffer(0); |
418 } | 467 } |
419 | 468 |
420 if (output) { | 469 if (output) { |
421 QueuedAudioBuffer queue_entry = { kOk, output }; | 470 QueuedAudioBuffer queue_entry = { kOk, output }; |
422 queued_audio_.push_back(queue_entry); | 471 queued_audio_.push_back(queue_entry); |
423 } | 472 } |
424 | 473 |
425 // Decoding finished successfully, update statistics. | 474 // Decoding finished successfully, update statistics. |
426 if (result > 0) { | 475 if (result > 0) { |
427 PipelineStatistics statistics; | 476 PipelineStatistics statistics; |
428 statistics.audio_bytes_decoded = result; | 477 statistics.audio_bytes_decoded = result; |
429 statistics_cb_.Run(statistics); | 478 statistics_cb_.Run(statistics); |
430 } | 479 } |
431 } while (packet.size > 0); | 480 } while (packet.size > 0); |
432 } | 481 } |
433 | 482 |
434 } // namespace media | 483 } // namespace media |
OLD | NEW |