586 int max_size, uint64_t *number)
599 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
608 if (read > max_size) {
611 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
619 total = (total << 8) |
avio_r8(pb);
635 if (res > 0 && *number + 1 == 1ULL << (7 * res))
636 *number = 0xffffffffffffffULL;
654 *num = (*num << 8) |
avio_r8(pb);
667 }
else if (size == 4) {
669 }
else if (size == 8){
771 *num = unum - ((1LL << (7*res - 1)) - 1);
780 uint32_t
id,
void *
data)
783 for (i=0; syntax[i].
id; i++)
784 if (
id == syntax[i].
id)
816 for (i=0; syntax[i].
id; i++)
817 switch (syntax[i].type) {
819 *(uint64_t *)((
char *)data+syntax[i].
data_offset) = syntax[i].def.
u;
822 *(
double *)((
char *)data+syntax[i].data_offset) = syntax[i].
def.
f;
850 uint32_t
id = syntax->
id;
861 list->
elem = newelem;
871 if (max_lengths[syntax->
type] && length > max_lengths[syntax->
type]) {
873 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for syntax element %i\n",
874 length, max_lengths[syntax->
type], syntax->
type);
879 switch (syntax->
type) {
904 for (i=0; syntax[i].
id; i++) {
905 void *data_off = (
char *)data + syntax[i].data_offset;
906 switch (syntax[i].type) {
911 if (syntax[i].list_elem_size) {
913 char *ptr = list->
elem;
931 int len_mask = 0x80,
size = 1, n = 1, i;
939 while (
size <= 8 && !(total & len_mask)) {
945 total &= (len_mask - 1);
947 total = (total << 8) | p->
buf[4 + n++];
959 if (total < probelen)
961 for (n = 4+
size; n <= 4+
size+total-probelen; n++)
977 if (tracks[i].num == num)
989 int isize = *buf_size;
992 int pkt_size = isize;
996 if (pkt_size >= 10000000)
999 switch (encodings[0].compression.algo) {
1007 pkt_size = isize + header_size;
1012 memcpy(pkt_data, header, header_size);
1013 memcpy(pkt_data + header_size, data, isize);
1019 olen = pkt_size *= 3;
1025 pkt_data = newpktdata;
1037 z_stream zstream = {0};
1038 if (inflateInit(&zstream) != Z_OK)
1040 zstream.next_in =
data;
1041 zstream.avail_in = isize;
1046 inflateEnd(&zstream);
1049 pkt_data = newpktdata;
1050 zstream.avail_out = pkt_size - zstream.total_out;
1051 zstream.next_out = pkt_data + zstream.total_out;
1052 result = inflate(&zstream, Z_NO_FLUSH);
1053 }
while (result==Z_OK && pkt_size<10000000);
1054 pkt_size = zstream.total_out;
1055 inflateEnd(&zstream);
1056 if (result != Z_STREAM_END) {
1057 if (result == Z_MEM_ERROR)
1068 bz_stream bzstream = {0};
1069 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1071 bzstream.next_in =
data;
1072 bzstream.avail_in = isize;
1077 BZ2_bzDecompressEnd(&bzstream);
1080 pkt_data = newpktdata;
1081 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1082 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1083 result = BZ2_bzDecompress(&bzstream);
1084 }
while (result==BZ_OK && pkt_size<10000000);
1085 pkt_size = bzstream.total_out_lo32;
1086 BZ2_bzDecompressEnd(&bzstream);
1087 if (result != BZ_STREAM_END) {
1088 if (result == BZ_MEM_ERROR)
1102 *buf_size = pkt_size;
1110 AVPacket *pkt, uint64_t display_duration)
1112 char *
line, *layer, *ptr = pkt->
data, *end = ptr+pkt->
size;
1113 for (; *ptr!=
',' && ptr<end-1; ptr++);
1116 for (; *ptr!=
',' && ptr<end-1; ptr++);
1118 int64_t end_pts = pkt->
pts + display_duration;
1120 int ec = matroska->
time_scale * end_pts / 10000000;
1121 int sh, sm, ss, eh, em, es,
len;
1122 sh = sc/360000; sc -= 360000*sh;
1123 sm = sc/ 6000; sc -= 6000*sm;
1124 ss = sc/ 100; sc -= 100*ss;
1125 eh = ec/360000; ec -= 360000*eh;
1126 em = ec/ 6000; ec -= 6000*em;
1127 es = ec/ 100; ec -= 100*es;
1132 snprintf(line,len,
"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
1133 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
1136 pkt->
size = strlen(line);
1142 int old_size = out->
size;
1161 for (i=0; i < list->
nb_elem; i++) {
1162 const char *lang = tags[i].
lang && strcmp(tags[i].lang,
"und") ?
1165 if (!tags[i].
name) {
1169 if (prefix) snprintf(key,
sizeof(key),
"%s/%s", prefix, tags[i].name);
1170 else av_strlcpy(key, tags[i].name,
sizeof(key));
1171 if (tags[i].def || !lang) {
1173 if (tags[i].sub.nb_elem)
1180 if (tags[i].sub.nb_elem)
1194 if (tags[i].target.attachuid) {
1197 if (attachment[j].uid == tags[i].target.attachuid
1200 &attachment[j].stream->metadata,
NULL);
1201 }
else if (tags[i].target.chapteruid) {
1204 if (chapter[j].uid == tags[i].target.chapteruid
1207 &chapter[j].chapter->metadata,
NULL);
1208 }
else if (tags[i].target.trackuid) {
1211 if (track[j].uid == tags[i].target.trackuid && track[j].
stream)
1213 &track[j].stream->metadata,
NULL);
1225 uint32_t level_up = matroska->
level_up;
1232 if (idx >= seekhead_list->
nb_elem
1244 "Max EBML element depth (%d) reached, "
1249 level.
length = (uint64_t)-1;
1254 ret =
ebml_parse(matroska, matroska_segment, matroska);
1259 if (length == (uint64_t)-1)
1283 for (i = 0; i < seekhead_list->
nb_elem; i++) {
1285 if (seekhead[i].pos <= before_pos)
1304 int index_scale = 1;
1307 for (i = 0; i < seekhead_list->
nb_elem; i++)
1310 assert(i <= seekhead_list->nb_elem);
1314 index_list = &matroska->
index;
1315 index = index_list->
elem;
1321 for (i = 0; i < index_list->
nb_elem; i++) {
1324 for (j = 0; j < pos_list->
nb_elem; j++) {
1326 if (track && track->stream)
1329 index[i].
time/index_scale, 0, 0,
1337 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
1341 if (strstr(codec_id, aac_profiles[profile]))
1364 uint64_t max_start = 0;
1376 "EBML header using unsupported features\n"
1377 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1395 if ((res =
ebml_parse(matroska, matroska_segments, matroska)) < 0)
1413 int extradata_size = 0;
1414 int extradata_offset = 0;
1422 "Unknown or unsupported track type %"PRIu64
"\n",
1440 if (encodings_list->
nb_elem > 1) {
1442 "Multiple combined encodings not supported");
1443 }
else if (encodings_list->
nb_elem == 1) {
1444 if (encodings[0].type ||
1456 encodings[0].
scope = 0;
1458 "Unsupported encoding type");
1468 "Failed to decode codec private data\n");
1488 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC")
1494 extradata_offset = 40;
1495 }
else if (!strcmp(track->
codec_id,
"A_MS/ACM")
1506 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME")
1529 if (extradata ==
NULL)
1531 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1532 extradata[1] = ((sri&0x01) << 7) | (track->
audio.
channels<<3);
1533 if (strstr(track->
codec_id,
"SBR")) {
1535 extradata[2] = 0x56;
1536 extradata[3] = 0xE5;
1537 extradata[4] = 0x80 | (sri<<3);
1547 if (extradata ==
NULL)
1549 AV_WB32(extradata, extradata_size);
1550 memcpy(&extradata[4],
"alac", 4);
1555 extradata_size = 30;
1557 if (extradata ==
NULL)
1569 extradata_offset = 26;
1585 if (flavor <= 0 || track->audio.coded_framesize <= 0 ||
1595 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1600 extradata_offset = 78;
1607 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
1615 if (strcmp(track->
language,
"und"))
1655 #if FF_API_R_FRAME_RATE
1672 attachements = attachements_list->
elem;
1673 for (j=0; j<attachements_list->
nb_elem; j++) {
1674 if (!(attachements[j].filename && attachements[j].mime &&
1675 attachements[j].bin.data && attachements[j].
bin.
size > 0)) {
1698 attachements[j].
stream = st;
1702 chapters = chapters_list->
elem;
1703 for (i=0; i<chapters_list->
nb_elem; i++)
1705 && (max_start==0 || chapters[i].start > max_start)) {
1708 chapters[i].
start, chapters[i].
end,
1711 "title", chapters[i].
title, 0);
1712 max_start = chapters[i].
start;
1737 matroska->
packets = newpackets;
1767 int* buf_size,
int type,
1768 uint32_t **lace_buf,
int *laces)
1770 int res = 0, n,
size = *buf_size;
1772 uint32_t *lace_size;
1780 *lace_buf[0] =
size;
1788 lace_size =
av_mallocz(*laces *
sizeof(
int));
1796 for (n = 0; res == 0 && n < *laces - 1; n++) {
1803 lace_size[n] += temp;
1809 total += lace_size[n];
1811 if (size <= total) {
1816 lace_size[n] = size - total;
1821 if (size % (*laces)) {
1825 for (n = 0; n < *laces; n++)
1826 lace_size[n] = size / *laces;
1835 "EBML block data error\n");
1841 total = lace_size[0] = num;
1842 for (n = 1; res == 0 && n < *laces - 1; n++) {
1848 "EBML block data error\n");
1854 lace_size[n] = lace_size[n - 1] + snum;
1855 total += lace_size[n];
1857 if (size <= total) {
1861 lace_size[*laces - 1] = size - total;
1867 *lace_buf = lace_size;
1877 uint64_t timecode, uint64_t
duration,
1892 if (size < cfs * h / 2) {
1894 "Corrupt int4 RM-style audio packet size\n");
1897 for (x=0; x<h/2; x++)
1898 memcpy(track->
audio.
buf+x*2*w+y*cfs,
1903 "Corrupt sipr RM-style audio packet size\n");
1906 memcpy(track->
audio.
buf + y*w, data, w);
1908 if (size < sps * w / sps) {
1910 "Corrupt generic RM-style audio packet size\n");
1913 for (x=0; x<w/sps; x++)
1914 memcpy(track->
audio.
buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
1943 uint64_t timecode, uint64_t
duration,
1944 int64_t pos,
int is_keyframe)
1948 int offset = 0, res;
1951 if (encodings && encodings->
scope & 1) {
1969 bytestream_put_be32(&buf, pkt_size);
1970 bytestream_put_be32(&buf,
MKBETAG(
'i',
'c',
'p',
'f'));
1973 memcpy(pkt->
data + offset, pkt_data, pkt_size);
1975 if (pkt_data != data)
1978 pkt->
flags = is_keyframe;
1982 pkt->
dts = timecode;
1984 pkt->
pts = timecode;
2009 int size, int64_t pos, uint64_t cluster_time,
2010 uint64_t block_duration,
int is_keyframe,
2011 int64_t cluster_pos)
2018 uint32_t *lace_size =
NULL;
2019 int n,
flags, laces = 0;
2030 if (!track || !track->
stream) {
2032 "Invalid stream %"PRIu64
" or size %u\n", num, size);
2034 }
else if (size <= 3)
2044 if (is_keyframe == -1)
2047 if (cluster_time != (uint64_t)-1
2048 && (block_time >= 0 || cluster_time >= -block_time)) {
2049 timecode = cluster_time + block_time;
2051 && timecode < track->end_timecode)
2058 if (!is_keyframe || timecode < matroska->skip_to_timecode)
2064 &lace_size, &laces);
2070 duration = block_duration / laces;
2071 if (block_duration != duration * laces) {
2073 "Incorrect block_duration, possibly corrupted container");
2077 block_duration = duration * laces;
2084 for (n = 0; n < laces; n++) {
2093 timecode, duration, pos);
2100 pos, !n? is_keyframe : 0);
2107 data += lace_size[n];
2121 matroska_cluster_incremental_parsing,
2136 matroska_clusters_incremental,
2141 matroska_cluster_incremental_parsing,
2149 blocks = blocks_list->
elem;
2153 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2155 if (!blocks[i].non_simple)
2158 blocks[i].bin.data, blocks[i].
bin.
size,
2166 if (res < 0) matroska->
done = 1;
2183 res =
ebml_parse(matroska, matroska_clusters, &cluster);
2184 blocks_list = &cluster.
blocks;
2185 blocks = blocks_list->
elem;
2186 for (i=0; i<blocks_list->
nb_elem && !res; i++)
2187 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2189 if (!blocks[i].non_simple)
2192 blocks[i].bin.data, blocks[i].
bin.
size,
2198 if (res < 0) matroska->
done = 1;
2222 int64_t timestamp,
int flags)
2227 int i,
index, index_sub, index_min;
2265 index_min = index_sub;
2296 .
name =
"matroska,webm",