1 /*****************************************************************************
2 * matroska_ebml.c: matroska muxer utilities
3 *****************************************************************************
4 * Copyright (C) 2005-2011 x264 project
6 * Authors: Mike Matsnev <mike@haali.su>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at licensing@x264.com.
24 *****************************************************************************/
27 #include "matroska_ebml.h"
29 #define CLSIZE 1048576
38 struct mk_context *next, **prev, *parent;
43 unsigned d_cur, d_max;
46 typedef struct mk_context mk_context;
52 unsigned duration_ptr;
54 mk_context *root, *cluster, *frame;
60 int64_t cluster_tc_scaled;
61 int64_t frame_tc, max_frame_tc;
63 char wrote_header, in_frame, keyframe, skippable;
66 static mk_context *mk_create_context( mk_writer *w, mk_context *parent, unsigned id )
73 w->freelist = w->freelist->next;
77 c = malloc( sizeof(*c) );
80 memset( c, 0, sizeof(*c) );
87 if( c->owner->actlist )
88 c->owner->actlist->prev = &c->next;
89 c->next = c->owner->actlist;
90 c->prev = &c->owner->actlist;
91 c->owner->actlist = c;
96 static int mk_append_context_data( mk_context *c, const void *data, unsigned size )
98 unsigned ns = c->d_cur + size;
103 unsigned dn = c->d_max ? c->d_max << 1 : 16;
107 dp = realloc( c->data, dn );
115 memcpy( (char*)c->data + c->d_cur, data, size );
122 static int mk_write_id( mk_context *c, unsigned id )
124 unsigned char c_id[4] = { id >> 24, id >> 16, id >> 8, id };
127 return mk_append_context_data( c, c_id, 4 );
129 return mk_append_context_data( c, c_id+1, 3 );
131 return mk_append_context_data( c, c_id+2, 2 );
132 return mk_append_context_data( c, c_id+3, 1 );
135 static int mk_write_size( mk_context *c, unsigned size )
137 unsigned char c_size[5] = { 0x08, size >> 24, size >> 16, size >> 8, size };
142 return mk_append_context_data( c, c_size+4, 1 );
147 return mk_append_context_data( c, c_size+3, 2 );
149 if( size < 0x1fffff )
152 return mk_append_context_data( c, c_size+2, 3 );
154 if( size < 0x0fffffff )
157 return mk_append_context_data( c, c_size+1, 4 );
159 return mk_append_context_data( c, c_size, 5 );
162 static int mk_flush_context_id( mk_context *c )
164 unsigned char ff = 0xff;
169 CHECK( mk_write_id( c->parent, c->id ) );
170 CHECK( mk_append_context_data( c->parent, &ff, 1 ) );
177 static int mk_flush_context_data( mk_context *c )
183 CHECK( mk_append_context_data( c->parent, c->data, c->d_cur ) );
184 else if( fwrite( c->data, c->d_cur, 1, c->owner->fp ) != 1 )
192 static int mk_close_context( mk_context *c, unsigned *off )
196 CHECK( mk_write_id( c->parent, c->id ) );
197 CHECK( mk_write_size( c->parent, c->d_cur ) );
200 if( c->parent && off )
201 *off += c->parent->d_cur;
203 CHECK( mk_flush_context_data( c ) );
206 c->next->prev = c->prev;
207 *(c->prev) = c->next;
208 c->next = c->owner->freelist;
209 c->owner->freelist = c;
214 static void mk_destroy_contexts( mk_writer *w )
218 for( mk_context *cur = w->freelist; cur; cur = next )
225 for( mk_context *cur = w->actlist; cur; cur = next )
232 w->freelist = w->actlist = w->root = NULL;
235 static int mk_write_string( mk_context *c, unsigned id, const char *str )
237 size_t len = strlen( str );
239 CHECK( mk_write_id( c, id ) );
240 CHECK( mk_write_size( c, len ) );
241 CHECK( mk_append_context_data( c, str, len ) );
245 static int mk_write_bin( mk_context *c, unsigned id, const void *data, unsigned size )
247 CHECK( mk_write_id( c, id ) );
248 CHECK( mk_write_size( c, size ) );
249 CHECK( mk_append_context_data( c, data, size ) ) ;
253 static int mk_write_uint( mk_context *c, unsigned id, int64_t ui )
255 unsigned char c_ui[8] = { ui >> 56, ui >> 48, ui >> 40, ui >> 32, ui >> 24, ui >> 16, ui >> 8, ui };
258 CHECK( mk_write_id( c, id ) );
259 while( i < 7 && !c_ui[i] )
261 CHECK( mk_write_size( c, 8 - i ) );
262 CHECK( mk_append_context_data( c, c_ui+i, 8 - i ) );
266 static int mk_write_float_raw( mk_context *c, float f )
273 unsigned char c_f[4];
281 return mk_append_context_data( c, c_f, 4 );
284 static int mk_write_float( mk_context *c, unsigned id, float f )
286 CHECK( mk_write_id( c, id ) );
287 CHECK( mk_write_size( c, 4 ) );
288 CHECK( mk_write_float_raw( c, f ) );
292 mk_writer *mk_create_writer( const char *filename )
294 mk_writer *w = malloc( sizeof(*w) );
298 memset( w, 0, sizeof(*w) );
300 w->root = mk_create_context( w, NULL, 0 );
307 if( !strcmp( filename, "-" ) )
310 w->fp = fopen( filename, "wb" );
313 mk_destroy_contexts( w );
318 w->timescale = 1000000;
323 int mk_write_header( mk_writer *w, const char *writing_app,
324 const char *codec_id,
325 const void *codec_private, unsigned codec_private_size,
326 int64_t default_frame_duration,
328 unsigned width, unsigned height,
329 unsigned d_width, unsigned d_height, int display_size_units )
331 mk_context *c, *ti, *v;
333 if( w->wrote_header )
336 w->timescale = timescale;
337 w->def_duration = default_frame_duration;
339 if( !(c = mk_create_context( w, w->root, 0x1a45dfa3 )) ) // EBML
341 CHECK( mk_write_uint( c, 0x4286, 1 ) ); // EBMLVersion
342 CHECK( mk_write_uint( c, 0x42f7, 1 ) ); // EBMLReadVersion
343 CHECK( mk_write_uint( c, 0x42f2, 4 ) ); // EBMLMaxIDLength
344 CHECK( mk_write_uint( c, 0x42f3, 8 ) ); // EBMLMaxSizeLength
345 CHECK( mk_write_string( c, 0x4282, "matroska") ); // DocType
346 CHECK( mk_write_uint( c, 0x4287, 2 ) ); // DocTypeVersion
347 CHECK( mk_write_uint( c, 0x4285, 2 ) ); // DocTypeReadversion
348 CHECK( mk_close_context( c, 0 ) );
350 if( !(c = mk_create_context( w, w->root, 0x18538067 )) ) // Segment
352 CHECK( mk_flush_context_id( c ) );
353 CHECK( mk_close_context( c, 0 ) );
355 if( !(c = mk_create_context( w, w->root, 0x1549a966 )) ) // SegmentInfo
357 CHECK( mk_write_string( c, 0x4d80, "Haali Matroska Writer b0" ) );
358 CHECK( mk_write_string( c, 0x5741, writing_app ) );
359 CHECK( mk_write_uint( c, 0x2ad7b1, w->timescale ) );
360 CHECK( mk_write_float( c, 0x4489, 0) );
361 w->duration_ptr = c->d_cur - 4;
362 CHECK( mk_close_context( c, &w->duration_ptr ) );
364 if( !(c = mk_create_context( w, w->root, 0x1654ae6b )) ) // tracks
366 if( !(ti = mk_create_context( w, c, 0xae )) ) // TrackEntry
368 CHECK( mk_write_uint( ti, 0xd7, 1 ) ); // TrackNumber
369 CHECK( mk_write_uint( ti, 0x73c5, 1 ) ); // TrackUID
370 CHECK( mk_write_uint( ti, 0x83, 1 ) ); // TrackType
371 CHECK( mk_write_uint( ti, 0x9c, 0 ) ); // FlagLacing
372 CHECK( mk_write_string( ti, 0x86, codec_id ) ); // codec_id
373 if( codec_private_size )
374 CHECK( mk_write_bin( ti, 0x63a2, codec_private, codec_private_size ) ); // codec_private
375 if( default_frame_duration )
376 CHECK( mk_write_uint( ti, 0x23e383, default_frame_duration ) ); // DefaultDuration
378 if( !(v = mk_create_context( w, ti, 0xe0 ) ) ) // Video
380 CHECK( mk_write_uint( v, 0xb0, width ) );
381 CHECK( mk_write_uint( v, 0xba, height ) );
382 CHECK( mk_write_uint( v, 0x54b2, display_size_units ) );
383 CHECK( mk_write_uint( v, 0x54b0, d_width ) );
384 CHECK( mk_write_uint( v, 0x54ba, d_height ) );
385 CHECK( mk_close_context( v, 0 ) );
387 CHECK( mk_close_context( ti, 0 ) );
389 CHECK( mk_close_context( c, 0 ) );
391 CHECK( mk_flush_context_data( w->root ) );
398 static int mk_close_cluster( mk_writer *w )
400 if( w->cluster == NULL )
402 CHECK( mk_close_context( w->cluster, 0 ) );
404 CHECK( mk_flush_context_data( w->root ) );
408 static int mk_flush_frame( mk_writer *w )
412 unsigned char c_delta_flags[3];
417 delta = w->frame_tc/w->timescale - w->cluster_tc_scaled;
418 if( delta > 32767ll || delta < -32768ll )
419 CHECK( mk_close_cluster( w ) );
423 w->cluster_tc_scaled = w->frame_tc / w->timescale;
424 w->cluster = mk_create_context( w, w->root, 0x1f43b675 ); // Cluster
428 CHECK( mk_write_uint( w->cluster, 0xe7, w->cluster_tc_scaled ) ); // Timecode
433 fsize = w->frame ? w->frame->d_cur : 0;
435 CHECK( mk_write_id( w->cluster, 0xa3 ) ); // SimpleBlock
436 CHECK( mk_write_size( w->cluster, fsize + 4 ) );
437 CHECK( mk_write_size( w->cluster, 1 ) ); // track number
439 c_delta_flags[0] = delta >> 8;
440 c_delta_flags[1] = delta;
441 c_delta_flags[2] = (w->keyframe << 7) | w->skippable;
442 CHECK( mk_append_context_data( w->cluster, c_delta_flags, 3 ) );
445 CHECK( mk_append_context_data( w->cluster, w->frame->data, w->frame->d_cur ) );
451 if( w->cluster->d_cur > CLSIZE )
452 CHECK( mk_close_cluster( w ) );
457 int mk_start_frame( mk_writer *w )
459 if( mk_flush_frame( w ) < 0 )
469 int mk_set_frame_flags( mk_writer *w, int64_t timestamp, int keyframe, int skippable )
474 w->frame_tc = timestamp;
475 w->keyframe = keyframe != 0;
476 w->skippable = skippable != 0;
478 if( w->max_frame_tc < timestamp )
479 w->max_frame_tc = timestamp;
484 int mk_add_frame_data( mk_writer *w, const void *data, unsigned size )
490 if( !(w->frame = mk_create_context( w, NULL, 0 )) )
493 return mk_append_context_data( w->frame, data, size );
496 int mk_close( mk_writer *w, int64_t last_delta )
499 if( mk_flush_frame( w ) < 0 || mk_close_cluster( w ) < 0 )
501 if( w->wrote_header && x264_is_regular_file( w->fp ) )
503 fseek( w->fp, w->duration_ptr, SEEK_SET );
504 int64_t last_frametime = w->def_duration ? w->def_duration : last_delta;
505 int64_t total_duration = w->max_frame_tc+last_frametime;
506 if( mk_write_float_raw( w->root, (float)((double)total_duration / w->timescale) ) < 0 ||
507 mk_flush_context_data( w->root ) < 0 )
510 mk_destroy_contexts( w );