5 #include <packager/media/base/container_names.h>
13 #include <absl/log/check.h>
14 #include <absl/log/log.h>
15 #include <libxml/parser.h>
16 #include <libxml/tree.h>
18 #include <packager/media/base/bit_reader.h>
19 #include <packager/mpd/base/xml/scoped_xml_ptr.h>
24 #define TAG(a, b, c, d) \
25 ((static_cast<uint32_t>(static_cast<uint8_t>(a)) << 24) | \
26 (static_cast<uint8_t>(b) << 16) | (static_cast<uint8_t>(c) << 8) | \
27 (static_cast<uint8_t>(d)))
35 #define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
38 static int Read16(
const uint8_t* p) {
39 return p[0] << 8 | p[1];
43 static uint32_t Read24(
const uint8_t* p) {
44 return p[0] << 16 | p[1] << 8 | p[2];
48 static uint32_t Read32(
const uint8_t* p) {
49 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
53 static uint32_t Read32LE(
const uint8_t* p) {
54 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
59 static bool StartsWith(
const uint8_t* buffer,
62 size_t prefix_size = strlen(prefix);
63 return (prefix_size <= buffer_size &&
64 memcmp(buffer, prefix, prefix_size) == 0);
69 static bool StartsWith(
const uint8_t* buffer,
71 const uint8_t* prefix,
73 return (prefix_size <= buffer_size &&
74 memcmp(buffer, prefix, prefix_size) == 0);
78 static uint64_t ReadBits(BitReader* reader,
int num_bits) {
79 DCHECK_GE(
static_cast<int>(reader->bits_available()), num_bits);
80 DCHECK((num_bits > 0) && (num_bits <= 64));
82 reader->ReadBits(num_bits, &value);
86 const int kAc3FrameSizeTable[38][3] = {
87 { 128, 138, 192 }, { 128, 140, 192 }, { 160, 174, 240 }, { 160, 176, 240 },
88 { 192, 208, 288 }, { 192, 210, 288 }, { 224, 242, 336 }, { 224, 244, 336 },
89 { 256, 278, 384 }, { 256, 280, 384 }, { 320, 348, 480 }, { 320, 350, 480 },
90 { 384, 416, 576 }, { 384, 418, 576 }, { 448, 486, 672 }, { 448, 488, 672 },
91 { 512, 556, 768 }, { 512, 558, 768 }, { 640, 696, 960 }, { 640, 698, 960 },
92 { 768, 834, 1152 }, { 768, 836, 1152 }, { 896, 974, 1344 },
93 { 896, 976, 1344 }, { 1024, 1114, 1536 }, { 1024, 1116, 1536 },
94 { 1280, 1392, 1920 }, { 1280, 1394, 1920 }, { 1536, 1670, 2304 },
95 { 1536, 1672, 2304 }, { 1792, 1950, 2688 }, { 1792, 1952, 2688 },
96 { 2048, 2228, 3072 }, { 2048, 2230, 3072 }, { 2304, 2506, 3456 },
97 { 2304, 2508, 3456 }, { 2560, 2768, 3840 }, { 2560, 2770, 3840 }
101 static bool CheckAac(
const uint8_t* buffer,
int buffer_size) {
104 RCHECK(buffer_size > 6);
107 while (offset + 6 < buffer_size) {
108 BitReader reader(buffer + offset, 6);
111 RCHECK(ReadBits(&reader, 12) == 0xfff);
117 RCHECK(ReadBits(&reader, 2) == 0);
120 reader.SkipBits(1 + 2);
123 RCHECK(ReadBits(&reader, 4) != 15);
127 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1);
130 int size = ReadBits(&reader, 13);
137 const uint16_t kAc3SyncWord = 0x0b77;
140 static bool CheckAc3(
const uint8_t* buffer,
int buffer_size) {
146 RCHECK(buffer_size > 6);
149 while (offset + 6 < buffer_size) {
150 BitReader reader(buffer + offset, 6);
153 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
159 int sample_rate_code = ReadBits(&reader, 2);
160 RCHECK(sample_rate_code != 3);
163 int frame_size_code = ReadBits(&reader, 6);
164 RCHECK(frame_size_code < 38);
167 RCHECK(ReadBits(&reader, 5) < 10);
169 offset += kAc3FrameSizeTable[frame_size_code][sample_rate_code];
175 static bool CheckEac3(
const uint8_t* buffer,
int buffer_size) {
181 RCHECK(buffer_size > 6);
184 while (offset + 6 < buffer_size) {
185 BitReader reader(buffer + offset, 6);
188 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
191 RCHECK(ReadBits(&reader, 2) != 3);
197 int frame_size = (ReadBits(&reader, 11) + 1) * 2;
198 RCHECK(frame_size >= 7);
201 reader.SkipBits(2 + 2 + 3 + 1);
204 int bit_stream_id = ReadBits(&reader, 5);
205 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16);
207 offset += frame_size;
213 static bool CheckBink(
const uint8_t* buffer,
int buffer_size) {
215 RCHECK(buffer_size >= 44);
218 RCHECK(Read32LE(buffer + 8) > 0);
221 int width = Read32LE(buffer + 20);
222 RCHECK(width > 0 && width <= 32767);
225 int height = Read32LE(buffer + 24);
226 RCHECK(height > 0 && height <= 32767);
229 RCHECK(Read32LE(buffer + 28) > 0);
232 RCHECK(Read32LE(buffer + 32) > 0);
235 return (Read32LE(buffer + 40) <= 256);
239 static bool CheckCaf(
const uint8_t* buffer,
int buffer_size) {
242 RCHECK(buffer_size >= 52);
243 BitReader reader(buffer, buffer_size);
246 RCHECK(ReadBits(&reader, 32) == TAG(
'c',
'a',
'f',
'f'));
249 RCHECK(ReadBits(&reader, 16) == 1);
255 RCHECK(ReadBits(&reader, 32) == TAG(
'd',
'e',
's',
'c'));
256 RCHECK(ReadBits(&reader, 64) == 32);
259 RCHECK(ReadBits(&reader, 64) != 0);
262 RCHECK(ReadBits(&reader, 32) != 0);
265 reader.SkipBits(32 + 32);
268 RCHECK(ReadBits(&reader, 32) != 0);
272 static bool kSamplingFrequencyValid[16] = {
false,
true,
true,
true,
false,
273 false,
true,
true,
true,
false,
274 false,
true,
true,
true,
false,
276 static bool kExtAudioIdValid[8] = {
true,
false,
true,
false,
false,
false,
280 static bool CheckDts(
const uint8_t* buffer,
int buffer_size) {
283 RCHECK(buffer_size > 11);
286 while (offset + 11 < buffer_size) {
287 BitReader reader(buffer + offset, 11);
290 RCHECK(ReadBits(&reader, 32) == 0x7ffe8001);
293 reader.SkipBits(1 + 5);
296 RCHECK(ReadBits(&reader, 1) == 0);
299 RCHECK(ReadBits(&reader, 7) >= 5);
302 int frame_size = ReadBits(&reader, 14);
303 RCHECK(frame_size >= 95);
309 RCHECK(kSamplingFrequencyValid[ReadBits(&reader, 4)]);
312 RCHECK(ReadBits(&reader, 5) <= 25);
315 RCHECK(ReadBits(&reader, 1) == 0);
318 reader.SkipBits(1 + 1 + 1 + 1);
321 RCHECK(kExtAudioIdValid[ReadBits(&reader, 3)]);
324 reader.SkipBits(1 + 1);
327 RCHECK(ReadBits(&reader, 2) != 3);
329 offset += frame_size + 1;
335 static bool CheckDV(
const uint8_t* buffer,
int buffer_size) {
338 RCHECK(buffer_size > 11);
341 int current_sequence_number = -1;
342 int last_block_number[6];
343 while (offset + 11 < buffer_size) {
344 BitReader reader(buffer + offset, 11);
347 int section = ReadBits(&reader, 3);
351 RCHECK(ReadBits(&reader, 1) == 1);
356 int sequence_number = ReadBits(&reader, 4);
362 RCHECK(ReadBits(&reader, 3) == 7);
364 int block_number = ReadBits(&reader, 8);
369 RCHECK(ReadBits(&reader, 1) == 0);
370 RCHECK(ReadBits(&reader, 11) == 0x7ff);
372 RCHECK(ReadBits(&reader, 4) == 0xf);
374 RCHECK(ReadBits(&reader, 4) == 0xf);
376 RCHECK(ReadBits(&reader, 4) == 0xf);
378 RCHECK(ReadBits(&reader, 24) == 0xffffff);
379 current_sequence_number = sequence_number;
380 for (
size_t i = 0; i < std::size(last_block_number); ++i)
381 last_block_number[i] = -1;
384 RCHECK(sequence_number == current_sequence_number);
386 RCHECK(block_number > last_block_number[section]);
387 last_block_number[section] = block_number;
398 static bool CheckGsm(
const uint8_t* buffer,
int buffer_size) {
403 RCHECK(buffer_size >= 1024);
406 while (offset < buffer_size) {
408 RCHECK((buffer[offset] & 0xf0) == 0xd0);
419 static bool AdvanceToStartCode(
const uint8_t* buffer,
424 uint32_t start_code) {
425 DCHECK_GE(bytes_needed, 3);
426 DCHECK_LE(num_bits, 24);
429 uint32_t bits_to_shift = 24 - num_bits;
430 uint32_t mask = (1 << num_bits) - 1;
431 while (*offset + bytes_needed < buffer_size) {
432 uint32_t next = Read24(buffer + *offset);
433 if (((next >> bits_to_shift) & mask) == start_code)
441 static bool CheckH261(
const uint8_t* buffer,
int buffer_size) {
444 RCHECK(buffer_size > 16);
447 bool seen_start_code =
false;
450 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 20, 0x10)) {
453 return seen_start_code;
458 BitReader reader(buffer + offset, buffer_size - offset);
459 RCHECK(ReadBits(&reader, 20) == 0x10);
462 reader.SkipBits(5 + 6);
466 int extra = ReadBits(&reader, 1);
468 if (!reader.SkipBits(8))
469 return seen_start_code;
470 if (!reader.ReadBits(1, &extra))
471 return seen_start_code;
478 if (!reader.ReadBits(16, &next))
479 return seen_start_code;
483 seen_start_code =
true;
489 static bool CheckH263(
const uint8_t* buffer,
int buffer_size) {
493 RCHECK(buffer_size > 16);
496 bool seen_start_code =
false;
499 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 9, 22, 0x20)) {
502 return seen_start_code;
507 BitReader reader(buffer + offset, 9);
508 RCHECK(ReadBits(&reader, 22) == 0x20);
514 RCHECK(ReadBits(&reader, 2) == 2);
518 reader.SkipBits(1 + 1 + 1);
521 int format = ReadBits(&reader, 3);
522 RCHECK(format != 0 && format != 6);
526 int ufep = ReadBits(&reader, 3);
529 format = ReadBits(&reader, 3);
530 RCHECK(format != 0 && format != 7);
533 RCHECK(ReadBits(&reader, 4) == 8);
539 int picture_type_code = ReadBits(&reader, 3);
540 RCHECK(picture_type_code != 6 && picture_type_code != 7);
544 reader.SkipBits(1 + 1 + 1);
547 RCHECK(ReadBits(&reader, 3) == 1);
551 seen_start_code =
true;
557 static bool CheckH264(
const uint8_t* buffer,
int buffer_size) {
561 RCHECK(buffer_size > 4);
564 int parameter_count = 0;
567 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 4, 24, 1)) {
570 return parameter_count > 0;
575 BitReader reader(buffer + offset, 4);
576 RCHECK(ReadBits(&reader, 24) == 1);
579 RCHECK(ReadBits(&reader, 1) == 0);
582 int nal_ref_idc = ReadBits(&reader, 2);
583 int nal_unit_type = ReadBits(&reader, 5);
585 switch (nal_unit_type) {
587 RCHECK(nal_ref_idc != 0);
594 RCHECK(nal_ref_idc == 0);
607 static const char kHlsSignature[] =
"#EXTM3U";
608 static const char kHls1[] =
"#EXT-X-STREAM-INF:";
609 static const char kHls2[] =
"#EXT-X-TARGETDURATION:";
610 static const char kHls3[] =
"#EXT-X-MEDIA-SEQUENCE:";
613 static bool CheckHls(
const uint8_t* buffer,
int buffer_size) {
618 if (StartsWith(buffer, buffer_size, kHlsSignature)) {
623 int offset = strlen(kHlsSignature);
624 while (offset < buffer_size) {
625 if (buffer[offset] ==
'#') {
626 if (StartsWith(buffer + offset, buffer_size - offset, kHls1) ||
627 StartsWith(buffer + offset, buffer_size - offset, kHls2) ||
628 StartsWith(buffer + offset, buffer_size - offset, kHls3)) {
639 static bool CheckMJpeg(
const uint8_t* buffer,
int buffer_size) {
642 RCHECK(buffer_size >= 16);
645 int last_restart = -1;
647 while (offset + 5 < buffer_size) {
649 RCHECK(buffer[offset] == 0xff);
650 uint8_t code = buffer[offset + 1];
651 RCHECK(code >= 0xc0 || code == 1);
664 if (code == 0xd8 || code == 1) {
667 }
else if (code >= 0xd0 && code <= 0xd7) {
669 int restart = code & 0x07;
670 if (last_restart >= 0)
671 RCHECK(restart == (last_restart + 1) % 8);
672 last_restart = restart;
676 int length = Read16(buffer + offset + 2) + 2;
682 int number_components = buffer[offset + 4];
683 RCHECK(length == 8 + 2 * number_components);
687 while (offset + 2 < buffer_size) {
688 if (buffer[offset] == 0xff && buffer[offset + 1] != 0)
699 return (num_codes > 1);
702 enum Mpeg2StartCodes {
703 PROGRAM_END_CODE = 0xb9,
704 PACK_START_CODE = 0xba
708 static bool CheckMpeg2ProgramStream(
const uint8_t* buffer,
int buffer_size) {
710 RCHECK(buffer_size > 14);
713 while (offset + 14 < buffer_size) {
714 BitReader reader(buffer + offset, 14);
717 RCHECK(ReadBits(&reader, 24) == 1);
718 RCHECK(ReadBits(&reader, 8) == PACK_START_CODE);
721 int mpeg_version = ReadBits(&reader, 2);
722 if (mpeg_version == 0) {
725 RCHECK(ReadBits(&reader, 2) == 2);
727 RCHECK(mpeg_version == 1);
734 RCHECK(ReadBits(&reader, 1) == 1);
740 RCHECK(ReadBits(&reader, 1) == 1);
746 RCHECK(ReadBits(&reader, 1) == 1);
748 if (mpeg_version == 0) {
750 RCHECK(ReadBits(&reader, 1) == 1);
756 RCHECK(ReadBits(&reader, 1) == 1);
766 RCHECK(ReadBits(&reader, 2) == 3);
772 int pack_stuffing_length = ReadBits(&reader, 3);
773 offset += 14 + pack_stuffing_length;
777 while (offset + 6 < buffer_size && Read24(buffer + offset) == 1) {
779 int stream_id = buffer[offset + 3];
782 if (mpeg_version == 0)
783 RCHECK(stream_id != 0xbc && stream_id < 0xf0);
785 RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe);
788 if (stream_id == PACK_START_CODE)
790 if (stream_id == PROGRAM_END_CODE)
793 int pes_length = Read16(buffer + offset + 4);
794 RCHECK(pes_length > 0);
795 offset = offset + 6 + pes_length;
803 const uint8_t kMpeg2SyncWord = 0x47;
806 static bool CheckMpeg2TransportStream(
const uint8_t* buffer,
int buffer_size) {
812 RCHECK(buffer_size >= 250);
815 int packet_length = -1;
816 while (buffer[offset] != kMpeg2SyncWord && offset < 20) {
821 while (offset + 6 < buffer_size) {
822 BitReader reader(buffer + offset, 6);
825 RCHECK(ReadBits(&reader, 8) == kMpeg2SyncWord);
829 reader.SkipBits(1 + 1 + 1);
832 int pid = ReadBits(&reader, 13);
833 RCHECK(pid < 3 || pid > 15);
839 int adaptation_field_control = ReadBits(&reader, 2);
840 RCHECK(adaptation_field_control != 0);
843 if (adaptation_field_control >= 2) {
848 int adaptation_field_length = ReadBits(&reader, 8);
849 if (adaptation_field_control == 2)
850 RCHECK(adaptation_field_length == 183);
852 RCHECK(adaptation_field_length <= 182);
856 if (packet_length < 0) {
857 if (buffer[offset + 188] == kMpeg2SyncWord)
859 else if (buffer[offset + 192] == kMpeg2SyncWord)
861 else if (buffer[offset + 204] == kMpeg2SyncWord)
866 offset += packet_length;
871 enum Mpeg4StartCodes {
872 VISUAL_OBJECT_SEQUENCE_START_CODE = 0xb0,
873 VISUAL_OBJECT_SEQUENCE_END_CODE = 0xb1,
874 VISUAL_OBJECT_START_CODE = 0xb5,
875 VOP_START_CODE = 0xb6
879 static bool CheckMpeg4BitStream(
const uint8_t* buffer,
int buffer_size) {
883 RCHECK(buffer_size > 4);
886 int sequence_start_count = 0;
887 int sequence_end_count = 0;
888 int visual_object_count = 0;
892 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 6, 24, 1)) {
895 return (sequence_start_count > 0 && visual_object_count > 0);
900 BitReader reader(buffer + offset, 6);
901 RCHECK(ReadBits(&reader, 24) == 1);
903 int start_code = ReadBits(&reader, 8);
904 RCHECK(start_code < 0x30 || start_code > 0xaf);
905 RCHECK(start_code < 0xb7 || start_code > 0xb9);
907 switch (start_code) {
908 case VISUAL_OBJECT_SEQUENCE_START_CODE: {
909 ++sequence_start_count;
911 int profile = ReadBits(&reader, 8);
913 RCHECK(profile < 0x04 || profile > 0x10);
914 RCHECK(profile < 0x13 || profile > 0x20);
915 RCHECK(profile < 0x23 || profile > 0x31);
916 RCHECK(profile < 0x35 || profile > 0x41);
917 RCHECK(profile < 0x43 || profile > 0x60);
918 RCHECK(profile < 0x65 || profile > 0x70);
919 RCHECK(profile < 0x73 || profile > 0x80);
920 RCHECK(profile < 0x83 || profile > 0x90);
921 RCHECK(profile < 0x95 || profile > 0xa0);
922 RCHECK(profile < 0xa4 || profile > 0xb0);
923 RCHECK(profile < 0xb5 || profile > 0xc0);
924 RCHECK(profile < 0xc3 || profile > 0xd0);
925 RCHECK(profile < 0xe4);
929 case VISUAL_OBJECT_SEQUENCE_END_CODE:
930 RCHECK(++sequence_end_count == sequence_start_count);
933 case VISUAL_OBJECT_START_CODE: {
934 ++visual_object_count;
935 if (ReadBits(&reader, 1) == 1) {
936 int visual_object_verid = ReadBits(&reader, 4);
937 RCHECK(visual_object_verid > 0 && visual_object_verid < 3);
938 RCHECK(ReadBits(&reader, 3) != 0);
940 int visual_object_type = ReadBits(&reader, 4);
941 RCHECK(visual_object_type > 0 && visual_object_type < 6);
946 RCHECK(++vop_count <= visual_object_count);
955 static bool CheckMov(
const uint8_t* buffer,
int buffer_size) {
958 RCHECK(buffer_size > 8);
962 while (offset + 8 < buffer_size) {
963 int atomsize = Read32(buffer + offset);
964 uint32_t atomtype = Read32(buffer + offset + 4);
967 case TAG(
'f',
't',
'y',
'p'):
968 case TAG(
'p',
'd',
'i',
'n'):
969 case TAG(
'b',
'l',
'o',
'c'):
970 case TAG(
'm',
'o',
'o',
'v'):
971 case TAG(
'm',
'o',
'o',
'f'):
972 case TAG(
'm',
'f',
'r',
'a'):
973 case TAG(
'm',
'd',
'a',
't'):
974 case TAG(
'f',
'r',
'e',
'e'):
975 case TAG(
's',
'k',
'i',
'p'):
976 case TAG(
'm',
'e',
't',
'a'):
977 case TAG(
'm',
'e',
'c',
'o'):
978 case TAG(
's',
't',
'y',
'p'):
979 case TAG(
's',
'i',
'd',
'x'):
980 case TAG(
's',
's',
'i',
'x'):
981 case TAG(
'p',
'r',
'f',
't'):
982 case TAG(
'u',
'u',
'i',
'd'):
987 if (++boxes_seen >= 2)
996 if (offset + 16 > buffer_size)
998 if (Read32(buffer + offset + 8) != 0)
1000 atomsize = Read32(buffer + offset + 12);
1022 static int kSampleRateTable[4][4] = { { 11025, 12000, 8000, 0 },
1024 { 22050, 24000, 16000, 0 },
1025 { 44100, 48000, 32000, 0 }
1028 static int kBitRateTableV1L1[16] = { 0, 32, 64, 96, 128, 160, 192, 224, 256,
1029 288, 320, 352, 384, 416, 448, 0 };
1030 static int kBitRateTableV1L2[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160,
1031 192, 224, 256, 320, 384, 0 };
1032 static int kBitRateTableV1L3[16] = { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128,
1033 160, 192, 224, 256, 320, 0 };
1034 static int kBitRateTableV2L1[16] = { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1035 160, 176, 192, 224, 256, 0 };
1036 static int kBitRateTableV2L23[16] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,
1037 112, 128, 144, 160, 0 };
1039 static bool ValidMpegAudioFrameHeader(
const uint8_t* header,
1043 DCHECK_GE(header_size, 4);
1045 BitReader reader(header, 4);
1048 RCHECK(ReadBits(&reader, 11) == 0x7ff);
1051 int version = ReadBits(&reader, 2);
1052 RCHECK(version != 1);
1055 int layer = ReadBits(&reader, 2);
1062 int bitrate_index = ReadBits(&reader, 4);
1063 RCHECK(bitrate_index != 0xf);
1066 int sampling_index = ReadBits(&reader, 2);
1067 RCHECK(sampling_index != 3);
1070 int padding = ReadBits(&reader, 1);
1076 int sampling_rate = kSampleRateTable[version][sampling_index];
1078 if (version == VERSION_1) {
1079 if (layer == LAYER_1)
1080 bitrate = kBitRateTableV1L1[bitrate_index];
1081 else if (layer == LAYER_2)
1082 bitrate = kBitRateTableV1L2[bitrate_index];
1084 bitrate = kBitRateTableV1L3[bitrate_index];
1086 if (layer == LAYER_1)
1087 bitrate = kBitRateTableV2L1[bitrate_index];
1089 bitrate = kBitRateTableV2L23[bitrate_index];
1091 if (layer == LAYER_1)
1092 *framesize = ((12000 * bitrate) / sampling_rate + padding) * 4;
1094 *framesize = (144000 * bitrate) / sampling_rate + padding;
1095 return (bitrate > 0 && sampling_rate > 0);
1099 static int GetMp3HeaderSize(
const uint8_t* buffer,
int buffer_size) {
1100 DCHECK_GE(buffer_size, 9);
1101 int size = ((buffer[6] & 0x7f) << 21) + ((buffer[7] & 0x7f) << 14) +
1102 ((buffer[8] & 0x7f) << 7) + (buffer[9] & 0x7f) + 10;
1103 if (buffer[5] & 0x10)
1109 static bool CheckMp3(
const uint8_t* buffer,
int buffer_size,
bool seenHeader) {
1110 RCHECK(buffer_size >= 10);
1116 offset = GetMp3HeaderSize(buffer, buffer_size);
1119 while (offset < buffer_size && buffer[offset] == 0)
1123 while (offset + 3 < buffer_size) {
1124 RCHECK(ValidMpegAudioFrameHeader(
1125 buffer + offset, buffer_size - offset, &framesize));
1130 offset += framesize;
1140 static bool VerifyNumber(
const uint8_t* buffer,
1144 RCHECK(*offset < buffer_size);
1147 while (isspace(buffer[*offset])) {
1149 RCHECK(*offset < buffer_size);
1154 while (--max_digits >= 0 && isdigit(buffer[*offset])) {
1157 if (*offset >= buffer_size)
1162 return (numSeen > 0);
1168 static inline bool VerifyCharacters(
const uint8_t* buffer,
1173 RCHECK(*offset < buffer_size);
1174 char c =
static_cast<char>(buffer[(*offset)++]);
1175 return (c == c1 || (c == c2 && c2 != 0));
1179 static bool CheckSrt(
const uint8_t* buffer,
int buffer_size) {
1181 RCHECK(buffer_size > 20);
1184 int offset = StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1185 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1186 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r'));
1189 while (VerifyCharacters(buffer, buffer_size, &offset,
'\n',
'\r')) {}
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));
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(VerifyCharacters(buffer, buffer_size, &offset,
'>', 0));
1206 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
' ', 0));
1207 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 100));
1208 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1209 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1210 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
':', 0));
1211 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 2));
1212 RCHECK(VerifyCharacters(buffer, buffer_size, &offset,
',',
'.'));
1213 RCHECK(VerifyNumber(buffer, buffer_size, &offset, 3));
1218 static int GetElementId(BitReader* reader) {
1222 if (reader->bits_available() >= 8) {
1223 int num_bits_to_read = 0;
1224 static int prefix[] = { 0x80, 0x4000, 0x200000, 0x10000000 };
1225 for (
int i = 0; i < 4; ++i) {
1226 num_bits_to_read += 7;
1227 if (ReadBits(reader, 1) == 1) {
1228 if (
static_cast<int>(reader->bits_available()) < num_bits_to_read)
1231 return ReadBits(reader, num_bits_to_read) | prefix[i];
1240 static uint64_t GetVint(BitReader* reader) {
1244 if (reader->bits_available() >= 8) {
1245 int num_bits_to_read = 0;
1246 for (
int i = 0; i < 8; ++i) {
1247 num_bits_to_read += 7;
1248 if (ReadBits(reader, 1) == 1) {
1249 if (
static_cast<int>(reader->bits_available()) < num_bits_to_read)
1251 return ReadBits(reader, num_bits_to_read);
1258 return (reader->bits_available() / 8) + 2;
1262 static bool CheckWebm(
const uint8_t* buffer,
int buffer_size) {
1264 RCHECK(buffer_size > 12);
1266 BitReader reader(buffer, buffer_size);
1269 RCHECK(GetElementId(&reader) == 0x1a45dfa3);
1272 int header_size = GetVint(&reader);
1273 RCHECK(
static_cast<int>(reader.bits_available()) / 8 >= header_size);
1276 while (reader.bits_available() > 0) {
1277 int tag = GetElementId(&reader);
1278 int tagsize = GetVint(&reader);
1288 RCHECK(reader.SkipBits(tagsize * 8));
1293 switch (ReadBits(&reader, 32)) {
1294 case TAG(
'w',
'e',
'b',
'm') :
1296 case TAG(
'm',
'a',
't',
'r') :
1297 return (ReadBits(&reader, 32) == TAG(
'o',
's',
'k',
'a'));
1308 enum VC1StartCodes {
1309 VC1_FRAME_START_CODE = 0x0d,
1310 VC1_ENTRY_POINT_START_CODE = 0x0e,
1311 VC1_SEQUENCE_START_CODE = 0x0f
1315 static bool CheckVC1(
const uint8_t* buffer,
int buffer_size) {
1322 RCHECK(buffer_size >= 24);
1325 if (buffer[0] == 0xc5 &&
1326 Read32(buffer + 4) == 0x04 &&
1327 Read32(buffer + 20) == 0x0c) {
1329 BitReader reader(buffer + 8, 12);
1331 int profile = ReadBits(&reader, 4);
1332 if (profile == 0 || profile == 4) {
1334 reader.SkipBits(3 + 5 + 1);
1337 RCHECK(ReadBits(&reader, 1) == 0);
1343 RCHECK(ReadBits(&reader, 1) == 1);
1346 reader.SkipBits(1 + 1 + 2 + 1);
1349 RCHECK(ReadBits(&reader, 1) == 0);
1353 reader.SkipBits(1 + 1 + 1 + 3 + 2 + 1);
1356 RCHECK(ReadBits(&reader, 1) == 1);
1359 RCHECK(profile == 12);
1360 RCHECK(ReadBits(&reader, 28) == 0);
1364 RCHECK(ReadBits(&reader, 32) <= 8192);
1365 RCHECK(ReadBits(&reader, 32) <= 8192);
1371 int sequence_start_code = 0;
1372 int frame_start_code = 0;
1375 if (!AdvanceToStartCode(buffer, buffer_size, &offset, 5, 24, 1)) {
1379 return (sequence_start_code > 0 && frame_start_code > 0);
1384 BitReader reader(buffer + offset, 5);
1385 RCHECK(ReadBits(&reader, 24) == 1);
1388 switch (ReadBits(&reader, 8)) {
1389 case VC1_SEQUENCE_START_CODE: {
1390 ++sequence_start_code;
1391 switch (ReadBits(&reader, 2)) {
1394 RCHECK(ReadBits(&reader, 2) == 0);
1399 RCHECK(ReadBits(&reader, 3) <= 4);
1400 RCHECK(ReadBits(&reader, 2) == 1);
1406 case VC1_ENTRY_POINT_START_CODE:
1409 RCHECK(sequence_start_code > 0);
1412 case VC1_FRAME_START_CODE:
1424 static const char kAmrSignature[] =
"#!AMR";
1425 static const uint8_t kAsfSignature[] = {0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
1426 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa,
1427 0x00, 0x62, 0xce, 0x6c};
1428 static const char kAssSignature[] =
"[Script Info]";
1429 static const char kAssBomSignature[] = UTF8_BYTE_ORDER_MARK
"[Script Info]";
1430 static const uint8_t kWtvSignature[] = {0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49,
1431 0xda, 0x11, 0xa6, 0x4e, 0x00, 0x07,
1432 0xe9, 0x5e, 0xad, 0x8d};
1437 static MediaContainerName LookupContainerByFirst4(
const uint8_t* buffer,
1440 if (buffer_size < 12)
1441 return CONTAINER_UNKNOWN;
1443 uint32_t first4 = Read32(buffer);
1446 if (CheckWebm(buffer, buffer_size))
1447 return CONTAINER_WEBM;
1451 if (StartsWith(buffer,
1454 sizeof(kAsfSignature))) {
1455 return CONTAINER_ASF;
1459 case TAG(
'#',
'!',
'A',
'M'):
1460 if (StartsWith(buffer, buffer_size, kAmrSignature))
1461 return CONTAINER_AMR;
1464 case TAG(
'#',
'E',
'X',
'T'):
1465 if (CheckHls(buffer, buffer_size))
1466 return CONTAINER_HLS;
1469 case TAG(
'.',
'R',
'M',
'F'):
1470 if (buffer[4] == 0 && buffer[5] == 0)
1471 return CONTAINER_RM;
1474 case TAG(
'.',
'r',
'a',
'\xfd'):
1475 return CONTAINER_RM;
1477 case TAG(
'B',
'I',
'K',
'b'):
1478 case TAG(
'B',
'I',
'K',
'd'):
1479 case TAG(
'B',
'I',
'K',
'f'):
1480 case TAG(
'B',
'I',
'K',
'g'):
1481 case TAG(
'B',
'I',
'K',
'h'):
1482 case TAG(
'B',
'I',
'K',
'i'):
1483 if (CheckBink(buffer, buffer_size))
1484 return CONTAINER_BINK;
1487 case TAG(
'c',
'a',
'f',
'f'):
1488 if (CheckCaf(buffer, buffer_size))
1489 return CONTAINER_CAF;
1492 case TAG(
'D',
'E',
'X',
'A'):
1493 if (buffer_size > 15 &&
1494 Read16(buffer + 11) <= 2048 &&
1495 Read16(buffer + 13) <= 2048) {
1496 return CONTAINER_DXA;
1500 case TAG(
'D',
'T',
'S',
'H'):
1501 if (Read32(buffer + 4) == TAG(
'D',
'H',
'D',
'R'))
1502 return CONTAINER_DTSHD;
1512 if (Read32(buffer + 4) != 0 && Read32(buffer + 8) != 0)
1513 return CONTAINER_IRCAM;
1516 case TAG(
'f',
'L',
'a',
'C'):
1517 return CONTAINER_FLAC;
1519 case TAG(
'F',
'L',
'V',0):
1520 case TAG(
'F',
'L',
'V',1):
1521 case TAG(
'F',
'L',
'V',2):
1522 case TAG(
'F',
'L',
'V',3):
1523 case TAG(
'F',
'L',
'V',4):
1524 if (buffer[5] == 0 && Read32(buffer + 5) > 8)
1525 return CONTAINER_FLV;
1528 case TAG(
'F',
'O',
'R',
'M'):
1529 switch (Read32(buffer + 8)) {
1530 case TAG(
'A',
'I',
'F',
'F'):
1531 case TAG(
'A',
'I',
'F',
'C'):
1532 return CONTAINER_AIFF;
1536 case TAG(
'M',
'A',
'C',
' '):
1537 return CONTAINER_APE;
1539 case TAG(
'O',
'N',
'2',
' '):
1540 if (Read32(buffer + 8) == TAG(
'O',
'N',
'2',
'f'))
1541 return CONTAINER_AVI;
1544 case TAG(
'O',
'g',
'g',
'S'):
1546 return CONTAINER_OGG;
1549 case TAG(
'R',
'F',
'6',
'4'):
1550 if (buffer_size > 16 && Read32(buffer + 12) == TAG(
'd',
's',
'6',
'4'))
1551 return CONTAINER_WAV;
1554 case TAG(
'R',
'I',
'F',
'F'):
1555 switch (Read32(buffer + 8)) {
1556 case TAG(
'A',
'V',
'I',
' '):
1557 case TAG(
'A',
'V',
'I',
'X'):
1558 case TAG(
'A',
'V',
'I',
'\x19'):
1559 case TAG(
'A',
'M',
'V',
' '):
1560 return CONTAINER_AVI;
1561 case TAG(
'W',
'A',
'V',
'E'):
1562 return CONTAINER_WAV;
1566 case TAG(
'[',
'S',
'c',
'r'):
1567 if (StartsWith(buffer, buffer_size, kAssSignature))
1568 return CONTAINER_ASS;
1571 case TAG(
'\xef',
'\xbb',
'\xbf',
'['):
1572 if (StartsWith(buffer, buffer_size, kAssBomSignature))
1573 return CONTAINER_ASS;
1580 if (CheckDts(buffer, buffer_size))
1581 return CONTAINER_DTS;
1585 if (StartsWith(buffer,
1588 sizeof(kWtvSignature))) {
1589 return CONTAINER_WTV;
1593 return CONTAINER_MPEG2PS;
1598 uint32_t first3 = first4 & 0xffffff00;
1600 case TAG(
'C',
'W',
'S',0):
1601 case TAG(
'F',
'W',
'S',0):
1602 return CONTAINER_SWF;
1604 case TAG(
'I',
'D',
'3',0):
1605 if (CheckMp3(buffer, buffer_size, true))
1606 return CONTAINER_MP3;
1611 uint32_t first2 = Read16(buffer);
1614 if (CheckAc3(buffer, buffer_size))
1615 return CONTAINER_AC3;
1616 if (CheckEac3(buffer, buffer_size))
1617 return CONTAINER_EAC3;
1624 if (CheckAac(buffer, buffer_size))
1625 return CONTAINER_AAC;
1630 if (CheckMp3(buffer, buffer_size,
false))
1631 return CONTAINER_MP3;
1633 return CONTAINER_UNKNOWN;
1637 const char kWebVtt[] =
"WEBVTT";
1639 bool CheckWebVtt(
const uint8_t* buffer,
int buffer_size) {
1641 StartsWith(buffer, buffer_size, UTF8_BYTE_ORDER_MARK) ? 3 : 0;
1643 return StartsWith(buffer + offset, buffer_size - offset,
1644 reinterpret_cast<const uint8_t*
>(kWebVtt),
1645 std::size(kWebVtt) - 1);
1648 bool CheckTtml(
const uint8_t* buffer,
int buffer_size) {
1650 if (!StartsWith(buffer, buffer_size,
"<?xml"))
1656 xml::scoped_xml_ptr<xmlDoc> doc(
1657 xmlParseMemory(
reinterpret_cast<const char*
>(buffer), buffer_size));
1661 xmlNodePtr root_node = xmlDocGetRootElement(doc.get());
1662 std::string root_node_name(
reinterpret_cast<const char*
>(root_node->name));
1664 return root_node_name ==
"tt";
1670 MediaContainerName DetermineContainer(
const uint8_t* buffer,
int buffer_size) {
1674 if (CheckMov(buffer, buffer_size))
1675 return CONTAINER_MOV;
1679 MediaContainerName result = LookupContainerByFirst4(buffer, buffer_size);
1680 if (result != CONTAINER_UNKNOWN)
1684 if (CheckWebVtt(buffer, buffer_size))
1685 return CONTAINER_WEBVTT;
1688 if (CheckMpeg2ProgramStream(buffer, buffer_size))
1689 return CONTAINER_MPEG2PS;
1690 if (CheckMpeg2TransportStream(buffer, buffer_size))
1691 return CONTAINER_MPEG2TS;
1692 if (CheckMJpeg(buffer, buffer_size))
1693 return CONTAINER_MJPEG;
1694 if (CheckDV(buffer, buffer_size))
1695 return CONTAINER_DV;
1696 if (CheckH261(buffer, buffer_size))
1697 return CONTAINER_H261;
1698 if (CheckH263(buffer, buffer_size))
1699 return CONTAINER_H263;
1700 if (CheckH264(buffer, buffer_size))
1701 return CONTAINER_H264;
1702 if (CheckMpeg4BitStream(buffer, buffer_size))
1703 return CONTAINER_MPEG4BS;
1704 if (CheckVC1(buffer, buffer_size))
1705 return CONTAINER_VC1;
1706 if (CheckSrt(buffer, buffer_size))
1707 return CONTAINER_SRT;
1708 if (CheckGsm(buffer, buffer_size))
1709 return CONTAINER_GSM;
1714 if (AdvanceToStartCode(buffer, buffer_size, &offset, 4, 16, kAc3SyncWord)) {
1715 if (CheckAc3(buffer + offset, buffer_size - offset))
1716 return CONTAINER_AC3;
1717 if (CheckEac3(buffer + offset, buffer_size - offset))
1718 return CONTAINER_EAC3;
1723 if (CheckTtml(buffer, buffer_size))
1724 return CONTAINER_TTML;
1726 return CONTAINER_UNKNOWN;
1729 MediaContainerName DetermineContainerFromFormatName(
1730 const std::string& format_name) {
1731 std::string normalized_format_name = format_name;
1732 std::transform(format_name.begin(), format_name.end(),
1733 normalized_format_name.begin(), ::tolower);
1735 if (normalized_format_name ==
"aac" || normalized_format_name ==
"adts") {
1736 return CONTAINER_AAC;
1737 }
else if (normalized_format_name ==
"ac3") {
1738 return CONTAINER_AC3;
1739 }
else if (normalized_format_name ==
"ec3" ||
1740 normalized_format_name ==
"eac3") {
1741 return CONTAINER_EAC3;
1742 }
else if (normalized_format_name ==
"mp3") {
1743 return CONTAINER_MP3;
1744 }
else if (normalized_format_name ==
"webm") {
1745 return CONTAINER_WEBM;
1746 }
else if (normalized_format_name ==
"cmfa" ||
1747 normalized_format_name ==
"cmft" ||
1748 normalized_format_name ==
"cmfv" ||
1749 normalized_format_name ==
"m4a" ||
1750 normalized_format_name ==
"m4s" ||
1751 normalized_format_name ==
"m4v" ||
1752 normalized_format_name ==
"mov" ||
1753 normalized_format_name ==
"mp4" ||
1754 normalized_format_name ==
"ttml+mp4" ||
1755 normalized_format_name ==
"webvtt+mp4" ||
1756 normalized_format_name ==
"vtt+mp4") {
1757 return CONTAINER_MOV;
1758 }
else if (normalized_format_name ==
"ts" ||
1759 normalized_format_name ==
"mpeg2ts") {
1760 return CONTAINER_MPEG2TS;
1761 }
else if (normalized_format_name ==
"wvm") {
1762 return CONTAINER_WVM;
1763 }
else if (normalized_format_name ==
"vtt" ||
1764 normalized_format_name ==
"webvtt") {
1765 return CONTAINER_WEBVTT;
1766 }
else if (normalized_format_name ==
"ttml" ||
1768 normalized_format_name ==
"xml") {
1769 return CONTAINER_TTML;
1771 return CONTAINER_UNKNOWN;
1774 MediaContainerName DetermineContainerFromFileName(
1775 const std::string& file_name) {
1776 const size_t pos = file_name.rfind(
'.');
1777 if (pos == std::string::npos)
1778 return CONTAINER_UNKNOWN;
1779 const std::string& file_extension = file_name.substr(pos + 1);
1780 return DetermineContainerFromFormatName(file_extension);
All the methods that are virtual are virtual for mocking.