Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: media/filters/ffmpeg_audio_decoder.cc

Issue 11280301: Roll FFMpeg for M26. Fix ffmpeg float audio decoding. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... rebase Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/filters/ffmpeg_audio_decoder.h ('k') | media/filters/ffmpeg_audio_decoder_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P)
289 codec_context_->request_sample_fmt = AV_SAMPLE_FMT_S16;
290
286 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id); 291 AVCodec* codec = avcodec_find_decoder(codec_context_->codec_id);
287 if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) { 292 if (!codec || avcodec_open2(codec_context_, codec, NULL) < 0) {
288 DLOG(ERROR) << "Could not initialize audio decoder: " 293 DLOG(ERROR) << "Could not initialize audio decoder: "
289 << codec_context_->codec_id; 294 << codec_context_->codec_id;
290 return false; 295 return false;
291 } 296 }
292 297
298 // Ensure avcodec_open2() respected our format request.
299 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_S16P) {
300 DLOG(ERROR) << "Unable to configure a supported sample format: "
301 << codec_context_->sample_fmt;
302 return false;
303 }
304
305 // Some codecs will only output float data, so we need to convert to integer
306 // before returning the decoded buffer.
307 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP ||
308 codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) {
309 // Preallocate the AudioBus for float conversions. We can treat interleaved
310 // float data as a single planar channel since our output is expected in an
311 // interleaved format anyways.
312 int channels = codec_context_->channels;
313 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT)
314 channels = 1;
315 converter_bus_ = AudioBus::CreateWrapper(channels);
316 }
317
293 // Success! 318 // Success!
294 av_frame_ = avcodec_alloc_frame(); 319 av_frame_ = avcodec_alloc_frame();
295 bits_per_channel_ = config.bits_per_channel(); 320 bits_per_channel_ = config.bits_per_channel();
296 channel_layout_ = config.channel_layout(); 321 channel_layout_ = config.channel_layout();
297 samples_per_second_ = config.samples_per_second(); 322 samples_per_second_ = config.samples_per_second();
298 output_timestamp_helper_.reset(new AudioTimestampHelper( 323 output_timestamp_helper_.reset(new AudioTimestampHelper(
299 config.bytes_per_frame(), config.samples_per_second())); 324 config.bytes_per_frame(), config.samples_per_second()));
325 bytes_per_frame_ = config.bytes_per_frame();
300 return true; 326 return true;
301 } 327 }
302 328
303 void FFmpegAudioDecoder::ReleaseFFmpegResources() { 329 void FFmpegAudioDecoder::ReleaseFFmpegResources() {
304 if (codec_context_) { 330 if (codec_context_) {
305 av_free(codec_context_->extradata); 331 av_free(codec_context_->extradata);
306 avcodec_close(codec_context_); 332 avcodec_close(codec_context_);
307 av_free(codec_context_); 333 av_free(codec_context_);
308 } 334 }
309 335
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 if (output_bytes_to_drop_ > 0) { 393 if (output_bytes_to_drop_ > 0) {
368 // Currently Vorbis is the only codec that causes us to drop samples. 394 // 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. 395 // If we have to drop samples it always means the timeline starts at 0.
370 DCHECK_EQ(codec_context_->codec_id, CODEC_ID_VORBIS); 396 DCHECK_EQ(codec_context_->codec_id, CODEC_ID_VORBIS);
371 output_timestamp_helper_->SetBaseTimestamp(base::TimeDelta()); 397 output_timestamp_helper_->SetBaseTimestamp(base::TimeDelta());
372 } else { 398 } else {
373 output_timestamp_helper_->SetBaseTimestamp(input->GetTimestamp()); 399 output_timestamp_helper_->SetBaseTimestamp(input->GetTimestamp());
374 } 400 }
375 } 401 }
376 402
377 const uint8* decoded_audio_data = NULL;
378 int decoded_audio_size = 0; 403 int decoded_audio_size = 0;
379 if (frame_decoded) { 404 if (frame_decoded) {
380 int output_sample_rate = av_frame_->sample_rate; 405 int output_sample_rate = av_frame_->sample_rate;
381 if (output_sample_rate != samples_per_second_) { 406 if (output_sample_rate != samples_per_second_) {
382 DLOG(ERROR) << "Output sample rate (" << output_sample_rate 407 DLOG(ERROR) << "Output sample rate (" << output_sample_rate
383 << ") doesn't match expected rate " << samples_per_second_; 408 << ") doesn't match expected rate " << samples_per_second_;
384 409
385 // This is an unrecoverable error, so bail out. 410 // This is an unrecoverable error, so bail out.
386 QueuedAudioBuffer queue_entry = { kDecodeError, NULL }; 411 QueuedAudioBuffer queue_entry = { kDecodeError, NULL };
387 queued_audio_.push_back(queue_entry); 412 queued_audio_.push_back(queue_entry);
388 break; 413 break;
389 } 414 }
390 415
391 decoded_audio_data = av_frame_->data[0];
392 decoded_audio_size = av_samples_get_buffer_size( 416 decoded_audio_size = av_samples_get_buffer_size(
393 NULL, codec_context_->channels, av_frame_->nb_samples, 417 NULL, codec_context_->channels, av_frame_->nb_samples,
394 codec_context_->sample_fmt, 1); 418 codec_context_->sample_fmt, 1);
419 // If we're decoding into float, adjust audio size.
420 if (converter_bus_ && bits_per_channel_ / 8 != sizeof(float)) {
421 DCHECK(codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT ||
422 codec_context_->sample_fmt == AV_SAMPLE_FMT_FLTP);
423 decoded_audio_size *=
424 static_cast<float>(bits_per_channel_ / 8) / sizeof(float);
425 }
395 } 426 }
396 427
397 scoped_refptr<DataBuffer> output; 428 int start_sample = 0;
429 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) {
430 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0)
431 << "Decoder didn't output full frames";
398 432
399 if (decoded_audio_size > 0 && output_bytes_to_drop_ > 0) {
400 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_); 433 int dropped_size = std::min(decoded_audio_size, output_bytes_to_drop_);
401 decoded_audio_data += dropped_size; 434 start_sample = dropped_size / bytes_per_frame_;
402 decoded_audio_size -= dropped_size; 435 decoded_audio_size -= dropped_size;
403 output_bytes_to_drop_ -= dropped_size; 436 output_bytes_to_drop_ -= dropped_size;
404 } 437 }
405 438
439 scoped_refptr<DataBuffer> output;
406 if (decoded_audio_size > 0) { 440 if (decoded_audio_size > 0) {
407 // Copy the audio samples into an output buffer. 441 DCHECK_EQ(decoded_audio_size % bytes_per_frame_, 0)
408 output = new DataBuffer(decoded_audio_data, decoded_audio_size); 442 << "Decoder didn't output full frames";
443
444 // Convert float data using an AudioBus.
445 if (converter_bus_) {
446 // Setup the AudioBus as a wrapper of the AVFrame data and then use
447 // AudioBus::ToInterleaved() to convert the data as necessary.
448 int skip_frames = start_sample;
449 int total_frames = av_frame_->nb_samples - start_sample;
450 if (codec_context_->sample_fmt == AV_SAMPLE_FMT_FLT) {
451 DCHECK_EQ(converter_bus_->channels(), 1);
452 total_frames *= codec_context_->channels;
453 skip_frames *= codec_context_->channels;
454 }
455 converter_bus_->set_frames(total_frames);
456 DCHECK_EQ(decoded_audio_size,
457 converter_bus_->frames() * bytes_per_frame_);
458
459 for (int i = 0; i < converter_bus_->channels(); ++i) {
460 converter_bus_->SetChannelData(i, reinterpret_cast<float*>(
461 av_frame_->extended_data[i]) + skip_frames);
462 }
463
464 output = new DataBuffer(decoded_audio_size);
465 output->SetDataSize(decoded_audio_size);
466 converter_bus_->ToInterleaved(
467 converter_bus_->frames(), bits_per_channel_ / 8,
468 output->GetWritableData());
469 } else {
470 output = new DataBuffer(
471 av_frame_->extended_data[0] + start_sample * bytes_per_frame_,
472 decoded_audio_size);
473 }
409 output->SetTimestamp(output_timestamp_helper_->GetTimestamp()); 474 output->SetTimestamp(output_timestamp_helper_->GetTimestamp());
410 output->SetDuration( 475 output->SetDuration(
411 output_timestamp_helper_->GetDuration(decoded_audio_size)); 476 output_timestamp_helper_->GetDuration(decoded_audio_size));
412 output_timestamp_helper_->AddBytes(decoded_audio_size); 477 output_timestamp_helper_->AddBytes(decoded_audio_size);
413 } else if (IsEndOfStream(result, decoded_audio_size, input) && 478 } else if (IsEndOfStream(result, decoded_audio_size, input) &&
414 !skip_eos_append) { 479 !skip_eos_append) {
415 DCHECK_EQ(packet.size, 0); 480 DCHECK_EQ(packet.size, 0);
416 // Create an end of stream output buffer. 481 // Create an end of stream output buffer.
417 output = new DataBuffer(0); 482 output = new DataBuffer(0);
418 } 483 }
419 484
420 if (output) { 485 if (output) {
421 QueuedAudioBuffer queue_entry = { kOk, output }; 486 QueuedAudioBuffer queue_entry = { kOk, output };
422 queued_audio_.push_back(queue_entry); 487 queued_audio_.push_back(queue_entry);
423 } 488 }
424 489
425 // Decoding finished successfully, update statistics. 490 // Decoding finished successfully, update statistics.
426 if (result > 0) { 491 if (result > 0) {
427 PipelineStatistics statistics; 492 PipelineStatistics statistics;
428 statistics.audio_bytes_decoded = result; 493 statistics.audio_bytes_decoded = result;
429 statistics_cb_.Run(statistics); 494 statistics_cb_.Run(statistics);
430 } 495 }
431 } while (packet.size > 0); 496 } while (packet.size > 0);
432 } 497 }
433 498
434 } // namespace media 499 } // namespace media
OLDNEW
« no previous file with comments | « media/filters/ffmpeg_audio_decoder.h ('k') | media/filters/ffmpeg_audio_decoder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698