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