OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include <algorithm> |
| 6 #include <iterator> |
| 7 #include <map> |
| 8 #include <string> |
| 9 |
| 10 #include "base/containers/hash_tables.h" |
| 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/stl_util.h" |
| 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_split.h" |
| 16 #include "base/strings/string_util.h" |
| 17 #include "media/base/mime_util.h" |
| 18 |
| 19 #if defined(OS_ANDROID) |
| 20 #include "base/android/build_info.h" |
| 21 #endif |
| 22 |
| 23 namespace media { |
| 24 |
| 25 // Singleton utility class for mime types. |
| 26 class MimeUtil { |
| 27 public: |
| 28 enum Codec { |
| 29 INVALID_CODEC, |
| 30 PCM, |
| 31 MP3, |
| 32 MPEG2_AAC_LC, |
| 33 MPEG2_AAC_MAIN, |
| 34 MPEG2_AAC_SSR, |
| 35 MPEG4_AAC_LC, |
| 36 MPEG4_AAC_SBR_v1, |
| 37 MPEG4_AAC_SBR_PS_v2, |
| 38 VORBIS, |
| 39 OPUS, |
| 40 H264_BASELINE, |
| 41 H264_MAIN, |
| 42 H264_HIGH, |
| 43 VP8, |
| 44 VP9, |
| 45 THEORA |
| 46 }; |
| 47 |
| 48 bool IsSupportedMediaMimeType(const std::string& mime_type) const; |
| 49 |
| 50 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; |
| 51 |
| 52 void ParseCodecString(const std::string& codecs, |
| 53 std::vector<std::string>* codecs_out, |
| 54 bool strip); |
| 55 |
| 56 bool IsStrictMediaMimeType(const std::string& mime_type) const; |
| 57 SupportsType IsSupportedStrictMediaMimeType( |
| 58 const std::string& mime_type, |
| 59 const std::vector<std::string>& codecs) const; |
| 60 |
| 61 void RemoveProprietaryMediaTypesAndCodecsForTests(); |
| 62 |
| 63 private: |
| 64 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; |
| 65 |
| 66 typedef base::hash_set<std::string> MimeMappings; |
| 67 |
| 68 typedef base::hash_set<int> CodecSet; |
| 69 typedef std::map<std::string, CodecSet> StrictMappings; |
| 70 struct CodecEntry { |
| 71 CodecEntry() : codec(INVALID_CODEC), is_ambiguous(true) {} |
| 72 CodecEntry(Codec c, bool ambiguous) : codec(c), is_ambiguous(ambiguous) {} |
| 73 Codec codec; |
| 74 bool is_ambiguous; |
| 75 }; |
| 76 typedef std::map<std::string, CodecEntry> StringToCodecMappings; |
| 77 |
| 78 MimeUtil(); |
| 79 |
| 80 // Returns IsSupported if all codec IDs in |codecs| are unambiguous |
| 81 // and are supported by the platform. MayBeSupported is returned if |
| 82 // at least one codec ID in |codecs| is ambiguous but all the codecs |
| 83 // are supported by the platform. IsNotSupported is returned if at |
| 84 // least one codec ID is not supported by the platform. |
| 85 SupportsType AreSupportedCodecs( |
| 86 const CodecSet& supported_codecs, |
| 87 const std::vector<std::string>& codecs) const; |
| 88 |
| 89 // For faster lookup, keep hash sets. |
| 90 void InitializeMimeTypeMaps(); |
| 91 |
| 92 // Converts a codec ID into an Codec enum value and indicates |
| 93 // whether the conversion was ambiguous. |
| 94 // Returns true if this method was able to map |codec_id| to a specific |
| 95 // Codec enum value. |codec| and |is_ambiguous| are only valid if true |
| 96 // is returned. Otherwise their value is undefined after the call. |
| 97 // |is_ambiguous| is true if |codec_id| did not have enough information to |
| 98 // unambiguously determine the proper Codec enum value. If |is_ambiguous| |
| 99 // is true |codec| contains the best guess for the intended Codec enum value. |
| 100 bool StringToCodec(const std::string& codec_id, |
| 101 Codec* codec, |
| 102 bool* is_ambiguous) const; |
| 103 |
| 104 // Returns true if |codec| is supported by the platform. |
| 105 // Note: This method will return false if the platform supports proprietary |
| 106 // codecs but |allow_proprietary_codecs_| is set to false. |
| 107 bool IsCodecSupported(Codec codec) const; |
| 108 |
| 109 // Returns true if |codec| refers to a proprietary codec. |
| 110 bool IsCodecProprietary(Codec codec) const; |
| 111 |
| 112 // Returns true and sets |*default_codec| if |mime_type| has a default codec |
| 113 // associated with it. Returns false otherwise and the value of |
| 114 // |*default_codec| is undefined. |
| 115 bool GetDefaultCodecLowerCase(const std::string& mime_type_lower_case, |
| 116 Codec* default_codec) const; |
| 117 |
| 118 // Returns true if |mime_type_lower_case| has a default codec associated with |
| 119 // it and IsCodecSupported() returns true for that particular codec. |
| 120 bool IsDefaultCodecSupportedLowerCase( |
| 121 const std::string& mime_type_lower_case) const; |
| 122 |
| 123 MimeMappings media_map_; |
| 124 |
| 125 // A map of mime_types and hash map of the supported codecs for the mime_type. |
| 126 StrictMappings strict_format_map_; |
| 127 |
| 128 // Keeps track of whether proprietary codec support should be |
| 129 // advertised to callers. |
| 130 bool allow_proprietary_codecs_; |
| 131 |
| 132 // Lookup table for string compare based string -> Codec mappings. |
| 133 StringToCodecMappings string_to_codec_map_; |
| 134 }; // class MimeUtil |
| 135 |
| 136 // This variable is Leaky because we need to access it from WorkerPool threads. |
| 137 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = |
| 138 LAZY_INSTANCE_INITIALIZER; |
| 139 |
| 140 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type |
| 141 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php |
| 142 // This set of codecs is supported by all variations of Chromium. |
| 143 static const char* const common_media_types[] = { |
| 144 // Ogg. |
| 145 "audio/ogg", |
| 146 "application/ogg", |
| 147 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora. |
| 148 "video/ogg", |
| 149 #endif |
| 150 |
| 151 // WebM. |
| 152 "video/webm", |
| 153 "audio/webm", |
| 154 |
| 155 // Wav. |
| 156 "audio/wav", |
| 157 "audio/x-wav", |
| 158 |
| 159 #if defined(OS_ANDROID) |
| 160 // HLS. Supported by Android ICS and above. |
| 161 "application/vnd.apple.mpegurl", |
| 162 "application/x-mpegurl", |
| 163 #endif |
| 164 }; |
| 165 |
| 166 // List of proprietary types only supported by Google Chrome. |
| 167 static const char* const proprietary_media_types[] = { |
| 168 // MPEG-4. |
| 169 "video/mp4", |
| 170 "video/x-m4v", |
| 171 "audio/mp4", |
| 172 "audio/x-m4a", |
| 173 |
| 174 // MP3. |
| 175 "audio/mp3", |
| 176 "audio/x-mp3", |
| 177 "audio/mpeg", |
| 178 "audio/aac", |
| 179 |
| 180 #if defined(ENABLE_MPEG2TS_STREAM_PARSER) |
| 181 // MPEG-2 TS. |
| 182 "video/mp2t", |
| 183 #endif |
| 184 }; |
| 185 |
| 186 #if defined(OS_ANDROID) |
| 187 static bool IsCodecSupportedOnAndroid(MimeUtil::Codec codec) { |
| 188 switch (codec) { |
| 189 case MimeUtil::INVALID_CODEC: |
| 190 return false; |
| 191 |
| 192 case MimeUtil::PCM: |
| 193 case MimeUtil::MP3: |
| 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: |
| 200 case MimeUtil::VP8: |
| 201 case MimeUtil::VORBIS: |
| 202 return true; |
| 203 |
| 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. |
| 208 return false; |
| 209 |
| 210 case MimeUtil::VP9: |
| 211 // VP9 is supported only in KitKat+ (API Level 19). |
| 212 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19; |
| 213 |
| 214 case MimeUtil::OPUS: |
| 215 // Opus is supported only in Lollipop+ (API Level 21). |
| 216 return base::android::BuildInfo::GetInstance()->sdk_int() >= 21; |
| 217 |
| 218 case MimeUtil::THEORA: |
| 219 return false; |
| 220 } |
| 221 |
| 222 return false; |
| 223 } |
| 224 |
| 225 static bool IsMimeTypeSupportedOnAndroid(const std::string& mimeType) { |
| 226 // HLS codecs are supported in ICS and above (API level 14) |
| 227 if ((!mimeType.compare("application/vnd.apple.mpegurl") || |
| 228 !mimeType.compare("application/x-mpegurl")) && |
| 229 base::android::BuildInfo::GetInstance()->sdk_int() < 14) { |
| 230 return false; |
| 231 } |
| 232 return true; |
| 233 } |
| 234 #endif |
| 235 |
| 236 struct MediaFormatStrict { |
| 237 const char* const mime_type; |
| 238 const char* const codecs_list; |
| 239 }; |
| 240 |
| 241 // Following is the list of RFC 6381 compliant codecs: |
| 242 // mp4a.66 - MPEG-2 AAC MAIN |
| 243 // mp4a.67 - MPEG-2 AAC LC |
| 244 // mp4a.68 - MPEG-2 AAC SSR |
| 245 // mp4a.69 - MPEG-2 extension to MPEG-1 |
| 246 // mp4a.6B - MPEG-1 audio |
| 247 // mp4a.40.2 - MPEG-4 AAC LC |
| 248 // mp4a.40.02 - MPEG-4 AAC LC (leading 0 in aud-oti for compatibility) |
| 249 // mp4a.40.5 - MPEG-4 HE-AAC v1 (AAC LC + SBR) |
| 250 // mp4a.40.05 - MPEG-4 HE-AAC v1 (AAC LC + SBR) (leading 0 in aud-oti for |
| 251 // compatibility) |
| 252 // mp4a.40.29 - MPEG-4 HE-AAC v2 (AAC LC + SBR + PS) |
| 253 // |
| 254 // avc1.42E0xx - H.264 Baseline |
| 255 // avc1.4D40xx - H.264 Main |
| 256 // avc1.6400xx - H.264 High |
| 257 static const char kMP4AudioCodecsExpression[] = |
| 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"; |
| 260 static const char kMP4VideoCodecsExpression[] = |
| 261 // This is not a complete list of supported avc1 codecs. It is simply used |
| 262 // to register support for the corresponding Codec enum. Instead of using |
| 263 // strings in these three arrays, we should use the Codec enum values. |
| 264 // This will avoid confusion and unnecessary parsing at runtime. |
| 265 // kUnambiguousCodecStringMap/kAmbiguousCodecStringMap should be the only |
| 266 // mapping from strings to codecs. See crbug.com/461009. |
| 267 "avc1.42E00A,avc1.4D400A,avc1.64000A," |
| 268 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5," |
| 269 "mp4a.40.05,mp4a.40.29"; |
| 270 |
| 271 // These containers are also included in |
| 272 // common_media_types/proprietary_media_types. See crbug.com/461012. |
| 273 static const MediaFormatStrict format_codec_mappings[] = { |
| 274 {"video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0"}, |
| 275 {"audio/webm", "opus,vorbis"}, |
| 276 {"audio/wav", "1"}, |
| 277 {"audio/x-wav", "1"}, |
| 278 // Android does not support Opus in Ogg container. |
| 279 #if defined(OS_ANDROID) |
| 280 {"video/ogg", "theora,vorbis"}, |
| 281 {"audio/ogg", "vorbis"}, |
| 282 {"application/ogg", "theora,vorbis"}, |
| 283 #else |
| 284 {"video/ogg", "opus,theora,vorbis"}, |
| 285 {"audio/ogg", "opus,vorbis"}, |
| 286 {"application/ogg", "opus,theora,vorbis"}, |
| 287 #endif |
| 288 {"audio/mpeg", "mp3"}, |
| 289 {"audio/mp3", ""}, |
| 290 {"audio/x-mp3", ""}, |
| 291 {"audio/mp4", kMP4AudioCodecsExpression}, |
| 292 {"audio/x-m4a", kMP4AudioCodecsExpression}, |
| 293 {"video/mp4", kMP4VideoCodecsExpression}, |
| 294 {"video/x-m4v", kMP4VideoCodecsExpression}, |
| 295 {"application/x-mpegurl", kMP4VideoCodecsExpression}, |
| 296 {"application/vnd.apple.mpegurl", kMP4VideoCodecsExpression}}; |
| 297 |
| 298 struct CodecIDMappings { |
| 299 const char* const codec_id; |
| 300 MimeUtil::Codec codec; |
| 301 }; |
| 302 |
| 303 // List of codec IDs that provide enough information to determine the |
| 304 // codec and profile being requested. |
| 305 // |
| 306 // The "mp4a" strings come from RFC 6381. |
| 307 static const CodecIDMappings kUnambiguousCodecStringMap[] = { |
| 308 {"1", MimeUtil::PCM}, // We only allow this for WAV so it isn't ambiguous. |
| 309 // avc1/avc3.XXXXXX may be unambiguous; handled by ParseH264CodecID(). |
| 310 {"mp3", MimeUtil::MP3}, |
| 311 {"mp4a.66", MimeUtil::MPEG2_AAC_MAIN}, |
| 312 {"mp4a.67", MimeUtil::MPEG2_AAC_LC}, |
| 313 {"mp4a.68", MimeUtil::MPEG2_AAC_SSR}, |
| 314 {"mp4a.69", MimeUtil::MP3}, |
| 315 {"mp4a.6B", MimeUtil::MP3}, |
| 316 {"mp4a.40.2", MimeUtil::MPEG4_AAC_LC}, |
| 317 {"mp4a.40.02", MimeUtil::MPEG4_AAC_LC}, |
| 318 {"mp4a.40.5", MimeUtil::MPEG4_AAC_SBR_v1}, |
| 319 {"mp4a.40.05", MimeUtil::MPEG4_AAC_SBR_v1}, |
| 320 {"mp4a.40.29", MimeUtil::MPEG4_AAC_SBR_PS_v2}, |
| 321 {"vorbis", MimeUtil::VORBIS}, |
| 322 {"opus", MimeUtil::OPUS}, |
| 323 {"vp8", MimeUtil::VP8}, |
| 324 {"vp8.0", MimeUtil::VP8}, |
| 325 {"vp9", MimeUtil::VP9}, |
| 326 {"vp9.0", MimeUtil::VP9}, |
| 327 {"theora", MimeUtil::THEORA}}; |
| 328 |
| 329 // List of codec IDs that are ambiguous and don't provide |
| 330 // enough information to determine the codec and profile. |
| 331 // The codec in these entries indicate the codec and profile |
| 332 // we assume the user is trying to indicate. |
| 333 static const CodecIDMappings kAmbiguousCodecStringMap[] = { |
| 334 {"mp4a.40", MimeUtil::MPEG4_AAC_LC}, |
| 335 {"avc1", MimeUtil::H264_BASELINE}, |
| 336 {"avc3", MimeUtil::H264_BASELINE}, |
| 337 // avc1/avc3.XXXXXX may be ambiguous; handled by ParseH264CodecID(). |
| 338 }; |
| 339 |
| 340 MimeUtil::MimeUtil() : allow_proprietary_codecs_(false) { |
| 341 InitializeMimeTypeMaps(); |
| 342 } |
| 343 |
| 344 SupportsType MimeUtil::AreSupportedCodecs( |
| 345 const CodecSet& supported_codecs, |
| 346 const std::vector<std::string>& codecs) const { |
| 347 DCHECK(!supported_codecs.empty()); |
| 348 DCHECK(!codecs.empty()); |
| 349 |
| 350 SupportsType result = IsSupported; |
| 351 for (size_t i = 0; i < codecs.size(); ++i) { |
| 352 bool is_ambiguous = true; |
| 353 Codec codec = INVALID_CODEC; |
| 354 if (!StringToCodec(codecs[i], &codec, &is_ambiguous)) |
| 355 return IsNotSupported; |
| 356 |
| 357 if (!IsCodecSupported(codec) || |
| 358 supported_codecs.find(codec) == supported_codecs.end()) { |
| 359 return IsNotSupported; |
| 360 } |
| 361 |
| 362 if (is_ambiguous) |
| 363 result = MayBeSupported; |
| 364 } |
| 365 |
| 366 return result; |
| 367 } |
| 368 |
| 369 void MimeUtil::InitializeMimeTypeMaps() { |
| 370 // Initialize the supported media types. |
| 371 for (size_t i = 0; i < arraysize(common_media_types); ++i) { |
| 372 #if defined(OS_ANDROID) |
| 373 if (!IsMimeTypeSupportedOnAndroid(common_media_types[i])) |
| 374 continue; |
| 375 #endif |
| 376 media_map_.insert(common_media_types[i]); |
| 377 } |
| 378 #if defined(USE_PROPRIETARY_CODECS) |
| 379 allow_proprietary_codecs_ = true; |
| 380 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) |
| 381 media_map_.insert(proprietary_media_types[i]); |
| 382 #endif |
| 383 |
| 384 for (size_t i = 0; i < arraysize(kUnambiguousCodecStringMap); ++i) { |
| 385 string_to_codec_map_[kUnambiguousCodecStringMap[i].codec_id] = |
| 386 CodecEntry(kUnambiguousCodecStringMap[i].codec, false); |
| 387 } |
| 388 |
| 389 for (size_t i = 0; i < arraysize(kAmbiguousCodecStringMap); ++i) { |
| 390 string_to_codec_map_[kAmbiguousCodecStringMap[i].codec_id] = |
| 391 CodecEntry(kAmbiguousCodecStringMap[i].codec, true); |
| 392 } |
| 393 |
| 394 // Initialize the strict supported media types. |
| 395 for (size_t i = 0; i < arraysize(format_codec_mappings); ++i) { |
| 396 std::vector<std::string> mime_type_codecs; |
| 397 ParseCodecString(format_codec_mappings[i].codecs_list, |
| 398 &mime_type_codecs, |
| 399 false); |
| 400 |
| 401 CodecSet codecs; |
| 402 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { |
| 403 Codec codec = INVALID_CODEC; |
| 404 bool is_ambiguous = true; |
| 405 CHECK(StringToCodec(mime_type_codecs[j], &codec, &is_ambiguous)); |
| 406 DCHECK(!is_ambiguous); |
| 407 codecs.insert(codec); |
| 408 } |
| 409 |
| 410 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; |
| 411 } |
| 412 } |
| 413 |
| 414 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { |
| 415 return media_map_.find(base::StringToLowerASCII(mime_type)) != |
| 416 media_map_.end(); |
| 417 } |
| 418 |
| 419 bool MimeUtil::AreSupportedMediaCodecs( |
| 420 const std::vector<std::string>& codecs) const { |
| 421 for (size_t i = 0; i < codecs.size(); ++i) { |
| 422 Codec codec = INVALID_CODEC; |
| 423 bool is_ambiguous = true; |
| 424 if (!StringToCodec(codecs[i], &codec, &is_ambiguous) || |
| 425 !IsCodecSupported(codec)) { |
| 426 return false; |
| 427 } |
| 428 } |
| 429 return true; |
| 430 } |
| 431 |
| 432 void MimeUtil::ParseCodecString(const std::string& codecs, |
| 433 std::vector<std::string>* codecs_out, |
| 434 bool strip) { |
| 435 std::string no_quote_codecs; |
| 436 base::TrimString(codecs, "\"", &no_quote_codecs); |
| 437 base::SplitString(no_quote_codecs, ',', codecs_out); |
| 438 |
| 439 if (!strip) |
| 440 return; |
| 441 |
| 442 // Strip everything past the first '.' |
| 443 for (std::vector<std::string>::iterator it = codecs_out->begin(); |
| 444 it != codecs_out->end(); |
| 445 ++it) { |
| 446 size_t found = it->find_first_of('.'); |
| 447 if (found != std::string::npos) |
| 448 it->resize(found); |
| 449 } |
| 450 } |
| 451 |
| 452 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { |
| 453 return strict_format_map_.find(base::StringToLowerASCII(mime_type)) != |
| 454 strict_format_map_.end(); |
| 455 } |
| 456 |
| 457 SupportsType MimeUtil::IsSupportedStrictMediaMimeType( |
| 458 const std::string& mime_type, |
| 459 const std::vector<std::string>& codecs) const { |
| 460 const std::string mime_type_lower_case = base::StringToLowerASCII(mime_type); |
| 461 StrictMappings::const_iterator it_strict_map = |
| 462 strict_format_map_.find(mime_type_lower_case); |
| 463 if (it_strict_map == strict_format_map_.end()) |
| 464 return codecs.empty() ? MayBeSupported : IsNotSupported; |
| 465 |
| 466 if (it_strict_map->second.empty()) { |
| 467 // We get here if the mimetype does not expect a codecs parameter. |
| 468 return (codecs.empty() && |
| 469 IsDefaultCodecSupportedLowerCase(mime_type_lower_case)) |
| 470 ? IsSupported |
| 471 : IsNotSupported; |
| 472 } |
| 473 |
| 474 if (codecs.empty()) { |
| 475 // We get here if the mimetype expects to get a codecs parameter, |
| 476 // but didn't get one. If |mime_type_lower_case| does not have a default |
| 477 // codec the best we can do is say "maybe" because we don't have enough |
| 478 // information. |
| 479 Codec default_codec = INVALID_CODEC; |
| 480 if (!GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec)) |
| 481 return MayBeSupported; |
| 482 |
| 483 return IsCodecSupported(default_codec) ? IsSupported : IsNotSupported; |
| 484 } |
| 485 |
| 486 return AreSupportedCodecs(it_strict_map->second, codecs); |
| 487 } |
| 488 |
| 489 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { |
| 490 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { |
| 491 media_map_.erase(proprietary_media_types[i]); |
| 492 } |
| 493 allow_proprietary_codecs_ = false; |
| 494 } |
| 495 |
| 496 // Returns true iff |profile_str| conforms to hex string "42y0", where y is one |
| 497 // of [8..F]. Requiring constraint_set0_flag be set and profile_idc be 0x42 is |
| 498 // taken from ISO-14496-10 7.3.2.1, 7.4.2.1, and Annex A.2.1. |
| 499 // |
| 500 // |profile_str| is the first four characters of the H.264 suffix string |
| 501 // (ignoring the last 2 characters of the full 6 character suffix that are |
| 502 // level_idc). From ISO-14496-10 7.3.2.1, it consists of: |
| 503 // 8 bits: profile_idc: required to be 0x42 here. |
| 504 // 1 bit: constraint_set0_flag : required to be true here. |
| 505 // 1 bit: constraint_set1_flag : ignored here. |
| 506 // 1 bit: constraint_set2_flag : ignored here. |
| 507 // 1 bit: constraint_set3_flag : ignored here. |
| 508 // 4 bits: reserved : required to be 0 here. |
| 509 // |
| 510 // The spec indicates other ways, not implemented here, that a |profile_str| |
| 511 // can indicate a baseline conforming decoder is sufficient for decode in Annex |
| 512 // A.2.1: "[profile_idc not necessarily 0x42] with constraint_set0_flag set and |
| 513 // in which level_idc and constraint_set3_flag represent a level less than or |
| 514 // equal to the specified level." |
| 515 static bool IsValidH264BaselineProfile(const std::string& profile_str) { |
| 516 uint32 constraint_set_bits; |
| 517 if (profile_str.size() != 4 || |
| 518 profile_str[0] != '4' || |
| 519 profile_str[1] != '2' || |
| 520 profile_str[3] != '0' || |
| 521 !base::HexStringToUInt(base::StringPiece(profile_str.c_str() + 2, 1), |
| 522 &constraint_set_bits)) { |
| 523 return false; |
| 524 } |
| 525 |
| 526 return constraint_set_bits >= 8; |
| 527 } |
| 528 |
| 529 static bool IsValidH264Level(const std::string& level_str) { |
| 530 uint32 level; |
| 531 if (level_str.size() != 2 || !base::HexStringToUInt(level_str, &level)) |
| 532 return false; |
| 533 |
| 534 // Valid levels taken from Table A-1 in ISO-14496-10. |
| 535 // Essentially |level_str| is toHex(10 * level). |
| 536 return ((level >= 10 && level <= 13) || |
| 537 (level >= 20 && level <= 22) || |
| 538 (level >= 30 && level <= 32) || |
| 539 (level >= 40 && level <= 42) || |
| 540 (level >= 50 && level <= 51)); |
| 541 } |
| 542 |
| 543 // Handle parsing H.264 codec IDs as outlined in RFC 6381 and ISO-14496-10. |
| 544 // avc1.42y0xx, y >= 8 - H.264 Baseline |
| 545 // avc1.4D40xx - H.264 Main |
| 546 // avc1.6400xx - H.264 High |
| 547 // |
| 548 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to |
| 549 // signal H.264 Baseline. For example, the idc_level, profile_idc and |
| 550 // constraint_set3_flag pieces may explicitly require decoder to conform to |
| 551 // baseline profile at the specified level (see Annex A and constraint_set0 in |
| 552 // ISO-14496-10). |
| 553 static bool ParseH264CodecID(const std::string& codec_id, |
| 554 MimeUtil::Codec* codec, |
| 555 bool* is_ambiguous) { |
| 556 // Make sure we have avc1.xxxxxx or avc3.xxxxxx |
| 557 if (codec_id.size() != 11 || |
| 558 (!StartsWithASCII(codec_id, "avc1.", true) && |
| 559 !StartsWithASCII(codec_id, "avc3.", true))) { |
| 560 return false; |
| 561 } |
| 562 |
| 563 std::string profile = StringToUpperASCII(codec_id.substr(5, 4)); |
| 564 if (IsValidH264BaselineProfile(profile)) { |
| 565 *codec = MimeUtil::H264_BASELINE; |
| 566 } else if (profile == "4D40") { |
| 567 *codec = MimeUtil::H264_MAIN; |
| 568 } else if (profile == "6400") { |
| 569 *codec = MimeUtil::H264_HIGH; |
| 570 } else { |
| 571 *codec = MimeUtil::H264_BASELINE; |
| 572 *is_ambiguous = true; |
| 573 return true; |
| 574 } |
| 575 |
| 576 *is_ambiguous = !IsValidH264Level(StringToUpperASCII(codec_id.substr(9))); |
| 577 return true; |
| 578 } |
| 579 |
| 580 bool MimeUtil::StringToCodec(const std::string& codec_id, |
| 581 Codec* codec, |
| 582 bool* is_ambiguous) const { |
| 583 StringToCodecMappings::const_iterator itr = |
| 584 string_to_codec_map_.find(codec_id); |
| 585 if (itr != string_to_codec_map_.end()) { |
| 586 *codec = itr->second.codec; |
| 587 *is_ambiguous = itr->second.is_ambiguous; |
| 588 return true; |
| 589 } |
| 590 |
| 591 // If |codec_id| is not in |string_to_codec_map_|, then we assume that it is |
| 592 // an H.264 codec ID because currently those are the only ones that can't be |
| 593 // stored in the |string_to_codec_map_| and require parsing. |
| 594 return ParseH264CodecID(codec_id, codec, is_ambiguous); |
| 595 } |
| 596 |
| 597 bool MimeUtil::IsCodecSupported(Codec codec) const { |
| 598 DCHECK_NE(codec, INVALID_CODEC); |
| 599 |
| 600 #if defined(OS_ANDROID) |
| 601 if (!IsCodecSupportedOnAndroid(codec)) |
| 602 return false; |
| 603 #endif |
| 604 |
| 605 return allow_proprietary_codecs_ || !IsCodecProprietary(codec); |
| 606 } |
| 607 |
| 608 bool MimeUtil::IsCodecProprietary(Codec codec) const { |
| 609 switch (codec) { |
| 610 case INVALID_CODEC: |
| 611 case MP3: |
| 612 case MPEG2_AAC_LC: |
| 613 case MPEG2_AAC_MAIN: |
| 614 case MPEG2_AAC_SSR: |
| 615 case MPEG4_AAC_LC: |
| 616 case MPEG4_AAC_SBR_v1: |
| 617 case MPEG4_AAC_SBR_PS_v2: |
| 618 case H264_BASELINE: |
| 619 case H264_MAIN: |
| 620 case H264_HIGH: |
| 621 return true; |
| 622 |
| 623 case PCM: |
| 624 case VORBIS: |
| 625 case OPUS: |
| 626 case VP8: |
| 627 case VP9: |
| 628 case THEORA: |
| 629 return false; |
| 630 } |
| 631 |
| 632 return true; |
| 633 } |
| 634 |
| 635 bool MimeUtil::GetDefaultCodecLowerCase(const std::string& mime_type_lower_case, |
| 636 Codec* default_codec) const { |
| 637 if (mime_type_lower_case == "audio/mpeg" || |
| 638 mime_type_lower_case == "audio/mp3" || |
| 639 mime_type_lower_case == "audio/x-mp3") { |
| 640 *default_codec = MimeUtil::MP3; |
| 641 return true; |
| 642 } |
| 643 |
| 644 return false; |
| 645 } |
| 646 |
| 647 bool MimeUtil::IsDefaultCodecSupportedLowerCase( |
| 648 const std::string& mime_type_lower_case) const { |
| 649 Codec default_codec = Codec::INVALID_CODEC; |
| 650 if (!GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec)) |
| 651 return false; |
| 652 return IsCodecSupported(default_codec); |
| 653 } |
| 654 |
| 655 bool IsSupportedMediaMimeType(const std::string& mime_type) { |
| 656 return g_mime_util.Get().IsSupportedMediaMimeType(mime_type); |
| 657 } |
| 658 |
| 659 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
| 660 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
| 661 } |
| 662 |
| 663 bool IsStrictMediaMimeType(const std::string& mime_type) { |
| 664 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
| 665 } |
| 666 |
| 667 SupportsType IsSupportedStrictMediaMimeType( |
| 668 const std::string& mime_type, |
| 669 const std::vector<std::string>& codecs) { |
| 670 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); |
| 671 } |
| 672 |
| 673 void ParseCodecString(const std::string& codecs, |
| 674 std::vector<std::string>* codecs_out, |
| 675 const bool strip) { |
| 676 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); |
| 677 } |
| 678 |
| 679 void RemoveProprietaryMediaTypesAndCodecsForTests() { |
| 680 g_mime_util.Get().RemoveProprietaryMediaTypesAndCodecsForTests(); |
| 681 } |
| 682 |
| 683 } // namespace media |
OLD | NEW |