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 switch (codec) { |
313 // |codec_context->sample_fmt| is not set by FFmpeg because Opus decoding is | 320 case kCodecOpus: |
314 // not enabled in FFmpeg. It doesn't matter what value is set here, so long | 321 // |codec_context->sample_fmt| is not set by FFmpeg because Opus decoding |
315 // as it's valid, the true sample format is selected inside the decoder. | 322 // is not enabled in FFmpeg. It doesn't matter what value is set here, so |
316 sample_format = kSampleFormatF32; | 323 // long as it's valid, the true sample format is selected inside the |
| 324 // decoder. |
| 325 sample_format = kSampleFormatF32; |
317 | 326 |
318 // Always use 48kHz for OPUS. Technically we should match to the highest | 327 // 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 | 328 // 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 | 329 // 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: | 330 // rarely used for output. See the "Input Sample Rate" section of the |
322 // http://tools.ietf.org/html/draft-terriberry-oggopus-01#page-11 | 331 // spec: http://tools.ietf.org/html/draft-terriberry-oggopus-01#page-11 |
323 sample_rate = 48000; | 332 sample_rate = 48000; |
| 333 break; |
| 334 |
| 335 // For AC3/EAC3 we enable only demuxing, but not decoding, so FFmpeg does |
| 336 // not fill |sample_fmt|. |
| 337 case kCodecAC3: |
| 338 case kCodecEAC3: |
| 339 #if BUILDFLAG(ENABLE_AC3_EAC3_AUDIO_DEMUXING) |
| 340 // The spec for AC3/EAC3 audio is ETSI TS 102 366. According to sections |
| 341 // F.3.1 and F.5.1 in that spec the sample_format for AC3/EAC3 must be 16. |
| 342 sample_format = kSampleFormatS16; |
| 343 #else |
| 344 NOTREACHED(); |
| 345 #endif |
| 346 break; |
| 347 |
| 348 default: |
| 349 break; |
324 } | 350 } |
325 | 351 |
326 base::TimeDelta seek_preroll; | 352 base::TimeDelta seek_preroll; |
327 if (codec_context->seek_preroll > 0) { | 353 if (codec_context->seek_preroll > 0) { |
328 seek_preroll = base::TimeDelta::FromMicroseconds( | 354 seek_preroll = base::TimeDelta::FromMicroseconds( |
329 codec_context->seek_preroll * 1000000.0 / codec_context->sample_rate); | 355 codec_context->seek_preroll * 1000000.0 / codec_context->sample_rate); |
330 } | 356 } |
331 | 357 |
332 // AVStream occasionally has invalid extra data. See http://crbug.com/517163 | 358 // AVStream occasionally has invalid extra data. See http://crbug.com/517163 |
333 if ((codec_context->extradata_size == 0) != | 359 if ((codec_context->extradata_size == 0) != |
(...skipping 12 matching lines...) Expand all Loading... |
346 } | 372 } |
347 config->Initialize(codec, | 373 config->Initialize(codec, |
348 sample_format, | 374 sample_format, |
349 channel_layout, | 375 channel_layout, |
350 sample_rate, | 376 sample_rate, |
351 extra_data, | 377 extra_data, |
352 is_encrypted, | 378 is_encrypted, |
353 seek_preroll, | 379 seek_preroll, |
354 codec_context->delay); | 380 codec_context->delay); |
355 | 381 |
356 if (codec != kCodecOpus) { | 382 // Verify that AudioConfig.bits_per_channel was calculated correctly for |
357 DCHECK_EQ(av_get_bytes_per_sample(codec_context->sample_fmt) * 8, | 383 // codecs that have |sample_fmt| set by FFmpeg. |
358 config->bits_per_channel()); | 384 switch (codec) { |
| 385 case kCodecOpus: |
| 386 #if BUILDFLAG(ENABLE_AC3_EAC3_AUDIO_DEMUXING) |
| 387 case kCodecAC3: |
| 388 case kCodecEAC3: |
| 389 #endif |
| 390 break; |
| 391 default: |
| 392 DCHECK_EQ(av_get_bytes_per_sample(codec_context->sample_fmt) * 8, |
| 393 config->bits_per_channel()); |
| 394 break; |
359 } | 395 } |
360 | 396 |
361 return true; | 397 return true; |
362 } | 398 } |
363 | 399 |
364 bool AVStreamToAudioDecoderConfig(const AVStream* stream, | 400 bool AVStreamToAudioDecoderConfig(const AVStream* stream, |
365 AudioDecoderConfig* config) { | 401 AudioDecoderConfig* config) { |
366 bool is_encrypted = false; | 402 bool is_encrypted = false; |
367 AVDictionaryEntry* key = | 403 AVDictionaryEntry* key = |
368 av_dict_get(stream->metadata, "enc_key_id", nullptr, 0); | 404 av_dict_get(stream->metadata, "enc_key_id", nullptr, 0); |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 } | 726 } |
691 | 727 |
692 int32_t HashCodecName(const char* codec_name) { | 728 int32_t HashCodecName(const char* codec_name) { |
693 // Use the first 32-bits from the SHA1 hash as the identifier. | 729 // Use the first 32-bits from the SHA1 hash as the identifier. |
694 int32_t hash; | 730 int32_t hash; |
695 memcpy(&hash, base::SHA1HashString(codec_name).substr(0, 4).c_str(), 4); | 731 memcpy(&hash, base::SHA1HashString(codec_name).substr(0, 4).c_str(), 4); |
696 return hash; | 732 return hash; |
697 } | 733 } |
698 | 734 |
699 } // namespace media | 735 } // namespace media |
OLD | NEW |