]> git.sesse.net Git - x264/blob - matroska.c
fix shared libs on MacOSX
[x264] / matroska.c
1 /*****************************************************************************
2  * matroska.c:
3  *****************************************************************************
4  * Copyright (C) 2005 x264 project
5  * $Id: $
6  *
7  * Authors: Mike Matsnev
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include "common/osdep.h"
27 #include "matroska.h"
28
29 #define CLSIZE    1048576
30 #define CHECK(x)  do { if ((x) < 0) return -1; } while (0)
31
32 struct mk_Context {
33   struct mk_Context *next, **prev, *parent;
34   struct mk_Writer  *owner;
35   unsigned          id;
36
37   void              *data;
38   unsigned          d_cur, d_max;
39 };
40
41 typedef struct mk_Context mk_Context;
42
43 struct mk_Writer {
44   FILE                *fp;
45
46   unsigned            duration_ptr;
47
48   mk_Context          *root, *cluster, *frame;
49   mk_Context          *freelist;
50   mk_Context          *actlist;
51
52   int64_t             def_duration;
53   int64_t             timescale;
54   int64_t             cluster_tc_scaled;
55   int64_t             frame_tc, prev_frame_tc_scaled, max_frame_tc;
56
57   char                wrote_header, in_frame, keyframe;
58 };
59
60 static mk_Context *mk_createContext(mk_Writer *w, mk_Context *parent, unsigned id) {
61   mk_Context  *c;
62
63   if (w->freelist) {
64     c = w->freelist;
65     w->freelist = w->freelist->next;
66   } else {
67     c = malloc(sizeof(*c));
68     memset(c, 0, sizeof(*c));
69   }
70
71   if (c == NULL)
72     return NULL;
73
74   c->parent = parent;
75   c->owner = w;
76   c->id = id;
77
78   if (c->owner->actlist)
79     c->owner->actlist->prev = &c->next;
80   c->next = c->owner->actlist;
81   c->prev = &c->owner->actlist;
82   c->owner->actlist = c;
83
84   return c;
85 }
86
87 static int        mk_appendContextData(mk_Context *c, const void *data, unsigned size) {
88   unsigned  ns = c->d_cur + size;
89
90   if (ns > c->d_max) {
91     void      *dp;
92     unsigned  dn = c->d_max ? c->d_max << 1 : 16;
93     while (ns > dn)
94       dn <<= 1;
95
96     dp = realloc(c->data, dn);
97     if (dp == NULL)
98       return -1;
99
100     c->data = dp;
101     c->d_max = dn;
102   }
103
104   memcpy((char*)c->data + c->d_cur, data, size);
105
106   c->d_cur = ns;
107
108   return 0;
109 }
110
111 static int        mk_writeID(mk_Context *c, unsigned id) {
112   unsigned char   c_id[4] = { id >> 24, id >> 16, id >> 8, id };
113
114   if (c_id[0])
115     return mk_appendContextData(c, c_id, 4);
116   if (c_id[1])
117     return mk_appendContextData(c, c_id+1, 3);
118   if (c_id[2])
119     return mk_appendContextData(c, c_id+2, 2);
120   return mk_appendContextData(c, c_id+3, 1);
121 }
122
123 static int        mk_writeSize(mk_Context *c, unsigned size) {
124   unsigned char   c_size[5] = { 0x08, size >> 24, size >> 16, size >> 8, size };
125
126   if (size < 0x7f) {
127     c_size[4] |= 0x80;
128     return mk_appendContextData(c, c_size+4, 1);
129   }
130   if (size < 0x3fff) {
131     c_size[3] |= 0x40;
132     return mk_appendContextData(c, c_size+3, 2);
133   }
134   if (size < 0x1fffff) {
135     c_size[2] |= 0x20;
136     return mk_appendContextData(c, c_size+2, 3);
137   }
138   if (size < 0x0fffffff) {
139     c_size[1] |= 0x10;
140     return mk_appendContextData(c, c_size+1, 4);
141   }
142   return mk_appendContextData(c, c_size, 5);
143 }
144
145 static int        mk_flushContextID(mk_Context *c) {
146   unsigned char ff = 0xff;
147
148   if (c->id == 0)
149     return 0;
150
151   CHECK(mk_writeID(c->parent, c->id));
152   CHECK(mk_appendContextData(c->parent, &ff, 1));
153
154   c->id = 0;
155
156   return 0;
157 }
158
159 static int        mk_flushContextData(mk_Context *c) {
160   if (c->d_cur == 0)
161     return 0;
162
163   if (c->parent)
164     CHECK(mk_appendContextData(c->parent, c->data, c->d_cur));
165   else
166     if (fwrite(c->data, c->d_cur, 1, c->owner->fp) != 1)
167       return -1;
168
169   c->d_cur = 0;
170
171   return 0;
172 }
173
174 static int        mk_closeContext(mk_Context *c, unsigned *off) {
175   if (c->id) {
176     CHECK(mk_writeID(c->parent, c->id));
177     CHECK(mk_writeSize(c->parent, c->d_cur));
178   }
179
180   if (c->parent && off != NULL)
181     *off += c->parent->d_cur;
182
183   CHECK(mk_flushContextData(c));
184
185   if (c->next)
186     c->next->prev = c->prev;
187   *(c->prev) = c->next;
188   c->next = c->owner->freelist;
189   c->owner->freelist = c;
190
191   return 0;
192 }
193
194 static void       mk_destroyContexts(mk_Writer *w) {
195   mk_Context  *cur, *next;
196
197   for (cur = w->freelist; cur; cur = next) {
198     next = cur->next;
199     free(cur->data);
200     free(cur);
201   }
202
203   for (cur = w->actlist; cur; cur = next) {
204     next = cur->next;
205     free(cur->data);
206     free(cur);
207   }
208
209   w->freelist = w->actlist = w->root = NULL;
210 }
211
212 static int        mk_writeStr(mk_Context *c, unsigned id, const char *str) {
213   size_t  len = strlen(str);
214
215   CHECK(mk_writeID(c, id));
216   CHECK(mk_writeSize(c, len));
217   CHECK(mk_appendContextData(c, str, len));
218   return 0;
219 }
220
221 static int        mk_writeBin(mk_Context *c, unsigned id, const void *data, unsigned size) {
222   CHECK(mk_writeID(c, id));
223   CHECK(mk_writeSize(c, size));
224   CHECK(mk_appendContextData(c, data, size));
225   return 0;
226 }
227
228 static int        mk_writeUInt(mk_Context *c, unsigned id, int64_t ui) {
229   unsigned char   c_ui[8] = { ui >> 56, ui >> 48, ui >> 40, ui >> 32, ui >> 24, ui >> 16, ui >> 8, ui };
230   unsigned        i = 0;
231
232   CHECK(mk_writeID(c, id));
233   while (i < 7 && c_ui[i] == 0)
234     ++i;
235   CHECK(mk_writeSize(c, 8 - i));
236   CHECK(mk_appendContextData(c, c_ui+i, 8 - i));
237   return 0;
238 }
239
240 static int        mk_writeSInt(mk_Context *c, unsigned id, int64_t si) {
241   unsigned char   c_si[8] = { si >> 56, si >> 48, si >> 40, si >> 32, si >> 24, si >> 16, si >> 8, si };
242   unsigned        i = 0;
243
244   CHECK(mk_writeID(c, id));
245   if (si < 0)
246     while (i < 7 && c_si[i] == 0xff && c_si[i+1] & 0x80)
247       ++i;
248   else
249     while (i < 7 && c_si[i] == 0 && !(c_si[i+1] & 0x80))
250       ++i;
251   CHECK(mk_writeSize(c, 8 - i));
252   CHECK(mk_appendContextData(c, c_si+i, 8 - i));
253   return 0;
254 }
255
256 static int        mk_writeFloatRaw(mk_Context *c, float f) {
257   union {
258     float f;
259     unsigned u;
260   } u;
261   unsigned char c_f[4];
262
263   u.f = f;
264   c_f[0] = u.u >> 24;
265   c_f[1] = u.u >> 16;
266   c_f[2] = u.u >> 8;
267   c_f[3] = u.u;
268
269   return mk_appendContextData(c, c_f, 4);
270 }
271
272 static int        mk_writeFloat(mk_Context *c, unsigned id, float f) {
273   CHECK(mk_writeID(c, id));
274   CHECK(mk_writeSize(c, 4));
275   CHECK(mk_writeFloatRaw(c, f));
276   return 0;
277 }
278
279 static unsigned   mk_ebmlSizeSize(unsigned s) {
280   if (s < 0x7f)
281     return 1;
282   if (s < 0x3fff)
283     return 2;
284   if (s < 0x1fffff)
285     return 3;
286   if (s < 0x0fffffff)
287     return 4;
288   return 5;
289 }
290
291 static unsigned   mk_ebmlSIntSize(int64_t si) {
292   unsigned char   c_si[8] = { si >> 56, si >> 48, si >> 40, si >> 32, si >> 24, si >> 16, si >> 8, si };
293   unsigned        i = 0;
294
295   if (si < 0)
296     while (i < 7 && c_si[i] == 0xff && c_si[i+1] & 0x80)
297       ++i;
298   else
299     while (i < 7 && c_si[i] == 0 && !(c_si[i+1] & 0x80))
300       ++i;
301
302   return 8 - i;
303 }
304
305 mk_Writer *mk_createWriter(const char *filename) {
306   mk_Writer *w = malloc(sizeof(*w));
307   if (w == NULL)
308     return NULL;
309
310   memset(w, 0, sizeof(*w));
311
312   w->root = mk_createContext(w, NULL, 0);
313   if (w->root == NULL) {
314     free(w);
315     return NULL;
316   }
317
318   w->fp = fopen(filename, "wb");
319   if (w->fp == NULL) {
320     mk_destroyContexts(w);
321     free(w);
322     return NULL;
323   }
324
325   w->timescale = 1000000;
326
327   return w;
328 }
329
330 int       mk_writeHeader(mk_Writer *w, const char *writingApp,
331                          const char *codecID,
332                          const void *codecPrivate, unsigned codecPrivateSize,
333                          int64_t default_frame_duration,
334                          int64_t timescale,
335                          unsigned width, unsigned height,
336                          unsigned d_width, unsigned d_height)
337 {
338   mk_Context  *c, *ti, *v;
339
340   if (w->wrote_header)
341     return -1;
342
343   w->timescale = timescale;
344   w->def_duration = default_frame_duration;
345
346   if ((c = mk_createContext(w, w->root, 0x1a45dfa3)) == NULL) // EBML
347     return -1;
348   CHECK(mk_writeUInt(c, 0x4286, 1)); // EBMLVersion
349   CHECK(mk_writeUInt(c, 0x42f7, 1)); // EBMLReadVersion
350   CHECK(mk_writeUInt(c, 0x42f2, 4)); // EBMLMaxIDLength
351   CHECK(mk_writeUInt(c, 0x42f3, 8)); // EBMLMaxSizeLength
352   CHECK(mk_writeStr(c, 0x4282, "matroska")); // DocType
353   CHECK(mk_writeUInt(c, 0x4287, 1)); // DocTypeVersion
354   CHECK(mk_writeUInt(c, 0x4285, 1)); // DocTypeReadversion
355   CHECK(mk_closeContext(c, 0));
356
357   if ((c = mk_createContext(w, w->root, 0x18538067)) == NULL) // Segment
358     return -1;
359   CHECK(mk_flushContextID(c));
360   CHECK(mk_closeContext(c, 0));
361
362   if ((c = mk_createContext(w, w->root, 0x1549a966)) == NULL) // SegmentInfo
363     return -1;
364   CHECK(mk_writeStr(c, 0x4d80, "Haali Matroska Writer b0"));
365   CHECK(mk_writeStr(c, 0x5741, writingApp));
366   CHECK(mk_writeUInt(c, 0x2ad7b1, w->timescale));
367   CHECK(mk_writeFloat(c, 0x4489, 0));
368   w->duration_ptr = c->d_cur - 4;
369   CHECK(mk_closeContext(c, &w->duration_ptr));
370
371   if ((c = mk_createContext(w, w->root, 0x1654ae6b)) == NULL) // tracks
372     return -1;
373   if ((ti = mk_createContext(w, c, 0xae)) == NULL) // TrackEntry
374     return -1;
375   CHECK(mk_writeUInt(ti, 0xd7, 1)); // TrackNumber
376   CHECK(mk_writeUInt(ti, 0x73c5, 1)); // TrackUID
377   CHECK(mk_writeUInt(ti, 0x83, 1)); // TrackType
378   CHECK(mk_writeUInt(ti, 0x9c, 0)); // FlagLacing
379   CHECK(mk_writeStr(ti, 0x86, codecID)); // CodecID
380   if (codecPrivateSize)
381     CHECK(mk_writeBin(ti, 0x63a2, codecPrivate, codecPrivateSize)); // CodecPrivate
382   if (default_frame_duration)
383     CHECK(mk_writeUInt(ti, 0x23e383, default_frame_duration)); // DefaultDuration
384
385   if ((v = mk_createContext(w, ti, 0xe0)) == NULL) // Video
386     return -1;
387   CHECK(mk_writeUInt(v, 0xb0, width));
388   CHECK(mk_writeUInt(v, 0xba, height));
389   CHECK(mk_writeUInt(v, 0x54b0, d_width));
390   CHECK(mk_writeUInt(v, 0x54ba, d_height));
391   CHECK(mk_closeContext(v, 0));
392
393   CHECK(mk_closeContext(ti, 0));
394
395   CHECK(mk_closeContext(c, 0));
396
397   CHECK(mk_flushContextData(w->root));
398
399   w->wrote_header = 1;
400
401   return 0;
402 }
403
404 static int mk_closeCluster(mk_Writer *w) {
405   if (w->cluster == NULL)
406     return 0;
407   CHECK(mk_closeContext(w->cluster, 0));
408   w->cluster = NULL;
409   CHECK(mk_flushContextData(w->root));
410   return 0;
411 }
412
413 int       mk_flushFrame(mk_Writer *w) {
414   int64_t       delta, ref = 0;
415   unsigned      fsize, bgsize;
416   unsigned char c_delta_flags[3];
417
418   if (!w->in_frame)
419     return 0;
420
421   delta = w->frame_tc/w->timescale - w->cluster_tc_scaled;
422   if (delta > 32767ll || delta < -32768ll)
423     CHECK(mk_closeCluster(w));
424
425   if (w->cluster == NULL) {
426     w->cluster_tc_scaled = w->frame_tc / w->timescale;
427     w->cluster = mk_createContext(w, w->root, 0x1f43b675); // Cluster
428     if (w->cluster == NULL)
429       return -1;
430
431     CHECK(mk_writeUInt(w->cluster, 0xe7, w->cluster_tc_scaled)); // Timecode
432
433     delta = 0;
434   }
435
436   fsize = w->frame ? w->frame->d_cur : 0;
437   bgsize = fsize + 4 + mk_ebmlSizeSize(fsize + 4) + 1;
438   if (!w->keyframe) {
439     ref = w->prev_frame_tc_scaled - w->cluster_tc_scaled - delta;
440     bgsize += 1 + 1 + mk_ebmlSIntSize(ref);
441   }
442
443   CHECK(mk_writeID(w->cluster, 0xa0)); // BlockGroup
444   CHECK(mk_writeSize(w->cluster, bgsize));
445   CHECK(mk_writeID(w->cluster, 0xa1)); // Block
446   CHECK(mk_writeSize(w->cluster, fsize + 4));
447   CHECK(mk_writeSize(w->cluster, 1)); // track number
448
449   c_delta_flags[0] = delta >> 8;
450   c_delta_flags[1] = delta;
451   c_delta_flags[2] = 0;
452   CHECK(mk_appendContextData(w->cluster, c_delta_flags, 3));
453   if (w->frame) {
454     CHECK(mk_appendContextData(w->cluster, w->frame->data, w->frame->d_cur));
455     w->frame->d_cur = 0;
456   }
457   if (!w->keyframe)
458     CHECK(mk_writeSInt(w->cluster, 0xfb, ref)); // ReferenceBlock
459
460   w->in_frame = 0;
461   w->prev_frame_tc_scaled = w->cluster_tc_scaled + delta;
462
463   if (w->cluster->d_cur > CLSIZE)
464     CHECK(mk_closeCluster(w));
465
466   return 0;
467 }
468
469 int       mk_startFrame(mk_Writer *w) {
470   if (mk_flushFrame(w) < 0)
471     return -1;
472
473   w->in_frame = 1;
474   w->keyframe = 0;
475
476   return 0;
477 }
478
479 int       mk_setFrameFlags(mk_Writer *w,int64_t timestamp, int keyframe) {
480   if (!w->in_frame)
481     return -1;
482
483   w->frame_tc = timestamp;
484   w->keyframe = keyframe != 0;
485
486   if (w->max_frame_tc < timestamp)
487     w->max_frame_tc = timestamp;
488
489   return 0;
490 }
491
492 int       mk_addFrameData(mk_Writer *w, const void *data, unsigned size) {
493   if (!w->in_frame)
494     return -1;
495
496   if (w->frame == NULL)
497     if ((w->frame = mk_createContext(w, NULL, 0)) == NULL)
498       return -1;
499
500   return mk_appendContextData(w->frame, data, size);
501 }
502
503 int       mk_close(mk_Writer *w) {
504   int   ret = 0;
505   if (mk_flushFrame(w) < 0 || mk_closeCluster(w) < 0)
506     ret = -1;
507   if (w->wrote_header) {
508     fseek(w->fp, w->duration_ptr, SEEK_SET);
509     if (mk_writeFloatRaw(w->root, (float)((double)(w->max_frame_tc+w->def_duration) / w->timescale)) < 0 ||
510         mk_flushContextData(w->root) < 0)
511       ret = -1;
512   }
513   mk_destroyContexts(w);
514   fclose(w->fp);
515   free(w);
516   return ret;
517 }
518