OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 <map> | 5 #include <map> |
6 | 6 |
7 #include "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 | 473 |
474 return AreSupportedCodecs(it_strict_map->second, codecs); | 474 return AreSupportedCodecs(it_strict_map->second, codecs); |
475 } | 475 } |
476 | 476 |
477 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { | 477 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { |
478 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) | 478 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) |
479 media_map_.erase(proprietary_media_types[i]); | 479 media_map_.erase(proprietary_media_types[i]); |
480 allow_proprietary_codecs_ = false; | 480 allow_proprietary_codecs_ = false; |
481 } | 481 } |
482 | 482 |
483 // Returns true iff |profile_str| conforms to hex string "42y0". | |
484 // | |
485 // |profile_str| is the first four characters of the H.264 suffix string. From | |
486 // ISO-14496-10 7.3.2.1, it consists of: | |
487 // 8 bits: profile_idc; required to be 0x42 here. | |
488 // 1 bit: constraint_set0_flag; ignored here. | |
489 // 1 bit: constraint_set1_flag; ignored here. | |
490 // 1 bit: constraint_set2_flag; ignored here. | |
491 // 1 bit: constraint_set3_flag; ignored here. | |
492 // 4 bits: reserved; required to be 0 here. | |
493 // | |
494 // The spec indicates other ways, not implemented here, that a |profile_str| | |
495 // can indicate a baseline conforming decoder is sufficient for decode in Annex | |
496 // A.2.1: "[profile_idc not necessarily 0x42] with constraint_set0_flag set and | |
497 // in which level_idc and constraint_set3_flag represent a level less than or | |
498 // equal to the specified level." | |
499 static bool IsValidH264BaselineProfile(const std::string& profile_str) { | |
500 return (profile_str.size() == 4 && profile_str[0] == '4' && | |
501 profile_str[1] == '2' && base::IsHexDigit(profile_str[2]) && | |
502 profile_str[3] == '0'); | |
503 } | |
504 | |
505 static bool IsValidH264Level(const std::string& level_str) { | 483 static bool IsValidH264Level(const std::string& level_str) { |
506 uint32 level; | 484 uint32 level; |
507 if (level_str.size() != 2 || !base::HexStringToUInt(level_str, &level)) | 485 if (level_str.size() != 2 || !base::HexStringToUInt(level_str, &level)) |
508 return false; | 486 return false; |
509 | 487 |
510 // Valid levels taken from Table A-1 in ISO-14496-10. | 488 // Valid levels taken from Table A-1 in ISO-14496-10. |
511 // Essentially |level_str| is toHex(10 * level). | 489 // Essentially |level_str| is toHex(10 * level). |
512 return ((level >= 10 && level <= 13) || | 490 return ((level >= 10 && level <= 13) || |
513 (level >= 20 && level <= 22) || | 491 (level >= 20 && level <= 22) || |
514 (level >= 30 && level <= 32) || | 492 (level >= 30 && level <= 32) || |
515 (level >= 40 && level <= 42) || | 493 (level >= 40 && level <= 42) || |
516 (level >= 50 && level <= 51)); | 494 (level >= 50 && level <= 51)); |
517 } | 495 } |
518 | 496 |
519 // Handle parsing H.264 codec IDs as outlined in RFC 6381 and ISO-14496-10. | 497 // Handle parsing H.264 codec IDs as outlined in RFC 6381 and ISO-14496-10. |
520 // avc1.42y0xx, y >= 8 - H.264 Baseline | 498 // avc1.42x0yy - H.264 Baseline |
521 // avc1.4D40xx - H.264 Main | 499 // avc1.4Dx0yy - H.264 Main |
522 // avc1.6400xx - H.264 High | 500 // avc1.64x0yy - H.264 High |
523 // | 501 // |
524 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to | 502 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to |
525 // signal H.264 Baseline. For example, the idc_level, profile_idc and | 503 // signal H.264 Baseline. For example, the idc_level, profile_idc and |
526 // constraint_set3_flag pieces may explicitly require decoder to conform to | 504 // constraint_set3_flag pieces may explicitly require decoder to conform to |
527 // baseline profile at the specified level (see Annex A and constraint_set0 in | 505 // baseline profile at the specified level (see Annex A and constraint_set0 in |
528 // ISO-14496-10). | 506 // ISO-14496-10). |
529 static bool ParseH264CodecID(const std::string& codec_id, | 507 static bool ParseH264CodecID(const std::string& codec_id, |
530 MimeUtil::Codec* codec, | 508 MimeUtil::Codec* codec, |
531 bool* is_ambiguous) { | 509 bool* is_ambiguous) { |
532 // Make sure we have avc1.xxxxxx or avc3.xxxxxx | 510 // Make sure we have avc1.xxxxxx or avc3.xxxxxx |
533 if (codec_id.size() != 11 || | 511 if (codec_id.size() != 11 || |
534 (!base::StartsWith(codec_id, "avc1.", base::CompareCase::SENSITIVE) && | 512 (!base::StartsWith(codec_id, "avc1.", base::CompareCase::SENSITIVE) && |
535 !base::StartsWith(codec_id, "avc3.", base::CompareCase::SENSITIVE))) { | 513 !base::StartsWith(codec_id, "avc3.", base::CompareCase::SENSITIVE))) { |
536 return false; | 514 return false; |
537 } | 515 } |
538 | 516 |
539 std::string profile = base::ToUpperASCII(codec_id.substr(5, 4)); | 517 // Validate constraint flags and reserved bits. |
540 if (IsValidH264BaselineProfile(profile)) { | 518 if (!base::IsHexDigit(codec_id[7]) || codec_id[8] != '0') { |
541 *codec = MimeUtil::H264_BASELINE; | 519 *codec = MimeUtil::H264_BASELINE; |
542 } else if (profile == "4D40") { | 520 *is_ambiguous = true; |
| 521 return true; |
| 522 } |
| 523 |
| 524 // Extract the profile. |
| 525 std::string profile = base::ToUpperASCII(codec_id.substr(5, 2)); |
| 526 if (profile == "42") { |
| 527 *codec = MimeUtil::H264_BASELINE; |
| 528 } else if (profile == "4D") { |
543 *codec = MimeUtil::H264_MAIN; | 529 *codec = MimeUtil::H264_MAIN; |
544 } else if (profile == "6400") { | 530 } else if (profile == "64") { |
545 *codec = MimeUtil::H264_HIGH; | 531 *codec = MimeUtil::H264_HIGH; |
546 } else { | 532 } else { |
547 *codec = MimeUtil::H264_BASELINE; | 533 *codec = MimeUtil::H264_BASELINE; |
548 *is_ambiguous = true; | 534 *is_ambiguous = true; |
549 return true; | 535 return true; |
550 } | 536 } |
551 | 537 |
| 538 // Validate level. |
552 *is_ambiguous = !IsValidH264Level(base::ToUpperASCII(codec_id.substr(9))); | 539 *is_ambiguous = !IsValidH264Level(base::ToUpperASCII(codec_id.substr(9))); |
553 return true; | 540 return true; |
554 } | 541 } |
555 | 542 |
556 bool MimeUtil::StringToCodec(const std::string& codec_id, | 543 bool MimeUtil::StringToCodec(const std::string& codec_id, |
557 Codec* codec, | 544 Codec* codec, |
558 bool* is_ambiguous) const { | 545 bool* is_ambiguous) const { |
559 StringToCodecMappings::const_iterator itr = | 546 StringToCodecMappings::const_iterator itr = |
560 string_to_codec_map_.find(codec_id); | 547 string_to_codec_map_.find(codec_id); |
561 if (itr != string_to_codec_map_.end()) { | 548 if (itr != string_to_codec_map_.end()) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 std::vector<std::string>* codecs_out, | 638 std::vector<std::string>* codecs_out, |
652 const bool strip) { | 639 const bool strip) { |
653 g_media_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); | 640 g_media_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); |
654 } | 641 } |
655 | 642 |
656 void RemoveProprietaryMediaTypesAndCodecsForTests() { | 643 void RemoveProprietaryMediaTypesAndCodecsForTests() { |
657 g_media_mime_util.Get().RemoveProprietaryMediaTypesAndCodecsForTests(); | 644 g_media_mime_util.Get().RemoveProprietaryMediaTypesAndCodecsForTests(); |
658 } | 645 } |
659 | 646 |
660 } // namespace media | 647 } // namespace media |
OLD | NEW |