Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: media/base/container_names.cc

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

Powered by Google App Engine
This is Rietveld 408576698