3 * Copyright (c) 2007 David Conrad
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 typedef struct MatroskaMuxContext {
32 static void put_ebml_id(ByteIOContext *pb, unsigned int id)
35 put_byte(pb, id >> 24);
37 put_byte(pb, id >> 16);
39 put_byte(pb, id >> 8);
43 // XXX: test this thoroughly and get rid of minbytes hack (currently needed to
44 // use up all of the space reserved in start_ebml_master)
45 static void put_ebml_size(ByteIOContext *pb, uint64_t size, int minbytes)
48 while (size >> (bytes*7 + 7)) bytes++;
50 // sizes larger than this are currently undefined in EBML
51 // XXX: error condition?
52 if (size > (1ULL<<56)-1) return;
54 put_byte(pb, (0x80 >> bytes) | (size >> bytes*8));
55 for (bytes -= 1; bytes >= 0; bytes--)
56 put_byte(pb, size >> bytes*8);
59 static void put_ebml_uint(ByteIOContext *pb, unsigned int elementid, uint64_t val)
62 while (val >> bytes*8) bytes++;
64 put_ebml_id(pb, elementid);
65 put_ebml_size(pb, bytes, 0);
66 for (bytes -= 1; bytes >= 0; bytes--)
67 put_byte(pb, val >> bytes*8);
70 //static void put_ebml_sint(ByteIOContext *pb, unsigned int elementid, int64_t val)
72 static void put_ebml_float(ByteIOContext *pb, unsigned int elementid, double val)
74 // XXX: single-precision floats?
75 put_ebml_id(pb, elementid);
76 put_ebml_size(pb, 8, 0);
77 put_be64(pb, av_dbl2int(val));
80 static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid,
81 const uint8_t *buf, int size)
83 put_ebml_id(pb, elementid);
84 put_ebml_size(pb, size, 0);
85 put_buffer(pb, buf, size);
88 static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, const char *str)
90 put_ebml_binary(pb, elementid, str, strlen(str));
93 static offset_t start_ebml_master(ByteIOContext *pb, unsigned int elementid)
95 put_ebml_id(pb, elementid);
96 // XXX: this always reserves the maximum needed space to store any size value
97 // we should be smarter (additional parameter for expected size?)
98 put_ebml_size(pb, (1ULL<<56)-1, 0); // largest unknown size
102 static void end_ebml_master(ByteIOContext *pb, offset_t start)
104 offset_t pos = url_ftell(pb);
106 url_fseek(pb, start - 8, SEEK_SET);
107 put_ebml_size(pb, pos - start, 7);
108 url_fseek(pb, pos, SEEK_SET);
112 static int mkv_write_header(AVFormatContext *s)
114 MatroskaMuxContext *mkv = s->priv_data;
115 ByteIOContext *pb = &s->pb;
116 offset_t ebml_header, segment_info, tracks;
119 ebml_header = start_ebml_master(pb, EBML_ID_HEADER);
120 put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1);
121 put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1);
122 put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4);
123 put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8);
124 put_ebml_string (pb, EBML_ID_DOCTYPE , "matroska");
125 put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 1);
126 put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 1);
127 end_ebml_master(pb, ebml_header);
129 mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT);
131 segment_info = start_ebml_master(pb, MATROSKA_ID_INFO);
132 put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
133 if (strlen(s->title))
134 put_ebml_string(pb, MATROSKA_ID_TITLE, s->title);
135 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
136 put_ebml_string(pb, MATROSKA_ID_MUXINGAPP, LIBAVFORMAT_IDENT);
137 // XXX: both are required; something better for writing app?
138 put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
140 // XXX: segment UID and duration
141 end_ebml_master(pb, segment_info);
143 tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS);
144 for (i = 0; i < s->nb_streams; i++) {
145 AVStream *st = s->streams[i];
146 AVCodecContext *codec = st->codec;
147 offset_t subinfo, track;
150 track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY);
151 put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i);
152 // XXX: random number for UID? and can we use the same UID when copying
153 // from another MKV as the specs recommend?
154 put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i);
155 put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
158 put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, st->language);
160 // look for a codec id string specific to mkv to use, if none are found, use AVI codes
161 for (j = 0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++) {
162 if (ff_mkv_codec_tags[j].id == codec->codec_id) {
163 put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
169 // XXX: CodecPrivate for vorbis, theora, aac, native mpeg4, ...
171 offset_t codecprivate;
173 if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA) {
174 uint8_t *header_start[3];
176 int first_header_size;
178 if (codec->codec_id == CODEC_ID_VORBIS)
179 first_header_size = 30;
181 first_header_size = 42;
183 if (ff_split_xiph_headers(codec->extradata, codec->extradata_size,
184 first_header_size, header_start, header_len) < 0) {
185 av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
189 codecprivate = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE);
190 put_byte(pb, 2); // number packets - 1
191 for (j = 0; j < 2; j++) {
192 for (k = 0; k < header_len[j] / 255; k++)
194 put_byte(pb, header_len[j]);
196 for (j = 0; j < 3; j++)
197 put_buffer(pb, header_start[j], header_len[j]);
198 end_ebml_master(pb, codecprivate);
200 put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codec->extradata, codec->extradata_size);
204 switch (codec->codec_type) {
205 case CODEC_TYPE_VIDEO:
206 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
210 // if there is no mkv-specific codec id, use VFW mode
211 if (!codec->codec_tag)
212 codec->codec_tag = codec_get_tag(codec_bmp_tags, codec->codec_id);
214 put_ebml_string(pb, MATROSKA_ID_CODECID, MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC);
215 // XXX: codec private isn't a master; is there a better way to re-use put_bmp_header?
216 bmp_header = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE);
217 put_bmp_header(pb, codec, codec_bmp_tags, 0);
218 end_ebml_master(pb, bmp_header);
220 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO);
221 // XXX: interlace flag?
222 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
223 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
224 // XXX: display width/height
225 end_ebml_master(pb, subinfo);
228 case CODEC_TYPE_AUDIO:
229 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
232 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO);
233 put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
234 put_ebml_float (pb, MATROSKA_ID_AUDIOSAMPLINGFREQ, codec->sample_rate);
235 // XXX: output sample freq (for sbr) and bitdepth (for pcm)
236 end_ebml_master(pb, subinfo);
240 av_log(s, AV_LOG_ERROR, "Only audio and video are supported for Matroska.");
243 end_ebml_master(pb, track);
245 // ms precision is the de-facto standard timescale for mkv files
246 av_set_pts_info(st, 64, 1, 1000);
248 end_ebml_master(pb, tracks);
250 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER);
251 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, 0);
256 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
258 ByteIOContext *pb = &s->pb;
261 block = start_ebml_master(pb, MATROSKA_ID_SIMPLEBLOCK);
262 put_byte(pb, 0x80 | pkt->stream_index); // this assumes stream_index is less than 127
263 put_be16(pb, pkt->pts);
264 put_byte(pb, !!(pkt->flags & PKT_FLAG_KEY));
265 put_buffer(pb, pkt->data, pkt->size);
266 end_ebml_master(pb, block);
270 static int mkv_write_trailer(AVFormatContext *s)
272 MatroskaMuxContext *mkv = s->priv_data;
273 ByteIOContext *pb = &s->pb;
274 end_ebml_master(pb, mkv->cluster);
275 end_ebml_master(pb, mkv->segment);
279 AVOutputFormat matroska_muxer = {
281 "Matroska File Format",
284 sizeof(MatroskaMuxContext),
290 .codec_tag = (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, 0},