95 #define MODE_MATROSKAv2 0x01
96 #define MODE_WEBM 0x02
131 #define MAX_SEEKENTRY_SIZE 21
135 #define MAX_CUETRACKPOS_SIZE 42
138 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks
141 #define OPUS_SEEK_PREROLL 80000000
145 return (
av_log2(
id + 1) - 1) / 7 + 1;
173 while ((num + 1) >> bytes * 7)
193 bytes = needed_bytes;
198 num |= 1ULL << bytes * 7;
199 for (i = bytes - 1; i >= 0; i--)
212 for (i = bytes - 1; i >= 0; i--)
219 uint64_t tmp = 2*(val < 0 ? val^-1 :
val);
221 while (tmp>>=8) bytes++;
225 for (i = bytes - 1; i >= 0; i--)
274 uint64_t expectedsize)
318 if (numelements > 0) {
393 currentpos = seekhead->
filepos;
413 int64_t cluster_pos, int64_t relative_pos, int64_t
duration)
448 uint64_t pts = entry->
pts;
452 for (j = 0; j < num_tracks; j++)
453 tracks[j].has_cue = 0;
454 for (j = 0; j < cues->
num_entries - i && entry[j].
pts == pts; j++) {
456 av_assert0(tracknum>=0 && tracknum<num_tracks);
468 for (j = 0; j < num_tracks; j++)
469 tracks[j].has_cue = 0;
470 for (j = 0; j < cues->
num_entries - i && entry[j].
pts == pts; j++) {
472 av_assert0(tracknum>=0 && tracknum<num_tracks);
496 int first_header_size;
500 first_header_size = 30;
502 first_header_size = 42;
505 first_header_size, header_start, header_len) < 0) {
511 for (j = 0; j < 2; j++) {
514 for (j = 0; j < 3; j++)
515 avio_write(pb, header_start[j], header_len[j]);
549 av_dict_set(&dict,
"WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
581 "Error parsing AAC extradata, unable to determine samplerate.\n");
610 "Invalid extradata found, ALAC expects a 36-byte "
634 int ret, codecpriv_size;
654 for(i = 0; i < 0x5a - 8; i++)
709 int stereo_mode = atoi(tag->
value);
717 if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
718 stereo_mode != 10 && stereo_mode != 12) {
730 switch (stereo->
type) {
779 "The specified stereo mode is not valid.\n");
799 int output_sample_rate = 0;
800 int display_width_div = 1;
801 int display_height_div = 1;
833 }
else if (tag && tag->
value) {
848 codec_id =
"D_WEBVTT/CAPTIONS";
851 codec_id =
"D_WEBVTT/DESCRIPTIONS";
854 codec_id =
"D_WEBVTT/METADATA";
857 codec_id =
"D_WEBVTT/SUBTITLES";
892 "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
896 switch (codec->codec_type) {
900 if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
901 && 1.0/
av_q2d(st->avg_frame_rate) >
av_q2d(codec->time_base))
916 else if (!native_id) {
919 mkv->tracks[i].write_dts = 1;
936 display_width_div = 2;
940 display_height_div = 2;
944 if (((
tag =
av_dict_get(st->metadata,
"alpha_mode", NULL, 0)) && atoi(
tag->value)) ||
950 if (st->sample_aspect_ratio.num) {
951 int64_t d_width =
av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
952 if (d_width > INT_MAX) {
958 }
else if (display_width_div != 1 || display_height_div != 1) {
964 uint32_t color_space =
av_le2ne32(codec->codec_tag);
980 if (output_sample_rate)
1019 int i,
ret, default_stream_exists = 0;
1051 if (ret < 0)
return ret;
1092 if ((p = strrchr(p,
'-')) &&
1100 else if (*p >=
'a' && *p <=
'z')
1125 if (ret < 0)
return ret;
1167 if (ret < 0)
return ret;
1177 if (ret < 0)
return ret;
1187 if (ret < 0)
return ret;
1209 if (ret < 0)
return ret;
1217 const char *mimetype = NULL;
1233 mimetype = t->
value;
1244 "it cannot be deduced from the codec id.\n", i);
1303 "The Matroska muxer does not yet support muxing %s\n",
1340 if (ret < 0)
return ret;
1347 uint32_t segment_uid[4];
1352 for (i = 0; i < 4; i++)
1362 const char *ident =
"Lavf";
1371 AV_WB64(date_utc_buf, date_utc);
1439 int size = pkt_size + 4;
1486 memcpy(dst + offset, src, header.
blocksize);
1509 uint64_t additional_id = 0;
1510 int64_t discard_padding = 0;
1511 ebml_master block_group, block_additions, block_more;
1514 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1543 if (side_data && side_data_size >= 10) {
1553 additional_id =
AV_RB64(side_data);
1555 side_data_size -= 8;
1558 if ((side_data_size && additional_id == 1) || discard_padding) {
1570 if (data != pkt->
data)
1573 if (discard_padding) {
1577 if (side_data_size && additional_id == 1) {
1587 if ((side_data_size && additional_id == 1) || discard_padding) {
1596 for (i = 0; i < 2 && !
duration; i++) {
1597 int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1598 if (sscanf(*buf,
"%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1599 &s_hour, &s_min, &s_sec, &s_hsec,
1600 &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1601 s_min += 60 * s_hour;
1602 e_min += 60 * e_hour;
1603 s_sec += 60 * s_min;
1605 e_sec += 60 * e_min;
1606 s_hsec += 1000 * s_sec;
1607 e_hsec += 1000 * e_sec;
1609 duration = e_hsec - s_hsec;
1637 int id_size, settings_size,
size;
1640 const int flags = 0;
1650 size = id_size + 1 + settings_size + 1 + pkt->
size;
1653 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1663 avio_printf(pb,
"%.*s\n%.*s\n%.*s", id_size,
id, settings_size, settings, pkt->
size, pkt->
data);
1698 "Starting new cluster at offset %" PRIu64
" bytes, "
1699 "pts %" PRIu64
"dts %" PRIu64
"\n",
1717 int64_t relative_packet_pos;
1728 if ((int16_t)cluster_time != cluster_time) {
1757 if (ret < 0)
return ret;
1792 int64_t cluster_time;
1794 int start_new_cluster;
1814 start_new_cluster = keyframe;
1818 start_new_cluster = 1;
1823 cluster_size > 4 * 1024))) {
1824 start_new_cluster = 1;
1826 start_new_cluster = 0;
1829 if (mkv->
cluster_pos != -1 && start_new_cluster) {
1837 mkv->
is_dash ? start_new_cluster : 0);
1841 "Could not write cached audio packet ret:%d\n", ret);
1874 "Flushing cluster at offset %" PRIu64
" bytes\n",
1891 int64_t currentpos, cuespos;
1900 "Could not write cached audio packet ret:%d\n", ret);
1930 "Insufficient space reserved for cues: %d "
1931 "(needed: %" PRId64
").\n",
1936 if (cues_end < cuespos + mkv->reserve_cues_space)
1938 (cues_end - cuespos));
2012 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
2013 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
2015 {
"reserve_index_space",
"Reserve a given amount of space (in bytes) at the beginning of the file for the index (cues).",
OFFSET(reserve_cues_space),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
FLAGS },
2016 {
"cluster_size_limit",
"Store at most the provided amount of bytes in a cluster. ",
OFFSET(cluster_size_limit),
AV_OPT_TYPE_INT , { .i64 = -1 }, -1, INT_MAX,
FLAGS },
2017 {
"cluster_time_limit",
"Store at most the provided number of milliseconds in a cluster.",
OFFSET(cluster_time_limit),
AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX,
FLAGS },
2018 {
"dash",
"Create a WebM file conforming to WebM DASH specification",
OFFSET(is_dash),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
FLAGS },
2019 {
"dash_track_number",
"Track number for the DASH stream",
OFFSET(dash_track_number),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127,
FLAGS },
2023 #if CONFIG_MATROSKA_MUXER
2024 static const AVClass matroska_class = {
2034 .mime_type =
"video/x-matroska",
2035 .extensions =
"mkv",
2037 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2039 .video_codec = CONFIG_LIBX264_ENCODER ?
2052 .priv_class = &matroska_class,
2056 #if CONFIG_WEBM_MUXER
2057 static const AVClass webm_class = {
2067 .mime_type =
"video/webm",
2068 .extensions =
"webm",
2078 .priv_class = &webm_class,
2082 #if CONFIG_MATROSKA_AUDIO_MUXER
2083 static const AVClass mka_class = {
2092 .mime_type =
"audio/x-matroska",
2093 .extensions =
"mka",
2095 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2106 .priv_class = &mka_class,