1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 x264 project
7 * Authors: Mike Matsnev
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
26 #include "common/osdep.h"
29 #define CLSIZE 1048576
30 #define CHECK(x) do { if ((x) < 0) return -1; } while (0)
33 struct mk_Context *next, **prev, *parent;
34 struct mk_Writer *owner;
38 unsigned d_cur, d_max;
41 typedef struct mk_Context mk_Context;
46 unsigned duration_ptr;
48 mk_Context *root, *cluster, *frame;
54 int64_t cluster_tc_scaled;
55 int64_t frame_tc, prev_frame_tc_scaled, max_frame_tc;
57 char wrote_header, in_frame, keyframe;
60 static mk_Context *mk_createContext(mk_Writer *w, mk_Context *parent, unsigned id) {
65 w->freelist = w->freelist->next;
67 c = malloc(sizeof(*c));
68 memset(c, 0, sizeof(*c));
78 if (c->owner->actlist)
79 c->owner->actlist->prev = &c->next;
80 c->next = c->owner->actlist;
81 c->prev = &c->owner->actlist;
86 static int mk_appendContextData(mk_Context *c, const void *data, unsigned size) {
87 unsigned ns = c->d_cur + size;
91 unsigned dn = c->d_max ? c->d_max << 1 : 16;
95 dp = realloc(c->data, dn);
103 memcpy((char*)c->data + c->d_cur, data, size);
110 static int mk_writeID(mk_Context *c, unsigned id) {
111 unsigned char c_id[4] = { id >> 24, id >> 16, id >> 8, id };
114 return mk_appendContextData(c, c_id, 4);
116 return mk_appendContextData(c, c_id+1, 3);
118 return mk_appendContextData(c, c_id+2, 2);
119 return mk_appendContextData(c, c_id+3, 1);
122 static int mk_writeSize(mk_Context *c, unsigned size) {
123 unsigned char c_size[5] = { 0x08, size >> 24, size >> 16, size >> 8, size };
127 return mk_appendContextData(c, c_size+4, 1);
131 return mk_appendContextData(c, c_size+3, 2);
133 if (size < 0x1fffff) {
135 return mk_appendContextData(c, c_size+2, 3);
137 if (size < 0x0fffffff) {
139 return mk_appendContextData(c, c_size+1, 4);
141 return mk_appendContextData(c, c_size, 5);
144 static int mk_flushContextID(mk_Context *c) {
145 unsigned char ff = 0xff;
150 CHECK(mk_writeID(c->parent, c->id));
151 CHECK(mk_appendContextData(c->parent, &ff, 1));
158 static int mk_flushContextData(mk_Context *c) {
163 CHECK(mk_appendContextData(c->parent, c->data, c->d_cur));
165 if (fwrite(c->data, c->d_cur, 1, c->owner->fp) != 1)
173 static int mk_closeContext(mk_Context *c, unsigned *off) {
175 CHECK(mk_writeID(c->parent, c->id));
176 CHECK(mk_writeSize(c->parent, c->d_cur));
179 if (c->parent && off != NULL)
180 *off += c->parent->d_cur;
182 CHECK(mk_flushContextData(c));
185 c->next->prev = c->prev;
186 *(c->prev) = c->next;
187 c->next = c->owner->freelist;
188 c->owner->freelist = c;
193 static void mk_destroyContexts(mk_Writer *w) {
194 mk_Context *cur, *next;
196 for (cur = w->freelist; cur; cur = next) {
202 for (cur = w->actlist; cur; cur = next) {
208 w->freelist = w->actlist = w->root = NULL;
211 static int mk_writeStr(mk_Context *c, unsigned id, const char *str) {
212 size_t len = strlen(str);
214 CHECK(mk_writeID(c, id));
215 CHECK(mk_writeSize(c, len));
216 CHECK(mk_appendContextData(c, str, len));
220 static int mk_writeBin(mk_Context *c, unsigned id, const void *data, unsigned size) {
221 CHECK(mk_writeID(c, id));
222 CHECK(mk_writeSize(c, size));
223 CHECK(mk_appendContextData(c, data, size));
227 static int mk_writeUInt(mk_Context *c, unsigned id, int64_t ui) {
228 unsigned char c_ui[8] = { ui >> 56, ui >> 48, ui >> 40, ui >> 32, ui >> 24, ui >> 16, ui >> 8, ui };
231 CHECK(mk_writeID(c, id));
232 while (i < 7 && c_ui[i] == 0)
234 CHECK(mk_writeSize(c, 8 - i));
235 CHECK(mk_appendContextData(c, c_ui+i, 8 - i));
239 static int mk_writeSInt(mk_Context *c, unsigned id, int64_t si) {
240 unsigned char c_si[8] = { si >> 56, si >> 48, si >> 40, si >> 32, si >> 24, si >> 16, si >> 8, si };
243 CHECK(mk_writeID(c, id));
245 while (i < 7 && c_si[i] == 0xff && c_si[i+1] & 0x80)
248 while (i < 7 && c_si[i] == 0 && !(c_si[i+1] & 0x80))
250 CHECK(mk_writeSize(c, 8 - i));
251 CHECK(mk_appendContextData(c, c_si+i, 8 - i));
255 static int mk_writeFloatRaw(mk_Context *c, float f) {
260 unsigned char c_f[4];
268 return mk_appendContextData(c, c_f, 4);
271 static int mk_writeFloat(mk_Context *c, unsigned id, float f) {
272 CHECK(mk_writeID(c, id));
273 CHECK(mk_writeSize(c, 4));
274 CHECK(mk_writeFloatRaw(c, f));
278 static unsigned mk_ebmlSizeSize(unsigned s) {
290 static unsigned mk_ebmlSIntSize(int64_t si) {
291 unsigned char c_si[8] = { si >> 56, si >> 48, si >> 40, si >> 32, si >> 24, si >> 16, si >> 8, si };
295 while (i < 7 && c_si[i] == 0xff && c_si[i+1] & 0x80)
298 while (i < 7 && c_si[i] == 0 && !(c_si[i+1] & 0x80))
304 mk_Writer *mk_createWriter(const char *filename) {
305 mk_Writer *w = malloc(sizeof(*w));
309 memset(w, 0, sizeof(*w));
311 w->root = mk_createContext(w, NULL, 0);
312 if (w->root == NULL) {
317 w->fp = fopen(filename, "wb");
319 mk_destroyContexts(w);
324 w->timescale = 1000000;
329 int mk_writeHeader(mk_Writer *w, const char *writingApp,
331 const void *codecPrivate, unsigned codecPrivateSize,
332 int64_t default_frame_duration,
334 unsigned width, unsigned height,
335 unsigned d_width, unsigned d_height)
337 mk_Context *c, *ti, *v;
342 w->timescale = timescale;
343 w->def_duration = default_frame_duration;
345 if ((c = mk_createContext(w, w->root, 0x1a45dfa3)) == NULL) // EBML
347 CHECK(mk_writeUInt(c, 0x4286, 1)); // EBMLVersion
348 CHECK(mk_writeUInt(c, 0x42f7, 1)); // EBMLReadVersion
349 CHECK(mk_writeUInt(c, 0x42f2, 4)); // EBMLMaxIDLength
350 CHECK(mk_writeUInt(c, 0x42f3, 8)); // EBMLMaxSizeLength
351 CHECK(mk_writeStr(c, 0x4282, "matroska")); // DocType
352 CHECK(mk_writeUInt(c, 0x4287, 1)); // DocTypeVersion
353 CHECK(mk_writeUInt(c, 0x4285, 1)); // DocTypeReadversion
354 CHECK(mk_closeContext(c, 0));
356 if ((c = mk_createContext(w, w->root, 0x18538067)) == NULL) // Segment
358 CHECK(mk_flushContextID(c));
359 CHECK(mk_closeContext(c, 0));
361 if ((c = mk_createContext(w, w->root, 0x1549a966)) == NULL) // SegmentInfo
363 CHECK(mk_writeStr(c, 0x4d80, "Haali Matroska Writer b0"));
364 CHECK(mk_writeStr(c, 0x5741, writingApp));
365 CHECK(mk_writeUInt(c, 0x2ad7b1, w->timescale));
366 CHECK(mk_writeFloat(c, 0x4489, 0));
367 w->duration_ptr = c->d_cur - 4;
368 CHECK(mk_closeContext(c, &w->duration_ptr));
370 if ((c = mk_createContext(w, w->root, 0x1654ae6b)) == NULL) // tracks
372 if ((ti = mk_createContext(w, c, 0xae)) == NULL) // TrackEntry
374 CHECK(mk_writeUInt(ti, 0xd7, 1)); // TrackNumber
375 CHECK(mk_writeUInt(ti, 0x73c5, 1)); // TrackUID
376 CHECK(mk_writeUInt(ti, 0x83, 1)); // TrackType
377 CHECK(mk_writeUInt(ti, 0x9c, 0)); // FlagLacing
378 CHECK(mk_writeStr(ti, 0x86, codecID)); // CodecID
379 if (codecPrivateSize)
380 CHECK(mk_writeBin(ti, 0x63a2, codecPrivate, codecPrivateSize)); // CodecPrivate
381 if (default_frame_duration)
382 CHECK(mk_writeUInt(ti, 0x23e383, default_frame_duration)); // DefaultDuration
384 if ((v = mk_createContext(w, ti, 0xe0)) == NULL) // Video
386 CHECK(mk_writeUInt(v, 0xb0, width));
387 CHECK(mk_writeUInt(v, 0xba, height));
388 CHECK(mk_writeUInt(v, 0x54b0, d_width));
389 CHECK(mk_writeUInt(v, 0x54ba, d_height));
390 CHECK(mk_closeContext(v, 0));
392 CHECK(mk_closeContext(ti, 0));
394 CHECK(mk_closeContext(c, 0));
396 CHECK(mk_flushContextData(w->root));
403 static int mk_closeCluster(mk_Writer *w) {
404 if (w->cluster == NULL)
406 CHECK(mk_closeContext(w->cluster, 0));
408 CHECK(mk_flushContextData(w->root));
412 int mk_flushFrame(mk_Writer *w) {
413 int64_t delta, ref = 0;
414 unsigned fsize, bgsize;
415 unsigned char c_delta_flags[3];
420 delta = w->frame_tc/w->timescale - w->cluster_tc_scaled;
421 if (delta > 32767ll || delta < -32768ll)
422 CHECK(mk_closeCluster(w));
424 if (w->cluster == NULL) {
425 w->cluster_tc_scaled = w->frame_tc / w->timescale;
426 w->cluster = mk_createContext(w, w->root, 0x1f43b675); // Cluster
427 if (w->cluster == NULL)
430 CHECK(mk_writeUInt(w->cluster, 0xe7, w->cluster_tc_scaled)); // Timecode
435 fsize = w->frame ? w->frame->d_cur : 0;
436 bgsize = fsize + 4 + mk_ebmlSizeSize(fsize + 4) + 1;
438 ref = w->prev_frame_tc_scaled - w->cluster_tc_scaled - delta;
439 bgsize += 1 + 1 + mk_ebmlSIntSize(ref);
442 CHECK(mk_writeID(w->cluster, 0xa0)); // BlockGroup
443 CHECK(mk_writeSize(w->cluster, bgsize));
444 CHECK(mk_writeID(w->cluster, 0xa1)); // Block
445 CHECK(mk_writeSize(w->cluster, fsize + 4));
446 CHECK(mk_writeSize(w->cluster, 1)); // track number
448 c_delta_flags[0] = delta >> 8;
449 c_delta_flags[1] = delta;
450 c_delta_flags[2] = 0;
451 CHECK(mk_appendContextData(w->cluster, c_delta_flags, 3));
453 CHECK(mk_appendContextData(w->cluster, w->frame->data, w->frame->d_cur));
457 CHECK(mk_writeSInt(w->cluster, 0xfb, ref)); // ReferenceBlock
460 w->prev_frame_tc_scaled = w->cluster_tc_scaled + delta;
462 if (w->cluster->d_cur > CLSIZE)
463 CHECK(mk_closeCluster(w));
468 int mk_startFrame(mk_Writer *w) {
469 if (mk_flushFrame(w) < 0)
478 int mk_setFrameFlags(mk_Writer *w,int64_t timestamp, int keyframe) {
482 w->frame_tc = timestamp;
483 w->keyframe = keyframe != 0;
485 if (w->max_frame_tc < timestamp)
486 w->max_frame_tc = timestamp;
491 int mk_addFrameData(mk_Writer *w, const void *data, unsigned size) {
495 if (w->frame == NULL)
496 if ((w->frame = mk_createContext(w, NULL, 0)) == NULL)
499 return mk_appendContextData(w->frame, data, size);
502 int mk_close(mk_Writer *w) {
504 if (mk_flushFrame(w) < 0 || mk_closeCluster(w) < 0)
506 if (w->wrote_header) {
507 fseek(w->fp, w->duration_ptr, SEEK_SET);
508 if (mk_writeFloatRaw(w->root, (float)((double)(w->max_frame_tc+w->def_duration) / w->timescale)) < 0 ||
509 mk_flushContextData(w->root) < 0)
512 mk_destroyContexts(w);