| 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/audio_decoder_config.h" | 14 #include "media/base/audio_decoder_config.h" |
| 15 #include "media/base/decoder_buffer.h" | 15 #include "media/base/decoder_buffer.h" |
| 16 #include "media/base/encryption_scheme.h" | |
| 17 #include "media/base/media_util.h" | |
| 18 #include "media/base/video_decoder_config.h" | 16 #include "media/base/video_decoder_config.h" |
| 19 #include "media/base/video_util.h" | 17 #include "media/base/video_util.h" |
| 20 #include "media/media_features.h" | 18 #include "media/media_features.h" |
| 21 | 19 |
| 22 namespace media { | 20 namespace media { |
| 23 | 21 |
| 24 namespace { | |
| 25 | |
| 26 EncryptionScheme GetEncryptionScheme(const AVStream* stream) { | |
| 27 AVDictionaryEntry* key = | |
| 28 av_dict_get(stream->metadata, "enc_key_id", nullptr, 0); | |
| 29 return key ? AesCtrEncryptionScheme() : Unencrypted(); | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 // Why FF_INPUT_BUFFER_PADDING_SIZE? FFmpeg assumes all input buffers are | 22 // Why FF_INPUT_BUFFER_PADDING_SIZE? FFmpeg assumes all input buffers are |
| 35 // padded. Check here to ensure FFmpeg only receives data padded to its | 23 // padded. Check here to ensure FFmpeg only receives data padded to its |
| 36 // specifications. | 24 // specifications. |
| 37 static_assert(DecoderBuffer::kPaddingSize >= FF_INPUT_BUFFER_PADDING_SIZE, | 25 static_assert(DecoderBuffer::kPaddingSize >= FF_INPUT_BUFFER_PADDING_SIZE, |
| 38 "DecoderBuffer padding size does not fit ffmpeg requirement"); | 26 "DecoderBuffer padding size does not fit ffmpeg requirement"); |
| 39 | 27 |
| 40 // Alignment requirement by FFmpeg for input and output buffers. This need to | 28 // Alignment requirement by FFmpeg for input and output buffers. This need to |
| 41 // be updated to match FFmpeg when it changes. | 29 // be updated to match FFmpeg when it changes. |
| 42 #if defined(ARCH_CPU_ARM_FAMILY) | 30 #if defined(ARCH_CPU_ARM_FAMILY) |
| 43 static const int kFFmpegBufferAddressAlignment = 16; | 31 static const int kFFmpegBufferAddressAlignment = 16; |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 case kSampleFormatPlanarS16: | 296 case kSampleFormatPlanarS16: |
| 309 return AV_SAMPLE_FMT_S16P; | 297 return AV_SAMPLE_FMT_S16P; |
| 310 case kSampleFormatPlanarF32: | 298 case kSampleFormatPlanarF32: |
| 311 return AV_SAMPLE_FMT_FLTP; | 299 return AV_SAMPLE_FMT_FLTP; |
| 312 default: | 300 default: |
| 313 DVLOG(1) << "Unknown SampleFormat: " << sample_format; | 301 DVLOG(1) << "Unknown SampleFormat: " << sample_format; |
| 314 } | 302 } |
| 315 return AV_SAMPLE_FMT_NONE; | 303 return AV_SAMPLE_FMT_NONE; |
| 316 } | 304 } |
| 317 | 305 |
| 318 bool AVCodecContextToAudioDecoderConfig( | 306 bool AVCodecContextToAudioDecoderConfig(const AVCodecContext* codec_context, |
| 319 const AVCodecContext* codec_context, | 307 bool is_encrypted, |
| 320 const EncryptionScheme& encryption_scheme, | 308 AudioDecoderConfig* config) { |
| 321 AudioDecoderConfig* config) { | |
| 322 DCHECK_EQ(codec_context->codec_type, AVMEDIA_TYPE_AUDIO); | 309 DCHECK_EQ(codec_context->codec_type, AVMEDIA_TYPE_AUDIO); |
| 323 | 310 |
| 324 AudioCodec codec = CodecIDToAudioCodec(codec_context->codec_id); | 311 AudioCodec codec = CodecIDToAudioCodec(codec_context->codec_id); |
| 325 | 312 |
| 326 SampleFormat sample_format = AVSampleFormatToSampleFormat( | 313 SampleFormat sample_format = AVSampleFormatToSampleFormat( |
| 327 codec_context->sample_fmt, codec_context->codec_id); | 314 codec_context->sample_fmt, codec_context->codec_id); |
| 328 | 315 |
| 329 ChannelLayout channel_layout = ChannelLayoutToChromeChannelLayout( | 316 ChannelLayout channel_layout = ChannelLayoutToChromeChannelLayout( |
| 330 codec_context->channel_layout, codec_context->channels); | 317 codec_context->channel_layout, codec_context->channels); |
| 331 | 318 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 << " extra data cannot have size of " | 364 << " extra data cannot have size of " |
| 378 << codec_context->extradata_size << "."; | 365 << codec_context->extradata_size << "."; |
| 379 return false; | 366 return false; |
| 380 } | 367 } |
| 381 | 368 |
| 382 std::vector<uint8_t> extra_data; | 369 std::vector<uint8_t> extra_data; |
| 383 if (codec_context->extradata_size > 0) { | 370 if (codec_context->extradata_size > 0) { |
| 384 extra_data.assign(codec_context->extradata, | 371 extra_data.assign(codec_context->extradata, |
| 385 codec_context->extradata + codec_context->extradata_size); | 372 codec_context->extradata + codec_context->extradata_size); |
| 386 } | 373 } |
| 387 | 374 config->Initialize(codec, |
| 388 config->Initialize(codec, sample_format, channel_layout, sample_rate, | 375 sample_format, |
| 389 extra_data, encryption_scheme, seek_preroll, | 376 channel_layout, |
| 377 sample_rate, |
| 378 extra_data, |
| 379 is_encrypted, |
| 380 seek_preroll, |
| 390 codec_context->delay); | 381 codec_context->delay); |
| 391 | 382 |
| 392 // Verify that AudioConfig.bits_per_channel was calculated correctly for | 383 // Verify that AudioConfig.bits_per_channel was calculated correctly for |
| 393 // codecs that have |sample_fmt| set by FFmpeg. | 384 // codecs that have |sample_fmt| set by FFmpeg. |
| 394 switch (codec) { | 385 switch (codec) { |
| 395 case kCodecOpus: | 386 case kCodecOpus: |
| 396 #if BUILDFLAG(ENABLE_AC3_EAC3_AUDIO_DEMUXING) | 387 #if BUILDFLAG(ENABLE_AC3_EAC3_AUDIO_DEMUXING) |
| 397 case kCodecAC3: | 388 case kCodecAC3: |
| 398 case kCodecEAC3: | 389 case kCodecEAC3: |
| 399 #endif | 390 #endif |
| 400 break; | 391 break; |
| 401 default: | 392 default: |
| 402 DCHECK_EQ(av_get_bytes_per_sample(codec_context->sample_fmt) * 8, | 393 DCHECK_EQ(av_get_bytes_per_sample(codec_context->sample_fmt) * 8, |
| 403 config->bits_per_channel()); | 394 config->bits_per_channel()); |
| 404 break; | 395 break; |
| 405 } | 396 } |
| 406 | 397 |
| 407 return true; | 398 return true; |
| 408 } | 399 } |
| 409 | 400 |
| 410 bool AVStreamToAudioDecoderConfig(const AVStream* stream, | 401 bool AVStreamToAudioDecoderConfig(const AVStream* stream, |
| 411 AudioDecoderConfig* config) { | 402 AudioDecoderConfig* config) { |
| 412 return AVCodecContextToAudioDecoderConfig( | 403 bool is_encrypted = false; |
| 413 stream->codec, GetEncryptionScheme(stream), config); | 404 AVDictionaryEntry* key = |
| 405 av_dict_get(stream->metadata, "enc_key_id", nullptr, 0); |
| 406 if (key) |
| 407 is_encrypted = true; |
| 408 return AVCodecContextToAudioDecoderConfig(stream->codec, is_encrypted, |
| 409 config); |
| 414 } | 410 } |
| 415 | 411 |
| 416 void AudioDecoderConfigToAVCodecContext(const AudioDecoderConfig& config, | 412 void AudioDecoderConfigToAVCodecContext(const AudioDecoderConfig& config, |
| 417 AVCodecContext* codec_context) { | 413 AVCodecContext* codec_context) { |
| 418 codec_context->codec_type = AVMEDIA_TYPE_AUDIO; | 414 codec_context->codec_type = AVMEDIA_TYPE_AUDIO; |
| 419 codec_context->codec_id = AudioCodecToCodecID(config.codec(), | 415 codec_context->codec_id = AudioCodecToCodecID(config.codec(), |
| 420 config.sample_format()); | 416 config.sample_format()); |
| 421 codec_context->sample_fmt = SampleFormatToAVSampleFormat( | 417 codec_context->sample_fmt = SampleFormatToAVSampleFormat( |
| 422 config.sample_format()); | 418 config.sample_format()); |
| 423 | 419 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 coded_size = visible_rect.size(); | 490 coded_size = visible_rect.size(); |
| 495 } | 491 } |
| 496 | 492 |
| 497 // Pad out |coded_size| for subsampled YUV formats. | 493 // Pad out |coded_size| for subsampled YUV formats. |
| 498 if (format != PIXEL_FORMAT_YV24) { | 494 if (format != PIXEL_FORMAT_YV24) { |
| 499 coded_size.set_width((coded_size.width() + 1) / 2 * 2); | 495 coded_size.set_width((coded_size.width() + 1) / 2 * 2); |
| 500 if (format != PIXEL_FORMAT_YV16) | 496 if (format != PIXEL_FORMAT_YV16) |
| 501 coded_size.set_height((coded_size.height() + 1) / 2 * 2); | 497 coded_size.set_height((coded_size.height() + 1) / 2 * 2); |
| 502 } | 498 } |
| 503 | 499 |
| 500 bool is_encrypted = false; |
| 501 AVDictionaryEntry* key = |
| 502 av_dict_get(stream->metadata, "enc_key_id", nullptr, 0); |
| 503 if (key) |
| 504 is_encrypted = true; |
| 505 |
| 504 AVDictionaryEntry* webm_alpha = | 506 AVDictionaryEntry* webm_alpha = |
| 505 av_dict_get(stream->metadata, "alpha_mode", nullptr, 0); | 507 av_dict_get(stream->metadata, "alpha_mode", nullptr, 0); |
| 506 if (webm_alpha && !strcmp(webm_alpha->value, "1")) { | 508 if (webm_alpha && !strcmp(webm_alpha->value, "1")) { |
| 507 format = PIXEL_FORMAT_YV12A; | 509 format = PIXEL_FORMAT_YV12A; |
| 508 } | 510 } |
| 509 | 511 |
| 510 // Prefer the color space found by libavcodec if available. | 512 // Prefer the color space found by libavcodec if available. |
| 511 ColorSpace color_space = AVColorSpaceToColorSpace(stream->codec->colorspace, | 513 ColorSpace color_space = AVColorSpaceToColorSpace(stream->codec->colorspace, |
| 512 stream->codec->color_range); | 514 stream->codec->color_range); |
| 513 if (color_space == COLOR_SPACE_UNSPECIFIED) { | 515 if (color_space == COLOR_SPACE_UNSPECIFIED) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 526 << stream->codec->extradata_size << "."; | 528 << stream->codec->extradata_size << "."; |
| 527 return false; | 529 return false; |
| 528 } | 530 } |
| 529 | 531 |
| 530 std::vector<uint8_t> extra_data; | 532 std::vector<uint8_t> extra_data; |
| 531 if (stream->codec->extradata_size > 0) { | 533 if (stream->codec->extradata_size > 0) { |
| 532 extra_data.assign(stream->codec->extradata, | 534 extra_data.assign(stream->codec->extradata, |
| 533 stream->codec->extradata + stream->codec->extradata_size); | 535 stream->codec->extradata + stream->codec->extradata_size); |
| 534 } | 536 } |
| 535 config->Initialize(codec, profile, format, color_space, coded_size, | 537 config->Initialize(codec, profile, format, color_space, coded_size, |
| 536 visible_rect, natural_size, extra_data, | 538 visible_rect, natural_size, extra_data, is_encrypted); |
| 537 GetEncryptionScheme(stream)); | |
| 538 return true; | 539 return true; |
| 539 } | 540 } |
| 540 | 541 |
| 541 void VideoDecoderConfigToAVCodecContext( | 542 void VideoDecoderConfigToAVCodecContext( |
| 542 const VideoDecoderConfig& config, | 543 const VideoDecoderConfig& config, |
| 543 AVCodecContext* codec_context) { | 544 AVCodecContext* codec_context) { |
| 544 codec_context->codec_type = AVMEDIA_TYPE_VIDEO; | 545 codec_context->codec_type = AVMEDIA_TYPE_VIDEO; |
| 545 codec_context->codec_id = VideoCodecToCodecID(config.codec()); | 546 codec_context->codec_id = VideoCodecToCodecID(config.codec()); |
| 546 codec_context->profile = VideoCodecProfileToProfileID(config.profile()); | 547 codec_context->profile = VideoCodecProfileToProfileID(config.profile()); |
| 547 codec_context->coded_width = config.coded_size().width(); | 548 codec_context->coded_width = config.coded_size().width(); |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 } | 760 } |
| 760 | 761 |
| 761 int32_t HashCodecName(const char* codec_name) { | 762 int32_t HashCodecName(const char* codec_name) { |
| 762 // Use the first 32-bits from the SHA1 hash as the identifier. | 763 // Use the first 32-bits from the SHA1 hash as the identifier. |
| 763 int32_t hash; | 764 int32_t hash; |
| 764 memcpy(&hash, base::SHA1HashString(codec_name).substr(0, 4).c_str(), 4); | 765 memcpy(&hash, base::SHA1HashString(codec_name).substr(0, 4).c_str(), 4); |
| 765 return hash; | 766 return hash; |
| 766 } | 767 } |
| 767 | 768 |
| 768 } // namespace media | 769 } // namespace media |
| OLD | NEW |