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
26 typedef struct MatroskaMuxContext {
30 static void put_ebml_id(ByteIOContext *pb, unsigned int id)
33 put_byte(pb, id >> 24);
35 put_byte(pb, id >> 16);
37 put_byte(pb, id >> 8);
41 // XXX: test this thoroughly and get rid of minbytes hack (currently needed to
42 // use up all of the space reserved in start_ebml_master)
43 static void put_ebml_size(ByteIOContext *pb, uint64_t size, int minbytes)
46 while (size >> (bytes*8 + 7-bytes) > 0) bytes++;
48 // sizes larger than this are currently undefined in EBML
49 // XXX: error condition?
50 if (size > (1ULL<<56)-1) return;
52 put_byte(pb, (0x80 >> bytes) | (size >> bytes*8));
53 for (bytes -= 1; bytes >= 0; bytes--)
54 put_byte(pb, size >> bytes*8);
57 static void put_ebml_uint(ByteIOContext *pb, unsigned int elementid, uint64_t val)
60 while (val >> bytes*8) bytes++;
62 put_ebml_id(pb, elementid);
63 put_ebml_size(pb, bytes, 0);
64 for (bytes -= 1; bytes >= 0; bytes--)
65 put_byte(pb, val >> bytes*8);
68 //static void put_ebml_sint(ByteIOContext *pb, unsigned int elementid, int64_t val)
70 static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid,
71 uint8_t *buf, int size)
73 put_ebml_id(pb, elementid);
74 put_ebml_size(pb, size, 0);
75 put_buffer(pb, buf, size);
78 // XXX: should we do any special checking for valid strings for these 2 functions?
79 static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, char *str)
81 put_ebml_binary(pb, elementid, str, strlen(str));
84 static void put_ebml_utf8(ByteIOContext *pb, unsigned int elementid, char *str)
86 put_ebml_binary(pb, elementid, str, strlen(str));
89 static offset_t start_ebml_master(ByteIOContext *pb, unsigned int elementid)
91 put_ebml_id(pb, elementid);
92 // XXX: this always reserves the maximum needed space to store any size value
93 // we should be smarter (additional parameter for expected size?)
94 put_ebml_size(pb, (1ULL<<56)-1, 0); // largest unknown size
98 static void end_ebml_master(ByteIOContext *pb, offset_t start)
100 offset_t pos = url_ftell(pb);
102 url_fseek(pb, start - 8, SEEK_SET);
103 put_ebml_size(pb, pos - start, 7);
104 url_fseek(pb, pos, SEEK_SET);
108 static int mkv_write_header(AVFormatContext *s)
110 MatroskaMuxContext *mkv = s->priv_data;
111 ByteIOContext *pb = &s->pb;
112 offset_t ebml_header, segment_info, tracks;
115 ebml_header = start_ebml_master(pb, EBML_ID_HEADER);
116 put_ebml_uint(pb, EBML_ID_EBMLVERSION, 1);
117 put_ebml_uint(pb, EBML_ID_EBMLREADVERSION, 1);
118 put_ebml_uint(pb, EBML_ID_EBMLMAXIDLENGTH, 4);
119 put_ebml_uint(pb, EBML_ID_EBMLMAXSIZELENGTH, 8);
120 put_ebml_string(pb, EBML_ID_DOCTYPE, "matroska");
121 put_ebml_uint(pb, EBML_ID_DOCTYPEVERSION, 1);
122 put_ebml_uint(pb, EBML_ID_DOCTYPEREADVERSION, 1);
123 end_ebml_master(pb, ebml_header);
125 mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT);
127 segment_info = start_ebml_master(pb, MATROSKA_ID_INFO);
128 put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
129 if (strlen(s->title))
130 put_ebml_utf8(pb, MATROSKA_ID_TITLE, s->title);
131 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
132 put_ebml_utf8(pb, MATROSKA_ID_MUXINGAPP, LIBAVFORMAT_IDENT);
133 // XXX: both are required; something better for writing app?
134 put_ebml_utf8(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
136 // XXX: segment UID and duration
137 end_ebml_master(pb, segment_info);
139 tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS);
140 for (i = 0; i < s->nb_streams; i++) {
141 AVStream *st = s->streams[i];
142 offset_t track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY);
144 end_ebml_master(pb, track);
146 end_ebml_master(pb, tracks);
148 put_be64(pb, 0xdeadbeef);
152 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
154 ByteIOContext *pb = &s->pb;
155 put_buffer(pb, pkt->data, pkt->size);
159 static int mkv_write_trailer(AVFormatContext *s)
161 MatroskaMuxContext *mkv = s->priv_data;
162 ByteIOContext *pb = &s->pb;
163 end_ebml_master(pb, mkv->segment);
167 AVOutputFormat matroska_muxer = {
169 "Matroska File Format",
172 sizeof(MatroskaMuxContext),
178 .codec_tag = (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, 0},