662 int max_size, uint64_t *number)
675 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
684 if (read > max_size) {
687 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
695 total = (total << 8) |
avio_r8(pb);
711 if (res > 0 && *number + 1 == 1ULL << (7 * res))
712 *number = 0xffffffffffffffULL;
730 *num = (*num << 8) |
avio_r8(pb);
743 }
else if (size == 4) {
745 }
else if (size == 8){
846 *num = unum - ((1LL << (7*res - 1)) - 1);
855 uint32_t
id,
void *
data)
858 for (i=0; syntax[i].
id; i++)
859 if (
id == syntax[i].
id)
891 for (i=0; syntax[i].
id; i++)
892 switch (syntax[i].type) {
894 *(uint64_t *)((
char *)data+syntax[i].
data_offset) = syntax[i].def.
u;
897 *(
double *)((
char *)data+syntax[i].data_offset) = syntax[i].
def.
f;
925 uint32_t
id = syntax->
id;
936 list->
elem = newelem;
946 if (max_lengths[syntax->
type] && length > max_lengths[syntax->
type]) {
948 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for syntax element %i\n",
949 length, max_lengths[syntax->
type], syntax->
type);
954 switch (syntax->
type) {
982 for (i=0; syntax[i].
id; i++) {
983 void *data_off = (
char *)data + syntax[i].data_offset;
984 switch (syntax[i].type) {
989 if (syntax[i].list_elem_size) {
991 char *ptr = list->
elem;
1009 int len_mask = 0x80,
size = 1, n = 1, i;
1017 while (
size <= 8 && !(total & len_mask)) {
1023 total &= (len_mask - 1);
1025 total = (total << 8) | p->
buf[4 + n++];
1037 if (total < probelen)
1039 for (n = 4+
size; n <= 4+
size+total-probelen; n++)
1055 if (tracks[i].num == num)
1067 int isize = *buf_size;
1070 int pkt_size = isize;
1074 if (pkt_size >= 10000000
U)
1077 switch (encodings[0].compression.algo) {
1082 if (header_size && !header) {
1090 pkt_size = isize + header_size;
1095 memcpy(pkt_data, header, header_size);
1096 memcpy(pkt_data + header_size, data, isize);
1102 olen = pkt_size *= 3;
1108 pkt_data = newpktdata;
1120 z_stream zstream = {0};
1121 if (inflateInit(&zstream) != Z_OK)
1123 zstream.next_in =
data;
1124 zstream.avail_in = isize;
1129 inflateEnd(&zstream);
1132 pkt_data = newpktdata;
1133 zstream.avail_out = pkt_size - zstream.total_out;
1134 zstream.next_out = pkt_data + zstream.total_out;
1136 result = inflate(&zstream, Z_NO_FLUSH);
1138 result = Z_MEM_ERROR;
1139 }
while (result==Z_OK && pkt_size<10000000);
1140 pkt_size = zstream.total_out;
1141 inflateEnd(&zstream);
1142 if (result != Z_STREAM_END) {
1143 if (result == Z_MEM_ERROR)
1154 bz_stream bzstream = {0};
1155 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1157 bzstream.next_in =
data;
1158 bzstream.avail_in = isize;
1163 BZ2_bzDecompressEnd(&bzstream);
1166 pkt_data = newpktdata;
1167 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1168 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1170 result = BZ2_bzDecompress(&bzstream);
1172 result = BZ_MEM_ERROR;
1173 }
while (result==BZ_OK && pkt_size<10000000);
1174 pkt_size = bzstream.total_out_lo32;
1175 BZ2_bzDecompressEnd(&bzstream);
1176 if (result != BZ_STREAM_END) {
1177 if (result == BZ_MEM_ERROR)
1191 *buf_size = pkt_size;
1202 for (; *ptr!=
',' && ptr<
end-1; ptr++);
1206 for (; *ptr!=
',' && ptr<end-1; ptr++);
1208 int64_t end_pts = pkt->
pts + display_duration;
1210 int ec = matroska->
time_scale * end_pts / 10000000;
1211 int sh, sm, ss, eh, em, es,
len;
1212 sh = sc/360000; sc -= 360000*sh;
1213 sm = sc/ 6000; sc -= 6000*sm;
1214 ss = sc/ 100; sc -= 100*ss;
1215 eh = ec/360000; ec -= 360000*eh;
1216 em = ec/ 6000; ec -= 6000*em;
1217 es = ec/ 100; ec -= 100*es;
1222 snprintf(line,len,
"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
1223 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
1226 pkt->
size = strlen(line);
1250 for (i=0; i < list->
nb_elem; i++) {
1251 const char *lang= (tags[i].
lang && strcmp(tags[i].lang,
"und")) ? tags[i].lang :
NULL;
1253 if (!tags[i].
name) {
1257 if (prefix)
snprintf(key,
sizeof(key),
"%s/%s", prefix, tags[i].name);
1258 else av_strlcpy(key, tags[i].name,
sizeof(key));
1259 if (tags[i].def || !lang) {
1261 if (tags[i].sub.nb_elem)
1268 if (tags[i].sub.nb_elem)
1282 if (tags[i].target.attachuid) {
1285 if (attachment[j].uid == tags[i].target.attachuid
1288 &attachment[j].stream->metadata,
NULL);
1289 }
else if (tags[i].target.chapteruid) {
1292 if (chapter[j].uid == tags[i].target.chapteruid
1295 &chapter[j].chapter->metadata,
NULL);
1296 }
else if (tags[i].target.trackuid) {
1299 if (track[j].uid == tags[i].target.trackuid && track[j].
stream)
1301 &track[j].stream->metadata,
NULL);
1313 uint32_t level_up = matroska->
level_up;
1320 if (idx >= seekhead_list->
nb_elem
1332 "Max EBML element depth (%d) reached, "
1337 level.
length = (uint64_t)-1;
1342 ret =
ebml_parse(matroska, matroska_segment, matroska);
1347 if (length == (uint64_t)-1)
1371 for (i = 0; i < seekhead_list->
nb_elem; i++) {
1373 if (seekhead[i].pos <= before_pos)
1393 int index_scale = 1;
1396 index_list = &matroska->
index;
1397 index = index_list->
elem;
1403 for (i = 0; i < index_list->
nb_elem; i++) {
1406 for (j = 0; j < pos_list->
nb_elem; j++) {
1408 if (track && track->stream)
1411 index[i].
time/index_scale, 0, 0,
1422 for (i = 0; i < seekhead_list->
nb_elem; i++)
1425 assert(i <= seekhead_list->nb_elem);
1434 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
1438 if (strstr(codec_id, aac_profiles[profile]))
1457 time_t creation_time = date_utc / 1000000000 + 978307200;
1458 struct tm *ptm = gmtime(&creation_time);
1460 strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", ptm);
1461 av_dict_set(metadata,
"creation_time", buffer, 0);
1472 uint64_t max_start = 0;
1485 "EBML header using unsupported features\n"
1486 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1492 "EBML header using unsupported features\n"
1493 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1510 res =
ebml_parse(matroska, matroska_segments, matroska);
1517 res =
ebml_parse(matroska, matroska_segment, matroska);
1538 int extradata_size = 0;
1539 int extradata_offset = 0;
1540 uint32_t fourcc = 0;
1548 "Unknown or unsupported track type %"PRIu64
"\n",
1568 if (encodings_list->
nb_elem > 1) {
1570 "Multiple combined encodings not supported");
1571 }
else if (encodings_list->
nb_elem == 1) {
1572 if (encodings[0].type ||
1584 encodings[0].
scope = 0;
1586 "Unsupported encoding type");
1596 "Failed to decode codec private data\n");
1616 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC")
1622 extradata_offset = 40;
1623 }
else if (!strcmp(track->
codec_id,
"A_MS/ACM")
1634 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME")
1645 if (extradata ==
NULL)
1647 AV_WB32(extradata, extradata_size);
1648 memcpy(&extradata[4],
"alac", 4);
1669 if (extradata ==
NULL)
1671 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1672 extradata[1] = ((sri&0x01) << 7) | (track->
audio.
channels<<3);
1673 if (strstr(track->
codec_id,
"SBR")) {
1675 extradata[2] = 0x56;
1676 extradata[3] = 0xE5;
1677 extradata[4] = 0x80 | (sri<<3);
1682 extradata_size = 30;
1684 if (extradata ==
NULL)
1696 extradata_offset = 26;
1720 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1725 extradata_offset = 78;
1732 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
1740 if (strcmp(track->
language,
"und"))
1781 #if FF_API_R_FRAME_RATE
1799 snprintf(buf,
sizeof(buf),
"%s_%d",
1802 if (planes[j].uid == tracks[k].uid) {
1804 "stereo_mode", buf, 0);
1822 attachements = attachements_list->
elem;
1823 for (j=0; j<attachements_list->
nb_elem; j++) {
1824 if (!(attachements[j].filename && attachements[j].mime &&
1825 attachements[j].bin.data && attachements[j].
bin.
size > 0)) {
1848 attachements[j].
stream = st;
1852 chapters = chapters_list->
elem;
1853 for (i=0; i<chapters_list->
nb_elem; i++)
1855 && (max_start==0 || chapters[i].start > max_start)) {
1858 chapters[i].
start, chapters[i].
end,
1861 "title", chapters[i].
title, 0);
1862 max_start = chapters[i].
start;
1889 matroska->
packets = newpackets;
1919 int* buf_size,
int type,
1920 uint32_t **lace_buf,
int *laces)
1922 int res = 0, n,
size = *buf_size;
1924 uint32_t *lace_size;
1932 *lace_buf[0] =
size;
1940 lace_size =
av_mallocz(*laces *
sizeof(
int));
1948 for (n = 0; res == 0 && n < *laces - 1; n++) {
1955 lace_size[n] +=
temp;
1961 total += lace_size[n];
1963 if (size <= total) {
1968 lace_size[n] = size - total;
1973 if (size % (*laces)) {
1977 for (n = 0; n < *laces; n++)
1978 lace_size[n] = size / *laces;
1987 "EBML block data error\n");
1993 total = lace_size[0] = num;
1994 for (n = 1; res == 0 && n < *laces - 1; n++) {
2000 "EBML block data error\n");
2006 lace_size[n] = lace_size[n - 1] + snum;
2007 total += lace_size[n];
2009 if (size <= total) {
2013 lace_size[*laces - 1] = size - total;
2019 *lace_buf = lace_size;
2044 if (size < cfs * h / 2) {
2046 "Corrupt int4 RM-style audio packet size\n");
2049 for (x=0; x<h/2; x++)
2050 memcpy(track->
audio.
buf+x*2*w+y*cfs,
2055 "Corrupt sipr RM-style audio packet size\n");
2058 memcpy(track->
audio.
buf + y*w, data, w);
2060 if (size < sps * w / sps || h<=0) {
2062 "Corrupt generic RM-style audio packet size\n");
2065 for (x=0; x<w/sps; x++)
2066 memcpy(track->
audio.
buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2098 uint64_t timecode, uint64_t lace_duration,
2099 int64_t pos,
int is_keyframe,
2100 uint8_t *additional, uint64_t additional_id,
int additional_size)
2107 if (encodings && encodings->
scope & 1) {
2125 bytestream_put_be32(&buf, pkt_size);
2126 bytestream_put_be32(&buf,
MKBETAG(
'i',
'c',
'p',
'f'));
2129 memcpy(pkt->
data + offset, pkt_data, pkt_size);
2131 if (pkt_data != data)
2134 pkt->
flags = is_keyframe;
2137 if (additional_size > 0) {
2140 additional_size + 8);
2141 if(side_data ==
NULL) {
2144 AV_WB64(side_data, additional_id);
2145 memcpy(side_data + 8, additional, additional_size);
2149 pkt->
dts = timecode;
2151 pkt->
pts = timecode;
2166 lace_duration <= INT_MAX) {
2195 int size, int64_t pos, uint64_t cluster_time,
2196 uint64_t block_duration,
int is_keyframe,
2197 uint8_t *additional, uint64_t additional_id,
int additional_size,
2198 int64_t cluster_pos)
2205 uint32_t *lace_size =
NULL;
2206 int n,
flags, laces = 0;
2217 if (!track || !track->
stream) {
2219 "Invalid stream %"PRIu64
" or size %u\n", num, size);
2221 }
else if (size <= 3)
2232 if (is_keyframe == -1)
2235 if (cluster_time != (uint64_t)-1
2236 && (block_time >= 0 || cluster_time >= -block_time)) {
2237 timecode = cluster_time + block_time;
2239 && timecode < track->end_timecode)
2246 if (timecode < matroska->skip_to_timecode)
2257 &lace_size, &laces);
2262 if (!block_duration)
2265 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
2269 for (n = 0; n < laces; n++) {
2270 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
2272 if (lace_size[n] > size) {
2290 timecode, lace_duration,
2291 pos, !n? is_keyframe : 0,
2292 additional, additional_id, additional_size);
2298 timecode = lace_duration ? timecode + lace_duration :
AV_NOPTS_VALUE;
2299 data += lace_size[n];
2300 size -= lace_size[n];
2314 matroska_cluster_incremental_parsing,
2329 matroska_clusters_incremental,
2334 matroska_cluster_incremental_parsing,
2342 blocks = blocks_list->
elem;
2346 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2350 if (!blocks[i].non_simple)
2353 blocks[i].bin.data, blocks[i].
bin.
size,
2379 res =
ebml_parse(matroska, matroska_clusters, &cluster);
2380 blocks_list = &cluster.
blocks;
2381 blocks = blocks_list->
elem;
2382 for (i=0; i<blocks_list->
nb_elem; i++)
2383 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2386 blocks[i].bin.data, blocks[i].
bin.
size,
2411 int64_t timestamp,
int flags)
2416 int i,
index, index_sub, index_min;
2454 index_min = index_sub;
2502 .
name =
"matroska,webm",