645 int max_size, uint64_t *number)
658 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
667 if (read > max_size) {
670 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
678 total = (total << 8) |
avio_r8(pb);
694 if (res > 0 && *number + 1 == 1ULL << (7 * res))
695 *number = 0xffffffffffffffULL;
713 *num = (*num << 8) |
avio_r8(pb);
726 }
else if (size == 4) {
728 }
else if (size == 8){
829 *num = unum - ((1LL << (7*res - 1)) - 1);
838 uint32_t
id,
void *
data)
841 for (i=0; syntax[i].
id; i++)
842 if (
id == syntax[i].
id)
874 for (i=0; syntax[i].
id; i++)
875 switch (syntax[i].type) {
877 *(uint64_t *)((
char *)data+syntax[i].
data_offset) = syntax[i].def.
u;
880 *(
double *)((
char *)data+syntax[i].data_offset) = syntax[i].
def.
f;
908 uint32_t
id = syntax->
id;
919 list->
elem = newelem;
929 if (max_lengths[syntax->
type] && length > max_lengths[syntax->
type]) {
931 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for syntax element %i\n",
932 length, max_lengths[syntax->
type], syntax->
type);
937 switch (syntax->
type) {
965 for (i=0; syntax[i].
id; i++) {
966 void *data_off = (
char *)data + syntax[i].data_offset;
967 switch (syntax[i].type) {
972 if (syntax[i].list_elem_size) {
974 char *ptr = list->
elem;
992 int len_mask = 0x80,
size = 1, n = 1, i;
1000 while (
size <= 8 && !(total & len_mask)) {
1006 total &= (len_mask - 1);
1008 total = (total << 8) | p->
buf[4 + n++];
1020 if (total < probelen)
1022 for (n = 4+
size; n <= 4+
size+total-probelen; n++)
1038 if (tracks[i].num == num)
1050 int isize = *buf_size;
1053 int pkt_size = isize;
1057 if (pkt_size >= 10000000
U)
1060 switch (encodings[0].compression.algo) {
1065 if (header_size && !header) {
1073 pkt_size = isize + header_size;
1078 memcpy(pkt_data, header, header_size);
1079 memcpy(pkt_data + header_size, data, isize);
1085 olen = pkt_size *= 3;
1091 pkt_data = newpktdata;
1103 z_stream zstream = {0};
1104 if (inflateInit(&zstream) != Z_OK)
1106 zstream.next_in =
data;
1107 zstream.avail_in = isize;
1112 inflateEnd(&zstream);
1115 pkt_data = newpktdata;
1116 zstream.avail_out = pkt_size - zstream.total_out;
1117 zstream.next_out = pkt_data + zstream.total_out;
1119 result = inflate(&zstream, Z_NO_FLUSH);
1121 result = Z_MEM_ERROR;
1122 }
while (result==Z_OK && pkt_size<10000000);
1123 pkt_size = zstream.total_out;
1124 inflateEnd(&zstream);
1125 if (result != Z_STREAM_END) {
1126 if (result == Z_MEM_ERROR)
1137 bz_stream bzstream = {0};
1138 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1140 bzstream.next_in =
data;
1141 bzstream.avail_in = isize;
1146 BZ2_bzDecompressEnd(&bzstream);
1149 pkt_data = newpktdata;
1150 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1151 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1153 result = BZ2_bzDecompress(&bzstream);
1155 result = BZ_MEM_ERROR;
1156 }
while (result==BZ_OK && pkt_size<10000000);
1157 pkt_size = bzstream.total_out_lo32;
1158 BZ2_bzDecompressEnd(&bzstream);
1159 if (result != BZ_STREAM_END) {
1160 if (result == BZ_MEM_ERROR)
1174 *buf_size = pkt_size;
1185 for (; *ptr!=
',' && ptr<
end-1; ptr++);
1189 for (; *ptr!=
',' && ptr<end-1; ptr++);
1191 int64_t end_pts = pkt->
pts + display_duration;
1193 int ec = matroska->
time_scale * end_pts / 10000000;
1194 int sh, sm, ss, eh, em, es,
len;
1195 sh = sc/360000; sc -= 360000*sh;
1196 sm = sc/ 6000; sc -= 6000*sm;
1197 ss = sc/ 100; sc -= 100*ss;
1198 eh = ec/360000; ec -= 360000*eh;
1199 em = ec/ 6000; ec -= 6000*em;
1200 es = ec/ 100; ec -= 100*es;
1205 snprintf(line,len,
"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
1206 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
1209 pkt->
size = strlen(line);
1233 for (i=0; i < list->
nb_elem; i++) {
1234 const char *lang= (tags[i].
lang && strcmp(tags[i].lang,
"und")) ? tags[i].lang :
NULL;
1236 if (!tags[i].
name) {
1240 if (prefix)
snprintf(key,
sizeof(key),
"%s/%s", prefix, tags[i].name);
1241 else av_strlcpy(key, tags[i].name,
sizeof(key));
1242 if (tags[i].def || !lang) {
1244 if (tags[i].sub.nb_elem)
1251 if (tags[i].sub.nb_elem)
1265 if (tags[i].target.attachuid) {
1268 if (attachment[j].uid == tags[i].target.attachuid
1271 &attachment[j].stream->metadata,
NULL);
1272 }
else if (tags[i].target.chapteruid) {
1275 if (chapter[j].uid == tags[i].target.chapteruid
1278 &chapter[j].chapter->metadata,
NULL);
1279 }
else if (tags[i].target.trackuid) {
1282 if (track[j].uid == tags[i].target.trackuid && track[j].
stream)
1284 &track[j].stream->metadata,
NULL);
1296 uint32_t level_up = matroska->
level_up;
1303 if (idx >= seekhead_list->
nb_elem
1315 "Max EBML element depth (%d) reached, "
1320 level.
length = (uint64_t)-1;
1325 ret =
ebml_parse(matroska, matroska_segment, matroska);
1330 if (length == (uint64_t)-1)
1354 for (i = 0; i < seekhead_list->
nb_elem; i++) {
1356 if (seekhead[i].pos <= before_pos)
1376 int index_scale = 1;
1379 index_list = &matroska->
index;
1380 index = index_list->
elem;
1386 for (i = 0; i < index_list->
nb_elem; i++) {
1389 for (j = 0; j < pos_list->
nb_elem; j++) {
1391 if (track && track->stream)
1394 index[i].
time/index_scale, 0, 0,
1405 for (i = 0; i < seekhead_list->
nb_elem; i++)
1408 assert(i <= seekhead_list->nb_elem);
1417 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
1421 if (strstr(codec_id, aac_profiles[profile]))
1440 time_t creation_time = date_utc / 1000000000 + 978307200;
1441 struct tm *ptm = gmtime(&creation_time);
1443 strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", ptm);
1444 av_dict_set(metadata,
"creation_time", buffer, 0);
1455 uint64_t max_start = 0;
1468 "EBML header using unsupported features\n"
1469 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1475 "EBML header using unsupported features\n"
1476 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1493 res =
ebml_parse(matroska, matroska_segments, matroska);
1500 res =
ebml_parse(matroska, matroska_segment, matroska);
1521 int extradata_size = 0;
1522 int extradata_offset = 0;
1523 uint32_t fourcc = 0;
1531 "Unknown or unsupported track type %"PRIu64
"\n",
1551 if (encodings_list->
nb_elem > 1) {
1553 "Multiple combined encodings not supported");
1554 }
else if (encodings_list->
nb_elem == 1) {
1555 if (encodings[0].type ||
1567 encodings[0].
scope = 0;
1569 "Unsupported encoding type");
1579 "Failed to decode codec private data\n");
1599 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC")
1605 extradata_offset = 40;
1606 }
else if (!strcmp(track->
codec_id,
"A_MS/ACM")
1617 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME")
1628 if (extradata ==
NULL)
1630 AV_WB32(extradata, extradata_size);
1631 memcpy(&extradata[4],
"alac", 4);
1652 if (extradata ==
NULL)
1654 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1655 extradata[1] = ((sri&0x01) << 7) | (track->
audio.
channels<<3);
1656 if (strstr(track->
codec_id,
"SBR")) {
1658 extradata[2] = 0x56;
1659 extradata[3] = 0xE5;
1660 extradata[4] = 0x80 | (sri<<3);
1665 extradata_size = 30;
1667 if (extradata ==
NULL)
1679 extradata_offset = 26;
1703 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1708 extradata_offset = 78;
1715 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
1723 if (strcmp(track->
language,
"und"))
1764 #if FF_API_R_FRAME_RATE
1778 snprintf(buf,
sizeof(buf),
"%s_%d",
1781 if (planes[j].uid == tracks[k].uid) {
1783 "stereo_mode", buf, 0);
1800 attachements = attachements_list->
elem;
1801 for (j=0; j<attachements_list->
nb_elem; j++) {
1802 if (!(attachements[j].filename && attachements[j].mime &&
1803 attachements[j].bin.data && attachements[j].
bin.
size > 0)) {
1826 attachements[j].
stream = st;
1830 chapters = chapters_list->
elem;
1831 for (i=0; i<chapters_list->
nb_elem; i++)
1833 && (max_start==0 || chapters[i].start > max_start)) {
1836 chapters[i].
start, chapters[i].
end,
1839 "title", chapters[i].
title, 0);
1840 max_start = chapters[i].
start;
1867 matroska->
packets = newpackets;
1897 int* buf_size,
int type,
1898 uint32_t **lace_buf,
int *laces)
1900 int res = 0, n,
size = *buf_size;
1902 uint32_t *lace_size;
1910 *lace_buf[0] =
size;
1918 lace_size =
av_mallocz(*laces *
sizeof(
int));
1926 for (n = 0; res == 0 && n < *laces - 1; n++) {
1933 lace_size[n] +=
temp;
1939 total += lace_size[n];
1941 if (size <= total) {
1946 lace_size[n] = size - total;
1951 if (size % (*laces)) {
1955 for (n = 0; n < *laces; n++)
1956 lace_size[n] = size / *laces;
1965 "EBML block data error\n");
1971 total = lace_size[0] = num;
1972 for (n = 1; res == 0 && n < *laces - 1; n++) {
1978 "EBML block data error\n");
1984 lace_size[n] = lace_size[n - 1] + snum;
1985 total += lace_size[n];
1987 if (size <= total) {
1991 lace_size[*laces - 1] = size - total;
1997 *lace_buf = lace_size;
2022 if (size < cfs * h / 2) {
2024 "Corrupt int4 RM-style audio packet size\n");
2027 for (x=0; x<h/2; x++)
2028 memcpy(track->
audio.
buf+x*2*w+y*cfs,
2033 "Corrupt sipr RM-style audio packet size\n");
2036 memcpy(track->
audio.
buf + y*w, data, w);
2038 if (size < sps * w / sps || h<=0) {
2040 "Corrupt generic RM-style audio packet size\n");
2043 for (x=0; x<w/sps; x++)
2044 memcpy(track->
audio.
buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2076 uint64_t timecode, uint64_t lace_duration,
2077 int64_t pos,
int is_keyframe)
2084 if (encodings && encodings->
scope & 1) {
2102 bytestream_put_be32(&buf, pkt_size);
2103 bytestream_put_be32(&buf,
MKBETAG(
'i',
'c',
'p',
'f'));
2106 memcpy(pkt->
data + offset, pkt_data, pkt_size);
2108 if (pkt_data != data)
2111 pkt->
flags = is_keyframe;
2115 pkt->
dts = timecode;
2117 pkt->
pts = timecode;
2132 lace_duration <= INT_MAX) {
2161 int size, int64_t pos, uint64_t cluster_time,
2162 uint64_t block_duration,
int is_keyframe,
2163 int64_t cluster_pos)
2170 uint32_t *lace_size =
NULL;
2171 int n,
flags, laces = 0;
2182 if (!track || !track->
stream) {
2184 "Invalid stream %"PRIu64
" or size %u\n", num, size);
2186 }
else if (size <= 3)
2197 if (is_keyframe == -1)
2200 if (cluster_time != (uint64_t)-1
2201 && (block_time >= 0 || cluster_time >= -block_time)) {
2202 timecode = cluster_time + block_time;
2204 && timecode < track->end_timecode)
2211 if (timecode < matroska->skip_to_timecode)
2222 &lace_size, &laces);
2227 if (!block_duration)
2230 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
2234 for (n = 0; n < laces; n++) {
2235 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
2237 if (lace_size[n] > size) {
2256 timecode, lace_duration,
2257 pos, !n? is_keyframe : 0);
2263 timecode = lace_duration ? timecode + lace_duration :
AV_NOPTS_VALUE;
2264 data += lace_size[n];
2265 size -= lace_size[n];
2279 matroska_cluster_incremental_parsing,
2294 matroska_clusters_incremental,
2299 matroska_cluster_incremental_parsing,
2307 blocks = blocks_list->
elem;
2311 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2313 if (!blocks[i].non_simple)
2316 blocks[i].bin.data, blocks[i].
bin.
size,
2340 res =
ebml_parse(matroska, matroska_clusters, &cluster);
2341 blocks_list = &cluster.
blocks;
2342 blocks = blocks_list->
elem;
2343 for (i=0; i<blocks_list->
nb_elem; i++)
2344 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2347 blocks[i].bin.data, blocks[i].
bin.
size,
2372 int64_t timestamp,
int flags)
2377 int i,
index, index_sub, index_min;
2415 index_min = index_sub;
2458 .
name =
"matroska,webm",