641 if (
avio_seek(pb, last_pos + 1, SEEK_SET) < 0)
689 int max_size, uint64_t *number)
702 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
711 if (read > max_size) {
714 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
722 total = (total << 8) |
avio_r8(pb);
738 if (res > 0 && *number + 1 == 1ULL << (7 * res))
739 *number = 0xffffffffffffffULL;
757 *num = (*num << 8) |
avio_r8(pb);
780 *num = (*num << 8) |
avio_r8(pb);
794 }
else if (size == 4) {
796 }
else if (size == 8){
897 *num = unum - ((1LL << (7*res - 1)) - 1);
906 uint32_t
id,
void *
data)
909 for (i=0; syntax[i].
id; i++)
910 if (
id == syntax[i].
id)
942 for (i=0; syntax[i].
id; i++)
943 switch (syntax[i].
type) {
945 *(uint64_t *)((
char *)data+syntax[i].
data_offset) = syntax[i].def.
u;
948 *(
double *)((
char *)data+syntax[i].data_offset) = syntax[i].
def.
f;
953 if (syntax[i].def.
s) {
982 uint32_t
id = syntax->
id;
993 list->
elem = newelem;
1003 if (max_lengths[syntax->
type] && length > max_lengths[syntax->
type]) {
1005 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for syntax element %i\n",
1006 length, max_lengths[syntax->
type], syntax->
type);
1011 switch (syntax->
type) {
1040 for (i=0; syntax[i].
id; i++) {
1041 void *data_off = (
char *)data + syntax[i].data_offset;
1042 switch (syntax[i].
type) {
1047 if (syntax[i].list_elem_size) {
1049 char *ptr = list->
elem;
1067 int len_mask = 0x80,
size = 1,
n = 1, i;
1075 while (
size <= 8 && !(total & len_mask)) {
1081 total &= (len_mask - 1);
1083 total = (total << 8) | p->
buf[4 +
n++];
1095 if (total < probelen)
1113 if (tracks[i].num == num)
1125 int isize = *buf_size;
1128 int pkt_size = isize;
1132 if (pkt_size >= 10000000
U)
1135 switch (encodings[0].compression.algo) {
1140 if (header_size && !header) {
1148 pkt_size = isize + header_size;
1153 memcpy(pkt_data, header, header_size);
1154 memcpy(pkt_data + header_size, data, isize);
1160 olen = pkt_size *= 3;
1166 pkt_data = newpktdata;
1178 z_stream zstream = {0};
1179 if (inflateInit(&zstream) != Z_OK)
1181 zstream.next_in =
data;
1182 zstream.avail_in = isize;
1187 inflateEnd(&zstream);
1190 pkt_data = newpktdata;
1191 zstream.avail_out = pkt_size - zstream.total_out;
1192 zstream.next_out = pkt_data + zstream.total_out;
1194 result = inflate(&zstream, Z_NO_FLUSH);
1196 result = Z_MEM_ERROR;
1197 }
while (result==Z_OK && pkt_size<10000000);
1198 pkt_size = zstream.total_out;
1199 inflateEnd(&zstream);
1200 if (result != Z_STREAM_END) {
1201 if (result == Z_MEM_ERROR)
1212 bz_stream bzstream = {0};
1213 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1215 bzstream.next_in =
data;
1216 bzstream.avail_in = isize;
1221 BZ2_bzDecompressEnd(&bzstream);
1224 pkt_data = newpktdata;
1225 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1226 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1228 result = BZ2_bzDecompress(&bzstream);
1230 result = BZ_MEM_ERROR;
1231 }
while (result==BZ_OK && pkt_size<10000000);
1232 pkt_size = bzstream.total_out_lo32;
1233 BZ2_bzDecompressEnd(&bzstream);
1234 if (result != BZ_STREAM_END) {
1235 if (result == BZ_MEM_ERROR)
1249 *buf_size = pkt_size;
1261 char *layer, *ptr = pkt->
data, *
end = ptr+pkt->
size;
1262 for (; *ptr!=
',' && ptr<
end-1; ptr++);
1266 for (; *ptr!=
',' && ptr<end-1; ptr++);
1268 int64_t end_pts = pkt->
pts + display_duration;
1270 int ec = matroska->
time_scale * end_pts / 10000000;
1271 int sh, sm, ss, eh, em, es,
len;
1272 sh = sc/360000; sc -= 360000*sh;
1273 sm = sc/ 6000; sc -= 6000*sm;
1274 ss = sc/ 100; sc -= 100*ss;
1275 eh = ec/360000; ec -= 360000*eh;
1276 em = ec/ 6000; ec -= 6000*em;
1277 es = ec/ 100; ec -= 100*es;
1282 snprintf(line->
data, len,
"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
1283 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
1312 for (i=0; i < list->
nb_elem; i++) {
1313 const char *lang = tags[i].
lang && strcmp(tags[i].lang,
"und") ?
1314 tags[i].
lang : NULL;
1316 if (!tags[i].
name) {
1320 if (prefix)
snprintf(key,
sizeof(key),
"%s/%s", prefix, tags[i].name);
1321 else av_strlcpy(key, tags[i].name,
sizeof(key));
1322 if (tags[i].def || !lang) {
1324 if (tags[i].sub.nb_elem)
1331 if (tags[i].sub.nb_elem)
1345 if (tags[i].target.attachuid) {
1348 if (attachment[j].uid == tags[i].target.attachuid
1351 &attachment[j].stream->metadata, NULL);
1352 }
else if (tags[i].target.chapteruid) {
1355 if (chapter[j].uid == tags[i].target.chapteruid
1358 &chapter[j].chapter->metadata, NULL);
1359 }
else if (tags[i].target.trackuid) {
1362 if (track[j].uid == tags[i].target.trackuid && track[j].
stream)
1364 &track[j].stream->metadata, NULL);
1376 uint32_t level_up = matroska->
level_up;
1383 if (idx >= seekhead_list->
nb_elem
1395 "Max EBML element depth (%d) reached, "
1400 level.
length = (uint64_t)-1;
1405 ret =
ebml_parse(matroska, matroska_segment, matroska);
1410 if (length == (uint64_t)-1)
1434 for (i = 0; i < seekhead_list->
nb_elem; i++) {
1436 if (seekhead[i].pos <= before_pos)
1456 int index_scale = 1;
1459 index_list = &matroska->
index;
1460 index = index_list->
elem;
1466 for (i = 0; i < index_list->
nb_elem; i++) {
1469 for (j = 0; j < pos_list->
nb_elem; j++) {
1471 if (track && track->stream)
1474 index[i].
time/index_scale, 0, 0,
1485 for (i = 0; i < seekhead_list->
nb_elem; i++)
1497 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
1501 if (strstr(codec_id, aac_profiles[profile]))
1520 time_t creation_time = date_utc / 1000000000 + 978307200;
1521 struct tm *ptm = gmtime(&creation_time);
1523 strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", ptm);
1524 av_dict_set(metadata,
"creation_time", buffer, 0);
1535 uint64_t max_start = 0;
1548 "EBML header using unsupported features\n"
1549 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1555 "EBML header using unsupported features\n"
1556 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1573 res =
ebml_parse(matroska, matroska_segments, matroska);
1580 res =
ebml_parse(matroska, matroska_segment, matroska);
1601 int extradata_size = 0;
1602 int extradata_offset = 0;
1603 uint32_t fourcc = 0;
1605 char* key_id_base64 = NULL;
1613 "Unknown or unsupported track type %"PRIu64
"\n",
1633 if (encodings_list->
nb_elem > 1) {
1635 "Multiple combined encodings not supported");
1636 }
else if (encodings_list->
nb_elem == 1) {
1637 if (encodings[0].
type) {
1638 if (encodings[0].encryption.key_id.size > 0) {
1641 const int b64_size =
AV_BASE64_SIZE(encodings[0].encryption.key_id.size);
1643 if (key_id_base64 == NULL)
1647 encodings[0].encryption.key_id.data,
1650 encodings[0].
scope = 0;
1652 "Unsupported encoding type");
1665 encodings[0].
scope = 0;
1667 "Unsupported encoding type");
1677 "Failed to decode codec private data\n");
1699 if (key_id_base64) {
1705 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC")
1711 extradata_offset = 40;
1712 }
else if (!strcmp(track->
codec_id,
"A_MS/ACM")
1717 0, NULL, NULL, NULL, NULL);
1723 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME")
1745 extradata =
av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
1746 if (extradata == NULL)
1748 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1749 extradata[1] = ((sri&0x01) << 7) | (track->
audio.
channels<<3);
1750 if (strstr(track->
codec_id,
"SBR")) {
1752 extradata[2] = 0x56;
1753 extradata[3] = 0xE5;
1754 extradata[4] = 0x80 | (sri<<3);
1763 extradata =
av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
1764 if (extradata == NULL)
1766 AV_WB32(extradata, extradata_size);
1767 memcpy(&extradata[4],
"alac", 4);
1772 extradata_size = 30;
1773 extradata =
av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
1774 if (extradata == NULL)
1777 NULL, NULL, NULL, NULL);
1788 extradata_offset = 26;
1798 0, NULL, NULL, NULL, NULL);
1806 if (flavor < 0 || track->audio.coded_framesize <= 0 ||
1818 static const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1823 extradata_offset = 78;
1830 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
1838 if (strcmp(track->
language,
"und"))
1877 #if FF_API_R_FRAME_RATE
1896 snprintf(buf,
sizeof(buf),
"%s_%d",
1899 if (planes[j].uid == tracks[k].uid) {
1901 "stereo_mode", buf, 0);
1927 if (!strcmp(track->codec_id,
"D_WEBVTT/CAPTIONS")) {
1929 }
else if (!strcmp(track->codec_id,
"D_WEBVTT/DESCRIPTIONS")) {
1931 }
else if (!strcmp(track->codec_id,
"D_WEBVTT/METADATA")) {
1946 attachements = attachements_list->elem;
1947 for (j=0; j<attachements_list->nb_elem; j++) {
1948 if (!(attachements[j].filename && attachements[j].mime &&
1949 attachements[j].bin.data && attachements[j].bin.size > 0)) {
1961 memcpy(st->
codec->
extradata, attachements[j].bin.data, attachements[j].bin.size);
1970 attachements[j].stream = st;
1974 chapters = chapters_list->elem;
1975 for (i=0; i<chapters_list->nb_elem; i++)
1977 && (max_start==0 || chapters[i].
start > max_start)) {
1978 chapters[i].chapter =
1980 chapters[i].start, chapters[i].end,
1983 "title", chapters[i].title, 0);
1984 max_start = chapters[i].start;
2011 matroska->
packets = newpackets;
2041 int* buf_size,
int type,
2042 uint32_t **lace_buf,
int *laces)
2046 uint32_t *lace_size;
2054 *lace_buf[0] =
size;
2062 lace_size =
av_mallocz(*laces *
sizeof(
int));
2070 for (
n = 0; res == 0 &&
n < *laces - 1;
n++) {
2072 if (size <= total) {
2078 lace_size[
n] +=
temp;
2085 if (size <= total) {
2090 lace_size[
n] = size - total;
2095 if (size % (*laces)) {
2099 for (
n = 0;
n < *laces;
n++)
2100 lace_size[
n] = size / *laces;
2107 if (n < 0 || num > INT_MAX) {
2109 "EBML block data error\n");
2115 total = lace_size[0] = num;
2116 for (
n = 1; res == 0 &&
n < *laces - 1;
n++) {
2120 if (r < 0 || lace_size[
n - 1] + snum > (uint64_t)INT_MAX) {
2122 "EBML block data error\n");
2128 lace_size[
n] = lace_size[
n - 1] + snum;
2129 total += lace_size[
n];
2131 if (size <= total) {
2135 lace_size[*laces - 1] = size - total;
2141 *lace_buf = lace_size;
2166 if (size < cfs * h / 2) {
2168 "Corrupt int4 RM-style audio packet size\n");
2171 for (x=0; x<h/2; x++)
2172 memcpy(track->
audio.
buf+x*2*w+y*cfs,
2177 "Corrupt sipr RM-style audio packet size\n");
2180 memcpy(track->
audio.
buf + y*w, data, w);
2182 if (size < sps * w / sps || h<=0 || w%sps) {
2184 "Corrupt generic RM-style audio packet size\n");
2187 for (x=0; x<w/sps; x++)
2188 memcpy(track->
audio.
buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2228 if (srclen < 12 || track->stream->codec->extradata_size < 2)
2237 while (srclen >= 8) {
2243 uint32_t crc =
AV_RL32(src + 4);
2247 multiblock = (flags & 0x1800) != 0x1800;
2259 if (blocksize > srclen) {
2264 tmp =
av_realloc(dst, dstlen + blocksize + 32);
2270 dstlen += blocksize + 32;
2273 AV_WL32(dst + offset + 4, blocksize + 24);
2274 AV_WL16(dst + offset + 8, ver);
2275 AV_WL16(dst + offset + 10, 0);
2276 AV_WL32(dst + offset + 12, 0);
2277 AV_WL32(dst + offset + 16, 0);
2278 AV_WL32(dst + offset + 20, samples);
2279 AV_WL32(dst + offset + 24, flags);
2280 AV_WL32(dst + offset + 28, crc);
2281 memcpy (dst + offset + 32, src, blocksize);
2284 srclen -= blocksize;
2285 offset += blocksize + 32;
2308 int id_len, settings_len, text_len;
2316 q = data + data_len;
2321 if (*p ==
'\r' || *p ==
'\n') {
2330 if (p >= q || *p !=
'\n')
2337 if (*p ==
'\r' || *p ==
'\n') {
2338 settings_len = p - settings;
2346 if (p >= q || *p !=
'\n')
2352 while (text_len > 0) {
2353 const int len = text_len - 1;
2355 if (c !=
'\r' && c !=
'\n')
2370 memcpy(pkt->
data, text, text_len);
2380 memcpy(buf,
id, id_len);
2383 if (settings_len > 0) {
2391 memcpy(buf, settings, settings_len);
2398 pkt->
pts = timecode;
2416 uint64_t timecode, uint64_t lace_duration,
2417 int64_t pos,
int is_keyframe,
2418 uint8_t *additional, uint64_t additional_id,
int additional_size,
2419 int64_t discard_padding)
2426 if (encodings && !encodings->
type && encodings->
scope & 1) {
2439 if (pkt_data != data)
2457 bytestream_put_be32(&buf, pkt_size);
2458 bytestream_put_be32(&buf,
MKBETAG(
'i',
'c',
'p',
'f'));
2461 memcpy(pkt->
data + offset, pkt_data, pkt_size);
2463 if (pkt_data != data)
2466 pkt->
flags = is_keyframe;
2469 if (additional_size > 0) {
2472 additional_size + 8);
2473 if(side_data == NULL) {
2478 AV_WB64(side_data, additional_id);
2479 memcpy(side_data + 8, additional, additional_size);
2482 if (discard_padding) {
2486 if(side_data == NULL) {
2498 pkt->
dts = timecode;
2500 pkt->
pts = timecode;
2515 lace_duration <= INT_MAX) {
2528 matroska_fix_ass_packet(matroska, pkt, lace_duration);
2535 matroska_merge_packets(matroska->
prev_pkt, pkt);
2547 if (pkt_data != data)
2553 int size, int64_t pos, uint64_t cluster_time,
2554 uint64_t block_duration,
int is_keyframe,
2555 uint8_t *additional, uint64_t additional_id,
int additional_size,
2556 int64_t cluster_pos, int64_t discard_padding)
2563 uint32_t *lace_size = NULL;
2566 int trust_default_duration = 1;
2576 if (!track || !track->
stream) {
2578 "Invalid stream %"PRIu64
" or size %u\n", num, size);
2580 }
else if (size <= 3)
2591 if (is_keyframe == -1)
2594 if (cluster_time != (uint64_t)-1
2595 && (block_time >= 0 || cluster_time >= -block_time)) {
2596 timecode = cluster_time + block_time;
2598 && timecode < track->end_timecode)
2605 if (timecode < matroska->skip_to_timecode)
2616 &lace_size, &laces);
2625 trust_default_duration = 0;
2629 if (!block_duration && trust_default_duration)
2632 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
2636 for (n = 0; n < laces; n++) {
2637 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
2639 if (lace_size[n] > size) {
2659 timecode, lace_duration,
2666 timecode, lace_duration,
2667 pos, !n? is_keyframe : 0,
2668 additional, additional_id, additional_size,
2675 timecode = lace_duration ? timecode + lace_duration :
AV_NOPTS_VALUE;
2676 data += lace_size[
n];
2677 size -= lace_size[
n];
2691 matroska_cluster_incremental_parsing,
2706 matroska_clusters_incremental,
2711 matroska_cluster_incremental_parsing,
2719 blocks = blocks_list->
elem;
2723 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2727 if (!blocks[i].non_simple)
2730 blocks[i].bin.data, blocks[i].
bin.
size,
2757 res =
ebml_parse(matroska, matroska_clusters, &cluster);
2758 blocks_list = &cluster.
blocks;
2759 blocks = blocks_list->
elem;
2760 for (i=0; i<blocks_list->
nb_elem; i++)
2761 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2764 blocks[i].bin.data, blocks[i].
bin.
size,
2766 blocks[i].
duration, is_keyframe, NULL, 0, 0,
2789 int64_t timestamp,
int flags)
2794 int i,
index, index_sub, index_min;
2829 while(index_sub >= 0
2831 && tracks[i].stream->index_entries[index_sub].pos < st->
index_entries[index_min].
pos
2881 .
name =
"matroska,webm",