632 if (
avio_seek(pb, last_pos + 1, SEEK_SET) < 0)
680 int max_size, uint64_t *number)
693 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
702 if (read > max_size) {
705 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
713 total = (total << 8) |
avio_r8(pb);
729 if (res > 0 && *number + 1 == 1ULL << (7 * res))
730 *number = 0xffffffffffffffULL;
748 *num = (*num << 8) |
avio_r8(pb);
761 }
else if (size == 4) {
763 }
else if (size == 8){
864 *num = unum - ((1LL << (7*res - 1)) - 1);
873 uint32_t
id,
void *
data)
876 for (i=0; syntax[i].
id; i++)
877 if (
id == syntax[i].
id)
909 for (i=0; syntax[i].
id; i++)
910 switch (syntax[i].
type) {
912 *(uint64_t *)((
char *)data+syntax[i].
data_offset) = syntax[i].def.
u;
915 *(
double *)((
char *)data+syntax[i].data_offset) = syntax[i].
def.
f;
943 uint32_t
id = syntax->
id;
954 list->
elem = newelem;
964 if (max_lengths[syntax->
type] && length > max_lengths[syntax->
type]) {
966 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for syntax element %i\n",
967 length, max_lengths[syntax->
type], syntax->
type);
972 switch (syntax->
type) {
1000 for (i=0; syntax[i].
id; i++) {
1001 void *data_off = (
char *)data + syntax[i].data_offset;
1002 switch (syntax[i].
type) {
1007 if (syntax[i].list_elem_size) {
1009 char *ptr = list->
elem;
1027 int len_mask = 0x80,
size = 1,
n = 1, i;
1035 while (
size <= 8 && !(total & len_mask)) {
1041 total &= (len_mask - 1);
1043 total = (total << 8) | p->
buf[4 +
n++];
1055 if (total < probelen)
1073 if (tracks[i].num == num)
1085 int isize = *buf_size;
1088 int pkt_size = isize;
1092 if (pkt_size >= 10000000
U)
1095 switch (encodings[0].compression.algo) {
1100 if (header_size && !header) {
1108 pkt_size = isize + header_size;
1113 memcpy(pkt_data, header, header_size);
1114 memcpy(pkt_data + header_size, data, isize);
1120 olen = pkt_size *= 3;
1126 pkt_data = newpktdata;
1138 z_stream zstream = {0};
1139 if (inflateInit(&zstream) != Z_OK)
1141 zstream.next_in =
data;
1142 zstream.avail_in = isize;
1147 inflateEnd(&zstream);
1150 pkt_data = newpktdata;
1151 zstream.avail_out = pkt_size - zstream.total_out;
1152 zstream.next_out = pkt_data + zstream.total_out;
1154 result = inflate(&zstream, Z_NO_FLUSH);
1156 result = Z_MEM_ERROR;
1157 }
while (result==Z_OK && pkt_size<10000000);
1158 pkt_size = zstream.total_out;
1159 inflateEnd(&zstream);
1160 if (result != Z_STREAM_END) {
1161 if (result == Z_MEM_ERROR)
1172 bz_stream bzstream = {0};
1173 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1175 bzstream.next_in =
data;
1176 bzstream.avail_in = isize;
1181 BZ2_bzDecompressEnd(&bzstream);
1184 pkt_data = newpktdata;
1185 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1186 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1188 result = BZ2_bzDecompress(&bzstream);
1190 result = BZ_MEM_ERROR;
1191 }
while (result==BZ_OK && pkt_size<10000000);
1192 pkt_size = bzstream.total_out_lo32;
1193 BZ2_bzDecompressEnd(&bzstream);
1194 if (result != BZ_STREAM_END) {
1195 if (result == BZ_MEM_ERROR)
1209 *buf_size = pkt_size;
1221 char *layer, *ptr = pkt->
data, *
end = ptr+pkt->
size;
1222 for (; *ptr!=
',' && ptr<
end-1; ptr++);
1226 for (; *ptr!=
',' && ptr<end-1; ptr++);
1228 int64_t end_pts = pkt->
pts + display_duration;
1230 int ec = matroska->
time_scale * end_pts / 10000000;
1231 int sh, sm, ss, eh, em, es,
len;
1232 sh = sc/360000; sc -= 360000*sh;
1233 sm = sc/ 6000; sc -= 6000*sm;
1234 ss = sc/ 100; sc -= 100*ss;
1235 eh = ec/360000; ec -= 360000*eh;
1236 em = ec/ 6000; ec -= 6000*em;
1237 es = ec/ 100; ec -= 100*es;
1242 snprintf(line->
data, len,
"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
1243 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
1272 for (i=0; i < list->
nb_elem; i++) {
1273 const char *lang= (tags[i].
lang && strcmp(tags[i].lang,
"und")) ? tags[i].lang : NULL;
1275 if (!tags[i].
name) {
1279 if (prefix)
snprintf(key,
sizeof(key),
"%s/%s", prefix, tags[i].name);
1280 else av_strlcpy(key, tags[i].name,
sizeof(key));
1281 if (tags[i].def || !lang) {
1283 if (tags[i].sub.nb_elem)
1290 if (tags[i].sub.nb_elem)
1304 if (tags[i].target.attachuid) {
1307 if (attachment[j].uid == tags[i].target.attachuid
1310 &attachment[j].stream->metadata, NULL);
1311 }
else if (tags[i].target.chapteruid) {
1314 if (chapter[j].uid == tags[i].target.chapteruid
1317 &chapter[j].chapter->metadata, NULL);
1318 }
else if (tags[i].target.trackuid) {
1321 if (track[j].uid == tags[i].target.trackuid && track[j].
stream)
1323 &track[j].stream->metadata, NULL);
1335 uint32_t level_up = matroska->
level_up;
1342 if (idx >= seekhead_list->
nb_elem
1354 "Max EBML element depth (%d) reached, "
1359 level.
length = (uint64_t)-1;
1364 ret =
ebml_parse(matroska, matroska_segment, matroska);
1369 if (length == (uint64_t)-1)
1393 for (i = 0; i < seekhead_list->
nb_elem; i++) {
1395 if (seekhead[i].pos <= before_pos)
1415 int index_scale = 1;
1418 index_list = &matroska->
index;
1419 index = index_list->
elem;
1425 for (i = 0; i < index_list->
nb_elem; i++) {
1428 for (j = 0; j < pos_list->
nb_elem; j++) {
1430 if (track && track->stream)
1433 index[i].
time/index_scale, 0, 0,
1444 for (i = 0; i < seekhead_list->
nb_elem; i++)
1456 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
1460 if (strstr(codec_id, aac_profiles[profile]))
1479 time_t creation_time = date_utc / 1000000000 + 978307200;
1480 struct tm *ptm = gmtime(&creation_time);
1482 strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", ptm);
1483 av_dict_set(metadata,
"creation_time", buffer, 0);
1494 uint64_t max_start = 0;
1507 "EBML header using unsupported features\n"
1508 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1514 "EBML header using unsupported features\n"
1515 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1532 res =
ebml_parse(matroska, matroska_segments, matroska);
1539 res =
ebml_parse(matroska, matroska_segment, matroska);
1560 int extradata_size = 0;
1561 int extradata_offset = 0;
1562 uint32_t fourcc = 0;
1564 char* key_id_base64 = NULL;
1571 "Unknown or unsupported track type %"PRIu64
"\n",
1591 if (encodings_list->
nb_elem > 1) {
1593 "Multiple combined encodings not supported");
1594 }
else if (encodings_list->
nb_elem == 1) {
1595 if (encodings[0].
type) {
1596 if (encodings[0].encryption.key_id.size > 0) {
1599 const int b64_size =
AV_BASE64_SIZE(encodings[0].encryption.key_id.size);
1601 if (key_id_base64 == NULL)
1605 encodings[0].encryption.key_id.data,
1608 encodings[0].
scope = 0;
1610 "Unsupported encoding type");
1623 encodings[0].
scope = 0;
1625 "Unsupported encoding type");
1635 "Failed to decode codec private data\n");
1657 if (key_id_base64) {
1663 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC")
1669 extradata_offset = 40;
1670 }
else if (!strcmp(track->
codec_id,
"A_MS/ACM")
1675 0, NULL, NULL, NULL, NULL);
1681 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME")
1703 extradata =
av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
1704 if (extradata == NULL)
1706 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1707 extradata[1] = ((sri&0x01) << 7) | (track->
audio.
channels<<3);
1708 if (strstr(track->
codec_id,
"SBR")) {
1710 extradata[2] = 0x56;
1711 extradata[3] = 0xE5;
1712 extradata[4] = 0x80 | (sri<<3);
1721 extradata =
av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
1722 if (extradata == NULL)
1724 AV_WB32(extradata, extradata_size);
1725 memcpy(&extradata[4],
"alac", 4);
1730 extradata_size = 30;
1731 extradata =
av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
1732 if (extradata == NULL)
1735 NULL, NULL, NULL, NULL);
1744 extradata_offset = 26;
1754 0, NULL, NULL, NULL, NULL);
1768 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1773 extradata_offset = 78;
1780 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
1788 if (strcmp(track->
language,
"und"))
1803 FF_INPUT_BUFFER_PADDING_SIZE);
1829 #if FF_API_R_FRAME_RATE
1847 snprintf(buf,
sizeof(buf),
"%s_%d",
1850 if (planes[j].uid == tracks[k].uid) {
1852 "stereo_mode", buf, 0);
1875 attachements = attachements_list->
elem;
1876 for (j=0; j<attachements_list->
nb_elem; j++) {
1877 if (!(attachements[j].filename && attachements[j].mime &&
1878 attachements[j].bin.data && attachements[j].
bin.
size > 0)) {
1901 attachements[j].
stream = st;
1905 chapters = chapters_list->
elem;
1906 for (i=0; i<chapters_list->
nb_elem; i++)
1908 && (max_start==0 || chapters[i].
start > max_start)) {
1911 chapters[i].
start, chapters[i].
end,
1914 "title", chapters[i].
title, 0);
1915 max_start = chapters[i].
start;
1942 matroska->
packets = newpackets;
1972 int* buf_size,
int type,
1973 uint32_t **lace_buf,
int *laces)
1977 uint32_t *lace_size;
1985 *lace_buf[0] =
size;
1993 lace_size =
av_mallocz(*laces *
sizeof(
int));
2001 for (
n = 0; res == 0 &&
n < *laces - 1;
n++) {
2003 if (size <= total) {
2009 lace_size[
n] +=
temp;
2016 if (size <= total) {
2021 lace_size[
n] = size - total;
2026 if (size % (*laces)) {
2030 for (
n = 0;
n < *laces;
n++)
2031 lace_size[
n] = size / *laces;
2038 if (n < 0 || num > INT_MAX) {
2040 "EBML block data error\n");
2046 total = lace_size[0] = num;
2047 for (
n = 1; res == 0 &&
n < *laces - 1;
n++) {
2051 if (r < 0 || lace_size[
n - 1] + snum > (uint64_t)INT_MAX) {
2053 "EBML block data error\n");
2059 lace_size[
n] = lace_size[
n - 1] + snum;
2060 total += lace_size[
n];
2062 if (size <= total) {
2066 lace_size[*laces - 1] = size - total;
2072 *lace_buf = lace_size;
2097 if (size < cfs * h / 2) {
2099 "Corrupt int4 RM-style audio packet size\n");
2102 for (x=0; x<h/2; x++)
2103 memcpy(track->
audio.
buf+x*2*w+y*cfs,
2108 "Corrupt sipr RM-style audio packet size\n");
2111 memcpy(track->
audio.
buf + y*w, data, w);
2113 if (size < sps * w / sps || h<=0) {
2115 "Corrupt generic RM-style audio packet size\n");
2118 for (x=0; x<w/sps; x++)
2119 memcpy(track->
audio.
buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2159 if (srclen < 12 || track->stream->codec->extradata_size < 2)
2168 while (srclen >= 8) {
2174 uint32_t crc =
AV_RL32(src + 4);
2178 multiblock = (flags & 0x1800) != 0x1800;
2190 if (blocksize > srclen) {
2195 tmp =
av_realloc(dst, dstlen + blocksize + 32);
2201 dstlen += blocksize + 32;
2204 AV_WL32(dst + offset + 4, blocksize + 24);
2205 AV_WL16(dst + offset + 8, ver);
2206 AV_WL16(dst + offset + 10, 0);
2207 AV_WL32(dst + offset + 12, 0);
2208 AV_WL32(dst + offset + 16, 0);
2209 AV_WL32(dst + offset + 20, samples);
2210 AV_WL32(dst + offset + 24, flags);
2211 AV_WL32(dst + offset + 28, crc);
2212 memcpy (dst + offset + 32, src, blocksize);
2215 srclen -= blocksize;
2216 offset += blocksize + 32;
2233 uint64_t timecode, uint64_t lace_duration,
2234 int64_t pos,
int is_keyframe,
2235 uint8_t *additional, uint64_t additional_id,
int additional_size)
2242 if (encodings && !encodings->
type && encodings->
scope & 1) {
2255 if (pkt_data != data)
2273 bytestream_put_be32(&buf, pkt_size);
2274 bytestream_put_be32(&buf,
MKBETAG(
'i',
'c',
'p',
'f'));
2277 memcpy(pkt->
data + offset, pkt_data, pkt_size);
2279 if (pkt_data != data)
2282 pkt->
flags = is_keyframe;
2285 if (additional_size > 0) {
2288 additional_size + 8);
2289 if(side_data == NULL) {
2294 AV_WB64(side_data, additional_id);
2295 memcpy(side_data + 8, additional, additional_size);
2299 pkt->
dts = timecode;
2301 pkt->
pts = timecode;
2316 lace_duration <= INT_MAX) {
2329 matroska_fix_ass_packet(matroska, pkt, lace_duration);
2336 matroska_merge_packets(matroska->
prev_pkt, pkt);
2348 if (pkt_data != data)
2354 int size, int64_t pos, uint64_t cluster_time,
2355 uint64_t block_duration,
int is_keyframe,
2356 uint8_t *additional, uint64_t additional_id,
int additional_size,
2357 int64_t cluster_pos)
2364 uint32_t *lace_size = NULL;
2376 if (!track || !track->
stream) {
2378 "Invalid stream %"PRIu64
" or size %u\n", num, size);
2380 }
else if (size <= 3)
2391 if (is_keyframe == -1)
2394 if (cluster_time != (uint64_t)-1
2395 && (block_time >= 0 || cluster_time >= -block_time)) {
2396 timecode = cluster_time + block_time;
2398 && timecode < track->end_timecode)
2405 if (timecode < matroska->skip_to_timecode)
2416 &lace_size, &laces);
2421 if (!block_duration)
2424 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
2428 for (n = 0; n < laces; n++) {
2429 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
2431 if (lace_size[n] > size) {
2450 timecode, lace_duration,
2451 pos, !n? is_keyframe : 0,
2452 additional, additional_id, additional_size);
2458 timecode = lace_duration ? timecode + lace_duration :
AV_NOPTS_VALUE;
2459 data += lace_size[
n];
2460 size -= lace_size[
n];
2474 matroska_cluster_incremental_parsing,
2489 matroska_clusters_incremental,
2494 matroska_cluster_incremental_parsing,
2502 blocks = blocks_list->
elem;
2506 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2510 if (!blocks[i].non_simple)
2513 blocks[i].bin.data, blocks[i].
bin.
size,
2539 res =
ebml_parse(matroska, matroska_clusters, &cluster);
2540 blocks_list = &cluster.
blocks;
2541 blocks = blocks_list->
elem;
2542 for (i=0; i<blocks_list->
nb_elem; i++)
2543 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2546 blocks[i].bin.data, blocks[i].
bin.
size,
2548 blocks[i].
duration, is_keyframe, NULL, 0, 0,
2571 int64_t timestamp,
int flags)
2576 int i,
index, index_sub, index_min;
2611 while(index_sub >= 0
2613 && tracks[i].stream->index_entries[index_sub].pos < st->
index_entries[index_min].
pos
2663 .
name =
"matroska,webm",