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/media/base/rcheck.h"
18 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
20 namespace edash_packager {
23 #define TAG(a, b, c, d) \
24 ((static_cast<uint32_t>(static_cast<uint8_t>(a)) << 24) | \
25 (static_cast<uint8_t>(b) << 16) | (static_cast<uint8_t>(c) << 8) | \
26 (static_cast<uint8_t>(d)))
28 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
31 static int Read16(
const uint8_t* p) {
32 return p[0] << 8 | p[1];
36 static uint32_t Read24(
const uint8_t* p) {
37 return p[0] << 16 | p[1] << 8 | p[2];
41 static uint32_t Read32(
const uint8_t* p) {
42 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
46 static uint32_t Read32LE(
const uint8_t* p) {
47 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
52 static bool StartsWith(
const uint8_t* buffer,
55 size_t prefix_size = strlen(prefix);
56 return (prefix_size <= buffer_size &&
57 memcmp(buffer, prefix, prefix_size) == 0);
62 static bool StartsWith(
const uint8_t* buffer,
64 const uint8_t* prefix,
66 return (prefix_size <= buffer_size &&
67 memcmp(buffer, prefix, prefix_size) == 0);
71 static uint64_t ReadBits(BitReader* reader,
int num_bits) {
72 DCHECK_GE(reader->bits_available(), num_bits);
73 DCHECK((num_bits > 0) && (num_bits <= 64));
75 reader->ReadBits(num_bits, &value);
79 const int kAc3FrameSizeTable[38][3] = {
80 { 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 },
81 { 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 },
82 { 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 },
83 { 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 },
84 { 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 },
85 { 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 },
86 { 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 },
87 { 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 },
88 { 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 },
89 { 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 },
90 { 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 }
94 static bool CheckAac(
const uint8_t* buffer,
int buffer_size) {
97 RCHECK(buffer_size > 6);
100 while (offset + 6 < buffer_size) {
101 BitReader reader(buffer + offset, 6);
104 RCHECK(ReadBits(&reader, 12) == 0xfff);
110 RCHECK(ReadBits(&reader, 2) == 0);
113 reader.SkipBits(1 + 2);
116 RCHECK(ReadBits(&reader, 4) != 15);
120 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1);
123 int size = ReadBits(&reader, 13);
130 const uint16_t kAc3SyncWord = 0x0b77;
133 static bool CheckAc3(
const uint8_t* buffer,
int buffer_size) {
139 RCHECK(buffer_size > 6);
142 while (offset + 6 < buffer_size) {
143 BitReader reader(buffer + offset, 6);
146 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
152 int sample_rate_code = ReadBits(&reader, 2);
153 RCHECK(sample_rate_code != 3);
156 int frame_size_code = ReadBits(&reader, 6);
157 RCHECK(frame_size_code < 38);
160 RCHECK(ReadBits(&reader, 5) < 10);
162 offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code];
168 static bool CheckEac3(
const uint8_t* buffer,
int buffer_size) {
174 RCHECK(buffer_size > 6);
177 while (offset + 6 < buffer_size) {
178 BitReader reader(buffer + offset, 6);
181 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
184 RCHECK(ReadBits(&reader, 2) != 3);
190 int frame_size = (ReadBits(&reader, 11) + 1) * 2;
191 RCHECK(frame_size >= 7);
194 reader.SkipBits(2 + 2 + 3 + 1);
197 int bit_stream_id = ReadBits(&reader, 5);
198 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16);
200 offset += frame_size;
206 static bool CheckBink(
const uint8_t* buffer,
int buffer_size) {
208 RCHECK(buffer_size >= 44);
211 RCHECK(Read32LE(buffer + 8) > 0);
214 int width = Read32LE(buffer + 20);
215 RCHECK(width > 0 && width <= 32767);
218 int height = Read32LE(buffer + 24);
219 RCHECK(height > 0 && height <= 32767);
222 RCHECK(Read32LE(buffer + 28) > 0);
225 RCHECK(Read32LE(buffer + 32) > 0);
228 return (Read32LE(buffer + 40) <= 256);
232 static bool CheckCaf(
const uint8_t* buffer,
int buffer_size) {
235 RCHECK(buffer_size >= 52);
236 BitReader reader(buffer, buffer_size);
239 RCHECK(ReadBits(&reader, 32) == TAG(
'c',
'a',
'f',
'f'));
242 RCHECK(ReadBits(&reader, 16) == 1);
248 RCHECK(ReadBits(&reader, 32) == TAG(
'd',
'e',
's',
'c'));
249 RCHECK(ReadBits(&reader, 64) == 32);
252 RCHECK(ReadBits(&reader, 64) != 0);
255 RCHECK(ReadBits(&reader, 32) != 0);
258 reader.SkipBits(32 + 32);
261 RCHECK(ReadBits(&reader, 32) != 0);
265 static bool kSamplingFrequencyValid[16] = {
false,
true,
true,
true,
false,
266 false,
true,
true,
true,
false,
267 false,
true,
true,
true,
false,
269 static bool kExtAudioIdValid[8] = {
true,
false,
true,
false,
false,
false,
273 static bool CheckDts(
const uint8_t* buffer,
int buffer_size) {
276 RCHECK(buffer_size > 11);
279 while (offset + 11 < buffer_size) {
280 BitReader reader(buffer + offset, 11);
283 RCHECK(ReadBits(&reader, 32) == 0x7ffe8001);
286 reader.SkipBits(1 + 5);
289 RCHECK(ReadBits(&reader, 1) == 0);
292 RCHECK(ReadBits(&reader, 7) >= 5);
295 int frame_size = ReadBits(&reader, 14);
296 RCHECK(frame_size >= 95);
302 RCHECK(kSamplingFrequencyValid[ReadBits(&reader, 4)]);
305 RCHECK(ReadBits(&reader, 5) <= 25);
308 RCHECK(ReadBits(&reader, 1) == 0);
311 reader.SkipBits(1 + 1 + 1 + 1);
314 RCHECK(kExtAudioIdValid[ReadBits(&reader, 3)]);
317 reader.SkipBits(1 + 1);
320 RCHECK(ReadBits(&reader, 2) != 3);
322 offset += frame_size + 1;
328 static bool CheckDV(
const uint8_t* buffer,
int buffer_size) {
331 RCHECK(buffer_size > 11);
334 int current_sequence_number = -1;
335 int last_block_number[6];
336 while (offset + 11 < buffer_size) {
337 BitReader reader(buffer + offset, 11);
340 int section = ReadBits(&reader, 3);
344 RCHECK(ReadBits(&reader, 1) == 1);
349 int sequence_number = ReadBits(&reader, 4);
355 RCHECK(ReadBits(&reader, 3) == 7);
357 int block_number = ReadBits(&reader, 8);
362 RCHECK(ReadBits(&reader, 1) == 0);
363 RCHECK(ReadBits(&reader, 11) == 0x7ff);
365 RCHECK(ReadBits(&reader, 4) == 0xf);
367 RCHECK(ReadBits(&reader, 4) == 0xf);
369 RCHECK(ReadBits(&reader, 4) == 0xf);
371 RCHECK(ReadBits(&reader, 24) == 0xffffff);
372 current_sequence_number = sequence_number;
373 for (
size_t i = 0; i < arraysize(last_block_number); ++i)
374 last_block_number[i] = -1;
377 RCHECK(sequence_number == current_sequence_number);
379 RCHECK(block_number > last_block_number[section]);
380 last_block_number[section] = block_number;
391 static bool CheckGsm(
const uint8_t* buffer,
int buffer_size) {
396 RCHECK(buffer_size >= 1024);
399 while (offset < buffer_size) {
401 RCHECK((buffer[offset] & 0xf0) == 0xd0);
412 static bool AdvanceToStartCode(
const uint8_t* buffer,
417 uint32_t start_code) {
418 DCHECK_GE(bytes_needed, 3);
419 DCHECK_LE(num_bits, 24);
422 uint32_t bits_to_shift = 24 - num_bits;
423 uint32_t mask = (1 << num_bits) - 1;
424 while (*offset + bytes_needed < buffer_size) {
425 uint32_t next = Read24(buffer + *offset);
426 if (((next >> bits_to_shift) & mask) == start_code)
434 static bool CheckH261(
const uint8_t* buffer,
int buffer_size) {
437 RCHECK(buffer_size > 16);
440 bool seen_start_code =
false;
443 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 20, 0x10)) {
446 return seen_start_code;
451 BitReader reader(buffer + offset, buffer_size - offset);
452 RCHECK(ReadBits(&reader, 20) == 0x10);
455 reader.SkipBits(5 + 6);
459 int extra = ReadBits(&reader, 1);
461 if (!reader.SkipBits(8))
462 return seen_start_code;
463 if (!reader.ReadBits(1, &extra))
464 return seen_start_code;
471 if (!reader.ReadBits(16, &next))
472 return seen_start_code;
476 seen_start_code =
true;
482 static bool CheckH263(
const uint8_t* buffer,
int buffer_size) {
486 RCHECK(buffer_size > 16);
489 bool seen_start_code =
false;
492 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 9, 22, 0x20)) {
495 return seen_start_code;
500 BitReader reader(buffer + offset, 9);
501 RCHECK(ReadBits(&reader, 22) == 0x20);
507 RCHECK(ReadBits(&reader, 2) == 2);
511 reader.SkipBits(1 + 1 + 1);
514 int format = ReadBits(&reader, 3);
515 RCHECK(format != 0 && format != 6);
519 int ufep = ReadBits(&reader, 3);
522 format = ReadBits(&reader, 3);
523 RCHECK(format != 0 && format != 7);
526 RCHECK(ReadBits(&reader, 4) == 8);
532 int picture_type_code = ReadBits(&reader, 3);
533 RCHECK(picture_type_code != 6 && picture_type_code != 7);
537 reader.SkipBits(1 + 1 + 1);
540 RCHECK(ReadBits(&reader, 3) == 1);
544 seen_start_code =
true;
550 static bool CheckH264(
const uint8_t* buffer,
int buffer_size) {
554 RCHECK(buffer_size > 4);
557 int parameter_count = 0;
560 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 24, 1)) {
563 return parameter_count > 0;
568 BitReader reader(buffer + offset, 4);
569 RCHECK(ReadBits(&reader, 24) == 1);
572 RCHECK(ReadBits(&reader, 1) == 0);
575 int nal_ref_idc = ReadBits(&reader, 2);
576 int nal_unit_type = ReadBits(&reader, 5);
578 switch (nal_unit_type) {
580 RCHECK(nal_ref_idc != 0);
587 RCHECK(nal_ref_idc == 0);
600 static const char kHlsSignature[] =
"#EXTM3U";
601 static const char kHls1[] =
"#EXT-X-STREAM-INF:";
602 static const char kHls2[] =
"#EXT-X-TARGETDURATION:";
603 static const char kHls3[] =
"#EXT-X-MEDIA-SEQUENCE:";
606 static bool CheckHls(
const uint8_t* buffer,
int buffer_size) {
611 if (StartsWith(buffer, buffer_size, kHlsSignature)) {
616 int offset = strlen(kHlsSignature);
617 while (offset < buffer_size) {
618 if (buffer[offset] ==
'#') {
619 if (StartsWith(buffer + offset, buffer_size - offset, kHls1) ||
620 StartsWith(buffer + offset, buffer_size - offset, kHls2) ||
621 StartsWith(buffer + offset, buffer_size - offset, kHls3)) {
632 static bool CheckMJpeg(
const uint8_t* buffer,
int buffer_size) {
635 RCHECK(buffer_size >= 16);
638 int last_restart = -1;
640 while (offset + 5 < buffer_size) {
642 RCHECK(buffer[offset] == 0xff);
643 uint8_t code = buffer[offset + 1];
644 RCHECK(code >= 0xc0 || code == 1);
657 if (code == 0xd8 || code == 1) {
660 }
else if (code >= 0xd0 && code <= 0xd7) {
662 int restart = code & 0x07;
663 if (last_restart >= 0)
664 RCHECK(restart == (last_restart + 1) % 8);
665 last_restart = restart;
669 int length = Read16(buffer + offset + 2) + 2;
675 int number_components = buffer[offset + 4];
676 RCHECK(length == 8 + 2 * number_components);
680 while (offset + 2 < buffer_size) {
681 if (buffer[offset] == 0xff && buffer[offset + 1] != 0)
692 return (num_codes > 1);
695 enum Mpeg2StartCodes {
696 PROGRAM_END_CODE = 0xb9,
697 PACK_START_CODE = 0xba
701 static bool CheckMpeg2ProgramStream(
const uint8_t* buffer,
int buffer_size) {
703 RCHECK(buffer_size > 14);
706 while (offset + 14 < buffer_size) {
707 BitReader reader(buffer + offset, 14);
710 RCHECK(ReadBits(&reader, 24) == 1);
711 RCHECK(ReadBits(&reader, 8) == PACK_START_CODE);
714 int mpeg_version = ReadBits(&reader, 2);
715 if (mpeg_version == 0) {
718 RCHECK(ReadBits(&reader, 2) == 2);
720 RCHECK(mpeg_version == 1);
727 RCHECK(ReadBits(&reader, 1) == 1);
733 RCHECK(ReadBits(&reader, 1) == 1);
739 RCHECK(ReadBits(&reader, 1) == 1);
741 if (mpeg_version == 0) {
743 RCHECK(ReadBits(&reader, 1) == 1);
749 RCHECK(ReadBits(&reader, 1) == 1);
759 RCHECK(ReadBits(&reader, 2) == 3);
765 int pack_stuffing_length = ReadBits(&reader, 3);
766 offset += 14 + pack_stuffing_length;
770 while (offset + 6 < buffer_size && Read24(buffer + offset) == 1) {
772 int stream_id = buffer[offset + 3];
775 if (mpeg_version == 0)
776 RCHECK(stream_id != 0xbc && stream_id < 0xf0);
778 RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe);
781 if (stream_id == PACK_START_CODE)
783 if (stream_id == PROGRAM_END_CODE)
786 int pes_length = Read16(buffer + offset + 4);
787 RCHECK(pes_length > 0);
788 offset = offset + 6 + pes_length;
796 const uint8_t kMpeg2SyncWord = 0x47;
799 static bool CheckMpeg2TransportStream(
const uint8_t* buffer,
int buffer_size) {
805 RCHECK(buffer_size >= 250);
808 int packet_length = -1;
809 while (buffer[offset] != kMpeg2SyncWord && offset < 20) {
814 while (offset + 6 < buffer_size) {
815 BitReader reader(buffer + offset, 6);
818 RCHECK(ReadBits(&reader, 8) == kMpeg2SyncWord);
822 reader.SkipBits(1 + 1 + 1);
825 int pid = ReadBits(&reader, 13);
826 RCHECK(pid < 3 || pid > 15);
832 int adaptation_field_control = ReadBits(&reader, 2);
833 RCHECK(adaptation_field_control != 0);
836 if (adaptation_field_control >= 2) {
841 int adaptation_field_length = ReadBits(&reader, 8);
842 if (adaptation_field_control == 2)
843 RCHECK(adaptation_field_length == 183);
845 RCHECK(adaptation_field_length <= 182);
849 if (packet_length < 0) {
850 if (buffer[offset + 188] == kMpeg2SyncWord)
852 else if (buffer[offset + 192] == kMpeg2SyncWord)
854 else if (buffer[offset + 204] == kMpeg2SyncWord)
859 offset += packet_length;
864 enum Mpeg4StartCodes {
865 VISUAL_OBJECT_SEQUENCE_START_CODE = 0xb0,
866 VISUAL_OBJECT_SEQUENCE_END_CODE = 0xb1,
867 VISUAL_OBJECT_START_CODE = 0xb5,
868 VOP_START_CODE = 0xb6
872 static bool CheckMpeg4BitStream(
const uint8_t* buffer,
int buffer_size) {
876 RCHECK(buffer_size > 4);
879 int sequence_start_count = 0;
880 int sequence_end_count = 0;
881 int visual_object_count = 0;
885 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 6, 24, 1)) {
888 return (sequence_start_count > 0 && visual_object_count > 0);
893 BitReader reader(buffer + offset, 6);
894 RCHECK(ReadBits(&reader, 24) == 1);
896 int start_code = ReadBits(&reader, 8);
897 RCHECK(start_code < 0x30 || start_code > 0xaf);
898 RCHECK(start_code < 0xb7 || start_code > 0xb9);
900 switch (start_code) {
901 case VISUAL_OBJECT_SEQUENCE_START_CODE: {
902 ++sequence_start_count;
904 int profile = ReadBits(&reader, 8);
906 RCHECK(profile < 0x04 || profile > 0x10);
907 RCHECK(profile < 0x13 || profile > 0x20);
908 RCHECK(profile < 0x23 || profile > 0x31);
909 RCHECK(profile < 0x35 || profile > 0x41);
910 RCHECK(profile < 0x43 || profile > 0x60);
911 RCHECK(profile < 0x65 || profile > 0x70);
912 RCHECK(profile < 0x73 || profile > 0x80);
913 RCHECK(profile < 0x83 || profile > 0x90);
914 RCHECK(profile < 0x95 || profile > 0xa0);
915 RCHECK(profile < 0xa4 || profile > 0xb0);
916 RCHECK(profile < 0xb5 || profile > 0xc0);
917 RCHECK(profile < 0xc3 || profile > 0xd0);
918 RCHECK(profile < 0xe4);
922 case VISUAL_OBJECT_SEQUENCE_END_CODE:
923 RCHECK(++sequence_end_count == sequence_start_count);
926 case VISUAL_OBJECT_START_CODE: {
927 ++visual_object_count;
928 if (ReadBits(&reader, 1) == 1) {
929 int visual_object_verid = ReadBits(&reader, 4);
930 RCHECK(visual_object_verid > 0 && visual_object_verid < 3);
931 RCHECK(ReadBits(&reader, 3) != 0);
933 int visual_object_type = ReadBits(&reader, 4);
934 RCHECK(visual_object_type > 0 && visual_object_type < 6);
939 RCHECK(++vop_count <= visual_object_count);
948 static bool CheckMov(
const uint8_t* buffer,
int buffer_size) {
951 RCHECK(buffer_size > 8);
954 while (offset + 8 < buffer_size) {
955 int atomsize = Read32(buffer + offset);
956 uint32_t atomtype = Read32(buffer + offset + 4);
959 case TAG(
'f',
't',
'y',
'p'):
960 case TAG(
'p',
'd',
'i',
'n'):
961 case TAG(
'm',
'o',
'o',
'v'):
962 case TAG(
'm',
'o',
'o',
'f'):
963 case TAG(
'm',
'f',
'r',
'a'):
964 case TAG(
'm',
'd',
'a',
't'):
965 case TAG(
'f',
'r',
'e',
'e'):
966 case TAG(
's',
'k',
'i',
'p'):
967 case TAG(
'm',
'e',
't',
'a'):
968 case TAG(
'm',
'e',
'c',
'o'):
969 case TAG(
's',
't',
'y',
'p'):
970 case TAG(
's',
'i',
'd',
'x'):
971 case TAG(
's',
's',
'i',
'x'):
972 case TAG(
'p',
'r',
'f',
't'):
973 case TAG(
'b',
'l',
'o',
'c'):
980 if (offset + 16 > buffer_size)
982 if (Read32(buffer + offset + 8) != 0)
984 atomsize = Read32(buffer + offset + 12);
1006 static int kSampleRateTable[4][4] = { { 11025, 12000, 8000, 0 },
1008 { 22050, 24000, 16000, 0 },
1009 { 44100, 48000, 32000, 0 }
1012 static int kBitRateTableV1L1[16] = { 0, 32, 64, 96, 128, 160, 192, 224, 256,
1013 288, 320, 352, 384, 416, 448, 0 };
1014 static int kBitRateTableV1L2[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160,
1015 192, 224, 256, 320, 384, 0 };
1016 static int kBitRateTableV1L3[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128,
1017 160, 192, 224, 256, 320, 0 };
1018 static int kBitRateTableV2L1[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1019 160, 176, 192, 224, 256, 0 };
1020 static int kBitRateTableV2L23[16] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,
1021 112, 128, 144, 160, 0 };
1023 static bool ValidMpegAudioFrameHeader(
const uint8_t* header,
1027 DCHECK_GE(header_size, 4);
1029 BitReader reader(header, 4);
1032 RCHECK(ReadBits(&reader, 11) == 0x7ff);
1035 int version = ReadBits(&reader, 2);
1036 RCHECK(version != 1);
1039 int layer = ReadBits(&reader, 2);
1046 int bitrate_index = ReadBits(&reader, 4);
1047 RCHECK(bitrate_index != 0xf);
1050 int sampling_index = ReadBits(&reader, 2);
1051 RCHECK(sampling_index != 3);
1054 int padding = ReadBits(&reader, 1);
1060 int sampling_rate = kSampleRateTable[version][sampling_index];
1062 if (version == VERSION_1) {
1063 if (layer == LAYER_1)
1064 bitrate = kBitRateTableV1L1[bitrate_index];
1065 else if (layer == LAYER_2)
1066 bitrate = kBitRateTableV1L2[bitrate_index];
1068 bitrate = kBitRateTableV1L3[bitrate_index];
1070 if (layer == LAYER_1)
1071 bitrate = kBitRateTableV2L1[bitrate_index];
1073 bitrate = kBitRateTableV2L23[bitrate_index];
1075 if (layer == LAYER_1)
1076 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4;
1078 *framesize = (144000 * bitrate) / sampling_rate + padding;
1079 return (bitrate > 0 && sampling_rate > 0);
1083 static int GetMp3HeaderSize(
const uint8_t* buffer,
int buffer_size) {
1084 DCHECK_GE(buffer_size, 9);
1085 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) +
1086 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10;
1087 if (buffer[5] & 0x10)
1093 static bool CheckMp3(
const uint8_t* buffer,
int buffer_size,
bool seenHeader) {
1094 RCHECK(buffer_size >= 10);
1100 offset = GetMp3HeaderSize(buffer, buffer_size);
1103 while (offset < buffer_size && buffer[offset] == 0)
1107 while (offset + 3 < buffer_size) {
1108 RCHECK(ValidMpegAudioFrameHeader(
1109 buffer + offset, buffer_size - offset, &framesize));
1114 offset += framesize;
1124 static bool VerifyNumber(
const uint8_t* buffer,
1128 RCHECK(*offset < buffer_size);
1131 while (isspace(buffer[*offset])) {
1133 RCHECK(*offset < buffer_size);
1138 while (--max_digits >= 0 && isdigit(buffer[*offset])) {
1141 if (*offset >= buffer_size)
1146 return (numSeen > 0);
1152 static inline bool VerifyCharacters(
const uint8_t* buffer,
1157 RCHECK(*offset < buffer_size);
1158 char c =
static_cast<char>(buffer[(*offset)++]);
1159 return (c == c1 || (c == c2 && c2 != 0));
1163 static bool CheckSrt(
const uint8_t* buffer,
int buffer_size) {
1165 RCHECK(buffer_size > 20);
1168 int offset = StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1169 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1170 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r'));
1173 while (VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r')) {}
1179 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1180 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1181 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1182 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1183 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1184 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
',',
'.'));
1185 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3));
1186 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
' ', 0));
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(VerifyNumber(buffer, buffer_size, &offset, 100));
1192 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1193 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1194 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1195 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1196 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
',',
'.'));
1197 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3));
1202 static int GetElementId(BitReader* reader) {
1206 if (reader->bits_available() >= 8) {
1207 int num_bits_to_read = 0;
1208 static int prefix[] = { 0x80, 0x4000, 0x200000, 0x10000000 };
1209 for (
int i = 0; i < 4; ++i) {
1210 num_bits_to_read += 7;
1211 if (ReadBits(reader, 1) == 1) {
1212 if (reader->bits_available() < num_bits_to_read)
1215 return ReadBits(reader, num_bits_to_read) | prefix[i];
1224 static uint64_t GetVint(BitReader* reader) {
1228 if (reader->bits_available() >= 8) {
1229 int num_bits_to_read = 0;
1230 for (
int i = 0; i < 8; ++i) {
1231 num_bits_to_read += 7;
1232 if (ReadBits(reader, 1) == 1) {
1233 if (reader->bits_available() < num_bits_to_read)
1235 return ReadBits(reader, num_bits_to_read);
1242 return (reader->bits_available() / 8) + 2;
1246 static bool CheckWebm(
const uint8_t* buffer,
int buffer_size) {
1248 RCHECK(buffer_size > 12);
1250 BitReader reader(buffer, buffer_size);
1253 RCHECK(GetElementId(&reader) == 0x1a45dfa3);
1256 int header_size = GetVint(&reader);
1257 RCHECK(reader.bits_available() / 8 >= header_size);
1260 while (reader.bits_available() > 0) {
1261 int tag = GetElementId(&reader);
1262 int tagsize = GetVint(&reader);
1272 RCHECK(reader.SkipBits(tagsize * 8));
1277 switch (ReadBits(&reader, 32)) {
1278 case TAG(
'w',
'e',
'b',
'm') :
1280 case TAG(
'm',
'a',
't',
'r') :
1281 return (ReadBits(&reader, 32) == TAG(
'o',
's',
'k',
'a'));
1292 enum VC1StartCodes {
1293 VC1_FRAME_START_CODE = 0x0d,
1294 VC1_ENTRY_POINT_START_CODE = 0x0e,
1295 VC1_SEQUENCE_START_CODE = 0x0f
1299 static bool CheckVC1(
const uint8_t* buffer,
int buffer_size) {
1306 RCHECK(buffer_size >= 24);
1309 if (buffer[0] == 0xc5 &&
1310 Read32(buffer + 4) == 0x04 &&
1311 Read32(buffer + 20) == 0x0c) {
1313 BitReader reader(buffer + 8, 12);
1315 int profile = ReadBits(&reader, 4);
1316 if (profile == 0 || profile == 4) {
1318 reader.SkipBits(3 + 5 + 1);
1321 RCHECK(ReadBits(&reader, 1) == 0);
1327 RCHECK(ReadBits(&reader, 1) == 1);
1330 reader.SkipBits(1 + 1 + 2 + 1);
1333 RCHECK(ReadBits(&reader, 1) == 0);
1337 reader.SkipBits(1 + 1 + 1 + 3 + 2 + 1);
1340 RCHECK(ReadBits(&reader, 1) == 1);
1343 RCHECK(profile == 12);
1344 RCHECK(ReadBits(&reader, 28) == 0);
1348 RCHECK(ReadBits(&reader, 32) <= 8192);
1349 RCHECK(ReadBits(&reader, 32) <= 8192);
1355 int sequence_start_code = 0;
1356 int frame_start_code = 0;
1359 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 5, 24, 1)) {
1363 return (sequence_start_code > 0 && frame_start_code > 0);
1368 BitReader reader(buffer + offset, 5);
1369 RCHECK(ReadBits(&reader, 24) == 1);
1372 switch (ReadBits(&reader, 8)) {
1373 case VC1_SEQUENCE_START_CODE: {
1374 ++sequence_start_code;
1375 switch (ReadBits(&reader, 2)) {
1378 RCHECK(ReadBits(&reader, 2) == 0);
1383 RCHECK(ReadBits(&reader, 3) <= 4);
1384 RCHECK(ReadBits(&reader, 2) == 1);
1390 case VC1_ENTRY_POINT_START_CODE:
1393 RCHECK(sequence_start_code > 0);
1396 case VC1_FRAME_START_CODE:
1408 static const char kAmrSignature[] =
"#!AMR";
1409 static const uint8_t kAsfSignature[] = {0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
1410 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa,
1411 0x00, 0x62, 0xce, 0x6c};
1412 static const char kAssSignature[] =
"[Script Info]";
1413 static const char kAssBomSignature[] = UTF8_BYTE_ORDER_MARK
"[Script Info]";
1414 static const uint8_t kWtvSignature[] = {0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49,
1415 0xda, 0x11, 0xa6, 0x4e, 0x00, 0x07,
1416 0xe9, 0x5e, 0xad, 0x8d};
1421 static MediaContainerName LookupContainerByFirst4(
const uint8_t* buffer,
1424 if (buffer_size < 12)
1425 return CONTAINER_UNKNOWN;
1427 uint32_t first4 = Read32(buffer);
1430 if (CheckWebm(buffer, buffer_size))
1431 return CONTAINER_WEBM;
1435 if (StartsWith(buffer,
1438 sizeof(kAsfSignature))) {
1439 return CONTAINER_ASF;
1443 case TAG(
'#',
'!',
'A',
'M'):
1444 if (StartsWith(buffer, buffer_size, kAmrSignature))
1445 return CONTAINER_AMR;
1448 case TAG(
'#',
'E',
'X',
'T'):
1449 if (CheckHls(buffer, buffer_size))
1450 return CONTAINER_HLS;
1453 case TAG(
'.',
'R',
'M',
'F'):
1454 if (buffer[4] == 0 && buffer[5] == 0)
1455 return CONTAINER_RM;
1458 case TAG(
'.',
'r',
'a',
'\xfd'):
1459 return CONTAINER_RM;
1461 case TAG(
'B',
'I',
'K',
'b'):
1462 case TAG(
'B',
'I',
'K',
'd'):
1463 case TAG(
'B',
'I',
'K',
'f'):
1464 case TAG(
'B',
'I',
'K',
'g'):
1465 case TAG(
'B',
'I',
'K',
'h'):
1466 case TAG(
'B',
'I',
'K',
'i'):
1467 if (CheckBink(buffer, buffer_size))
1468 return CONTAINER_BINK;
1471 case TAG(
'c',
'a',
'f',
'f'):
1472 if (CheckCaf(buffer, buffer_size))
1473 return CONTAINER_CAF;
1476 case TAG(
'D',
'E',
'X',
'A'):
1477 if (buffer_size > 15 &&
1478 Read16(buffer + 11) <= 2048 &&
1479 Read16(buffer + 13) <= 2048) {
1480 return CONTAINER_DXA;
1484 case TAG(
'D',
'T',
'S',
'H'):
1485 if (Read32(buffer + 4) == TAG(
'D',
'H',
'D',
'R'))
1486 return CONTAINER_DTSHD;
1496 if (Read32(buffer + 4) != 0 && Read32(buffer + 8) != 0)
1497 return CONTAINER_IRCAM;
1500 case TAG(
'f',
'L',
'a',
'C'):
1501 return CONTAINER_FLAC;
1503 case TAG(
'F',
'L',
'V',0):
1504 case TAG(
'F',
'L',
'V',1):
1505 case TAG(
'F',
'L',
'V',2):
1506 case TAG(
'F',
'L',
'V',3):
1507 case TAG(
'F',
'L',
'V',4):
1508 if (buffer[5] == 0 && Read32(buffer + 5) > 8)
1509 return CONTAINER_FLV;
1512 case TAG(
'F',
'O',
'R',
'M'):
1513 switch (Read32(buffer + 8)) {
1514 case TAG(
'A',
'I',
'F',
'F'):
1515 case TAG(
'A',
'I',
'F',
'C'):
1516 return CONTAINER_AIFF;
1520 case TAG(
'M',
'A',
'C',
' '):
1521 return CONTAINER_APE;
1523 case TAG(
'O',
'N',
'2',
' '):
1524 if (Read32(buffer + 8) == TAG(
'O',
'N',
'2',
'f'))
1525 return CONTAINER_AVI;
1528 case TAG(
'O',
'g',
'g',
'S'):
1530 return CONTAINER_OGG;
1533 case TAG(
'R',
'F',
'6',
'4'):
1534 if (buffer_size > 16 && Read32(buffer + 12) == TAG(
'd',
's',
'6',
'4'))
1535 return CONTAINER_WAV;
1538 case TAG(
'R',
'I',
'F',
'F'):
1539 switch (Read32(buffer + 8)) {
1540 case TAG(
'A',
'V',
'I',
' '):
1541 case TAG(
'A',
'V',
'I',
'X'):
1542 case TAG(
'A',
'V',
'I',
'\x19'):
1543 case TAG(
'A',
'M',
'V',
' '):
1544 return CONTAINER_AVI;
1545 case TAG(
'W',
'A',
'V',
'E'):
1546 return CONTAINER_WAV;
1550 case TAG(
'[',
'S',
'c',
'r'):
1551 if (StartsWith(buffer, buffer_size, kAssSignature))
1552 return CONTAINER_ASS;
1555 case TAG(
'\xef',
'\xbb',
'\xbf',
'['):
1556 if (StartsWith(buffer, buffer_size, kAssBomSignature))
1557 return CONTAINER_ASS;
1564 if (CheckDts(buffer, buffer_size))
1565 return CONTAINER_DTS;
1569 if (StartsWith(buffer,
1572 sizeof(kWtvSignature))) {
1573 return CONTAINER_WTV;
1577 return CONTAINER_MPEG2PS;
1582 uint32_t first3 = first4 & 0xffffff00;
1584 case TAG(
'C',
'W',
'S',0):
1585 case TAG(
'F',
'W',
'S',0):
1586 return CONTAINER_SWF;
1588 case TAG(
'I',
'D',
'3',0):
1589 if (CheckMp3(buffer, buffer_size, true))
1590 return CONTAINER_MP3;
1595 uint32_t first2 = Read16(buffer);
1598 if (CheckAc3(buffer, buffer_size))
1599 return CONTAINER_AC3;
1600 if (CheckEac3(buffer, buffer_size))
1601 return CONTAINER_EAC3;
1608 if (CheckAac(buffer, buffer_size))
1609 return CONTAINER_AAC;
1614 if (CheckMp3(buffer, buffer_size,
false))
1615 return CONTAINER_MP3;
1617 return CONTAINER_UNKNOWN;
1621 const char kWebVtt[] =
"WEBVTT";
1623 bool CheckWebVtt(
const uint8_t* buffer,
int buffer_size) {
1625 StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1627 return StartsWith(buffer + offset, buffer_size - offset,
1628 reinterpret_cast<const uint8_t*>(kWebVtt),
1629 arraysize(kWebVtt) - 1);
1632 bool CheckTtml(
const uint8_t* buffer,
int buffer_size) {
1634 if (!StartsWith(buffer, buffer_size,
"<?xml"))
1640 xml::scoped_xml_ptr<xmlDoc> doc(
1641 xmlParseMemory(reinterpret_cast<const char*>(buffer), buffer_size));
1645 xmlNodePtr root_node = xmlDocGetRootElement(doc.get());
1646 std::string root_node_name(reinterpret_cast<const char*>(root_node->name));
1648 return root_node_name ==
"tt";
1654 MediaContainerName DetermineContainer(
const uint8_t* buffer,
int buffer_size) {
1658 if (CheckMov(buffer, buffer_size))
1659 return CONTAINER_MOV;
1663 MediaContainerName result = LookupContainerByFirst4(buffer, buffer_size);
1664 if (result != CONTAINER_UNKNOWN)
1668 if (CheckWebVtt(buffer, buffer_size))
1669 return CONTAINER_WEBVTT;
1672 if (CheckMpeg2ProgramStream(buffer, buffer_size))
1673 return CONTAINER_MPEG2PS;
1674 if (CheckMpeg2TransportStream(buffer, buffer_size))
1675 return CONTAINER_MPEG2TS;
1676 if (CheckMJpeg(buffer, buffer_size))
1677 return CONTAINER_MJPEG;
1678 if (CheckDV(buffer, buffer_size))
1679 return CONTAINER_DV;
1680 if (CheckH261(buffer, buffer_size))
1681 return CONTAINER_H261;
1682 if (CheckH263(buffer, buffer_size))
1683 return CONTAINER_H263;
1684 if (CheckH264(buffer, buffer_size))
1685 return CONTAINER_H264;
1686 if (CheckMpeg4BitStream(buffer, buffer_size))
1687 return CONTAINER_MPEG4BS;
1688 if (CheckVC1(buffer, buffer_size))
1689 return CONTAINER_VC1;
1690 if (CheckSrt(buffer, buffer_size))
1691 return CONTAINER_SRT;
1692 if (CheckGsm(buffer, buffer_size))
1693 return CONTAINER_GSM;
1698 if (AdvanceToStartCode(buffer, buffer_size, &offset, 4, 16, kAc3SyncWord)) {
1699 if (CheckAc3(buffer + offset, buffer_size - offset))
1700 return CONTAINER_AC3;
1701 if (CheckEac3(buffer + offset, buffer_size - offset))
1702 return CONTAINER_EAC3;
1707 if (CheckTtml(buffer, buffer_size))
1708 return CONTAINER_TTML;
1710 return CONTAINER_UNKNOWN;
1713 MediaContainerName DetermineContainerFromFormatName(
1714 const std::string& format_name) {
1715 if (base::EqualsCaseInsensitiveASCII(format_name,
"webm")) {
1716 return CONTAINER_WEBM;
1717 }
else if (base::EqualsCaseInsensitiveASCII(format_name,
"m4a") ||
1718 base::EqualsCaseInsensitiveASCII(format_name,
"m4v") ||
1719 base::EqualsCaseInsensitiveASCII(format_name,
"mp4") ||
1720 base::EqualsCaseInsensitiveASCII(format_name,
"mov")) {
1721 return CONTAINER_MOV;
1722 }
else if (base::EqualsCaseInsensitiveASCII(format_name,
"ts") ||
1723 base::EqualsCaseInsensitiveASCII(format_name,
"mpeg2ts")) {
1724 return CONTAINER_MPEG2TS;
1726 return CONTAINER_UNKNOWN;
1729 MediaContainerName DetermineContainerFromFileName(
1730 const std::string& file_name) {
1731 if (base::EndsWith(file_name,
".webm",
1732 base::CompareCase::INSENSITIVE_ASCII)) {
1733 return CONTAINER_WEBM;
1734 }
else if (base::EndsWith(file_name,
".mp4",
1735 base::CompareCase::INSENSITIVE_ASCII) ||
1736 base::EndsWith(file_name,
".m4a",
1737 base::CompareCase::INSENSITIVE_ASCII) ||
1738 base::EndsWith(file_name,
".m4v",
1739 base::CompareCase::INSENSITIVE_ASCII)) {
1740 return CONTAINER_MOV;
1741 }
else if (base::EndsWith(file_name,
".ts",
1742 base::CompareCase::INSENSITIVE_ASCII)) {
1743 return CONTAINER_MPEG2TS;
1745 return CONTAINER_UNKNOWN;