95 #define MODE_MATROSKAv2 0x01
96 #define MODE_WEBM 0x02
133 #define MAX_SEEKENTRY_SIZE 21
137 #define MAX_CUETRACKPOS_SIZE 42
140 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks
143 #define OPUS_SEEK_PREROLL 80000000
147 return (
av_log2(
id + 1) - 1) / 7 + 1;
175 while ((num + 1) >> bytes * 7)
195 bytes = needed_bytes;
200 num |= 1ULL << bytes * 7;
201 for (i = bytes - 1; i >= 0; i--)
214 for (i = bytes - 1; i >= 0; i--)
221 uint64_t tmp = 2*(val < 0 ? val^-1 :
val);
223 while (tmp>>=8) bytes++;
227 for (i = bytes - 1; i >= 0; i--)
276 uint64_t expectedsize)
320 if (numelements > 0) {
395 currentpos = seekhead->
filepos;
415 int64_t cluster_pos, int64_t relative_pos, int64_t
duration)
450 uint64_t
pts = entry->
pts;
454 for (j = 0; j < num_tracks; j++)
455 tracks[j].has_cue = 0;
458 av_assert0(tracknum>=0 && tracknum<num_tracks);
470 for (j = 0; j < num_tracks; j++)
471 tracks[j].has_cue = 0;
474 av_assert0(tracknum>=0 && tracknum<num_tracks);
496 const uint8_t *header_start[3];
498 int first_header_size;
502 first_header_size = 30;
504 first_header_size = 42;
507 first_header_size, header_start, header_len) < 0) {
513 for (j = 0; j < 2; j++) {
516 for (j = 0; j < 3; j++)
517 avio_write(pb, header_start[j], header_len[j]);
551 av_dict_set(&dict,
"WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
583 "Error parsing AAC extradata, unable to determine samplerate.\n");
613 "Invalid extradata found, ALAC expects a 36-byte "
637 int ret, codecpriv_size;
657 for(i = 0; i < 0x5a - 8; i++)
714 int stereo_mode = atoi(tag->
value);
722 if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
723 stereo_mode != 10 && stereo_mode != 12) {
736 switch (stereo->
type) {
788 "The specified stereo mode is not valid.\n");
809 int output_sample_rate = 0;
810 int display_width_div = 1;
811 int display_height_div = 1;
846 }
else if (tag && tag->
value) {
861 codec_id =
"D_WEBVTT/CAPTIONS";
864 codec_id =
"D_WEBVTT/DESCRIPTIONS";
867 codec_id =
"D_WEBVTT/METADATA";
870 codec_id =
"D_WEBVTT/SUBTITLES";
888 av_log(s,
AV_LOG_ERROR,
"Raw RGB is not supported Natively in Matroska, you can use AVI or NUT or\n"
889 "If you would like to store it anyway using VFW mode, enable allow_raw_vfw (-allow_raw_vfw 1)\n");
915 "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
919 switch (codec->codec_type) {
923 if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
924 && 1.0/
av_q2d(st->avg_frame_rate) >
av_q2d(codec->time_base))
939 else if (!native_id) {
942 mkv->tracks[i].write_dts = 1;
954 &display_height_div);
966 if (st->sample_aspect_ratio.num) {
967 int64_t d_width =
av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
968 if (d_width > INT_MAX) {
974 }
else if (display_width_div != 1 || display_height_div != 1) {
980 uint32_t color_space =
av_le2ne32(codec->codec_tag);
997 if (output_sample_rate)
1016 av_log(
s,
AV_LOG_ERROR,
"Only audio, video, and subtitles are supported for Matroska.\n");
1036 int i,
ret, default_stream_exists = 0;
1068 if (ret < 0)
return ret;
1080 if (chapterstart < 0 || chapterstart > chapterend)
1114 if ((p = strrchr(p,
'-')) &&
1122 else if (*p >=
'a' && *p <=
'z')
1148 if (ret < 0)
return ret;
1194 if (ret < 0)
return ret;
1204 if (ret < 0)
return ret;
1214 if (ret < 0)
return ret;
1236 if (ret < 0)
return ret;
1244 const char *mimetype =
NULL;
1260 mimetype = t->
value;
1271 "it cannot be deduced from the codec id.\n", i);
1330 "The Matroska muxer does not yet support muxing %s\n",
1367 if (ret < 0)
return ret;
1374 uint32_t segment_uid[4];
1379 for (i = 0; i < 4; i++)
1389 const char *ident =
"Lavf";
1398 AV_WB64(date_utc_buf, date_utc);
1466 int size = pkt_size + 4;
1513 memcpy(dst + offset, src, header.
blocksize);
1536 uint64_t additional_id = 0;
1537 int64_t discard_padding = 0;
1538 ebml_master block_group, block_additions, block_more;
1541 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1570 if (side_data && side_data_size >= 10) {
1580 additional_id =
AV_RB64(side_data);
1582 side_data_size -= 8;
1585 if ((side_data_size && additional_id == 1) || discard_padding) {
1597 if (data != pkt->
data)
1600 if (discard_padding) {
1604 if (side_data_size && additional_id == 1) {
1614 if ((side_data_size && additional_id == 1) || discard_padding) {
1623 int id_size, settings_size,
size;
1626 const int flags = 0;
1636 size = id_size + 1 + settings_size + 1 + pkt->
size;
1639 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1649 avio_printf(pb,
"%.*s\n%.*s\n%.*s", id_size,
id, settings_size, settings, pkt->
size, pkt->
data);
1684 "Starting new cluster at offset %" PRIu64
" bytes, "
1685 "pts %" PRIu64
"dts %" PRIu64
"\n",
1703 int64_t relative_packet_pos;
1714 if ((int16_t)cluster_time != cluster_time) {
1744 if (ret < 0)
return ret;
1777 int64_t cluster_time;
1779 int start_new_cluster;
1799 start_new_cluster = keyframe;
1804 start_new_cluster = 1;
1809 cluster_size > 4 * 1024))) {
1810 start_new_cluster = 1;
1812 start_new_cluster = 0;
1815 if (mkv->
cluster_pos != -1 && start_new_cluster) {
1823 mkv->
is_dash ? start_new_cluster : 0);
1827 "Could not write cached audio packet ret:%d\n", ret);
1860 "Flushing cluster at offset %" PRIu64
" bytes\n",
1877 int64_t currentpos, cuespos;
1886 "Could not write cached audio packet ret:%d\n", ret);
1916 "Insufficient space reserved for cues: %d "
1917 "(needed: %" PRId64
").\n",
1922 if (cues_end < cuespos + mkv->reserve_cues_space)
1924 (cues_end - cuespos));
1998 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
1999 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
2001 {
"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 },
2002 {
"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 },
2003 {
"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 },
2004 {
"dash",
"Create a WebM file conforming to WebM DASH specification",
OFFSET(is_dash),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
FLAGS },
2005 {
"dash_track_number",
"Track number for the DASH stream",
OFFSET(dash_track_number),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127,
FLAGS },
2010 #if CONFIG_MATROSKA_MUXER
2011 static const AVClass matroska_class = {
2021 .mime_type =
"video/x-matroska",
2022 .extensions =
"mkv",
2024 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2026 .video_codec = CONFIG_LIBX264_ENCODER ?
2039 .priv_class = &matroska_class,
2043 #if CONFIG_WEBM_MUXER
2044 static const AVClass webm_class = {
2054 .mime_type =
"video/webm",
2055 .extensions =
"webm",
2065 .priv_class = &webm_class,
2069 #if CONFIG_MATROSKA_AUDIO_MUXER
2070 static const AVClass mka_class = {
2079 .mime_type =
"audio/x-matroska",
2080 .extensions =
"mka",
2082 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2093 .priv_class = &mka_class,