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.
5 #include "media/base/container_names.h"
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "media/base/bit_reader.h"
16 namespace container_names
{
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)))
28 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
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];
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];
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];
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];
50 // Helper function to do buffer comparisons with a string without going off the
52 static bool StartsWith(const uint8
* buffer
,
55 size_t prefix_size
= strlen(prefix
);
56 return (prefix_size
<= buffer_size
&&
57 memcmp(buffer
, prefix
, prefix_size
) == 0);
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
,
66 return (prefix_size
<= buffer_size
&&
67 memcmp(buffer
, prefix
, prefix_size
) == 0);
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));
75 reader
->ReadBits(num_bits
, &value
);
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 }
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);
100 while (offset
+ 6 < buffer_size
) {
101 BitReader
reader(buffer
+ offset
, 6);
103 // Syncword must be 0xfff.
104 RCHECK(ReadBits(&reader
, 12) == 0xfff);
106 // Skip MPEG version.
109 // Layer is always 0.
110 RCHECK(ReadBits(&reader
, 2) == 0);
112 // Skip protection + profile.
113 reader
.SkipBits(1 + 2);
115 // Check sampling frequency index.
116 RCHECK(ReadBits(&reader
, 4) != 15); // Forbidden.
118 // Skip private stream, channel configuration, originality, home,
119 // copyrighted stream, and copyright_start.
120 reader
.SkipBits(1 + 3 + 1 + 1 + 1 + 1);
122 // Get frame length (includes header).
123 int size
= ReadBits(&reader
, 13);
130 const uint16 kAc3SyncWord
= 0x0b77;
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)
136 // (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf)
138 // AC3 container looks like syncinfo | bsi | audblk * 6 | aux | check.
139 RCHECK(buffer_size
> 6);
142 while (offset
+ 6 < buffer_size
) {
143 BitReader
reader(buffer
+ offset
, 6);
146 RCHECK(ReadBits(&reader
, 16) == kAc3SyncWord
);
152 int sample_rate_code
= ReadBits(&reader
, 2);
153 RCHECK(sample_rate_code
!= 3); // Reserved.
155 // Verify frmsizecod.
156 int frame_size_code
= ReadBits(&reader
, 6);
157 RCHECK(frame_size_code
< 38); // Undefined.
160 RCHECK(ReadBits(&reader
, 5) < 10); // Normally 8 or 6, 16 used by EAC3.
162 offset
+= kAc3FrameSizeTable
[frame_size_code
][sample_rate_code
];
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)
171 // (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf)
173 // EAC3 container looks like syncinfo | bsi | audfrm | audblk* | aux | check.
174 RCHECK(buffer_size
> 6);
177 while (offset
+ 6 < buffer_size
) {
178 BitReader
reader(buffer
+ offset
, 6);
181 RCHECK(ReadBits(&reader
, 16) == kAc3SyncWord
);
184 RCHECK(ReadBits(&reader
, 2) != 3);
189 // Get frmsize. Include syncinfo size and convert to bytes.
190 int frame_size
= (ReadBits(&reader
, 11) + 1) * 2;
191 RCHECK(frame_size
>= 7);
193 // Skip fscod, fscod2, acmod, and lfeon.
194 reader
.SkipBits(2 + 2 + 3 + 1);
197 int bit_stream_id
= ReadBits(&reader
, 5);
198 RCHECK(bit_stream_id
>= 11 && bit_stream_id
<= 16);
200 offset
+= frame_size
;
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);
210 // Verify number of frames specified.
211 RCHECK(Read32LE(buffer
+ 8) > 0);
213 // Verify width in range.
214 int width
= Read32LE(buffer
+ 20);
215 RCHECK(width
> 0 && width
<= 32767);
217 // Verify height in range.
218 int height
= Read32LE(buffer
+ 24);
219 RCHECK(height
> 0 && height
<= 32767);
221 // Verify frames per second specified.
222 RCHECK(Read32LE(buffer
+ 28) > 0);
224 // Verify video frames per second specified.
225 RCHECK(Read32LE(buffer
+ 32) > 0);
227 // Number of audio tracks must be 256 or less.
228 return (Read32LE(buffer
+ 40) <= 256);
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
);
238 // mFileType should be "caff".
239 RCHECK(ReadBits(&reader
, 32) == TAG('c', 'a', 'f', 'f'));
241 // mFileVersion should be 1.
242 RCHECK(ReadBits(&reader
, 16) == 1);
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);
251 // CAFAudioFormat.mSampleRate(float64) not 0
252 RCHECK(ReadBits(&reader
, 64) != 0);
254 // CAFAudioFormat.mFormatID not 0
255 RCHECK(ReadBits(&reader
, 32) != 0);
257 // Skip CAFAudioFormat.mBytesPerPacket and mFramesPerPacket.
258 reader
.SkipBits(32 + 32);
260 // CAFAudioFormat.mChannelsPerFrame not 0
261 RCHECK(ReadBits(&reader
, 32) != 0);
265 static bool kSamplingFrequencyValid
[16] = { false, true, true, true, false,
266 false, true, true, true, false,
267 false, true, true, true, false,
269 static bool kExtAudioIdValid
[8] = { true, false, true, false, false, false,
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);
279 while (offset
+ 11 < buffer_size
) {
280 BitReader
reader(buffer
+ offset
, 11);
283 RCHECK(ReadBits(&reader
, 32) == 0x7ffe8001);
285 // Skip frame type and deficit sample count.
286 reader
.SkipBits(1 + 5);
288 // Verify CRC present flag.
289 RCHECK(ReadBits(&reader
, 1) == 0); // CPF must be 0.
291 // Verify number of PCM sample blocks.
292 RCHECK(ReadBits(&reader
, 7) >= 5);
294 // Verify primary frame byte size.
295 int frame_size
= ReadBits(&reader
, 14);
296 RCHECK(frame_size
>= 95);
298 // Skip audio channel arrangement.
301 // Verify core audio sampling frequency is an allowed value.
302 RCHECK(kSamplingFrequencyValid
[ReadBits(&reader
, 4)]);
304 // Verify transmission bit rate is valid.
305 RCHECK(ReadBits(&reader
, 5) <= 25);
307 // Verify reserved field is 0.
308 RCHECK(ReadBits(&reader
, 1) == 0);
310 // Skip dynamic range flag, time stamp flag, auxiliary data flag, and HDCD.
311 reader
.SkipBits(1 + 1 + 1 + 1);
313 // Verify extension audio descriptor flag is an allowed value.
314 RCHECK(kExtAudioIdValid
[ReadBits(&reader
, 3)]);
316 // Skip extended coding flag and audio sync word insertion flag.
317 reader
.SkipBits(1 + 1);
319 // Verify low frequency effects flag is an allowed value.
320 RCHECK(ReadBits(&reader
, 2) != 3);
322 offset
+= frame_size
+ 1;
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);
334 int current_sequence_number
= -1;
335 int last_block_number
[6];
336 while (offset
+ 11 < buffer_size
) {
337 BitReader
reader(buffer
+ offset
, 11);
339 // Decode ID data. Sections 5, 6, and 7 are reserved.
340 int section
= ReadBits(&reader
, 3);
343 // Next bit must be 1.
344 RCHECK(ReadBits(&reader
, 1) == 1);
346 // Skip arbitrary bits.
349 int sequence_number
= ReadBits(&reader
, 4);
354 // Next 3 bits must be 1.
355 RCHECK(ReadBits(&reader
, 3) == 7);
357 int block_number
= ReadBits(&reader
, 8);
359 if (section
== 0) { // Header.
360 // Validate the reserved bits in the next 8 bytes.
362 RCHECK(ReadBits(&reader
, 1) == 0);
363 RCHECK(ReadBits(&reader
, 11) == 0x7ff);
365 RCHECK(ReadBits(&reader
, 4) == 0xf);
367 RCHECK(ReadBits(&reader
, 4) == 0xf);
369 RCHECK(ReadBits(&reader
, 4) == 0xf);
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;
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
;
383 // Move to next block.
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.
399 while (offset
< buffer_size
) {
400 // First 4 bits of each block are xD.
401 RCHECK((buffer
[offset
] & 0xf0) == 0xd0);
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),
412 static bool AdvanceToStartCode(const uint8
* buffer
,
418 DCHECK_GE(bytes_needed
, 3);
419 DCHECK_LE(num_bits
, 24); // Only supports up to 24 bits.
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
)
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);
440 bool seen_start_code
= false;
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
;
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);
454 // Skip the temporal reference and PTYPE.
455 reader
.SkipBits(5 + 6);
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);
461 if (!reader
.SkipBits(8))
462 return seen_start_code
;
463 if (!reader
.ReadBits(1, &extra
))
464 return seen_start_code
;
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.
471 if (!reader
.ReadBits(16, &next
))
472 return seen_start_code
;
475 // Move to the next block.
476 seen_start_code
= true;
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);
489 bool seen_start_code
= false;
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
;
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);
503 // Skip the temporal reference.
506 // Verify that the first 2 bits of PTYPE are 10b.
507 RCHECK(ReadBits(&reader
, 2) == 2);
509 // Skip the split screen indicator, document camera indicator, and full
510 // picture freeze release.
511 reader
.SkipBits(1 + 1 + 1);
513 // Verify Source Format.
514 int format
= ReadBits(&reader
, 3);
515 RCHECK(format
!= 0 && format
!= 6); // Forbidden or reserved.
518 // Verify full extended PTYPE.
519 int ufep
= ReadBits(&reader
, 3);
521 // Verify the optional part of PLUSPTYPE.
522 format
= ReadBits(&reader
, 3);
523 RCHECK(format
!= 0 && format
!= 7); // Reserved.
525 // Next 4 bits should be b1000.
526 RCHECK(ReadBits(&reader
, 4) == 8); // Not allowed.
528 RCHECK(ufep
== 0); // Only 0 and 1 allowed.
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.
535 // Skip picture resampling mode, reduced resolution mode,
536 // and rounding type.
537 reader
.SkipBits(1 + 1 + 1);
539 // Next 3 bits should be b001.
540 RCHECK(ReadBits(&reader
, 3) == 1); // Not allowed.
543 // Move to the next block.
544 seen_start_code
= true;
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);
557 int parameter_count
= 0;
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;
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);
571 // Verify forbidden_zero_bit.
572 RCHECK(ReadBits(&reader
, 1) == 0);
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);
578 switch (nal_unit_type
) {
579 case 5: // Coded slice of an IDR picture.
580 RCHECK(nal_ref_idc
!= 0);
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);
589 case 7: // Sequence parameter set.
590 case 8: // Picture parameter set.
595 // Skip the current start_code_prefix and move to the next.
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:";
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)
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
)) {
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);
638 int last_restart
= -1;
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);
646 // Skip sequences of xFF.
652 // Success if the next marker code is EOI (end of image)
656 // Check remaining codes.
657 if (code
== 0xd8 || code
== 1) {
658 // SOI (start of image) / TEM (private use). No other data with header.
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
;
668 // All remaining marker codes are followed by a length of the header.
669 int length
= Read16(buffer
+ offset
+ 2) + 2;
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.
675 int number_components
= buffer
[offset
+ 4];
676 RCHECK(length
== 8 + 2 * number_components
);
678 // Advance to the next marker.
680 while (offset
+ 2 < buffer_size
) {
681 if (buffer
[offset
] == 0xff && buffer
[offset
+ 1] != 0)
686 // Skip over the marker data for the other marker codes.
692 return (num_codes
> 1);
695 enum Mpeg2StartCodes
{
696 PROGRAM_END_CODE
= 0xb9,
697 PACK_START_CODE
= 0xba
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);
706 while (offset
+ 14 < buffer_size
) {
707 BitReader
reader(buffer
+ offset
, 14);
709 // Must start with pack_start_code.
710 RCHECK(ReadBits(&reader
, 24) == 1);
711 RCHECK(ReadBits(&reader
, 8) == PACK_START_CODE
);
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);
720 RCHECK(mpeg_version
== 1);
723 // Skip system_clock_reference_base [32..30].
726 // Verify marker bit.
727 RCHECK(ReadBits(&reader
, 1) == 1);
729 // Skip system_clock_reference_base [29..15].
732 // Verify next marker bit.
733 RCHECK(ReadBits(&reader
, 1) == 1);
735 // Skip system_clock_reference_base [14..0].
738 // Verify next marker bit.
739 RCHECK(ReadBits(&reader
, 1) == 1);
741 if (mpeg_version
== 0) {
742 // Verify second marker bit.
743 RCHECK(ReadBits(&reader
, 1) == 1);
748 // Verify next marker bit.
749 RCHECK(ReadBits(&reader
, 1) == 1);
751 // Update offset to be after this header.
755 // Skip program_mux_rate.
758 // Verify pair of marker bits.
759 RCHECK(ReadBits(&reader
, 2) == 3);
764 // Update offset to be after this header.
765 int pack_stuffing_length
= ReadBits(&reader
, 3);
766 offset
+= 14 + pack_stuffing_length
;
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];
774 // Some stream types are reserved and shouldn't occur.
775 if (mpeg_version
== 0)
776 RCHECK(stream_id
!= 0xbc && stream_id
< 0xf0);
778 RCHECK(stream_id
!= 0xfc && stream_id
!= 0xfd && stream_id
!= 0xfe);
780 // Some stream types are used for pack headers.
781 if (stream_id
== PACK_START_CODE
) // back to outer loop.
783 if (stream_id
== PROGRAM_END_CODE
) // end of stream.
786 int pes_length
= Read16(buffer
+ offset
+ 4);
787 RCHECK(pes_length
> 0);
788 offset
= offset
+ 6 + pes_length
;
791 // Success as we are off the end of the buffer and liked everything
796 const uint8 kMpeg2SyncWord
= 0x47;
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.
808 int packet_length
= -1;
809 while (buffer
[offset
] != kMpeg2SyncWord
&& offset
< 20) {
810 // Skip over any header in the first 20 bytes.
814 while (offset
+ 6 < buffer_size
) {
815 BitReader
reader(buffer
+ offset
, 6);
817 // Must start with sync byte.
818 RCHECK(ReadBits(&reader
, 8) == kMpeg2SyncWord
);
820 // Skip transport_error_indicator, payload_unit_start_indicator, and
821 // transport_priority.
822 reader
.SkipBits(1 + 1 + 1);
824 // Verify the pid is not a reserved value.
825 int pid
= ReadBits(&reader
, 13);
826 RCHECK(pid
< 3 || pid
> 15);
828 // Skip transport_scrambling_control.
831 // Adaptation_field_control can not be 0.
832 int adaptation_field_control
= ReadBits(&reader
, 2);
833 RCHECK(adaptation_field_control
!= 0);
835 // If there is an adaptation_field, verify it.
836 if (adaptation_field_control
>= 2) {
837 // Skip continuity_counter.
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);
845 RCHECK(adaptation_field_length
<= 182);
848 // Attempt to determine the packet length on the first packet.
849 if (packet_length
< 0) {
850 if (buffer
[offset
+ 188] == kMpeg2SyncWord
)
852 else if (buffer
[offset
+ 192] == kMpeg2SyncWord
)
854 else if (buffer
[offset
+ 204] == kMpeg2SyncWord
)
859 offset
+= packet_length
;
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
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);
879 int sequence_start_count
= 0;
880 int sequence_end_count
= 0;
881 int visual_object_count
= 0;
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);
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);
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
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);
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);
922 case VISUAL_OBJECT_SEQUENCE_END_CODE
:
923 RCHECK(++sequence_end_count
== sequence_start_count
);
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);
933 int visual_object_type
= ReadBits(&reader
, 4);
934 RCHECK(visual_object_type
> 0 && visual_object_type
< 6);
939 RCHECK(++vop_count
<= visual_object_count
);
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);
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.
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'):
979 // Indicates that the length is the next 64bits.
980 if (offset
+ 16 > buffer_size
)
982 if (Read32(buffer
+ offset
+ 8) != 0)
983 break; // Offset is way past buffer size.
984 atomsize
= Read32(buffer
+ offset
+ 12);
987 break; // Indicates the last atom or length too big.
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
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 };
1023 static bool ValidMpegAudioFrameHeader(const uint8
* header
,
1026 // Reference: http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm.
1027 DCHECK_GE(header_size
, 4);
1029 BitReader
reader(header
, 4); // Header can only be 4 bytes long.
1031 // Verify frame sync (11 bits) are all set.
1032 RCHECK(ReadBits(&reader
, 11) == 0x7ff);
1034 // Verify MPEG audio version id.
1035 int version
= ReadBits(&reader
, 2);
1036 RCHECK(version
!= 1); // Reserved.
1039 int layer
= ReadBits(&reader
, 2);
1042 // Skip protection bit.
1045 // Verify bitrate index.
1046 int bitrate_index
= ReadBits(&reader
, 4);
1047 RCHECK(bitrate_index
!= 0xf);
1049 // Verify sampling rate frequency index.
1050 int sampling_index
= ReadBits(&reader
, 2);
1051 RCHECK(sampling_index
!= 3);
1054 int padding
= ReadBits(&reader
, 1);
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
];
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
];
1068 bitrate
= kBitRateTableV1L3
[bitrate_index
];
1070 if (layer
== LAYER_1
)
1071 bitrate
= kBitRateTableV2L1
[bitrate_index
];
1073 bitrate
= kBitRateTableV2L23
[bitrate_index
];
1075 if (layer
== LAYER_1
)
1076 *framesize
= ((12000 * bitrate
) / sampling_rate
+ padding
) * 4;
1078 *framesize
= (144000 * bitrate
) / sampling_rate
+ padding
;
1079 return (bitrate
> 0 && sampling_rate
> 0);
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?
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.
1100 offset
= GetMp3HeaderSize(buffer
, buffer_size
);
1102 // Skip over leading 0's.
1103 while (offset
< buffer_size
&& buffer
[offset
] == 0)
1107 while (offset
+ 3 < buffer_size
) {
1108 RCHECK(ValidMpegAudioFrameHeader(
1109 buffer
+ offset
, buffer_size
- offset
, &framesize
));
1111 // Have we seen enough valid headers?
1114 offset
+= framesize
;
1116 // Off the end of the buffer, return success if a few valid headers seen.
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
,
1128 RCHECK(*offset
< buffer_size
);
1130 // Skip over any leading space.
1131 while (isspace(buffer
[*offset
])) {
1133 RCHECK(*offset
< buffer_size
);
1136 // Need to process up to max_digits digits.
1138 while (--max_digits
>= 0 && isdigit(buffer
[*offset
])) {
1141 if (*offset
>= buffer_size
)
1142 return true; // Out of space but seen a digit.
1145 // Success if at least one digit seen.
1146 return (numSeen
> 0);
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
1152 static inline bool VerifyCharacters(const uint8
* buffer
,
1157 RCHECK(*offset
< buffer_size
);
1158 char c
= static_cast<char>(buffer
[(*offset
)++]);
1159 return (c
== c1
|| (c
== c2
&& c2
!= 0));
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);
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'));
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.
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));
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
)
1214 // prefix[] adds back the bits read individually.
1215 return ReadBits(reader
, num_bits_to_read
) | prefix
[i
];
1219 // Invalid encoding, return something not expected.
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
)
1235 return ReadBits(reader
, num_bits_to_read
);
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;
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);
1250 BitReader
reader(buffer
, buffer_size
);
1252 // Verify starting Element Id.
1253 RCHECK(GetElementId(&reader
) == 0x1a45dfa3);
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
);
1259 // Loop through the header.
1260 while (reader
.bits_available() > 0) {
1261 int tag
= GetElementId(&reader
);
1262 int tagsize
= GetVint(&reader
);
1264 case 0x4286: // EBMLVersion
1265 case 0x42f7: // EBMLReadVersion
1266 case 0x42f2: // EBMLMaxIdLength
1267 case 0x42f3: // EBMLMaxSizeLength
1268 case 0x4287: // DocTypeVersion
1269 case 0x4285: // DocTypeReadVersion
1272 RCHECK(reader
.SkipBits(tagsize
* 8));
1275 case 0x4282: // EBMLDocType
1276 // Need to see "webm" or "matroska" next.
1277 switch (ReadBits(&reader
, 32)) {
1278 case TAG('w', 'e', 'b', 'm') :
1280 case TAG('m', 'a', 't', 'r') :
1281 return (ReadBits(&reader
, 32) == TAG('o', 's', 'k', 'a'));
1285 default: // Unrecognized tag
1292 enum VC1StartCodes
{
1293 VC1_FRAME_START_CODE
= 0x0d,
1294 VC1_ENTRY_POINT_START_CODE
= 0x0e,
1295 VC1_SEQUENCE_START_CODE
= 0x0f
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.
1306 RCHECK(buffer_size
>= 24);
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);
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);
1320 // Next bit must be 0.
1321 RCHECK(ReadBits(&reader
, 1) == 0);
1326 // Next bit must be 1.
1327 RCHECK(ReadBits(&reader
, 1) == 1);
1329 // Skip FASTUVMC, EXTENDED_MV, DQUANT, and VSTRANSFORM.
1330 reader
.SkipBits(1 + 1 + 2 + 1);
1332 // Next bit must be 0.
1333 RCHECK(ReadBits(&reader
, 1) == 0);
1335 // Skip OVERLAP, SYNCMARKER, RANGERED, MAXBFRAMES, QUANTIZER, and
1337 reader
.SkipBits(1 + 1 + 1 + 3 + 2 + 1);
1339 // Next bit must be 1.
1340 RCHECK(ReadBits(&reader
, 1) == 1);
1343 RCHECK(profile
== 12); // Other profile values not allowed.
1344 RCHECK(ReadBits(&reader
, 28) == 0);
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);
1353 // Buffer isn't Bitstream Metadata, so scan for start codes.
1355 int sequence_start_code
= 0;
1356 int frame_start_code
= 0;
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);
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);
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)) {
1378 RCHECK(ReadBits(&reader
, 2) == 0);
1383 RCHECK(ReadBits(&reader
, 3) <= 4); // Verify level = 0..4
1384 RCHECK(ReadBits(&reader
, 2) == 1); // Verify colordiff_format = 1
1390 case VC1_ENTRY_POINT_START_CODE
:
1391 // No fields in entry data to check. However, it must occur after
1393 RCHECK(sequence_start_code
> 0);
1396 case VC1_FRAME_START_CODE
:
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
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,
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,
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
,
1423 // Minimum size that the code expects to exist without checking size.
1424 if (buffer_size
< 12)
1425 return CONTAINER_UNKNOWN
;
1427 uint32 first4
= Read32(buffer
);
1430 if (CheckWebm(buffer
, buffer_size
))
1431 return CONTAINER_WEBM
;
1435 if (StartsWith(buffer
,
1438 sizeof(kAsfSignature
))) {
1439 return CONTAINER_ASF
;
1443 case TAG('#','!','A','M'):
1444 if (StartsWith(buffer
, buffer_size
, kAmrSignature
))
1445 return CONTAINER_AMR
;
1448 case TAG('#','E','X','T'):
1449 if (CheckHls(buffer
, buffer_size
))
1450 return CONTAINER_HLS
;
1453 case TAG('.','R','M','F'):
1454 if (buffer
[4] == 0 && buffer
[5] == 0)
1455 return CONTAINER_RM
;
1458 case TAG('.','r','a','\xfd'):
1459 return CONTAINER_RM
;
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
;
1471 case TAG('c','a','f','f'):
1472 if (CheckCaf(buffer
, buffer_size
))
1473 return CONTAINER_CAF
;
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
;
1484 case TAG('D','T','S','H'):
1485 if (Read32(buffer
+ 4) == TAG('D','H','D','R'))
1486 return CONTAINER_DTSHD
;
1496 if (Read32(buffer
+ 4) != 0 && Read32(buffer
+ 8) != 0)
1497 return CONTAINER_IRCAM
;
1500 case TAG('f','L','a','C'):
1501 return CONTAINER_FLAC
;
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
;
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
;
1520 case TAG('M','A','C',' '):
1521 return CONTAINER_APE
;
1523 case TAG('O','N','2',' '):
1524 if (Read32(buffer
+ 8) == TAG('O','N','2','f'))
1525 return CONTAINER_AVI
;
1528 case TAG('O','g','g','S'):
1530 return CONTAINER_OGG
;
1533 case TAG('R','F','6','4'):
1534 if (buffer_size
> 16 && Read32(buffer
+ 12) == TAG('d','s','6','4'))
1535 return CONTAINER_WAV
;
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
;
1550 case TAG('[','S','c','r'):
1551 if (StartsWith(buffer
, buffer_size
, kAssSignature
))
1552 return CONTAINER_ASS
;
1555 case TAG('\xef','\xbb','\xbf','['):
1556 if (StartsWith(buffer
, buffer_size
, kAssBomSignature
))
1557 return CONTAINER_ASS
;
1564 if (CheckDts(buffer
, buffer_size
))
1565 return CONTAINER_DTS
;
1569 if (StartsWith(buffer
,
1572 sizeof(kWtvSignature
))) {
1573 return CONTAINER_WTV
;
1578 // Now try a few different ones that look at something other
1579 // than the first 4 bytes.
1580 uint32 first3
= first4
& 0xffffff00;
1582 case TAG('C','W','S',0):
1583 case TAG('F','W','S',0):
1584 return CONTAINER_SWF
;
1586 case TAG('I','D','3',0):
1587 if (CheckMp3(buffer
, buffer_size
, true))
1588 return CONTAINER_MP3
;
1592 // Maybe the first 2 characters are something we can use.
1593 uint32 first2
= Read16(buffer
);
1596 if (CheckAc3(buffer
, buffer_size
))
1597 return CONTAINER_AC3
;
1598 if (CheckEac3(buffer
, buffer_size
))
1599 return CONTAINER_EAC3
;
1606 if (CheckAac(buffer
, buffer_size
))
1607 return CONTAINER_AAC
;
1611 // Check if the file is in MP3 format without the header.
1612 if (CheckMp3(buffer
, buffer_size
, false))
1613 return CONTAINER_MP3
;
1615 return CONTAINER_UNKNOWN
;
1618 // Attempt to determine the container name from the buffer provided.
1619 MediaContainerName
DetermineContainer(const uint8
* buffer
, int buffer_size
) {
1622 // Since MOV/QuickTime/MPEG4 streams are common, check for them first.
1623 if (CheckMov(buffer
, buffer_size
))
1624 return CONTAINER_MOV
;
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
)
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
;
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
;
1666 return CONTAINER_UNKNOWN
;
1669 } // namespace container_names
1671 } // namespace media