90 #define MODE_MATROSKAv2 0x01
91 #define MODE_WEBM 0x02
124 #define MAX_SEEKENTRY_SIZE 21
128 #define MAX_CUETRACKPOS_SIZE 42
131 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks
134 #define OPUS_SEEK_PREROLL 80000000
167 while ((num+1) >> bytes*7) bytes++;
186 bytes = needed_bytes;
191 num |= 1ULL << bytes*7;
192 for (i = bytes - 1; i >= 0; i--)
200 while (tmp>>=8) bytes++;
204 for (i = bytes - 1; i >= 0; i--)
211 uint64_t tmp = 2*(val < 0 ? val^-1 :
val);
213 while (tmp>>=8) bytes++;
217 for (i = bytes - 1; i >= 0; i--)
302 if (new_seekhead == NULL)
307 if (numelements > 0) {
382 currentpos = seekhead->
filepos;
435 uint64_t pts = entry->
pts;
442 for (j = 0; j < num_tracks; j++)
443 tracks[j].has_cue = 0;
444 for (j = 0; j < cues->
num_entries - i && entry[j].
pts == pts; j++) {
445 int tracknum = entry[j].
tracknum - 1;
446 av_assert0(tracknum>=0 && tracknum<num_tracks);
447 if (tracks[tracknum].has_cue)
470 int first_header_size;
474 first_header_size = 30;
476 first_header_size = 42;
479 first_header_size, header_start, header_len) < 0) {
485 for (j = 0; j < 2; j++) {
488 for (j = 0; j < 3; j++)
489 avio_write(pb, header_start[j], header_len[j]);
521 int ret, codecpriv_size;
541 "Invalid extradata found, ALAC expects a 36-byte "
561 for(i=0; i<0x5a-8; i++)
608 int i, j,
ret, default_stream_exists = 0;
611 if (ret < 0)
return ret;
626 int output_sample_rate = 0;
627 int display_width_div = 1;
628 int display_height_div = 1;
654 }
else if (tag && tag->
value) {
669 codec_id =
"D_WEBVTT/CAPTIONS";
672 codec_id =
"D_WEBVTT/DESCRIPTIONS";
675 codec_id =
"D_WEBVTT/METADATA";
678 codec_id =
"D_WEBVTT/SUBTITLES";
709 "Only VP8,VP9 video and Vorbis,Opus(experimental, use -strict -2) audio and WebVTT subtitles are supported for WebM.\n");
713 switch (codec->codec_type) {
716 if(st->avg_frame_rate.num && st->avg_frame_rate.den && 1.0/
av_q2d(st->avg_frame_rate) >
av_q2d(codec->time_base))
731 else if (!native_id) {
754 || st_mode >= MATROSKA_VIDEO_STEREO_MODE_COUNT) {
756 "The specified stereo mode is not valid.\n");
766 display_width_div = 2;
772 display_height_div = 2;
783 if (st->sample_aspect_ratio.num) {
784 int64_t d_width =
av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
785 if (d_width > INT_MAX) {
791 }
else if (display_width_div != 1 || display_height_div != 1) {
797 uint32_t color_space =
av_le2ne32(codec->codec_tag);
813 if (output_sample_rate)
832 av_log(s,
AV_LOG_ERROR,
"Only audio, video, and subtitles are supported for Matroska.\n");
838 if (ret < 0)
return ret;
862 if (ret < 0)
return ret;
903 if ((p = strrchr(p,
'-')) &&
911 else if (*p >=
'a' && *p <=
'z')
936 if (ret < 0)
return ret;
978 if (ret < 0)
return ret;
988 if (ret < 0)
return ret;
998 if (ret < 0)
return ret;
1020 if (ret < 0)
return ret;
1028 const char *mimetype = NULL;
1044 mimetype = t->
value;
1055 "it cannot be deduced from the codec id.\n", i);
1107 "The Matroska muxer does not yet support muxing %s\n",
1139 if (ret < 0)
return ret;
1146 uint32_t segment_uid[4];
1151 for (i = 0; i < 4; i++)
1161 const char *ident =
"Lavf";
1170 AV_WB64(date_utc_buf, date_utc);
1181 if (ret < 0)
return ret;
1188 if (ret < 0)
return ret;
1191 if (ret < 0)
return ret;
1194 if (ret < 0)
return ret;
1201 if (mkv->
cues == NULL)
1234 int size = pkt_size + 4;
1245 int sh, sm, ss, sc, eh, em, es, ec;
1248 if (sscanf(p,
"%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
1249 &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
1251 start = 3600000LL*sh + 60000LL*sm + 1000LL*ss + 10LL*sc;
1252 end = 3600000LL*eh + 60000LL*em + 1000LL*es + 10LL*ec;
1267 max_duration =
FFMAX(duration, max_duration);
1268 end = memchr(data,
'\n', data_size);
1270 size -= end ? (end[-1]==
'\r')+1 : 0;
1272 for (i=0; i<3; i++, start++)
1273 if (!(start = memchr(start,
',',
size-(start-data))))
1274 return max_duration;
1276 sscanf(data,
"Dialogue: %d,", &layer);
1277 i =
snprintf(buffer,
sizeof(buffer),
"%"PRId64
",%d,",
1280 memcpy(buffer+i, start,
size-i);
1283 "pts %" PRId64
", duration %d\n",
1296 data_size -= line_size;
1299 return max_duration;
1341 memcpy(dst + offset, src, header.
blocksize);
1361 uint8_t *data = NULL, *side_data = NULL;
1364 uint64_t additional_id = 0;
1365 int64_t discard_padding = 0;
1366 ebml_master block_group, block_additions, block_more;
1369 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1398 if (side_data && side_data_size >= 10) {
1408 additional_id =
AV_RB64(side_data);
1410 side_data_size -= 8;
1413 if ((side_data_size && additional_id == 1) || discard_padding) {
1424 if (data != pkt->
data)
1427 if (discard_padding) {
1431 if (side_data_size && additional_id == 1) {
1441 if ((side_data_size && additional_id == 1) || discard_padding) {
1448 int i, duration = 0;
1451 int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1452 if (sscanf(*buf,
"%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1453 &s_hour, &s_min, &s_sec, &s_hsec,
1454 &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1455 s_min += 60*s_hour; e_min += 60*e_hour;
1456 s_sec += 60*s_min; e_sec += 60*e_min;
1457 s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
1458 duration = e_hsec - s_hsec;
1485 int id_size, settings_size,
size;
1488 const int flags = 0;
1498 size = id_size + 1 + settings_size + 1 + pkt->
size;
1501 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1511 avio_printf(pb,
"%.*s\n%.*s\n%.*s", id_size,
id, settings_size, settings, pkt->
size, pkt->
data);
1543 int64_t relative_packet_pos;
1573 duration = mkv_write_ass_blocks(s, pb, pkt);
1593 if (ret < 0)
return ret;
1606 int64_t cluster_time;
1629 cluster_size > 4 * 1024))) {
1631 " bytes, pts %" PRIu64
"dts %" PRIu64
"\n",
1694 int64_t currentpos, cuespos;
1716 if (ret < 0)
return ret;
1732 cues_end - cuespos);
1736 if (cues_end < cuespos + mkv->reserve_cues_space)
1745 if (ret < 0)
return ret;
1808 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
1809 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
1811 {
"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 },
1812 {
"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 },
1813 {
"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 },
1817 #if CONFIG_MATROSKA_MUXER
1818 static const AVClass matroska_class = {
1828 .mime_type =
"video/x-matroska",
1829 .extensions =
"mkv",
1831 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1833 .video_codec = CONFIG_LIBX264_ENCODER ?
1850 .priv_class = &matroska_class,
1854 #if CONFIG_WEBM_MUXER
1855 static const AVClass webm_class = {
1865 .mime_type =
"video/webm",
1866 .extensions =
"webm",
1876 .priv_class = &webm_class,
1880 #if CONFIG_MATROSKA_AUDIO_MUXER
1881 static const AVClass mka_class = {
1890 .mime_type =
"audio/x-matroska",
1891 .extensions =
"mka",
1893 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1904 .priv_class = &mka_class,