94 #define MODE_MATROSKAv2 0x01
95 #define MODE_WEBM 0x02
130 #define MAX_SEEKENTRY_SIZE 21
134 #define MAX_CUETRACKPOS_SIZE 42
137 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks
140 #define OPUS_SEEK_PREROLL 80000000
144 return (
av_log2(
id + 1) - 1) / 7 + 1;
172 while ((num + 1) >> bytes * 7)
192 bytes = needed_bytes;
197 num |= 1ULL << bytes * 7;
198 for (i = bytes - 1; i >= 0; i--)
211 for (i = bytes - 1; i >= 0; i--)
218 uint64_t tmp = 2*(val < 0 ? val^-1 :
val);
220 while (tmp>>=8) bytes++;
224 for (i = bytes - 1; i >= 0; i--)
273 uint64_t expectedsize)
312 if (new_seekhead == NULL)
317 if (numelements > 0) {
392 currentpos = seekhead->
filepos;
412 int64_t cluster_pos, int64_t relative_pos, int64_t
duration)
447 uint64_t pts = entry->
pts;
454 for (j = 0; j < num_tracks; j++)
455 tracks[j].has_cue = 0;
456 for (j = 0; j < cues->
num_entries - i && entry[j].
pts == pts; j++) {
458 av_assert0(tracknum>=0 && tracknum<num_tracks);
482 int first_header_size;
486 first_header_size = 30;
488 first_header_size = 42;
491 first_header_size, header_start, header_len) < 0) {
497 for (j = 0; j < 2; j++) {
500 for (j = 0; j < 3; j++)
501 avio_write(pb, header_start[j], header_len[j]);
535 av_dict_set(&dict,
"WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
567 "Error parsing AAC extradata, unable to determine samplerate.\n");
596 "Invalid extradata found, ALAC expects a 36-byte "
620 int ret, codecpriv_size;
640 for(i = 0; i < 0x5a - 8; i++)
686 if ((mode ==
MODE_WEBM && st_mode > 3 && st_mode != 11)
689 "The specified stereo mode is not valid.\n");
707 int output_sample_rate = 0;
708 int display_width_div = 1;
709 int display_height_div = 1;
741 }
else if (tag && tag->
value) {
756 codec_id =
"D_WEBVTT/CAPTIONS";
759 codec_id =
"D_WEBVTT/DESCRIPTIONS";
762 codec_id =
"D_WEBVTT/METADATA";
765 codec_id =
"D_WEBVTT/SUBTITLES";
800 "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
804 switch (codec->codec_type) {
808 if( st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
809 && 1.0/
av_q2d(st->avg_frame_rate) >
av_q2d(codec->time_base))
824 else if (!native_id) {
827 mkv->tracks[i].write_dts = 1;
853 display_width_div = 2;
859 display_height_div = 2;
870 if (st->sample_aspect_ratio.num) {
871 int64_t d_width =
av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
872 if (d_width > INT_MAX) {
878 }
else if (display_width_div != 1 || display_height_div != 1) {
884 uint32_t color_space =
av_le2ne32(codec->codec_tag);
900 if (output_sample_rate)
939 int i,
ret, default_stream_exists = 0;
971 if (ret < 0)
return ret;
1012 if ((p = strrchr(p,
'-')) &&
1020 else if (*p >=
'a' && *p <=
'z')
1045 if (ret < 0)
return ret;
1087 if (ret < 0)
return ret;
1097 if (ret < 0)
return ret;
1107 if (ret < 0)
return ret;
1129 if (ret < 0)
return ret;
1137 const char *mimetype = NULL;
1153 mimetype = t->
value;
1164 "it cannot be deduced from the codec id.\n", i);
1223 "The Matroska muxer does not yet support muxing %s\n",
1260 if (ret < 0)
return ret;
1267 uint32_t segment_uid[4];
1272 for (i = 0; i < 4; i++)
1282 const char *ident =
"Lavf";
1291 AV_WB64(date_utc_buf, date_utc);
1326 if (mkv->
cues == NULL)
1359 int size = pkt_size + 4;
1370 int sh, sm, ss, sc, eh, em, es, ec;
1373 if (sscanf(p,
"%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
1374 &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
1376 start = 3600000LL * sh + 60000LL * sm + 1000LL * ss + 10LL * sc;
1377 end = 3600000LL * eh + 60000LL * em + 1000LL * es + 10LL * ec;
1391 int i, layer = 0,
size, line_size, data_size = pkt->
size - (*datap - pkt->
data);
1397 end = memchr(data,
'\n', data_size);
1398 size = line_size = end ? end - data + 1 : data_size;
1399 size -= end ? (end[-1] ==
'\r') + 1 : 0;
1401 for (i = 0; i < 3; i++, start++)
1402 if (!(start = memchr(start,
',',
size - (start - data))))
1405 sscanf(data,
"Dialogue: %d,", &layer);
1406 i =
snprintf(buffer,
sizeof(buffer),
"%" PRId64
",%d,",
1409 memcpy(buffer + i, start,
size - i);
1412 "Writing block at offset %" PRIu64
", size %d, "
1413 "pts %" PRId64
", duration %d\n",
1427 *datap += line_size;
1471 memcpy(dst + offset, src, header.
blocksize);
1491 uint8_t *data = NULL, *side_data = NULL;
1494 uint64_t additional_id = 0;
1495 int64_t discard_padding = 0;
1496 ebml_master block_group, block_additions, block_more;
1499 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1528 if (side_data && side_data_size >= 10) {
1538 additional_id =
AV_RB64(side_data);
1540 side_data_size -= 8;
1543 if ((side_data_size && additional_id == 1) || discard_padding) {
1555 if (data != pkt->
data)
1558 if (discard_padding) {
1562 if (side_data_size && additional_id == 1) {
1572 if ((side_data_size && additional_id == 1) || discard_padding) {
1579 int i, duration = 0;
1581 for (i = 0; i < 2 && !
duration; i++) {
1582 int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1583 if (sscanf(*buf,
"%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1584 &s_hour, &s_min, &s_sec, &s_hsec,
1585 &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1586 s_min += 60 * s_hour;
1587 e_min += 60 * e_hour;
1588 s_sec += 60 * s_min;
1590 e_sec += 60 * e_min;
1591 s_hsec += 1000 * s_sec;
1592 e_hsec += 1000 * e_sec;
1594 duration = e_hsec - s_hsec;
1622 int id_size, settings_size,
size;
1625 const int flags = 0;
1635 size = id_size + 1 + settings_size + 1 + pkt->
size;
1638 "pts %" PRId64
", dts %" PRId64
", duration %d, flags %d\n",
1648 avio_printf(pb,
"%.*s\n%.*s\n%.*s", id_size,
id, settings_size, settings, pkt->
size, pkt->
data);
1680 int64_t relative_packet_pos;
1713 if (ret < 0)
return ret;
1717 while (data_offset < pkt->data + pkt->
size) {
1718 duration = mkv_write_ass_block(s, pb, pkt, &data_offset);
1720 if (ret < 0)
return ret;
1757 int64_t cluster_time;
1760 int start_new_cluster;
1782 start_new_cluster = keyframe;
1786 start_new_cluster = 1;
1791 cluster_size > 4 * 1024))) {
1792 start_new_cluster = 1;
1794 start_new_cluster = 0;
1797 if (mkv->
cluster_pos != -1 && start_new_cluster) {
1799 "Starting new cluster at offset %" PRIu64
" bytes, "
1800 "pts %" PRIu64
"dts %" PRIu64
"\n",
1813 mkv->
is_dash ? start_new_cluster : 0);
1817 "Could not write cached audio packet ret:%d\n", ret);
1850 "Flushing cluster at offset %" PRIu64
" bytes\n",
1867 int64_t currentpos, cuespos;
1876 "Could not write cached audio packet ret:%d\n", ret);
1906 "Insufficient space reserved for cues: %d "
1907 "(needed: %" PRId64
").\n",
1912 if (cues_end < cuespos + mkv->reserve_cues_space)
1914 (cues_end - cuespos));
1988 #define OFFSET(x) offsetof(MatroskaMuxContext, x)
1989 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
1991 {
"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 },
1992 {
"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 },
1993 {
"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 },
1994 {
"dash",
"Create a WebM file conforming to WebM DASH specification",
OFFSET(is_dash),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
FLAGS },
1995 {
"dash_track_number",
"Track number for the DASH stream",
OFFSET(dash_track_number),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127,
FLAGS },
1999 #if CONFIG_MATROSKA_MUXER
2000 static const AVClass matroska_class = {
2010 .mime_type =
"video/x-matroska",
2011 .extensions =
"mkv",
2013 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2015 .video_codec = CONFIG_LIBX264_ENCODER ?
2032 .priv_class = &matroska_class,
2036 #if CONFIG_WEBM_MUXER
2037 static const AVClass webm_class = {
2047 .mime_type =
"video/webm",
2048 .extensions =
"webm",
2058 .priv_class = &webm_class,
2062 #if CONFIG_MATROSKA_AUDIO_MUXER
2063 static const AVClass mka_class = {
2072 .mime_type =
"audio/x-matroska",
2073 .extensions =
"mka",
2075 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
2086 .priv_class = &mka_class,