32 #include "config_components.h"
83 #define EBML_UNKNOWN_LENGTH UINT64_MAX
84 #define NEEDS_CHECKING 2
88 #define SKIP_THRESHOLD 1024 * 1024
91 #define UNKNOWN_EQUIV 50 * 1024
147 typedef struct Ebml {
444 #define CHILD_OF(parent) { .def = { .n = parent } }
876 "Seek to desired resync point failed. Seeking to "
877 "earliest point available instead.\n");
879 last_pos + 1), SEEK_SET);
915 int max_size, uint64_t *number,
int eof_forbidden)
929 if (!total ||
read > max_size) {
933 "0x00 at pos %"PRId64
" (0x%"PRIx64
") invalid as first byte "
934 "of an EBML number\n",
pos,
pos);
937 "Length %d indicated by an EBML number's first byte 0x%02x "
938 "at pos %"PRId64
" (0x%"PRIx64
") exceeds max length %d.\n",
947 total = (total << 8) |
avio_r8(pb);
962 "Read error at pos. %"PRIu64
" (0x%"PRIx64
")\n",
968 "at pos. %"PRIu64
" (0x%"PRIx64
")\n",
pos,
pos);
983 if (res > 0 && *number + 1 == 1ULL << (7 * res))
993 uint64_t default_value, uint64_t *num)
998 *num = default_value;
1004 *num = (*num << 8) |
avio_r8(pb);
1019 *num = default_value;
1026 *num = ((uint64_t)*num << 8) |
avio_r8(pb);
1037 double default_value,
double *num)
1040 *num = default_value;
1042 }
else if (
size == 4) {
1044 }
else if (
size == 8) {
1057 const char *default_value,
char **str)
1062 if (
size == 0 && default_value) {
1128 level->length = length;
1148 *num = unum - ((1LL << (7 * res - 1)) - 1);
1218 return id && (
bits + 7) / 8 == (8 -
bits % 8);
1298 "at pos. %"PRIu64
" (0x%"PRIx64
")\n",
pos,
pos);
1305 pos_alt =
pos + res;
1330 "%"PRId64
"\n",
id,
pos);
1340 if ((
unsigned)
list->nb_elem + 1 >= UINT_MAX /
syntax->list_elem_size)
1343 &
list->alloc_elem_size,
1344 (
list->nb_elem + 1) *
syntax->list_elem_size);
1347 list->elem = newelem;
1364 uint64_t elem_end = pos_alt + length,
1367 if (elem_end < level_end) {
1369 }
else if (elem_end == level_end) {
1373 "Element at 0x%"PRIx64
" ending at 0x%"PRIx64
" exceeds "
1374 "containing master element ending at 0x%"PRIx64
"\n",
1375 pos, elem_end, level_end);
1382 "at 0x%"PRIx64
" inside parent with finite size\n",
pos);
1392 "Found unknown-length element 0x%"PRIX32
" other than "
1393 "a cluster at 0x%"PRIx64
". Spec-incompliant, but "
1394 "parsing will nevertheless be attempted.\n",
id,
pos);
1401 if (max_lengths[
syntax->type] && length > max_lengths[
syntax->type]) {
1404 "Invalid length 0x%"PRIx64
" > 0x%"PRIx64
" for element "
1405 "with ID 0x%"PRIX32
" at 0x%"PRIx64
"\n",
1406 length, max_lengths[
syntax->type],
id,
pos);
1409 "Element with ID 0x%"PRIX32
" at pos. 0x%"PRIx64
" has "
1410 "unknown length, yet the length of an element of its "
1411 "type must be known.\n",
id,
pos);
1414 "Found unknown-length element with ID 0x%"PRIX32
" at "
1415 "pos. 0x%"PRIx64
" for which no syntax for parsing is "
1416 "available.\n",
id,
pos);
1453 "Unknown element %"PRIX32
" at pos. 0x%"PRIx64
" with "
1454 "length 0x%"PRIx64
" considered as invalid data. Last "
1455 "known good position 0x%"PRIx64
", %d unknown elements"
1501 if (!level1_elem->
pos) {
1504 }
else if (level1_elem->
pos !=
pos)
1522 if ((res2 =
avio_skip(pb, length - 1)) >= 0) {
1561 if (elem->
count != UINT_MAX)
1584 void *data_off = (
char *)
data +
syntax[
i].data_offset;
1595 if (
syntax[
i].list_elem_size) {
1597 char *ptr =
list->elem;
1598 for (j = 0; j <
list->nb_elem;
1599 j++, ptr +=
syntax[
i].list_elem_size)
1603 list->alloc_elem_size = 0;
1618 int len_mask = 0x80,
size = 1, n = 1,
i;
1626 while (
size <= 8 && !(total & len_mask)) {
1632 total &= (len_mask - 1);
1634 total = (total << 8) | p->
buf[4 + n++];
1636 if (total + 1 == 1ULL << (7 *
size)){
1651 if (total < probelen)
1653 for (n = 4 +
size; n <= 4 +
size + total - probelen; n++)
1669 if (tracks[
i].num == num)
1680 uint8_t *
data = *buf;
1681 int isize = *buf_size;
1682 uint8_t *pkt_data =
NULL;
1684 int pkt_size = isize;
1688 if (pkt_size >= 10000000
U)
1691 switch (encodings[0].compression.algo) {
1697 if (header_size && !
header) {
1705 pkt_size = isize + header_size;
1710 memcpy(pkt_data,
header, header_size);
1711 memcpy(pkt_data + header_size,
data, isize);
1717 olen = pkt_size *= 3;
1724 pkt_data = newpktdata;
1736 z_stream zstream = { 0 };
1737 if (!pkt_size || inflateInit(&zstream) != Z_OK)
1739 zstream.next_in =
data;
1740 zstream.avail_in = isize;
1745 inflateEnd(&zstream);
1749 pkt_data = newpktdata;
1750 zstream.avail_out = pkt_size - zstream.total_out;
1751 zstream.next_out = pkt_data + zstream.total_out;
1753 }
while (
result == Z_OK && pkt_size < 10000000);
1754 pkt_size = zstream.total_out;
1755 inflateEnd(&zstream);
1756 if (
result != Z_STREAM_END) {
1757 if (
result == Z_MEM_ERROR)
1769 bz_stream bzstream = { 0 };
1770 if (!pkt_size || BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1772 bzstream.next_in =
data;
1773 bzstream.avail_in = isize;
1778 BZ2_bzDecompressEnd(&bzstream);
1782 pkt_data = newpktdata;
1783 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1784 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1785 result = BZ2_bzDecompress(&bzstream);
1786 }
while (
result == BZ_OK && pkt_size < 10000000);
1787 pkt_size = bzstream.total_out_lo32;
1788 BZ2_bzDecompressEnd(&bzstream);
1789 if (
result != BZ_STREAM_END) {
1790 if (
result == BZ_MEM_ERROR)
1806 *buf_size = pkt_size;
1821 for (
i = 0;
i <
list->nb_elem;
i++) {
1822 const char *lang = tags[
i].
lang &&
1823 strcmp(tags[
i].lang,
"und") ? tags[
i].
lang :
NULL;
1825 if (!tags[
i].
name) {
1833 if (tags[
i].def || !lang) {
1835 if (tags[
i].sub.nb_elem)
1842 if (tags[
i].sub.nb_elem)
1856 if (tags[
i].target.attachuid) {
1860 if (attachment[j].
uid == tags[
i].target.attachuid &&
1861 attachment[j].stream) {
1863 &attachment[j].stream->metadata,
NULL);
1869 "The tags at index %d refer to a "
1870 "non-existent attachment %"PRId64
".\n",
1871 i, tags[
i].target.attachuid);
1873 }
else if (tags[
i].target.chapteruid) {
1877 if (chapter[j].
uid == tags[
i].target.chapteruid &&
1878 chapter[j].chapter) {
1880 &chapter[j].chapter->metadata,
NULL);
1886 "The tags at index %d refer to a non-existent chapter "
1888 i, tags[
i].target.chapteruid);
1890 }
else if (tags[
i].target.trackuid) {
1894 if (track[j].
uid == tags[
i].target.trackuid &&
1897 &track[j].stream->metadata,
NULL);
1903 "The tags at index %d refer to a non-existent track "
1905 i, tags[
i].target.trackuid);
1909 tags[
i].target.type);
1928 "Max EBML element depth (%d) reached, "
1961 for (
i = 0;
i < seekhead_list->
nb_elem;
i++) {
1963 uint32_t
id = seekheads[
i].
id;
1971 if (!elem || elem->
parsed)
1994 uint64_t index_scale = 1;
2000 index_list = &matroska->
index;
2011 for (j = 0; j < pos_list->
nb_elem; j++) {
2014 if (track && track->
stream)
2043 unsigned nb_encodings,
2045 char **key_id_base64,
void *logctx)
2047 if (nb_encodings > 1) {
2049 "Multiple combined encodings not supported\n");
2054 if (encodings->
type) {
2060 if (!*key_id_base64)
2067 encodings->
scope = 0;
2079 encodings->
scope = 0;
2090 "Failed to decode codec private data\n");
2108 encodings->
scope & 1 &&
2118 static const char *
const aac_profiles[] = {
"MAIN",
"LC",
"SSR" };
2164 int block_last, block_type, block_size;
2170 if (block_size >
size)
2179 chmask =
av_dict_get(dict,
"WAVEFORMATEXTENSIBLE_CHANNEL_MASK",
NULL, 0);
2184 "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
2200 int minor, micro, bttb = 0;
2204 if (matroska->
muxingapp && sscanf(matroska->
muxingapp,
"Lavf57.%d.%d", &minor, µ) == 2)
2205 bttb = (minor >= 36 && minor <= 51 && micro >= 100);
2207 switch (field_order) {
2226 int *h_width,
int *h_height)
2228 switch (stereo_mode) {
2252 static const struct {
2255 } stereo_mode_conv [] = {
2256 #define STEREO_MODE_CONV(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
2257 [(STEREOMODETYPE)] = { .type = (STEREO3DTYPE), .flags = (FLAGS) },
2258 #define NOTHING(STEREOMODETYPE, WDIV, HDIV, WEBM)
2268 stereo->
type = stereo_mode_conv[stereo_mode].type;
2269 stereo->
flags = stereo_mode_conv[stereo_mode].flags;
2283 int has_mastering_primaries, has_mastering_luminance;
2288 mastering_meta = &
color->mastering_meta;
2290 has_mastering_primaries =
2291 mastering_meta->
r_x > 0 && mastering_meta->
r_y > 0 &&
2292 mastering_meta->
g_x > 0 && mastering_meta->
g_y > 0 &&
2293 mastering_meta->
b_x > 0 && mastering_meta->
b_y > 0 &&
2317 (
color->chroma_siting_vert - 1) << 7);
2333 if (has_mastering_primaries || has_mastering_luminance) {
2343 if (has_mastering_primaries) {
2354 if (has_mastering_luminance) {
2368 double pitch = proj->
pitch, yaw = proj->
yaw, roll = proj->
roll;
2372 if (pitch == 0.0 && yaw == 0.0 && roll == 0.0)
2377 if (pitch != 0.0 || (yaw != 0.0 && yaw != 180.0 && yaw != -180.0) ||
2380 "projection in stream %u (yaw %f, pitch %f, roll %f)\n",
2381 st->
index, yaw, pitch, roll);
2410 const uint8_t *priv_data = mkv_projection->
private.
data;
2412 size_t spherical_size;
2413 uint32_t l = 0, t = 0,
r = 0,
b = 0;
2414 uint32_t padding = 0;
2416 if (mkv_projection->
private.
size && priv_data[0] != 0) {
2431 if (
b >= UINT_MAX - t ||
r >= UINT_MAX - l) {
2433 "Invalid bounding rectangle coordinates "
2434 "%"PRIu32
",%"PRIu32
",%"PRIu32
",%"PRIu32
"\n",
2443 if (l || t ||
r ||
b)
2456 "Unknown spherical cubemap layout %"PRIu32
"\n",
layout);
2460 padding =
AV_RB32(priv_data + 8);
2468 "Unknown spherical metadata type %"PRIu64
"\n",
2511 for (
int i = 0;
i < mappings_list->
nb_elem;
i++) {
2515 switch (mapping->
type) {
2518 "Explicit block Addition Mapping type \"Use BlockAddIDValue\", value %"PRIu64
","
2519 " name \"%s\" found.\n", mapping->
value, mapping->
name ? mapping->
name :
"");
2527 "Invalid Block Addition Value 0x%"PRIx64
" for Block Addition Mapping Type "
2528 "0x%"PRIx64
", name \"%s\"\n", mapping->
value, mapping->
type,
2529 mapping->
name ? mapping->
name :
"");
2542 "Unknown Block Addition Mapping type 0x%"PRIx64
", value %"PRIu64
", name \"%s\"\n",
2544 if (mapping->
value < 2) {
2547 "Invalid Block Addition value 0x%"PRIu64
" for unknown Block Addition Mapping "
2548 "type %"PRIx64
", name \"%s\"\n", mapping->
value, mapping->
type,
2549 mapping->
name ? mapping->
name :
"");
2595 #define AAC_MAX_EXTRADATA_SIZE 5
2596 #define TTA_EXTRADATA_SIZE 22
2597 #define WAVPACK_EXTRADATA_SIZE 2
2606 int extradata_size = 0;
2609 if (!strcmp(track->
codec_id,
"A_MS/ACM") &&
2620 }
else if (!strcmp(track->
codec_id,
"A_QUICKTIME") &&
2625 uint16_t sample_size;
2632 if (sample_size == 8) {
2635 }
else if (sample_size == 16) {
2641 fourcc ==
MKTAG(
's',
'o',
'w',
't')) && sample_size == 8)
2684 extradata[0] = (
profile << 3) | ((sri & 0x0E) >> 1);
2685 extradata[1] = ((sri & 0x01) << 7) | (track->
audio.
channels << 3);
2686 if (strstr(track->
codec_id,
"SBR")) {
2688 extradata[2] = 0x56;
2689 extradata[3] = 0xE5;
2690 extradata[4] = 0x80 | (sri << 3);
2717 "Too large audio channel number %"PRIu64
2718 " or bitdepth %"PRIu64
". Skipping track.\n",
2729 bytestream_put_be32(&ptr,
AV_RB32(
"TTA1"));
2730 bytestream_put_le16(&ptr, 1);
2757 flavor = bytestream_get_be16(&ptr);
2776 static const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
2780 par->
bit_rate = sipr_bit_rate[flavor];
2785 *extradata_offset = 78;
2811 "in absence of valid CodecPrivate.\n");
2818 if (extradata_size > 0) {
2822 memcpy(par->
extradata, extradata, extradata_size);
2838 s, extradata_offset);
2860 (
AVRational){1, par->codec_id == AV_CODEC_ID_OPUS ?
2861 48000 : par->sample_rate});
2875 int *extradata_offset)
2877 if (!strcmp(track->
codec_id,
"V_MS/VFW/FOURCC") &&
2887 *extradata_offset = 40;
2889 }
else if (!strcmp(track->
codec_id,
"V_QUICKTIME") &&
2924 *extradata_offset = 26;
2944 int *extradata_offset)
2948 int display_width_mul = 1;
2949 int display_height_mul = 1;
2971 &display_width_mul, &display_height_mul);
2990 sizeof(uint32_t) * 4, 0);
2994 cropping = sd->
data;
3007 #if FF_API_R_FRAME_RATE
3029 snprintf(buf,
sizeof(buf),
"%s_%d",
3032 if (
planes[j].
uid == tracks[k].
uid && tracks[k].stream) {
3034 "stereo_mode", buf, 0);
3068 switch (data_component_id) {
3072 if (component_tag >= 0x30 && component_tag <= 0x37) {
3079 if (component_tag == 0x87) {
3089 "Unknown ARIB caption profile utilized: %02x / %04x\n",
3090 component_tag, data_component_id);
3096 if (!strcmp(track->
codec_id,
"D_WEBVTT/CAPTIONS")) {
3098 }
else if (!strcmp(track->
codec_id,
"D_WEBVTT/DESCRIPTIONS")) {
3100 }
else if (!strcmp(track->
codec_id,
"D_WEBVTT/METADATA")) {
3120 int extradata_offset = 0;
3122 char* key_id_base64 =
NULL;
3130 "Unknown or unsupported track type %"PRIu64
"\n",
3149 "Invalid sample rate %f, defaulting to 8000 instead.\n",
3157 if (default_duration > UINT64_MAX || default_duration < 0) {
3159 "Invalid frame rate %e. Cannot calculate default duration.\n",
3186 track, &key_id_base64, matroska->
ctx);
3220 if (key_id_base64) {
3226 if (strcmp(track->
language,
"und"))
3232 "Track TimestampScale too small %f, assuming 1.0.\n",
3241 1000 * 1000 * 1000);
3254 s, &extradata_offset);
3278 "Unknown/unsupported AVCodecID %s.\n", track->
codec_id);
3305 uint64_t max_start = 0;
3320 ebml.
max_size >
sizeof(uint64_t) ||
3324 "EBML version %"PRIu64
", doctype %s, doc version %"PRIu64,
3330 "EBML header using unsupported features\n"
3331 "(EBML version %"PRIu64
", doctype %s, doc version %"PRIu64
")\n",
3385 attachments = attachments_list->
elem;
3386 for (j = 0; j < attachments_list->
nb_elem; j++) {
3387 if (!(attachments[j].filename && attachments[j].mime &&
3388 attachments[j].bin.data && attachments[j].bin.size > 0)) {
3407 attachments[j].
stream = st;
3418 attachments[j].bin.size);
3430 chapters = chapters_list->
elem;
3433 (max_start == 0 || chapters[
i].start > max_start)) {
3439 max_start = chapters[
i].
start;
3487 uint32_t lace_size[256],
int *laces)
3490 uint8_t *
data = *buf;
3494 lace_size[0] =
size;
3510 for (n = 0; n < *laces - 1; n++) {
3518 lace_size[n] +=
temp;
3521 }
while (
temp == 0xff);
3526 lace_size[n] =
size - total;
3531 if (
size % (*laces))
3533 for (n = 0; n < *laces; n++)
3534 lace_size[n] =
size / *laces;
3551 total = lace_size[0] = num;
3553 for (n = 1; n < *laces - 1; n++) {
3559 if (lace_size[n - 1] + snum > (uint64_t)INT_MAX)
3562 lace_size[n] = lace_size[n - 1] + snum;
3563 total += lace_size[n];
3571 lace_size[*laces - 1] =
size - total;
3583 uint8_t *
data,
int size, uint64_t timecode,
3598 if (
size < cfs *
h / 2) {
3600 "Corrupt int4 RM-style audio packet size\n");
3603 for (x = 0; x <
h / 2; x++)
3604 memcpy(track->
audio.
buf + x * 2 *
w + y * cfs,
3605 data + x * cfs, cfs);
3609 "Corrupt sipr RM-style audio packet size\n");
3616 "Corrupt generic RM-style audio packet size\n");
3619 for (x = 0; x <
w /
sps; x++)
3621 sps * (
h * x + ((
h + 1) / 2) * (y & 1) + (y >> 1)),
3680 while (srclen >= 8) {
3690 multiblock = (
flags & 0x1800) != 0x1800;
3702 if (blocksize > srclen) {
3713 dstlen += blocksize + 32;
3727 srclen -= blocksize;
3728 offset += blocksize + 32;
3747 int dstlen = *
size + 8;
3755 memcpy(
dst + 8, *
data, dstlen - 8);
3767 uint8_t *
data,
int data_len,
3773 uint8_t *
id, *settings, *text, *buf;
3774 int id_len, settings_len, text_len;
3782 q =
data + data_len;
3787 if (*p ==
'\r' || *p ==
'\n') {
3796 if (p >= q || *p !=
'\n')
3803 if (*p ==
'\r' || *p ==
'\n') {
3804 settings_len = p - settings;
3812 if (p >= q || *p !=
'\n')
3818 while (text_len > 0) {
3819 const int len = text_len - 1;
3820 const uint8_t
c = p[
len];
3821 if (
c !=
'\r' &&
c !=
'\n')
3834 memcpy(
pkt->
data, text, text_len);
3844 memcpy(buf,
id, id_len);
3847 if (settings_len > 0) {
3855 memcpy(buf, settings, settings_len);
3881 const uint8_t *
data,
int size, uint64_t
id)
3891 "BlockAddID %"PRIu64
" is higher than the reported MaxBlockAdditionID %"PRIu64
" "
3898 for (
int i = 0;
i < mappings_list->
nb_elem;
i++) {
3899 if (
id != mappings[
i].
value)
3901 mapping = &mappings[
i];
3905 if (
id != 1 && !matroska->
is_webm && !mapping) {
3910 if (mapping && mapping->
type)
3916 int country_code, provider_code;
3917 int provider_oriented_code, application_identifier;
3918 size_t hdrplus_size;
3927 country_code = bytestream2_get_byteu(&bc);
3928 provider_code = bytestream2_get_be16u(&bc);
3934 provider_oriented_code = bytestream2_get_be16u(&bc);
3935 application_identifier = bytestream2_get_byteu(&bc);
3937 if (provider_oriented_code != 1 || application_identifier != 4)
3947 (uint8_t *)hdrplus, hdrplus_size)) < 0) {
3972 uint64_t timecode, uint64_t lace_duration,
3977 uint8_t *pkt_data =
data;
3985 "Error parsing a wavpack block.\n");
3998 "Error parsing a prores block.\n");
4006 if (!pkt_size && !nb_blockmore)
4012 "Unexpected BlockAdditions found in a Block from Track with TrackNumber %"PRIu64
" "
4013 "where MaxBlockAdditionID is 0\n", track->
num);
4036 for (
int i = 0;
i < nb_blockmore;
i++) {
4050 if (discard_padding) {
4061 if (discard_padding > 0) {
4062 AV_WL32A(side_data + 4, discard_padding);
4064 AV_WL32A(side_data, -discard_padding);
4102 uint32_t lace_size[256];
4103 int n,
flags, laces = 0;
4105 int trust_default_duration;
4117 if (!track ||
size < 3)
4120 if (!(st = track->
stream)) {
4122 "No stream associated to TrackNumber %"PRIu64
". "
4123 "Ignoring Block with this TrackNumber.\n", num);
4129 if (block_duration > INT64_MAX)
4130 block_duration = INT64_MAX;
4139 if (cluster_time != (uint64_t) -1 &&
4140 (block_time >= 0 || cluster_time >= -block_time)) {
4141 uint64_t timecode_cluster_in_track_tb = (
double) cluster_time / track->
time_scale;
4144 timecode < track->end_timecode)
4162 else if (!
ffstream(st)->skip_to_keyframe) {
4169 &pb.
pub, lace_size, &laces);
4180 trust_default_duration = 0;
4184 if (!block_duration && trust_default_duration)
4187 if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
4191 for (n = 0; n < laces; n++) {
4192 int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
4193 uint8_t *out_data =
data;
4218 timecode, lace_duration,
4228 blockmore, nb_blockmore,
4235 timecode = lace_duration ? timecode + lace_duration :
AV_NOPTS_VALUE;
4267 if (res >= 0 &&
block->bin.size > 0) {
4274 block->blockmore.nb_elem, cluster->
pos,
4275 block->discard_padding);
4285 "end of segment.\n");
4321 AVStream *st =
s->streams[stream_index];