651 if (
avio_seek(pb, last_pos + 1, SEEK_SET) < 0)
700 int max_size, uint64_t *number)
713 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
722 if (read > max_size) {
725 "Invalid EBML number size tag 0x%02x at pos %"PRIu64
" (0x%"PRIx64
")\n",
733 total = (total << 8) |
avio_r8(pb);
749 if (res > 0 && *number + 1 == 1ULL << (7 * res))
750 *number = 0xffffffffffffffULL;
768 *num = (*num << 8) |
avio_r8(pb);
791 *num = (*num << 8) |
avio_r8(pb);
908 *num = unum - ((1LL << (7 * res - 1)) - 1);
917 uint32_t
id,
void *
data)
920 for (i = 0; syntax[i].
id; i++)
921 if (
id == syntax[i].
id)
953 for (i = 0; syntax[i].
id; i++)
954 switch (syntax[i].
type) {
956 *(uint64_t *) ((
char *) data + syntax[i].
data_offset) = syntax[i].def.
u;
959 *(
double *) ((
char *) data + syntax[i].data_offset) = syntax[i].
def.
f;
964 if (syntax[i].def.
s) {
993 uint32_t
id = syntax->
id;
1004 list->
elem = newelem;
1014 if (max_lengths[syntax->
type] && length > max_lengths[syntax->
type]) {
1016 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for syntax element %i\n",
1017 length, max_lengths[syntax->
type], syntax->
type);
1022 switch (syntax->
type) {
1064 for (i = 0; syntax[i].
id; i++) {
1065 void *data_off = (
char *) data + syntax[i].data_offset;
1066 switch (syntax[i].
type) {
1075 if (syntax[i].list_elem_size) {
1077 char *ptr = list->
elem;
1078 for (j = 0; j < list->
nb_elem;
1096 int len_mask = 0x80,
size = 1,
n = 1, i;
1104 while (
size <= 8 && !(total & len_mask)) {
1110 total &= (len_mask - 1);
1112 total = (total << 8) | p->
buf[4 +
n++];
1124 if (total < probelen)
1126 for (
n = 4 +
size;
n <= 4 +
size + total - probelen;
n++)
1142 if (tracks[i].num == num)
1154 int isize = *buf_size;
1157 int pkt_size = isize;
1161 if (pkt_size >= 10000000
U)
1164 switch (encodings[0].compression.algo) {
1170 if (header_size && !header) {
1178 pkt_size = isize + header_size;
1183 memcpy(pkt_data, header, header_size);
1184 memcpy(pkt_data + header_size, data, isize);
1190 olen = pkt_size *= 3;
1196 pkt_data = newpktdata;
1209 z_stream zstream = { 0 };
1210 if (inflateInit(&zstream) != Z_OK)
1212 zstream.next_in =
data;
1213 zstream.avail_in = isize;
1218 inflateEnd(&zstream);
1221 pkt_data = newpktdata;
1222 zstream.avail_out = pkt_size - zstream.total_out;
1223 zstream.next_out = pkt_data + zstream.total_out;
1225 result = inflate(&zstream, Z_NO_FLUSH);
1227 result = Z_MEM_ERROR;
1228 }
while (result == Z_OK && pkt_size < 10000000);
1229 pkt_size = zstream.total_out;
1230 inflateEnd(&zstream);
1231 if (result != Z_STREAM_END) {
1232 if (result == Z_MEM_ERROR)
1244 bz_stream bzstream = { 0 };
1245 if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1247 bzstream.next_in =
data;
1248 bzstream.avail_in = isize;
1253 BZ2_bzDecompressEnd(&bzstream);
1256 pkt_data = newpktdata;
1257 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1258 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1260 result = BZ2_bzDecompress(&bzstream);
1262 result = BZ_MEM_ERROR;
1263 }
while (result == BZ_OK && pkt_size < 10000000);
1264 pkt_size = bzstream.total_out_lo32;
1265 BZ2_bzDecompressEnd(&bzstream);
1266 if (result != BZ_STREAM_END) {
1267 if (result == BZ_MEM_ERROR)
1281 *buf_size = pkt_size;
1296 for (i = 0; i < list->
nb_elem; i++) {
1297 const char *lang = tags[i].
lang &&
1298 strcmp(tags[i].lang,
"und") ? tags[i].
lang : NULL;
1300 if (!tags[i].
name) {
1305 snprintf(key,
sizeof(key),
"%s/%s", prefix, tags[i].name);
1308 if (tags[i].def || !lang) {
1310 if (tags[i].sub.nb_elem)
1317 if (tags[i].sub.nb_elem)
1331 if (tags[i].target.attachuid) {
1334 if (attachment[j].uid == tags[i].target.attachuid &&
1337 &attachment[j].stream->metadata, NULL);
1338 }
else if (tags[i].target.chapteruid) {
1341 if (chapter[j].uid == tags[i].target.chapteruid &&
1344 &chapter[j].chapter->metadata, NULL);
1345 }
else if (tags[i].target.trackuid) {
1348 if (track[j].uid == tags[i].target.trackuid && track[j].
stream)
1350 &track[j].stream->metadata, NULL);
1362 uint32_t level_up = matroska->
level_up;
1370 if (idx >= seekhead_list->
nb_elem ||
1382 "Max EBML element depth (%d) reached, "
1387 level.
length = (uint64_t) -1;
1392 ret =
ebml_parse(matroska, matroska_segment, matroska);
1397 if (length == (uint64_t) -1)
1421 for (i = 0; i < seekhead_list->
nb_elem; i++) {
1423 if (seekhead[i].pos <= before_pos)
1444 int index_scale = 1;
1447 index_list = &matroska->
index;
1448 index = index_list->
elem;
1454 for (i = 0; i < index_list->
nb_elem; i++) {
1457 for (j = 0; j < pos_list->
nb_elem; j++) {
1460 if (track && track->stream)
1463 index[i].
time / index_scale, 0, 0,
1474 for (i = 0; i < seekhead_list->
nb_elem; i++)
1486 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
1489 for (profile = 0; profile <
FF_ARRAY_ELEMS(aac_profiles); profile++)
1490 if (strstr(codec_id, aac_profiles[profile]))
1509 time_t creation_time = date_utc / 1000000000 + 978307200;
1510 struct tm *ptm = gmtime(&creation_time);
1512 strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", ptm);
1513 av_dict_set(metadata,
"creation_time", buffer, 0);
1537 int block_last, block_type, block_size;
1543 if (block_size > size)
1552 chmask =
av_dict_get(dict,
"WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
1554 uint64_t
mask = strtol(chmask->
value, NULL, 0);
1555 if (!mask || mask & ~0x3ffffULL) {
1557 "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
1585 int extradata_size = 0;
1586 int extradata_offset = 0;
1587 uint32_t fourcc = 0;
1589 char* key_id_base64 = NULL;
1598 "Unknown or unsupported track type %"PRIu64
"\n",
1618 if (encodings_list->
nb_elem > 1) {
1620 "Multiple combined encodings not supported");
1621 }
else if (encodings_list->
nb_elem == 1) {
1622 if (encodings[0].
type) {
1623 if (encodings[0].encryption.key_id.size > 0) {
1626 const int b64_size =
AV_BASE64_SIZE(encodings[0].encryption.key_id.size);
1628 if (key_id_base64 == NULL)
1632 encodings[0].encryption.key_id.data,
1635 encodings[0].
scope = 0;
1637 "Unsupported encoding type");
1650 encodings[0].
scope = 0;
1652 "Unsupported encoding type");
1662 "Failed to decode codec private data\n");
1684 if (key_id_base64) {
1690 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC") &&
1701 extradata_offset = 40;
1702 }
else if (!strcmp(track->
codec_id,
"A_MS/ACM") &&
1708 0, NULL, NULL, NULL, NULL);
1714 }
else if (!strcmp(track->
codec_id,
"A_QUICKTIME")
1723 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME") &&
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);
1786 AV_WB32(extradata, extradata_size);
1787 memcpy(&extradata[4],
"alac", 4);
1792 extradata_size = 30;
1797 NULL, NULL, NULL, NULL);
1812 extradata_offset = 26;
1825 0, NULL, NULL, NULL, NULL);
1848 static const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
1853 extradata_offset = 78;
1866 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
1871 1000 * 1000 * 1000);
1880 if (strcmp(track->language,
"und"))
1884 if (track->flag_default)
1886 if (track->flag_forced)
1893 }
else if (track->codec_priv.data && track->codec_priv.size > 0) {
1897 track->codec_priv.data + extradata_offset,
1898 track->codec_priv.size);
1914 st->
codec->
width * track->video.display_height,
1919 if (track->default_duration) {
1921 1000000000, track->default_duration, 30000);
1922 #if FF_API_R_FRAME_RATE
1933 if (track->video.alpha_mode)
1937 for (j=0; j < track->operation.combine_planes.nb_elem; j++) {
1941 snprintf(buf,
sizeof(buf),
"%s_%d",
1944 if (planes[j].uid == tracks[k].uid) {
1946 "stereo_mode", buf, 0);
1952 track->video.stereo_mode != 10 && track->video.stereo_mode != 12) {
1965 if (track->codec_delay > 0) {
1970 if (track->seek_preroll > 0) {
1979 if (!strcmp(track->codec_id,
"D_WEBVTT/CAPTIONS")) {
1981 }
else if (!strcmp(track->codec_id,
"D_WEBVTT/DESCRIPTIONS")) {
1983 }
else if (!strcmp(track->codec_id,
"D_WEBVTT/METADATA")) {
1989 matroska->contains_ssa = 1;
2003 uint64_t max_start = 0;
2011 if (
ebml_parse(matroska, ebml_syntax, &ebml) ||
2013 ebml.
max_size >
sizeof(uint64_t) ||
2018 "EBML header using unsupported features\n"
2019 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
2025 "EBML header using unsupported features\n"
2026 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
2043 res =
ebml_parse(matroska, matroska_segments, matroska);
2050 res =
ebml_parse(matroska, matroska_segment, matroska);
2069 attachments = attachments_list->
elem;
2070 for (j = 0; j < attachments_list->
nb_elem; j++) {
2071 if (!(attachments[j].filename && attachments[j].mime &&
2072 attachments[j].bin.data && attachments[j].
bin.
size > 0)) {
2094 attachments[j].
stream = st;
2098 chapters = chapters_list->
elem;
2099 for (i = 0; i < chapters_list->
nb_elem; i++)
2101 (max_start == 0 || chapters[i].
start > max_start)) {
2105 chapters[i].
start, chapters[i].
end,
2107 if (chapters[i].chapter) {
2109 "title", chapters[i].
title, 0);
2111 max_start = chapters[i].
start;
2139 matroska->
packets = newpackets;
2169 int *buf_size,
int type,
2170 uint32_t **lace_buf,
int *laces)
2172 int res = 0,
n,
size = *buf_size;
2174 uint32_t *lace_size;
2182 *lace_buf[0] =
size;
2190 lace_size =
av_mallocz(*laces *
sizeof(
int));
2199 for (
n = 0; res == 0 &&
n < *laces - 1;
n++) {
2201 if (size <= total) {
2207 lace_size[
n] +=
temp;
2214 if (size <= total) {
2219 lace_size[
n] = size - total;
2224 if (size % (*laces)) {
2228 for (
n = 0;
n < *laces;
n++)
2229 lace_size[
n] = size / *laces;
2237 if (n < 0 || num > INT_MAX) {
2239 "EBML block data error\n");
2245 total = lace_size[0] = num;
2246 for (
n = 1; res == 0 &&
n < *laces - 1;
n++) {
2250 if (r < 0 || lace_size[
n - 1] + snum > (uint64_t)INT_MAX) {
2252 "EBML block data error\n");
2258 lace_size[
n] = lace_size[
n - 1] + snum;
2259 total += lace_size[
n];
2261 if (size <= total) {
2265 lace_size[*laces - 1] = size - total;
2271 *lace_buf = lace_size;
2294 if (size < cfs * h / 2) {
2296 "Corrupt int4 RM-style audio packet size\n");
2299 for (x = 0; x < h / 2; x++)
2300 memcpy(track->
audio.
buf + x * 2 * w + y * cfs,
2301 data + x * cfs, cfs);
2305 "Corrupt sipr RM-style audio packet size\n");
2308 memcpy(track->
audio.
buf + y * w, data, w);
2310 if (size < sps * w / sps || h<=0 || w%sps) {
2312 "Corrupt generic RM-style audio packet size\n");
2315 for (x = 0; x < w / sps; x++)
2317 sps * (h * x + ((h + 1) / 2) * (y & 1) + (y >> 1)),
2318 data + x * sps, sps);
2359 if (srclen < 12 || track->stream->codec->extradata_size < 2)
2368 while (srclen >= 8) {
2374 uint32_t crc =
AV_RL32(src + 4);
2378 multiblock = (flags & 0x1800) != 0x1800;
2390 if (blocksize > srclen) {
2395 tmp =
av_realloc(dst, dstlen + blocksize + 32);
2401 dstlen += blocksize + 32;
2404 AV_WL32(dst + offset + 4, blocksize + 24);
2405 AV_WL16(dst + offset + 8, ver);
2406 AV_WL16(dst + offset + 10, 0);
2407 AV_WL32(dst + offset + 12, 0);
2408 AV_WL32(dst + offset + 16, 0);
2409 AV_WL32(dst + offset + 20, samples);
2410 AV_WL32(dst + offset + 24, flags);
2411 AV_WL32(dst + offset + 28, crc);
2412 memcpy(dst + offset + 32, src, blocksize);
2415 srclen -= blocksize;
2416 offset += blocksize + 32;
2439 int id_len, settings_len, text_len;
2447 q = data + data_len;
2452 if (*p ==
'\r' || *p ==
'\n') {
2461 if (p >= q || *p !=
'\n')
2468 if (*p ==
'\r' || *p ==
'\n') {
2469 settings_len = p - settings;
2477 if (p >= q || *p !=
'\n')
2483 while (text_len > 0) {
2484 const int len = text_len - 1;
2486 if (c !=
'\r' && c !=
'\n')
2501 memcpy(pkt->
data, text, text_len);
2511 memcpy(buf,
id, id_len);
2514 if (settings_len > 0) {
2522 memcpy(buf, settings, settings_len);
2529 pkt->
pts = timecode;
2546 uint64_t timecode, uint64_t lace_duration,
2548 uint8_t *additional, uint64_t additional_id,
int additional_size,
2549 int64_t discard_padding)
2556 if (encodings && !encodings->
type && encodings->
scope & 1) {
2567 "Error parsing a wavpack block.\n");
2570 if (pkt_data != data)
2589 bytestream_put_be32(&buf, pkt_size);
2590 bytestream_put_be32(&buf,
MKBETAG(
'i',
'c',
'p',
'f'));
2593 memcpy(pkt->
data + offset, pkt_data, pkt_size);
2595 if (pkt_data != data)
2601 if (additional_size > 0) {
2604 additional_size + 8);
2610 AV_WB64(side_data, additional_id);
2611 memcpy(side_data + 8, additional, additional_size);
2614 if (discard_padding) {
2630 pkt->
dts = timecode;
2632 pkt->
pts = timecode;
2647 lace_duration <= INT_MAX) {
2664 if (pkt_data != data)
2670 int size, int64_t pos, uint64_t cluster_time,
2672 uint8_t *additional, uint64_t additional_id,
int additional_size,
2673 int64_t cluster_pos, int64_t discard_padding)
2680 uint32_t *lace_size = NULL;
2683 int trust_default_duration = 1;
2693 if (!track || !track->
stream) {
2695 "Invalid stream %"PRIu64
" or size %u\n", num, size);
2697 }
else if (size <= 3)
2708 if (is_keyframe == -1)
2711 if (cluster_time != (uint64_t) -1 &&
2712 (block_time >= 0 || cluster_time >= -block_time)) {
2713 timecode = cluster_time + block_time - track->
codec_delay;
2715 timecode < track->end_timecode)
2724 if (timecode < matroska->skip_to_timecode)
2735 &lace_size, &laces);
2744 trust_default_duration = 0;
2748 if (!block_duration && trust_default_duration)
2751 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
2755 for (n = 0; n < laces; n++) {
2756 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
2758 if (lace_size[n] > size) {
2777 timecode, lace_duration,
2783 timecode, lace_duration, pos,
2784 !n ? is_keyframe : 0,
2785 additional, additional_id, additional_size,
2792 timecode = lace_duration ? timecode + lace_duration :
AV_NOPTS_VALUE;
2793 data += lace_size[
n];
2794 size -= lace_size[
n];
2808 matroska_cluster_incremental_parsing,
2823 matroska_clusters_incremental,
2828 matroska_cluster_incremental_parsing,
2836 blocks = blocks_list->
elem;
2840 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2844 if (!blocks[i].non_simple)
2874 res =
ebml_parse(matroska, matroska_clusters, &cluster);
2875 blocks_list = &cluster.
blocks;
2876 blocks = blocks_list->
elem;
2877 for (i = 0; i < blocks_list->
nb_elem; i++)
2878 if (blocks[i].bin.size > 0 && blocks[i].
bin.
data) {
2883 is_keyframe, NULL, 0, 0, pos,
2906 int64_t timestamp,
int flags)
2911 int i,
index, index_sub, index_min;
2949 while (index_sub >= 0 &&
2951 tracks[i].stream->index_entries[index_sub].pos < st->
index_entries[index_min].
pos &&
3018 for (i = 1; i < nb_index_entries; i++) {
3019 if (index_entries[i - 1].timestamp * matroska->
time_scale <= ts &&
3027 if (i != nb_index_entries - 1) {
3043 int64_t cluster_pos, before_pos;
3048 if (index < 0)
return 0;
3052 int64_t cluster_id = 0, cluster_length = 0;
3058 if (cluster_id != 0xF43B675) {
3069 cluster_pos += cluster_length + 12;
3080 double min_buffer,
double*
buffer,
3084 double nano_seconds_per_second = 1000000000.0;
3085 double time_sec = time_ns / nano_seconds_per_second;
3087 int64_t time_to_search_ns = (int64_t)(search_sec * nano_seconds_per_second);
3088 int64_t end_time_ns = time_ns + time_to_search_ns;
3089 double sec_downloaded = 0.0;
3093 *sec_to_download = 0.0;
3097 int64_t cue_nano = desc_curr.
end_time_ns - time_ns;
3100 double timeToDownload = (cueBytes * 8.0) /
bps;
3102 sec_downloaded += (cue_nano / nano_seconds_per_second) - timeToDownload;
3103 *sec_to_download += timeToDownload;
3107 double desc_end_time_sec = desc_curr.
end_time_ns / nano_seconds_per_second;
3108 double percent_to_sub = search_sec / (desc_end_time_sec - time_sec);
3109 sec_downloaded = percent_to_sub * sec_downloaded;
3110 *sec_to_download = percent_to_sub * *sec_to_download;
3113 if ((sec_downloaded + *buffer) <= min_buffer) {
3124 double desc_sec = desc_ns / nano_seconds_per_second;
3125 double bits = (desc_bytes * 8.0);
3126 double time_to_download = bits /
bps;
3128 sec_downloaded += desc_sec - time_to_download;
3129 *sec_to_download += time_to_download;
3132 double desc_end_time_sec = desc_curr.
end_time_ns / nano_seconds_per_second;
3133 double percent_to_sub = search_sec / (desc_end_time_sec - time_sec);
3134 sec_downloaded = percent_to_sub * sec_downloaded;
3135 *sec_to_download = percent_to_sub * *sec_to_download;
3137 if ((sec_downloaded + *buffer) <= min_buffer)
3142 if ((sec_downloaded + *buffer) <= min_buffer) {
3149 *buffer = *buffer + sec_downloaded;
3164 double bandwidth = 0.0;
3168 int64_t prebuffer_ns = 1000000000;
3170 double nano_seconds_per_second = 1000000000.0;
3171 int64_t prebuffered_ns = time_ns + prebuffer_ns;
3172 double prebuffer_bytes = 0.0;
3173 int64_t temp_prebuffer_ns = prebuffer_ns;
3174 int64_t pre_bytes, pre_ns;
3175 double pre_sec, prebuffer, bits_per_second;
3198 pre_sec = pre_ns / nano_seconds_per_second;
3200 pre_bytes * ((temp_prebuffer_ns / nano_seconds_per_second) / pre_sec);
3202 prebuffer = prebuffer_ns / nano_seconds_per_second;
3205 bits_per_second = 0.0;
3209 double desc_sec = desc_ns / nano_seconds_per_second;
3210 double calc_bits_per_second = (desc_bytes * 8) / desc_sec;
3213 double percent = (desc_bytes - prebuffer_bytes) / desc_bytes;
3214 double mod_bits_per_second = calc_bits_per_second * percent;
3216 if (prebuffer < desc_sec) {
3222 int64_t
bps = (int64_t)(mod_bits_per_second) + 1;
3223 const double min_buffer = 0.0;
3224 double buffer = prebuffer;
3225 double sec_to_download = 0.0;
3228 min_buffer, &buffer, &sec_to_download,
3232 }
else if (rv == 0) {
3233 bits_per_second = (double)(bps);
3240 if (bandwidth < bits_per_second) bandwidth = bits_per_second;
3242 return (int64_t)bandwidth;
3251 int64_t cues_start = -1, cues_end = -1, before_pos, bandwidth;
3255 for (i = 0; i < seekhead_list->
nb_elem; i++)
3259 if (i >= seekhead_list->
nb_elem)
return -1;
3263 if (
avio_seek(matroska->
ctx->
pb, cues_start, SEEK_SET) == cues_start) {
3264 uint64_t cues_length = 0, cues_id = 0;
3267 cues_end = cues_start + cues_length + 11;
3270 if (cues_start == -1 || cues_end == -1)
return -1;
3283 if (bandwidth < 0)
return -1;
3292 if (!buf)
return -1;
3295 snprintf(buf, (i + 1) * 20 *
sizeof(
char),
3298 strncat(buf,
",",
sizeof(
char));
3323 if (!buf)
return -1;
3328 if (!buf)
return AVERROR(ENOMEM);
3346 .
name =
"matroska,webm",
3348 .extensions =
"mkv,mk3d,mka,mks",
3355 .mime_type =
"audio/webm,audio/x-matroska,video/webm,video/x-matroska"
3359 .
name =
"webm_dash_manifest",