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