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
[] = {
180 #if defined(ENABLE_MPEG2TS_STREAM_PARSER)
186 #if defined(OS_ANDROID)
187 static bool IsCodecSupportedOnAndroid(MimeUtil::Codec codec
) {
189 case MimeUtil::INVALID_CODEC
:
194 case MimeUtil::MPEG4_AAC_LC
:
195 case MimeUtil::MPEG4_AAC_SBR_v1
:
196 case MimeUtil::MPEG4_AAC_SBR_PS_v2
:
197 case MimeUtil::H264_BASELINE
:
198 case MimeUtil::H264_MAIN
:
199 case MimeUtil::H264_HIGH
:
201 case MimeUtil::VORBIS
:
204 case MimeUtil::MPEG2_AAC_LC
:
205 case MimeUtil::MPEG2_AAC_MAIN
:
206 case MimeUtil::MPEG2_AAC_SSR
:
207 // MPEG-2 variants of AAC are not supported on Android.
211 // VP9 is supported only in KitKat+ (API Level 19).
212 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
215 // Opus is supported only in Lollipop+ (API Level 21).
216 return base::android::BuildInfo::GetInstance()->sdk_int() >= 21;
218 case MimeUtil::THEORA
:
226 struct MediaFormatStrict
{
227 const char* const mime_type
;
228 const char* const codecs_list
;
231 // Following is the list of RFC 6381 compliant codecs:
232 // mp4a.66 - MPEG-2 AAC MAIN
233 // mp4a.67 - MPEG-2 AAC LC
234 // mp4a.68 - MPEG-2 AAC SSR
235 // mp4a.69 - MPEG-2 extension to MPEG-1
236 // mp4a.6B - MPEG-1 audio
237 // mp4a.40.2 - MPEG-4 AAC LC
238 // mp4a.40.02 - MPEG-4 AAC LC (leading 0 in aud-oti for compatibility)
239 // mp4a.40.5 - MPEG-4 HE-AAC v1 (AAC LC + SBR)
240 // mp4a.40.05 - MPEG-4 HE-AAC v1 (AAC LC + SBR) (leading 0 in aud-oti for
242 // mp4a.40.29 - MPEG-4 HE-AAC v2 (AAC LC + SBR + PS)
244 // avc1.42E0xx - H.264 Baseline
245 // avc1.4D40xx - H.264 Main
246 // avc1.6400xx - H.264 High
247 static const char kMP4AudioCodecsExpression
[] =
248 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
249 "mp4a.40.05,mp4a.40.29";
250 static const char kMP4VideoCodecsExpression
[] =
251 // This is not a complete list of supported avc1 codecs. It is simply used
252 // to register support for the corresponding Codec enum. Instead of using
253 // strings in these three arrays, we should use the Codec enum values.
254 // This will avoid confusion and unnecessary parsing at runtime.
255 // kUnambiguousCodecStringMap/kAmbiguousCodecStringMap should be the only
256 // mapping from strings to codecs. See crbug.com/461009.
257 "avc1.42E00A,avc1.4D400A,avc1.64000A,"
258 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
259 "mp4a.40.05,mp4a.40.29";
261 // These containers are also included in
262 // common_media_types/proprietary_media_types. See crbug.com/461012.
263 static const MediaFormatStrict format_codec_mappings
[] = {
264 {"video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0"},
265 {"audio/webm", "opus,vorbis"},
267 {"audio/x-wav", "1"},
268 // Android does not support Opus in Ogg container.
269 #if defined(OS_ANDROID)
270 {"video/ogg", "theora,vorbis"},
271 {"audio/ogg", "vorbis"},
272 {"application/ogg", "theora,vorbis"},
274 {"video/ogg", "opus,theora,vorbis"},
275 {"audio/ogg", "opus,vorbis"},
276 {"application/ogg", "opus,theora,vorbis"},
278 {"audio/mpeg", "mp3"},
282 {"audio/mp4", kMP4AudioCodecsExpression
},
283 {"audio/x-m4a", kMP4AudioCodecsExpression
},
284 {"video/mp4", kMP4VideoCodecsExpression
},
285 {"video/x-m4v", kMP4VideoCodecsExpression
},
286 {"application/x-mpegurl", kMP4VideoCodecsExpression
},
287 {"application/vnd.apple.mpegurl", kMP4VideoCodecsExpression
}};
289 struct CodecIDMappings
{
290 const char* const codec_id
;
291 MimeUtil::Codec codec
;
294 // List of codec IDs that provide enough information to determine the
295 // codec and profile being requested.
297 // The "mp4a" strings come from RFC 6381.
298 static const CodecIDMappings kUnambiguousCodecStringMap
[] = {
299 {"1", MimeUtil::PCM
}, // We only allow this for WAV so it isn't ambiguous.
300 // avc1/avc3.XXXXXX may be unambiguous; handled by ParseH264CodecID().
301 {"mp3", MimeUtil::MP3
},
302 {"mp4a.66", MimeUtil::MPEG2_AAC_MAIN
},
303 {"mp4a.67", MimeUtil::MPEG2_AAC_LC
},
304 {"mp4a.68", MimeUtil::MPEG2_AAC_SSR
},
305 {"mp4a.69", MimeUtil::MP3
},
306 {"mp4a.6B", MimeUtil::MP3
},
307 {"mp4a.40.2", MimeUtil::MPEG4_AAC_LC
},
308 {"mp4a.40.02", MimeUtil::MPEG4_AAC_LC
},
309 {"mp4a.40.5", MimeUtil::MPEG4_AAC_SBR_v1
},
310 {"mp4a.40.05", MimeUtil::MPEG4_AAC_SBR_v1
},
311 {"mp4a.40.29", MimeUtil::MPEG4_AAC_SBR_PS_v2
},
312 {"vorbis", MimeUtil::VORBIS
},
313 {"opus", MimeUtil::OPUS
},
314 {"vp8", MimeUtil::VP8
},
315 {"vp8.0", MimeUtil::VP8
},
316 {"vp9", MimeUtil::VP9
},
317 {"vp9.0", MimeUtil::VP9
},
318 {"theora", MimeUtil::THEORA
}};
320 // List of codec IDs that are ambiguous and don't provide
321 // enough information to determine the codec and profile.
322 // The codec in these entries indicate the codec and profile
323 // we assume the user is trying to indicate.
324 static const CodecIDMappings kAmbiguousCodecStringMap
[] = {
325 {"mp4a.40", MimeUtil::MPEG4_AAC_LC
},
326 {"avc1", MimeUtil::H264_BASELINE
},
327 {"avc3", MimeUtil::H264_BASELINE
},
328 // avc1/avc3.XXXXXX may be ambiguous; handled by ParseH264CodecID().
331 MimeUtil::MimeUtil() : allow_proprietary_codecs_(false) {
332 InitializeMimeTypeMaps();
335 SupportsType
MimeUtil::AreSupportedCodecs(
336 const CodecSet
& supported_codecs
,
337 const std::vector
<std::string
>& codecs
) const {
338 DCHECK(!supported_codecs
.empty());
339 DCHECK(!codecs
.empty());
341 SupportsType result
= IsSupported
;
342 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
343 bool is_ambiguous
= true;
344 Codec codec
= INVALID_CODEC
;
345 if (!StringToCodec(codecs
[i
], &codec
, &is_ambiguous
))
346 return IsNotSupported
;
348 if (!IsCodecSupported(codec
) ||
349 supported_codecs
.find(codec
) == supported_codecs
.end()) {
350 return IsNotSupported
;
354 result
= MayBeSupported
;
360 void MimeUtil::InitializeMimeTypeMaps() {
361 // Initialize the supported media types.
362 for (size_t i
= 0; i
< arraysize(common_media_types
); ++i
)
363 media_map_
.insert(common_media_types
[i
]);
364 #if defined(USE_PROPRIETARY_CODECS)
365 allow_proprietary_codecs_
= true;
367 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
368 media_map_
.insert(proprietary_media_types
[i
]);
371 for (size_t i
= 0; i
< arraysize(kUnambiguousCodecStringMap
); ++i
) {
372 string_to_codec_map_
[kUnambiguousCodecStringMap
[i
].codec_id
] =
373 CodecEntry(kUnambiguousCodecStringMap
[i
].codec
, false);
376 for (size_t i
= 0; i
< arraysize(kAmbiguousCodecStringMap
); ++i
) {
377 string_to_codec_map_
[kAmbiguousCodecStringMap
[i
].codec_id
] =
378 CodecEntry(kAmbiguousCodecStringMap
[i
].codec
, true);
381 // Initialize the strict supported media types.
382 for (size_t i
= 0; i
< arraysize(format_codec_mappings
); ++i
) {
383 std::vector
<std::string
> mime_type_codecs
;
384 ParseCodecString(format_codec_mappings
[i
].codecs_list
,
389 for (size_t j
= 0; j
< mime_type_codecs
.size(); ++j
) {
390 Codec codec
= INVALID_CODEC
;
391 bool is_ambiguous
= true;
392 CHECK(StringToCodec(mime_type_codecs
[j
], &codec
, &is_ambiguous
));
393 DCHECK(!is_ambiguous
);
394 codecs
.insert(codec
);
397 strict_format_map_
[format_codec_mappings
[i
].mime_type
] = codecs
;
401 bool MimeUtil::IsSupportedMediaMimeType(const std::string
& mime_type
) const {
402 return media_map_
.find(base::ToLowerASCII(mime_type
)) != media_map_
.end();
406 bool MimeUtil::AreSupportedMediaCodecs(
407 const std::vector
<std::string
>& codecs
) const {
408 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
409 Codec codec
= INVALID_CODEC
;
410 bool is_ambiguous
= true;
411 if (!StringToCodec(codecs
[i
], &codec
, &is_ambiguous
) ||
412 !IsCodecSupported(codec
)) {
419 void MimeUtil::ParseCodecString(const std::string
& codecs
,
420 std::vector
<std::string
>* codecs_out
,
422 *codecs_out
= base::SplitString(
423 base::TrimString(codecs
, "\"", base::TRIM_ALL
),
424 ",", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
426 // Convert empty or all-whitespace input to 0 results.
427 if (codecs_out
->size() == 1 && (*codecs_out
)[0].empty())
433 // Strip everything past the first '.'
434 for (std::vector
<std::string
>::iterator it
= codecs_out
->begin();
435 it
!= codecs_out
->end();
437 size_t found
= it
->find_first_of('.');
438 if (found
!= std::string::npos
)
443 bool MimeUtil::IsStrictMediaMimeType(const std::string
& mime_type
) const {
444 return strict_format_map_
.find(base::ToLowerASCII(mime_type
)) !=
445 strict_format_map_
.end();
448 SupportsType
MimeUtil::IsSupportedStrictMediaMimeType(
449 const std::string
& mime_type
,
450 const std::vector
<std::string
>& codecs
) const {
451 const std::string mime_type_lower_case
= base::ToLowerASCII(mime_type
);
452 StrictMappings::const_iterator it_strict_map
=
453 strict_format_map_
.find(mime_type_lower_case
);
454 if (it_strict_map
== strict_format_map_
.end())
455 return codecs
.empty() ? MayBeSupported
: IsNotSupported
;
457 if (it_strict_map
->second
.empty()) {
458 // We get here if the mimetype does not expect a codecs parameter.
459 return (codecs
.empty() &&
460 IsDefaultCodecSupportedLowerCase(mime_type_lower_case
))
465 if (codecs
.empty()) {
466 // We get here if the mimetype expects to get a codecs parameter,
467 // but didn't get one. If |mime_type_lower_case| does not have a default
468 // codec the best we can do is say "maybe" because we don't have enough
470 Codec default_codec
= INVALID_CODEC
;
471 if (!GetDefaultCodecLowerCase(mime_type_lower_case
, &default_codec
))
472 return MayBeSupported
;
474 return IsCodecSupported(default_codec
) ? IsSupported
: IsNotSupported
;
477 return AreSupportedCodecs(it_strict_map
->second
, codecs
);
480 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
481 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
482 media_map_
.erase(proprietary_media_types
[i
]);
483 allow_proprietary_codecs_
= false;
486 static bool IsValidH264Level(const std::string
& level_str
) {
488 if (level_str
.size() != 2 || !base::HexStringToUInt(level_str
, &level
))
491 // Valid levels taken from Table A-1 in ISO-14496-10.
492 // Essentially |level_str| is toHex(10 * level).
493 return ((level
>= 10 && level
<= 13) ||
494 (level
>= 20 && level
<= 22) ||
495 (level
>= 30 && level
<= 32) ||
496 (level
>= 40 && level
<= 42) ||
497 (level
>= 50 && level
<= 51));
500 // Handle parsing H.264 codec IDs as outlined in RFC 6381 and ISO-14496-10.
501 // avc1.42x0yy - H.264 Baseline
502 // avc1.4Dx0yy - H.264 Main
503 // avc1.64x0yy - H.264 High
505 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to
506 // signal H.264 Baseline. For example, the idc_level, profile_idc and
507 // constraint_set3_flag pieces may explicitly require decoder to conform to
508 // baseline profile at the specified level (see Annex A and constraint_set0 in
510 static bool ParseH264CodecID(const std::string
& codec_id
,
511 MimeUtil::Codec
* codec
,
512 bool* is_ambiguous
) {
513 // Make sure we have avc1.xxxxxx or avc3.xxxxxx
514 if (codec_id
.size() != 11 ||
515 (!base::StartsWith(codec_id
, "avc1.", base::CompareCase::SENSITIVE
) &&
516 !base::StartsWith(codec_id
, "avc3.", base::CompareCase::SENSITIVE
))) {
520 // Validate constraint flags and reserved bits.
521 if (!base::IsHexDigit(codec_id
[7]) || codec_id
[8] != '0') {
522 *codec
= MimeUtil::H264_BASELINE
;
523 *is_ambiguous
= true;
527 // Extract the profile.
528 std::string profile
= base::ToUpperASCII(codec_id
.substr(5, 2));
529 if (profile
== "42") {
530 *codec
= MimeUtil::H264_BASELINE
;
531 } else if (profile
== "4D") {
532 *codec
= MimeUtil::H264_MAIN
;
533 } else if (profile
== "64") {
534 *codec
= MimeUtil::H264_HIGH
;
536 *codec
= MimeUtil::H264_BASELINE
;
537 *is_ambiguous
= true;
542 *is_ambiguous
= !IsValidH264Level(base::ToUpperASCII(codec_id
.substr(9)));
546 bool MimeUtil::StringToCodec(const std::string
& codec_id
,
548 bool* is_ambiguous
) const {
549 StringToCodecMappings::const_iterator itr
=
550 string_to_codec_map_
.find(codec_id
);
551 if (itr
!= string_to_codec_map_
.end()) {
552 *codec
= itr
->second
.codec
;
553 *is_ambiguous
= itr
->second
.is_ambiguous
;
557 // If |codec_id| is not in |string_to_codec_map_|, then we assume that it is
558 // an H.264 codec ID because currently those are the only ones that can't be
559 // stored in the |string_to_codec_map_| and require parsing.
560 return ParseH264CodecID(codec_id
, codec
, is_ambiguous
);
563 bool MimeUtil::IsCodecSupported(Codec codec
) const {
564 DCHECK_NE(codec
, INVALID_CODEC
);
566 #if defined(OS_ANDROID)
567 if (!IsCodecSupportedOnAndroid(codec
))
571 return allow_proprietary_codecs_
|| !IsCodecProprietary(codec
);
574 bool MimeUtil::IsCodecProprietary(Codec codec
) const {
582 case MPEG4_AAC_SBR_v1
:
583 case MPEG4_AAC_SBR_PS_v2
:
601 bool MimeUtil::GetDefaultCodecLowerCase(const std::string
& mime_type_lower_case
,
602 Codec
* default_codec
) const {
603 if (mime_type_lower_case
== "audio/mpeg" ||
604 mime_type_lower_case
== "audio/mp3" ||
605 mime_type_lower_case
== "audio/x-mp3") {
606 *default_codec
= MimeUtil::MP3
;
610 if (mime_type_lower_case
== "audio/aac") {
611 *default_codec
= MimeUtil::MPEG4_AAC_LC
;
618 bool MimeUtil::IsDefaultCodecSupportedLowerCase(
619 const std::string
& mime_type_lower_case
) const {
620 Codec default_codec
= Codec::INVALID_CODEC
;
621 if (!GetDefaultCodecLowerCase(mime_type_lower_case
, &default_codec
))
623 return IsCodecSupported(default_codec
);
626 bool IsSupportedMediaMimeType(const std::string
& mime_type
) {
627 return g_media_mime_util
.Get().IsSupportedMediaMimeType(mime_type
);
630 bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
) {
631 return g_media_mime_util
.Get().AreSupportedMediaCodecs(codecs
);
634 bool IsStrictMediaMimeType(const std::string
& mime_type
) {
635 return g_media_mime_util
.Get().IsStrictMediaMimeType(mime_type
);
638 SupportsType
IsSupportedStrictMediaMimeType(
639 const std::string
& mime_type
,
640 const std::vector
<std::string
>& codecs
) {
641 return g_media_mime_util
.Get().IsSupportedStrictMediaMimeType(
645 void ParseCodecString(const std::string
& codecs
,
646 std::vector
<std::string
>* codecs_out
,
648 g_media_mime_util
.Get().ParseCodecString(codecs
, codecs_out
, strip
);
651 void RemoveProprietaryMediaTypesAndCodecsForTests() {
652 g_media_mime_util
.Get().RemoveProprietaryMediaTypesAndCodecsForTests();