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