571 if (
avio_seek(pb, last_pos + 1, SEEK_SET) < 0)
619 int max_size, uint64_t *number)
632 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
641 if (read > max_size) {
644 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
652 total = (total << 8) |
avio_r8(pb);
668 if (res > 0 && *number + 1 == 1ULL << (7 * res))
669 *number = 0xffffffffffffffULL;
687 *num = (*num << 8) |
avio_r8(pb);
700 }
else if (size == 4) {
702 }
else if (size == 8){
804 *num = unum - ((1LL << (7*res - 1)) - 1);
813 uint32_t
id,
void *
data)
816 for (i=0; syntax[i].
id; i++)
817 if (
id == syntax[i].
id)
849 for (i=0; syntax[i].
id; i++)
850 switch (syntax[i].type) {
852 *(uint64_t *)((
char *)data+syntax[i].
data_offset) = syntax[i].def.
u;
855 *(
double *)((
char *)data+syntax[i].data_offset) = syntax[i].
def.
f;
860 if (syntax[i].def.
s) {
889 uint32_t
id = syntax->
id;
911 if (max_lengths[syntax->
type] && length > max_lengths[syntax->
type]) {
913 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for syntax element %i\n",
914 length, max_lengths[syntax->
type], syntax->
type);
919 switch (syntax->
type) {
944 for (i=0; syntax[i].
id; i++) {
945 void *data_off = (
char *)data + syntax[i].data_offset;
946 switch (syntax[i].type) {
951 if (syntax[i].list_elem_size) {
953 char *ptr = list->
elem;
971 int len_mask = 0x80,
size = 1, n = 1, i;
979 while (
size <= 8 && !(total & len_mask)) {
985 total &= (len_mask - 1);
987 total = (total << 8) | p->
buf[4 + n++];
999 if (total < probelen)
1001 for (n = 4+
size; n <= 4+
size+total-probelen; n++)
1017 if (tracks[i].num == num)
1029 int isize = *buf_size;
1032 int pkt_size = isize;
1036 if (pkt_size >= 10000000)
1039 switch (encodings[0].compression.algo) {
1047 pkt_size = isize + header_size;
1052 memcpy(pkt_data, header, header_size);
1053 memcpy(pkt_data + header_size, data, isize);
1059 olen = pkt_size *= 3;
1065 pkt_data = newpktdata;
1077 z_stream zstream = {0};
1078 if (inflateInit(&zstream) != Z_OK)
1080 zstream.next_in =
data;
1081 zstream.avail_in = isize;
1086 inflateEnd(&zstream);
1089 pkt_data = newpktdata;
1090 zstream.avail_out = pkt_size - zstream.total_out;
1091 zstream.next_out = pkt_data + zstream.total_out;
1092 result = inflate(&zstream, Z_NO_FLUSH);
1093 }
while (result==Z_OK && pkt_size<10000000);
1094 pkt_size = zstream.total_out;
1095 inflateEnd(&zstream);
1096 if (result != Z_STREAM_END) {
1097 if (result == Z_MEM_ERROR)
1108 bz_stream bzstream = {0};
1109 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1111 bzstream.next_in =
data;
1112 bzstream.avail_in = isize;
1117 BZ2_bzDecompressEnd(&bzstream);
1120 pkt_data = newpktdata;
1121 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1122 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1123 result = BZ2_bzDecompress(&bzstream);
1124 }
while (result==BZ_OK && pkt_size<10000000);
1125 pkt_size = bzstream.total_out_lo32;
1126 BZ2_bzDecompressEnd(&bzstream);
1127 if (result != BZ_STREAM_END) {
1128 if (result == BZ_MEM_ERROR)
1142 *buf_size = pkt_size;
1150 AVPacket *pkt, uint64_t display_duration)
1153 char *layer, *ptr = pkt->
data, *end = ptr+pkt->
size;
1154 for (; *ptr!=
',' && ptr<end-1; ptr++);
1157 for (; *ptr!=
',' && ptr<end-1; ptr++);
1159 int64_t end_pts = pkt->
pts + display_duration;
1161 int ec = matroska->
time_scale * end_pts / 10000000;
1162 int sh, sm, ss, eh, em, es,
len;
1163 sh = sc/360000; sc -= 360000*sh;
1164 sm = sc/ 6000; sc -= 6000*sm;
1165 ss = sc/ 100; sc -= 100*ss;
1166 eh = ec/360000; ec -= 360000*eh;
1167 em = ec/ 6000; ec -= 6000*em;
1168 es = ec/ 100; ec -= 100*es;
1173 snprintf(line->
data, len,
"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
1174 layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
1184 int old_size = out->
size;
1203 for (i=0; i < list->
nb_elem; i++) {
1204 const char *lang = tags[i].
lang && strcmp(tags[i].lang,
"und") ?
1207 if (!tags[i].
name) {
1211 if (prefix) snprintf(key,
sizeof(key),
"%s/%s", prefix, tags[i].name);
1212 else av_strlcpy(key, tags[i].name,
sizeof(key));
1213 if (tags[i].def || !lang) {
1215 if (tags[i].sub.nb_elem)
1222 if (tags[i].sub.nb_elem)
1236 if (tags[i].target.attachuid) {
1239 if (attachment[j].uid == tags[i].target.attachuid
1242 &attachment[j].stream->metadata,
NULL);
1243 }
else if (tags[i].target.chapteruid) {
1246 if (chapter[j].uid == tags[i].target.chapteruid
1249 &chapter[j].chapter->metadata,
NULL);
1250 }
else if (tags[i].target.trackuid) {
1253 if (track[j].uid == tags[i].target.trackuid && track[j].
stream)
1255 &track[j].stream->metadata,
NULL);
1267 uint32_t level_up = matroska->
level_up;
1274 if (idx >= seekhead_list->
nb_elem
1286 "Max EBML element depth (%d) reached, "
1291 level.
length = (uint64_t)-1;
1296 ret =
ebml_parse(matroska, matroska_segment, matroska);
1301 if (length == (uint64_t)-1)
1325 for (i = 0; i < seekhead_list->
nb_elem; i++) {
1327 if (seekhead[i].pos <= before_pos)
1346 int index_scale = 1;
1349 for (i = 0; i < seekhead_list->
nb_elem; i++)
1352 assert(i <= seekhead_list->nb_elem);
1356 index_list = &matroska->
index;
1357 index = index_list->
elem;
1363 for (i = 0; i < index_list->
nb_elem; i++) {
1366 for (j = 0; j < pos_list->
nb_elem; j++) {
1368 if (track && track->stream)
1371 index[i].
time/index_scale, 0, 0,
1379 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
1383 if (strstr(codec_id, aac_profiles[profile]))
1406 uint64_t max_start = 0;
1423 "EBML header using unsupported features\n"
1424 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
1443 res =
ebml_parse(matroska, matroska_segments, matroska);
1450 res =
ebml_parse(matroska, matroska_segment, matroska);
1468 int extradata_size = 0;
1469 int extradata_offset = 0;
1477 "Unknown or unsupported track type %"PRIu64
"\n",
1495 if (encodings_list->
nb_elem > 1) {
1497 "Multiple combined encodings not supported");
1498 }
else if (encodings_list->
nb_elem == 1) {
1499 if (encodings[0].type ||
1511 encodings[0].
scope = 0;
1513 "Unsupported encoding type");
1523 "Failed to decode codec private data\n");
1543 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC")
1549 extradata_offset = 40;
1550 }
else if (!strcmp(track->
codec_id,
"A_MS/ACM")
1561 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME")
1584 if (extradata ==
NULL)
1586 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
1587 extradata[1] = ((sri&0x01) << 7) | (track->
audio.
channels<<3);
1588 if (strstr(track->
codec_id,
"SBR")) {
1590 extradata[2] = 0x56;
1591 extradata[3] = 0xE5;
1592 extradata[4] = 0x80 | (sri<<3);
1602 if (extradata ==
NULL)
1604 AV_WB32(extradata, extradata_size);
1605 memcpy(&extradata[4],
"alac", 4);
1610 extradata_size = 30;
1612 if (extradata ==
NULL)
1624 extradata_offset = 26;
1640 if (flavor <= 0 || track->audio.coded_framesize <= 0 ||
1650 const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1655 extradata_offset = 78;
1662 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
1675 if (strcmp(track->language,
"und"))
1679 if (track->flag_default)
1681 if (track->flag_forced)
1688 }
else if(track->codec_priv.data && track->codec_priv.size > 0){
1695 track->codec_priv.data + extradata_offset,
1696 track->codec_priv.size);
1708 st->
codec->
width * track->video.display_height,
1713 if (track->default_duration) {
1715 1000000000, track->default_duration, 30000);
1730 attachements = attachements_list->elem;
1731 for (j=0; j<attachements_list->nb_elem; j++) {
1732 if (!(attachements[j].filename && attachements[j].mime &&
1733 attachements[j].bin.data && attachements[j].bin.size > 0)) {
1747 memcpy(st->
codec->
extradata, attachements[j].bin.data, attachements[j].bin.size);
1756 attachements[j].stream = st;
1760 chapters = chapters_list->elem;
1761 for (i=0; i<chapters_list->nb_elem; i++)
1763 && (max_start==0 || chapters[i].start > max_start)) {
1764 chapters[i].chapter =
1766 chapters[i].start, chapters[i].end,
1769 "title", chapters[i].title, 0);
1770 max_start = chapters[i].start;
1795 matroska->
packets = newpackets;
1825 int* buf_size,
int type,
1826 uint32_t **lace_buf,
int *laces)
1828 int res = 0, n,
size = *buf_size;
1830 uint32_t *lace_size;
1838 *lace_buf[0] =
size;
1846 lace_size =
av_mallocz(*laces *
sizeof(
int));
1854 for (n = 0; res == 0 && n < *laces - 1; n++) {
1861 lace_size[n] += temp;
1867 total += lace_size[n];
1869 if (size <= total) {
1874 lace_size[n] = size - total;
1879 if (size % (*laces)) {
1883 for (n = 0; n < *laces; n++)
1884 lace_size[n] = size / *laces;
1893 "EBML block data error\n");
1899 total = lace_size[0] = num;
1900 for (n = 1; res == 0 && n < *laces - 1; n++) {
1906 "EBML block data error\n");
1912 lace_size[n] = lace_size[n - 1] + snum;
1913 total += lace_size[n];
1915 if (size <= total) {
1919 lace_size[*laces - 1] = size - total;
1925 *lace_buf = lace_size;
1935 uint64_t timecode, uint64_t
duration,
1950 if (size < cfs * h / 2) {
1952 "Corrupt int4 RM-style audio packet size\n");
1955 for (x=0; x<h/2; x++)
1956 memcpy(track->
audio.
buf+x*2*w+y*cfs,
1961 "Corrupt sipr RM-style audio packet size\n");
1964 memcpy(track->
audio.
buf + y*w, data, w);
1966 if (size < sps * w / sps) {
1968 "Corrupt generic RM-style audio packet size\n");
1971 for (x=0; x<w/sps; x++)
1972 memcpy(track->
audio.
buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2007 int ret, offset = 0;
2009 if (srclen < 12 || track->stream->codec->extradata_size < 2)
2018 while (srclen >= 8) {
2024 uint32_t crc =
AV_RL32(src + 4);
2028 multiblock = (flags & 0x1800) != 0x1800;
2040 if (blocksize > srclen) {
2045 tmp =
av_realloc(dst, dstlen + blocksize + 32);
2051 dstlen += blocksize + 32;
2054 AV_WL32(dst + offset + 4, blocksize + 24);
2055 AV_WL16(dst + offset + 8, ver);
2056 AV_WL16(dst + offset + 10, 0);
2057 AV_WL32(dst + offset + 12, 0);
2058 AV_WL32(dst + offset + 16, 0);
2059 AV_WL32(dst + offset + 20, samples);
2060 AV_WL32(dst + offset + 24, flags);
2061 AV_WL32(dst + offset + 28, crc);
2062 memcpy (dst + offset + 32, src, blocksize);
2065 srclen -= blocksize;
2066 offset += blocksize + 32;
2083 uint64_t timecode, uint64_t
duration,
2084 int64_t pos,
int is_keyframe)
2088 int offset = 0, res;
2091 if (encodings && encodings->
scope & 1) {
2104 if (pkt_data != data)
2121 bytestream_put_be32(&buf, pkt_size);
2122 bytestream_put_be32(&buf,
MKBETAG(
'i',
'c',
'p',
'f'));
2125 memcpy(pkt->
data + offset, pkt_data, pkt_size);
2127 if (pkt_data != data)
2130 pkt->
flags = is_keyframe;
2134 pkt->
dts = timecode;
2136 pkt->
pts = timecode;
2159 if (pkt_data != data)
2165 int size, int64_t pos, uint64_t cluster_time,
2166 uint64_t block_duration,
int is_keyframe,
2167 int64_t cluster_pos)
2174 uint32_t *lace_size =
NULL;
2175 int n,
flags, laces = 0;
2186 if (!track || !track->
stream) {
2188 "Invalid stream %"PRIu64
" or size %u\n", num, size);
2190 }
else if (size <= 3)
2200 if (is_keyframe == -1)
2203 if (cluster_time != (uint64_t)-1
2204 && (block_time >= 0 || cluster_time >= -block_time)) {
2205 timecode = cluster_time + block_time - track->
codec_delay;
2207 && timecode < track->end_timecode)
2214 if (!is_keyframe || timecode < matroska->skip_to_timecode)
2220 &lace_size, &laces);
2226 duration = block_duration / laces;
2227 if (block_duration != duration * laces) {
2229 "Incorrect block_duration, possibly corrupted container");
2233 block_duration = duration * laces;
2240 for (n = 0; n < laces; n++) {
2249 timecode, duration, pos);
2256 pos, !n? is_keyframe : 0);
2263 data += lace_size[n];
2277 matroska_cluster_incremental_parsing,
2292 matroska_clusters_incremental,
2297 matroska_cluster_incremental_parsing,
2305 blocks = blocks_list->
elem;
2309 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2311 if (!blocks[i].non_simple)
2314 blocks[i].bin.data, blocks[i].
bin.
size,
2322 if (res < 0) matroska->
done = 1;
2339 res =
ebml_parse(matroska, matroska_clusters, &cluster);
2340 blocks_list = &cluster.
blocks;
2341 blocks = blocks_list->
elem;
2342 for (i=0; i<blocks_list->
nb_elem && !res; i++)
2343 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2345 if (!blocks[i].non_simple)
2348 blocks[i].bin.data, blocks[i].
bin.
size,
2379 int64_t timestamp,
int flags)
2384 int i,
index, index_sub, index_min;
2423 index_min = index_sub;
2454 .
name =
"matroska,webm",