32 #include "config_components.h"
79 #define EBML_UNKNOWN_LENGTH UINT64_MAX
80 #define NEEDS_CHECKING 2
84 #define SKIP_THRESHOLD 1024 * 1024
87 #define UNKNOWN_EQUIV 50 * 1024
143 typedef struct Ebml {
428 #define CHILD_OF(parent) { .def = { .n = parent } }
825 uint32_t
id, int64_t position)
854 "Seek to desired resync point failed. Seeking to "
855 "earliest point available instead.\n");
857 last_pos + 1), SEEK_SET);
893 int max_size, uint64_t *number,
int eof_forbidden)
907 if (!total ||
read > max_size) {
911 "0x00 at pos %"PRId64
" (0x%"PRIx64
") invalid as first byte "
912 "of an EBML number\n",
pos,
pos);
915 "Length %d indicated by an EBML number's first byte 0x%02x "
916 "at pos %"PRId64
" (0x%"PRIx64
") exceeds max length %d.\n",
925 total = (total << 8) |
avio_r8(pb);
940 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
946 "at pos. %"PRIu64
" (0x%"PRIx64
")\n",
pos,
pos);
961 if (res > 0 && *number + 1 == 1ULL << (7 * res))
971 uint64_t default_value, uint64_t *num)
976 *num = default_value;
982 *num = (*num << 8) |
avio_r8(pb);
992 int64_t default_value, int64_t *num)
997 *num = default_value;
1004 *num = ((uint64_t)*num << 8) |
avio_r8(pb);
1015 double default_value,
double *num)
1018 *num = default_value;
1020 }
else if (
size == 4) {
1022 }
else if (
size == 8) {
1035 const char *default_value,
char **
str)
1040 if (
size == 0 && default_value) {
1094 uint64_t length, int64_t
pos)
1106 level->length = length;
1126 *num = unum - ((1LL << (7 * res - 1)) - 1);
1157 *(uint64_t *)dst =
syntax[
i].def.u;
1160 *(int64_t *) dst =
syntax[
i].def.i;
1163 *(
double *) dst =
syntax[
i].def.f;
1196 return id && (
bits + 7) / 8 == (8 -
bits % 8);
1204 uint32_t
id, int64_t
pos)
1276 "at pos. %"PRIu64
" (0x%"PRIx64
")\n",
pos,
pos);
1283 pos_alt =
pos + res;
1306 "%"PRId64
"\n",
id,
pos);
1316 if ((
unsigned)
list->nb_elem + 1 >= UINT_MAX /
syntax->list_elem_size)
1319 &
list->alloc_elem_size,
1320 (
list->nb_elem + 1) *
syntax->list_elem_size);
1323 list->elem = newelem;
1340 uint64_t elem_end = pos_alt + length,
1343 if (elem_end < level_end) {
1345 }
else if (elem_end == level_end) {
1349 "Element at 0x%"PRIx64
" ending at 0x%"PRIx64
" exceeds "
1350 "containing master element ending at 0x%"PRIx64
"\n",
1351 pos, elem_end, level_end);
1358 "at 0x%"PRIx64
" inside parent with finite size\n",
pos);
1368 "Found unknown-length element 0x%"PRIX32
" other than "
1369 "a cluster at 0x%"PRIx64
". Spec-incompliant, but "
1370 "parsing will nevertheless be attempted.\n",
id,
pos);
1377 if (max_lengths[
syntax->type] && length > max_lengths[
syntax->type]) {
1380 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for element "
1381 "with ID 0x%"PRIX32
" at 0x%"PRIx64
"\n",
1382 length, max_lengths[
syntax->type],
id,
pos);
1385 "Element with ID 0x%"PRIX32
" at pos. 0x%"PRIx64
" has "
1386 "unknown length, yet the length of an element of its "
1387 "type must be known.\n",
id,
pos);
1390 "Found unknown-length element with ID 0x%"PRIX32
" at "
1391 "pos. 0x%"PRIx64
" for which no syntax for parsing is "
1392 "available.\n",
id,
pos);
1429 "Unknown element %"PRIX32
" at pos. 0x%"PRIx64
" with "
1430 "length 0x%"PRIx64
" considered as invalid data. Last "
1431 "known good position 0x%"PRIx64
", %d unknown elements"
1477 if (!level1_elem->
pos) {
1480 }
else if (level1_elem->
pos !=
pos)
1498 if ((res2 =
avio_skip(pb, length - 1)) >= 0) {
1537 if (elem->
count != UINT_MAX)
1560 void *data_off = (
char *)
data +
syntax[
i].data_offset;
1571 if (
syntax[
i].list_elem_size) {
1573 char *ptr =
list->elem;
1574 for (j = 0; j <
list->nb_elem;
1575 j++, ptr +=
syntax[
i].list_elem_size)
1579 list->alloc_elem_size = 0;
1594 int len_mask = 0x80,
size = 1, n = 1,
i;
1602 while (
size <= 8 && !(total & len_mask)) {
1608 total &= (len_mask - 1);
1610 total = (total << 8) | p->
buf[4 + n++];
1612 if (total + 1 == 1ULL << (7 *
size)){
1627 if (total < probelen)
1629 for (n = 4 +
size; n <= 4 +
size + total - probelen; n++)
1645 if (tracks[
i].num == num)
1656 uint8_t *
data = *buf;
1657 int isize = *buf_size;
1658 uint8_t *pkt_data =
NULL;
1660 int pkt_size = isize;
1664 if (pkt_size >= 10000000
U)
1667 switch (encodings[0].compression.algo) {
1673 if (header_size && !
header) {
1681 pkt_size = isize + header_size;
1686 memcpy(pkt_data,
header, header_size);
1687 memcpy(pkt_data + header_size,
data, isize);
1693 olen = pkt_size *= 3;
1700 pkt_data = newpktdata;
1712 z_stream zstream = { 0 };
1713 if (!pkt_size || inflateInit(&zstream) != Z_OK)
1715 zstream.next_in =
data;
1716 zstream.avail_in = isize;
1721 inflateEnd(&zstream);
1725 pkt_data = newpktdata;
1726 zstream.avail_out = pkt_size - zstream.total_out;
1727 zstream.next_out = pkt_data + zstream.total_out;
1729 }
while (
result == Z_OK && pkt_size < 10000000);
1730 pkt_size = zstream.total_out;
1731 inflateEnd(&zstream);
1732 if (
result != Z_STREAM_END) {
1733 if (
result == Z_MEM_ERROR)
1745 bz_stream bzstream = { 0 };
1746 if (!pkt_size || BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1748 bzstream.next_in =
data;
1749 bzstream.avail_in = isize;
1754 BZ2_bzDecompressEnd(&bzstream);
1758 pkt_data = newpktdata;
1759 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1760 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1761 result = BZ2_bzDecompress(&bzstream);
1762 }
while (
result == BZ_OK && pkt_size < 10000000);
1763 pkt_size = bzstream.total_out_lo32;
1764 BZ2_bzDecompressEnd(&bzstream);
1765 if (
result != BZ_STREAM_END) {
1766 if (
result == BZ_MEM_ERROR)
1782 *buf_size = pkt_size;
1797 for (
i = 0;
i <
list->nb_elem;
i++) {
1798 const char *lang = tags[
i].
lang &&
1799 strcmp(tags[
i].lang,
"und") ? tags[
i].
lang :
NULL;
1801 if (!tags[
i].
name) {
1809 if (tags[
i].def || !lang) {
1811 if (tags[
i].
sub.nb_elem)
1818 if (tags[
i].
sub.nb_elem)
1832 if (tags[
i].target.attachuid) {
1836 if (attachment[j].
uid == tags[
i].target.attachuid &&
1837 attachment[j].stream) {
1839 &attachment[j].stream->metadata,
NULL);
1845 "The tags at index %d refer to a "
1846 "non-existent attachment %"PRId64
".\n",
1847 i, tags[
i].target.attachuid);
1849 }
else if (tags[
i].target.chapteruid) {
1853 if (chapter[j].
uid == tags[
i].target.chapteruid &&
1854 chapter[j].chapter) {
1856 &chapter[j].chapter->metadata,
NULL);
1862 "The tags at index %d refer to a non-existent chapter "
1864 i, tags[
i].target.chapteruid);
1866 }
else if (tags[
i].target.trackuid) {
1870 if (track[j].
uid == tags[
i].target.trackuid &&
1873 &track[j].stream->metadata,
NULL);
1879 "The tags at index %d refer to a non-existent track "
1881 i, tags[
i].target.trackuid);
1885 tags[
i].target.type);
1904 "Max EBML element depth (%d) reached, "
1937 for (
i = 0;
i < seekhead_list->
nb_elem;
i++) {
1939 uint32_t
id = seekheads[
i].
id;
1947 if (!elem || elem->
parsed)
1970 uint64_t index_scale = 1;
1976 index_list = &matroska->
index;
1987 for (j = 0; j < pos_list->
nb_elem; j++) {
1990 if (track && track->
stream)
2020 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
2066 int block_last, block_type, block_size;
2072 if (block_size >
size)
2081 chmask =
av_dict_get(dict,
"WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
NULL, 0);
2086 "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
2102 int minor, micro, bttb = 0;
2107 bttb = (
minor >= 36 && minor <= 51 && micro >= 100);
2109 switch (field_order) {
2128 int *h_width,
int *h_height)
2130 switch (stereo_mode) {
2155 int has_mastering_primaries, has_mastering_luminance;
2160 mastering_meta = &
color->mastering_meta;
2162 has_mastering_primaries =
2163 mastering_meta->
r_x > 0 && mastering_meta->
r_y > 0 &&
2164 mastering_meta->
g_x > 0 && mastering_meta->
g_y > 0 &&
2165 mastering_meta->
b_x > 0 && mastering_meta->
b_y > 0 &&
2189 (
color->chroma_siting_vert - 1) << 7);
2198 (uint8_t *)metadata,
size);
2207 if (has_mastering_primaries || has_mastering_luminance) {
2216 if (has_mastering_primaries) {
2227 if (has_mastering_luminance) {
2240 double pitch = proj->
pitch, yaw = proj->
yaw, roll = proj->
roll;
2244 if (pitch == 0.0 && yaw == 0.0 && roll == 0.0)
2249 if (pitch != 0.0 || (yaw != 0.0 && yaw != 180.0 && yaw != -180.0) ||
2252 "projection in stream %u (yaw %f, pitch %f, roll %f)\n",
2253 st->
index, yaw, pitch, roll);
2279 const uint8_t *priv_data = mkv_projection->
private.
data;
2281 size_t spherical_size;
2282 uint32_t l = 0, t = 0,
r = 0,
b = 0;
2283 uint32_t padding = 0;
2286 if (mkv_projection->
private.
size && priv_data[0] != 0) {
2301 if (
b >= UINT_MAX - t ||
r >= UINT_MAX - l) {
2303 "Invalid bounding rectangle coordinates "
2304 "%"PRIu32
",%"PRIu32
",%"PRIu32
",%"PRIu32
"\n",
2313 if (l || t ||
r ||
b)
2326 "Unknown spherical cubemap layout %"PRIu32
"\n",
layout);
2330 padding =
AV_RB32(priv_data + 8);
2338 "Unknown spherical metadata type %"PRIu64
"\n",
2382 for (
int i = 0;
i < mappings_list->
nb_elem;
i++) {
2385 switch (mapping->
type) {
2386 case MKBETAG(
'd',
'v',
'c',
'C'):
2387 case
MKBETAG(
'd',
'v',
'v',
'C'):
2394 "Unknown block additional mapping type 0x%"PRIx64
", value %"PRIu64
", name \"%s\"\n",
2442 uint8_t *extradata =
NULL;
2443 int extradata_size = 0;
2444 int extradata_offset = 0;
2449 char* key_id_base64 =
NULL;
2458 "Unknown or unsupported track type %"PRIu64
"\n",
2477 "Invalid sample rate %f, defaulting to 8000 instead.\n",
2485 if (default_duration > UINT64_MAX || default_duration < 0) {
2487 "Invalid frame rate %e. Cannot calculate default duration.\n",
2503 if (encodings_list->
nb_elem > 1) {
2505 "Multiple combined encodings not supported");
2506 }
else if (encodings_list->
nb_elem == 1) {
2507 if (encodings[0].
type) {
2508 if (encodings[0].encryption.key_id.size > 0) {
2511 const int b64_size =
AV_BASE64_SIZE(encodings[0].encryption.key_id.size);
2513 if (key_id_base64 ==
NULL)
2517 encodings[0].encryption.key_id.data,
2518 encodings[0].encryption.key_id.size);
2520 encodings[0].
scope = 0;
2522 "Unsupported encoding type");
2533 encodings[0].
scope = 0;
2535 "Unsupported encoding type");
2545 "Failed to decode codec private data\n");
2564 encodings[0].
scope & 1 &&
2583 if (key_id_base64) {
2589 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC") &&
2600 extradata_offset = 40;
2601 }
else if (!strcmp(track->
codec_id,
"A_MS/ACM") &&
2615 }
else if (!strcmp(track->
codec_id,
"A_QUICKTIME")
2619 uint16_t sample_size;
2625 if (sample_size == 8) {
2628 }
else if (sample_size == 16) {
2637 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME") &&
2693 extradata[0] = (
profile << 3) | ((sri & 0x0E) >> 1);
2694 extradata[1] = ((sri & 0x01) << 7) | (track->
audio.
channels << 3);
2695 if (strstr(track->
codec_id,
"SBR")) {
2697 extradata[2] = 0x56;
2698 extradata[3] = 0xE5;
2699 extradata[4] = 0x80 | (sri << 3);
2712 AV_WB32(extradata, extradata_size);
2713 memcpy(&extradata[4],
"alac", 4);
2722 "Too large audio channel number %"PRIu64
2723 " or bitdepth %"PRIu64
". Skipping track.\n",
2732 extradata_size = 22;
2737 bytestream_put_be32(&ptr,
AV_RB32(
"TTA1"));
2738 bytestream_put_le16(&ptr, 1);
2749 extradata_offset = 26;
2764 flavor = bytestream_get_be16(&ptr);
2783 static const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
2792 extradata_offset = 78;
2804 "in absence of valid CodecPrivate.\n");
2821 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
2825 "Track TimestampScale too small %f, assuming 1.0.\n",
2830 1000 * 1000 * 1000);
2839 if (strcmp(track->
language,
"und"))
2872 int display_width_mul = 1;
2873 int display_height_mul = 1;
2907 #if FF_API_R_FRAME_RATE
2927 snprintf(buf,
sizeof(buf),
"%s_%d",
2930 if (
planes[j].
uid == tracks[k].
uid && tracks[k].stream) {
2932 "stereo_mode", buf, 0);
2970 (
AVRational){1, st->codecpar->codec_id == AV_CODEC_ID_OPUS ?
2971 48000 : st->codecpar->sample_rate});
2981 if (!strcmp(track->
codec_id,
"D_WEBVTT/CAPTIONS")) {
2983 }
else if (!strcmp(track->
codec_id,
"D_WEBVTT/DESCRIPTIONS")) {
2985 }
else if (!strcmp(track->
codec_id,
"D_WEBVTT/METADATA")) {
3011 uint64_t max_start = 0;
3026 ebml.
max_size >
sizeof(uint64_t) ||
3030 "EBML version %"PRIu64
", doctype %s, doc version %"PRIu64,
3036 "EBML header using unsupported features\n"
3037 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
3089 attachments = attachments_list->
elem;
3090 for (j = 0; j < attachments_list->
nb_elem; j++) {
3091 if (!(attachments[j].filename && attachments[j].mime &&
3092 attachments[j].bin.data && attachments[j].bin.size > 0)) {
3111 attachments[j].
stream = st;
3122 attachments[j].bin.size);
3134 chapters = chapters_list->
elem;
3137 (max_start == 0 || chapters[
i].start > max_start)) {
3143 max_start = chapters[
i].
start;
3191 uint32_t lace_size[256],
int *laces)
3194 uint8_t *
data = *buf;
3198 lace_size[0] =
size;
3214 for (n = 0; n < *laces - 1; n++) {
3222 lace_size[n] +=
temp;
3225 }
while (
temp == 0xff);
3230 lace_size[n] =
size - total;
3235 if (
size % (*laces))
3237 for (n = 0; n < *laces; n++)
3238 lace_size[n] =
size / *laces;
3255 total = lace_size[0] = num;
3257 for (n = 1; n < *laces - 1; n++) {
3263 if (lace_size[n - 1] + snum > (uint64_t)INT_MAX)
3266 lace_size[n] = lace_size[n - 1] + snum;
3267 total += lace_size[n];
3275 lace_size[*laces - 1] =
size - total;
3287 uint8_t *
data,
int size, uint64_t timecode,
3302 if (
size < cfs *
h / 2) {
3304 "Corrupt int4 RM-style audio packet size\n");
3307 for (x = 0; x <
h / 2; x++)
3308 memcpy(track->
audio.
buf + x * 2 *
w + y * cfs,
3309 data + x * cfs, cfs);
3313 "Corrupt sipr RM-style audio packet size\n");
3320 "Corrupt generic RM-style audio packet size\n");
3323 for (x = 0; x <
w /
sps; x++)
3325 sps * (
h * x + ((
h + 1) / 2) * (y & 1) + (y >> 1)),
3366 uint8_t *dst =
NULL;
3384 while (srclen >= 8) {
3394 multiblock = (
flags & 0x1800) != 0x1800;
3406 if (blocksize > srclen) {
3417 dstlen += blocksize + 32;
3428 memcpy(dst +
offset + 32,
src, blocksize);
3431 srclen -= blocksize;
3432 offset += blocksize + 32;
3451 int dstlen = *
size + 8;
3459 memcpy(dst + 8, *
data, dstlen - 8);
3471 uint8_t *
data,
int data_len,
3477 uint8_t *
id, *settings, *text, *buf;
3478 int id_len, settings_len, text_len;
3486 q =
data + data_len;
3491 if (*p ==
'\r' || *p ==
'\n') {
3500 if (p >= q || *p !=
'\n')
3507 if (*p ==
'\r' || *p ==
'\n') {
3508 settings_len = p - settings;
3516 if (p >= q || *p !=
'\n')
3522 while (text_len > 0) {
3523 const int len = text_len - 1;
3524 const uint8_t
c = p[
len];
3525 if (
c !=
'\r' &&
c !=
'\n')
3538 memcpy(
pkt->
data, text, text_len);
3548 memcpy(buf,
id, id_len);
3551 if (settings_len > 0) {
3559 memcpy(buf, settings, settings_len);
3586 uint64_t timecode, uint64_t lace_duration,
3588 uint8_t *additional, uint64_t additional_id,
int additional_size,
3589 int64_t discard_padding)
3591 uint8_t *pkt_data =
data;
3599 "Error parsing a wavpack block.\n");
3612 "Error parsing a prores block.\n");
3620 if (!pkt_size && !additional_size)
3639 if (additional_size > 0) {
3642 additional_size + 8);
3647 AV_WB64(side_data, additional_id);
3648 memcpy(side_data + 8, additional, additional_size);
3651 if (discard_padding) {
3662 if (discard_padding > 0) {
3663 AV_WL32(side_data + 4, discard_padding);
3665 AV_WL32(side_data, -discard_padding);
3692 int size, int64_t
pos, uint64_t cluster_time,
3694 uint8_t *additional, uint64_t additional_id,
int additional_size,
3695 int64_t cluster_pos, int64_t discard_padding)
3703 uint32_t lace_size[256];
3704 int n,
flags, laces = 0;
3706 int trust_default_duration;
3718 if (!track ||
size < 3)
3721 if (!(st = track->
stream)) {
3723 "No stream associated to TrackNumber %"PRIu64
". "
3724 "Ignoring Block with this TrackNumber.\n", num);
3730 if (block_duration > INT64_MAX)
3731 block_duration = INT64_MAX;
3740 if (cluster_time != (uint64_t) -1 &&
3741 (block_time >= 0 || cluster_time >= -block_time)) {
3742 uint64_t timecode_cluster_in_track_tb = (
double) cluster_time / track->
time_scale;
3745 timecode < track->end_timecode)
3763 else if (!
ffstream(st)->skip_to_keyframe) {
3770 &pb.
pub, lace_size, &laces);
3781 trust_default_duration = 0;
3785 if (!block_duration && trust_default_duration)
3788 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
3792 for (n = 0; n < laces; n++) {
3793 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
3794 uint8_t *out_data =
data;
3819 timecode, lace_duration,
3829 additional, additional_id, additional_size,
3836 timecode = lace_duration ? timecode + lace_duration :
AV_NOPTS_VALUE;
3868 if (res >= 0 &&
block->bin.size > 0) {
3870 uint8_t* additional =
block->additional.size > 0 ?
3877 block->additional.size, cluster->
pos,
3878 block->discard_padding);
3888 "end of segment.\n");
3920 int64_t timestamp,
int flags)
3924 AVStream *st =
s->streams[stream_index];
4003 #if CONFIG_WEBM_DASH_MANIFEST_DEMUXER
4005 int64_t start_time_ns;
4006 int64_t end_time_ns;
4007 int64_t start_offset;
4016 static CueDesc get_cue_desc(
AVFormatContext *
s, int64_t ts, int64_t cues_start) {
4025 return (CueDesc) {-1, -1, -1, -1};
4026 for (
i = 1;
i < nb_index_entries;
i++) {
4027 if (index_entries[
i - 1].timestamp * matroska->
time_scale <= ts &&
4028 index_entries[
i].timestamp * matroska->
time_scale > ts) {
4033 if (index_entries[
i].timestamp > matroska->
duration)
4034 return (CueDesc) {-1, -1, -1, -1};
4037 if (
i != nb_index_entries - 1) {
4056 int64_t cluster_pos, before_pos;
4069 uint64_t cluster_id, cluster_length;
4075 if (
read < 0 || cluster_id != 0xF43B675)
4089 cluster_pos += 4 +
read + cluster_length;
4102 static int buffer_size_after_time_downloaded(int64_t time_ns,
double search_sec, int64_t
bps,
4103 double min_buffer,
double*
buffer,
4107 double nano_seconds_per_second = 1000000000.0;
4108 double time_sec = time_ns / nano_seconds_per_second;
4110 int64_t time_to_search_ns = (int64_t)(search_sec * nano_seconds_per_second);
4111 int64_t end_time_ns = time_ns + time_to_search_ns;
4112 double sec_downloaded = 0.0;
4113 CueDesc desc_curr = get_cue_desc(
s, time_ns, cues_start);
4114 if (desc_curr.start_time_ns == -1)
4116 *sec_to_download = 0.0;
4119 if (time_ns > desc_curr.start_time_ns) {
4120 int64_t cue_nano = desc_curr.end_time_ns - time_ns;
4121 double percent = (
double)(cue_nano) / (desc_curr.end_time_ns - desc_curr.start_time_ns);
4122 double cueBytes = (desc_curr.end_offset - desc_curr.start_offset) * percent;
4123 double timeToDownload = (cueBytes * 8.0) /
bps;
4125 sec_downloaded += (cue_nano / nano_seconds_per_second) - timeToDownload;
4126 *sec_to_download += timeToDownload;
4129 if (desc_curr.end_time_ns >= end_time_ns) {
4130 double desc_end_time_sec = desc_curr.end_time_ns / nano_seconds_per_second;
4131 double percent_to_sub = search_sec / (desc_end_time_sec - time_sec);
4132 sec_downloaded = percent_to_sub * sec_downloaded;
4133 *sec_to_download = percent_to_sub * *sec_to_download;
4136 if ((sec_downloaded + *
buffer) <= min_buffer) {
4141 desc_curr = get_cue_desc(
s, desc_curr.end_time_ns, cues_start);
4144 while (desc_curr.start_time_ns != -1) {
4145 int64_t desc_bytes = desc_curr.end_offset - desc_curr.start_offset;
4146 int64_t desc_ns = desc_curr.end_time_ns - desc_curr.start_time_ns;
4147 double desc_sec = desc_ns / nano_seconds_per_second;
4148 double bits = (desc_bytes * 8.0);
4149 double time_to_download =
bits /
bps;
4151 sec_downloaded += desc_sec - time_to_download;
4152 *sec_to_download += time_to_download;
4154 if (desc_curr.end_time_ns >= end_time_ns) {
4155 double desc_end_time_sec = desc_curr.end_time_ns / nano_seconds_per_second;
4156 double percent_to_sub = search_sec / (desc_end_time_sec - time_sec);
4157 sec_downloaded = percent_to_sub * sec_downloaded;
4158 *sec_to_download = percent_to_sub * *sec_to_download;
4160 if ((sec_downloaded + *
buffer) <= min_buffer)
4165 if ((sec_downloaded + *
buffer) <= min_buffer) {
4170 desc_curr = get_cue_desc(
s, desc_curr.end_time_ns, cues_start);
4183 static int64_t webm_dash_manifest_compute_bandwidth(
AVFormatContext *
s, int64_t cues_start)
4188 double bandwidth = 0.0;
4191 int64_t prebuffer_ns = 1000000000;
4193 double nano_seconds_per_second = 1000000000.0;
4194 int64_t prebuffered_ns = time_ns + prebuffer_ns;
4195 double prebuffer_bytes = 0.0;
4196 int64_t temp_prebuffer_ns = prebuffer_ns;
4197 int64_t pre_bytes, pre_ns;
4198 double pre_sec, prebuffer, bits_per_second;
4199 CueDesc desc_beg = get_cue_desc(
s, time_ns, cues_start);
4202 CueDesc desc_end = desc_beg;
4206 while (desc_end.start_time_ns != -1 && desc_end.end_time_ns < prebuffered_ns) {
4208 prebuffer_bytes += desc_end.end_offset - desc_end.start_offset;
4209 temp_prebuffer_ns -= desc_end.end_time_ns - desc_end.start_time_ns;
4210 desc_end = get_cue_desc(
s, desc_end.end_time_ns, cues_start);
4212 if (desc_end.start_time_ns == -1) {
4216 bits_per_second = 0.0;
4220 pre_bytes = desc_end.end_offset - desc_end.start_offset;
4221 pre_ns = desc_end.end_time_ns - desc_end.start_time_ns;
4224 pre_sec = pre_ns / nano_seconds_per_second;
4226 pre_bytes * ((temp_prebuffer_ns / nano_seconds_per_second) / pre_sec);
4228 prebuffer = prebuffer_ns / nano_seconds_per_second;
4231 bits_per_second = 0.0;
4233 int64_t desc_bytes = desc_end.end_offset - desc_beg.start_offset;
4234 int64_t desc_ns = desc_end.end_time_ns - desc_beg.start_time_ns;
4235 double desc_sec, calc_bits_per_second, percent, mod_bits_per_second;
4236 if (desc_bytes <= 0)
4239 desc_sec = desc_ns / nano_seconds_per_second;
4240 calc_bits_per_second = (desc_bytes * 8) / desc_sec;
4243 percent = (desc_bytes - prebuffer_bytes) / desc_bytes;
4244 mod_bits_per_second = calc_bits_per_second * percent;
4246 if (prebuffer < desc_sec) {
4252 int64_t
bps = (int64_t)(mod_bits_per_second) + 1;
4253 const double min_buffer = 0.0;
4254 double buffer = prebuffer;
4255 double sec_to_download = 0.0;
4257 int rv = buffer_size_after_time_downloaded(prebuffered_ns, search_sec,
bps,
4258 min_buffer, &
buffer, &sec_to_download,
4262 }
else if (rv == 0) {
4268 desc_end = get_cue_desc(
s, desc_end.end_time_ns, cues_start);
4269 }
while (desc_end.start_time_ns != -1);
4271 if (bandwidth < bits_per_second) bandwidth = bits_per_second;
4273 return (int64_t)bandwidth;
4285 int64_t cues_start = -1, cues_end = -1, before_pos, bandwidth;
4294 if (
i >= seekhead_list->
nb_elem)
return -1;
4298 if (
avio_seek(matroska->
ctx->
pb, cues_start, SEEK_SET) == cues_start) {
4302 uint64_t cues_length, cues_id;
4310 cues_end = cues_start + 4 + bytes_read + cues_length - 1;
4313 if (cues_start == -1 || cues_end == -1)
return -1;
4329 if (cues_start <= init_range)
4333 bandwidth = webm_dash_manifest_compute_bandwidth(
s, cues_start);
4334 if (bandwidth < 0)
return -1;
4350 bprint.str[--bprint.len] =
'\0';
4389 buf = strrchr(
s->url,
'/');