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 mkv_seekhead_entry {
28 unsigned int elementid;
32 typedef struct mkv_seekhead {
34 offset_t segment_offset; ///< the file offset to the beginning of the segment
35 int reserved_size; ///< -1 if appending to file
37 mkv_seekhead_entry *entries;
44 offset_t cluster_pos; ///< file offset of the cluster containing the block
48 offset_t segment_offset;
49 mkv_cuepoint *entries;
53 typedef struct MatroskaMuxContext {
55 offset_t segment_offset;
57 offset_t cluster_pos; ///< file offset of the current cluster
59 offset_t duration_offset;
61 mkv_seekhead *main_seekhead;
62 mkv_seekhead *cluster_seekhead;
66 static void put_ebml_id(ByteIOContext *pb, unsigned int id)
69 put_byte(pb, id >> 24);
71 put_byte(pb, id >> 16);
73 put_byte(pb, id >> 8);
77 static int ebml_id_size(unsigned int id)
79 return (av_log2(id+1)-1)/7+1;
82 // XXX: test this thoroughly and get rid of minbytes hack (currently needed to
83 // use up all of the space reserved in start_ebml_master)
84 static void put_ebml_size(ByteIOContext *pb, uint64_t size, int minbytes)
88 // sizes larger than this are currently undefined in EBML
89 // so write "unknown" size
90 size = FFMIN(size, (1ULL<<56)-1);
92 while (size >> (bytes*7 + 7)) bytes++;
94 put_byte(pb, (0x80 >> bytes) | (size >> bytes*8));
95 for (bytes -= 1; bytes >= 0; bytes--)
96 put_byte(pb, size >> bytes*8);
99 static void put_ebml_uint(ByteIOContext *pb, unsigned int elementid, uint64_t val)
102 while (val >> bytes*8) bytes++;
104 put_ebml_id(pb, elementid);
105 put_ebml_size(pb, bytes, 0);
106 for (bytes -= 1; bytes >= 0; bytes--)
107 put_byte(pb, val >> bytes*8);
110 static void put_ebml_float(ByteIOContext *pb, unsigned int elementid, double val)
112 // XXX: single-precision floats?
113 put_ebml_id(pb, elementid);
114 put_ebml_size(pb, 8, 0);
115 put_be64(pb, av_dbl2int(val));
118 static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid,
119 const uint8_t *buf, int size)
121 put_ebml_id(pb, elementid);
122 put_ebml_size(pb, size, 0);
123 put_buffer(pb, buf, size);
126 static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, const char *str)
128 put_ebml_binary(pb, elementid, str, strlen(str));
131 // this reserves exactly the amount of space specified by size, which must be at least 2
132 static void put_ebml_void(ByteIOContext *pb, uint64_t size)
134 offset_t currentpos = url_ftell(pb);
139 put_ebml_id(pb, EBML_ID_VOID);
140 // we need to subtract the length needed to store the size from the size we need to reserve
141 // so 2 cases, we use 8 bytes to store the size if possible, 1 byte otherwise
143 put_ebml_size(pb, size-1, 0);
145 put_ebml_size(pb, size-9, 7);
146 url_fseek(pb, currentpos + size, SEEK_SET);
149 static offset_t start_ebml_master(ByteIOContext *pb, unsigned int elementid)
151 put_ebml_id(pb, elementid);
152 // XXX: this always reserves the maximum needed space to store any size value
153 // we should be smarter (additional parameter for expected size?)
154 put_ebml_size(pb, (1ULL<<56)-1, 0); // largest unknown size
155 return url_ftell(pb);
158 static void end_ebml_master(ByteIOContext *pb, offset_t start)
160 offset_t pos = url_ftell(pb);
162 url_fseek(pb, start - 8, SEEK_SET);
163 put_ebml_size(pb, pos - start, 7);
164 url_fseek(pb, pos, SEEK_SET);
167 // initializes a mkv_seekhead element to be ready to index level 1 matroska elements
168 // if numelements is greater than 0, it reserves enough space for that many elements
169 // at the current file position and writes the seekhead there, otherwise the seekhead
170 // will be appended to the file when end_mkv_seekhead() is called
171 static mkv_seekhead * mkv_start_seekhead(ByteIOContext *pb, offset_t segment_offset, int numelements)
173 mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
174 if (new_seekhead == NULL)
177 new_seekhead->segment_offset = segment_offset;
179 if (numelements > 0) {
180 new_seekhead->filepos = url_ftell(pb);
181 // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID and size,
182 // and 3 bytes to guarantee that an EBML void element will fit afterwards
183 // XXX: 28 bytes right now because begin_ebml_master() reserves more than necessary
184 new_seekhead->reserved_size = numelements * 28 + 13;
185 new_seekhead->max_entries = numelements;
186 put_ebml_void(pb, new_seekhead->reserved_size);
191 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
193 mkv_seekhead_entry *entries = seekhead->entries;
194 int new_entry = seekhead->num_entries;
196 // don't store more elements than we reserved space for
197 if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
200 entries = av_realloc(entries, (seekhead->num_entries + 1) * sizeof(mkv_seekhead_entry));
204 entries[new_entry].elementid = elementid;
205 entries[new_entry].segmentpos = filepos - seekhead->segment_offset;
207 seekhead->entries = entries;
208 seekhead->num_entries++;
213 // returns the file offset where the seekhead was written and frees the seekhead
214 static offset_t mkv_write_seekhead(ByteIOContext *pb, mkv_seekhead *seekhead)
216 offset_t metaseek, seekentry, currentpos;
219 currentpos = url_ftell(pb);
221 if (seekhead->reserved_size > 0)
222 url_fseek(pb, seekhead->filepos, SEEK_SET);
224 metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD);
225 for (i = 0; i < seekhead->num_entries; i++) {
226 mkv_seekhead_entry *entry = &seekhead->entries[i];
228 seekentry = start_ebml_master(pb, MATROSKA_ID_SEEKENTRY);
230 put_ebml_id(pb, MATROSKA_ID_SEEKID);
231 put_ebml_size(pb, ebml_id_size(entry->elementid), 0);
232 put_ebml_id(pb, entry->elementid);
234 put_ebml_uint(pb, MATROSKA_ID_SEEKPOSITION, entry->segmentpos);
235 end_ebml_master(pb, seekentry);
237 end_ebml_master(pb, metaseek);
239 if (seekhead->reserved_size > 0) {
240 uint64_t remaining = seekhead->filepos + seekhead->reserved_size - url_ftell(pb);
241 put_ebml_void(pb, remaining);
242 url_fseek(pb, currentpos, SEEK_SET);
244 currentpos = seekhead->filepos;
246 av_free(seekhead->entries);
252 static mkv_cues * mkv_start_cues(offset_t segment_offset)
254 mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
258 cues->segment_offset = segment_offset;
262 static int mkv_add_cuepoint(mkv_cues *cues, AVPacket *pkt, offset_t cluster_pos)
264 mkv_cuepoint *entries = cues->entries;
265 int new_entry = cues->num_entries;
267 entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));
271 entries[new_entry].pts = pkt->pts;
272 entries[new_entry].tracknum = pkt->stream_index + 1;
273 entries[new_entry].cluster_pos = cluster_pos - cues->segment_offset;
275 cues->entries = entries;
280 static offset_t mkv_write_cues(ByteIOContext *pb, mkv_cues *cues)
282 offset_t currentpos, cues_element;
285 currentpos = url_ftell(pb);
286 cues_element = start_ebml_master(pb, MATROSKA_ID_CUES);
288 for (i = 0; i < cues->num_entries; i++) {
289 offset_t cuepoint, track_positions;
290 mkv_cuepoint *entry = &cues->entries[i];
291 uint64_t pts = entry->pts;
293 cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY);
294 put_ebml_uint(pb, MATROSKA_ID_CUETIME, pts);
296 // put all the entries from different tracks that have the exact same
297 // timestamp into the same CuePoint
298 for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
299 track_positions = start_ebml_master(pb, MATROSKA_ID_CUETRACKPOSITION);
300 put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
301 put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION, entry[j].cluster_pos);
302 end_ebml_master(pb, track_positions);
305 end_ebml_master(pb, cuepoint);
307 end_ebml_master(pb, cues_element);
309 av_free(cues->entries);
314 static int put_xiph_codecpriv(ByteIOContext *pb, AVCodecContext *codec)
316 offset_t codecprivate;
317 uint8_t *header_start[3];
319 int first_header_size;
322 if (codec->codec_id == CODEC_ID_VORBIS)
323 first_header_size = 30;
325 first_header_size = 42;
327 if (ff_split_xiph_headers(codec->extradata, codec->extradata_size,
328 first_header_size, header_start, header_len) < 0) {
329 av_log(codec, AV_LOG_ERROR, "Extradata corrupt.\n");
333 codecprivate = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE);
334 put_byte(pb, 2); // number packets - 1
335 for (j = 0; j < 2; j++) {
336 for (k = 0; k < header_len[j] / 255; k++)
338 put_byte(pb, header_len[j] % 255);
340 for (j = 0; j < 3; j++)
341 put_buffer(pb, header_start[j], header_len[j]);
342 end_ebml_master(pb, codecprivate);
347 static void get_aac_sample_rates(AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
349 static const int aac_sample_rates[] = {
350 96000, 88200, 64000, 48000, 44100, 32000,
351 24000, 22050, 16000, 12000, 11025, 8000,
355 if (codec->extradata_size < 2) {
356 av_log(codec, AV_LOG_WARNING, "no aac extradata, unable to determine sample rate\n");
360 sri = ((codec->extradata[0] << 1) & 0xE) | (codec->extradata[1] >> 7);
362 av_log(codec, AV_LOG_WARNING, "aac samplerate index out of bounds\n");
365 *sample_rate = aac_sample_rates[sri];
367 // if sbr, get output sample rate as well
368 if (codec->extradata_size == 5) {
369 sri = (codec->extradata[4] >> 3) & 0xF;
371 av_log(codec, AV_LOG_WARNING, "aac output samplerate index out of bounds\n");
374 *output_sample_rate = aac_sample_rates[sri];
378 static int mkv_write_tracks(AVFormatContext *s)
380 MatroskaMuxContext *mkv = s->priv_data;
381 ByteIOContext *pb = &s->pb;
385 if (mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, url_ftell(pb)) < 0)
388 tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS);
389 for (i = 0; i < s->nb_streams; i++) {
390 AVStream *st = s->streams[i];
391 AVCodecContext *codec = st->codec;
392 offset_t subinfo, track;
394 int bit_depth = av_get_bits_per_sample(codec->codec_id);
395 int sample_rate = codec->sample_rate;
396 int output_sample_rate = 0;
398 if (codec->codec_id == CODEC_ID_AAC)
399 get_aac_sample_rates(codec, &sample_rate, &output_sample_rate);
401 track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY);
402 put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1);
403 put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
404 put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
407 put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, st->language);
409 // look for a codec id string specific to mkv to use, if none are found, use AVI codes
410 for (j = 0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++) {
411 if (ff_mkv_codec_tags[j].id == codec->codec_id) {
412 put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
418 // XXX: CodecPrivate for vorbis, theora, aac, native mpeg4, ...
420 if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA) {
421 if (put_xiph_codecpriv(pb, codec) < 0)
424 put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codec->extradata, codec->extradata_size);
428 switch (codec->codec_type) {
429 case CODEC_TYPE_VIDEO:
430 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
434 // if there is no mkv-specific codec id, use VFW mode
435 if (!codec->codec_tag)
436 codec->codec_tag = codec_get_tag(codec_bmp_tags, codec->codec_id);
438 put_ebml_string(pb, MATROSKA_ID_CODECID, MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC);
439 bmp_header = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE);
440 put_bmp_header(pb, codec, codec_bmp_tags, 0);
441 end_ebml_master(pb, bmp_header);
443 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO);
444 // XXX: interlace flag?
445 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
446 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
447 // XXX: display width/height
448 end_ebml_master(pb, subinfo);
451 case CODEC_TYPE_AUDIO:
452 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
456 // no mkv-specific ID, use ACM mode
457 codec->codec_tag = codec_get_tag(codec_wav_tags, codec->codec_id);
458 if (!codec->codec_tag) {
459 av_log(s, AV_LOG_ERROR, "no codec id found for stream %d", i);
463 put_ebml_string(pb, MATROSKA_ID_CODECID, MATROSKA_CODEC_ID_AUDIO_ACM);
464 wav_header = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE);
465 put_wav_header(pb, codec);
466 end_ebml_master(pb, wav_header);
468 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO);
469 put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
470 put_ebml_float (pb, MATROSKA_ID_AUDIOSAMPLINGFREQ, sample_rate);
471 if (output_sample_rate)
472 put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
474 put_ebml_uint(pb, MATROSKA_ID_AUDIOBITDEPTH, bit_depth);
475 end_ebml_master(pb, subinfo);
479 av_log(s, AV_LOG_ERROR, "Only audio and video are supported for Matroska.");
482 end_ebml_master(pb, track);
484 // ms precision is the de-facto standard timescale for mkv files
485 av_set_pts_info(st, 64, 1, 1000);
487 end_ebml_master(pb, tracks);
491 static int mkv_write_header(AVFormatContext *s)
493 MatroskaMuxContext *mkv = s->priv_data;
494 ByteIOContext *pb = &s->pb;
495 offset_t ebml_header, segment_info;
497 ebml_header = start_ebml_master(pb, EBML_ID_HEADER);
498 put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1);
499 put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1);
500 put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4);
501 put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8);
502 put_ebml_string (pb, EBML_ID_DOCTYPE , "matroska");
503 put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 2);
504 put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 2);
505 end_ebml_master(pb, ebml_header);
507 mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT);
508 mkv->segment_offset = url_ftell(pb);
510 // we write 2 seek heads - one at the end of the file to point to each cluster, and
511 // one at the beginning to point to all other level one elements (including the seek
512 // head at the end of the file), which isn't more than 10 elements if we only write one
513 // of each other currently defined level 1 element
514 mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
515 mkv->cluster_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 0);
517 if (mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, url_ftell(pb)) < 0)
520 segment_info = start_ebml_master(pb, MATROSKA_ID_INFO);
521 put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
522 if (strlen(s->title))
523 put_ebml_string(pb, MATROSKA_ID_TITLE, s->title);
524 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
525 put_ebml_string(pb, MATROSKA_ID_MUXINGAPP, LIBAVFORMAT_IDENT);
526 // XXX: both are required; something better for writing app?
527 put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
530 // reserve space for the duration
532 mkv->duration_offset = url_ftell(pb);
533 put_ebml_void(pb, 11); // assumes double-precision float to be written
534 end_ebml_master(pb, segment_info);
536 if (mkv_write_tracks(s) < 0)
539 if (mkv_add_seekhead_entry(mkv->cluster_seekhead, MATROSKA_ID_CLUSTER, url_ftell(pb)) < 0)
542 mkv->cluster_pos = url_ftell(pb);
543 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER);
544 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, 0);
545 mkv->cluster_pts = 0;
547 mkv->cues = mkv_start_cues(mkv->segment_offset);
548 if (mkv->cues == NULL)
554 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
556 MatroskaMuxContext *mkv = s->priv_data;
557 ByteIOContext *pb = &s->pb;
558 int keyframe = !!(pkt->flags & PKT_FLAG_KEY);
560 // start a new cluster every 5 MB or 5 sec
561 if (url_ftell(pb) > mkv->cluster + 5*1024*1024 || pkt->pts > mkv->cluster_pts + 5000) {
562 end_ebml_master(pb, mkv->cluster);
564 if (mkv_add_seekhead_entry(mkv->cluster_seekhead, MATROSKA_ID_CLUSTER, url_ftell(pb)) < 0)
567 mkv->cluster_pos = url_ftell(pb);
568 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER);
569 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, pkt->pts);
570 mkv->cluster_pts = pkt->pts;
573 put_ebml_id(pb, MATROSKA_ID_SIMPLEBLOCK);
574 put_ebml_size(pb, pkt->size + 4, 0);
575 put_byte(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
576 put_be16(pb, pkt->pts - mkv->cluster_pts);
577 put_byte(pb, keyframe << 7);
578 put_buffer(pb, pkt->data, pkt->size);
580 if (s->streams[pkt->stream_index]->codec->codec_type == CODEC_TYPE_VIDEO && keyframe) {
581 if (mkv_add_cuepoint(mkv->cues, pkt, mkv->cluster_pos) < 0)
585 mkv->duration = pkt->pts + pkt->duration;
589 static int mkv_write_trailer(AVFormatContext *s)
591 MatroskaMuxContext *mkv = s->priv_data;
592 ByteIOContext *pb = &s->pb;
593 offset_t currentpos, second_seekhead, cuespos;
595 end_ebml_master(pb, mkv->cluster);
597 cuespos = mkv_write_cues(pb, mkv->cues);
598 second_seekhead = mkv_write_seekhead(pb, mkv->cluster_seekhead);
600 mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES , cuespos);
601 mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_SEEKHEAD, second_seekhead);
602 mkv_write_seekhead(pb, mkv->main_seekhead);
604 // update the duration
605 currentpos = url_ftell(pb);
606 url_fseek(pb, mkv->duration_offset, SEEK_SET);
607 put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);
608 url_fseek(pb, currentpos, SEEK_SET);
610 end_ebml_master(pb, mkv->segment);
614 AVOutputFormat matroska_muxer = {
616 "Matroska File Format",
619 sizeof(MatroskaMuxContext),
625 .codec_tag = (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, 0},