5 #include "packager/media/base/container_names.h"
7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
14 #include "packager/base/logging.h"
15 #include "packager/media/base/bit_reader.h"
16 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
18 namespace edash_packager {
21 #define TAG(a, b, c, d) \
22 ((static_cast<uint32_t>(static_cast<uint8_t>(a)) << 24) | \
23 (static_cast<uint8_t>(b) << 16) | (static_cast<uint8_t>(c) << 8) | \
24 (static_cast<uint8_t>(d)))
32 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
35 static int Read16(
const uint8_t* p) {
36 return p[0] << 8 | p[1];
40 static uint32_t Read24(
const uint8_t* p) {
41 return p[0] << 16 | p[1] << 8 | p[2];
45 static uint32_t Read32(
const uint8_t* p) {
46 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
50 static uint32_t Read32LE(
const uint8_t* p) {
51 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
56 static bool StartsWith(
const uint8_t* buffer,
59 size_t prefix_size = strlen(prefix);
60 return (prefix_size <= buffer_size &&
61 memcmp(buffer, prefix, prefix_size) == 0);
66 static bool StartsWith(
const uint8_t* buffer,
68 const uint8_t* prefix,
70 return (prefix_size <= buffer_size &&
71 memcmp(buffer, prefix, prefix_size) == 0);
75 static uint64_t ReadBits(BitReader* reader,
int num_bits) {
76 DCHECK_GE(reader->bits_available(), num_bits);
77 DCHECK((num_bits > 0) && (num_bits <= 64));
79 reader->ReadBits(num_bits, &value);
83 const int kAc3FrameSizeTable[38][3] = {
84 { 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 },
85 { 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 },
86 { 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 },
87 { 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 },
88 { 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 },
89 { 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 },
90 { 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 },
91 { 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 },
92 { 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 },
93 { 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 },
94 { 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 }
98 static bool CheckAac(
const uint8_t* buffer,
int buffer_size) {
101 RCHECK(buffer_size > 6);
104 while (offset + 6 < buffer_size) {
105 BitReader reader(buffer + offset, 6);
108 RCHECK(ReadBits(&reader, 12) == 0xfff);
114 RCHECK(ReadBits(&reader, 2) == 0);
117 reader.SkipBits(1 + 2);
120 RCHECK(ReadBits(&reader, 4) != 15);
124 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1);
127 int size = ReadBits(&reader, 13);
134 const uint16_t kAc3SyncWord = 0x0b77;
137 static bool CheckAc3(
const uint8_t* buffer,
int buffer_size) {
143 RCHECK(buffer_size > 6);
146 while (offset + 6 < buffer_size) {
147 BitReader reader(buffer + offset, 6);
150 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
156 int sample_rate_code = ReadBits(&reader, 2);
157 RCHECK(sample_rate_code != 3);
160 int frame_size_code = ReadBits(&reader, 6);
161 RCHECK(frame_size_code < 38);
164 RCHECK(ReadBits(&reader, 5) < 10);
166 offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code];
172 static bool CheckEac3(
const uint8_t* buffer,
int buffer_size) {
178 RCHECK(buffer_size > 6);
181 while (offset + 6 < buffer_size) {
182 BitReader reader(buffer + offset, 6);
185 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
188 RCHECK(ReadBits(&reader, 2) != 3);
194 int frame_size = (ReadBits(&reader, 11) + 1) * 2;
195 RCHECK(frame_size >= 7);
198 reader.SkipBits(2 + 2 + 3 + 1);
201 int bit_stream_id = ReadBits(&reader, 5);
202 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16);
204 offset += frame_size;
210 static bool CheckBink(
const uint8_t* buffer,
int buffer_size) {
212 RCHECK(buffer_size >= 44);
215 RCHECK(Read32LE(buffer + 8) > 0);
218 int width = Read32LE(buffer + 20);
219 RCHECK(width > 0 && width <= 32767);
222 int height = Read32LE(buffer + 24);
223 RCHECK(height > 0 && height <= 32767);
226 RCHECK(Read32LE(buffer + 28) > 0);
229 RCHECK(Read32LE(buffer + 32) > 0);
232 return (Read32LE(buffer + 40) <= 256);
236 static bool CheckCaf(
const uint8_t* buffer,
int buffer_size) {
239 RCHECK(buffer_size >= 52);
240 BitReader reader(buffer, buffer_size);
243 RCHECK(ReadBits(&reader, 32) == TAG(
'c',
'a',
'f',
'f'));
246 RCHECK(ReadBits(&reader, 16) == 1);
252 RCHECK(ReadBits(&reader, 32) == TAG(
'd',
'e',
's',
'c'));
253 RCHECK(ReadBits(&reader, 64) == 32);
256 RCHECK(ReadBits(&reader, 64) != 0);
259 RCHECK(ReadBits(&reader, 32) != 0);
262 reader.SkipBits(32 + 32);
265 RCHECK(ReadBits(&reader, 32) != 0);
269 static bool kSamplingFrequencyValid[16] = {
false,
true,
true,
true,
false,
270 false,
true,
true,
true,
false,
271 false,
true,
true,
true,
false,
273 static bool kExtAudioIdValid[8] = {
true,
false,
true,
false,
false,
false,
277 static bool CheckDts(
const uint8_t* buffer,
int buffer_size) {
280 RCHECK(buffer_size > 11);
283 while (offset + 11 < buffer_size) {
284 BitReader reader(buffer + offset, 11);
287 RCHECK(ReadBits(&reader, 32) == 0x7ffe8001);
290 reader.SkipBits(1 + 5);
293 RCHECK(ReadBits(&reader, 1) == 0);
296 RCHECK(ReadBits(&reader, 7) >= 5);
299 int frame_size = ReadBits(&reader, 14);
300 RCHECK(frame_size >= 95);
306 RCHECK(kSamplingFrequencyValid[ReadBits(&reader, 4)]);
309 RCHECK(ReadBits(&reader, 5) <= 25);
312 RCHECK(ReadBits(&reader, 1) == 0);
315 reader.SkipBits(1 + 1 + 1 + 1);
318 RCHECK(kExtAudioIdValid[ReadBits(&reader, 3)]);
321 reader.SkipBits(1 + 1);
324 RCHECK(ReadBits(&reader, 2) != 3);
326 offset += frame_size + 1;
332 static bool CheckDV(
const uint8_t* buffer,
int buffer_size) {
335 RCHECK(buffer_size > 11);
338 int current_sequence_number = -1;
339 int last_block_number[6];
340 while (offset + 11 < buffer_size) {
341 BitReader reader(buffer + offset, 11);
344 int section = ReadBits(&reader, 3);
348 RCHECK(ReadBits(&reader, 1) == 1);
353 int sequence_number = ReadBits(&reader, 4);
359 RCHECK(ReadBits(&reader, 3) == 7);
361 int block_number = ReadBits(&reader, 8);
366 RCHECK(ReadBits(&reader, 1) == 0);
367 RCHECK(ReadBits(&reader, 11) == 0x7ff);
369 RCHECK(ReadBits(&reader, 4) == 0xf);
371 RCHECK(ReadBits(&reader, 4) == 0xf);
373 RCHECK(ReadBits(&reader, 4) == 0xf);
375 RCHECK(ReadBits(&reader, 24) == 0xffffff);
376 current_sequence_number = sequence_number;
377 for (
size_t i = 0; i < arraysize(last_block_number); ++i)
378 last_block_number[i] = -1;
381 RCHECK(sequence_number == current_sequence_number);
383 RCHECK(block_number > last_block_number[section]);
384 last_block_number[section] = block_number;
395 static bool CheckGsm(
const uint8_t* buffer,
int buffer_size) {
400 RCHECK(buffer_size >= 1024);
403 while (offset < buffer_size) {
405 RCHECK((buffer[offset] & 0xf0) == 0xd0);
416 static bool AdvanceToStartCode(
const uint8_t* buffer,
421 uint32_t start_code) {
422 DCHECK_GE(bytes_needed, 3);
423 DCHECK_LE(num_bits, 24);
426 uint32_t bits_to_shift = 24 - num_bits;
427 uint32_t mask = (1 << num_bits) - 1;
428 while (*offset + bytes_needed < buffer_size) {
429 uint32_t next = Read24(buffer + *offset);
430 if (((next >> bits_to_shift) & mask) == start_code)
438 static bool CheckH261(
const uint8_t* buffer,
int buffer_size) {
441 RCHECK(buffer_size > 16);
444 bool seen_start_code =
false;
447 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 20, 0x10)) {
450 return seen_start_code;
455 BitReader reader(buffer + offset, buffer_size - offset);
456 RCHECK(ReadBits(&reader, 20) == 0x10);
459 reader.SkipBits(5 + 6);
463 int extra = ReadBits(&reader, 1);
465 if (!reader.SkipBits(8))
466 return seen_start_code;
467 if (!reader.ReadBits(1, &extra))
468 return seen_start_code;
475 if (!reader.ReadBits(16, &next))
476 return seen_start_code;
480 seen_start_code =
true;
486 static bool CheckH263(
const uint8_t* buffer,
int buffer_size) {
490 RCHECK(buffer_size > 16);
493 bool seen_start_code =
false;
496 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 9, 22, 0x20)) {
499 return seen_start_code;
504 BitReader reader(buffer + offset, 9);
505 RCHECK(ReadBits(&reader, 22) == 0x20);
511 RCHECK(ReadBits(&reader, 2) == 2);
515 reader.SkipBits(1 + 1 + 1);
518 int format = ReadBits(&reader, 3);
519 RCHECK(format != 0 && format != 6);
523 int ufep = ReadBits(&reader, 3);
526 format = ReadBits(&reader, 3);
527 RCHECK(format != 0 && format != 7);
530 RCHECK(ReadBits(&reader, 4) == 8);
536 int picture_type_code = ReadBits(&reader, 3);
537 RCHECK(picture_type_code != 6 && picture_type_code != 7);
541 reader.SkipBits(1 + 1 + 1);
544 RCHECK(ReadBits(&reader, 3) == 1);
548 seen_start_code =
true;
554 static bool CheckH264(
const uint8_t* buffer,
int buffer_size) {
558 RCHECK(buffer_size > 4);
561 int parameter_count = 0;
564 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 24, 1)) {
567 return parameter_count > 0;
572 BitReader reader(buffer + offset, 4);
573 RCHECK(ReadBits(&reader, 24) == 1);
576 RCHECK(ReadBits(&reader, 1) == 0);
579 int nal_ref_idc = ReadBits(&reader, 2);
580 int nal_unit_type = ReadBits(&reader, 5);
582 switch (nal_unit_type) {
584 RCHECK(nal_ref_idc != 0);
591 RCHECK(nal_ref_idc == 0);
604 static const char kHlsSignature[] =
"#EXTM3U";
605 static const char kHls1[] =
"#EXT-X-STREAM-INF:";
606 static const char kHls2[] =
"#EXT-X-TARGETDURATION:";
607 static const char kHls3[] =
"#EXT-X-MEDIA-SEQUENCE:";
610 static bool CheckHls(
const uint8_t* buffer,
int buffer_size) {
615 if (StartsWith(buffer, buffer_size, kHlsSignature)) {
620 int offset = strlen(kHlsSignature);
621 while (offset < buffer_size) {
622 if (buffer[offset] ==
'#') {
623 if (StartsWith(buffer + offset, buffer_size - offset, kHls1) ||
624 StartsWith(buffer + offset, buffer_size - offset, kHls2) ||
625 StartsWith(buffer + offset, buffer_size - offset, kHls3)) {
636 static bool CheckMJpeg(
const uint8_t* buffer,
int buffer_size) {
639 RCHECK(buffer_size >= 16);
642 int last_restart = -1;
644 while (offset + 5 < buffer_size) {
646 RCHECK(buffer[offset] == 0xff);
647 uint8_t code = buffer[offset + 1];
648 RCHECK(code >= 0xc0 || code == 1);
661 if (code == 0xd8 || code == 1) {
664 }
else if (code >= 0xd0 && code <= 0xd7) {
666 int restart = code & 0x07;
667 if (last_restart >= 0)
668 RCHECK(restart == (last_restart + 1) % 8);
669 last_restart = restart;
673 int length = Read16(buffer + offset + 2) + 2;
679 int number_components = buffer[offset + 4];
680 RCHECK(length == 8 + 2 * number_components);
684 while (offset + 2 < buffer_size) {
685 if (buffer[offset] == 0xff && buffer[offset + 1] != 0)
696 return (num_codes > 1);
699 enum Mpeg2StartCodes {
700 PROGRAM_END_CODE = 0xb9,
701 PACK_START_CODE = 0xba
705 static bool CheckMpeg2ProgramStream(
const uint8_t* buffer,
int buffer_size) {
707 RCHECK(buffer_size > 14);
710 while (offset + 14 < buffer_size) {
711 BitReader reader(buffer + offset, 14);
714 RCHECK(ReadBits(&reader, 24) == 1);
715 RCHECK(ReadBits(&reader, 8) == PACK_START_CODE);
718 int mpeg_version = ReadBits(&reader, 2);
719 if (mpeg_version == 0) {
722 RCHECK(ReadBits(&reader, 2) == 2);
724 RCHECK(mpeg_version == 1);
731 RCHECK(ReadBits(&reader, 1) == 1);
737 RCHECK(ReadBits(&reader, 1) == 1);
743 RCHECK(ReadBits(&reader, 1) == 1);
745 if (mpeg_version == 0) {
747 RCHECK(ReadBits(&reader, 1) == 1);
753 RCHECK(ReadBits(&reader, 1) == 1);
763 RCHECK(ReadBits(&reader, 2) == 3);
769 int pack_stuffing_length = ReadBits(&reader, 3);
770 offset += 14 + pack_stuffing_length;
774 while (offset + 6 < buffer_size && Read24(buffer + offset) == 1) {
776 int stream_id = buffer[offset + 3];
779 if (mpeg_version == 0)
780 RCHECK(stream_id != 0xbc && stream_id < 0xf0);
782 RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe);
785 if (stream_id == PACK_START_CODE)
787 if (stream_id == PROGRAM_END_CODE)
790 int pes_length = Read16(buffer + offset + 4);
791 RCHECK(pes_length > 0);
792 offset = offset + 6 + pes_length;
800 const uint8_t kMpeg2SyncWord = 0x47;
803 static bool CheckMpeg2TransportStream(
const uint8_t* buffer,
int buffer_size) {
809 RCHECK(buffer_size >= 250);
812 int packet_length = -1;
813 while (buffer[offset] != kMpeg2SyncWord && offset < 20) {
818 while (offset + 6 < buffer_size) {
819 BitReader reader(buffer + offset, 6);
822 RCHECK(ReadBits(&reader, 8) == kMpeg2SyncWord);
826 reader.SkipBits(1 + 1 + 1);
829 int pid = ReadBits(&reader, 13);
830 RCHECK(pid < 3 || pid > 15);
836 int adaptation_field_control = ReadBits(&reader, 2);
837 RCHECK(adaptation_field_control != 0);
840 if (adaptation_field_control >= 2) {
845 int adaptation_field_length = ReadBits(&reader, 8);
846 if (adaptation_field_control == 2)
847 RCHECK(adaptation_field_length == 183);
849 RCHECK(adaptation_field_length <= 182);
853 if (packet_length < 0) {
854 if (buffer[offset + 188] == kMpeg2SyncWord)
856 else if (buffer[offset + 192] == kMpeg2SyncWord)
858 else if (buffer[offset + 204] == kMpeg2SyncWord)
863 offset += packet_length;
868 enum Mpeg4StartCodes {
869 VISUAL_OBJECT_SEQUENCE_START_CODE = 0xb0,
870 VISUAL_OBJECT_SEQUENCE_END_CODE = 0xb1,
871 VISUAL_OBJECT_START_CODE = 0xb5,
872 VOP_START_CODE = 0xb6
876 static bool CheckMpeg4BitStream(
const uint8_t* buffer,
int buffer_size) {
880 RCHECK(buffer_size > 4);
883 int sequence_start_count = 0;
884 int sequence_end_count = 0;
885 int visual_object_count = 0;
889 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 6, 24, 1)) {
892 return (sequence_start_count > 0 && visual_object_count > 0);
897 BitReader reader(buffer + offset, 6);
898 RCHECK(ReadBits(&reader, 24) == 1);
900 int start_code = ReadBits(&reader, 8);
901 RCHECK(start_code < 0x30 || start_code > 0xaf);
902 RCHECK(start_code < 0xb7 || start_code > 0xb9);
904 switch (start_code) {
905 case VISUAL_OBJECT_SEQUENCE_START_CODE: {
906 ++sequence_start_count;
908 int profile = ReadBits(&reader, 8);
910 RCHECK(profile < 0x04 || profile > 0x10);
911 RCHECK(profile < 0x13 || profile > 0x20);
912 RCHECK(profile < 0x23 || profile > 0x31);
913 RCHECK(profile < 0x35 || profile > 0x41);
914 RCHECK(profile < 0x43 || profile > 0x60);
915 RCHECK(profile < 0x65 || profile > 0x70);
916 RCHECK(profile < 0x73 || profile > 0x80);
917 RCHECK(profile < 0x83 || profile > 0x90);
918 RCHECK(profile < 0x95 || profile > 0xa0);
919 RCHECK(profile < 0xa4 || profile > 0xb0);
920 RCHECK(profile < 0xb5 || profile > 0xc0);
921 RCHECK(profile < 0xc3 || profile > 0xd0);
922 RCHECK(profile < 0xe4);
926 case VISUAL_OBJECT_SEQUENCE_END_CODE:
927 RCHECK(++sequence_end_count == sequence_start_count);
930 case VISUAL_OBJECT_START_CODE: {
931 ++visual_object_count;
932 if (ReadBits(&reader, 1) == 1) {
933 int visual_object_verid = ReadBits(&reader, 4);
934 RCHECK(visual_object_verid > 0 && visual_object_verid < 3);
935 RCHECK(ReadBits(&reader, 3) != 0);
937 int visual_object_type = ReadBits(&reader, 4);
938 RCHECK(visual_object_type > 0 && visual_object_type < 6);
943 RCHECK(++vop_count <= visual_object_count);
952 static bool CheckMov(
const uint8_t* buffer,
int buffer_size) {
955 RCHECK(buffer_size > 8);
958 while (offset + 8 < buffer_size) {
959 int atomsize = Read32(buffer + offset);
960 uint32_t atomtype = Read32(buffer + offset + 4);
963 case TAG(
'f',
't',
'y',
'p'):
964 case TAG(
'p',
'd',
'i',
'n'):
965 case TAG(
'm',
'o',
'o',
'v'):
966 case TAG(
'm',
'o',
'o',
'f'):
967 case TAG(
'm',
'f',
'r',
'a'):
968 case TAG(
'm',
'd',
'a',
't'):
969 case TAG(
'f',
'r',
'e',
'e'):
970 case TAG(
's',
'k',
'i',
'p'):
971 case TAG(
'm',
'e',
't',
'a'):
972 case TAG(
'm',
'e',
'c',
'o'):
973 case TAG(
's',
't',
'y',
'p'):
974 case TAG(
's',
'i',
'd',
'x'):
975 case TAG(
's',
's',
'i',
'x'):
976 case TAG(
'p',
'r',
'f',
't'):
977 case TAG(
'b',
'l',
'o',
'c'):
984 if (offset + 16 > buffer_size)
986 if (Read32(buffer + offset + 8) != 0)
988 atomsize = Read32(buffer + offset + 12);
1010 static int kSampleRateTable[4][4] = { { 11025, 12000, 8000, 0 },
1012 { 22050, 24000, 16000, 0 },
1013 { 44100, 48000, 32000, 0 }
1016 static int kBitRateTableV1L1[16] = { 0, 32, 64, 96, 128, 160, 192, 224, 256,
1017 288, 320, 352, 384, 416, 448, 0 };
1018 static int kBitRateTableV1L2[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160,
1019 192, 224, 256, 320, 384, 0 };
1020 static int kBitRateTableV1L3[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128,
1021 160, 192, 224, 256, 320, 0 };
1022 static int kBitRateTableV2L1[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1023 160, 176, 192, 224, 256, 0 };
1024 static int kBitRateTableV2L23[16] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,
1025 112, 128, 144, 160, 0 };
1027 static bool ValidMpegAudioFrameHeader(
const uint8_t* header,
1031 DCHECK_GE(header_size, 4);
1033 BitReader reader(header, 4);
1036 RCHECK(ReadBits(&reader, 11) == 0x7ff);
1039 int version = ReadBits(&reader, 2);
1040 RCHECK(version != 1);
1043 int layer = ReadBits(&reader, 2);
1050 int bitrate_index = ReadBits(&reader, 4);
1051 RCHECK(bitrate_index != 0xf);
1054 int sampling_index = ReadBits(&reader, 2);
1055 RCHECK(sampling_index != 3);
1058 int padding = ReadBits(&reader, 1);
1064 int sampling_rate = kSampleRateTable[version][sampling_index];
1066 if (version == VERSION_1) {
1067 if (layer == LAYER_1)
1068 bitrate = kBitRateTableV1L1[bitrate_index];
1069 else if (layer == LAYER_2)
1070 bitrate = kBitRateTableV1L2[bitrate_index];
1072 bitrate = kBitRateTableV1L3[bitrate_index];
1074 if (layer == LAYER_1)
1075 bitrate = kBitRateTableV2L1[bitrate_index];
1077 bitrate = kBitRateTableV2L23[bitrate_index];
1079 if (layer == LAYER_1)
1080 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4;
1082 *framesize = (144000 * bitrate) / sampling_rate + padding;
1083 return (bitrate > 0 && sampling_rate > 0);
1087 static int GetMp3HeaderSize(
const uint8_t* buffer,
int buffer_size) {
1088 DCHECK_GE(buffer_size, 9);
1089 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) +
1090 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10;
1091 if (buffer[5] & 0x10)
1097 static bool CheckMp3(
const uint8_t* buffer,
int buffer_size,
bool seenHeader) {
1098 RCHECK(buffer_size >= 10);
1104 offset = GetMp3HeaderSize(buffer, buffer_size);
1107 while (offset < buffer_size && buffer[offset] == 0)
1111 while (offset + 3 < buffer_size) {
1112 RCHECK(ValidMpegAudioFrameHeader(
1113 buffer + offset, buffer_size - offset, &framesize));
1118 offset += framesize;
1128 static bool VerifyNumber(
const uint8_t* buffer,
1132 RCHECK(*offset < buffer_size);
1135 while (isspace(buffer[*offset])) {
1137 RCHECK(*offset < buffer_size);
1142 while (--max_digits >= 0 && isdigit(buffer[*offset])) {
1145 if (*offset >= buffer_size)
1150 return (numSeen > 0);
1156 static inline bool VerifyCharacters(
const uint8_t* buffer,
1161 RCHECK(*offset < buffer_size);
1162 char c =
static_cast<char>(buffer[(*offset)++]);
1163 return (c == c1 || (c == c2 && c2 != 0));
1167 static bool CheckSrt(
const uint8_t* buffer,
int buffer_size) {
1169 RCHECK(buffer_size > 20);
1172 int offset = StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1173 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1174 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r'));
1177 while (VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r')) {}
1183 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1184 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1185 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1186 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1187 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1188 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
',',
'.'));
1189 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3));
1190 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
' ', 0));
1191 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'-', 0));
1192 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'-', 0));
1193 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'>', 0));
1194 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
' ', 0));
1195 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1196 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1197 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1198 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1199 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1200 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
',',
'.'));
1201 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3));
1206 static int GetElementId(BitReader* reader) {
1210 if (reader->bits_available() >= 8) {
1211 int num_bits_to_read = 0;
1212 static int prefix[] = { 0x80, 0x4000, 0x200000, 0x10000000 };
1213 for (
int i = 0; i < 4; ++i) {
1214 num_bits_to_read += 7;
1215 if (ReadBits(reader, 1) == 1) {
1216 if (reader->bits_available() < num_bits_to_read)
1219 return ReadBits(reader, num_bits_to_read) | prefix[i];
1228 static uint64_t GetVint(BitReader* reader) {
1232 if (reader->bits_available() >= 8) {
1233 int num_bits_to_read = 0;
1234 for (
int i = 0; i < 8; ++i) {
1235 num_bits_to_read += 7;
1236 if (ReadBits(reader, 1) == 1) {
1237 if (reader->bits_available() < num_bits_to_read)
1239 return ReadBits(reader, num_bits_to_read);
1246 return (reader->bits_available() / 8) + 2;
1250 static bool CheckWebm(
const uint8_t* buffer,
int buffer_size) {
1252 RCHECK(buffer_size > 12);
1254 BitReader reader(buffer, buffer_size);
1257 RCHECK(GetElementId(&reader) == 0x1a45dfa3);
1260 int header_size = GetVint(&reader);
1261 RCHECK(reader.bits_available() / 8 >= header_size);
1264 while (reader.bits_available() > 0) {
1265 int tag = GetElementId(&reader);
1266 int tagsize = GetVint(&reader);
1276 RCHECK(reader.SkipBits(tagsize * 8));
1281 switch (ReadBits(&reader, 32)) {
1282 case TAG(
'w',
'e',
'b',
'm') :
1284 case TAG(
'm',
'a',
't',
'r') :
1285 return (ReadBits(&reader, 32) == TAG(
'o',
's',
'k',
'a'));
1296 enum VC1StartCodes {
1297 VC1_FRAME_START_CODE = 0x0d,
1298 VC1_ENTRY_POINT_START_CODE = 0x0e,
1299 VC1_SEQUENCE_START_CODE = 0x0f
1303 static bool CheckVC1(
const uint8_t* buffer,
int buffer_size) {
1310 RCHECK(buffer_size >= 24);
1313 if (buffer[0] == 0xc5 &&
1314 Read32(buffer + 4) == 0x04 &&
1315 Read32(buffer + 20) == 0x0c) {
1317 BitReader reader(buffer + 8, 12);
1319 int profile = ReadBits(&reader, 4);
1320 if (profile == 0 || profile == 4) {
1322 reader.SkipBits(3 + 5 + 1);
1325 RCHECK(ReadBits(&reader, 1) == 0);
1331 RCHECK(ReadBits(&reader, 1) == 1);
1334 reader.SkipBits(1 + 1 + 2 + 1);
1337 RCHECK(ReadBits(&reader, 1) == 0);
1341 reader.SkipBits(1 + 1 + 1 + 3 + 2 + 1);
1344 RCHECK(ReadBits(&reader, 1) == 1);
1347 RCHECK(profile == 12);
1348 RCHECK(ReadBits(&reader, 28) == 0);
1352 RCHECK(ReadBits(&reader, 32) <= 8192);
1353 RCHECK(ReadBits(&reader, 32) <= 8192);
1359 int sequence_start_code = 0;
1360 int frame_start_code = 0;
1363 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 5, 24, 1)) {
1367 return (sequence_start_code > 0 && frame_start_code > 0);
1372 BitReader reader(buffer + offset, 5);
1373 RCHECK(ReadBits(&reader, 24) == 1);
1376 switch (ReadBits(&reader, 8)) {
1377 case VC1_SEQUENCE_START_CODE: {
1378 ++sequence_start_code;
1379 switch (ReadBits(&reader, 2)) {
1382 RCHECK(ReadBits(&reader, 2) == 0);
1387 RCHECK(ReadBits(&reader, 3) <= 4);
1388 RCHECK(ReadBits(&reader, 2) == 1);
1394 case VC1_ENTRY_POINT_START_CODE:
1397 RCHECK(sequence_start_code > 0);
1400 case VC1_FRAME_START_CODE:
1412 static const char kAmrSignature[] =
"#!AMR";
1413 static const uint8_t kAsfSignature[] = {0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
1414 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa,
1415 0x00, 0x62, 0xce, 0x6c};
1416 static const char kAssSignature[] =
"[Script Info]";
1417 static const char kAssBomSignature[] = UTF8_BYTE_ORDER_MARK
"[Script Info]";
1418 static const uint8_t kWtvSignature[] = {0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49,
1419 0xda, 0x11, 0xa6, 0x4e, 0x00, 0x07,
1420 0xe9, 0x5e, 0xad, 0x8d};
1425 static MediaContainerName LookupContainerByFirst4(
const uint8_t* buffer,
1428 if (buffer_size < 12)
1429 return CONTAINER_UNKNOWN;
1431 uint32_t first4 = Read32(buffer);
1434 if (CheckWebm(buffer, buffer_size))
1435 return CONTAINER_WEBM;
1439 if (StartsWith(buffer,
1442 sizeof(kAsfSignature))) {
1443 return CONTAINER_ASF;
1447 case TAG(
'#',
'!',
'A',
'M'):
1448 if (StartsWith(buffer, buffer_size, kAmrSignature))
1449 return CONTAINER_AMR;
1452 case TAG(
'#',
'E',
'X',
'T'):
1453 if (CheckHls(buffer, buffer_size))
1454 return CONTAINER_HLS;
1457 case TAG(
'.',
'R',
'M',
'F'):
1458 if (buffer[4] == 0 && buffer[5] == 0)
1459 return CONTAINER_RM;
1462 case TAG(
'.',
'r',
'a',
'\xfd'):
1463 return CONTAINER_RM;
1465 case TAG(
'B',
'I',
'K',
'b'):
1466 case TAG(
'B',
'I',
'K',
'd'):
1467 case TAG(
'B',
'I',
'K',
'f'):
1468 case TAG(
'B',
'I',
'K',
'g'):
1469 case TAG(
'B',
'I',
'K',
'h'):
1470 case TAG(
'B',
'I',
'K',
'i'):
1471 if (CheckBink(buffer, buffer_size))
1472 return CONTAINER_BINK;
1475 case TAG(
'c',
'a',
'f',
'f'):
1476 if (CheckCaf(buffer, buffer_size))
1477 return CONTAINER_CAF;
1480 case TAG(
'D',
'E',
'X',
'A'):
1481 if (buffer_size > 15 &&
1482 Read16(buffer + 11) <= 2048 &&
1483 Read16(buffer + 13) <= 2048) {
1484 return CONTAINER_DXA;
1488 case TAG(
'D',
'T',
'S',
'H'):
1489 if (Read32(buffer + 4) == TAG(
'D',
'H',
'D',
'R'))
1490 return CONTAINER_DTSHD;
1500 if (Read32(buffer + 4) != 0 && Read32(buffer + 8) != 0)
1501 return CONTAINER_IRCAM;
1504 case TAG(
'f',
'L',
'a',
'C'):
1505 return CONTAINER_FLAC;
1507 case TAG(
'F',
'L',
'V',0):
1508 case TAG(
'F',
'L',
'V',1):
1509 case TAG(
'F',
'L',
'V',2):
1510 case TAG(
'F',
'L',
'V',3):
1511 case TAG(
'F',
'L',
'V',4):
1512 if (buffer[5] == 0 && Read32(buffer + 5) > 8)
1513 return CONTAINER_FLV;
1516 case TAG(
'F',
'O',
'R',
'M'):
1517 switch (Read32(buffer + 8)) {
1518 case TAG(
'A',
'I',
'F',
'F'):
1519 case TAG(
'A',
'I',
'F',
'C'):
1520 return CONTAINER_AIFF;
1524 case TAG(
'M',
'A',
'C',
' '):
1525 return CONTAINER_APE;
1527 case TAG(
'O',
'N',
'2',
' '):
1528 if (Read32(buffer + 8) == TAG(
'O',
'N',
'2',
'f'))
1529 return CONTAINER_AVI;
1532 case TAG(
'O',
'g',
'g',
'S'):
1534 return CONTAINER_OGG;
1537 case TAG(
'R',
'F',
'6',
'4'):
1538 if (buffer_size > 16 && Read32(buffer + 12) == TAG(
'd',
's',
'6',
'4'))
1539 return CONTAINER_WAV;
1542 case TAG(
'R',
'I',
'F',
'F'):
1543 switch (Read32(buffer + 8)) {
1544 case TAG(
'A',
'V',
'I',
' '):
1545 case TAG(
'A',
'V',
'I',
'X'):
1546 case TAG(
'A',
'V',
'I',
'\x19'):
1547 case TAG(
'A',
'M',
'V',
' '):
1548 return CONTAINER_AVI;
1549 case TAG(
'W',
'A',
'V',
'E'):
1550 return CONTAINER_WAV;
1554 case TAG(
'[',
'S',
'c',
'r'):
1555 if (StartsWith(buffer, buffer_size, kAssSignature))
1556 return CONTAINER_ASS;
1559 case TAG(
'\xef',
'\xbb',
'\xbf',
'['):
1560 if (StartsWith(buffer, buffer_size, kAssBomSignature))
1561 return CONTAINER_ASS;
1568 if (CheckDts(buffer, buffer_size))
1569 return CONTAINER_DTS;
1573 if (StartsWith(buffer,
1576 sizeof(kWtvSignature))) {
1577 return CONTAINER_WTV;
1581 return CONTAINER_MPEG2PS;
1586 uint32_t first3 = first4 & 0xffffff00;
1588 case TAG(
'C',
'W',
'S',0):
1589 case TAG(
'F',
'W',
'S',0):
1590 return CONTAINER_SWF;
1592 case TAG(
'I',
'D',
'3',0):
1593 if (CheckMp3(buffer, buffer_size, true))
1594 return CONTAINER_MP3;
1599 uint32_t first2 = Read16(buffer);
1602 if (CheckAc3(buffer, buffer_size))
1603 return CONTAINER_AC3;
1604 if (CheckEac3(buffer, buffer_size))
1605 return CONTAINER_EAC3;
1612 if (CheckAac(buffer, buffer_size))
1613 return CONTAINER_AAC;
1618 if (CheckMp3(buffer, buffer_size,
false))
1619 return CONTAINER_MP3;
1621 return CONTAINER_UNKNOWN;
1625 const char kWebVtt[] =
"WEBVTT";
1627 bool CheckWebVtt(
const uint8_t* buffer,
int buffer_size) {
1629 StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1631 return StartsWith(buffer + offset, buffer_size - offset,
1632 reinterpret_cast<const uint8_t*>(kWebVtt),
1633 arraysize(kWebVtt) - 1);
1636 bool CheckTtml(
const uint8_t* buffer,
int buffer_size) {
1638 if (!StartsWith(buffer, buffer_size,
"<?xml"))
1644 xml::scoped_xml_ptr<xmlDoc> doc(
1645 xmlParseMemory(reinterpret_cast<const char*>(buffer), buffer_size));
1649 xmlNodePtr root_node = xmlDocGetRootElement(doc.get());
1650 std::string root_node_name(reinterpret_cast<const char*>(root_node->name));
1652 return root_node_name ==
"tt";
1658 MediaContainerName DetermineContainer(
const uint8_t* buffer,
int buffer_size) {
1662 if (CheckMov(buffer, buffer_size))
1663 return CONTAINER_MOV;
1667 MediaContainerName result = LookupContainerByFirst4(buffer, buffer_size);
1668 if (result != CONTAINER_UNKNOWN)
1672 if (CheckWebVtt(buffer, buffer_size))
1673 return CONTAINER_WEBVTT;
1676 if (CheckMpeg2ProgramStream(buffer, buffer_size))
1677 return CONTAINER_MPEG2PS;
1678 if (CheckMpeg2TransportStream(buffer, buffer_size))
1679 return CONTAINER_MPEG2TS;
1680 if (CheckMJpeg(buffer, buffer_size))
1681 return CONTAINER_MJPEG;
1682 if (CheckDV(buffer, buffer_size))
1683 return CONTAINER_DV;
1684 if (CheckH261(buffer, buffer_size))
1685 return CONTAINER_H261;
1686 if (CheckH263(buffer, buffer_size))
1687 return CONTAINER_H263;
1688 if (CheckH264(buffer, buffer_size))
1689 return CONTAINER_H264;
1690 if (CheckMpeg4BitStream(buffer, buffer_size))
1691 return CONTAINER_MPEG4BS;
1692 if (CheckVC1(buffer, buffer_size))
1693 return CONTAINER_VC1;
1694 if (CheckSrt(buffer, buffer_size))
1695 return CONTAINER_SRT;
1696 if (CheckGsm(buffer, buffer_size))
1697 return CONTAINER_GSM;
1702 if (AdvanceToStartCode(buffer, buffer_size, &offset, 4, 16, kAc3SyncWord)) {
1703 if (CheckAc3(buffer + offset, buffer_size - offset))
1704 return CONTAINER_AC3;
1705 if (CheckEac3(buffer + offset, buffer_size - offset))
1706 return CONTAINER_EAC3;
1711 if (CheckTtml(buffer, buffer_size))
1712 return CONTAINER_TTML;
1714 return CONTAINER_UNKNOWN;