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/ffmpeg/ffmpeg_common.h" | 5 #include "media/ffmpeg/ffmpeg_common.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/sha1.h" | 9 #include "base/sha1.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "media/base/decoder_buffer.h" | 14 #include "media/base/decoder_buffer.h" |
15 #include "media/base/video_decoder_config.h" | 15 #include "media/base/video_decoder_config.h" |
16 #include "media/base/video_util.h" | 16 #include "media/base/video_util.h" |
17 #include "media/media_features.h" | |
17 | 18 |
18 namespace media { | 19 namespace media { |
19 | 20 |
20 // Why FF_INPUT_BUFFER_PADDING_SIZE? FFmpeg assumes all input buffers are | 21 // Why FF_INPUT_BUFFER_PADDING_SIZE? FFmpeg assumes all input buffers are |
21 // padded. Check here to ensure FFmpeg only receives data padded to its | 22 // padded. Check here to ensure FFmpeg only receives data padded to its |
22 // specifications. | 23 // specifications. |
23 static_assert(DecoderBuffer::kPaddingSize >= FF_INPUT_BUFFER_PADDING_SIZE, | 24 static_assert(DecoderBuffer::kPaddingSize >= FF_INPUT_BUFFER_PADDING_SIZE, |
24 "DecoderBuffer padding size does not fit ffmpeg requirement"); | 25 "DecoderBuffer padding size does not fit ffmpeg requirement"); |
25 | 26 |
26 // Alignment requirement by FFmpeg for input and output buffers. This need to | 27 // Alignment requirement by FFmpeg for input and output buffers. This need to |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 int64_t ConvertToTimeBase(const AVRational& time_base, | 61 int64_t ConvertToTimeBase(const AVRational& time_base, |
61 const base::TimeDelta& timestamp) { | 62 const base::TimeDelta& timestamp) { |
62 return av_rescale_q(timestamp.InMicroseconds(), kMicrosBase, time_base); | 63 return av_rescale_q(timestamp.InMicroseconds(), kMicrosBase, time_base); |
63 } | 64 } |
64 | 65 |
65 // Converts an FFmpeg audio codec ID into its corresponding supported codec id. | 66 // Converts an FFmpeg audio codec ID into its corresponding supported codec id. |
66 static AudioCodec CodecIDToAudioCodec(AVCodecID codec_id) { | 67 static AudioCodec CodecIDToAudioCodec(AVCodecID codec_id) { |
67 switch (codec_id) { | 68 switch (codec_id) { |
68 case AV_CODEC_ID_AAC: | 69 case AV_CODEC_ID_AAC: |
69 return kCodecAAC; | 70 return kCodecAAC; |
71 #if BUILDFLAG(ENABLE_AC3_EAC3_AUDIO_DEMUXING) | |
72 case AV_CODEC_ID_AC3: | |
73 return kCodecAC3; | |
74 case AV_CODEC_ID_EAC3: | |
75 return kCodecEAC3; | |
76 #endif | |
70 case AV_CODEC_ID_MP3: | 77 case AV_CODEC_ID_MP3: |
71 return kCodecMP3; | 78 return kCodecMP3; |
72 case AV_CODEC_ID_VORBIS: | 79 case AV_CODEC_ID_VORBIS: |
73 return kCodecVorbis; | 80 return kCodecVorbis; |
74 case AV_CODEC_ID_PCM_U8: | 81 case AV_CODEC_ID_PCM_U8: |
75 case AV_CODEC_ID_PCM_S16LE: | 82 case AV_CODEC_ID_PCM_S16LE: |
76 case AV_CODEC_ID_PCM_S24LE: | 83 case AV_CODEC_ID_PCM_S24LE: |
77 case AV_CODEC_ID_PCM_S32LE: | 84 case AV_CODEC_ID_PCM_S32LE: |
78 case AV_CODEC_ID_PCM_F32LE: | 85 case AV_CODEC_ID_PCM_F32LE: |
79 return kCodecPCM; | 86 return kCodecPCM; |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 | 309 |
303 AudioCodec codec = CodecIDToAudioCodec(codec_context->codec_id); | 310 AudioCodec codec = CodecIDToAudioCodec(codec_context->codec_id); |
304 | 311 |
305 SampleFormat sample_format = AVSampleFormatToSampleFormat( | 312 SampleFormat sample_format = AVSampleFormatToSampleFormat( |
306 codec_context->sample_fmt, codec_context->codec_id); | 313 codec_context->sample_fmt, codec_context->codec_id); |
307 | 314 |
308 ChannelLayout channel_layout = ChannelLayoutToChromeChannelLayout( | 315 ChannelLayout channel_layout = ChannelLayoutToChromeChannelLayout( |
309 codec_context->channel_layout, codec_context->channels); | 316 codec_context->channel_layout, codec_context->channels); |
310 | 317 |
311 int sample_rate = codec_context->sample_rate; | 318 int sample_rate = codec_context->sample_rate; |
312 if (codec == kCodecOpus) { | 319 if (codec == kCodecOpus) { |
ddorwin
2016/01/08 20:15:42
I think a switch statement would make sense here t
servolk
2016/01/08 21:55:48
Done (converted to switch, but I think it's better
| |
313 // |codec_context->sample_fmt| is not set by FFmpeg because Opus decoding is | 320 // |codec_context->sample_fmt| is not set by FFmpeg because Opus decoding is |
314 // not enabled in FFmpeg. It doesn't matter what value is set here, so long | 321 // not enabled in FFmpeg. It doesn't matter what value is set here, so long |
315 // as it's valid, the true sample format is selected inside the decoder. | 322 // as it's valid, the true sample format is selected inside the decoder. |
316 sample_format = kSampleFormatF32; | 323 sample_format = kSampleFormatF32; |
317 | 324 |
318 // Always use 48kHz for OPUS. Technically we should match to the highest | 325 // Always use 48kHz for OPUS. Technically we should match to the highest |
319 // supported hardware sample rate among [8, 12, 16, 24, 48] kHz, but we | 326 // supported hardware sample rate among [8, 12, 16, 24, 48] kHz, but we |
320 // don't know the hardware sample rate at this point and those rates are | 327 // don't know the hardware sample rate at this point and those rates are |
321 // rarely used for output. See the "Input Sample Rate" section of the spec: | 328 // rarely used for output. See the "Input Sample Rate" section of the spec: |
322 // http://tools.ietf.org/html/draft-terriberry-oggopus-01#page-11 | 329 // http://tools.ietf.org/html/draft-terriberry-oggopus-01#page-11 |
323 sample_rate = 48000; | 330 sample_rate = 48000; |
324 } | 331 } |
325 | 332 |
333 #if BUILDFLAG(ENABLE_AC3_EAC3_AUDIO_DEMUXING) | |
334 // For AC3/EAC3 we enable only demuxing, but not decoding, so FFmpeg does not | |
335 // fill |sample_fmt|. | |
336 if (codec == kCodecAC3 || codec == kCodecEAC3) { | |
337 // The spec for AC3/EAC3 audio is ETSI TS 102 366. According to sections | |
338 // F.3.1 and F.5.1 in that spec the sample_format for AC3/EAC3 must be 16. | |
339 sample_format = kSampleFormatS16; | |
340 } | |
341 #else | |
342 DCHECK(codec != kCodecAC3 && codec != kCodecEAC3); | |
343 #endif | |
344 | |
326 base::TimeDelta seek_preroll; | 345 base::TimeDelta seek_preroll; |
327 if (codec_context->seek_preroll > 0) { | 346 if (codec_context->seek_preroll > 0) { |
328 seek_preroll = base::TimeDelta::FromMicroseconds( | 347 seek_preroll = base::TimeDelta::FromMicroseconds( |
329 codec_context->seek_preroll * 1000000.0 / codec_context->sample_rate); | 348 codec_context->seek_preroll * 1000000.0 / codec_context->sample_rate); |
330 } | 349 } |
331 | 350 |
332 // AVStream occasionally has invalid extra data. See http://crbug.com/517163 | 351 // AVStream occasionally has invalid extra data. See http://crbug.com/517163 |
333 if ((codec_context->extradata_size == 0) != | 352 if ((codec_context->extradata_size == 0) != |
334 (codec_context->extradata == nullptr)) { | 353 (codec_context->extradata == nullptr)) { |
335 LOG(ERROR) << __FUNCTION__ | 354 LOG(ERROR) << __FUNCTION__ |
(...skipping 10 matching lines...) Expand all Loading... | |
346 } | 365 } |
347 config->Initialize(codec, | 366 config->Initialize(codec, |
348 sample_format, | 367 sample_format, |
349 channel_layout, | 368 channel_layout, |
350 sample_rate, | 369 sample_rate, |
351 extra_data, | 370 extra_data, |
352 is_encrypted, | 371 is_encrypted, |
353 seek_preroll, | 372 seek_preroll, |
354 codec_context->delay); | 373 codec_context->delay); |
355 | 374 |
356 if (codec != kCodecOpus) { | 375 switch (codec) { |
357 DCHECK_EQ(av_get_bytes_per_sample(codec_context->sample_fmt) * 8, | 376 case kCodecOpus: |
358 config->bits_per_channel()); | 377 #if BUILDFLAG(ENABLE_AC3_EAC3_AUDIO_DEMUXING) |
378 case kCodecAC3: | |
379 case kCodecEAC3: | |
380 #endif | |
381 break; | |
382 default: | |
383 // Verify that AudioConfig.bits_per_channel was calculated correctly for | |
ddorwin
2016/01/08 20:15:42
nit: Since we're not doing anything else, it might
servolk
2016/01/08 21:55:48
Done.
| |
384 // codecs that have |sample_fmt| set by FFmpeg. | |
385 DCHECK_EQ(av_get_bytes_per_sample(codec_context->sample_fmt) * 8, | |
386 config->bits_per_channel()); | |
387 break; | |
359 } | 388 } |
360 | 389 |
361 return true; | 390 return true; |
362 } | 391 } |
363 | 392 |
364 bool AVStreamToAudioDecoderConfig(const AVStream* stream, | 393 bool AVStreamToAudioDecoderConfig(const AVStream* stream, |
365 AudioDecoderConfig* config) { | 394 AudioDecoderConfig* config) { |
366 bool is_encrypted = false; | 395 bool is_encrypted = false; |
367 AVDictionaryEntry* key = | 396 AVDictionaryEntry* key = |
368 av_dict_get(stream->metadata, "enc_key_id", nullptr, 0); | 397 av_dict_get(stream->metadata, "enc_key_id", nullptr, 0); |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
690 } | 719 } |
691 | 720 |
692 int32_t HashCodecName(const char* codec_name) { | 721 int32_t HashCodecName(const char* codec_name) { |
693 // Use the first 32-bits from the SHA1 hash as the identifier. | 722 // Use the first 32-bits from the SHA1 hash as the identifier. |
694 int32_t hash; | 723 int32_t hash; |
695 memcpy(&hash, base::SHA1HashString(codec_name).substr(0, 4).c_str(), 4); | 724 memcpy(&hash, base::SHA1HashString(codec_name).substr(0, 4).c_str(), 4); |
696 return hash; | 725 return hash; |
697 } | 726 } |
698 | 727 |
699 } // namespace media | 728 } // namespace media |
OLD | NEW |