1 // Copyright (c) 2013 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 "media/base/container_names.h" 6 7 #include <cctype> 8 #include <limits> 9 10 #include "base/basictypes.h" 11 #include "base/logging.h" 12 #include "media/base/bit_reader.h" 13 14 namespace media { 15 16 namespace container_names { 17 18 #define TAG(a, b, c, d) \ 19 ((static_cast<uint8>(a) << 24) | (static_cast<uint8>(b) << 16) | \ 20 (static_cast<uint8>(c) << 8) | (static_cast<uint8>(d))) 21 22 #define RCHECK(x) \ 23 do { \ 24 if (!(x)) \ 25 return false; \ 26 } while (0) 27 28 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf" 29 30 // Helper function to read 2 bytes (16 bits, big endian) from a buffer. 31 static int Read16(const uint8* p) { 32 return p[0] << 8 | p[1]; 33 } 34 35 // Helper function to read 3 bytes (24 bits, big endian) from a buffer. 36 static uint32 Read24(const uint8* p) { 37 return p[0] << 16 | p[1] << 8 | p[2]; 38 } 39 40 // Helper function to read 4 bytes (32 bits, big endian) from a buffer. 41 static uint32 Read32(const uint8* p) { 42 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]; 43 } 44 45 // Helper function to read 4 bytes (32 bits, little endian) from a buffer. 46 static uint32 Read32LE(const uint8* p) { 47 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0]; 48 } 49 50 // Helper function to do buffer comparisons with a string without going off the 51 // end of the buffer. 52 static bool StartsWith(const uint8* buffer, 53 size_t buffer_size, 54 const char* prefix) { 55 size_t prefix_size = strlen(prefix); 56 return (prefix_size <= buffer_size && 57 memcmp(buffer, prefix, prefix_size) == 0); 58 } 59 60 // Helper function to do buffer comparisons with another buffer (to allow for 61 // embedded \0 in the comparison) without going off the end of the buffer. 62 static bool StartsWith(const uint8* buffer, 63 size_t buffer_size, 64 const uint8* prefix, 65 size_t prefix_size) { 66 return (prefix_size <= buffer_size && 67 memcmp(buffer, prefix, prefix_size) == 0); 68 } 69 70 // Helper function to read up to 64 bits from a bit stream. 71 static uint64 ReadBits(BitReader* reader, int num_bits) { 72 DCHECK_GE(reader->bits_available(), num_bits); 73 DCHECK((num_bits > 0) && (num_bits <= 64)); 74 uint64 value; 75 reader->ReadBits(num_bits, &value); 76 return value; 77 } 78 79 const int kAc3FrameSizeTable[38][3] = { 80 { 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 }, 81 { 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 }, 82 { 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 }, 83 { 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 }, 84 { 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 }, 85 { 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 }, 86 { 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 }, 87 { 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 }, 88 { 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 }, 89 { 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 }, 90 { 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 } 91 }; 92 93 // Checks for an ADTS AAC container. 94 static bool CheckAac(const uint8* buffer, int buffer_size) { 95 // Audio Data Transport Stream (ADTS) header is 7 or 9 bytes 96 // (from http://wiki.multimedia.cx/index.php?title=ADTS) 97 RCHECK(buffer_size > 6); 98 99 int offset = 0; 100 while (offset + 6 < buffer_size) { 101 BitReader reader(buffer + offset, 6); 102 103 // Syncword must be 0xfff. 104 RCHECK(ReadBits(&reader, 12) == 0xfff); 105 106 // Skip MPEG version. 107 reader.SkipBits(1); 108 109 // Layer is always 0. 110 RCHECK(ReadBits(&reader, 2) == 0); 111 112 // Skip protection + profile. 113 reader.SkipBits(1 + 2); 114 115 // Check sampling frequency index. 116 RCHECK(ReadBits(&reader, 4) != 15); // Forbidden. 117 118 // Skip private stream, channel configuration, originality, home, 119 // copyrighted stream, and copyright_start. 120 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1); 121 122 // Get frame length (includes header). 123 int size = ReadBits(&reader, 13); 124 RCHECK(size > 0); 125 offset += size; 126 } 127 return true; 128 } 129 130 const uint16 kAc3SyncWord = 0x0b77; 131 132 // Checks for an AC3 container. 133 static bool CheckAc3(const uint8* buffer, int buffer_size) { 134 // Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3) 135 // Doc. A/52:2012 136 // (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf) 137 138 // AC3 container looks like syncinfo | bsi | audblk * 6 | aux | check. 139 RCHECK(buffer_size > 6); 140 141 int offset = 0; 142 while (offset + 6 < buffer_size) { 143 BitReader reader(buffer + offset, 6); 144 145 // Check syncinfo. 146 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord); 147 148 // Skip crc1. 149 reader.SkipBits(16); 150 151 // Verify fscod. 152 int sample_rate_code = ReadBits(&reader, 2); 153 RCHECK(sample_rate_code != 3); // Reserved. 154 155 // Verify frmsizecod. 156 int frame_size_code = ReadBits(&reader, 6); 157 RCHECK(frame_size_code < 38); // Undefined. 158 159 // Verify bsid. 160 RCHECK(ReadBits(&reader, 5) < 10); // Normally 8 or 6, 16 used by EAC3. 161 162 offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code]; 163 } 164 return true; 165 } 166 167 // Checks for an EAC3 container (very similar to AC3) 168 static bool CheckEac3(const uint8* buffer, int buffer_size) { 169 // Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3) 170 // Doc. A/52:2012 171 // (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf) 172 173 // EAC3 container looks like syncinfo | bsi | audfrm | audblk* | aux | check. 174 RCHECK(buffer_size > 6); 175 176 int offset = 0; 177 while (offset + 6 < buffer_size) { 178 BitReader reader(buffer + offset, 6); 179 180 // Check syncinfo. 181 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord); 182 183 // Verify strmtyp. 184 RCHECK(ReadBits(&reader, 2) != 3); 185 186 // Skip substreamid. 187 reader.SkipBits(3); 188 189 // Get frmsize. Include syncinfo size and convert to bytes. 190 int frame_size = (ReadBits(&reader, 11) + 1) * 2; 191 RCHECK(frame_size >= 7); 192 193 // Skip fscod, fscod2, acmod, and lfeon. 194 reader.SkipBits(2 + 2 + 3 + 1); 195 196 // Verify bsid. 197 int bit_stream_id = ReadBits(&reader, 5); 198 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16); 199 200 offset += frame_size; 201 } 202 return true; 203 } 204 205 // Additional checks for a BINK container. 206 static bool CheckBink(const uint8* buffer, int buffer_size) { 207 // Reference: http://wiki.multimedia.cx/index.php?title=Bink_Container 208 RCHECK(buffer_size >= 44); 209 210 // Verify number of frames specified. 211 RCHECK(Read32LE(buffer + 8) > 0); 212 213 // Verify width in range. 214 int width = Read32LE(buffer + 20); 215 RCHECK(width > 0 && width <= 32767); 216 217 // Verify height in range. 218 int height = Read32LE(buffer + 24); 219 RCHECK(height > 0 && height <= 32767); 220 221 // Verify frames per second specified. 222 RCHECK(Read32LE(buffer + 28) > 0); 223 224 // Verify video frames per second specified. 225 RCHECK(Read32LE(buffer + 32) > 0); 226 227 // Number of audio tracks must be 256 or less. 228 return (Read32LE(buffer + 40) <= 256); 229 } 230 231 // Additional checks for a CAF container. 232 static bool CheckCaf(const uint8* buffer, int buffer_size) { 233 // Reference: Apple Core Audio Format Specification 1.0 234 // (https://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/CAFSpec/CAF_spec/CAF_spec.html) 235 RCHECK(buffer_size >= 52); 236 BitReader reader(buffer, buffer_size); 237 238 // mFileType should be "caff". 239 RCHECK(ReadBits(&reader, 32) == TAG('c', 'a', 'f', 'f')); 240 241 // mFileVersion should be 1. 242 RCHECK(ReadBits(&reader, 16) == 1); 243 244 // Skip mFileFlags. 245 reader.SkipBits(16); 246 247 // First chunk should be Audio Description chunk, size 32l. 248 RCHECK(ReadBits(&reader, 32) == TAG('d', 'e', 's', 'c')); 249 RCHECK(ReadBits(&reader, 64) == 32); 250 251 // CAFAudioFormat.mSampleRate(float64) not 0 252 RCHECK(ReadBits(&reader, 64) != 0); 253 254 // CAFAudioFormat.mFormatID not 0 255 RCHECK(ReadBits(&reader, 32) != 0); 256 257 // Skip CAFAudioFormat.mBytesPerPacket and mFramesPerPacket. 258 reader.SkipBits(32 + 32); 259 260 // CAFAudioFormat.mChannelsPerFrame not 0 261 RCHECK(ReadBits(&reader, 32) != 0); 262 return true; 263 } 264 265 static bool kSamplingFrequencyValid[16] = { false, true, true, true, false, 266 false, true, true, true, false, 267 false, true, true, true, false, 268 false }; 269 static bool kExtAudioIdValid[8] = { true, false, true, false, false, false, 270 true, false }; 271 272 // Additional checks for a DTS container. 273 static bool CheckDts(const uint8* buffer, int buffer_size) { 274 // Reference: ETSI TS 102 114 V1.3.1 (2011-08) 275 // (http://www.etsi.org/deliver/etsi_ts/102100_102199/102114/01.03.01_60/ts_102114v010301p.pdf) 276 RCHECK(buffer_size > 11); 277 278 int offset = 0; 279 while (offset + 11 < buffer_size) { 280 BitReader reader(buffer + offset, 11); 281 282 // Verify sync word. 283 RCHECK(ReadBits(&reader, 32) == 0x7ffe8001); 284 285 // Skip frame type and deficit sample count. 286 reader.SkipBits(1 + 5); 287 288 // Verify CRC present flag. 289 RCHECK(ReadBits(&reader, 1) == 0); // CPF must be 0. 290 291 // Verify number of PCM sample blocks. 292 RCHECK(ReadBits(&reader, 7) >= 5); 293 294 // Verify primary frame byte size. 295 int frame_size = ReadBits(&reader, 14); 296 RCHECK(frame_size >= 95); 297 298 // Skip audio channel arrangement. 299 reader.SkipBits(6); 300 301 // Verify core audio sampling frequency is an allowed value. 302 RCHECK(kSamplingFrequencyValid[ReadBits(&reader, 4)]); 303 304 // Verify transmission bit rate is valid. 305 RCHECK(ReadBits(&reader, 5) <= 25); 306 307 // Verify reserved field is 0. 308 RCHECK(ReadBits(&reader, 1) == 0); 309 310 // Skip dynamic range flag, time stamp flag, auxiliary data flag, and HDCD. 311 reader.SkipBits(1 + 1 + 1 + 1); 312 313 // Verify extension audio descriptor flag is an allowed value. 314 RCHECK(kExtAudioIdValid[ReadBits(&reader, 3)]); 315 316 // Skip extended coding flag and audio sync word insertion flag. 317 reader.SkipBits(1 + 1); 318 319 // Verify low frequency effects flag is an allowed value. 320 RCHECK(ReadBits(&reader, 2) != 3); 321 322 offset += frame_size + 1; 323 } 324 return true; 325 } 326 327 // Checks for a DV container. 328 static bool CheckDV(const uint8* buffer, int buffer_size) { 329 // Reference: SMPTE 314M (Annex A has differences with IEC 61834). 330 // (http://standards.smpte.org/content/978-1-61482-454-1/st-314-2005/SEC1.body.pdf) 331 RCHECK(buffer_size > 11); 332 333 int offset = 0; 334 int current_sequence_number = -1; 335 int last_block_number[6]; 336 while (offset + 11 < buffer_size) { 337 BitReader reader(buffer + offset, 11); 338 339 // Decode ID data. Sections 5, 6, and 7 are reserved. 340 int section = ReadBits(&reader, 3); 341 RCHECK(section < 5); 342 343 // Next bit must be 1. 344 RCHECK(ReadBits(&reader, 1) == 1); 345 346 // Skip arbitrary bits. 347 reader.SkipBits(4); 348 349 int sequence_number = ReadBits(&reader, 4); 350 351 // Skip FSC. 352 reader.SkipBits(1); 353 354 // Next 3 bits must be 1. 355 RCHECK(ReadBits(&reader, 3) == 7); 356 357 int block_number = ReadBits(&reader, 8); 358 359 if (section == 0) { // Header. 360 // Validate the reserved bits in the next 8 bytes. 361 reader.SkipBits(1); 362 RCHECK(ReadBits(&reader, 1) == 0); 363 RCHECK(ReadBits(&reader, 11) == 0x7ff); 364 reader.SkipBits(4); 365 RCHECK(ReadBits(&reader, 4) == 0xf); 366 reader.SkipBits(4); 367 RCHECK(ReadBits(&reader, 4) == 0xf); 368 reader.SkipBits(4); 369 RCHECK(ReadBits(&reader, 4) == 0xf); 370 reader.SkipBits(3); 371 RCHECK(ReadBits(&reader, 24) == 0xffffff); 372 current_sequence_number = sequence_number; 373 for (size_t i = 0; i < arraysize(last_block_number); ++i) 374 last_block_number[i] = -1; 375 } else { 376 // Sequence number must match (this will also fail if no header seen). 377 RCHECK(sequence_number == current_sequence_number); 378 // Block number should be increasing. 379 RCHECK(block_number > last_block_number[section]); 380 last_block_number[section] = block_number; 381 } 382 383 // Move to next block. 384 offset += 80; 385 } 386 return true; 387 } 388 389 390 // Checks for a GSM container. 391 static bool CheckGsm(const uint8* buffer, int buffer_size) { 392 // Reference: ETSI EN 300 961 V8.1.1 393 // (http://www.etsi.org/deliver/etsi_en/300900_300999/300961/08.01.01_60/en_300961v080101p.pdf) 394 // also http://tools.ietf.org/html/rfc3551#page-24 395 // GSM files have a 33 byte block, only first 4 bits are fixed. 396 RCHECK(buffer_size >= 1024); // Need enough data to do a decent check. 397 398 int offset = 0; 399 while (offset < buffer_size) { 400 // First 4 bits of each block are xD. 401 RCHECK((buffer[offset] & 0xf0) == 0xd0); 402 offset += 33; 403 } 404 return true; 405 } 406 407 // Advance to the first set of |num_bits| bits that match |start_code|. |offset| 408 // is the current location in the buffer, and is updated. |bytes_needed| is the 409 // number of bytes that must remain in the buffer when |start_code| is found. 410 // Returns true if start_code found (and enough space in the buffer after it), 411 // false otherwise. 412 static bool AdvanceToStartCode(const uint8* buffer, 413 int buffer_size, 414 int* offset, 415 int bytes_needed, 416 int num_bits, 417 uint32 start_code) { 418 DCHECK_GE(bytes_needed, 3); 419 DCHECK_LE(num_bits, 24); // Only supports up to 24 bits. 420 421 // Create a mask to isolate |num_bits| bits, once shifted over. 422 uint32 bits_to_shift = 24 - num_bits; 423 uint32 mask = (1 << num_bits) - 1; 424 while (*offset + bytes_needed < buffer_size) { 425 uint32 next = Read24(buffer + *offset); 426 if (((next >> bits_to_shift) & mask) == start_code) 427 return true; 428 ++(*offset); 429 } 430 return false; 431 } 432 433 // Checks for an H.261 container. 434 static bool CheckH261(const uint8* buffer, int buffer_size) { 435 // Reference: ITU-T Recommendation H.261 (03/1993) 436 // (http://www.itu.int/rec/T-REC-H.261-199303-I/en) 437 RCHECK(buffer_size > 16); 438 439 int offset = 0; 440 bool seen_start_code = false; 441 while (true) { 442 // Advance to picture_start_code, if there is one. 443 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 20, 0x10)) { 444 // No start code found (or off end of buffer), so success if 445 // there was at least one valid header. 446 return seen_start_code; 447 } 448 449 // Now verify the block. AdvanceToStartCode() made sure that there are 450 // at least 4 bytes remaining in the buffer. 451 BitReader reader(buffer + offset, buffer_size - offset); 452 RCHECK(ReadBits(&reader, 20) == 0x10); 453 454 // Skip the temporal reference and PTYPE. 455 reader.SkipBits(5 + 6); 456 457 // Skip any extra insertion information. Since this is open-ended, if we run 458 // out of bits assume that the buffer is correctly formatted. 459 int extra = ReadBits(&reader, 1); 460 while (extra == 1) { 461 if (!reader.SkipBits(8)) 462 return seen_start_code; 463 if (!reader.ReadBits(1, &extra)) 464 return seen_start_code; 465 } 466 467 // Next should be a Group of Blocks start code. Again, if we run out of 468 // bits, then assume that the buffer up to here is correct, and the buffer 469 // just happened to end in the middle of a header. 470 int next; 471 if (!reader.ReadBits(16, &next)) 472 return seen_start_code; 473 RCHECK(next == 1); 474 475 // Move to the next block. 476 seen_start_code = true; 477 offset += 4; 478 } 479 } 480 481 // Checks for an H.263 container. 482 static bool CheckH263(const uint8* buffer, int buffer_size) { 483 // Reference: ITU-T Recommendation H.263 (01/2005) 484 // (http://www.itu.int/rec/T-REC-H.263-200501-I/en) 485 // header is PSC(22b) + TR(8b) + PTYPE(8+b). 486 RCHECK(buffer_size > 16); 487 488 int offset = 0; 489 bool seen_start_code = false; 490 while (true) { 491 // Advance to picture_start_code, if there is one. 492 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 9, 22, 0x20)) { 493 // No start code found (or off end of buffer), so success if 494 // there was at least one valid header. 495 return seen_start_code; 496 } 497 498 // Now verify the block. AdvanceToStartCode() made sure that there are 499 // at least 9 bytes remaining in the buffer. 500 BitReader reader(buffer + offset, 9); 501 RCHECK(ReadBits(&reader, 22) == 0x20); 502 503 // Skip the temporal reference. 504 reader.SkipBits(8); 505 506 // Verify that the first 2 bits of PTYPE are 10b. 507 RCHECK(ReadBits(&reader, 2) == 2); 508 509 // Skip the split screen indicator, document camera indicator, and full 510 // picture freeze release. 511 reader.SkipBits(1 + 1 + 1); 512 513 // Verify Source Format. 514 int format = ReadBits(&reader, 3); 515 RCHECK(format != 0 && format != 6); // Forbidden or reserved. 516 517 if (format == 7) { 518 // Verify full extended PTYPE. 519 int ufep = ReadBits(&reader, 3); 520 if (ufep == 1) { 521 // Verify the optional part of PLUSPTYPE. 522 format = ReadBits(&reader, 3); 523 RCHECK(format != 0 && format != 7); // Reserved. 524 reader.SkipBits(11); 525 // Next 4 bits should be b1000. 526 RCHECK(ReadBits(&reader, 4) == 8); // Not allowed. 527 } else { 528 RCHECK(ufep == 0); // Only 0 and 1 allowed. 529 } 530 531 // Verify picture type code is not a reserved value. 532 int picture_type_code = ReadBits(&reader, 3); 533 RCHECK(picture_type_code != 6 && picture_type_code != 7); // Reserved. 534 535 // Skip picture resampling mode, reduced resolution mode, 536 // and rounding type. 537 reader.SkipBits(1 + 1 + 1); 538 539 // Next 3 bits should be b001. 540 RCHECK(ReadBits(&reader, 3) == 1); // Not allowed. 541 } 542 543 // Move to the next block. 544 seen_start_code = true; 545 offset += 9; 546 } 547 } 548 549 // Checks for an H.264 container. 550 static bool CheckH264(const uint8* buffer, int buffer_size) { 551 // Reference: ITU-T Recommendation H.264 (01/2012) 552 // (http://www.itu.int/rec/T-REC-H.264) 553 // Section B.1: Byte stream NAL unit syntax and semantics. 554 RCHECK(buffer_size > 4); 555 556 int offset = 0; 557 int parameter_count = 0; 558 while (true) { 559 // Advance to picture_start_code, if there is one. 560 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 24, 1)) { 561 // No start code found (or off end of buffer), so success if 562 // there was at least one valid header. 563 return parameter_count > 0; 564 } 565 566 // Now verify the block. AdvanceToStartCode() made sure that there are 567 // at least 4 bytes remaining in the buffer. 568 BitReader reader(buffer + offset, 4); 569 RCHECK(ReadBits(&reader, 24) == 1); 570 571 // Verify forbidden_zero_bit. 572 RCHECK(ReadBits(&reader, 1) == 0); 573 574 // Extract nal_ref_idc and nal_unit_type. 575 int nal_ref_idc = ReadBits(&reader, 2); 576 int nal_unit_type = ReadBits(&reader, 5); 577 578 switch (nal_unit_type) { 579 case 5: // Coded slice of an IDR picture. 580 RCHECK(nal_ref_idc != 0); 581 break; 582 case 6: // Supplemental enhancement information (SEI). 583 case 9: // Access unit delimiter. 584 case 10: // End of sequence. 585 case 11: // End of stream. 586 case 12: // Filler data. 587 RCHECK(nal_ref_idc == 0); 588 break; 589 case 7: // Sequence parameter set. 590 case 8: // Picture parameter set. 591 ++parameter_count; 592 break; 593 } 594 595 // Skip the current start_code_prefix and move to the next. 596 offset += 4; 597 } 598 } 599 600 static const char kHlsSignature[] = "#EXTM3U"; 601 static const char kHls1[] = "#EXT-X-STREAM-INF:"; 602 static const char kHls2[] = "#EXT-X-TARGETDURATION:"; 603 static const char kHls3[] = "#EXT-X-MEDIA-SEQUENCE:"; 604 605 // Additional checks for a HLS container. 606 static bool CheckHls(const uint8* buffer, int buffer_size) { 607 // HLS is simply a play list used for Apple HTTP Live Streaming. 608 // Reference: Apple HTTP Live Streaming Overview 609 // (http://goo.gl/MIwxj) 610 611 if (StartsWith(buffer, buffer_size, kHlsSignature)) { 612 // Need to find "#EXT-X-STREAM-INF:", "#EXT-X-TARGETDURATION:", or 613 // "#EXT-X-MEDIA-SEQUENCE:" somewhere in the buffer. Other playlists (like 614 // WinAmp) only have additional lines with #EXTINF 615 // (http://en.wikipedia.org/wiki/M3U). 616 int offset = strlen(kHlsSignature); 617 while (offset < buffer_size) { 618 if (buffer[offset] == '#') { 619 if (StartsWith(buffer + offset, buffer_size - offset, kHls1) || 620 StartsWith(buffer + offset, buffer_size - offset, kHls2) || 621 StartsWith(buffer + offset, buffer_size - offset, kHls3)) { 622 return true; 623 } 624 } 625 ++offset; 626 } 627 } 628 return false; 629 } 630 631 // Checks for a MJPEG stream. 632 static bool CheckMJpeg(const uint8* buffer, int buffer_size) { 633 // Reference: ISO/IEC 10918-1 : 1993(E), Annex B 634 // (http://www.w3.org/Graphics/JPEG/itu-t81.pdf) 635 RCHECK(buffer_size >= 16); 636 637 int offset = 0; 638 int last_restart = -1; 639 int num_codes = 0; 640 while (offset + 5 < buffer_size) { 641 // Marker codes are always a two byte code with the first byte xFF. 642 RCHECK(buffer[offset] == 0xff); 643 uint8 code = buffer[offset + 1]; 644 RCHECK(code >= 0xc0 || code == 1); 645 646 // Skip sequences of xFF. 647 if (code == 0xff) { 648 ++offset; 649 continue; 650 } 651 652 // Success if the next marker code is EOI (end of image) 653 if (code == 0xd9) 654 return true; 655 656 // Check remaining codes. 657 if (code == 0xd8 || code == 1) { 658 // SOI (start of image) / TEM (private use). No other data with header. 659 offset += 2; 660 } else if (code >= 0xd0 && code <= 0xd7) { 661 // RST (restart) codes must be in sequence. No other data with header. 662 int restart = code & 0x07; 663 if (last_restart >= 0) 664 RCHECK(restart == (last_restart + 1) % 8); 665 last_restart = restart; 666 offset += 2; 667 } else { 668 // All remaining marker codes are followed by a length of the header. 669 int length = Read16(buffer + offset + 2) + 2; 670 671 // Special handling of SOS (start of scan) marker since the entropy 672 // coded data follows the SOS. Any xFF byte in the data block must be 673 // followed by x00 in the data. 674 if (code == 0xda) { 675 int number_components = buffer[offset + 4]; 676 RCHECK(length == 8 + 2 * number_components); 677 678 // Advance to the next marker. 679 offset += length; 680 while (offset + 2 < buffer_size) { 681 if (buffer[offset] == 0xff && buffer[offset + 1] != 0) 682 break; 683 ++offset; 684 } 685 } else { 686 // Skip over the marker data for the other marker codes. 687 offset += length; 688 } 689 } 690 ++num_codes; 691 } 692 return (num_codes > 1); 693 } 694 695 enum Mpeg2StartCodes { 696 PROGRAM_END_CODE = 0xb9, 697 PACK_START_CODE = 0xba 698 }; 699 700 // Checks for a MPEG2 Program Stream. 701 static bool CheckMpeg2ProgramStream(const uint8* buffer, int buffer_size) { 702 // Reference: ISO/IEC 13818-1 : 2000 (E) / ITU-T Rec. H.222.0 (2000 E). 703 RCHECK(buffer_size > 14); 704 705 int offset = 0; 706 while (offset + 14 < buffer_size) { 707 BitReader reader(buffer + offset, 14); 708 709 // Must start with pack_start_code. 710 RCHECK(ReadBits(&reader, 24) == 1); 711 RCHECK(ReadBits(&reader, 8) == PACK_START_CODE); 712 713 // Determine MPEG version (MPEG1 has b0010, while MPEG2 has b01). 714 int mpeg_version = ReadBits(&reader, 2); 715 if (mpeg_version == 0) { 716 // MPEG1, 10 byte header 717 // Validate rest of version code 718 RCHECK(ReadBits(&reader, 2) == 2); 719 } else { 720 RCHECK(mpeg_version == 1); 721 } 722 723 // Skip system_clock_reference_base [32..30]. 724 reader.SkipBits(3); 725 726 // Verify marker bit. 727 RCHECK(ReadBits(&reader, 1) == 1); 728 729 // Skip system_clock_reference_base [29..15]. 730 reader.SkipBits(15); 731 732 // Verify next marker bit. 733 RCHECK(ReadBits(&reader, 1) == 1); 734 735 // Skip system_clock_reference_base [14..0]. 736 reader.SkipBits(15); 737 738 // Verify next marker bit. 739 RCHECK(ReadBits(&reader, 1) == 1); 740 741 if (mpeg_version == 0) { 742 // Verify second marker bit. 743 RCHECK(ReadBits(&reader, 1) == 1); 744 745 // Skip mux_rate. 746 reader.SkipBits(22); 747 748 // Verify next marker bit. 749 RCHECK(ReadBits(&reader, 1) == 1); 750 751 // Update offset to be after this header. 752 offset += 12; 753 } else { 754 // Must be MPEG2. 755 // Skip program_mux_rate. 756 reader.SkipBits(22); 757 758 // Verify pair of marker bits. 759 RCHECK(ReadBits(&reader, 2) == 3); 760 761 // Skip reserved. 762 reader.SkipBits(5); 763 764 // Update offset to be after this header. 765 int pack_stuffing_length = ReadBits(&reader, 3); 766 offset += 14 + pack_stuffing_length; 767 } 768 769 // Check for system headers and PES_packets. 770 while (offset + 6 < buffer_size && Read24(buffer + offset) == 1) { 771 // Next 8 bits determine stream type. 772 int stream_id = buffer[offset + 3]; 773 774 // Some stream types are reserved and shouldn't occur. 775 if (mpeg_version == 0) 776 RCHECK(stream_id != 0xbc && stream_id < 0xf0); 777 else 778 RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe); 779 780 // Some stream types are used for pack headers. 781 if (stream_id == PACK_START_CODE) // back to outer loop. 782 break; 783 if (stream_id == PROGRAM_END_CODE) // end of stream. 784 return true; 785 786 int pes_length = Read16(buffer + offset + 4); 787 RCHECK(pes_length > 0); 788 offset = offset + 6 + pes_length; 789 } 790 } 791 // Success as we are off the end of the buffer and liked everything 792 // in the buffer. 793 return true; 794 } 795 796 const uint8 kMpeg2SyncWord = 0x47; 797 798 // Checks for a MPEG2 Transport Stream. 799 static bool CheckMpeg2TransportStream(const uint8* buffer, int buffer_size) { 800 // Spec: ISO/IEC 13818-1 : 2000 (E) / ITU-T Rec. H.222.0 (2000 E). 801 // Normal packet size is 188 bytes. However, some systems add various error 802 // correction data at the end, resulting in packet of length 192/204/208 803 // (https://en.wikipedia.org/wiki/MPEG_transport_stream). Determine the 804 // length with the first packet. 805 RCHECK(buffer_size >= 250); // Want more than 1 packet to check. 806 807 int offset = 0; 808 int packet_length = -1; 809 while (buffer[offset] != kMpeg2SyncWord && offset < 20) { 810 // Skip over any header in the first 20 bytes. 811 ++offset; 812 } 813 814 while (offset + 6 < buffer_size) { 815 BitReader reader(buffer + offset, 6); 816 817 // Must start with sync byte. 818 RCHECK(ReadBits(&reader, 8) == kMpeg2SyncWord); 819 820 // Skip transport_error_indicator, payload_unit_start_indicator, and 821 // transport_priority. 822 reader.SkipBits(1 + 1 + 1); 823 824 // Verify the pid is not a reserved value. 825 int pid = ReadBits(&reader, 13); 826 RCHECK(pid < 3 || pid > 15); 827 828 // Skip transport_scrambling_control. 829 reader.SkipBits(2); 830 831 // Adaptation_field_control can not be 0. 832 int adaptation_field_control = ReadBits(&reader, 2); 833 RCHECK(adaptation_field_control != 0); 834 835 // If there is an adaptation_field, verify it. 836 if (adaptation_field_control >= 2) { 837 // Skip continuity_counter. 838 reader.SkipBits(4); 839 840 // Get adaptation_field_length and verify it. 841 int adaptation_field_length = ReadBits(&reader, 8); 842 if (adaptation_field_control == 2) 843 RCHECK(adaptation_field_length == 183); 844 else 845 RCHECK(adaptation_field_length <= 182); 846 } 847 848 // Attempt to determine the packet length on the first packet. 849 if (packet_length < 0) { 850 if (buffer[offset + 188] == kMpeg2SyncWord) 851 packet_length = 188; 852 else if (buffer[offset + 192] == kMpeg2SyncWord) 853 packet_length = 192; 854 else if (buffer[offset + 204] == kMpeg2SyncWord) 855 packet_length = 204; 856 else 857 packet_length = 208; 858 } 859 offset += packet_length; 860 } 861 return true; 862 } 863 864 enum Mpeg4StartCodes { 865 VISUAL_OBJECT_SEQUENCE_START_CODE = 0xb0, 866 VISUAL_OBJECT_SEQUENCE_END_CODE = 0xb1, 867 VISUAL_OBJECT_START_CODE = 0xb5, 868 VOP_START_CODE = 0xb6 869 }; 870 871 // Checks for a raw MPEG4 bitstream container. 872 static bool CheckMpeg4BitStream(const uint8* buffer, int buffer_size) { 873 // Defined in ISO/IEC 14496-2:2001. 874 // However, no length ... simply scan for start code values. 875 // Note tags are very similar to H.264. 876 RCHECK(buffer_size > 4); 877 878 int offset = 0; 879 int sequence_start_count = 0; 880 int sequence_end_count = 0; 881 int visual_object_count = 0; 882 int vop_count = 0; 883 while (true) { 884 // Advance to start_code, if there is one. 885 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 6, 24, 1)) { 886 // Not a complete sequence in memory, so return true if we've seen a 887 // visual_object_sequence_start_code and a visual_object_start_code. 888 return (sequence_start_count > 0 && visual_object_count > 0); 889 } 890 891 // Now verify the block. AdvanceToStartCode() made sure that there are 892 // at least 6 bytes remaining in the buffer. 893 BitReader reader(buffer + offset, 6); 894 RCHECK(ReadBits(&reader, 24) == 1); 895 896 int start_code = ReadBits(&reader, 8); 897 RCHECK(start_code < 0x30 || start_code > 0xaf); // 30..AF and 898 RCHECK(start_code < 0xb7 || start_code > 0xb9); // B7..B9 reserved 899 900 switch (start_code) { 901 case VISUAL_OBJECT_SEQUENCE_START_CODE: { 902 ++sequence_start_count; 903 // Verify profile in not one of many reserved values. 904 int profile = ReadBits(&reader, 8); 905 RCHECK(profile > 0); 906 RCHECK(profile < 0x04 || profile > 0x10); 907 RCHECK(profile < 0x13 || profile > 0x20); 908 RCHECK(profile < 0x23 || profile > 0x31); 909 RCHECK(profile < 0x35 || profile > 0x41); 910 RCHECK(profile < 0x43 || profile > 0x60); 911 RCHECK(profile < 0x65 || profile > 0x70); 912 RCHECK(profile < 0x73 || profile > 0x80); 913 RCHECK(profile < 0x83 || profile > 0x90); 914 RCHECK(profile < 0x95 || profile > 0xa0); 915 RCHECK(profile < 0xa4 || profile > 0xb0); 916 RCHECK(profile < 0xb5 || profile > 0xc0); 917 RCHECK(profile < 0xc3 || profile > 0xd0); 918 RCHECK(profile < 0xe4); 919 break; 920 } 921 922 case VISUAL_OBJECT_SEQUENCE_END_CODE: 923 RCHECK(++sequence_end_count == sequence_start_count); 924 break; 925 926 case VISUAL_OBJECT_START_CODE: { 927 ++visual_object_count; 928 if (ReadBits(&reader, 1) == 1) { 929 int visual_object_verid = ReadBits(&reader, 4); 930 RCHECK(visual_object_verid > 0 && visual_object_verid < 3); 931 RCHECK(ReadBits(&reader, 3) != 0); 932 } 933 int visual_object_type = ReadBits(&reader, 4); 934 RCHECK(visual_object_type > 0 && visual_object_type < 6); 935 break; 936 } 937 938 case VOP_START_CODE: 939 RCHECK(++vop_count <= visual_object_count); 940 break; 941 } 942 // Skip this block. 943 offset += 6; 944 } 945 } 946 947 // Additional checks for a MOV/QuickTime/MPEG4 container. 948 static bool CheckMov(const uint8* buffer, int buffer_size) { 949 // Reference: ISO/IEC 14496-12:2005(E). 950 // (http://standards.iso.org/ittf/PubliclyAvailableStandards/c061988_ISO_IEC_14496-12_2012.zip) 951 RCHECK(buffer_size > 8); 952 953 int offset = 0; 954 while (offset + 8 < buffer_size) { 955 int atomsize = Read32(buffer + offset); 956 uint32 atomtype = Read32(buffer + offset + 4); 957 // Only need to check for ones that are valid at the top level. 958 switch (atomtype) { 959 case TAG('f','t','y','p'): 960 case TAG('p','d','i','n'): 961 case TAG('m','o','o','v'): 962 case TAG('m','o','o','f'): 963 case TAG('m','f','r','a'): 964 case TAG('m','d','a','t'): 965 case TAG('f','r','e','e'): 966 case TAG('s','k','i','p'): 967 case TAG('m','e','t','a'): 968 case TAG('m','e','c','o'): 969 case TAG('s','t','y','p'): 970 case TAG('s','i','d','x'): 971 case TAG('s','s','i','x'): 972 case TAG('p','r','f','t'): 973 case TAG('b','l','o','c'): 974 break; 975 default: 976 return false; 977 } 978 if (atomsize == 1) { 979 // Indicates that the length is the next 64bits. 980 if (offset + 16 > buffer_size) 981 break; 982 if (Read32(buffer + offset + 8) != 0) 983 break; // Offset is way past buffer size. 984 atomsize = Read32(buffer + offset + 12); 985 } 986 if (atomsize <= 0) 987 break; // Indicates the last atom or length too big. 988 offset += atomsize; 989 } 990 return true; 991 } 992 993 enum MPEGVersion { 994 VERSION_25 = 0, 995 VERSION_RESERVED, 996 VERSION_2, 997 VERSION_1 998 }; 999 enum MPEGLayer { 1000 L_RESERVED = 0, 1001 LAYER_3, 1002 LAYER_2, 1003 LAYER_1 1004 }; 1005 1006 static int kSampleRateTable[4][4] = { { 11025, 12000, 8000, 0 }, // v2.5 1007 { 0, 0, 0, 0 }, // not used 1008 { 22050, 24000, 16000, 0 }, // v2 1009 { 44100, 48000, 32000, 0 } // v1 1010 }; 1011 1012 static int kBitRateTableV1L1[16] = { 0, 32, 64, 96, 128, 160, 192, 224, 256, 1013 288, 320, 352, 384, 416, 448, 0 }; 1014 static int kBitRateTableV1L2[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 1015 192, 224, 256, 320, 384, 0 }; 1016 static int kBitRateTableV1L3[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 1017 160, 192, 224, 256, 320, 0 }; 1018 static int kBitRateTableV2L1[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 1019 160, 176, 192, 224, 256, 0 }; 1020 static int kBitRateTableV2L23[16] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 1021 112, 128, 144, 160, 0 }; 1022 1023 static bool ValidMpegAudioFrameHeader(const uint8* header, 1024 int header_size, 1025 int* framesize) { 1026 // Reference: http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm. 1027 DCHECK_GE(header_size, 4); 1028 *framesize = 0; 1029 BitReader reader(header, 4); // Header can only be 4 bytes long. 1030 1031 // Verify frame sync (11 bits) are all set. 1032 RCHECK(ReadBits(&reader, 11) == 0x7ff); 1033 1034 // Verify MPEG audio version id. 1035 int version = ReadBits(&reader, 2); 1036 RCHECK(version != 1); // Reserved. 1037 1038 // Verify layer. 1039 int layer = ReadBits(&reader, 2); 1040 RCHECK(layer != 0); 1041 1042 // Skip protection bit. 1043 reader.SkipBits(1); 1044 1045 // Verify bitrate index. 1046 int bitrate_index = ReadBits(&reader, 4); 1047 RCHECK(bitrate_index != 0xf); 1048 1049 // Verify sampling rate frequency index. 1050 int sampling_index = ReadBits(&reader, 2); 1051 RCHECK(sampling_index != 3); 1052 1053 // Get padding bit. 1054 int padding = ReadBits(&reader, 1); 1055 1056 // Frame size: 1057 // For Layer I files = (12 * BitRate / SampleRate + Padding) * 4 1058 // For others = 144 * BitRate / SampleRate + Padding 1059 // Unfortunately, BitRate and SampleRate are coded. 1060 int sampling_rate = kSampleRateTable[version][sampling_index]; 1061 int bitrate; 1062 if (version == VERSION_1) { 1063 if (layer == LAYER_1) 1064 bitrate = kBitRateTableV1L1[bitrate_index]; 1065 else if (layer == LAYER_2) 1066 bitrate = kBitRateTableV1L2[bitrate_index]; 1067 else 1068 bitrate = kBitRateTableV1L3[bitrate_index]; 1069 } else { 1070 if (layer == LAYER_1) 1071 bitrate = kBitRateTableV2L1[bitrate_index]; 1072 else 1073 bitrate = kBitRateTableV2L23[bitrate_index]; 1074 } 1075 if (layer == LAYER_1) 1076 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4; 1077 else 1078 *framesize = (144000 * bitrate) / sampling_rate + padding; 1079 return (bitrate > 0 && sampling_rate > 0); 1080 } 1081 1082 // Extract a size encoded the MP3 way. 1083 static int GetMp3HeaderSize(const uint8* buffer, int buffer_size) { 1084 DCHECK_GE(buffer_size, 9); 1085 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) + 1086 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10; 1087 if (buffer[5] & 0x10) // Footer added? 1088 size += 10; 1089 return size; 1090 } 1091 1092 // Additional checks for a MP3 container. 1093 static bool CheckMp3(const uint8* buffer, int buffer_size, bool seenHeader) { 1094 RCHECK(buffer_size >= 10); // Must be enough to read the initial header. 1095 1096 int framesize; 1097 int numSeen = 0; 1098 int offset = 0; 1099 if (seenHeader) { 1100 offset = GetMp3HeaderSize(buffer, buffer_size); 1101 } else { 1102 // Skip over leading 0's. 1103 while (offset < buffer_size && buffer[offset] == 0) 1104 ++offset; 1105 } 1106 1107 while (offset + 3 < buffer_size) { 1108 RCHECK(ValidMpegAudioFrameHeader( 1109 buffer + offset, buffer_size - offset, &framesize)); 1110 1111 // Have we seen enough valid headers? 1112 if (++numSeen > 10) 1113 return true; 1114 offset += framesize; 1115 } 1116 // Off the end of the buffer, return success if a few valid headers seen. 1117 return numSeen > 2; 1118 } 1119 1120 // Check that the next characters in |buffer| represent a number. The format 1121 // accepted is optional whitespace followed by 1 or more digits. |max_digits| 1122 // specifies the maximum number of digits to process. Returns true if a valid 1123 // number is found, false otherwise. 1124 static bool VerifyNumber(const uint8* buffer, 1125 int buffer_size, 1126 int* offset, 1127 int max_digits) { 1128 RCHECK(*offset < buffer_size); 1129 1130 // Skip over any leading space. 1131 while (isspace(buffer[*offset])) { 1132 ++(*offset); 1133 RCHECK(*offset < buffer_size); 1134 } 1135 1136 // Need to process up to max_digits digits. 1137 int numSeen = 0; 1138 while (--max_digits >= 0 && isdigit(buffer[*offset])) { 1139 ++numSeen; 1140 ++(*offset); 1141 if (*offset >= buffer_size) 1142 return true; // Out of space but seen a digit. 1143 } 1144 1145 // Success if at least one digit seen. 1146 return (numSeen > 0); 1147 } 1148 1149 // Check that the next character in |buffer| is one of |c1| or |c2|. |c2| is 1150 // optional. Returns true if there is a match, false if no match or out of 1151 // space. 1152 static inline bool VerifyCharacters(const uint8* buffer, 1153 int buffer_size, 1154 int* offset, 1155 char c1, 1156 char c2) { 1157 RCHECK(*offset < buffer_size); 1158 char c = static_cast<char>(buffer[(*offset)++]); 1159 return (c == c1 || (c == c2 && c2 != 0)); 1160 } 1161 1162 // Checks for a SRT container. 1163 static bool CheckSrt(const uint8* buffer, int buffer_size) { 1164 // Reference: http://en.wikipedia.org/wiki/SubRip 1165 RCHECK(buffer_size > 20); 1166 1167 // First line should just be the subtitle sequence number. 1168 int offset = StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0; 1169 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100)); 1170 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, '\n', '\r')); 1171 1172 // Skip any additional \n\r. 1173 while (VerifyCharacters(buffer, buffer_size, &offset, '\n', '\r')) {} 1174 --offset; // Since VerifyCharacters() gobbled up the next non-CR/LF. 1175 1176 // Second line should look like the following: 1177 // 00:00:10,500 --> 00:00:13,000 1178 // Units separator can be , or . 1179 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100)); 1180 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, ':', 0)); 1181 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2)); 1182 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, ':', 0)); 1183 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2)); 1184 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, ',', '.')); 1185 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3)); 1186 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, ' ', 0)); 1187 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, '-', 0)); 1188 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, '-', 0)); 1189 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, '>', 0)); 1190 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, ' ', 0)); 1191 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100)); 1192 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, ':', 0)); 1193 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2)); 1194 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, ':', 0)); 1195 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2)); 1196 RCHECK(VerifyCharacters(buffer, buffer_size, &offset, ',', '.')); 1197 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3)); 1198 return true; 1199 } 1200 1201 // Read a Matroska Element Id. 1202 static int GetElementId(BitReader* reader) { 1203 // Element ID is coded with the leading zero bits (max 3) determining size. 1204 // If it is an invalid encoding or the end of the buffer is reached, 1205 // return -1 as a tag that won't be expected. 1206 if (reader->bits_available() >= 8) { 1207 int num_bits_to_read = 0; 1208 static int prefix[] = { 0x80, 0x4000, 0x200000, 0x10000000 }; 1209 for (int i = 0; i < 4; ++i) { 1210 num_bits_to_read += 7; 1211 if (ReadBits(reader, 1) == 1) { 1212 if (reader->bits_available() < num_bits_to_read) 1213 break; 1214 // prefix[] adds back the bits read individually. 1215 return ReadBits(reader, num_bits_to_read) | prefix[i]; 1216 } 1217 } 1218 } 1219 // Invalid encoding, return something not expected. 1220 return -1; 1221 } 1222 1223 // Read a Matroska Unsigned Integer (VINT). 1224 static uint64 GetVint(BitReader* reader) { 1225 // Values are coded with the leading zero bits (max 7) determining size. 1226 // If it is an invalid coding or the end of the buffer is reached, 1227 // return something that will go off the end of the buffer. 1228 if (reader->bits_available() >= 8) { 1229 int num_bits_to_read = 0; 1230 for (int i = 0; i < 8; ++i) { 1231 num_bits_to_read += 7; 1232 if (ReadBits(reader, 1) == 1) { 1233 if (reader->bits_available() < num_bits_to_read) 1234 break; 1235 return ReadBits(reader, num_bits_to_read); 1236 } 1237 } 1238 } 1239 // Incorrect format (more than 7 leading 0's) or off the end of the buffer. 1240 // Since the return value is used as a byte size, return a value that will 1241 // cause a failure when used. 1242 return (reader->bits_available() / 8) + 2; 1243 } 1244 1245 // Additional checks for a WEBM container. 1246 static bool CheckWebm(const uint8* buffer, int buffer_size) { 1247 // Reference: http://www.matroska.org/technical/specs/index.html 1248 RCHECK(buffer_size > 12); 1249 1250 BitReader reader(buffer, buffer_size); 1251 1252 // Verify starting Element Id. 1253 RCHECK(GetElementId(&reader) == 0x1a45dfa3); 1254 1255 // Get the header size, and ensure there are enough bits to check. 1256 int header_size = GetVint(&reader); 1257 RCHECK(reader.bits_available() / 8 >= header_size); 1258 1259 // Loop through the header. 1260 while (reader.bits_available() > 0) { 1261 int tag = GetElementId(&reader); 1262 int tagsize = GetVint(&reader); 1263 switch (tag) { 1264 case 0x4286: // EBMLVersion 1265 case 0x42f7: // EBMLReadVersion 1266 case 0x42f2: // EBMLMaxIdLength 1267 case 0x42f3: // EBMLMaxSizeLength 1268 case 0x4287: // DocTypeVersion 1269 case 0x4285: // DocTypeReadVersion 1270 case 0xec: // void 1271 case 0xbf: // CRC32 1272 RCHECK(reader.SkipBits(tagsize * 8)); 1273 break; 1274 1275 case 0x4282: // EBMLDocType 1276 // Need to see "webm" or "matroska" next. 1277 switch (ReadBits(&reader, 32)) { 1278 case TAG('w', 'e', 'b', 'm') : 1279 return true; 1280 case TAG('m', 'a', 't', 'r') : 1281 return (ReadBits(&reader, 32) == TAG('o', 's', 'k', 'a')); 1282 } 1283 return false; 1284 1285 default: // Unrecognized tag 1286 return false; 1287 } 1288 } 1289 return false; 1290 } 1291 1292 enum VC1StartCodes { 1293 VC1_FRAME_START_CODE = 0x0d, 1294 VC1_ENTRY_POINT_START_CODE = 0x0e, 1295 VC1_SEQUENCE_START_CODE = 0x0f 1296 }; 1297 1298 // Checks for a VC1 bitstream container. 1299 static bool CheckVC1(const uint8* buffer, int buffer_size) { 1300 // Reference: SMPTE 421M 1301 // (http://standards.smpte.org/content/978-1-61482-555-5/st-421-2006/SEC1.body.pdf) 1302 // However, no length ... simply scan for start code values. 1303 // Expect to see SEQ | [ [ ENTRY ] PIC* ]* 1304 // Note tags are very similar to H.264. 1305 1306 RCHECK(buffer_size >= 24); 1307 1308 // First check for Bitstream Metadata Serialization (Annex L) 1309 if (buffer[0] == 0xc5 && 1310 Read32(buffer + 4) == 0x04 && 1311 Read32(buffer + 20) == 0x0c) { 1312 // Verify settings in STRUCT_C and STRUCT_A 1313 BitReader reader(buffer + 8, 12); 1314 1315 int profile = ReadBits(&reader, 4); 1316 if (profile == 0 || profile == 4) { // simple or main 1317 // Skip FRMRTQ_POSTPROC, BITRTQ_POSTPROC, and LOOPFILTER. 1318 reader.SkipBits(3 + 5 + 1); 1319 1320 // Next bit must be 0. 1321 RCHECK(ReadBits(&reader, 1) == 0); 1322 1323 // Skip MULTIRES. 1324 reader.SkipBits(1); 1325 1326 // Next bit must be 1. 1327 RCHECK(ReadBits(&reader, 1) == 1); 1328 1329 // Skip FASTUVMC, EXTENDED_MV, DQUANT, and VSTRANSFORM. 1330 reader.SkipBits(1 + 1 + 2 + 1); 1331 1332 // Next bit must be 0. 1333 RCHECK(ReadBits(&reader, 1) == 0); 1334 1335 // Skip OVERLAP, SYNCMARKER, RANGERED, MAXBFRAMES, QUANTIZER, and 1336 // FINTERPFLAG. 1337 reader.SkipBits(1 + 1 + 1 + 3 + 2 + 1); 1338 1339 // Next bit must be 1. 1340 RCHECK(ReadBits(&reader, 1) == 1); 1341 1342 } else { 1343 RCHECK(profile == 12); // Other profile values not allowed. 1344 RCHECK(ReadBits(&reader, 28) == 0); 1345 } 1346 1347 // Now check HORIZ_SIZE and VERT_SIZE, which must be 8192 or less. 1348 RCHECK(ReadBits(&reader, 32) <= 8192); 1349 RCHECK(ReadBits(&reader, 32) <= 8192); 1350 return true; 1351 } 1352 1353 // Buffer isn't Bitstream Metadata, so scan for start codes. 1354 int offset = 0; 1355 int sequence_start_code = 0; 1356 int frame_start_code = 0; 1357 while (true) { 1358 // Advance to start_code, if there is one. 1359 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 5, 24, 1)) { 1360 // Not a complete sequence in memory, so return true if we've seen a 1361 // sequence start and a frame start (not checking entry points since 1362 // they only occur in advanced profiles). 1363 return (sequence_start_code > 0 && frame_start_code > 0); 1364 } 1365 1366 // Now verify the block. AdvanceToStartCode() made sure that there are 1367 // at least 5 bytes remaining in the buffer. 1368 BitReader reader(buffer + offset, 5); 1369 RCHECK(ReadBits(&reader, 24) == 1); 1370 1371 // Keep track of the number of certain types received. 1372 switch (ReadBits(&reader, 8)) { 1373 case VC1_SEQUENCE_START_CODE: { 1374 ++sequence_start_code; 1375 switch (ReadBits(&reader, 2)) { 1376 case 0: // simple 1377 case 1: // main 1378 RCHECK(ReadBits(&reader, 2) == 0); 1379 break; 1380 case 2: // complex 1381 return false; 1382 case 3: // advanced 1383 RCHECK(ReadBits(&reader, 3) <= 4); // Verify level = 0..4 1384 RCHECK(ReadBits(&reader, 2) == 1); // Verify colordiff_format = 1 1385 break; 1386 } 1387 break; 1388 } 1389 1390 case VC1_ENTRY_POINT_START_CODE: 1391 // No fields in entry data to check. However, it must occur after 1392 // sequence header. 1393 RCHECK(sequence_start_code > 0); 1394 break; 1395 1396 case VC1_FRAME_START_CODE: 1397 ++frame_start_code; 1398 break; 1399 } 1400 offset += 5; 1401 } 1402 } 1403 1404 // For some formats the signature is a bunch of characters. They are defined 1405 // below. Note that the first 4 characters of the string may be used as a TAG 1406 // in LookupContainerByFirst4. For signatures that contain embedded \0, use 1407 // uint8[]. 1408 static const char kAmrSignature[] = "#!AMR"; 1409 static const uint8 kAsfSignature[] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66, 0xcf, 1410 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 1411 0xce, 0x6c }; 1412 static const char kAssSignature[] = "[Script Info]"; 1413 static const char kAssBomSignature[] = UTF8_BYTE_ORDER_MARK "[Script Info]"; 1414 static const uint8 kWtvSignature[] = { 0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49, 0xda, 1415 0x11, 0xa6, 0x4e, 0x00, 0x07, 0xe9, 0x5e, 1416 0xad, 0x8d }; 1417 1418 // Attempt to determine the container type from the buffer provided. This is 1419 // a simple pass, that uses the first 4 bytes of the buffer as an index to get 1420 // a rough idea of the container format. 1421 static MediaContainerName LookupContainerByFirst4(const uint8* buffer, 1422 int buffer_size) { 1423 // Minimum size that the code expects to exist without checking size. 1424 if (buffer_size < 12) 1425 return CONTAINER_UNKNOWN; 1426 1427 uint32 first4 = Read32(buffer); 1428 switch (first4) { 1429 case 0x1a45dfa3: 1430 if (CheckWebm(buffer, buffer_size)) 1431 return CONTAINER_WEBM; 1432 break; 1433 1434 case 0x3026b275: 1435 if (StartsWith(buffer, 1436 buffer_size, 1437 kAsfSignature, 1438 sizeof(kAsfSignature))) { 1439 return CONTAINER_ASF; 1440 } 1441 break; 1442 1443 case TAG('#','!','A','M'): 1444 if (StartsWith(buffer, buffer_size, kAmrSignature)) 1445 return CONTAINER_AMR; 1446 break; 1447 1448 case TAG('#','E','X','T'): 1449 if (CheckHls(buffer, buffer_size)) 1450 return CONTAINER_HLS; 1451 break; 1452 1453 case TAG('.','R','M','F'): 1454 if (buffer[4] == 0 && buffer[5] == 0) 1455 return CONTAINER_RM; 1456 break; 1457 1458 case TAG('.','r','a','\xfd'): 1459 return CONTAINER_RM; 1460 1461 case TAG('B','I','K','b'): 1462 case TAG('B','I','K','d'): 1463 case TAG('B','I','K','f'): 1464 case TAG('B','I','K','g'): 1465 case TAG('B','I','K','h'): 1466 case TAG('B','I','K','i'): 1467 if (CheckBink(buffer, buffer_size)) 1468 return CONTAINER_BINK; 1469 break; 1470 1471 case TAG('c','a','f','f'): 1472 if (CheckCaf(buffer, buffer_size)) 1473 return CONTAINER_CAF; 1474 break; 1475 1476 case TAG('D','E','X','A'): 1477 if (buffer_size > 15 && 1478 Read16(buffer + 11) <= 2048 && 1479 Read16(buffer + 13) <= 2048) { 1480 return CONTAINER_DXA; 1481 } 1482 break; 1483 1484 case TAG('D','T','S','H'): 1485 if (Read32(buffer + 4) == TAG('D','H','D','R')) 1486 return CONTAINER_DTSHD; 1487 break; 1488 1489 case 0x64a30100: 1490 case 0x64a30200: 1491 case 0x64a30300: 1492 case 0x64a30400: 1493 case 0x0001a364: 1494 case 0x0002a364: 1495 case 0x0003a364: 1496 if (Read32(buffer + 4) != 0 && Read32(buffer + 8) != 0) 1497 return CONTAINER_IRCAM; 1498 break; 1499 1500 case TAG('f','L','a','C'): 1501 return CONTAINER_FLAC; 1502 1503 case TAG('F','L','V',0): 1504 case TAG('F','L','V',1): 1505 case TAG('F','L','V',2): 1506 case TAG('F','L','V',3): 1507 case TAG('F','L','V',4): 1508 if (buffer[5] == 0 && Read32(buffer + 5) > 8) 1509 return CONTAINER_FLV; 1510 break; 1511 1512 case TAG('F','O','R','M'): 1513 switch (Read32(buffer + 8)) { 1514 case TAG('A','I','F','F'): 1515 case TAG('A','I','F','C'): 1516 return CONTAINER_AIFF; 1517 } 1518 break; 1519 1520 case TAG('M','A','C',' '): 1521 return CONTAINER_APE; 1522 1523 case TAG('O','N','2',' '): 1524 if (Read32(buffer + 8) == TAG('O','N','2','f')) 1525 return CONTAINER_AVI; 1526 break; 1527 1528 case TAG('O','g','g','S'): 1529 if (buffer[5] <= 7) 1530 return CONTAINER_OGG; 1531 break; 1532 1533 case TAG('R','F','6','4'): 1534 if (buffer_size > 16 && Read32(buffer + 12) == TAG('d','s','6','4')) 1535 return CONTAINER_WAV; 1536 break; 1537 1538 case TAG('R','I','F','F'): 1539 switch (Read32(buffer + 8)) { 1540 case TAG('A','V','I',' '): 1541 case TAG('A','V','I','X'): 1542 case TAG('A','V','I','\x19'): 1543 case TAG('A','M','V',' '): 1544 return CONTAINER_AVI; 1545 case TAG('W','A','V','E'): 1546 return CONTAINER_WAV; 1547 } 1548 break; 1549 1550 case TAG('[','S','c','r'): 1551 if (StartsWith(buffer, buffer_size, kAssSignature)) 1552 return CONTAINER_ASS; 1553 break; 1554 1555 case TAG('\xef','\xbb','\xbf','['): 1556 if (StartsWith(buffer, buffer_size, kAssBomSignature)) 1557 return CONTAINER_ASS; 1558 break; 1559 1560 case 0x7ffe8001: 1561 case 0xfe7f0180: 1562 case 0x1fffe800: 1563 case 0xff1f00e8: 1564 if (CheckDts(buffer, buffer_size)) 1565 return CONTAINER_DTS; 1566 break; 1567 1568 case 0xb7d80020: 1569 if (StartsWith(buffer, 1570 buffer_size, 1571 kWtvSignature, 1572 sizeof(kWtvSignature))) { 1573 return CONTAINER_WTV; 1574 } 1575 break; 1576 } 1577 1578 // Now try a few different ones that look at something other 1579 // than the first 4 bytes. 1580 uint32 first3 = first4 & 0xffffff00; 1581 switch (first3) { 1582 case TAG('C','W','S',0): 1583 case TAG('F','W','S',0): 1584 return CONTAINER_SWF; 1585 1586 case TAG('I','D','3',0): 1587 if (CheckMp3(buffer, buffer_size, true)) 1588 return CONTAINER_MP3; 1589 break; 1590 } 1591 1592 // Maybe the first 2 characters are something we can use. 1593 uint32 first2 = Read16(buffer); 1594 switch (first2) { 1595 case kAc3SyncWord: 1596 if (CheckAc3(buffer, buffer_size)) 1597 return CONTAINER_AC3; 1598 if (CheckEac3(buffer, buffer_size)) 1599 return CONTAINER_EAC3; 1600 break; 1601 1602 case 0xfff0: 1603 case 0xfff1: 1604 case 0xfff8: 1605 case 0xfff9: 1606 if (CheckAac(buffer, buffer_size)) 1607 return CONTAINER_AAC; 1608 break; 1609 } 1610 1611 // Check if the file is in MP3 format without the header. 1612 if (CheckMp3(buffer, buffer_size, false)) 1613 return CONTAINER_MP3; 1614 1615 return CONTAINER_UNKNOWN; 1616 } 1617 1618 // Attempt to determine the container name from the buffer provided. 1619 MediaContainerName DetermineContainer(const uint8* buffer, int buffer_size) { 1620 DCHECK(buffer); 1621 1622 // Since MOV/QuickTime/MPEG4 streams are common, check for them first. 1623 if (CheckMov(buffer, buffer_size)) 1624 return CONTAINER_MOV; 1625 1626 // Next attempt the simple checks, that typically look at just the 1627 // first few bytes of the file. 1628 MediaContainerName result = LookupContainerByFirst4(buffer, buffer_size); 1629 if (result != CONTAINER_UNKNOWN) 1630 return result; 1631 1632 // Additional checks that may scan a portion of the buffer. 1633 if (CheckMpeg2ProgramStream(buffer, buffer_size)) 1634 return CONTAINER_MPEG2PS; 1635 if (CheckMpeg2TransportStream(buffer, buffer_size)) 1636 return CONTAINER_MPEG2TS; 1637 if (CheckMJpeg(buffer, buffer_size)) 1638 return CONTAINER_MJPEG; 1639 if (CheckDV(buffer, buffer_size)) 1640 return CONTAINER_DV; 1641 if (CheckH261(buffer, buffer_size)) 1642 return CONTAINER_H261; 1643 if (CheckH263(buffer, buffer_size)) 1644 return CONTAINER_H263; 1645 if (CheckH264(buffer, buffer_size)) 1646 return CONTAINER_H264; 1647 if (CheckMpeg4BitStream(buffer, buffer_size)) 1648 return CONTAINER_MPEG4BS; 1649 if (CheckVC1(buffer, buffer_size)) 1650 return CONTAINER_VC1; 1651 if (CheckSrt(buffer, buffer_size)) 1652 return CONTAINER_SRT; 1653 if (CheckGsm(buffer, buffer_size)) 1654 return CONTAINER_GSM; 1655 1656 // AC3/EAC3 might not start at the beginning of the stream, 1657 // so scan for a start code. 1658 int offset = 1; // No need to start at byte 0 due to First4 check. 1659 if (AdvanceToStartCode(buffer, buffer_size, &offset, 4, 16, kAc3SyncWord)) { 1660 if (CheckAc3(buffer + offset, buffer_size - offset)) 1661 return CONTAINER_AC3; 1662 if (CheckEac3(buffer + offset, buffer_size - offset)) 1663 return CONTAINER_EAC3; 1664 } 1665 1666 return CONTAINER_UNKNOWN; 1667 } 1668 1669 } // namespace container_names 1670 1671 } // namespace media 1672