5 #include "packager/media/base/container_names.h"
12 #include "packager/base/logging.h"
13 #include "packager/media/base/bit_reader.h"
15 namespace edash_packager {
18 #define TAG(a, b, c, d) \
19 ((static_cast<uint32_t>(static_cast<uint8_t>(a)) << 24) | \
20 (static_cast<uint8_t>(b) << 16) | (static_cast<uint8_t>(c) << 8) | \
21 (static_cast<uint8_t>(d)))
29 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
32 static int Read16(
const uint8_t* p) {
33 return p[0] << 8 | p[1];
37 static uint32_t Read24(
const uint8_t* p) {
38 return p[0] << 16 | p[1] << 8 | p[2];
42 static uint32_t Read32(
const uint8_t* p) {
43 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
47 static uint32_t Read32LE(
const uint8_t* p) {
48 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
53 static bool StartsWith(
const uint8_t* buffer,
56 size_t prefix_size = strlen(prefix);
57 return (prefix_size <= buffer_size &&
58 memcmp(buffer, prefix, prefix_size) == 0);
63 static bool StartsWith(
const uint8_t* buffer,
65 const uint8_t* prefix,
67 return (prefix_size <= buffer_size &&
68 memcmp(buffer, prefix, prefix_size) == 0);
72 static uint64_t ReadBits(BitReader* reader,
int num_bits) {
73 DCHECK_GE(reader->bits_available(), num_bits);
74 DCHECK((num_bits > 0) && (num_bits <= 64));
76 reader->ReadBits(num_bits, &value);
80 const int kAc3FrameSizeTable[38][3] = {
81 { 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 },
82 { 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 },
83 { 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 },
84 { 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 },
85 { 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 },
86 { 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 },
87 { 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 },
88 { 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 },
89 { 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 },
90 { 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 },
91 { 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 }
95 static bool CheckAac(
const uint8_t* buffer,
int buffer_size) {
98 RCHECK(buffer_size > 6);
101 while (offset + 6 < buffer_size) {
102 BitReader reader(buffer + offset, 6);
105 RCHECK(ReadBits(&reader, 12) == 0xfff);
111 RCHECK(ReadBits(&reader, 2) == 0);
114 reader.SkipBits(1 + 2);
117 RCHECK(ReadBits(&reader, 4) != 15);
121 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1);
124 int size = ReadBits(&reader, 13);
131 const uint16_t kAc3SyncWord = 0x0b77;
134 static bool CheckAc3(
const uint8_t* buffer,
int buffer_size) {
140 RCHECK(buffer_size > 6);
143 while (offset + 6 < buffer_size) {
144 BitReader reader(buffer + offset, 6);
147 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
153 int sample_rate_code = ReadBits(&reader, 2);
154 RCHECK(sample_rate_code != 3);
157 int frame_size_code = ReadBits(&reader, 6);
158 RCHECK(frame_size_code < 38);
161 RCHECK(ReadBits(&reader, 5) < 10);
163 offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code];
169 static bool CheckEac3(
const uint8_t* buffer,
int buffer_size) {
175 RCHECK(buffer_size > 6);
178 while (offset + 6 < buffer_size) {
179 BitReader reader(buffer + offset, 6);
182 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
185 RCHECK(ReadBits(&reader, 2) != 3);
191 int frame_size = (ReadBits(&reader, 11) + 1) * 2;
192 RCHECK(frame_size >= 7);
195 reader.SkipBits(2 + 2 + 3 + 1);
198 int bit_stream_id = ReadBits(&reader, 5);
199 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16);
201 offset += frame_size;
207 static bool CheckBink(
const uint8_t* buffer,
int buffer_size) {
209 RCHECK(buffer_size >= 44);
212 RCHECK(Read32LE(buffer + 8) > 0);
215 int width = Read32LE(buffer + 20);
216 RCHECK(width > 0 && width <= 32767);
219 int height = Read32LE(buffer + 24);
220 RCHECK(height > 0 && height <= 32767);
223 RCHECK(Read32LE(buffer + 28) > 0);
226 RCHECK(Read32LE(buffer + 32) > 0);
229 return (Read32LE(buffer + 40) <= 256);
233 static bool CheckCaf(
const uint8_t* buffer,
int buffer_size) {
236 RCHECK(buffer_size >= 52);
237 BitReader reader(buffer, buffer_size);
240 RCHECK(ReadBits(&reader, 32) == TAG(
'c',
'a',
'f',
'f'));
243 RCHECK(ReadBits(&reader, 16) == 1);
249 RCHECK(ReadBits(&reader, 32) == TAG(
'd',
'e',
's',
'c'));
250 RCHECK(ReadBits(&reader, 64) == 32);
253 RCHECK(ReadBits(&reader, 64) != 0);
256 RCHECK(ReadBits(&reader, 32) != 0);
259 reader.SkipBits(32 + 32);
262 RCHECK(ReadBits(&reader, 32) != 0);
266 static bool kSamplingFrequencyValid[16] = {
false,
true,
true,
true,
false,
267 false,
true,
true,
true,
false,
268 false,
true,
true,
true,
false,
270 static bool kExtAudioIdValid[8] = {
true,
false,
true,
false,
false,
false,
274 static bool CheckDts(
const uint8_t* buffer,
int buffer_size) {
277 RCHECK(buffer_size > 11);
280 while (offset + 11 < buffer_size) {
281 BitReader reader(buffer + offset, 11);
284 RCHECK(ReadBits(&reader, 32) == 0x7ffe8001);
287 reader.SkipBits(1 + 5);
290 RCHECK(ReadBits(&reader, 1) == 0);
293 RCHECK(ReadBits(&reader, 7) >= 5);
296 int frame_size = ReadBits(&reader, 14);
297 RCHECK(frame_size >= 95);
303 RCHECK(kSamplingFrequencyValid[ReadBits(&reader, 4)]);
306 RCHECK(ReadBits(&reader, 5) <= 25);
309 RCHECK(ReadBits(&reader, 1) == 0);
312 reader.SkipBits(1 + 1 + 1 + 1);
315 RCHECK(kExtAudioIdValid[ReadBits(&reader, 3)]);
318 reader.SkipBits(1 + 1);
321 RCHECK(ReadBits(&reader, 2) != 3);
323 offset += frame_size + 1;
329 static bool CheckDV(
const uint8_t* buffer,
int buffer_size) {
332 RCHECK(buffer_size > 11);
335 int current_sequence_number = -1;
336 int last_block_number[6];
337 while (offset + 11 < buffer_size) {
338 BitReader reader(buffer + offset, 11);
341 int section = ReadBits(&reader, 3);
345 RCHECK(ReadBits(&reader, 1) == 1);
350 int sequence_number = ReadBits(&reader, 4);
356 RCHECK(ReadBits(&reader, 3) == 7);
358 int block_number = ReadBits(&reader, 8);
363 RCHECK(ReadBits(&reader, 1) == 0);
364 RCHECK(ReadBits(&reader, 11) == 0x7ff);
366 RCHECK(ReadBits(&reader, 4) == 0xf);
368 RCHECK(ReadBits(&reader, 4) == 0xf);
370 RCHECK(ReadBits(&reader, 4) == 0xf);
372 RCHECK(ReadBits(&reader, 24) == 0xffffff);
373 current_sequence_number = sequence_number;
374 for (
size_t i = 0; i < arraysize(last_block_number); ++i)
375 last_block_number[i] = -1;
378 RCHECK(sequence_number == current_sequence_number);
380 RCHECK(block_number > last_block_number[section]);
381 last_block_number[section] = block_number;
392 static bool CheckGsm(
const uint8_t* buffer,
int buffer_size) {
397 RCHECK(buffer_size >= 1024);
400 while (offset < buffer_size) {
402 RCHECK((buffer[offset] & 0xf0) == 0xd0);
413 static bool AdvanceToStartCode(
const uint8_t* buffer,
418 uint32_t start_code) {
419 DCHECK_GE(bytes_needed, 3);
420 DCHECK_LE(num_bits, 24);
423 uint32_t bits_to_shift = 24 - num_bits;
424 uint32_t mask = (1 << num_bits) - 1;
425 while (*offset + bytes_needed < buffer_size) {
426 uint32_t next = Read24(buffer + *offset);
427 if (((next >> bits_to_shift) & mask) == start_code)
435 static bool CheckH261(
const uint8_t* buffer,
int buffer_size) {
438 RCHECK(buffer_size > 16);
441 bool seen_start_code =
false;
444 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 20, 0x10)) {
447 return seen_start_code;
452 BitReader reader(buffer + offset, buffer_size - offset);
453 RCHECK(ReadBits(&reader, 20) == 0x10);
456 reader.SkipBits(5 + 6);
460 int extra = ReadBits(&reader, 1);
462 if (!reader.SkipBits(8))
463 return seen_start_code;
464 if (!reader.ReadBits(1, &extra))
465 return seen_start_code;
472 if (!reader.ReadBits(16, &next))
473 return seen_start_code;
477 seen_start_code =
true;
483 static bool CheckH263(
const uint8_t* buffer,
int buffer_size) {
487 RCHECK(buffer_size > 16);
490 bool seen_start_code =
false;
493 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 9, 22, 0x20)) {
496 return seen_start_code;
501 BitReader reader(buffer + offset, 9);
502 RCHECK(ReadBits(&reader, 22) == 0x20);
508 RCHECK(ReadBits(&reader, 2) == 2);
512 reader.SkipBits(1 + 1 + 1);
515 int format = ReadBits(&reader, 3);
516 RCHECK(format != 0 && format != 6);
520 int ufep = ReadBits(&reader, 3);
523 format = ReadBits(&reader, 3);
524 RCHECK(format != 0 && format != 7);
527 RCHECK(ReadBits(&reader, 4) == 8);
533 int picture_type_code = ReadBits(&reader, 3);
534 RCHECK(picture_type_code != 6 && picture_type_code != 7);
538 reader.SkipBits(1 + 1 + 1);
541 RCHECK(ReadBits(&reader, 3) == 1);
545 seen_start_code =
true;
551 static bool CheckH264(
const uint8_t* buffer,
int buffer_size) {
555 RCHECK(buffer_size > 4);
558 int parameter_count = 0;
561 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 24, 1)) {
564 return parameter_count > 0;
569 BitReader reader(buffer + offset, 4);
570 RCHECK(ReadBits(&reader, 24) == 1);
573 RCHECK(ReadBits(&reader, 1) == 0);
576 int nal_ref_idc = ReadBits(&reader, 2);
577 int nal_unit_type = ReadBits(&reader, 5);
579 switch (nal_unit_type) {
581 RCHECK(nal_ref_idc != 0);
588 RCHECK(nal_ref_idc == 0);
601 static const char kHlsSignature[] =
"#EXTM3U";
602 static const char kHls1[] =
"#EXT-X-STREAM-INF:";
603 static const char kHls2[] =
"#EXT-X-TARGETDURATION:";
604 static const char kHls3[] =
"#EXT-X-MEDIA-SEQUENCE:";
607 static bool CheckHls(
const uint8_t* buffer,
int buffer_size) {
612 if (StartsWith(buffer, buffer_size, kHlsSignature)) {
617 int offset = strlen(kHlsSignature);
618 while (offset < buffer_size) {
619 if (buffer[offset] ==
'#') {
620 if (StartsWith(buffer + offset, buffer_size - offset, kHls1) ||
621 StartsWith(buffer + offset, buffer_size - offset, kHls2) ||
622 StartsWith(buffer + offset, buffer_size - offset, kHls3)) {
633 static bool CheckMJpeg(
const uint8_t* buffer,
int buffer_size) {
636 RCHECK(buffer_size >= 16);
639 int last_restart = -1;
641 while (offset + 5 < buffer_size) {
643 RCHECK(buffer[offset] == 0xff);
644 uint8_t code = buffer[offset + 1];
645 RCHECK(code >= 0xc0 || code == 1);
658 if (code == 0xd8 || code == 1) {
661 }
else if (code >= 0xd0 && code <= 0xd7) {
663 int restart = code & 0x07;
664 if (last_restart >= 0)
665 RCHECK(restart == (last_restart + 1) % 8);
666 last_restart = restart;
670 int length = Read16(buffer + offset + 2) + 2;
676 int number_components = buffer[offset + 4];
677 RCHECK(length == 8 + 2 * number_components);
681 while (offset + 2 < buffer_size) {
682 if (buffer[offset] == 0xff && buffer[offset + 1] != 0)
693 return (num_codes > 1);
696 enum Mpeg2StartCodes {
697 PROGRAM_END_CODE = 0xb9,
698 PACK_START_CODE = 0xba
702 static bool CheckMpeg2ProgramStream(
const uint8_t* buffer,
int buffer_size) {
704 RCHECK(buffer_size > 14);
707 while (offset + 14 < buffer_size) {
708 BitReader reader(buffer + offset, 14);
711 RCHECK(ReadBits(&reader, 24) == 1);
712 RCHECK(ReadBits(&reader, 8) == PACK_START_CODE);
715 int mpeg_version = ReadBits(&reader, 2);
716 if (mpeg_version == 0) {
719 RCHECK(ReadBits(&reader, 2) == 2);
721 RCHECK(mpeg_version == 1);
728 RCHECK(ReadBits(&reader, 1) == 1);
734 RCHECK(ReadBits(&reader, 1) == 1);
740 RCHECK(ReadBits(&reader, 1) == 1);
742 if (mpeg_version == 0) {
744 RCHECK(ReadBits(&reader, 1) == 1);
750 RCHECK(ReadBits(&reader, 1) == 1);
760 RCHECK(ReadBits(&reader, 2) == 3);
766 int pack_stuffing_length = ReadBits(&reader, 3);
767 offset += 14 + pack_stuffing_length;
771 while (offset + 6 < buffer_size && Read24(buffer + offset) == 1) {
773 int stream_id = buffer[offset + 3];
776 if (mpeg_version == 0)
777 RCHECK(stream_id != 0xbc && stream_id < 0xf0);
779 RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe);
782 if (stream_id == PACK_START_CODE)
784 if (stream_id == PROGRAM_END_CODE)
787 int pes_length = Read16(buffer + offset + 4);
788 RCHECK(pes_length > 0);
789 offset = offset + 6 + pes_length;
797 const uint8_t kMpeg2SyncWord = 0x47;
800 static bool CheckMpeg2TransportStream(
const uint8_t* buffer,
int buffer_size) {
806 RCHECK(buffer_size >= 250);
809 int packet_length = -1;
810 while (buffer[offset] != kMpeg2SyncWord && offset < 20) {
815 while (offset + 6 < buffer_size) {
816 BitReader reader(buffer + offset, 6);
819 RCHECK(ReadBits(&reader, 8) == kMpeg2SyncWord);
823 reader.SkipBits(1 + 1 + 1);
826 int pid = ReadBits(&reader, 13);
827 RCHECK(pid < 3 || pid > 15);
833 int adaptation_field_control = ReadBits(&reader, 2);
834 RCHECK(adaptation_field_control != 0);
837 if (adaptation_field_control >= 2) {
842 int adaptation_field_length = ReadBits(&reader, 8);
843 if (adaptation_field_control == 2)
844 RCHECK(adaptation_field_length == 183);
846 RCHECK(adaptation_field_length <= 182);
850 if (packet_length < 0) {
851 if (buffer[offset + 188] == kMpeg2SyncWord)
853 else if (buffer[offset + 192] == kMpeg2SyncWord)
855 else if (buffer[offset + 204] == kMpeg2SyncWord)
860 offset += packet_length;
865 enum Mpeg4StartCodes {
866 VISUAL_OBJECT_SEQUENCE_START_CODE = 0xb0,
867 VISUAL_OBJECT_SEQUENCE_END_CODE = 0xb1,
868 VISUAL_OBJECT_START_CODE = 0xb5,
869 VOP_START_CODE = 0xb6
873 static bool CheckMpeg4BitStream(
const uint8_t* buffer,
int buffer_size) {
877 RCHECK(buffer_size > 4);
880 int sequence_start_count = 0;
881 int sequence_end_count = 0;
882 int visual_object_count = 0;
886 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 6, 24, 1)) {
889 return (sequence_start_count > 0 && visual_object_count > 0);
894 BitReader reader(buffer + offset, 6);
895 RCHECK(ReadBits(&reader, 24) == 1);
897 int start_code = ReadBits(&reader, 8);
898 RCHECK(start_code < 0x30 || start_code > 0xaf);
899 RCHECK(start_code < 0xb7 || start_code > 0xb9);
901 switch (start_code) {
902 case VISUAL_OBJECT_SEQUENCE_START_CODE: {
903 ++sequence_start_count;
905 int profile = ReadBits(&reader, 8);
907 RCHECK(profile < 0x04 || profile > 0x10);
908 RCHECK(profile < 0x13 || profile > 0x20);
909 RCHECK(profile < 0x23 || profile > 0x31);
910 RCHECK(profile < 0x35 || profile > 0x41);
911 RCHECK(profile < 0x43 || profile > 0x60);
912 RCHECK(profile < 0x65 || profile > 0x70);
913 RCHECK(profile < 0x73 || profile > 0x80);
914 RCHECK(profile < 0x83 || profile > 0x90);
915 RCHECK(profile < 0x95 || profile > 0xa0);
916 RCHECK(profile < 0xa4 || profile > 0xb0);
917 RCHECK(profile < 0xb5 || profile > 0xc0);
918 RCHECK(profile < 0xc3 || profile > 0xd0);
919 RCHECK(profile < 0xe4);
923 case VISUAL_OBJECT_SEQUENCE_END_CODE:
924 RCHECK(++sequence_end_count == sequence_start_count);
927 case VISUAL_OBJECT_START_CODE: {
928 ++visual_object_count;
929 if (ReadBits(&reader, 1) == 1) {
930 int visual_object_verid = ReadBits(&reader, 4);
931 RCHECK(visual_object_verid > 0 && visual_object_verid < 3);
932 RCHECK(ReadBits(&reader, 3) != 0);
934 int visual_object_type = ReadBits(&reader, 4);
935 RCHECK(visual_object_type > 0 && visual_object_type < 6);
940 RCHECK(++vop_count <= visual_object_count);
949 static bool CheckMov(
const uint8_t* buffer,
int buffer_size) {
952 RCHECK(buffer_size > 8);
955 while (offset + 8 < buffer_size) {
956 int atomsize = Read32(buffer + offset);
957 uint32_t atomtype = Read32(buffer + offset + 4);
960 case TAG(
'f',
't',
'y',
'p'):
961 case TAG(
'p',
'd',
'i',
'n'):
962 case TAG(
'm',
'o',
'o',
'v'):
963 case TAG(
'm',
'o',
'o',
'f'):
964 case TAG(
'm',
'f',
'r',
'a'):
965 case TAG(
'm',
'd',
'a',
't'):
966 case TAG(
'f',
'r',
'e',
'e'):
967 case TAG(
's',
'k',
'i',
'p'):
968 case TAG(
'm',
'e',
't',
'a'):
969 case TAG(
'm',
'e',
'c',
'o'):
970 case TAG(
's',
't',
'y',
'p'):
971 case TAG(
's',
'i',
'd',
'x'):
972 case TAG(
's',
's',
'i',
'x'):
973 case TAG(
'p',
'r',
'f',
't'):
974 case TAG(
'b',
'l',
'o',
'c'):
981 if (offset + 16 > buffer_size)
983 if (Read32(buffer + offset + 8) != 0)
985 atomsize = Read32(buffer + offset + 12);
1007 static int kSampleRateTable[4][4] = { { 11025, 12000, 8000, 0 },
1009 { 22050, 24000, 16000, 0 },
1010 { 44100, 48000, 32000, 0 }
1013 static int kBitRateTableV1L1[16] = { 0, 32, 64, 96, 128, 160, 192, 224, 256,
1014 288, 320, 352, 384, 416, 448, 0 };
1015 static int kBitRateTableV1L2[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160,
1016 192, 224, 256, 320, 384, 0 };
1017 static int kBitRateTableV1L3[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128,
1018 160, 192, 224, 256, 320, 0 };
1019 static int kBitRateTableV2L1[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1020 160, 176, 192, 224, 256, 0 };
1021 static int kBitRateTableV2L23[16] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,
1022 112, 128, 144, 160, 0 };
1024 static bool ValidMpegAudioFrameHeader(
const uint8_t* header,
1028 DCHECK_GE(header_size, 4);
1030 BitReader reader(header, 4);
1033 RCHECK(ReadBits(&reader, 11) == 0x7ff);
1036 int version = ReadBits(&reader, 2);
1037 RCHECK(version != 1);
1040 int layer = ReadBits(&reader, 2);
1047 int bitrate_index = ReadBits(&reader, 4);
1048 RCHECK(bitrate_index != 0xf);
1051 int sampling_index = ReadBits(&reader, 2);
1052 RCHECK(sampling_index != 3);
1055 int padding = ReadBits(&reader, 1);
1061 int sampling_rate = kSampleRateTable[version][sampling_index];
1063 if (version == VERSION_1) {
1064 if (layer == LAYER_1)
1065 bitrate = kBitRateTableV1L1[bitrate_index];
1066 else if (layer == LAYER_2)
1067 bitrate = kBitRateTableV1L2[bitrate_index];
1069 bitrate = kBitRateTableV1L3[bitrate_index];
1071 if (layer == LAYER_1)
1072 bitrate = kBitRateTableV2L1[bitrate_index];
1074 bitrate = kBitRateTableV2L23[bitrate_index];
1076 if (layer == LAYER_1)
1077 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4;
1079 *framesize = (144000 * bitrate) / sampling_rate + padding;
1080 return (bitrate > 0 && sampling_rate > 0);
1084 static int GetMp3HeaderSize(
const uint8_t* buffer,
int buffer_size) {
1085 DCHECK_GE(buffer_size, 9);
1086 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) +
1087 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10;
1088 if (buffer[5] & 0x10)
1094 static bool CheckMp3(
const uint8_t* buffer,
int buffer_size,
bool seenHeader) {
1095 RCHECK(buffer_size >= 10);
1101 offset = GetMp3HeaderSize(buffer, buffer_size);
1104 while (offset < buffer_size && buffer[offset] == 0)
1108 while (offset + 3 < buffer_size) {
1109 RCHECK(ValidMpegAudioFrameHeader(
1110 buffer + offset, buffer_size - offset, &framesize));
1115 offset += framesize;
1125 static bool VerifyNumber(
const uint8_t* buffer,
1129 RCHECK(*offset < buffer_size);
1132 while (isspace(buffer[*offset])) {
1134 RCHECK(*offset < buffer_size);
1139 while (--max_digits >= 0 && isdigit(buffer[*offset])) {
1142 if (*offset >= buffer_size)
1147 return (numSeen > 0);
1153 static inline bool VerifyCharacters(
const uint8_t* buffer,
1158 RCHECK(*offset < buffer_size);
1159 char c =
static_cast<char>(buffer[(*offset)++]);
1160 return (c == c1 || (c == c2 && c2 != 0));
1164 static bool CheckSrt(
const uint8_t* buffer,
int buffer_size) {
1166 RCHECK(buffer_size > 20);
1169 int offset = StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1170 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1171 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r'));
1174 while (VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r')) {}
1180 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1181 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1182 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1183 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1184 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1185 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
',',
'.'));
1186 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3));
1187 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
' ', 0));
1188 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'-', 0));
1189 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'-', 0));
1190 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'>', 0));
1191 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
' ', 0));
1192 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1193 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1194 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1195 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1196 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1197 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
',',
'.'));
1198 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3));
1203 static int GetElementId(BitReader* reader) {
1207 if (reader->bits_available() >= 8) {
1208 int num_bits_to_read = 0;
1209 static int prefix[] = { 0x80, 0x4000, 0x200000, 0x10000000 };
1210 for (
int i = 0; i < 4; ++i) {
1211 num_bits_to_read += 7;
1212 if (ReadBits(reader, 1) == 1) {
1213 if (reader->bits_available() < num_bits_to_read)
1216 return ReadBits(reader, num_bits_to_read) | prefix[i];
1225 static uint64_t GetVint(BitReader* reader) {
1229 if (reader->bits_available() >= 8) {
1230 int num_bits_to_read = 0;
1231 for (
int i = 0; i < 8; ++i) {
1232 num_bits_to_read += 7;
1233 if (ReadBits(reader, 1) == 1) {
1234 if (reader->bits_available() < num_bits_to_read)
1236 return ReadBits(reader, num_bits_to_read);
1243 return (reader->bits_available() / 8) + 2;
1247 static bool CheckWebm(
const uint8_t* buffer,
int buffer_size) {
1249 RCHECK(buffer_size > 12);
1251 BitReader reader(buffer, buffer_size);
1254 RCHECK(GetElementId(&reader) == 0x1a45dfa3);
1257 int header_size = GetVint(&reader);
1258 RCHECK(reader.bits_available() / 8 >= header_size);
1261 while (reader.bits_available() > 0) {
1262 int tag = GetElementId(&reader);
1263 int tagsize = GetVint(&reader);
1273 RCHECK(reader.SkipBits(tagsize * 8));
1278 switch (ReadBits(&reader, 32)) {
1279 case TAG(
'w',
'e',
'b',
'm') :
1281 case TAG(
'm',
'a',
't',
'r') :
1282 return (ReadBits(&reader, 32) == TAG(
'o',
's',
'k',
'a'));
1293 enum VC1StartCodes {
1294 VC1_FRAME_START_CODE = 0x0d,
1295 VC1_ENTRY_POINT_START_CODE = 0x0e,
1296 VC1_SEQUENCE_START_CODE = 0x0f
1300 static bool CheckVC1(
const uint8_t* buffer,
int buffer_size) {
1307 RCHECK(buffer_size >= 24);
1310 if (buffer[0] == 0xc5 &&
1311 Read32(buffer + 4) == 0x04 &&
1312 Read32(buffer + 20) == 0x0c) {
1314 BitReader reader(buffer + 8, 12);
1316 int profile = ReadBits(&reader, 4);
1317 if (profile == 0 || profile == 4) {
1319 reader.SkipBits(3 + 5 + 1);
1322 RCHECK(ReadBits(&reader, 1) == 0);
1328 RCHECK(ReadBits(&reader, 1) == 1);
1331 reader.SkipBits(1 + 1 + 2 + 1);
1334 RCHECK(ReadBits(&reader, 1) == 0);
1338 reader.SkipBits(1 + 1 + 1 + 3 + 2 + 1);
1341 RCHECK(ReadBits(&reader, 1) == 1);
1344 RCHECK(profile == 12);
1345 RCHECK(ReadBits(&reader, 28) == 0);
1349 RCHECK(ReadBits(&reader, 32) <= 8192);
1350 RCHECK(ReadBits(&reader, 32) <= 8192);
1356 int sequence_start_code = 0;
1357 int frame_start_code = 0;
1360 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 5, 24, 1)) {
1364 return (sequence_start_code > 0 && frame_start_code > 0);
1369 BitReader reader(buffer + offset, 5);
1370 RCHECK(ReadBits(&reader, 24) == 1);
1373 switch (ReadBits(&reader, 8)) {
1374 case VC1_SEQUENCE_START_CODE: {
1375 ++sequence_start_code;
1376 switch (ReadBits(&reader, 2)) {
1379 RCHECK(ReadBits(&reader, 2) == 0);
1384 RCHECK(ReadBits(&reader, 3) <= 4);
1385 RCHECK(ReadBits(&reader, 2) == 1);
1391 case VC1_ENTRY_POINT_START_CODE:
1394 RCHECK(sequence_start_code > 0);
1397 case VC1_FRAME_START_CODE:
1409 static const char kAmrSignature[] =
"#!AMR";
1410 static const uint8_t kAsfSignature[] = {0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
1411 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa,
1412 0x00, 0x62, 0xce, 0x6c};
1413 static const char kAssSignature[] =
"[Script Info]";
1414 static const char kAssBomSignature[] = UTF8_BYTE_ORDER_MARK
"[Script Info]";
1415 static const uint8_t kWtvSignature[] = {0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49,
1416 0xda, 0x11, 0xa6, 0x4e, 0x00, 0x07,
1417 0xe9, 0x5e, 0xad, 0x8d};
1422 static MediaContainerName LookupContainerByFirst4(
const uint8_t* buffer,
1425 if (buffer_size < 12)
1426 return CONTAINER_UNKNOWN;
1428 uint32_t first4 = Read32(buffer);
1431 if (CheckWebm(buffer, buffer_size))
1432 return CONTAINER_WEBM;
1436 if (StartsWith(buffer,
1439 sizeof(kAsfSignature))) {
1440 return CONTAINER_ASF;
1444 case TAG(
'#',
'!',
'A',
'M'):
1445 if (StartsWith(buffer, buffer_size, kAmrSignature))
1446 return CONTAINER_AMR;
1449 case TAG(
'#',
'E',
'X',
'T'):
1450 if (CheckHls(buffer, buffer_size))
1451 return CONTAINER_HLS;
1454 case TAG(
'.',
'R',
'M',
'F'):
1455 if (buffer[4] == 0 && buffer[5] == 0)
1456 return CONTAINER_RM;
1459 case TAG(
'.',
'r',
'a',
'\xfd'):
1460 return CONTAINER_RM;
1462 case TAG(
'B',
'I',
'K',
'b'):
1463 case TAG(
'B',
'I',
'K',
'd'):
1464 case TAG(
'B',
'I',
'K',
'f'):
1465 case TAG(
'B',
'I',
'K',
'g'):
1466 case TAG(
'B',
'I',
'K',
'h'):
1467 case TAG(
'B',
'I',
'K',
'i'):
1468 if (CheckBink(buffer, buffer_size))
1469 return CONTAINER_BINK;
1472 case TAG(
'c',
'a',
'f',
'f'):
1473 if (CheckCaf(buffer, buffer_size))
1474 return CONTAINER_CAF;
1477 case TAG(
'D',
'E',
'X',
'A'):
1478 if (buffer_size > 15 &&
1479 Read16(buffer + 11) <= 2048 &&
1480 Read16(buffer + 13) <= 2048) {
1481 return CONTAINER_DXA;
1485 case TAG(
'D',
'T',
'S',
'H'):
1486 if (Read32(buffer + 4) == TAG(
'D',
'H',
'D',
'R'))
1487 return CONTAINER_DTSHD;
1497 if (Read32(buffer + 4) != 0 && Read32(buffer + 8) != 0)
1498 return CONTAINER_IRCAM;
1501 case TAG(
'f',
'L',
'a',
'C'):
1502 return CONTAINER_FLAC;
1504 case TAG(
'F',
'L',
'V',0):
1505 case TAG(
'F',
'L',
'V',1):
1506 case TAG(
'F',
'L',
'V',2):
1507 case TAG(
'F',
'L',
'V',3):
1508 case TAG(
'F',
'L',
'V',4):
1509 if (buffer[5] == 0 && Read32(buffer + 5) > 8)
1510 return CONTAINER_FLV;
1513 case TAG(
'F',
'O',
'R',
'M'):
1514 switch (Read32(buffer + 8)) {
1515 case TAG(
'A',
'I',
'F',
'F'):
1516 case TAG(
'A',
'I',
'F',
'C'):
1517 return CONTAINER_AIFF;
1521 case TAG(
'M',
'A',
'C',
' '):
1522 return CONTAINER_APE;
1524 case TAG(
'O',
'N',
'2',
' '):
1525 if (Read32(buffer + 8) == TAG(
'O',
'N',
'2',
'f'))
1526 return CONTAINER_AVI;
1529 case TAG(
'O',
'g',
'g',
'S'):
1531 return CONTAINER_OGG;
1534 case TAG(
'R',
'F',
'6',
'4'):
1535 if (buffer_size > 16 && Read32(buffer + 12) == TAG(
'd',
's',
'6',
'4'))
1536 return CONTAINER_WAV;
1539 case TAG(
'R',
'I',
'F',
'F'):
1540 switch (Read32(buffer + 8)) {
1541 case TAG(
'A',
'V',
'I',
' '):
1542 case TAG(
'A',
'V',
'I',
'X'):
1543 case TAG(
'A',
'V',
'I',
'\x19'):
1544 case TAG(
'A',
'M',
'V',
' '):
1545 return CONTAINER_AVI;
1546 case TAG(
'W',
'A',
'V',
'E'):
1547 return CONTAINER_WAV;
1551 case TAG(
'[',
'S',
'c',
'r'):
1552 if (StartsWith(buffer, buffer_size, kAssSignature))
1553 return CONTAINER_ASS;
1556 case TAG(
'\xef',
'\xbb',
'\xbf',
'['):
1557 if (StartsWith(buffer, buffer_size, kAssBomSignature))
1558 return CONTAINER_ASS;
1565 if (CheckDts(buffer, buffer_size))
1566 return CONTAINER_DTS;
1570 if (StartsWith(buffer,
1573 sizeof(kWtvSignature))) {
1574 return CONTAINER_WTV;
1578 return CONTAINER_MPEG2PS;
1583 uint32_t first3 = first4 & 0xffffff00;
1585 case TAG(
'C',
'W',
'S',0):
1586 case TAG(
'F',
'W',
'S',0):
1587 return CONTAINER_SWF;
1589 case TAG(
'I',
'D',
'3',0):
1590 if (CheckMp3(buffer, buffer_size, true))
1591 return CONTAINER_MP3;
1596 uint32_t first2 = Read16(buffer);
1599 if (CheckAc3(buffer, buffer_size))
1600 return CONTAINER_AC3;
1601 if (CheckEac3(buffer, buffer_size))
1602 return CONTAINER_EAC3;
1609 if (CheckAac(buffer, buffer_size))
1610 return CONTAINER_AAC;
1615 if (CheckMp3(buffer, buffer_size,
false))
1616 return CONTAINER_MP3;
1618 return CONTAINER_UNKNOWN;
1622 const char kWebVtt[] =
"WEBVTT";
1624 bool CheckWebVtt(
const uint8_t* buffer,
int buffer_size) {
1626 StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1628 return StartsWith(buffer + offset, buffer_size - offset,
1629 reinterpret_cast<const uint8_t*>(kWebVtt),
1630 arraysize(kWebVtt) - 1);
1636 bool CheckTtml(
const uint8_t* buffer,
int buffer_size) {
1637 return StartsWith(buffer, buffer_size,
1638 "<?xml version='1.0' encoding='UTF-8'?>") ||
1639 StartsWith(buffer, buffer_size,
1640 "<?xml version='1.0' encoding='UTF-16'?>");
1645 MediaContainerName DetermineContainer(
const uint8_t* buffer,
int buffer_size) {
1649 if (CheckMov(buffer, buffer_size))
1650 return CONTAINER_MOV;
1654 MediaContainerName result = LookupContainerByFirst4(buffer, buffer_size);
1655 if (result != CONTAINER_UNKNOWN)
1659 if (CheckWebVtt(buffer, buffer_size))
1660 return CONTAINER_WEBVTT;
1663 if (CheckMpeg2ProgramStream(buffer, buffer_size))
1664 return CONTAINER_MPEG2PS;
1665 if (CheckMpeg2TransportStream(buffer, buffer_size))
1666 return CONTAINER_MPEG2TS;
1667 if (CheckMJpeg(buffer, buffer_size))
1668 return CONTAINER_MJPEG;
1669 if (CheckDV(buffer, buffer_size))
1670 return CONTAINER_DV;
1671 if (CheckH261(buffer, buffer_size))
1672 return CONTAINER_H261;
1673 if (CheckH263(buffer, buffer_size))
1674 return CONTAINER_H263;
1675 if (CheckH264(buffer, buffer_size))
1676 return CONTAINER_H264;
1677 if (CheckMpeg4BitStream(buffer, buffer_size))
1678 return CONTAINER_MPEG4BS;
1679 if (CheckVC1(buffer, buffer_size))
1680 return CONTAINER_VC1;
1681 if (CheckSrt(buffer, buffer_size))
1682 return CONTAINER_SRT;
1683 if (CheckGsm(buffer, buffer_size))
1684 return CONTAINER_GSM;
1689 if (AdvanceToStartCode(buffer, buffer_size, &offset, 4, 16, kAc3SyncWord)) {
1690 if (CheckAc3(buffer + offset, buffer_size - offset))
1691 return CONTAINER_AC3;
1692 if (CheckEac3(buffer + offset, buffer_size - offset))
1693 return CONTAINER_EAC3;
1698 if (CheckTtml(buffer, buffer_size))
1699 return CONTAINER_TTML;
1701 return CONTAINER_UNKNOWN;