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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 return false; | 277 return false; |
277 } | 278 } |
278 | 279 |
279 // Release existing decoder resources if necessary. | 280 // Release existing decoder resources if necessary. |
280 ReleaseFFmpegResources(); | 281 ReleaseFFmpegResources(); |
281 | 282 |
282 // Initialize AVCodecContext structure. | 283 // Initialize AVCodecContext structure. |
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 |
287 // MP3 decodes to S16P which we don't support, tell it to use S16 instead. | |
288 bool request_s16_format = codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P; | |
scherkus (not reviewing)
2012/12/13 22:40:11
ditto
| |
289 if (request_s16_format) | |
290 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16; | |
291 | |
286 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); | 292 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); |
287 if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) { | 293 if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) { |
288 DLOG(ERROR) << "Could not initialize audio decoder: " | 294 DLOG(ERROR) << "Could not initialize audio decoder: " |
289 << codec_context_->codec_id; | 295 << codec_context_->codec_id; |
290 return false; | 296 return false; |
291 } | 297 } |
292 | 298 |
299 // Ensure avcodec_open2() respected our format request. | |
300 if (request_s16_format && codec_context_->sample_fmt != AV_SAMPLE_FMT_S16) { | |
301 DLOG(ERROR) << "Unable to configure a supported sample format: " | |
302 << codec_context_->sample_fmt; | |
303 return false; | |
304 } | |
305 | |
306 // Some codecs will only output float data, so we need to convert to integer | |
307 // before returning the decoded buffer. | |
308 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP || | |
309 codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { | |
310 // Preallocate the AudioBus for float conversions. We can treat interleaved | |
311 // float data as a single planar channel since our output is expected in an | |
312 // interleaved format anyways. | |
313 int channels = codec_context_->channels; | |
314 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) | |
315 channels = 1; | |
316 converter_bus_ = AudioBus::CreateWrapper(channels); | |
317 } | |
318 | |
293 // Success! | 319 // Success! |
294 av_frame_ = avcodec_alloc_frame(); | 320 av_frame_ = avcodec_alloc_frame(); |
295 bits_per_channel_ = config.bits_per_channel(); | 321 bits_per_channel_ = config.bits_per_channel(); |
296 channel_layout_ = config.channel_layout(); | 322 channel_layout_ = config.channel_layout(); |
297 samples_per_second_ = config.samples_per_second(); | 323 samples_per_second_ = config.samples_per_second(); |
298 output_timestamp_helper_.reset(new AudioTimestampHelper( | 324 output_timestamp_helper_.reset(new AudioTimestampHelper( |
299 config.bytes_per_frame(), config.samples_per_second())); | 325 config.bytes_per_frame(), config.samples_per_second())); |
326 bytes_per_frame_ = config.bytes_per_frame(); | |
300 return true; | 327 return true; |
301 } | 328 } |
302 | 329 |
303 void FFmpegAudioDecoder::ReleaseFFmpegResources() { | 330 void FFmpegAudioDecoder::ReleaseFFmpegResources() { |
304 if (codec_context_) { | 331 if (codec_context_) { |
305 av_free(codec_context_->extradata); | 332 av_free(codec_context_->extradata); |
306 avcodec_close(codec_context_); | 333 avcodec_close(codec_context_); |
307 av_free(codec_context_); | 334 av_free(codec_context_); |
308 } | 335 } |
309 | 336 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 if (output_bytes_to_drop_ > 0) { | 394 if (output_bytes_to_drop_ > 0) { |
368 // Currently Vorbis is the only codec that causes us to drop samples. | 395 // 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. | 396 // If we have to drop samples it always means the timeline starts at 0. |
370 DCHECK_EQ(codec_context_->codec_id, CODEC_ID_VORBIS); | 397 DCHECK_EQ(codec_context_->codec_id, CODEC_ID_VORBIS); |
371 output_timestamp_helper_->SetBaseTimestamp(base::TimeDelta()); | 398 output_timestamp_helper_->SetBaseTimestamp(base::TimeDelta()); |
372 } else { | 399 } else { |
373 output_timestamp_helper_->SetBaseTimestamp(input->GetTimestamp()); | 400 output_timestamp_helper_->SetBaseTimestamp(input->GetTimestamp()); |
374 } | 401 } |
375 } | 402 } |
376 | 403 |
377 const uint8* decoded_audio_data = NULL; | |
378 int decoded_audio_size = 0; | 404 int decoded_audio_size = 0; |
379 if (frame_decoded) { | 405 if (frame_decoded) { |
380 int output_sample_rate = av_frame_->sample_rate; | 406 int output_sample_rate = av_frame_->sample_rate; |
381 if (output_sample_rate != samples_per_second_) { | 407 if (output_sample_rate != samples_per_second_) { |
382 DLOG(ERROR) << "Output sample rate (" << output_sample_rate | 408 DLOG(ERROR) << "Output sample rate (" << output_sample_rate |
383 << ") doesn't match expected rate " << samples_per_second_; | 409 << ") doesn't match expected rate " << samples_per_second_; |
384 | 410 |
385 // This is an unrecoverable error, so bail out. | 411 // This is an unrecoverable error, so bail out. |
386 QueuedAudioBuffer queue_entry = { kDecodeError, NULL }; | 412 QueuedAudioBuffer queue_entry = { kDecodeError, NULL }; |
387 queued_audio_.push_back(queue_entry); | 413 queued_audio_.push_back(queue_entry); |
388 break; | 414 break; |
389 } | 415 } |
390 | 416 |
391 decoded_audio_data = av_frame_->data[0]; | |
392 decoded_audio_size = av_samples_get_buffer_size( | 417 decoded_audio_size = av_samples_get_buffer_size( |
393 NULL, codec_context_->channels, av_frame_->nb_samples, | 418 NULL, codec_context_->channels, av_frame_->nb_samples, |
394 codec_context_->sample_fmt, 1); | 419 codec_context_->sample_fmt, 1); |
420 // If we're decoding into float, adjust audio size. | |
421 if (converter_bus_ && bits_per_channel_ / 8 != sizeof(float)) { | |
422 DCHECK(codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT || | |
423 codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP); | |
424 decoded_audio_size *= | |
425 static_cast<float>(bits_per_channel_ / 8) / sizeof(float); | |
426 } | |
395 } | 427 } |
396 | 428 |
397 scoped_refptr<DataBuffer> output; | 429 int start_sample = 0; |
430 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { | |
431 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) | |
432 << "Decoder didn't output full frames"; | |
398 | 433 |
399 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) { | |
400 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); | 434 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); |
401 decoded_audio_data += dropped_size; | 435 start_sample = dropped_size / bytes_per_frame_; |
402 decoded_audio_size -= dropped_size; | 436 decoded_audio_size -= dropped_size; |
403 output_bytes_to_drop_ -= dropped_size; | 437 output_bytes_to_drop_ -= dropped_size; |
404 } | 438 } |
405 | 439 |
440 scoped_refptr<DataBuffer> output; | |
406 if (decoded_audio_size > 0) { | 441 if (decoded_audio_size > 0) { |
407 // Copy the audio samples into an output buffer. | 442 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0) |
408 output = new DataBuffer(decoded_audio_data, decoded_audio_size); | 443 << "Decoder didn't output full frames"; |
444 | |
445 // Convert float data using an AudioBus. | |
446 if (converter_bus_) { | |
447 // Setup the AudioBus as a wrapper of the AVFrame data and then use | |
448 // AudioBus::ToInterleaved() to convert the data as necessary. | |
449 int skip_frames = start_sample; | |
450 int total_frames = av_frame_->nb_samples - start_sample; | |
451 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) { | |
452 DCHECK_EQ(converter_bus_->channels(), 1); | |
453 total_frames *= codec_context_->channels; | |
454 skip_frames *= codec_context_->channels; | |
455 } | |
456 converter_bus_->set_frames(total_frames); | |
457 DCHECK_EQ(decoded_audio_size, | |
458 converter_bus_->frames() * bytes_per_frame_); | |
459 | |
460 for (int i = 0; i < converter_bus_->channels(); ++i) { | |
461 converter_bus_->SetChannelData(i, reinterpret_cast<float*>( | |
462 av_frame_->extended_data[i]) + skip_frames); | |
463 } | |
464 | |
465 output = new DataBuffer(decoded_audio_size); | |
466 output->SetDataSize(decoded_audio_size); | |
467 converter_bus_->ToInterleaved( | |
468 converter_bus_->frames(), bits_per_channel_ / 8, | |
469 output->GetWritableData()); | |
470 } else { | |
471 output = new DataBuffer( | |
472 av_frame_->extended_data[0] + start_sample * bytes_per_frame_, | |
473 decoded_audio_size); | |
474 } | |
409 output->SetTimestamp(output_timestamp_helper_->GetTimestamp()); | 475 output->SetTimestamp(output_timestamp_helper_->GetTimestamp()); |
410 output->SetDuration( | 476 output->SetDuration( |
411 output_timestamp_helper_->GetDuration(decoded_audio_size)); | 477 output_timestamp_helper_->GetDuration(decoded_audio_size)); |
412 output_timestamp_helper_->AddBytes(decoded_audio_size); | 478 output_timestamp_helper_->AddBytes(decoded_audio_size); |
413 } else if (IsEndOfStream(result, decoded_audio_size, input) && | 479 } else if (IsEndOfStream(result, decoded_audio_size, input) && |
414 !skip_eos_append) { | 480 !skip_eos_append) { |
415 DCHECK_EQ(packet.size, 0); | 481 DCHECK_EQ(packet.size, 0); |
416 // Create an end of stream output buffer. | 482 // Create an end of stream output buffer. |
417 output = new DataBuffer(0); | 483 output = new DataBuffer(0); |
418 } | 484 } |
419 | 485 |
420 if (output) { | 486 if (output) { |
421 QueuedAudioBuffer queue_entry = { kOk, output }; | 487 QueuedAudioBuffer queue_entry = { kOk, output }; |
422 queued_audio_.push_back(queue_entry); | 488 queued_audio_.push_back(queue_entry); |
423 } | 489 } |
424 | 490 |
425 // Decoding finished successfully, update statistics. | 491 // Decoding finished successfully, update statistics. |
426 if (result > 0) { | 492 if (result > 0) { |
427 PipelineStatistics statistics; | 493 PipelineStatistics statistics; |
428 statistics.audio_bytes_decoded = result; | 494 statistics.audio_bytes_decoded = result; |
429 statistics_cb_.Run(statistics); | 495 statistics_cb_.Run(statistics); |
430 } | 496 } |
431 } while (packet.size > 0); | 497 } while (packet.size > 0); |
432 } | 498 } |
433 | 499 |
434 } // namespace media | 500 } // namespace media |
OLD | NEW |