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