87 #define MODE_MATROSKAv2 0x01
88 #define MODE_WEBM 0x02
121 #define MAX_SEEKENTRY_SIZE 21
125 #define MAX_CUETRACKPOS_SIZE 42
128 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks
131 #define OPUS_SEEK_PREROLL 80000000
164 while ((num+1) >> bytes*7) bytes++;
183 bytes = needed_bytes;
188 num |= 1ULL << bytes*7;
189 for (i = bytes - 1; i >= 0; i--)
197 while (tmp>>=8) bytes++;
201 for (i = bytes - 1; i >= 0; i--)
208 uint64_t tmp = 2*(val < 0 ? val^-1 :
val);
210 while (tmp>>=8) bytes++;
214 for (i = bytes - 1; i >= 0; i--)
299 if (new_seekhead == NULL)
304 if (numelements > 0) {
379 currentpos = seekhead->
filepos;
432 uint64_t pts = entry->
pts;
439 for (j = 0; j < num_tracks; j++)
440 tracks[j].has_cue = 0;
441 for (j = 0; j < cues->
num_entries - i && entry[j].
pts == pts; j++) {
442 int tracknum = entry[j].
tracknum - 1;
443 av_assert0(tracknum>=0 && tracknum<num_tracks);
444 if (tracks[tracknum].has_cue)
467 int first_header_size;
471 first_header_size = 30;
473 first_header_size = 42;
476 first_header_size, header_start, header_len) < 0) {
482 for (j = 0; j < 2; j++) {
485 for (j = 0; j < 3; j++)
486 avio_write(pb, header_start[j], header_len[j]);
518 int ret, codecpriv_size;
536 "Invalid extradata found, ALAC expects a 36-byte "
589 int i, j,
ret, default_stream_exists = 0;
592 if (ret < 0)
return ret;
607 int output_sample_rate = 0;
633 }
else if (tag && tag->
value) {
648 codec_id =
"D_WEBVTT/CAPTIONS";
651 codec_id =
"D_WEBVTT/DESCRIPTIONS";
654 codec_id =
"D_WEBVTT/METADATA";
657 codec_id =
"D_WEBVTT/SUBTITLES";
688 "Only VP8,VP9 video and Vorbis,Opus(experimental, use -strict -2) audio and WebVTT subtitles are supported for WebM.\n");
692 switch (codec->codec_type) {
695 if(st->avg_frame_rate.num && st->avg_frame_rate.den && 1.0/
av_q2d(st->avg_frame_rate) >
av_q2d(codec->time_base))
710 else if (!native_id) {
733 || st_mode >= MATROSKA_VIDEO_STEREO_MODE_COUNT) {
735 "The specified stereo mode is not valid.\n");
747 if (st->sample_aspect_ratio.num) {
748 int64_t d_width =
av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
749 if (d_width > INT_MAX) {
758 uint32_t color_space =
av_le2ne32(codec->codec_tag);
774 if (output_sample_rate)
793 av_log(s,
AV_LOG_ERROR,
"Only audio, video, and subtitles are supported for Matroska.\n");
799 if (ret < 0)
return ret;
823 if (ret < 0)
return ret;
864 if ((p = strrchr(p,
'-')) &&
872 else if (*p >=
'a' && *p <=
'z')
897 if (ret < 0)
return ret;
937 if (ret < 0)
return ret;
947 if (ret < 0)
return ret;
957 if (ret < 0)
return ret;
979 if (ret < 0)
return ret;
987 const char *mimetype = NULL;
1003 mimetype = t->
value;
1014 "it cannot be deduced from the codec id.\n", i);
1066 "The Matroska muxer does not yet support muxing %s\n",
1098 if (ret < 0)
return ret;
1105 uint32_t segment_uid[4];
1110 for (i = 0; i < 4; i++)
1117 const char *ident =
"Lavf";
1126 AV_WB64(date_utc_buf, date_utc);
1137 if (ret < 0)
return ret;
1144 if (ret < 0)
return ret;
1147 if (ret < 0)
return ret;
1150 if (ret < 0)
return ret;
1157 if (mkv->
cues == NULL)
1190 int size = pkt_size + 4;
1201 int sh, sm, ss, sc, eh, em, es, ec;
1204 if (sscanf(p,
"%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
1205 &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
1207 start = 3600000LL*sh + 60000LL*sm + 1000LL*ss + 10LL*sc;
1208 end = 3600000LL*eh + 60000LL*em + 1000LL*es + 10LL*ec;
1216 int i, layer = 0, max_duration = 0,
size, line_size, data_size = pkt->
size;
1223 max_duration =
FFMAX(duration, max_duration);
1224 end = memchr(data,
'\n', data_size);
1225 size = line_size = end ? end-data+1 : data_size;
1226 size -= end ? (end[-1]==
'\r')+1 : 0;
1228 for (i=0; i<3; i++, start++)
1229 if (!(start = memchr(start,
',',
size-(start-data))))
1230 return max_duration;
1232 sscanf(data,
"Dialogue: %d,", &layer);
1233 i =
snprintf(buffer,
sizeof(buffer),
"%"PRId64
",%d,",
1236 memcpy(buffer+i, start,
size-i);
1239 "pts %" PRId64
", duration %d\n",
1252 data_size -= line_size;
1255 return max_duration;
1297 memcpy(dst + offset, src, header.
blocksize);
1317 uint8_t *data = NULL, *side_data = NULL;
1320 uint64_t additional_id = 0;
1321 int64_t discard_padding = 0;
1322 ebml_master block_group, block_additions, block_more;
1325 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1350 if (side_data && side_data_size >= 10) {
1360 additional_id =
AV_RB64(side_data);
1362 side_data_size -= 8;
1365 if ((side_data_size && additional_id == 1) || discard_padding) {
1376 if (data != pkt->
data)
1379 if (discard_padding) {
1383 if (side_data_size && additional_id == 1) {
1393 if ((side_data_size && additional_id == 1) || discard_padding) {
1400 int i, duration = 0;
1403 int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1404 if (sscanf(*buf,
"%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1405 &s_hour, &s_min, &s_sec, &s_hsec,
1406 &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1407 s_min += 60*s_hour; e_min += 60*e_hour;
1408 s_sec += 60*s_min; e_sec += 60*e_min;
1409 s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
1410 duration = e_hsec - s_hsec;
1437 int id_size, settings_size,
size;
1440 const int flags = 0;
1450 size = id_size + 1 + settings_size + 1 + pkt->
size;
1453 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1463 avio_printf(pb,
"%.*s\n%.*s\n%.*s", id_size,
id, settings_size, settings, pkt->
size, pkt->
data);
1495 int64_t relative_packet_pos;
1525 duration = mkv_write_ass_blocks(s, pb, pkt);
1545 if (ret < 0)
return ret;
1558 int64_t cluster_time;
1581 cluster_size > 4 * 1024))) {
1583 " bytes, pts %" PRIu64
"dts %" PRIu64
"\n",
1646 int64_t currentpos, cuespos;
1668 if (ret < 0)
return ret;
1684 cues_end - cuespos);
1688 if (cues_end < cuespos + mkv->reserve_cues_space)
1697 if (ret < 0)
return ret;
1760 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
1761 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
1763 {
"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 },
1764 {
"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 },
1765 {
"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 },
1769 #if CONFIG_MATROSKA_MUXER
1770 static const AVClass matroska_class = {
1780 .mime_type =
"video/x-matroska",
1781 .extensions =
"mkv",
1783 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1785 .video_codec = CONFIG_LIBX264_ENCODER ?
1802 .priv_class = &matroska_class,
1806 #if CONFIG_WEBM_MUXER
1807 static const AVClass webm_class = {
1817 .mime_type =
"video/webm",
1818 .extensions =
"webm",
1828 .priv_class = &webm_class,
1832 #if CONFIG_MATROSKA_AUDIO_MUXER
1833 static const AVClass mka_class = {
1842 .mime_type =
"audio/x-matroska",
1843 .extensions =
"mka",
1845 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1856 .priv_class = &mka_class,