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
3 // found in the LICENSE file.
7 #include "base/containers/hash_tables.h"
8 #include "base/lazy_instance.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "build/build_config.h"
13 #include "media/base/mime_util.h"
15 #if defined(OS_ANDROID)
16 #include "base/android/build_info.h"
21 // Singleton utility class for mime types.
44 bool IsSupportedMediaMimeType(const std::string
& mime_type
) const;
46 bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
) const;
48 void ParseCodecString(const std::string
& codecs
,
49 std::vector
<std::string
>* codecs_out
,
52 bool IsStrictMediaMimeType(const std::string
& mime_type
) const;
53 SupportsType
IsSupportedStrictMediaMimeType(
54 const std::string
& mime_type
,
55 const std::vector
<std::string
>& codecs
) const;
57 void RemoveProprietaryMediaTypesAndCodecsForTests();
60 friend struct base::DefaultLazyInstanceTraits
<MimeUtil
>;
62 typedef base::hash_set
<int> CodecSet
;
63 typedef std::map
<std::string
, CodecSet
> StrictMappings
;
65 CodecEntry() : codec(INVALID_CODEC
), is_ambiguous(true) {}
66 CodecEntry(Codec c
, bool ambiguous
) : codec(c
), is_ambiguous(ambiguous
) {}
70 typedef std::map
<std::string
, CodecEntry
> StringToCodecMappings
;
74 // For faster lookup, keep hash sets.
75 void InitializeMimeTypeMaps();
77 // Returns IsSupported if all codec IDs in |codecs| are unambiguous
78 // and are supported by the platform. MayBeSupported is returned if
79 // at least one codec ID in |codecs| is ambiguous but all the codecs
80 // are supported by the platform. IsNotSupported is returned if at
81 // least one codec ID is not supported by the platform.
82 SupportsType
AreSupportedCodecs(
83 const CodecSet
& supported_codecs
,
84 const std::vector
<std::string
>& codecs
) const;
86 // Converts a codec ID into an Codec enum value and indicates
87 // whether the conversion was ambiguous.
88 // Returns true if this method was able to map |codec_id| to a specific
89 // Codec enum value. |codec| and |is_ambiguous| are only valid if true
90 // is returned. Otherwise their value is undefined after the call.
91 // |is_ambiguous| is true if |codec_id| did not have enough information to
92 // unambiguously determine the proper Codec enum value. If |is_ambiguous|
93 // is true |codec| contains the best guess for the intended Codec enum value.
94 bool StringToCodec(const std::string
& codec_id
,
96 bool* is_ambiguous
) const;
98 // Returns true if |codec| is supported by the platform.
99 // Note: This method will return false if the platform supports proprietary
100 // codecs but |allow_proprietary_codecs_| is set to false.
101 bool IsCodecSupported(Codec codec
) const;
103 // Returns true if |codec| refers to a proprietary codec.
104 bool IsCodecProprietary(Codec codec
) const;
106 // Returns true and sets |*default_codec| if |mime_type| has a default codec
107 // associated with it. Returns false otherwise and the value of
108 // |*default_codec| is undefined.
109 bool GetDefaultCodecLowerCase(const std::string
& mime_type_lower_case
,
110 Codec
* default_codec
) const;
112 // Returns true if |mime_type_lower_case| has a default codec associated with
113 // it and IsCodecSupported() returns true for that particular codec.
114 bool IsDefaultCodecSupportedLowerCase(
115 const std::string
& mime_type_lower_case
) const;
117 using MimeTypes
= base::hash_set
<std::string
>;
118 MimeTypes media_map_
;
120 // A map of mime_types and hash map of the supported codecs for the mime_type.
121 StrictMappings strict_format_map_
;
123 // Keeps track of whether proprietary codec support should be
124 // advertised to callers.
125 bool allow_proprietary_codecs_
;
127 // Lookup table for string compare based string -> Codec mappings.
128 StringToCodecMappings string_to_codec_map_
;
130 DISALLOW_COPY_AND_ASSIGN(MimeUtil
);
133 // This variable is Leaky because it is accessed from WorkerPool threads.
134 static base::LazyInstance
<MimeUtil
>::Leaky g_media_mime_util
=
135 LAZY_INSTANCE_INITIALIZER
;
138 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type
139 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php
140 // This set of codecs is supported by all variations of Chromium.
141 static const char* const common_media_types
[] = {
145 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora.
157 #if defined(OS_ANDROID)
159 "application/vnd.apple.mpegurl",
160 "application/x-mpegurl",
164 // List of proprietary types only supported by Google Chrome.
165 static const char* const proprietary_media_types
[] = {
178 #if defined(ENABLE_MPEG2TS_STREAM_PARSER)
184 #if defined(OS_ANDROID)
185 static bool IsCodecSupportedOnAndroid(MimeUtil::Codec codec
) {
187 case MimeUtil::INVALID_CODEC
:
192 case MimeUtil::MPEG4_AAC_LC
:
193 case MimeUtil::MPEG4_AAC_SBR_v1
:
194 case MimeUtil::MPEG4_AAC_SBR_PS_v2
:
195 case MimeUtil::H264_BASELINE
:
196 case MimeUtil::H264_MAIN
:
197 case MimeUtil::H264_HIGH
:
199 case MimeUtil::VORBIS
:
202 case MimeUtil::MPEG2_AAC_LC
:
203 case MimeUtil::MPEG2_AAC_MAIN
:
204 case MimeUtil::MPEG2_AAC_SSR
:
205 // MPEG-2 variants of AAC are not supported on Android.
209 // VP9 is supported only in KitKat+ (API Level 19).
210 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
213 // Opus is supported only in Lollipop+ (API Level 21).
214 return base::android::BuildInfo::GetInstance()->sdk_int() >= 21;
216 case MimeUtil::THEORA
:
224 struct MediaFormatStrict
{
225 const char* const mime_type
;
226 const char* const codecs_list
;
229 // Following is the list of RFC 6381 compliant codecs:
230 // mp4a.66 - MPEG-2 AAC MAIN
231 // mp4a.67 - MPEG-2 AAC LC
232 // mp4a.68 - MPEG-2 AAC SSR
233 // mp4a.69 - MPEG-2 extension to MPEG-1
234 // mp4a.6B - MPEG-1 audio
235 // mp4a.40.2 - MPEG-4 AAC LC
236 // mp4a.40.02 - MPEG-4 AAC LC (leading 0 in aud-oti for compatibility)
237 // mp4a.40.5 - MPEG-4 HE-AAC v1 (AAC LC + SBR)
238 // mp4a.40.05 - MPEG-4 HE-AAC v1 (AAC LC + SBR) (leading 0 in aud-oti for
240 // mp4a.40.29 - MPEG-4 HE-AAC v2 (AAC LC + SBR + PS)
242 // avc1.42E0xx - H.264 Baseline
243 // avc1.4D40xx - H.264 Main
244 // avc1.6400xx - H.264 High
245 static const char kMP4AudioCodecsExpression
[] =
246 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
247 "mp4a.40.05,mp4a.40.29";
248 static const char kMP4VideoCodecsExpression
[] =
249 // This is not a complete list of supported avc1 codecs. It is simply used
250 // to register support for the corresponding Codec enum. Instead of using
251 // strings in these three arrays, we should use the Codec enum values.
252 // This will avoid confusion and unnecessary parsing at runtime.
253 // kUnambiguousCodecStringMap/kAmbiguousCodecStringMap should be the only
254 // mapping from strings to codecs. See crbug.com/461009.
255 "avc1.42E00A,avc1.4D400A,avc1.64000A,"
256 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
257 "mp4a.40.05,mp4a.40.29";
259 // These containers are also included in
260 // common_media_types/proprietary_media_types. See crbug.com/461012.
261 static const MediaFormatStrict format_codec_mappings
[] = {
262 {"video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0"},
263 {"audio/webm", "opus,vorbis"},
265 {"audio/x-wav", "1"},
266 // Android does not support Opus in Ogg container.
267 #if defined(OS_ANDROID)
268 {"video/ogg", "theora,vorbis"},
269 {"audio/ogg", "vorbis"},
270 {"application/ogg", "theora,vorbis"},
272 {"video/ogg", "opus,theora,vorbis"},
273 {"audio/ogg", "opus,vorbis"},
274 {"application/ogg", "opus,theora,vorbis"},
276 {"audio/mpeg", "mp3"},
279 {"audio/mp4", kMP4AudioCodecsExpression
},
280 {"audio/x-m4a", kMP4AudioCodecsExpression
},
281 {"video/mp4", kMP4VideoCodecsExpression
},
282 {"video/x-m4v", kMP4VideoCodecsExpression
},
283 {"application/x-mpegurl", kMP4VideoCodecsExpression
},
284 {"application/vnd.apple.mpegurl", kMP4VideoCodecsExpression
}};
286 struct CodecIDMappings
{
287 const char* const codec_id
;
288 MimeUtil::Codec codec
;
291 // List of codec IDs that provide enough information to determine the
292 // codec and profile being requested.
294 // The "mp4a" strings come from RFC 6381.
295 static const CodecIDMappings kUnambiguousCodecStringMap
[] = {
296 {"1", MimeUtil::PCM
}, // We only allow this for WAV so it isn't ambiguous.
297 // avc1/avc3.XXXXXX may be unambiguous; handled by ParseH264CodecID().
298 {"mp3", MimeUtil::MP3
},
299 {"mp4a.66", MimeUtil::MPEG2_AAC_MAIN
},
300 {"mp4a.67", MimeUtil::MPEG2_AAC_LC
},
301 {"mp4a.68", MimeUtil::MPEG2_AAC_SSR
},
302 {"mp4a.69", MimeUtil::MP3
},
303 {"mp4a.6B", MimeUtil::MP3
},
304 {"mp4a.40.2", MimeUtil::MPEG4_AAC_LC
},
305 {"mp4a.40.02", MimeUtil::MPEG4_AAC_LC
},
306 {"mp4a.40.5", MimeUtil::MPEG4_AAC_SBR_v1
},
307 {"mp4a.40.05", MimeUtil::MPEG4_AAC_SBR_v1
},
308 {"mp4a.40.29", MimeUtil::MPEG4_AAC_SBR_PS_v2
},
309 {"vorbis", MimeUtil::VORBIS
},
310 {"opus", MimeUtil::OPUS
},
311 {"vp8", MimeUtil::VP8
},
312 {"vp8.0", MimeUtil::VP8
},
313 {"vp9", MimeUtil::VP9
},
314 {"vp9.0", MimeUtil::VP9
},
315 {"theora", MimeUtil::THEORA
}};
317 // List of codec IDs that are ambiguous and don't provide
318 // enough information to determine the codec and profile.
319 // The codec in these entries indicate the codec and profile
320 // we assume the user is trying to indicate.
321 static const CodecIDMappings kAmbiguousCodecStringMap
[] = {
322 {"mp4a.40", MimeUtil::MPEG4_AAC_LC
},
323 {"avc1", MimeUtil::H264_BASELINE
},
324 {"avc3", MimeUtil::H264_BASELINE
},
325 // avc1/avc3.XXXXXX may be ambiguous; handled by ParseH264CodecID().
328 MimeUtil::MimeUtil() : allow_proprietary_codecs_(false) {
329 InitializeMimeTypeMaps();
332 SupportsType
MimeUtil::AreSupportedCodecs(
333 const CodecSet
& supported_codecs
,
334 const std::vector
<std::string
>& codecs
) const {
335 DCHECK(!supported_codecs
.empty());
336 DCHECK(!codecs
.empty());
338 SupportsType result
= IsSupported
;
339 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
340 bool is_ambiguous
= true;
341 Codec codec
= INVALID_CODEC
;
342 if (!StringToCodec(codecs
[i
], &codec
, &is_ambiguous
))
343 return IsNotSupported
;
345 if (!IsCodecSupported(codec
) ||
346 supported_codecs
.find(codec
) == supported_codecs
.end()) {
347 return IsNotSupported
;
351 result
= MayBeSupported
;
357 void MimeUtil::InitializeMimeTypeMaps() {
358 // Initialize the supported media types.
359 for (size_t i
= 0; i
< arraysize(common_media_types
); ++i
)
360 media_map_
.insert(common_media_types
[i
]);
361 #if defined(USE_PROPRIETARY_CODECS)
362 allow_proprietary_codecs_
= true;
364 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
365 media_map_
.insert(proprietary_media_types
[i
]);
368 for (size_t i
= 0; i
< arraysize(kUnambiguousCodecStringMap
); ++i
) {
369 string_to_codec_map_
[kUnambiguousCodecStringMap
[i
].codec_id
] =
370 CodecEntry(kUnambiguousCodecStringMap
[i
].codec
, false);
373 for (size_t i
= 0; i
< arraysize(kAmbiguousCodecStringMap
); ++i
) {
374 string_to_codec_map_
[kAmbiguousCodecStringMap
[i
].codec_id
] =
375 CodecEntry(kAmbiguousCodecStringMap
[i
].codec
, true);
378 // Initialize the strict supported media types.
379 for (size_t i
= 0; i
< arraysize(format_codec_mappings
); ++i
) {
380 std::vector
<std::string
> mime_type_codecs
;
381 ParseCodecString(format_codec_mappings
[i
].codecs_list
,
386 for (size_t j
= 0; j
< mime_type_codecs
.size(); ++j
) {
387 Codec codec
= INVALID_CODEC
;
388 bool is_ambiguous
= true;
389 CHECK(StringToCodec(mime_type_codecs
[j
], &codec
, &is_ambiguous
));
390 DCHECK(!is_ambiguous
);
391 codecs
.insert(codec
);
394 strict_format_map_
[format_codec_mappings
[i
].mime_type
] = codecs
;
398 bool MimeUtil::IsSupportedMediaMimeType(const std::string
& mime_type
) const {
399 return media_map_
.find(base::StringToLowerASCII(mime_type
)) !=
404 bool MimeUtil::AreSupportedMediaCodecs(
405 const std::vector
<std::string
>& codecs
) const {
406 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
407 Codec codec
= INVALID_CODEC
;
408 bool is_ambiguous
= true;
409 if (!StringToCodec(codecs
[i
], &codec
, &is_ambiguous
) ||
410 !IsCodecSupported(codec
)) {
417 void MimeUtil::ParseCodecString(const std::string
& codecs
,
418 std::vector
<std::string
>* codecs_out
,
420 std::string no_quote_codecs
;
421 base::TrimString(codecs
, "\"", &no_quote_codecs
);
422 base::SplitString(no_quote_codecs
, ',', codecs_out
);
427 // Strip everything past the first '.'
428 for (std::vector
<std::string
>::iterator it
= codecs_out
->begin();
429 it
!= codecs_out
->end();
431 size_t found
= it
->find_first_of('.');
432 if (found
!= std::string::npos
)
437 bool MimeUtil::IsStrictMediaMimeType(const std::string
& mime_type
) const {
438 return strict_format_map_
.find(base::StringToLowerASCII(mime_type
)) !=
439 strict_format_map_
.end();
442 SupportsType
MimeUtil::IsSupportedStrictMediaMimeType(
443 const std::string
& mime_type
,
444 const std::vector
<std::string
>& codecs
) const {
445 const std::string mime_type_lower_case
= base::StringToLowerASCII(mime_type
);
446 StrictMappings::const_iterator it_strict_map
=
447 strict_format_map_
.find(mime_type_lower_case
);
448 if (it_strict_map
== strict_format_map_
.end())
449 return codecs
.empty() ? MayBeSupported
: IsNotSupported
;
451 if (it_strict_map
->second
.empty()) {
452 // We get here if the mimetype does not expect a codecs parameter.
453 return (codecs
.empty() &&
454 IsDefaultCodecSupportedLowerCase(mime_type_lower_case
))
459 if (codecs
.empty()) {
460 // We get here if the mimetype expects to get a codecs parameter,
461 // but didn't get one. If |mime_type_lower_case| does not have a default
462 // codec the best we can do is say "maybe" because we don't have enough
464 Codec default_codec
= INVALID_CODEC
;
465 if (!GetDefaultCodecLowerCase(mime_type_lower_case
, &default_codec
))
466 return MayBeSupported
;
468 return IsCodecSupported(default_codec
) ? IsSupported
: IsNotSupported
;
471 return AreSupportedCodecs(it_strict_map
->second
, codecs
);
474 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
475 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
476 media_map_
.erase(proprietary_media_types
[i
]);
477 allow_proprietary_codecs_
= false;
480 // Returns true iff |profile_str| conforms to hex string "42y0", where y is one
481 // of [8..F]. Requiring constraint_set0_flag be set and profile_idc be 0x42 is
482 // taken from ISO-14496-10 7.3.2.1, 7.4.2.1, and Annex A.2.1.
484 // |profile_str| is the first four characters of the H.264 suffix string
485 // (ignoring the last 2 characters of the full 6 character suffix that are
486 // level_idc). From 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 : required to be true 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.
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 uint32 constraint_set_bits
;
501 if (profile_str
.size() != 4 ||
502 profile_str
[0] != '4' ||
503 profile_str
[1] != '2' ||
504 profile_str
[3] != '0' ||
505 !base::HexStringToUInt(base::StringPiece(profile_str
.c_str() + 2, 1),
506 &constraint_set_bits
)) {
510 return constraint_set_bits
>= 8;
513 static bool IsValidH264Level(const std::string
& level_str
) {
515 if (level_str
.size() != 2 || !base::HexStringToUInt(level_str
, &level
))
518 // Valid levels taken from Table A-1 in ISO-14496-10.
519 // Essentially |level_str| is toHex(10 * level).
520 return ((level
>= 10 && level
<= 13) ||
521 (level
>= 20 && level
<= 22) ||
522 (level
>= 30 && level
<= 32) ||
523 (level
>= 40 && level
<= 42) ||
524 (level
>= 50 && level
<= 51));
527 // Handle parsing H.264 codec IDs as outlined in RFC 6381 and ISO-14496-10.
528 // avc1.42y0xx, y >= 8 - H.264 Baseline
529 // avc1.4D40xx - H.264 Main
530 // avc1.6400xx - H.264 High
532 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to
533 // signal H.264 Baseline. For example, the idc_level, profile_idc and
534 // constraint_set3_flag pieces may explicitly require decoder to conform to
535 // baseline profile at the specified level (see Annex A and constraint_set0 in
537 static bool ParseH264CodecID(const std::string
& codec_id
,
538 MimeUtil::Codec
* codec
,
539 bool* is_ambiguous
) {
540 // Make sure we have avc1.xxxxxx or avc3.xxxxxx
541 if (codec_id
.size() != 11 ||
542 (!base::StartsWithASCII(codec_id
, "avc1.", true) &&
543 !base::StartsWithASCII(codec_id
, "avc3.", true))) {
547 std::string profile
= base::StringToUpperASCII(codec_id
.substr(5, 4));
548 if (IsValidH264BaselineProfile(profile
)) {
549 *codec
= MimeUtil::H264_BASELINE
;
550 } else if (profile
== "4D40") {
551 *codec
= MimeUtil::H264_MAIN
;
552 } else if (profile
== "6400") {
553 *codec
= MimeUtil::H264_HIGH
;
555 *codec
= MimeUtil::H264_BASELINE
;
556 *is_ambiguous
= true;
561 !IsValidH264Level(base::StringToUpperASCII(codec_id
.substr(9)));
565 bool MimeUtil::StringToCodec(const std::string
& codec_id
,
567 bool* is_ambiguous
) const {
568 StringToCodecMappings::const_iterator itr
=
569 string_to_codec_map_
.find(codec_id
);
570 if (itr
!= string_to_codec_map_
.end()) {
571 *codec
= itr
->second
.codec
;
572 *is_ambiguous
= itr
->second
.is_ambiguous
;
576 // If |codec_id| is not in |string_to_codec_map_|, then we assume that it is
577 // an H.264 codec ID because currently those are the only ones that can't be
578 // stored in the |string_to_codec_map_| and require parsing.
579 return ParseH264CodecID(codec_id
, codec
, is_ambiguous
);
582 bool MimeUtil::IsCodecSupported(Codec codec
) const {
583 DCHECK_NE(codec
, INVALID_CODEC
);
585 #if defined(OS_ANDROID)
586 if (!IsCodecSupportedOnAndroid(codec
))
590 return allow_proprietary_codecs_
|| !IsCodecProprietary(codec
);
593 bool MimeUtil::IsCodecProprietary(Codec codec
) const {
601 case MPEG4_AAC_SBR_v1
:
602 case MPEG4_AAC_SBR_PS_v2
:
620 bool MimeUtil::GetDefaultCodecLowerCase(const std::string
& mime_type_lower_case
,
621 Codec
* default_codec
) const {
622 if (mime_type_lower_case
== "audio/mpeg" ||
623 mime_type_lower_case
== "audio/mp3" ||
624 mime_type_lower_case
== "audio/x-mp3") {
625 *default_codec
= MimeUtil::MP3
;
632 bool MimeUtil::IsDefaultCodecSupportedLowerCase(
633 const std::string
& mime_type_lower_case
) const {
634 Codec default_codec
= Codec::INVALID_CODEC
;
635 if (!GetDefaultCodecLowerCase(mime_type_lower_case
, &default_codec
))
637 return IsCodecSupported(default_codec
);
640 bool IsSupportedMediaMimeType(const std::string
& mime_type
) {
641 return g_media_mime_util
.Get().IsSupportedMediaMimeType(mime_type
);
644 bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
) {
645 return g_media_mime_util
.Get().AreSupportedMediaCodecs(codecs
);
648 bool IsStrictMediaMimeType(const std::string
& mime_type
) {
649 return g_media_mime_util
.Get().IsStrictMediaMimeType(mime_type
);
652 SupportsType
IsSupportedStrictMediaMimeType(
653 const std::string
& mime_type
,
654 const std::vector
<std::string
>& codecs
) {
655 return g_media_mime_util
.Get().IsSupportedStrictMediaMimeType(
659 void ParseCodecString(const std::string
& codecs
,
660 std::vector
<std::string
>* codecs_out
,
662 g_media_mime_util
.Get().ParseCodecString(codecs
, codecs_out
, strip
);
665 void RemoveProprietaryMediaTypesAndCodecsForTests() {
666 g_media_mime_util
.Get().RemoveProprietaryMediaTypesAndCodecsForTests();