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::ToLowerASCII(mime_type
)) != media_map_
.end();
403 bool MimeUtil::AreSupportedMediaCodecs(
404 const std::vector
<std::string
>& codecs
) const {
405 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
406 Codec codec
= INVALID_CODEC
;
407 bool is_ambiguous
= true;
408 if (!StringToCodec(codecs
[i
], &codec
, &is_ambiguous
) ||
409 !IsCodecSupported(codec
)) {
416 void MimeUtil::ParseCodecString(const std::string
& codecs
,
417 std::vector
<std::string
>* codecs_out
,
419 *codecs_out
= base::SplitString(
420 base::TrimString(codecs
, "\"", base::TRIM_ALL
),
421 ",", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
423 // Convert empty or all-whitespace input to 0 results.
424 if (codecs_out
->size() == 1 && (*codecs_out
)[0].empty())
430 // Strip everything past the first '.'
431 for (std::vector
<std::string
>::iterator it
= codecs_out
->begin();
432 it
!= codecs_out
->end();
434 size_t found
= it
->find_first_of('.');
435 if (found
!= std::string::npos
)
440 bool MimeUtil::IsStrictMediaMimeType(const std::string
& mime_type
) const {
441 return strict_format_map_
.find(base::ToLowerASCII(mime_type
)) !=
442 strict_format_map_
.end();
445 SupportsType
MimeUtil::IsSupportedStrictMediaMimeType(
446 const std::string
& mime_type
,
447 const std::vector
<std::string
>& codecs
) const {
448 const std::string mime_type_lower_case
= base::ToLowerASCII(mime_type
);
449 StrictMappings::const_iterator it_strict_map
=
450 strict_format_map_
.find(mime_type_lower_case
);
451 if (it_strict_map
== strict_format_map_
.end())
452 return codecs
.empty() ? MayBeSupported
: IsNotSupported
;
454 if (it_strict_map
->second
.empty()) {
455 // We get here if the mimetype does not expect a codecs parameter.
456 return (codecs
.empty() &&
457 IsDefaultCodecSupportedLowerCase(mime_type_lower_case
))
462 if (codecs
.empty()) {
463 // We get here if the mimetype expects to get a codecs parameter,
464 // but didn't get one. If |mime_type_lower_case| does not have a default
465 // codec the best we can do is say "maybe" because we don't have enough
467 Codec default_codec
= INVALID_CODEC
;
468 if (!GetDefaultCodecLowerCase(mime_type_lower_case
, &default_codec
))
469 return MayBeSupported
;
471 return IsCodecSupported(default_codec
) ? IsSupported
: IsNotSupported
;
474 return AreSupportedCodecs(it_strict_map
->second
, codecs
);
477 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
478 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
479 media_map_
.erase(proprietary_media_types
[i
]);
480 allow_proprietary_codecs_
= false;
483 static bool IsValidH264Level(const std::string
& level_str
) {
485 if (level_str
.size() != 2 || !base::HexStringToUInt(level_str
, &level
))
488 // Valid levels taken from Table A-1 in ISO-14496-10.
489 // Essentially |level_str| is toHex(10 * level).
490 return ((level
>= 10 && level
<= 13) ||
491 (level
>= 20 && level
<= 22) ||
492 (level
>= 30 && level
<= 32) ||
493 (level
>= 40 && level
<= 42) ||
494 (level
>= 50 && level
<= 51));
497 // Handle parsing H.264 codec IDs as outlined in RFC 6381 and ISO-14496-10.
498 // avc1.42x0yy - H.264 Baseline
499 // avc1.4Dx0yy - H.264 Main
500 // avc1.64x0yy - H.264 High
502 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to
503 // signal H.264 Baseline. For example, the idc_level, profile_idc and
504 // constraint_set3_flag pieces may explicitly require decoder to conform to
505 // baseline profile at the specified level (see Annex A and constraint_set0 in
507 static bool ParseH264CodecID(const std::string
& codec_id
,
508 MimeUtil::Codec
* codec
,
509 bool* is_ambiguous
) {
510 // Make sure we have avc1.xxxxxx or avc3.xxxxxx
511 if (codec_id
.size() != 11 ||
512 (!base::StartsWith(codec_id
, "avc1.", base::CompareCase::SENSITIVE
) &&
513 !base::StartsWith(codec_id
, "avc3.", base::CompareCase::SENSITIVE
))) {
517 // Validate constraint flags and reserved bits.
518 if (!base::IsHexDigit(codec_id
[7]) || codec_id
[8] != '0') {
519 *codec
= MimeUtil::H264_BASELINE
;
520 *is_ambiguous
= true;
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") {
529 *codec
= MimeUtil::H264_MAIN
;
530 } else if (profile
== "64") {
531 *codec
= MimeUtil::H264_HIGH
;
533 *codec
= MimeUtil::H264_BASELINE
;
534 *is_ambiguous
= true;
539 *is_ambiguous
= !IsValidH264Level(base::ToUpperASCII(codec_id
.substr(9)));
543 bool MimeUtil::StringToCodec(const std::string
& codec_id
,
545 bool* is_ambiguous
) const {
546 StringToCodecMappings::const_iterator itr
=
547 string_to_codec_map_
.find(codec_id
);
548 if (itr
!= string_to_codec_map_
.end()) {
549 *codec
= itr
->second
.codec
;
550 *is_ambiguous
= itr
->second
.is_ambiguous
;
554 // If |codec_id| is not in |string_to_codec_map_|, then we assume that it is
555 // an H.264 codec ID because currently those are the only ones that can't be
556 // stored in the |string_to_codec_map_| and require parsing.
557 return ParseH264CodecID(codec_id
, codec
, is_ambiguous
);
560 bool MimeUtil::IsCodecSupported(Codec codec
) const {
561 DCHECK_NE(codec
, INVALID_CODEC
);
563 #if defined(OS_ANDROID)
564 if (!IsCodecSupportedOnAndroid(codec
))
568 return allow_proprietary_codecs_
|| !IsCodecProprietary(codec
);
571 bool MimeUtil::IsCodecProprietary(Codec codec
) const {
579 case MPEG4_AAC_SBR_v1
:
580 case MPEG4_AAC_SBR_PS_v2
:
598 bool MimeUtil::GetDefaultCodecLowerCase(const std::string
& mime_type_lower_case
,
599 Codec
* default_codec
) const {
600 if (mime_type_lower_case
== "audio/mpeg" ||
601 mime_type_lower_case
== "audio/mp3" ||
602 mime_type_lower_case
== "audio/x-mp3") {
603 *default_codec
= MimeUtil::MP3
;
610 bool MimeUtil::IsDefaultCodecSupportedLowerCase(
611 const std::string
& mime_type_lower_case
) const {
612 Codec default_codec
= Codec::INVALID_CODEC
;
613 if (!GetDefaultCodecLowerCase(mime_type_lower_case
, &default_codec
))
615 return IsCodecSupported(default_codec
);
618 bool IsSupportedMediaMimeType(const std::string
& mime_type
) {
619 return g_media_mime_util
.Get().IsSupportedMediaMimeType(mime_type
);
622 bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
) {
623 return g_media_mime_util
.Get().AreSupportedMediaCodecs(codecs
);
626 bool IsStrictMediaMimeType(const std::string
& mime_type
) {
627 return g_media_mime_util
.Get().IsStrictMediaMimeType(mime_type
);
630 SupportsType
IsSupportedStrictMediaMimeType(
631 const std::string
& mime_type
,
632 const std::vector
<std::string
>& codecs
) {
633 return g_media_mime_util
.Get().IsSupportedStrictMediaMimeType(
637 void ParseCodecString(const std::string
& codecs
,
638 std::vector
<std::string
>* codecs_out
,
640 g_media_mime_util
.Get().ParseCodecString(codecs
, codecs_out
, strip
);
643 void RemoveProprietaryMediaTypesAndCodecsForTests() {
644 g_media_mime_util
.Get().RemoveProprietaryMediaTypesAndCodecsForTests();