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/base/strings/string_util.h"
16 #include "packager/media/base/bit_reader.h"
17 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
22 #define TAG(a, b, c, d) \
23 ((static_cast<uint32_t>(static_cast<uint8_t>(a)) << 24) | \
24 (static_cast<uint8_t>(b) << 16) | (static_cast<uint8_t>(c) << 8) | \
25 (static_cast<uint8_t>(d)))
33 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
36 static int Read16(
const uint8_t* p) {
37 return p[0] << 8 | p[1];
41 static uint32_t Read24(
const uint8_t* p) {
42 return p[0] << 16 | p[1] << 8 | p[2];
46 static uint32_t Read32(
const uint8_t* p) {
47 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
51 static uint32_t Read32LE(
const uint8_t* p) {
52 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
57 static bool StartsWith(
const uint8_t* buffer,
60 size_t prefix_size = strlen(prefix);
61 return (prefix_size <= buffer_size &&
62 memcmp(buffer, prefix, prefix_size) == 0);
67 static bool StartsWith(
const uint8_t* buffer,
69 const uint8_t* prefix,
71 return (prefix_size <= buffer_size &&
72 memcmp(buffer, prefix, prefix_size) == 0);
76 static uint64_t ReadBits(BitReader* reader,
int num_bits) {
77 DCHECK_GE(static_cast<int>(reader->bits_available()), num_bits);
78 DCHECK((num_bits > 0) && (num_bits <= 64));
80 reader->ReadBits(num_bits, &value);
84 const int kAc3FrameSizeTable[38][3] = {
85 { 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 },
86 { 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 },
87 { 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 },
88 { 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 },
89 { 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 },
90 { 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 },
91 { 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 },
92 { 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 },
93 { 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 },
94 { 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 },
95 { 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 }
99 static bool CheckAac(
const uint8_t* buffer,
int buffer_size) {
102 RCHECK(buffer_size > 6);
105 while (offset + 6 < buffer_size) {
106 BitReader reader(buffer + offset, 6);
109 RCHECK(ReadBits(&reader, 12) == 0xfff);
115 RCHECK(ReadBits(&reader, 2) == 0);
118 reader.SkipBits(1 + 2);
121 RCHECK(ReadBits(&reader, 4) != 15);
125 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1);
128 int size = ReadBits(&reader, 13);
135 const uint16_t kAc3SyncWord = 0x0b77;
138 static bool CheckAc3(
const uint8_t* buffer,
int buffer_size) {
144 RCHECK(buffer_size > 6);
147 while (offset + 6 < buffer_size) {
148 BitReader reader(buffer + offset, 6);
151 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
157 int sample_rate_code = ReadBits(&reader, 2);
158 RCHECK(sample_rate_code != 3);
161 int frame_size_code = ReadBits(&reader, 6);
162 RCHECK(frame_size_code < 38);
165 RCHECK(ReadBits(&reader, 5) < 10);
167 offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code];
173 static bool CheckEac3(
const uint8_t* buffer,
int buffer_size) {
179 RCHECK(buffer_size > 6);
182 while (offset + 6 < buffer_size) {
183 BitReader reader(buffer + offset, 6);
186 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
189 RCHECK(ReadBits(&reader, 2) != 3);
195 int frame_size = (ReadBits(&reader, 11) + 1) * 2;
196 RCHECK(frame_size >= 7);
199 reader.SkipBits(2 + 2 + 3 + 1);
202 int bit_stream_id = ReadBits(&reader, 5);
203 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16);
205 offset += frame_size;
211 static bool CheckBink(
const uint8_t* buffer,
int buffer_size) {
213 RCHECK(buffer_size >= 44);
216 RCHECK(Read32LE(buffer + 8) > 0);
219 int width = Read32LE(buffer + 20);
220 RCHECK(width > 0 && width <= 32767);
223 int height = Read32LE(buffer + 24);
224 RCHECK(height > 0 && height <= 32767);
227 RCHECK(Read32LE(buffer + 28) > 0);
230 RCHECK(Read32LE(buffer + 32) > 0);
233 return (Read32LE(buffer + 40) <= 256);
237 static bool CheckCaf(
const uint8_t* buffer,
int buffer_size) {
240 RCHECK(buffer_size >= 52);
241 BitReader reader(buffer, buffer_size);
244 RCHECK(ReadBits(&reader, 32) == TAG(
'c',
'a',
'f',
'f'));
247 RCHECK(ReadBits(&reader, 16) == 1);
253 RCHECK(ReadBits(&reader, 32) == TAG(
'd',
'e',
's',
'c'));
254 RCHECK(ReadBits(&reader, 64) == 32);
257 RCHECK(ReadBits(&reader, 64) != 0);
260 RCHECK(ReadBits(&reader, 32) != 0);
263 reader.SkipBits(32 + 32);
266 RCHECK(ReadBits(&reader, 32) != 0);
270 static bool kSamplingFrequencyValid[16] = {
false,
true,
true,
true,
false,
271 false,
true,
true,
true,
false,
272 false,
true,
true,
true,
false,
274 static bool kExtAudioIdValid[8] = {
true,
false,
true,
false,
false,
false,
278 static bool CheckDts(
const uint8_t* buffer,
int buffer_size) {
281 RCHECK(buffer_size > 11);
284 while (offset + 11 < buffer_size) {
285 BitReader reader(buffer + offset, 11);
288 RCHECK(ReadBits(&reader, 32) == 0x7ffe8001);
291 reader.SkipBits(1 + 5);
294 RCHECK(ReadBits(&reader, 1) == 0);
297 RCHECK(ReadBits(&reader, 7) >= 5);
300 int frame_size = ReadBits(&reader, 14);
301 RCHECK(frame_size >= 95);
307 RCHECK(kSamplingFrequencyValid[ReadBits(&reader, 4)]);
310 RCHECK(ReadBits(&reader, 5) <= 25);
313 RCHECK(ReadBits(&reader, 1) == 0);
316 reader.SkipBits(1 + 1 + 1 + 1);
319 RCHECK(kExtAudioIdValid[ReadBits(&reader, 3)]);
322 reader.SkipBits(1 + 1);
325 RCHECK(ReadBits(&reader, 2) != 3);
327 offset += frame_size + 1;
333 static bool CheckDV(
const uint8_t* buffer,
int buffer_size) {
336 RCHECK(buffer_size > 11);
339 int current_sequence_number = -1;
340 int last_block_number[6];
341 while (offset + 11 < buffer_size) {
342 BitReader reader(buffer + offset, 11);
345 int section = ReadBits(&reader, 3);
349 RCHECK(ReadBits(&reader, 1) == 1);
354 int sequence_number = ReadBits(&reader, 4);
360 RCHECK(ReadBits(&reader, 3) == 7);
362 int block_number = ReadBits(&reader, 8);
367 RCHECK(ReadBits(&reader, 1) == 0);
368 RCHECK(ReadBits(&reader, 11) == 0x7ff);
370 RCHECK(ReadBits(&reader, 4) == 0xf);
372 RCHECK(ReadBits(&reader, 4) == 0xf);
374 RCHECK(ReadBits(&reader, 4) == 0xf);
376 RCHECK(ReadBits(&reader, 24) == 0xffffff);
377 current_sequence_number = sequence_number;
378 for (
size_t i = 0; i < arraysize(last_block_number); ++i)
379 last_block_number[i] = -1;
382 RCHECK(sequence_number == current_sequence_number);
384 RCHECK(block_number > last_block_number[section]);
385 last_block_number[section] = block_number;
396 static bool CheckGsm(
const uint8_t* buffer,
int buffer_size) {
401 RCHECK(buffer_size >= 1024);
404 while (offset < buffer_size) {
406 RCHECK((buffer[offset] & 0xf0) == 0xd0);
417 static bool AdvanceToStartCode(
const uint8_t* buffer,
422 uint32_t start_code) {
423 DCHECK_GE(bytes_needed, 3);
424 DCHECK_LE(num_bits, 24);
427 uint32_t bits_to_shift = 24 - num_bits;
428 uint32_t mask = (1 << num_bits) - 1;
429 while (*offset + bytes_needed < buffer_size) {
430 uint32_t next = Read24(buffer + *offset);
431 if (((next >> bits_to_shift) & mask) == start_code)
439 static bool CheckH261(
const uint8_t* buffer,
int buffer_size) {
442 RCHECK(buffer_size > 16);
445 bool seen_start_code =
false;
448 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 20, 0x10)) {
451 return seen_start_code;
456 BitReader reader(buffer + offset, buffer_size - offset);
457 RCHECK(ReadBits(&reader, 20) == 0x10);
460 reader.SkipBits(5 + 6);
464 int extra = ReadBits(&reader, 1);
466 if (!reader.SkipBits(8))
467 return seen_start_code;
468 if (!reader.ReadBits(1, &extra))
469 return seen_start_code;
476 if (!reader.ReadBits(16, &next))
477 return seen_start_code;
481 seen_start_code =
true;
487 static bool CheckH263(
const uint8_t* buffer,
int buffer_size) {
491 RCHECK(buffer_size > 16);
494 bool seen_start_code =
false;
497 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 9, 22, 0x20)) {
500 return seen_start_code;
505 BitReader reader(buffer + offset, 9);
506 RCHECK(ReadBits(&reader, 22) == 0x20);
512 RCHECK(ReadBits(&reader, 2) == 2);
516 reader.SkipBits(1 + 1 + 1);
519 int format = ReadBits(&reader, 3);
520 RCHECK(format != 0 && format != 6);
524 int ufep = ReadBits(&reader, 3);
527 format = ReadBits(&reader, 3);
528 RCHECK(format != 0 && format != 7);
531 RCHECK(ReadBits(&reader, 4) == 8);
537 int picture_type_code = ReadBits(&reader, 3);
538 RCHECK(picture_type_code != 6 && picture_type_code != 7);
542 reader.SkipBits(1 + 1 + 1);
545 RCHECK(ReadBits(&reader, 3) == 1);
549 seen_start_code =
true;
555 static bool CheckH264(
const uint8_t* buffer,
int buffer_size) {
559 RCHECK(buffer_size > 4);
562 int parameter_count = 0;
565 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 24, 1)) {
568 return parameter_count > 0;
573 BitReader reader(buffer + offset, 4);
574 RCHECK(ReadBits(&reader, 24) == 1);
577 RCHECK(ReadBits(&reader, 1) == 0);
580 int nal_ref_idc = ReadBits(&reader, 2);
581 int nal_unit_type = ReadBits(&reader, 5);
583 switch (nal_unit_type) {
585 RCHECK(nal_ref_idc != 0);
592 RCHECK(nal_ref_idc == 0);
605 static const char kHlsSignature[] =
"#EXTM3U";
606 static const char kHls1[] =
"#EXT-X-STREAM-INF:";
607 static const char kHls2[] =
"#EXT-X-TARGETDURATION:";
608 static const char kHls3[] =
"#EXT-X-MEDIA-SEQUENCE:";
611 static bool CheckHls(
const uint8_t* buffer,
int buffer_size) {
616 if (StartsWith(buffer, buffer_size, kHlsSignature)) {
621 int offset = strlen(kHlsSignature);
622 while (offset < buffer_size) {
623 if (buffer[offset] ==
'#') {
624 if (StartsWith(buffer + offset, buffer_size - offset, kHls1) ||
625 StartsWith(buffer + offset, buffer_size - offset, kHls2) ||
626 StartsWith(buffer + offset, buffer_size - offset, kHls3)) {
637 static bool CheckMJpeg(
const uint8_t* buffer,
int buffer_size) {
640 RCHECK(buffer_size >= 16);
643 int last_restart = -1;
645 while (offset + 5 < buffer_size) {
647 RCHECK(buffer[offset] == 0xff);
648 uint8_t code = buffer[offset + 1];
649 RCHECK(code >= 0xc0 || code == 1);
662 if (code == 0xd8 || code == 1) {
665 }
else if (code >= 0xd0 && code <= 0xd7) {
667 int restart = code & 0x07;
668 if (last_restart >= 0)
669 RCHECK(restart == (last_restart + 1) % 8);
670 last_restart = restart;
674 int length = Read16(buffer + offset + 2) + 2;
680 int number_components = buffer[offset + 4];
681 RCHECK(length == 8 + 2 * number_components);
685 while (offset + 2 < buffer_size) {
686 if (buffer[offset] == 0xff && buffer[offset + 1] != 0)
697 return (num_codes > 1);
700 enum Mpeg2StartCodes {
701 PROGRAM_END_CODE = 0xb9,
702 PACK_START_CODE = 0xba
706 static bool CheckMpeg2ProgramStream(
const uint8_t* buffer,
int buffer_size) {
708 RCHECK(buffer_size > 14);
711 while (offset + 14 < buffer_size) {
712 BitReader reader(buffer + offset, 14);
715 RCHECK(ReadBits(&reader, 24) == 1);
716 RCHECK(ReadBits(&reader, 8) == PACK_START_CODE);
719 int mpeg_version = ReadBits(&reader, 2);
720 if (mpeg_version == 0) {
723 RCHECK(ReadBits(&reader, 2) == 2);
725 RCHECK(mpeg_version == 1);
732 RCHECK(ReadBits(&reader, 1) == 1);
738 RCHECK(ReadBits(&reader, 1) == 1);
744 RCHECK(ReadBits(&reader, 1) == 1);
746 if (mpeg_version == 0) {
748 RCHECK(ReadBits(&reader, 1) == 1);
754 RCHECK(ReadBits(&reader, 1) == 1);
764 RCHECK(ReadBits(&reader, 2) == 3);
770 int pack_stuffing_length = ReadBits(&reader, 3);
771 offset += 14 + pack_stuffing_length;
775 while (offset + 6 < buffer_size && Read24(buffer + offset) == 1) {
777 int stream_id = buffer[offset + 3];
780 if (mpeg_version == 0)
781 RCHECK(stream_id != 0xbc && stream_id < 0xf0);
783 RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe);
786 if (stream_id == PACK_START_CODE)
788 if (stream_id == PROGRAM_END_CODE)
791 int pes_length = Read16(buffer + offset + 4);
792 RCHECK(pes_length > 0);
793 offset = offset + 6 + pes_length;
801 const uint8_t kMpeg2SyncWord = 0x47;
804 static bool CheckMpeg2TransportStream(
const uint8_t* buffer,
int buffer_size) {
810 RCHECK(buffer_size >= 250);
813 int packet_length = -1;
814 while (buffer[offset] != kMpeg2SyncWord && offset < 20) {
819 while (offset + 6 < buffer_size) {
820 BitReader reader(buffer + offset, 6);
823 RCHECK(ReadBits(&reader, 8) == kMpeg2SyncWord);
827 reader.SkipBits(1 + 1 + 1);
830 int pid = ReadBits(&reader, 13);
831 RCHECK(pid < 3 || pid > 15);
837 int adaptation_field_control = ReadBits(&reader, 2);
838 RCHECK(adaptation_field_control != 0);
841 if (adaptation_field_control >= 2) {
846 int adaptation_field_length = ReadBits(&reader, 8);
847 if (adaptation_field_control == 2)
848 RCHECK(adaptation_field_length == 183);
850 RCHECK(adaptation_field_length <= 182);
854 if (packet_length < 0) {
855 if (buffer[offset + 188] == kMpeg2SyncWord)
857 else if (buffer[offset + 192] == kMpeg2SyncWord)
859 else if (buffer[offset + 204] == kMpeg2SyncWord)
864 offset += packet_length;
869 enum Mpeg4StartCodes {
870 VISUAL_OBJECT_SEQUENCE_START_CODE = 0xb0,
871 VISUAL_OBJECT_SEQUENCE_END_CODE = 0xb1,
872 VISUAL_OBJECT_START_CODE = 0xb5,
873 VOP_START_CODE = 0xb6
877 static bool CheckMpeg4BitStream(
const uint8_t* buffer,
int buffer_size) {
881 RCHECK(buffer_size > 4);
884 int sequence_start_count = 0;
885 int sequence_end_count = 0;
886 int visual_object_count = 0;
890 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 6, 24, 1)) {
893 return (sequence_start_count > 0 && visual_object_count > 0);
898 BitReader reader(buffer + offset, 6);
899 RCHECK(ReadBits(&reader, 24) == 1);
901 int start_code = ReadBits(&reader, 8);
902 RCHECK(start_code < 0x30 || start_code > 0xaf);
903 RCHECK(start_code < 0xb7 || start_code > 0xb9);
905 switch (start_code) {
906 case VISUAL_OBJECT_SEQUENCE_START_CODE: {
907 ++sequence_start_count;
909 int profile = ReadBits(&reader, 8);
911 RCHECK(profile < 0x04 || profile > 0x10);
912 RCHECK(profile < 0x13 || profile > 0x20);
913 RCHECK(profile < 0x23 || profile > 0x31);
914 RCHECK(profile < 0x35 || profile > 0x41);
915 RCHECK(profile < 0x43 || profile > 0x60);
916 RCHECK(profile < 0x65 || profile > 0x70);
917 RCHECK(profile < 0x73 || profile > 0x80);
918 RCHECK(profile < 0x83 || profile > 0x90);
919 RCHECK(profile < 0x95 || profile > 0xa0);
920 RCHECK(profile < 0xa4 || profile > 0xb0);
921 RCHECK(profile < 0xb5 || profile > 0xc0);
922 RCHECK(profile < 0xc3 || profile > 0xd0);
923 RCHECK(profile < 0xe4);
927 case VISUAL_OBJECT_SEQUENCE_END_CODE:
928 RCHECK(++sequence_end_count == sequence_start_count);
931 case VISUAL_OBJECT_START_CODE: {
932 ++visual_object_count;
933 if (ReadBits(&reader, 1) == 1) {
934 int visual_object_verid = ReadBits(&reader, 4);
935 RCHECK(visual_object_verid > 0 && visual_object_verid < 3);
936 RCHECK(ReadBits(&reader, 3) != 0);
938 int visual_object_type = ReadBits(&reader, 4);
939 RCHECK(visual_object_type > 0 && visual_object_type < 6);
944 RCHECK(++vop_count <= visual_object_count);
953 static bool CheckMov(
const uint8_t* buffer,
int buffer_size) {
956 RCHECK(buffer_size > 8);
960 while (offset + 8 < buffer_size) {
961 int atomsize = Read32(buffer + offset);
962 uint32_t atomtype = Read32(buffer + offset + 4);
965 case TAG(
'f',
't',
'y',
'p'):
966 case TAG(
'p',
'd',
'i',
'n'):
967 case TAG(
'b',
'l',
'o',
'c'):
968 case TAG(
'm',
'o',
'o',
'v'):
969 case TAG(
'm',
'o',
'o',
'f'):
970 case TAG(
'm',
'f',
'r',
'a'):
971 case TAG(
'm',
'd',
'a',
't'):
972 case TAG(
'f',
'r',
'e',
'e'):
973 case TAG(
's',
'k',
'i',
'p'):
974 case TAG(
'm',
'e',
't',
'a'):
975 case TAG(
'm',
'e',
'c',
'o'):
976 case TAG(
's',
't',
'y',
'p'):
977 case TAG(
's',
'i',
'd',
'x'):
978 case TAG(
's',
's',
'i',
'x'):
979 case TAG(
'p',
'r',
'f',
't'):
980 case TAG(
'u',
'u',
'i',
'd'):
985 if (++boxes_seen >= 2)
994 if (offset + 16 > buffer_size)
996 if (Read32(buffer + offset + 8) != 0)
998 atomsize = Read32(buffer + offset + 12);
1020 static int kSampleRateTable[4][4] = { { 11025, 12000, 8000, 0 },
1022 { 22050, 24000, 16000, 0 },
1023 { 44100, 48000, 32000, 0 }
1026 static int kBitRateTableV1L1[16] = { 0, 32, 64, 96, 128, 160, 192, 224, 256,
1027 288, 320, 352, 384, 416, 448, 0 };
1028 static int kBitRateTableV1L2[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160,
1029 192, 224, 256, 320, 384, 0 };
1030 static int kBitRateTableV1L3[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128,
1031 160, 192, 224, 256, 320, 0 };
1032 static int kBitRateTableV2L1[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1033 160, 176, 192, 224, 256, 0 };
1034 static int kBitRateTableV2L23[16] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,
1035 112, 128, 144, 160, 0 };
1037 static bool ValidMpegAudioFrameHeader(
const uint8_t* header,
1041 DCHECK_GE(header_size, 4);
1043 BitReader reader(header, 4);
1046 RCHECK(ReadBits(&reader, 11) == 0x7ff);
1049 int version = ReadBits(&reader, 2);
1050 RCHECK(version != 1);
1053 int layer = ReadBits(&reader, 2);
1060 int bitrate_index = ReadBits(&reader, 4);
1061 RCHECK(bitrate_index != 0xf);
1064 int sampling_index = ReadBits(&reader, 2);
1065 RCHECK(sampling_index != 3);
1068 int padding = ReadBits(&reader, 1);
1074 int sampling_rate = kSampleRateTable[version][sampling_index];
1076 if (version == VERSION_1) {
1077 if (layer == LAYER_1)
1078 bitrate = kBitRateTableV1L1[bitrate_index];
1079 else if (layer == LAYER_2)
1080 bitrate = kBitRateTableV1L2[bitrate_index];
1082 bitrate = kBitRateTableV1L3[bitrate_index];
1084 if (layer == LAYER_1)
1085 bitrate = kBitRateTableV2L1[bitrate_index];
1087 bitrate = kBitRateTableV2L23[bitrate_index];
1089 if (layer == LAYER_1)
1090 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4;
1092 *framesize = (144000 * bitrate) / sampling_rate + padding;
1093 return (bitrate > 0 && sampling_rate > 0);
1097 static int GetMp3HeaderSize(
const uint8_t* buffer,
int buffer_size) {
1098 DCHECK_GE(buffer_size, 9);
1099 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) +
1100 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10;
1101 if (buffer[5] & 0x10)
1107 static bool CheckMp3(
const uint8_t* buffer,
int buffer_size,
bool seenHeader) {
1108 RCHECK(buffer_size >= 10);
1114 offset = GetMp3HeaderSize(buffer, buffer_size);
1117 while (offset < buffer_size && buffer[offset] == 0)
1121 while (offset + 3 < buffer_size) {
1122 RCHECK(ValidMpegAudioFrameHeader(
1123 buffer + offset, buffer_size - offset, &framesize));
1128 offset += framesize;
1138 static bool VerifyNumber(
const uint8_t* buffer,
1142 RCHECK(*offset < buffer_size);
1145 while (isspace(buffer[*offset])) {
1147 RCHECK(*offset < buffer_size);
1152 while (--max_digits >= 0 && isdigit(buffer[*offset])) {
1155 if (*offset >= buffer_size)
1160 return (numSeen > 0);
1166 static inline bool VerifyCharacters(
const uint8_t* buffer,
1171 RCHECK(*offset < buffer_size);
1172 char c =
static_cast<char>(buffer[(*offset)++]);
1173 return (c == c1 || (c == c2 && c2 != 0));
1177 static bool CheckSrt(
const uint8_t* buffer,
int buffer_size) {
1179 RCHECK(buffer_size > 20);
1182 int offset = StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1183 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1184 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r'));
1187 while (VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r')) {}
1193 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1194 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1195 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1196 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1197 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1198 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
',',
'.'));
1199 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3));
1200 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
' ', 0));
1201 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'-', 0));
1202 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'-', 0));
1203 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'>', 0));
1204 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
' ', 0));
1205 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1206 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1207 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1208 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1209 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1210 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
',',
'.'));
1211 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3));
1216 static int GetElementId(BitReader* reader) {
1220 if (reader->bits_available() >= 8) {
1221 int num_bits_to_read = 0;
1222 static int prefix[] = { 0x80, 0x4000, 0x200000, 0x10000000 };
1223 for (
int i = 0; i < 4; ++i) {
1224 num_bits_to_read += 7;
1225 if (ReadBits(reader, 1) == 1) {
1226 if (static_cast<int>(reader->bits_available()) < num_bits_to_read)
1229 return ReadBits(reader, num_bits_to_read) | prefix[i];
1238 static uint64_t GetVint(BitReader* reader) {
1242 if (reader->bits_available() >= 8) {
1243 int num_bits_to_read = 0;
1244 for (
int i = 0; i < 8; ++i) {
1245 num_bits_to_read += 7;
1246 if (ReadBits(reader, 1) == 1) {
1247 if (static_cast<int>(reader->bits_available()) < num_bits_to_read)
1249 return ReadBits(reader, num_bits_to_read);
1256 return (reader->bits_available() / 8) + 2;
1260 static bool CheckWebm(
const uint8_t* buffer,
int buffer_size) {
1262 RCHECK(buffer_size > 12);
1264 BitReader reader(buffer, buffer_size);
1267 RCHECK(GetElementId(&reader) == 0x1a45dfa3);
1270 int header_size = GetVint(&reader);
1271 RCHECK(static_cast<int>(reader.bits_available()) / 8 >= header_size);
1274 while (reader.bits_available() > 0) {
1275 int tag = GetElementId(&reader);
1276 int tagsize = GetVint(&reader);
1286 RCHECK(reader.SkipBits(tagsize * 8));
1291 switch (ReadBits(&reader, 32)) {
1292 case TAG(
'w',
'e',
'b',
'm') :
1294 case TAG(
'm',
'a',
't',
'r') :
1295 return (ReadBits(&reader, 32) == TAG(
'o',
's',
'k',
'a'));
1306 enum VC1StartCodes {
1307 VC1_FRAME_START_CODE = 0x0d,
1308 VC1_ENTRY_POINT_START_CODE = 0x0e,
1309 VC1_SEQUENCE_START_CODE = 0x0f
1313 static bool CheckVC1(
const uint8_t* buffer,
int buffer_size) {
1320 RCHECK(buffer_size >= 24);
1323 if (buffer[0] == 0xc5 &&
1324 Read32(buffer + 4) == 0x04 &&
1325 Read32(buffer + 20) == 0x0c) {
1327 BitReader reader(buffer + 8, 12);
1329 int profile = ReadBits(&reader, 4);
1330 if (profile == 0 || profile == 4) {
1332 reader.SkipBits(3 + 5 + 1);
1335 RCHECK(ReadBits(&reader, 1) == 0);
1341 RCHECK(ReadBits(&reader, 1) == 1);
1344 reader.SkipBits(1 + 1 + 2 + 1);
1347 RCHECK(ReadBits(&reader, 1) == 0);
1351 reader.SkipBits(1 + 1 + 1 + 3 + 2 + 1);
1354 RCHECK(ReadBits(&reader, 1) == 1);
1357 RCHECK(profile == 12);
1358 RCHECK(ReadBits(&reader, 28) == 0);
1362 RCHECK(ReadBits(&reader, 32) <= 8192);
1363 RCHECK(ReadBits(&reader, 32) <= 8192);
1369 int sequence_start_code = 0;
1370 int frame_start_code = 0;
1373 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 5, 24, 1)) {
1377 return (sequence_start_code > 0 && frame_start_code > 0);
1382 BitReader reader(buffer + offset, 5);
1383 RCHECK(ReadBits(&reader, 24) == 1);
1386 switch (ReadBits(&reader, 8)) {
1387 case VC1_SEQUENCE_START_CODE: {
1388 ++sequence_start_code;
1389 switch (ReadBits(&reader, 2)) {
1392 RCHECK(ReadBits(&reader, 2) == 0);
1397 RCHECK(ReadBits(&reader, 3) <= 4);
1398 RCHECK(ReadBits(&reader, 2) == 1);
1404 case VC1_ENTRY_POINT_START_CODE:
1407 RCHECK(sequence_start_code > 0);
1410 case VC1_FRAME_START_CODE:
1422 static const char kAmrSignature[] =
"#!AMR";
1423 static const uint8_t kAsfSignature[] = {0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
1424 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa,
1425 0x00, 0x62, 0xce, 0x6c};
1426 static const char kAssSignature[] =
"[Script Info]";
1427 static const char kAssBomSignature[] = UTF8_BYTE_ORDER_MARK
"[Script Info]";
1428 static const uint8_t kWtvSignature[] = {0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49,
1429 0xda, 0x11, 0xa6, 0x4e, 0x00, 0x07,
1430 0xe9, 0x5e, 0xad, 0x8d};
1435 static MediaContainerName LookupContainerByFirst4(
const uint8_t* buffer,
1438 if (buffer_size < 12)
1439 return CONTAINER_UNKNOWN;
1441 uint32_t first4 = Read32(buffer);
1444 if (CheckWebm(buffer, buffer_size))
1445 return CONTAINER_WEBM;
1449 if (StartsWith(buffer,
1452 sizeof(kAsfSignature))) {
1453 return CONTAINER_ASF;
1457 case TAG(
'#',
'!',
'A',
'M'):
1458 if (StartsWith(buffer, buffer_size, kAmrSignature))
1459 return CONTAINER_AMR;
1462 case TAG(
'#',
'E',
'X',
'T'):
1463 if (CheckHls(buffer, buffer_size))
1464 return CONTAINER_HLS;
1467 case TAG(
'.',
'R',
'M',
'F'):
1468 if (buffer[4] == 0 && buffer[5] == 0)
1469 return CONTAINER_RM;
1472 case TAG(
'.',
'r',
'a',
'\xfd'):
1473 return CONTAINER_RM;
1475 case TAG(
'B',
'I',
'K',
'b'):
1476 case TAG(
'B',
'I',
'K',
'd'):
1477 case TAG(
'B',
'I',
'K',
'f'):
1478 case TAG(
'B',
'I',
'K',
'g'):
1479 case TAG(
'B',
'I',
'K',
'h'):
1480 case TAG(
'B',
'I',
'K',
'i'):
1481 if (CheckBink(buffer, buffer_size))
1482 return CONTAINER_BINK;
1485 case TAG(
'c',
'a',
'f',
'f'):
1486 if (CheckCaf(buffer, buffer_size))
1487 return CONTAINER_CAF;
1490 case TAG(
'D',
'E',
'X',
'A'):
1491 if (buffer_size > 15 &&
1492 Read16(buffer + 11) <= 2048 &&
1493 Read16(buffer + 13) <= 2048) {
1494 return CONTAINER_DXA;
1498 case TAG(
'D',
'T',
'S',
'H'):
1499 if (Read32(buffer + 4) == TAG(
'D',
'H',
'D',
'R'))
1500 return CONTAINER_DTSHD;
1510 if (Read32(buffer + 4) != 0 && Read32(buffer + 8) != 0)
1511 return CONTAINER_IRCAM;
1514 case TAG(
'f',
'L',
'a',
'C'):
1515 return CONTAINER_FLAC;
1517 case TAG(
'F',
'L',
'V',0):
1518 case TAG(
'F',
'L',
'V',1):
1519 case TAG(
'F',
'L',
'V',2):
1520 case TAG(
'F',
'L',
'V',3):
1521 case TAG(
'F',
'L',
'V',4):
1522 if (buffer[5] == 0 && Read32(buffer + 5) > 8)
1523 return CONTAINER_FLV;
1526 case TAG(
'F',
'O',
'R',
'M'):
1527 switch (Read32(buffer + 8)) {
1528 case TAG(
'A',
'I',
'F',
'F'):
1529 case TAG(
'A',
'I',
'F',
'C'):
1530 return CONTAINER_AIFF;
1534 case TAG(
'M',
'A',
'C',
' '):
1535 return CONTAINER_APE;
1537 case TAG(
'O',
'N',
'2',
' '):
1538 if (Read32(buffer + 8) == TAG(
'O',
'N',
'2',
'f'))
1539 return CONTAINER_AVI;
1542 case TAG(
'O',
'g',
'g',
'S'):
1544 return CONTAINER_OGG;
1547 case TAG(
'R',
'F',
'6',
'4'):
1548 if (buffer_size > 16 && Read32(buffer + 12) == TAG(
'd',
's',
'6',
'4'))
1549 return CONTAINER_WAV;
1552 case TAG(
'R',
'I',
'F',
'F'):
1553 switch (Read32(buffer + 8)) {
1554 case TAG(
'A',
'V',
'I',
' '):
1555 case TAG(
'A',
'V',
'I',
'X'):
1556 case TAG(
'A',
'V',
'I',
'\x19'):
1557 case TAG(
'A',
'M',
'V',
' '):
1558 return CONTAINER_AVI;
1559 case TAG(
'W',
'A',
'V',
'E'):
1560 return CONTAINER_WAV;
1564 case TAG(
'[',
'S',
'c',
'r'):
1565 if (StartsWith(buffer, buffer_size, kAssSignature))
1566 return CONTAINER_ASS;
1569 case TAG(
'\xef',
'\xbb',
'\xbf',
'['):
1570 if (StartsWith(buffer, buffer_size, kAssBomSignature))
1571 return CONTAINER_ASS;
1578 if (CheckDts(buffer, buffer_size))
1579 return CONTAINER_DTS;
1583 if (StartsWith(buffer,
1586 sizeof(kWtvSignature))) {
1587 return CONTAINER_WTV;
1591 return CONTAINER_MPEG2PS;
1596 uint32_t first3 = first4 & 0xffffff00;
1598 case TAG(
'C',
'W',
'S',0):
1599 case TAG(
'F',
'W',
'S',0):
1600 return CONTAINER_SWF;
1602 case TAG(
'I',
'D',
'3',0):
1603 if (CheckMp3(buffer, buffer_size, true))
1604 return CONTAINER_MP3;
1609 uint32_t first2 = Read16(buffer);
1612 if (CheckAc3(buffer, buffer_size))
1613 return CONTAINER_AC3;
1614 if (CheckEac3(buffer, buffer_size))
1615 return CONTAINER_EAC3;
1622 if (CheckAac(buffer, buffer_size))
1623 return CONTAINER_AAC;
1628 if (CheckMp3(buffer, buffer_size,
false))
1629 return CONTAINER_MP3;
1631 return CONTAINER_UNKNOWN;
1635 const char kWebVtt[] =
"WEBVTT";
1637 bool CheckWebVtt(
const uint8_t* buffer,
int buffer_size) {
1639 StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1641 return StartsWith(buffer + offset, buffer_size - offset,
1642 reinterpret_cast<const uint8_t*>(kWebVtt),
1643 arraysize(kWebVtt) - 1);
1646 bool CheckTtml(
const uint8_t* buffer,
int buffer_size) {
1648 if (!StartsWith(buffer, buffer_size,
"<?xml"))
1654 xml::scoped_xml_ptr<xmlDoc> doc(
1655 xmlParseMemory(reinterpret_cast<const char*>(buffer), buffer_size));
1659 xmlNodePtr root_node = xmlDocGetRootElement(doc.get());
1660 std::string root_node_name(reinterpret_cast<const char*>(root_node->name));
1662 return root_node_name ==
"tt";
1668 MediaContainerName DetermineContainer(
const uint8_t* buffer,
int buffer_size) {
1672 if (CheckMov(buffer, buffer_size))
1673 return CONTAINER_MOV;
1677 MediaContainerName result = LookupContainerByFirst4(buffer, buffer_size);
1678 if (result != CONTAINER_UNKNOWN)
1682 if (CheckWebVtt(buffer, buffer_size))
1683 return CONTAINER_WEBVTT;
1686 if (CheckMpeg2ProgramStream(buffer, buffer_size))
1687 return CONTAINER_MPEG2PS;
1688 if (CheckMpeg2TransportStream(buffer, buffer_size))
1689 return CONTAINER_MPEG2TS;
1690 if (CheckMJpeg(buffer, buffer_size))
1691 return CONTAINER_MJPEG;
1692 if (CheckDV(buffer, buffer_size))
1693 return CONTAINER_DV;
1694 if (CheckH261(buffer, buffer_size))
1695 return CONTAINER_H261;
1696 if (CheckH263(buffer, buffer_size))
1697 return CONTAINER_H263;
1698 if (CheckH264(buffer, buffer_size))
1699 return CONTAINER_H264;
1700 if (CheckMpeg4BitStream(buffer, buffer_size))
1701 return CONTAINER_MPEG4BS;
1702 if (CheckVC1(buffer, buffer_size))
1703 return CONTAINER_VC1;
1704 if (CheckSrt(buffer, buffer_size))
1705 return CONTAINER_SRT;
1706 if (CheckGsm(buffer, buffer_size))
1707 return CONTAINER_GSM;
1712 if (AdvanceToStartCode(buffer, buffer_size, &offset, 4, 16, kAc3SyncWord)) {
1713 if (CheckAc3(buffer + offset, buffer_size - offset))
1714 return CONTAINER_AC3;
1715 if (CheckEac3(buffer + offset, buffer_size - offset))
1716 return CONTAINER_EAC3;
1721 if (CheckTtml(buffer, buffer_size))
1722 return CONTAINER_TTML;
1724 return CONTAINER_UNKNOWN;
1727 MediaContainerName DetermineContainerFromFormatName(
1728 const std::string& format_name) {
1729 if (base::EqualsCaseInsensitiveASCII(format_name,
"webm")) {
1730 return CONTAINER_WEBM;
1731 }
else if (base::EqualsCaseInsensitiveASCII(format_name,
"m4a") ||
1732 base::EqualsCaseInsensitiveASCII(format_name,
"m4v") ||
1733 base::EqualsCaseInsensitiveASCII(format_name,
"mp4") ||
1734 base::EqualsCaseInsensitiveASCII(format_name,
"mov")) {
1735 return CONTAINER_MOV;
1736 }
else if (base::EqualsCaseInsensitiveASCII(format_name,
"ts") ||
1737 base::EqualsCaseInsensitiveASCII(format_name,
"mpeg2ts")) {
1738 return CONTAINER_MPEG2TS;
1740 return CONTAINER_UNKNOWN;
1743 MediaContainerName DetermineContainerFromFileName(
1744 const std::string& file_name) {
1745 if (base::EndsWith(file_name,
".webm",
1746 base::CompareCase::INSENSITIVE_ASCII)) {
1747 return CONTAINER_WEBM;
1748 }
else if (base::EndsWith(file_name,
".mp4",
1749 base::CompareCase::INSENSITIVE_ASCII) ||
1750 base::EndsWith(file_name,
".m4a",
1751 base::CompareCase::INSENSITIVE_ASCII) ||
1752 base::EndsWith(file_name,
".m4v",
1753 base::CompareCase::INSENSITIVE_ASCII)) {
1754 return CONTAINER_MOV;
1755 }
else if (base::EndsWith(file_name,
".ts",
1756 base::CompareCase::INSENSITIVE_ASCII)) {
1757 return CONTAINER_MPEG2TS;
1758 }
else if (base::EndsWith(file_name,
".vtt",
1759 base::CompareCase::INSENSITIVE_ASCII)) {
1760 return CONTAINER_WEBVTT;
1761 }
else if (base::EndsWith(file_name,
".webvtt",
1762 base::CompareCase::INSENSITIVE_ASCII)) {
1763 return CONTAINER_WEBVTT;
1764 }
else if (base::EndsWith(file_name,
".ttml",
1765 base::CompareCase::INSENSITIVE_ASCII)) {
1766 return CONTAINER_TTML;
1767 }
else if (base::EndsWith(file_name,
".xml",
1768 base::CompareCase::INSENSITIVE_ASCII)) {
1770 return CONTAINER_TTML;
1772 return CONTAINER_UNKNOWN;