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