forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_names.cc
1673 lines (1405 loc) · 52.9 KB
/
container_names.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/container_names.h"
#include <cctype>
#include <limits>
#include "base/basictypes.h"
#include "base/logging.h"
#include "media/base/bit_reader.h"
namespace media {
namespace container_names {
#define TAG(a, b, c, d) \
((static_cast<uint32>(static_cast<uint8>(a)) << 24) | \
(static_cast<uint32>(static_cast<uint8>(b)) << 16) | \
(static_cast<uint32>(static_cast<uint8>(c)) << 8) | \
(static_cast<uint32>(static_cast<uint8>(d))))
#define RCHECK(x) \
do { \
if (!(x)) \
return false; \
} while (0)
#define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
// Helper function to read 2 bytes (16 bits, big endian) from a buffer.
static int Read16(const uint8* p) {
return p[0] << 8 | p[1];
}
// Helper function to read 3 bytes (24 bits, big endian) from a buffer.
static uint32 Read24(const uint8* p) {
return p[0] << 16 | p[1] << 8 | p[2];
}
// Helper function to read 4 bytes (32 bits, big endian) from a buffer.
static uint32 Read32(const uint8* p) {
return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
}
// Helper function to read 4 bytes (32 bits, little endian) from a buffer.
static uint32 Read32LE(const uint8* p) {
return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
}
// Helper function to do buffer comparisons with a string without going off the
// end of the buffer.
static bool StartsWith(const uint8* buffer,
size_t buffer_size,
const char* prefix) {
size_t prefix_size = strlen(prefix);
return (prefix_size <= buffer_size &&
memcmp(buffer, prefix, prefix_size) == 0);
}
// Helper function to do buffer comparisons with another buffer (to allow for
// embedded \0 in the comparison) without going off the end of the buffer.
static bool StartsWith(const uint8* buffer,
size_t buffer_size,
const uint8* prefix,
size_t prefix_size) {
return (prefix_size <= buffer_size &&
memcmp(buffer, prefix, prefix_size) == 0);
}
// Helper function to read up to 64 bits from a bit stream.
static uint64 ReadBits(BitReader* reader, int num_bits) {
DCHECK_GE(reader->bits_available(), num_bits);
DCHECK((num_bits > 0) && (num_bits <= 64));
uint64 value;
reader->ReadBits(num_bits, &value);
return value;
}
const int kAc3FrameSizeTable[38][3] = {
{ 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 },
{ 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 },
{ 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 },
{ 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 },
{ 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 },
{ 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 },
{ 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 },
{ 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 },
{ 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 },
{ 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 },
{ 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 }
};
// Checks for an ADTS AAC container.
static bool CheckAac(const uint8* buffer, int buffer_size) {
// Audio Data Transport Stream (ADTS) header is 7 or 9 bytes
// (from http://wiki.multimedia.cx/index.php?title=ADTS)
RCHECK(buffer_size > 6);
int offset = 0;
while (offset + 6 < buffer_size) {
BitReader reader(buffer + offset, 6);
// Syncword must be 0xfff.
RCHECK(ReadBits(&reader, 12) == 0xfff);
// Skip MPEG version.
reader.SkipBits(1);
// Layer is always 0.
RCHECK(ReadBits(&reader, 2) == 0);
// Skip protection + profile.
reader.SkipBits(1 + 2);
// Check sampling frequency index.
RCHECK(ReadBits(&reader, 4) != 15); // Forbidden.
// Skip private stream, channel configuration, originality, home,
// copyrighted stream, and copyright_start.
reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1);
// Get frame length (includes header).
int size = ReadBits(&reader, 13);
RCHECK(size > 0);
offset += size;
}
return true;
}
const uint16 kAc3SyncWord = 0x0b77;
// Checks for an AC3 container.
static bool CheckAc3(const uint8* buffer, int buffer_size) {
// Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3)
// Doc. A/52:2012
// (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf)
// AC3 container looks like syncinfo | bsi | audblk * 6 | aux | check.
RCHECK(buffer_size > 6);
int offset = 0;
while (offset + 6 < buffer_size) {
BitReader reader(buffer + offset, 6);
// Check syncinfo.
RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
// Skip crc1.
reader.SkipBits(16);
// Verify fscod.
int sample_rate_code = ReadBits(&reader, 2);
RCHECK(sample_rate_code != 3); // Reserved.
// Verify frmsizecod.
int frame_size_code = ReadBits(&reader, 6);
RCHECK(frame_size_code < 38); // Undefined.
// Verify bsid.
RCHECK(ReadBits(&reader, 5) < 10); // Normally 8 or 6, 16 used by EAC3.
offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code];
}
return true;
}
// Checks for an EAC3 container (very similar to AC3)
static bool CheckEac3(const uint8* buffer, int buffer_size) {
// Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3)
// Doc. A/52:2012
// (http://www.atsc.org/cms/standards/A52-2012(12-17).pdf)
// EAC3 container looks like syncinfo | bsi | audfrm | audblk* | aux | check.
RCHECK(buffer_size > 6);
int offset = 0;
while (offset + 6 < buffer_size) {
BitReader reader(buffer + offset, 6);
// Check syncinfo.
RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
// Verify strmtyp.
RCHECK(ReadBits(&reader, 2) != 3);
// Skip substreamid.
reader.SkipBits(3);
// Get frmsize. Include syncinfo size and convert to bytes.
int frame_size = (ReadBits(&reader, 11) + 1) * 2;
RCHECK(frame_size >= 7);
// Skip fscod, fscod2, acmod, and lfeon.
reader.SkipBits(2 + 2 + 3 + 1);
// Verify bsid.
int bit_stream_id = ReadBits(&reader, 5);
RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16);
offset += frame_size;
}
return true;
}
// Additional checks for a BINK container.
static bool CheckBink(const uint8* buffer, int buffer_size) {
// Reference: http://wiki.multimedia.cx/index.php?title=Bink_Container
RCHECK(buffer_size >= 44);
// Verify number of frames specified.
RCHECK(Read32LE(buffer + 8) > 0);
// Verify width in range.
int width = Read32LE(buffer + 20);
RCHECK(width > 0 && width <= 32767);
// Verify height in range.
int height = Read32LE(buffer + 24);
RCHECK(height > 0 && height <= 32767);
// Verify frames per second specified.
RCHECK(Read32LE(buffer + 28) > 0);
// Verify video frames per second specified.
RCHECK(Read32LE(buffer + 32) > 0);
// Number of audio tracks must be 256 or less.
return (Read32LE(buffer + 40) <= 256);
}
// Additional checks for a CAF container.
static bool CheckCaf(const uint8* buffer, int buffer_size) {
// Reference: Apple Core Audio Format Specification 1.0
// (https://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/CAFSpec/CAF_spec/CAF_spec.html)
RCHECK(buffer_size >= 52);
BitReader reader(buffer, buffer_size);
// mFileType should be "caff".
RCHECK(ReadBits(&reader, 32) == TAG('c', 'a', 'f', 'f'));
// mFileVersion should be 1.
RCHECK(ReadBits(&reader, 16) == 1);
// Skip mFileFlags.
reader.SkipBits(16);
// First chunk should be Audio Description chunk, size 32l.
RCHECK(ReadBits(&reader, 32) == TAG('d', 'e', 's', 'c'));
RCHECK(ReadBits(&reader, 64) == 32);
// CAFAudioFormat.mSampleRate(float64) not 0
RCHECK(ReadBits(&reader, 64) != 0);
// CAFAudioFormat.mFormatID not 0
RCHECK(ReadBits(&reader, 32) != 0);
// Skip CAFAudioFormat.mBytesPerPacket and mFramesPerPacket.
reader.SkipBits(32 + 32);
// CAFAudioFormat.mChannelsPerFrame not 0
RCHECK(ReadBits(&reader, 32) != 0);
return true;
}
static bool kSamplingFrequencyValid[16] = { false, true, true, true, false,
false, true, true, true, false,
false, true, true, true, false,
false };
static bool kExtAudioIdValid[8] = { true, false, true, false, false, false,
true, false };
// Additional checks for a DTS container.
static bool CheckDts(const uint8* buffer, int buffer_size) {
// Reference: ETSI TS 102 114 V1.3.1 (2011-08)
// (http://www.etsi.org/deliver/etsi_ts/102100_102199/102114/01.03.01_60/ts_102114v010301p.pdf)
RCHECK(buffer_size > 11);
int offset = 0;
while (offset + 11 < buffer_size) {
BitReader reader(buffer + offset, 11);
// Verify sync word.
RCHECK(ReadBits(&reader, 32) == 0x7ffe8001);
// Skip frame type and deficit sample count.
reader.SkipBits(1 + 5);
// Verify CRC present flag.
RCHECK(ReadBits(&reader, 1) == 0); // CPF must be 0.
// Verify number of PCM sample blocks.
RCHECK(ReadBits(&reader, 7) >= 5);
// Verify primary frame byte size.
int frame_size = ReadBits(&reader, 14);
RCHECK(frame_size >= 95);
// Skip audio channel arrangement.
reader.SkipBits(6);
// Verify core audio sampling frequency is an allowed value.
RCHECK(kSamplingFrequencyValid[ReadBits(&reader, 4)]);
// Verify transmission bit rate is valid.
RCHECK(ReadBits(&reader, 5) <= 25);
// Verify reserved field is 0.
RCHECK(ReadBits(&reader, 1) == 0);
// Skip dynamic range flag, time stamp flag, auxiliary data flag, and HDCD.
reader.SkipBits(1 + 1 + 1 + 1);
// Verify extension audio descriptor flag is an allowed value.
RCHECK(kExtAudioIdValid[ReadBits(&reader, 3)]);
// Skip extended coding flag and audio sync word insertion flag.
reader.SkipBits(1 + 1);
// Verify low frequency effects flag is an allowed value.
RCHECK(ReadBits(&reader, 2) != 3);
offset += frame_size + 1;
}
return true;
}
// Checks for a DV container.
static bool CheckDV(const uint8* buffer, int buffer_size) {
// Reference: SMPTE 314M (Annex A has differences with IEC 61834).
// (http://standards.smpte.org/content/978-1-61482-454-1/st-314-2005/SEC1.body.pdf)
RCHECK(buffer_size > 11);
int offset = 0;
int current_sequence_number = -1;
int last_block_number[6];
while (offset + 11 < buffer_size) {
BitReader reader(buffer + offset, 11);
// Decode ID data. Sections 5, 6, and 7 are reserved.
int section = ReadBits(&reader, 3);
RCHECK(section < 5);
// Next bit must be 1.
RCHECK(ReadBits(&reader, 1) == 1);
// Skip arbitrary bits.
reader.SkipBits(4);
int sequence_number = ReadBits(&reader, 4);
// Skip FSC.
reader.SkipBits(1);
// Next 3 bits must be 1.
RCHECK(ReadBits(&reader, 3) == 7);
int block_number = ReadBits(&reader, 8);
if (section == 0) { // Header.
// Validate the reserved bits in the next 8 bytes.
reader.SkipBits(1);
RCHECK(ReadBits(&reader, 1) == 0);
RCHECK(ReadBits(&reader, 11) == 0x7ff);
reader.SkipBits(4);
RCHECK(ReadBits(&reader, 4) == 0xf);
reader.SkipBits(4);
RCHECK(ReadBits(&reader, 4) == 0xf);
reader.SkipBits(4);
RCHECK(ReadBits(&reader, 4) == 0xf);
reader.SkipBits(3);
RCHECK(ReadBits(&reader, 24) == 0xffffff);
current_sequence_number = sequence_number;
for (size_t i = 0; i < arraysize(last_block_number); ++i)
last_block_number[i] = -1;
} else {
// Sequence number must match (this will also fail if no header seen).
RCHECK(sequence_number == current_sequence_number);
// Block number should be increasing.
RCHECK(block_number > last_block_number[section]);
last_block_number[section] = block_number;
}
// Move to next block.
offset += 80;
}
return true;
}
// Checks for a GSM container.
static bool CheckGsm(const uint8* buffer, int buffer_size) {
// Reference: ETSI EN 300 961 V8.1.1
// (http://www.etsi.org/deliver/etsi_en/300900_300999/300961/08.01.01_60/en_300961v080101p.pdf)
// also http://tools.ietf.org/html/rfc3551#page-24
// GSM files have a 33 byte block, only first 4 bits are fixed.
RCHECK(buffer_size >= 1024); // Need enough data to do a decent check.
int offset = 0;
while (offset < buffer_size) {
// First 4 bits of each block are xD.
RCHECK((buffer[offset] & 0xf0) == 0xd0);
offset += 33;
}
return true;
}
// Advance to the first set of |num_bits| bits that match |start_code|. |offset|
// is the current location in the buffer, and is updated. |bytes_needed| is the
// number of bytes that must remain in the buffer when |start_code| is found.
// Returns true if start_code found (and enough space in the buffer after it),
// false otherwise.
static bool AdvanceToStartCode(const uint8* buffer,
int buffer_size,
int* offset,
int bytes_needed,
int num_bits,
uint32 start_code) {
DCHECK_GE(bytes_needed, 3);
DCHECK_LE(num_bits, 24); // Only supports up to 24 bits.
// Create a mask to isolate |num_bits| bits, once shifted over.
uint32 bits_to_shift = 24 - num_bits;
uint32 mask = (1 << num_bits) - 1;
while (*offset + bytes_needed < buffer_size) {
uint32 next = Read24(buffer + *offset);
if (((next >> bits_to_shift) & mask) == start_code)
return true;
++(*offset);
}
return false;
}
// Checks for an H.261 container.
static bool CheckH261(const uint8* buffer, int buffer_size) {
// Reference: ITU-T Recommendation H.261 (03/1993)
// (http://www.itu.int/rec/T-REC-H.261-199303-I/en)
RCHECK(buffer_size > 16);
int offset = 0;
bool seen_start_code = false;
while (true) {
// Advance to picture_start_code, if there is one.
if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 20, 0x10)) {
// No start code found (or off end of buffer), so success if
// there was at least one valid header.
return seen_start_code;
}
// Now verify the block. AdvanceToStartCode() made sure that there are
// at least 4 bytes remaining in the buffer.
BitReader reader(buffer + offset, buffer_size - offset);
RCHECK(ReadBits(&reader, 20) == 0x10);
// Skip the temporal reference and PTYPE.
reader.SkipBits(5 + 6);
// Skip any extra insertion information. Since this is open-ended, if we run
// out of bits assume that the buffer is correctly formatted.
int extra = ReadBits(&reader, 1);
while (extra == 1) {
if (!reader.SkipBits(8))
return seen_start_code;
if (!reader.ReadBits(1, &extra))
return seen_start_code;
}
// Next should be a Group of Blocks start code. Again, if we run out of
// bits, then assume that the buffer up to here is correct, and the buffer
// just happened to end in the middle of a header.
int next;
if (!reader.ReadBits(16, &next))
return seen_start_code;
RCHECK(next == 1);
// Move to the next block.
seen_start_code = true;
offset += 4;
}
}
// Checks for an H.263 container.
static bool CheckH263(const uint8* buffer, int buffer_size) {
// Reference: ITU-T Recommendation H.263 (01/2005)
// (http://www.itu.int/rec/T-REC-H.263-200501-I/en)
// header is PSC(22b) + TR(8b) + PTYPE(8+b).
RCHECK(buffer_size > 16);
int offset = 0;
bool seen_start_code = false;
while (true) {
// Advance to picture_start_code, if there is one.
if (!AdvanceToStartCode(buffer, buffer_size, &offset, 9, 22, 0x20)) {
// No start code found (or off end of buffer), so success if
// there was at least one valid header.
return seen_start_code;
}
// Now verify the block. AdvanceToStartCode() made sure that there are
// at least 9 bytes remaining in the buffer.
BitReader reader(buffer + offset, 9);
RCHECK(ReadBits(&reader, 22) == 0x20);
// Skip the temporal reference.
reader.SkipBits(8);
// Verify that the first 2 bits of PTYPE are 10b.
RCHECK(ReadBits(&reader, 2) == 2);
// Skip the split screen indicator, document camera indicator, and full
// picture freeze release.
reader.SkipBits(1 + 1 + 1);
// Verify Source Format.
int format = ReadBits(&reader, 3);
RCHECK(format != 0 && format != 6); // Forbidden or reserved.
if (format == 7) {
// Verify full extended PTYPE.
int ufep = ReadBits(&reader, 3);
if (ufep == 1) {
// Verify the optional part of PLUSPTYPE.
format = ReadBits(&reader, 3);
RCHECK(format != 0 && format != 7); // Reserved.
reader.SkipBits(11);
// Next 4 bits should be b1000.
RCHECK(ReadBits(&reader, 4) == 8); // Not allowed.
} else {
RCHECK(ufep == 0); // Only 0 and 1 allowed.
}
// Verify picture type code is not a reserved value.
int picture_type_code = ReadBits(&reader, 3);
RCHECK(picture_type_code != 6 && picture_type_code != 7); // Reserved.
// Skip picture resampling mode, reduced resolution mode,
// and rounding type.
reader.SkipBits(1 + 1 + 1);
// Next 3 bits should be b001.
RCHECK(ReadBits(&reader, 3) == 1); // Not allowed.
}
// Move to the next block.
seen_start_code = true;
offset += 9;
}
}
// Checks for an H.264 container.
static bool CheckH264(const uint8* buffer, int buffer_size) {
// Reference: ITU-T Recommendation H.264 (01/2012)
// (http://www.itu.int/rec/T-REC-H.264)
// Section B.1: Byte stream NAL unit syntax and semantics.
RCHECK(buffer_size > 4);
int offset = 0;
int parameter_count = 0;
while (true) {
// Advance to picture_start_code, if there is one.
if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 24, 1)) {
// No start code found (or off end of buffer), so success if
// there was at least one valid header.
return parameter_count > 0;
}
// Now verify the block. AdvanceToStartCode() made sure that there are
// at least 4 bytes remaining in the buffer.
BitReader reader(buffer + offset, 4);
RCHECK(ReadBits(&reader, 24) == 1);
// Verify forbidden_zero_bit.
RCHECK(ReadBits(&reader, 1) == 0);
// Extract nal_ref_idc and nal_unit_type.
int nal_ref_idc = ReadBits(&reader, 2);
int nal_unit_type = ReadBits(&reader, 5);
switch (nal_unit_type) {
case 5: // Coded slice of an IDR picture.
RCHECK(nal_ref_idc != 0);
break;
case 6: // Supplemental enhancement information (SEI).
case 9: // Access unit delimiter.
case 10: // End of sequence.
case 11: // End of stream.
case 12: // Filler data.
RCHECK(nal_ref_idc == 0);
break;
case 7: // Sequence parameter set.
case 8: // Picture parameter set.
++parameter_count;
break;
}
// Skip the current start_code_prefix and move to the next.
offset += 4;
}
}
static const char kHlsSignature[] = "#EXTM3U";
static const char kHls1[] = "#EXT-X-STREAM-INF:";
static const char kHls2[] = "#EXT-X-TARGETDURATION:";
static const char kHls3[] = "#EXT-X-MEDIA-SEQUENCE:";
// Additional checks for a HLS container.
static bool CheckHls(const uint8* buffer, int buffer_size) {
// HLS is simply a play list used for Apple HTTP Live Streaming.
// Reference: Apple HTTP Live Streaming Overview
// (http://goo.gl/MIwxj)
if (StartsWith(buffer, buffer_size, kHlsSignature)) {
// Need to find "#EXT-X-STREAM-INF:", "#EXT-X-TARGETDURATION:", or
// "#EXT-X-MEDIA-SEQUENCE:" somewhere in the buffer. Other playlists (like
// WinAmp) only have additional lines with #EXTINF
// (http://en.wikipedia.org/wiki/M3U).
int offset = strlen(kHlsSignature);
while (offset < buffer_size) {
if (buffer[offset] == '#') {
if (StartsWith(buffer + offset, buffer_size - offset, kHls1) ||
StartsWith(buffer + offset, buffer_size - offset, kHls2) ||
StartsWith(buffer + offset, buffer_size - offset, kHls3)) {
return true;
}
}
++offset;
}
}
return false;
}
// Checks for a MJPEG stream.
static bool CheckMJpeg(const uint8* buffer, int buffer_size) {
// Reference: ISO/IEC 10918-1 : 1993(E), Annex B
// (http://www.w3.org/Graphics/JPEG/itu-t81.pdf)
RCHECK(buffer_size >= 16);
int offset = 0;
int last_restart = -1;
int num_codes = 0;
while (offset + 5 < buffer_size) {
// Marker codes are always a two byte code with the first byte xFF.
RCHECK(buffer[offset] == 0xff);
uint8 code = buffer[offset + 1];
RCHECK(code >= 0xc0 || code == 1);
// Skip sequences of xFF.
if (code == 0xff) {
++offset;
continue;
}
// Success if the next marker code is EOI (end of image)
if (code == 0xd9)
return true;
// Check remaining codes.
if (code == 0xd8 || code == 1) {
// SOI (start of image) / TEM (private use). No other data with header.
offset += 2;
} else if (code >= 0xd0 && code <= 0xd7) {
// RST (restart) codes must be in sequence. No other data with header.
int restart = code & 0x07;
if (last_restart >= 0)
RCHECK(restart == (last_restart + 1) % 8);
last_restart = restart;
offset += 2;
} else {
// All remaining marker codes are followed by a length of the header.
int length = Read16(buffer + offset + 2) + 2;
// Special handling of SOS (start of scan) marker since the entropy
// coded data follows the SOS. Any xFF byte in the data block must be
// followed by x00 in the data.
if (code == 0xda) {
int number_components = buffer[offset + 4];
RCHECK(length == 8 + 2 * number_components);
// Advance to the next marker.
offset += length;
while (offset + 2 < buffer_size) {
if (buffer[offset] == 0xff && buffer[offset + 1] != 0)
break;
++offset;
}
} else {
// Skip over the marker data for the other marker codes.
offset += length;
}
}
++num_codes;
}
return (num_codes > 1);
}
enum Mpeg2StartCodes {
PROGRAM_END_CODE = 0xb9,
PACK_START_CODE = 0xba
};
// Checks for a MPEG2 Program Stream.
static bool CheckMpeg2ProgramStream(const uint8* buffer, int buffer_size) {
// Reference: ISO/IEC 13818-1 : 2000 (E) / ITU-T Rec. H.222.0 (2000 E).
RCHECK(buffer_size > 14);
int offset = 0;
while (offset + 14 < buffer_size) {
BitReader reader(buffer + offset, 14);
// Must start with pack_start_code.
RCHECK(ReadBits(&reader, 24) == 1);
RCHECK(ReadBits(&reader, 8) == PACK_START_CODE);
// Determine MPEG version (MPEG1 has b0010, while MPEG2 has b01).
int mpeg_version = ReadBits(&reader, 2);
if (mpeg_version == 0) {
// MPEG1, 10 byte header
// Validate rest of version code
RCHECK(ReadBits(&reader, 2) == 2);
} else {
RCHECK(mpeg_version == 1);
}
// Skip system_clock_reference_base [32..30].
reader.SkipBits(3);
// Verify marker bit.
RCHECK(ReadBits(&reader, 1) == 1);
// Skip system_clock_reference_base [29..15].
reader.SkipBits(15);
// Verify next marker bit.
RCHECK(ReadBits(&reader, 1) == 1);
// Skip system_clock_reference_base [14..0].
reader.SkipBits(15);
// Verify next marker bit.
RCHECK(ReadBits(&reader, 1) == 1);
if (mpeg_version == 0) {
// Verify second marker bit.
RCHECK(ReadBits(&reader, 1) == 1);
// Skip mux_rate.
reader.SkipBits(22);
// Verify next marker bit.
RCHECK(ReadBits(&reader, 1) == 1);
// Update offset to be after this header.
offset += 12;
} else {
// Must be MPEG2.
// Skip program_mux_rate.
reader.SkipBits(22);
// Verify pair of marker bits.
RCHECK(ReadBits(&reader, 2) == 3);
// Skip reserved.
reader.SkipBits(5);
// Update offset to be after this header.
int pack_stuffing_length = ReadBits(&reader, 3);
offset += 14 + pack_stuffing_length;
}
// Check for system headers and PES_packets.
while (offset + 6 < buffer_size && Read24(buffer + offset) == 1) {
// Next 8 bits determine stream type.
int stream_id = buffer[offset + 3];
// Some stream types are reserved and shouldn't occur.
if (mpeg_version == 0)
RCHECK(stream_id != 0xbc && stream_id < 0xf0);
else
RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe);
// Some stream types are used for pack headers.
if (stream_id == PACK_START_CODE) // back to outer loop.
break;
if (stream_id == PROGRAM_END_CODE) // end of stream.
return true;
int pes_length = Read16(buffer + offset + 4);
RCHECK(pes_length > 0);
offset = offset + 6 + pes_length;
}
}
// Success as we are off the end of the buffer and liked everything
// in the buffer.
return true;
}
const uint8 kMpeg2SyncWord = 0x47;
// Checks for a MPEG2 Transport Stream.
static bool CheckMpeg2TransportStream(const uint8* buffer, int buffer_size) {
// Spec: ISO/IEC 13818-1 : 2000 (E) / ITU-T Rec. H.222.0 (2000 E).
// Normal packet size is 188 bytes. However, some systems add various error
// correction data at the end, resulting in packet of length 192/204/208
// (https://en.wikipedia.org/wiki/MPEG_transport_stream). Determine the
// length with the first packet.
RCHECK(buffer_size >= 250); // Want more than 1 packet to check.
int offset = 0;
int packet_length = -1;
while (buffer[offset] != kMpeg2SyncWord && offset < 20) {
// Skip over any header in the first 20 bytes.
++offset;
}
while (offset + 6 < buffer_size) {
BitReader reader(buffer + offset, 6);
// Must start with sync byte.
RCHECK(ReadBits(&reader, 8) == kMpeg2SyncWord);
// Skip transport_error_indicator, payload_unit_start_indicator, and
// transport_priority.
reader.SkipBits(1 + 1 + 1);
// Verify the pid is not a reserved value.
int pid = ReadBits(&reader, 13);
RCHECK(pid < 3 || pid > 15);
// Skip transport_scrambling_control.
reader.SkipBits(2);
// Adaptation_field_control can not be 0.
int adaptation_field_control = ReadBits(&reader, 2);
RCHECK(adaptation_field_control != 0);
// If there is an adaptation_field, verify it.
if (adaptation_field_control >= 2) {
// Skip continuity_counter.
reader.SkipBits(4);
// Get adaptation_field_length and verify it.
int adaptation_field_length = ReadBits(&reader, 8);
if (adaptation_field_control == 2)
RCHECK(adaptation_field_length == 183);
else
RCHECK(adaptation_field_length <= 182);
}
// Attempt to determine the packet length on the first packet.
if (packet_length < 0) {
if (buffer[offset + 188] == kMpeg2SyncWord)
packet_length = 188;
else if (buffer[offset + 192] == kMpeg2SyncWord)
packet_length = 192;
else if (buffer[offset + 204] == kMpeg2SyncWord)
packet_length = 204;
else
packet_length = 208;
}
offset += packet_length;
}
return true;
}
enum Mpeg4StartCodes {
VISUAL_OBJECT_SEQUENCE_START_CODE = 0xb0,
VISUAL_OBJECT_SEQUENCE_END_CODE = 0xb1,
VISUAL_OBJECT_START_CODE = 0xb5,
VOP_START_CODE = 0xb6
};
// Checks for a raw MPEG4 bitstream container.
static bool CheckMpeg4BitStream(const uint8* buffer, int buffer_size) {
// Defined in ISO/IEC 14496-2:2001.
// However, no length ... simply scan for start code values.
// Note tags are very similar to H.264.
RCHECK(buffer_size > 4);
int offset = 0;
int sequence_start_count = 0;
int sequence_end_count = 0;
int visual_object_count = 0;
int vop_count = 0;
while (true) {
// Advance to start_code, if there is one.
if (!AdvanceToStartCode(buffer, buffer_size, &offset, 6, 24, 1)) {
// Not a complete sequence in memory, so return true if we've seen a
// visual_object_sequence_start_code and a visual_object_start_code.
return (sequence_start_count > 0 && visual_object_count > 0);
}
// Now verify the block. AdvanceToStartCode() made sure that there are
// at least 6 bytes remaining in the buffer.
BitReader reader(buffer + offset, 6);
RCHECK(ReadBits(&reader, 24) == 1);
int start_code = ReadBits(&reader, 8);
RCHECK(start_code < 0x30 || start_code > 0xaf); // 30..AF and
RCHECK(start_code < 0xb7 || start_code > 0xb9); // B7..B9 reserved
switch (start_code) {
case VISUAL_OBJECT_SEQUENCE_START_CODE: {
++sequence_start_count;
// Verify profile in not one of many reserved values.
int profile = ReadBits(&reader, 8);
RCHECK(profile > 0);
RCHECK(profile < 0x04 || profile > 0x10);
RCHECK(profile < 0x13 || profile > 0x20);
RCHECK(profile < 0x23 || profile > 0x31);
RCHECK(profile < 0x35 || profile > 0x41);
RCHECK(profile < 0x43 || profile > 0x60);
RCHECK(profile < 0x65 || profile > 0x70);
RCHECK(profile < 0x73 || profile > 0x80);
RCHECK(profile < 0x83 || profile > 0x90);
RCHECK(profile < 0x95 || profile > 0xa0);
RCHECK(profile < 0xa4 || profile > 0xb0);
RCHECK(profile < 0xb5 || profile > 0xc0);
RCHECK(profile < 0xc3 || profile > 0xd0);
RCHECK(profile < 0xe4);
break;
}
case VISUAL_OBJECT_SEQUENCE_END_CODE:
RCHECK(++sequence_end_count == sequence_start_count);
break;
case VISUAL_OBJECT_START_CODE: {
++visual_object_count;
if (ReadBits(&reader, 1) == 1) {
int visual_object_verid = ReadBits(&reader, 4);
RCHECK(visual_object_verid > 0 && visual_object_verid < 3);
RCHECK(ReadBits(&reader, 3) != 0);
}
int visual_object_type = ReadBits(&reader, 4);
RCHECK(visual_object_type > 0 && visual_object_type < 6);
break;
}
case VOP_START_CODE:
RCHECK(++vop_count <= visual_object_count);
break;
}
// Skip this block.
offset += 6;
}
}
// Additional checks for a MOV/QuickTime/MPEG4 container.
static bool CheckMov(const uint8* buffer, int buffer_size) {
// Reference: ISO/IEC 14496-12:2005(E).
// (http://standards.iso.org/ittf/PubliclyAvailableStandards/c061988_ISO_IEC_14496-12_2012.zip)
RCHECK(buffer_size > 8);
int offset = 0;
while (offset + 8 < buffer_size) {
int atomsize = Read32(buffer + offset);
uint32 atomtype = Read32(buffer + offset + 4);
// Only need to check for ones that are valid at the top level.
switch (atomtype) {
case TAG('f','t','y','p'):
case TAG('p','d','i','n'):
case TAG('m','o','o','v'):
case TAG('m','o','o','f'):
case TAG('m','f','r','a'):
case TAG('m','d','a','t'):
case TAG('f','r','e','e'):
case TAG('s','k','i','p'):
case TAG('m','e','t','a'):
case TAG('m','e','c','o'):
case TAG('s','t','y','p'):
case TAG('s','i','d','x'):
case TAG('s','s','i','x'):
case TAG('p','r','f','t'):
case TAG('b','l','o','c'):
break;
default:
return false;
}
if (atomsize == 1) {
// Indicates that the length is the next 64bits.
if (offset + 16 > buffer_size)
break;
if (Read32(buffer + offset + 8) != 0)
break; // Offset is way past buffer size.
atomsize = Read32(buffer + offset + 12);
}
if (atomsize <= 0)
break; // Indicates the last atom or length too big.
offset += atomsize;
}
return true;
}
enum MPEGVersion {
VERSION_25 = 0,
VERSION_RESERVED,
VERSION_2,
VERSION_1
};