#include "matroska.h"
#include "avc.h"
#include "flacenc.h"
+#include "avlanguage.h"
+#include "libavcore/samplefmt.h"
#include "libavutil/intreadwrite.h"
-#include "libavutil/md5.h"
+#include "libavutil/random_seed.h"
+#include "libavutil/lfg.h"
#include "libavcodec/xiph.h"
#include "libavcodec/mpeg4audio.h"
+#include <strings.h>
typedef struct ebml_master {
int64_t pos; ///< absolute offset in the file where the master's elements start
int write_dts;
} mkv_track;
+#define MODE_MATROSKAv2 0x01
+#define MODE_WEBM 0x02
+
typedef struct MatroskaMuxContext {
+ int mode;
ByteIOContext *dyn_bc;
ebml_master segment;
int64_t segment_offset;
- int64_t segment_uid;
ebml_master cluster;
int64_t cluster_pos; ///< file offset of the current cluster
int64_t cluster_pts;
int64_t duration_offset;
int64_t duration;
mkv_seekhead *main_seekhead;
- mkv_seekhead *cluster_seekhead;
mkv_cues *cues;
mkv_track *tracks;
- struct AVMD5 *md5_ctx;
+ unsigned int audio_buffer_size;
+ AVPacket cur_audio_pkt;
} MatroskaMuxContext;
}
static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid,
- const uint8_t *buf, int size)
+ const void *buf, int size)
{
put_ebml_id(pb, elementid);
put_ebml_num(pb, size, 0);
}
/**
- * Writes a void element of a given size. Useful for reserving space in
+ * Write a void element of a given size. Useful for reserving space in
* the file to be written to later.
*
* @param size The number of bytes to reserve, which must be at least 2.
ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
else if (codec->extradata_size)
put_buffer(dyn_cp, codec->extradata, codec->extradata_size);
- } else if (codec->codec_type == CODEC_TYPE_VIDEO) {
+ } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
if (qt_id) {
if (!codec->codec_tag)
codec->codec_tag = ff_codec_get_tag(codec_movvideo_tags, codec->codec_id);
ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
}
- } else if (codec->codec_type == CODEC_TYPE_AUDIO) {
- if (!codec->codec_tag)
- codec->codec_tag = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id);
- if (!codec->codec_tag) {
+ } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
+ unsigned int tag;
+ tag = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id);
+ if (!tag) {
av_log(s, AV_LOG_ERROR, "No wav codec ID found.\n");
ret = -1;
}
+ if (!codec->codec_tag)
+ codec->codec_tag = tag;
ff_put_wav_header(dyn_cp, codec);
}
AVMetadataTag *tag;
if (!bit_depth)
- bit_depth = av_get_bits_per_sample_format(codec->sample_fmt);
+ bit_depth = av_get_bits_per_sample_fmt(codec->sample_fmt);
if (codec->codec_id == CODEC_ID_AAC)
get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1);
put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
- put_ebml_float(pb, MATROSKA_ID_TRACKTIMECODESCALE, 1.0);
if ((tag = av_metadata_get(st->metadata, "title", NULL, 0)))
put_ebml_string(pb, MATROSKA_ID_TRACKNAME, tag->value);
}
}
+ if (mkv->mode == MODE_WEBM && !(codec->codec_id == CODEC_ID_VP8 ||
+ codec->codec_id == CODEC_ID_VORBIS)) {
+ av_log(s, AV_LOG_ERROR,
+ "Only VP8 video and Vorbis audio are supported for WebM.\n");
+ return AVERROR(EINVAL);
+ }
+
switch (codec->codec_type) {
- case CODEC_TYPE_VIDEO:
+ case AVMEDIA_TYPE_VIDEO:
put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
+ put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
if (!native_id &&
ff_codec_get_tag(codec_movvideo_tags, codec->codec_id) &&
int d_width = codec->width*av_q2d(st->sample_aspect_ratio);
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width);
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height);
+ put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, 3);
}
end_ebml_master(pb, subinfo);
break;
- case CODEC_TYPE_AUDIO:
+ case AVMEDIA_TYPE_AUDIO:
put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
if (!native_id)
end_ebml_master(pb, subinfo);
break;
- case CODEC_TYPE_SUBTITLE:
+ case AVMEDIA_TYPE_SUBTITLE:
put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_SUBTITLE);
break;
default:
return 0;
}
+static void mkv_write_simpletag(ByteIOContext *pb, AVMetadataTag *t)
+{
+ uint8_t *key = av_strdup(t->key);
+ uint8_t *p = key;
+ const uint8_t *lang = NULL;
+ ebml_master tag;
+
+ if ((p = strrchr(p, '-')) &&
+ (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
+ *p = 0;
+
+ p = key;
+ while (*p) {
+ if (*p == ' ')
+ *p = '_';
+ else if (*p >= 'a' && *p <= 'z')
+ *p -= 'a' - 'A';
+ p++;
+ }
+
+ tag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG, 0);
+ put_ebml_string(pb, MATROSKA_ID_TAGNAME, key);
+ if (lang)
+ put_ebml_string(pb, MATROSKA_ID_TAGLANG, lang);
+ put_ebml_string(pb, MATROSKA_ID_TAGSTRING, t->value);
+ end_ebml_master(pb, tag);
+
+ av_freep(&key);
+}
+
+static int mkv_write_tag(AVFormatContext *s, AVMetadata *m, unsigned int elementid,
+ unsigned int uid, ebml_master *tags)
+{
+ MatroskaMuxContext *mkv = s->priv_data;
+ ebml_master tag, targets;
+ AVMetadataTag *t = NULL;
+ int ret;
+
+ if (!tags->pos) {
+ ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TAGS, url_ftell(s->pb));
+ if (ret < 0) return ret;
+
+ *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
+ }
+
+ tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
+ targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
+ if (elementid)
+ put_ebml_uint(s->pb, elementid, uid);
+ end_ebml_master(s->pb, targets);
+
+ while ((t = av_metadata_get(m, "", t, AV_METADATA_IGNORE_SUFFIX)))
+ if (strcasecmp(t->key, "title"))
+ mkv_write_simpletag(s->pb, t);
+
+ end_ebml_master(s->pb, tag);
+ return 0;
+}
+
+static int mkv_write_tags(AVFormatContext *s)
+{
+ ebml_master tags = {0};
+ int i, ret;
+
+ ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL);
+
+ if (av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX)) {
+ ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
+ if (ret < 0) return ret;
+ }
+
+ for (i = 0; i < s->nb_streams; i++) {
+ AVStream *st = s->streams[i];
+
+ if (!av_metadata_get(st->metadata, "", 0, AV_METADATA_IGNORE_SUFFIX))
+ continue;
+
+ ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
+ if (ret < 0) return ret;
+ }
+
+ for (i = 0; i < s->nb_chapters; i++) {
+ AVChapter *ch = s->chapters[i];
+
+ if (!av_metadata_get(ch->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
+ continue;
+
+ ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id, &tags);
+ if (ret < 0) return ret;
+ }
+
+ if (tags.pos)
+ end_ebml_master(s->pb, tags);
+ return 0;
+}
+
static int mkv_write_header(AVFormatContext *s)
{
MatroskaMuxContext *mkv = s->priv_data;
ByteIOContext *pb = s->pb;
ebml_master ebml_header, segment_info;
AVMetadataTag *tag;
- int ret;
+ int ret, i;
+
+ if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
+ else mkv->mode = MODE_MATROSKAv2;
- mkv->md5_ctx = av_mallocz(av_md5_size);
- av_md5_init(mkv->md5_ctx);
mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
+ if (!mkv->tracks)
+ return AVERROR(ENOMEM);
ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1);
put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1);
put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4);
put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8);
- put_ebml_string (pb, EBML_ID_DOCTYPE , "matroska");
+ put_ebml_string (pb, EBML_ID_DOCTYPE , s->oformat->name);
put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 2);
put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 2);
end_ebml_master(pb, ebml_header);
// isn't more than 10 elements if we only write one of each other
// currently defined level 1 element
mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
- mkv->cluster_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 0);
- if (mkv->main_seekhead == NULL || mkv->cluster_seekhead == NULL)
+ if (!mkv->main_seekhead)
return AVERROR(ENOMEM);
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, url_ftell(pb));
if ((tag = av_metadata_get(s->metadata, "title", NULL, 0)))
put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
+ uint32_t segment_uid[4];
+ AVLFG lfg;
+
+ av_lfg_init(&lfg, av_get_random_seed());
+
+ for (i = 0; i < 4; i++)
+ segment_uid[i] = av_lfg_get(&lfg);
+
put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT);
put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
-
- // reserve space to write the segment UID later
- mkv->segment_uid = url_ftell(pb);
- put_ebml_void(pb, 19);
+ put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
}
// reserve space for the duration
ret = mkv_write_tracks(s);
if (ret < 0) return ret;
- ret = mkv_write_chapters(s);
- if (ret < 0) return ret;
+ if (mkv->mode != MODE_WEBM) {
+ ret = mkv_write_chapters(s);
+ if (ret < 0) return ret;
+
+ ret = mkv_write_tags(s);
+ if (ret < 0) return ret;
+ }
if (url_is_streamed(s->pb))
mkv_write_seekhead(pb, mkv->main_seekhead);
if (mkv->cues == NULL)
return AVERROR(ENOMEM);
+ av_init_packet(&mkv->cur_audio_pkt);
+ mkv->cur_audio_pkt.size = 0;
+ mkv->audio_buffer_size = 0;
+
put_flush_packet(pb);
return 0;
}
av_free(data);
}
+static int srt_get_duration(uint8_t **buf)
+{
+ int i, duration = 0;
+
+ for (i=0; i<2 && !duration; i++) {
+ int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
+ if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
+ &s_hour, &s_min, &s_sec, &s_hsec,
+ &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
+ s_min += 60*s_hour; e_min += 60*e_hour;
+ s_sec += 60*s_min; e_sec += 60*e_min;
+ s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
+ duration = e_hsec - s_hsec;
+ }
+ *buf += strcspn(*buf, "\n") + 1;
+ }
+ return duration;
+}
+
+static int mkv_write_srt_blocks(AVFormatContext *s, ByteIOContext *pb, AVPacket *pkt)
+{
+ ebml_master blockgroup;
+ AVPacket pkt2 = *pkt;
+ int64_t duration = srt_get_duration(&pkt2.data);
+ pkt2.size -= pkt2.data - pkt->data;
+
+ blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
+ mkv_blockgroup_size(pkt2.size));
+ mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
+ put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
+ end_ebml_master(pb, blockgroup);
+
+ return duration;
+}
+
static void mkv_flush_dynbuf(AVFormatContext *s)
{
MatroskaMuxContext *mkv = s->priv_data;
mkv->dyn_bc = NULL;
}
-static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
+static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
{
MatroskaMuxContext *mkv = s->priv_data;
ByteIOContext *pb = s->pb;
AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
- int keyframe = !!(pkt->flags & PKT_FLAG_KEY);
+ int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
int duration = pkt->duration;
int ret;
int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
}
if (!mkv->cluster_pos) {
- ret = mkv_add_seekhead_entry(mkv->cluster_seekhead, MATROSKA_ID_CLUSTER, url_ftell(pb));
- if (ret < 0) return ret;
-
mkv->cluster_pos = url_ftell(s->pb);
mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER, 0);
put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));
mkv->cluster_pts = FFMAX(0, ts);
- av_md5_update(mkv->md5_ctx, pkt->data, FFMIN(200, pkt->size));
}
- if (codec->codec_type != CODEC_TYPE_SUBTITLE) {
+ if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
} else if (codec->codec_id == CODEC_ID_SSA) {
duration = mkv_write_ass_blocks(s, pb, pkt);
+ } else if (codec->codec_id == CODEC_ID_SRT) {
+ duration = mkv_write_srt_blocks(s, pb, pkt);
} else {
ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size));
duration = pkt->convergence_duration;
end_ebml_master(pb, blockgroup);
}
- if (codec->codec_type == CODEC_TYPE_VIDEO && keyframe) {
+ if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) {
ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos);
if (ret < 0) return ret;
}
- // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming
- if ((url_is_streamed(s->pb) && (url_ftell(pb) > 32*1024 || ts > mkv->cluster_pts + 1000))
- || url_ftell(pb) > mkv->cluster_pos + 5*1024*1024 || ts > mkv->cluster_pts + 5000) {
+ mkv->duration = FFMAX(mkv->duration, ts + duration);
+ return 0;
+}
+
+static int mkv_copy_packet(MatroskaMuxContext *mkv, const AVPacket *pkt)
+{
+ uint8_t *data = mkv->cur_audio_pkt.data;
+ mkv->cur_audio_pkt = *pkt;
+ mkv->cur_audio_pkt.data = av_fast_realloc(data, &mkv->audio_buffer_size, pkt->size);
+ if (!mkv->cur_audio_pkt.data)
+ return AVERROR(ENOMEM);
+
+ memcpy(mkv->cur_audio_pkt.data, pkt->data, pkt->size);
+ mkv->cur_audio_pkt.size = pkt->size;
+ return 0;
+}
+
+static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ MatroskaMuxContext *mkv = s->priv_data;
+ ByteIOContext *pb = url_is_streamed(s->pb) ? mkv->dyn_bc : s->pb;
+ AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
+ int ret, keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
+ int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
+ int cluster_size = url_ftell(pb) - (url_is_streamed(s->pb) ? 0 : mkv->cluster_pos);
+
+ // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
+ // after 4k and on a keyframe
+ if (mkv->cluster_pos &&
+ ((url_is_streamed(s->pb) && (cluster_size > 32*1024 || ts > mkv->cluster_pts + 1000))
+ || cluster_size > 5*1024*1024 || ts > mkv->cluster_pts + 5000
+ || (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe && cluster_size > 4*1024))) {
av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
" bytes, pts %" PRIu64 "\n", url_ftell(pb), ts);
end_ebml_master(pb, mkv->cluster);
mkv_flush_dynbuf(s);
}
- mkv->duration = FFMAX(mkv->duration, ts + duration);
- return 0;
+ // check if we have an audio packet cached
+ if (mkv->cur_audio_pkt.size > 0) {
+ ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
+ mkv->cur_audio_pkt.size = 0;
+ if (ret < 0) {
+ av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
+ return ret;
+ }
+ }
+
+ // buffer an audio packet to ensure the packet containing the video
+ // keyframe's timecode is contained in the same cluster for WebM
+ if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
+ ret = mkv_copy_packet(mkv, pkt);
+ else
+ ret = mkv_write_packet_internal(s, pkt);
+ return ret;
}
static int mkv_write_trailer(AVFormatContext *s)
{
MatroskaMuxContext *mkv = s->priv_data;
ByteIOContext *pb = s->pb;
- int64_t currentpos, second_seekhead, cuespos;
+ int64_t currentpos, cuespos;
int ret;
+ // check if we have an audio packet cached
+ if (mkv->cur_audio_pkt.size > 0) {
+ ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
+ mkv->cur_audio_pkt.size = 0;
+ if (ret < 0) {
+ av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
+ return ret;
+ }
+ }
+
if (mkv->dyn_bc) {
end_ebml_master(mkv->dyn_bc, mkv->cluster);
mkv_flush_dynbuf(s);
if (!url_is_streamed(pb)) {
cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);
- second_seekhead = mkv_write_seekhead(pb, mkv->cluster_seekhead);
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES , cuespos);
if (ret < 0) return ret;
- if (second_seekhead >= 0) {
- ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_SEEKHEAD, second_seekhead);
- if (ret < 0) return ret;
- }
mkv_write_seekhead(pb, mkv->main_seekhead);
// update the duration
url_fseek(pb, mkv->duration_offset, SEEK_SET);
put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);
- // write the md5sum of some frames as the segment UID
- if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
- uint8_t segment_uid[16];
- av_md5_final(mkv->md5_ctx, segment_uid);
- url_fseek(pb, mkv->segment_uid, SEEK_SET);
- put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
- }
url_fseek(pb, currentpos, SEEK_SET);
}
end_ebml_master(pb, mkv->segment);
- av_free(mkv->md5_ctx);
av_free(mkv->tracks);
+ av_destruct_packet(&mkv->cur_audio_pkt);
put_flush_packet(pb);
return 0;
}
-AVOutputFormat matroska_muxer = {
+#if CONFIG_MATROSKA_MUXER
+AVOutputFormat ff_matroska_muxer = {
"matroska",
NULL_IF_CONFIG_SMALL("Matroska file format"),
"video/x-matroska",
.codec_tag = (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
.subtitle_codec = CODEC_ID_TEXT,
};
+#endif
+
+#if CONFIG_WEBM_MUXER
+AVOutputFormat ff_webm_muxer = {
+ "webm",
+ NULL_IF_CONFIG_SMALL("WebM file format"),
+ "video/webm",
+ "webm",
+ sizeof(MatroskaMuxContext),
+ CODEC_ID_VORBIS,
+ CODEC_ID_VP8,
+ mkv_write_header,
+ mkv_write_packet,
+ mkv_write_trailer,
+ .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
+};
+#endif
-AVOutputFormat matroska_audio_muxer = {
+#if CONFIG_MATROSKA_AUDIO_MUXER
+AVOutputFormat ff_matroska_audio_muxer = {
"matroska",
NULL_IF_CONFIG_SMALL("Matroska file format"),
"audio/x-matroska",
.flags = AVFMT_GLOBALHEADER,
.codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
};
+#endif