42 #include "libavutil/intfloat.h"
43 #include "libavutil/intreadwrite.h"
44 #include "libavutil/avstring.h"
45 #include "libavutil/lzo.h"
46 #include "libavutil/dict.h"
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)
1698 extradata_offset = 26;
1722 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1727 extradata_offset = 78;
1734 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
1742 if (strcmp(track->
language,
"und"))
1783 #if FF_API_R_FRAME_RATE
1801 snprintf(buf,
sizeof(buf),
"%s_%d",
1804 if (planes[j].uid == tracks[k].uid) {
1806 "stereo_mode", buf, 0);
1824 attachements = attachements_list->
elem;
1825 for (j=0; j<attachements_list->
nb_elem; j++) {
1826 if (!(attachements[j].filename && attachements[j].mime &&
1827 attachements[j].bin.data && attachements[j].
bin.
size > 0)) {
1850 attachements[j].
stream = st;
1854 chapters = chapters_list->
elem;
1855 for (i=0; i<chapters_list->
nb_elem; i++)
1857 && (max_start==0 || chapters[i].start > max_start)) {
1860 chapters[i].
start, chapters[i].
end,
1863 "title", chapters[i].
title, 0);
1864 max_start = chapters[i].
start;
1891 matroska->
packets = newpackets;
1921 int* buf_size,
int type,
1922 uint32_t **lace_buf,
int *laces)
1924 int res = 0, n,
size = *buf_size;
1926 uint32_t *lace_size;
1934 *lace_buf[0] =
size;
1942 lace_size =
av_mallocz(*laces *
sizeof(
int));
1950 for (n = 0; res == 0 && n < *laces - 1; n++) {
1957 lace_size[n] +=
temp;
1963 total += lace_size[n];
1965 if (size <= total) {
1970 lace_size[n] = size - total;
1975 if (size % (*laces)) {
1979 for (n = 0; n < *laces; n++)
1980 lace_size[n] = size / *laces;
1989 "EBML block data error\n");
1995 total = lace_size[0] = num;
1996 for (n = 1; res == 0 && n < *laces - 1; n++) {
2002 "EBML block data error\n");
2008 lace_size[n] = lace_size[n - 1] + snum;
2009 total += lace_size[n];
2011 if (size <= total) {
2015 lace_size[*laces - 1] = size - total;
2021 *lace_buf = lace_size;
2046 if (size < cfs * h / 2) {
2048 "Corrupt int4 RM-style audio packet size\n");
2051 for (x=0; x<h/2; x++)
2052 memcpy(track->
audio.
buf+x*2*w+y*cfs,
2057 "Corrupt sipr RM-style audio packet size\n");
2060 memcpy(track->
audio.
buf + y*w, data, w);
2062 if (size < sps * w / sps || h<=0) {
2064 "Corrupt generic RM-style audio packet size\n");
2067 for (x=0; x<w/sps; x++)
2068 memcpy(track->
audio.
buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2100 uint64_t timecode, uint64_t lace_duration,
2101 int64_t pos,
int is_keyframe,
2102 uint8_t *additional, uint64_t additional_id,
int additional_size)
2109 if (encodings && encodings->
scope & 1) {
2127 bytestream_put_be32(&buf, pkt_size);
2128 bytestream_put_be32(&buf,
MKBETAG(
'i',
'c',
'p',
'f'));
2131 memcpy(pkt->
data + offset, pkt_data, pkt_size);
2133 if (pkt_data != data)
2136 pkt->
flags = is_keyframe;
2139 if (additional_size > 0) {
2142 additional_size + 8);
2143 if(side_data ==
NULL) {
2146 AV_WB64(side_data, additional_id);
2147 memcpy(side_data + 8, additional, additional_size);
2151 pkt->
dts = timecode;
2153 pkt->
pts = timecode;
2168 lace_duration <= INT_MAX) {
2197 int size, int64_t pos, uint64_t cluster_time,
2198 uint64_t block_duration,
int is_keyframe,
2199 uint8_t *additional, uint64_t additional_id,
int additional_size,
2200 int64_t cluster_pos)
2207 uint32_t *lace_size =
NULL;
2208 int n,
flags, laces = 0;
2210 int trust_default_duration = 1;
2220 if (!track || !track->
stream) {
2222 "Invalid stream %"PRIu64
" or size %u\n", num, size);
2224 }
else if (size <= 3)
2235 if (is_keyframe == -1)
2238 if (cluster_time != (uint64_t)-1
2239 && (block_time >= 0 || cluster_time >= -block_time)) {
2240 timecode = cluster_time + block_time;
2242 && timecode < track->end_timecode)
2249 if (timecode < matroska->skip_to_timecode)
2260 &lace_size, &laces);
2269 trust_default_duration = 0;
2273 if (!block_duration && trust_default_duration)
2276 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
2280 for (n = 0; n < laces; n++) {
2281 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
2283 if (lace_size[n] > size) {
2301 timecode, lace_duration,
2302 pos, !n? is_keyframe : 0,
2303 additional, additional_id, additional_size);
2309 timecode = lace_duration ? timecode + lace_duration :
AV_NOPTS_VALUE;
2310 data += lace_size[n];
2311 size -= lace_size[n];
2325 matroska_cluster_incremental_parsing,
2340 matroska_clusters_incremental,
2345 matroska_cluster_incremental_parsing,
2353 blocks = blocks_list->
elem;
2357 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2361 if (!blocks[i].non_simple)
2364 blocks[i].bin.data, blocks[i].
bin.
size,
2390 res =
ebml_parse(matroska, matroska_clusters, &cluster);
2391 blocks_list = &cluster.
blocks;
2392 blocks = blocks_list->
elem;
2393 for (i=0; i<blocks_list->
nb_elem; i++)
2394 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2397 blocks[i].bin.data, blocks[i].
bin.
size,
2422 int64_t timestamp,
int flags)
2427 int i,
index, index_sub, index_min;
2462 while(index_sub >= 0
2464 && tracks[i].stream->index_entries[index_sub].pos < st->
index_entries[index_min].
pos
2514 .
name =
"matroska,webm",