642 if (
avio_seek(pb, last_pos + 1, SEEK_SET) < 0)
690 int max_size, uint64_t *number)
703 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
712 if (read > max_size) {
715 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
723 total = (total << 8) |
avio_r8(pb);
739 if (res > 0 && *number + 1 == 1ULL << (7 * res))
740 *number = 0xffffffffffffffULL;
758 *num = (*num << 8) |
avio_r8(pb);
781 *num = (*num << 8) |
avio_r8(pb);
795 }
else if (size == 4) {
797 }
else if (size == 8){
898 *num = unum - ((1LL << (7*res - 1)) - 1);
907 uint32_t
id,
void *
data)
910 for (i=0; syntax[i].
id; i++)
911 if (
id == syntax[i].
id)
943 for (i=0; syntax[i].
id; i++)
944 switch (syntax[i].
type) {
946 *(uint64_t *)((
char *)data+syntax[i].
data_offset) = syntax[i].def.
u;
949 *(
double *)((
char *)data+syntax[i].data_offset) = syntax[i].
def.
f;
954 if (syntax[i].def.
s) {
983 uint32_t
id = syntax->
id;
994 list->
elem = newelem;
1004 if (max_lengths[syntax->
type] && length > max_lengths[syntax->
type]) {
1006 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for syntax element %i\n",
1007 length, max_lengths[syntax->
type], syntax->
type);
1012 switch (syntax->
type) {
1041 for (i=0; syntax[i].
id; i++) {
1042 void *data_off = (
char *)data + syntax[i].data_offset;
1043 switch (syntax[i].
type) {
1048 if (syntax[i].list_elem_size) {
1050 char *ptr = list->
elem;
1068 int len_mask = 0x80,
size = 1,
n = 1, i;
1076 while (
size <= 8 && !(total & len_mask)) {
1082 total &= (len_mask - 1);
1084 total = (total << 8) | p->
buf[4 +
n++];
1096 if (total < probelen)
1114 if (tracks[i].num == num)
1126 int isize = *buf_size;
1129 int pkt_size = isize;
1133 if (pkt_size >= 10000000
U)
1136 switch (encodings[0].compression.algo) {
1141 if (header_size && !header) {
1149 pkt_size = isize + header_size;
1154 memcpy(pkt_data, header, header_size);
1155 memcpy(pkt_data + header_size, data, isize);
1161 olen = pkt_size *= 3;
1167 pkt_data = newpktdata;
1179 z_stream zstream = {0};
1180 if (inflateInit(&zstream) != Z_OK)
1182 zstream.next_in =
data;
1183 zstream.avail_in = isize;
1188 inflateEnd(&zstream);
1191 pkt_data = newpktdata;
1192 zstream.avail_out = pkt_size - zstream.total_out;
1193 zstream.next_out = pkt_data + zstream.total_out;
1195 result = inflate(&zstream, Z_NO_FLUSH);
1197 result = Z_MEM_ERROR;
1198 }
while (result==Z_OK && pkt_size<10000000);
1199 pkt_size = zstream.total_out;
1200 inflateEnd(&zstream);
1201 if (result != Z_STREAM_END) {
1202 if (result == Z_MEM_ERROR)
1213 bz_stream bzstream = {0};
1214 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1216 bzstream.next_in =
data;
1217 bzstream.avail_in = isize;
1222 BZ2_bzDecompressEnd(&bzstream);
1225 pkt_data = newpktdata;
1226 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1227 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1229 result = BZ2_bzDecompress(&bzstream);
1231 result = BZ_MEM_ERROR;
1232 }
while (result==BZ_OK && pkt_size<10000000);
1233 pkt_size = bzstream.total_out_lo32;
1234 BZ2_bzDecompressEnd(&bzstream);
1235 if (result != BZ_STREAM_END) {
1236 if (result == BZ_MEM_ERROR)
1250 *buf_size = pkt_size;
1262 char *layer, *ptr = pkt->
data, *
end = ptr+pkt->
size;
1263 for (; *ptr!=
',' && ptr<
end-1; ptr++);
1267 for (; *ptr!=
',' && ptr<end-1; ptr++);
1269 int64_t end_pts = pkt->
pts + display_duration;
1271 int ec = matroska->
time_scale * end_pts / 10000000;
1272 int sh, sm, ss, eh, em, es,
len;
1273 sh = sc/360000; sc -= 360000*sh;
1274 sm = sc/ 6000; sc -= 6000*sm;
1275 ss = sc/ 100; sc -= 100*ss;
1276 eh = ec/360000; ec -= 360000*eh;
1277 em = ec/ 6000; ec -= 6000*em;
1278 es = ec/ 100; ec -= 100*es;
1283 snprintf(line->
data, len,
"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
1284 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
1313 for (i=0; i < list->
nb_elem; i++) {
1314 const char *lang = tags[i].
lang && strcmp(tags[i].lang,
"und") ?
1315 tags[i].
lang : NULL;
1317 if (!tags[i].
name) {
1321 if (prefix)
snprintf(key,
sizeof(key),
"%s/%s", prefix, tags[i].name);
1322 else av_strlcpy(key, tags[i].name,
sizeof(key));
1323 if (tags[i].def || !lang) {
1325 if (tags[i].sub.nb_elem)
1332 if (tags[i].sub.nb_elem)
1346 if (tags[i].target.attachuid) {
1349 if (attachment[j].uid == tags[i].target.attachuid
1352 &attachment[j].stream->metadata, NULL);
1353 }
else if (tags[i].target.chapteruid) {
1356 if (chapter[j].uid == tags[i].target.chapteruid
1359 &chapter[j].chapter->metadata, NULL);
1360 }
else if (tags[i].target.trackuid) {
1363 if (track[j].uid == tags[i].target.trackuid && track[j].
stream)
1365 &track[j].stream->metadata, NULL);
1377 uint32_t level_up = matroska->
level_up;
1384 if (idx >= seekhead_list->
nb_elem
1396 "Max EBML element depth (%d) reached, "
1401 level.
length = (uint64_t)-1;
1406 ret =
ebml_parse(matroska, matroska_segment, matroska);
1411 if (length == (uint64_t)-1)
1435 for (i = 0; i < seekhead_list->
nb_elem; i++) {
1437 if (seekhead[i].pos <= before_pos)
1457 int index_scale = 1;
1460 index_list = &matroska->
index;
1461 index = index_list->
elem;
1467 for (i = 0; i < index_list->
nb_elem; i++) {
1470 for (j = 0; j < pos_list->
nb_elem; j++) {
1472 if (track && track->stream)
1475 index[i].
time/index_scale, 0, 0,
1486 for (i = 0; i < seekhead_list->
nb_elem; i++)
1498 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
1502 if (strstr(codec_id, aac_profiles[profile]))
1521 time_t creation_time = date_utc / 1000000000 + 978307200;
1522 struct tm *ptm = gmtime(&creation_time);
1524 strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", ptm);
1525 av_dict_set(metadata,
"creation_time", buffer, 0);
1536 uint64_t max_start = 0;
1549 "EBML header using unsupported features\n"
1550 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1556 "EBML header using unsupported features\n"
1557 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1574 res =
ebml_parse(matroska, matroska_segments, matroska);
1581 res =
ebml_parse(matroska, matroska_segment, matroska);
1603 int extradata_size = 0;
1604 int extradata_offset = 0;
1605 uint32_t fourcc = 0;
1607 char* key_id_base64 = NULL;
1616 "Unknown or unsupported track type %"PRIu64
"\n",
1636 if (encodings_list->
nb_elem > 1) {
1638 "Multiple combined encodings not supported");
1639 }
else if (encodings_list->
nb_elem == 1) {
1640 if (encodings[0].
type) {
1641 if (encodings[0].encryption.key_id.size > 0) {
1644 const int b64_size =
AV_BASE64_SIZE(encodings[0].encryption.key_id.size);
1646 if (key_id_base64 == NULL)
1650 encodings[0].encryption.key_id.data,
1653 encodings[0].
scope = 0;
1655 "Unsupported encoding type");
1668 encodings[0].
scope = 0;
1670 "Unsupported encoding type");
1680 "Failed to decode codec private data\n");
1702 if (key_id_base64) {
1708 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC")
1715 extradata_offset = 40;
1716 }
else if (!strcmp(track->
codec_id,
"A_MS/ACM")
1721 0, NULL, NULL, NULL, NULL);
1727 }
else if (!strcmp(track->
codec_id,
"A_QUICKTIME")
1736 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME")
1764 extradata =
av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
1765 if (extradata == NULL)
1767 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1768 extradata[1] = ((sri&0x01) << 7) | (track->
audio.
channels<<3);
1769 if (strstr(track->
codec_id,
"SBR")) {
1771 extradata[2] = 0x56;
1772 extradata[3] = 0xE5;
1773 extradata[4] = 0x80 | (sri<<3);
1782 extradata =
av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
1783 if (extradata == NULL)
1785 AV_WB32(extradata, extradata_size);
1786 memcpy(&extradata[4],
"alac", 4);
1791 extradata_size = 30;
1792 extradata =
av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
1793 if (extradata == NULL)
1796 NULL, NULL, NULL, NULL);
1807 extradata_offset = 26;
1817 0, NULL, NULL, NULL, NULL);
1825 if (flavor < 0 || track->audio.coded_framesize <= 0 ||
1837 static const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1842 extradata_offset = 78;
1849 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
1857 if (strcmp(track->
language,
"und"))
1899 #if FF_API_R_FRAME_RATE
1918 snprintf(buf,
sizeof(buf),
"%s_%d",
1921 if (planes[j].uid == tracks[k].uid) {
1923 "stereo_mode", buf, 0);
1949 if (!strcmp(track->codec_id,
"D_WEBVTT/CAPTIONS")) {
1951 }
else if (!strcmp(track->codec_id,
"D_WEBVTT/DESCRIPTIONS")) {
1953 }
else if (!strcmp(track->codec_id,
"D_WEBVTT/METADATA")) {
1968 attachements = attachements_list->elem;
1969 for (j=0; j<attachements_list->nb_elem; j++) {
1970 if (!(attachements[j].filename && attachements[j].mime &&
1971 attachements[j].bin.data && attachements[j].bin.size > 0)) {
1983 memcpy(st->
codec->
extradata, attachements[j].bin.data, attachements[j].bin.size);
1992 attachements[j].stream = st;
1996 chapters = chapters_list->elem;
1997 for (i=0; i<chapters_list->nb_elem; i++)
1999 && (max_start==0 || chapters[i].
start > max_start)) {
2000 chapters[i].chapter =
2002 chapters[i].start, chapters[i].end,
2005 "title", chapters[i].title, 0);
2006 max_start = chapters[i].start;
2033 matroska->
packets = newpackets;
2063 int* buf_size,
int type,
2064 uint32_t **lace_buf,
int *laces)
2068 uint32_t *lace_size;
2076 *lace_buf[0] =
size;
2084 lace_size =
av_mallocz(*laces *
sizeof(
int));
2092 for (
n = 0; res == 0 &&
n < *laces - 1;
n++) {
2094 if (size <= total) {
2100 lace_size[
n] +=
temp;
2107 if (size <= total) {
2112 lace_size[
n] = size - total;
2117 if (size % (*laces)) {
2121 for (
n = 0;
n < *laces;
n++)
2122 lace_size[
n] = size / *laces;
2129 if (n < 0 || num > INT_MAX) {
2131 "EBML block data error\n");
2137 total = lace_size[0] = num;
2138 for (
n = 1; res == 0 &&
n < *laces - 1;
n++) {
2142 if (r < 0 || lace_size[
n - 1] + snum > (uint64_t)INT_MAX) {
2144 "EBML block data error\n");
2150 lace_size[
n] = lace_size[
n - 1] + snum;
2151 total += lace_size[
n];
2153 if (size <= total) {
2157 lace_size[*laces - 1] = size - total;
2163 *lace_buf = lace_size;
2188 if (size < cfs * h / 2) {
2190 "Corrupt int4 RM-style audio packet size\n");
2193 for (x=0; x<h/2; x++)
2194 memcpy(track->
audio.
buf+x*2*w+y*cfs,
2199 "Corrupt sipr RM-style audio packet size\n");
2202 memcpy(track->
audio.
buf + y*w, data, w);
2204 if (size < sps * w / sps || h<=0 || w%sps) {
2206 "Corrupt generic RM-style audio packet size\n");
2209 for (x=0; x<w/sps; x++)
2210 memcpy(track->
audio.
buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2250 if (srclen < 12 || track->stream->codec->extradata_size < 2)
2259 while (srclen >= 8) {
2265 uint32_t crc =
AV_RL32(src + 4);
2269 multiblock = (flags & 0x1800) != 0x1800;
2281 if (blocksize > srclen) {
2286 tmp =
av_realloc(dst, dstlen + blocksize + 32);
2292 dstlen += blocksize + 32;
2295 AV_WL32(dst + offset + 4, blocksize + 24);
2296 AV_WL16(dst + offset + 8, ver);
2297 AV_WL16(dst + offset + 10, 0);
2298 AV_WL32(dst + offset + 12, 0);
2299 AV_WL32(dst + offset + 16, 0);
2300 AV_WL32(dst + offset + 20, samples);
2301 AV_WL32(dst + offset + 24, flags);
2302 AV_WL32(dst + offset + 28, crc);
2303 memcpy (dst + offset + 32, src, blocksize);
2306 srclen -= blocksize;
2307 offset += blocksize + 32;
2330 int id_len, settings_len, text_len;
2338 q = data + data_len;
2343 if (*p ==
'\r' || *p ==
'\n') {
2352 if (p >= q || *p !=
'\n')
2359 if (*p ==
'\r' || *p ==
'\n') {
2360 settings_len = p - settings;
2368 if (p >= q || *p !=
'\n')
2374 while (text_len > 0) {
2375 const int len = text_len - 1;
2377 if (c !=
'\r' && c !=
'\n')
2392 memcpy(pkt->
data, text, text_len);
2402 memcpy(buf,
id, id_len);
2405 if (settings_len > 0) {
2413 memcpy(buf, settings, settings_len);
2420 pkt->
pts = timecode;
2438 uint64_t timecode, uint64_t lace_duration,
2440 uint8_t *additional, uint64_t additional_id,
int additional_size,
2441 int64_t discard_padding)
2448 if (encodings && !encodings->
type && encodings->
scope & 1) {
2461 if (pkt_data != data)
2479 bytestream_put_be32(&buf, pkt_size);
2480 bytestream_put_be32(&buf,
MKBETAG(
'i',
'c',
'p',
'f'));
2483 memcpy(pkt->
data + offset, pkt_data, pkt_size);
2485 if (pkt_data != data)
2491 if (additional_size > 0) {
2494 additional_size + 8);
2495 if(side_data == NULL) {
2500 AV_WB64(side_data, additional_id);
2501 memcpy(side_data + 8, additional, additional_size);
2504 if (discard_padding) {
2508 if(side_data == NULL) {
2520 pkt->
dts = timecode;
2522 pkt->
pts = timecode;
2537 lace_duration <= INT_MAX) {
2550 matroska_fix_ass_packet(matroska, pkt, lace_duration);
2557 matroska_merge_packets(matroska->
prev_pkt, pkt);
2569 if (pkt_data != data)
2575 int size, int64_t pos, uint64_t cluster_time,
2577 uint8_t *additional, uint64_t additional_id,
int additional_size,
2578 int64_t cluster_pos, int64_t discard_padding)
2585 uint32_t *lace_size = NULL;
2588 int trust_default_duration = 1;
2598 if (!track || !track->
stream) {
2600 "Invalid stream %"PRIu64
" or size %u\n", num, size);
2602 }
else if (size <= 3)
2613 if (is_keyframe == -1)
2616 if (cluster_time != (uint64_t)-1
2617 && (block_time >= 0 || cluster_time >= -block_time)) {
2618 timecode = cluster_time + block_time;
2620 && timecode < track->end_timecode)
2627 if (timecode < matroska->skip_to_timecode)
2638 &lace_size, &laces);
2647 trust_default_duration = 0;
2651 if (!block_duration && trust_default_duration)
2654 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
2658 for (n = 0; n < laces; n++) {
2659 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
2661 if (lace_size[n] > size) {
2681 timecode, lace_duration,
2688 timecode, lace_duration,
2689 pos, !n? is_keyframe : 0,
2690 additional, additional_id, additional_size,
2697 timecode = lace_duration ? timecode + lace_duration :
AV_NOPTS_VALUE;
2698 data += lace_size[
n];
2699 size -= lace_size[
n];
2713 matroska_cluster_incremental_parsing,
2728 matroska_clusters_incremental,
2733 matroska_cluster_incremental_parsing,
2741 blocks = blocks_list->
elem;
2745 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2749 if (!blocks[i].non_simple)
2752 blocks[i].bin.data, blocks[i].
bin.
size,
2779 res =
ebml_parse(matroska, matroska_clusters, &cluster);
2780 blocks_list = &cluster.
blocks;
2781 blocks = blocks_list->
elem;
2782 for (i=0; i<blocks_list->
nb_elem; i++)
2783 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2786 blocks[i].bin.data, blocks[i].
bin.
size,
2788 blocks[i].
duration, is_keyframe, NULL, 0, 0,
2811 int64_t timestamp,
int flags)
2816 int i,
index, index_sub, index_min;
2851 while(index_sub >= 0
2853 && tracks[i].stream->index_entries[index_sub].pos < st->
index_entries[index_min].
pos
2903 .
name =
"matroska,webm",