1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 Mike Matsnev
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *****************************************************************************/
22 #include "matroska_ebml.h"
24 #define CLSIZE 1048576
33 struct mk_context *next, **prev, *parent;
38 unsigned d_cur, d_max;
41 typedef struct mk_context mk_context;
47 unsigned duration_ptr;
49 mk_context *root, *cluster, *frame;
55 int64_t cluster_tc_scaled;
56 int64_t frame_tc, max_frame_tc;
58 char wrote_header, in_frame, keyframe, skippable;
61 static mk_context *mk_create_context( mk_writer *w, mk_context *parent, unsigned id )
68 w->freelist = w->freelist->next;
72 c = malloc( sizeof(*c) );
75 memset( c, 0, sizeof(*c) );
82 if( c->owner->actlist )
83 c->owner->actlist->prev = &c->next;
84 c->next = c->owner->actlist;
85 c->prev = &c->owner->actlist;
86 c->owner->actlist = c;
91 static int mk_append_context_data( mk_context *c, const void *data, unsigned size )
93 unsigned ns = c->d_cur + size;
98 unsigned dn = c->d_max ? c->d_max << 1 : 16;
102 dp = realloc( c->data, dn );
110 memcpy( (char*)c->data + c->d_cur, data, size );
117 static int mk_write_id( mk_context *c, unsigned id )
119 unsigned char c_id[4] = { id >> 24, id >> 16, id >> 8, id };
122 return mk_append_context_data( c, c_id, 4 );
124 return mk_append_context_data( c, c_id+1, 3 );
126 return mk_append_context_data( c, c_id+2, 2 );
127 return mk_append_context_data( c, c_id+3, 1 );
130 static int mk_write_size( mk_context *c, unsigned size )
132 unsigned char c_size[5] = { 0x08, size >> 24, size >> 16, size >> 8, size };
137 return mk_append_context_data( c, c_size+4, 1 );
142 return mk_append_context_data( c, c_size+3, 2 );
144 if( size < 0x1fffff )
147 return mk_append_context_data( c, c_size+2, 3 );
149 if( size < 0x0fffffff )
152 return mk_append_context_data( c, c_size+1, 4 );
154 return mk_append_context_data( c, c_size, 5 );
157 static int mk_flush_context_id( mk_context *c )
159 unsigned char ff = 0xff;
164 CHECK( mk_write_id( c->parent, c->id ) );
165 CHECK( mk_append_context_data( c->parent, &ff, 1 ) );
172 static int mk_flush_context_data( mk_context *c )
178 CHECK( mk_append_context_data( c->parent, c->data, c->d_cur ) );
179 else if( fwrite( c->data, c->d_cur, 1, c->owner->fp ) != 1 )
187 static int mk_close_context( mk_context *c, unsigned *off )
191 CHECK( mk_write_id( c->parent, c->id ) );
192 CHECK( mk_write_size( c->parent, c->d_cur ) );
195 if( c->parent && off )
196 *off += c->parent->d_cur;
198 CHECK( mk_flush_context_data( c ) );
201 c->next->prev = c->prev;
202 *(c->prev) = c->next;
203 c->next = c->owner->freelist;
204 c->owner->freelist = c;
209 static void mk_destroy_contexts( mk_writer *w )
213 for( mk_context *cur = w->freelist; cur; cur = next )
220 for( mk_context *cur = w->actlist; cur; cur = next )
227 w->freelist = w->actlist = w->root = NULL;
230 static int mk_write_string( mk_context *c, unsigned id, const char *str )
232 size_t len = strlen( str );
234 CHECK( mk_write_id( c, id ) );
235 CHECK( mk_write_size( c, len ) );
236 CHECK( mk_append_context_data( c, str, len ) );
240 static int mk_write_bin( mk_context *c, unsigned id, const void *data, unsigned size )
242 CHECK( mk_write_id( c, id ) );
243 CHECK( mk_write_size( c, size ) );
244 CHECK( mk_append_context_data( c, data, size ) ) ;
248 static int mk_write_uint( mk_context *c, unsigned id, int64_t ui )
250 unsigned char c_ui[8] = { ui >> 56, ui >> 48, ui >> 40, ui >> 32, ui >> 24, ui >> 16, ui >> 8, ui };
253 CHECK( mk_write_id( c, id ) );
254 while( i < 7 && !c_ui[i] )
256 CHECK( mk_write_size( c, 8 - i ) );
257 CHECK( mk_append_context_data( c, c_ui+i, 8 - i ) );
261 static int mk_write_float_raw( mk_context *c, float f )
268 unsigned char c_f[4];
276 return mk_append_context_data( c, c_f, 4 );
279 static int mk_write_float( mk_context *c, unsigned id, float f )
281 CHECK( mk_write_id( c, id ) );
282 CHECK( mk_write_size( c, 4 ) );
283 CHECK( mk_write_float_raw( c, f ) );
287 mk_writer *mk_create_writer( const char *filename )
289 mk_writer *w = malloc( sizeof(*w) );
293 memset( w, 0, sizeof(*w) );
295 w->root = mk_create_context( w, NULL, 0 );
302 if( !strcmp( filename, "-" ) )
305 w->fp = fopen( filename, "wb" );
308 mk_destroy_contexts( w );
313 w->timescale = 1000000;
318 int mk_writeHeader( mk_writer *w, const char *writing_app,
319 const char *codec_id,
320 const void *codec_private, unsigned codec_private_size,
321 int64_t default_frame_duration,
323 unsigned width, unsigned height,
324 unsigned d_width, unsigned d_height )
326 mk_context *c, *ti, *v;
328 if( w->wrote_header )
331 w->timescale = timescale;
332 w->def_duration = default_frame_duration;
334 if( !(c = mk_create_context( w, w->root, 0x1a45dfa3 )) ) // EBML
336 CHECK( mk_write_uint( c, 0x4286, 1 ) ); // EBMLVersion
337 CHECK( mk_write_uint( c, 0x42f7, 1 ) ); // EBMLReadVersion
338 CHECK( mk_write_uint( c, 0x42f2, 4 ) ); // EBMLMaxIDLength
339 CHECK( mk_write_uint( c, 0x42f3, 8 ) ); // EBMLMaxSizeLength
340 CHECK( mk_write_string( c, 0x4282, "matroska") ); // DocType
341 CHECK( mk_write_uint( c, 0x4287, 2 ) ); // DocTypeVersion
342 CHECK( mk_write_uint( c, 0x4285, 2 ) ); // DocTypeReadversion
343 CHECK( mk_close_context( c, 0 ) );
345 if( !(c = mk_create_context( w, w->root, 0x18538067 )) ) // Segment
347 CHECK( mk_flush_context_id( c ) );
348 CHECK( mk_close_context( c, 0 ) );
350 if( !(c = mk_create_context( w, w->root, 0x1549a966 )) ) // SegmentInfo
352 CHECK( mk_write_string( c, 0x4d80, "Haali Matroska Writer b0" ) );
353 CHECK( mk_write_string( c, 0x5741, writing_app ) );
354 CHECK( mk_write_uint( c, 0x2ad7b1, w->timescale ) );
355 CHECK( mk_write_float( c, 0x4489, 0) );
356 w->duration_ptr = c->d_cur - 4;
357 CHECK( mk_close_context( c, &w->duration_ptr ) );
359 if( !(c = mk_create_context( w, w->root, 0x1654ae6b )) ) // tracks
361 if( !(ti = mk_create_context( w, c, 0xae )) ) // TrackEntry
363 CHECK( mk_write_uint( ti, 0xd7, 1 ) ); // TrackNumber
364 CHECK( mk_write_uint( ti, 0x73c5, 1 ) ); // TrackUID
365 CHECK( mk_write_uint( ti, 0x83, 1 ) ); // TrackType
366 CHECK( mk_write_uint( ti, 0x9c, 0 ) ); // FlagLacing
367 CHECK( mk_write_string( ti, 0x86, codec_id ) ); // codec_id
368 if( codec_private_size )
369 CHECK( mk_write_bin( ti, 0x63a2, codec_private, codec_private_size ) ); // codec_private
370 if( default_frame_duration )
371 CHECK( mk_write_uint( ti, 0x23e383, default_frame_duration ) ); // DefaultDuration
373 if( !(v = mk_create_context( w, ti, 0xe0 ) ) ) // Video
375 CHECK( mk_write_uint( v, 0xb0, width ) );
376 CHECK( mk_write_uint( v, 0xba, height ) );
377 CHECK( mk_write_uint( v, 0x54b0, d_width ) );
378 CHECK( mk_write_uint( v, 0x54ba, d_height ) );
379 CHECK( mk_close_context( v, 0 ) );
381 CHECK( mk_close_context( ti, 0 ) );
383 CHECK( mk_close_context( c, 0 ) );
385 CHECK( mk_flush_context_data( w->root ) );
392 static int mk_close_cluster( mk_writer *w )
394 if( w->cluster == NULL )
396 CHECK( mk_close_context( w->cluster, 0 ) );
398 CHECK( mk_flush_context_data( w->root ) );
402 static int mk_flush_frame( mk_writer *w )
406 unsigned char c_delta_flags[3];
411 delta = w->frame_tc/w->timescale - w->cluster_tc_scaled;
412 if( delta > 32767ll || delta < -32768ll )
413 CHECK( mk_close_cluster( w ) );
417 w->cluster_tc_scaled = w->frame_tc / w->timescale;
418 w->cluster = mk_create_context( w, w->root, 0x1f43b675 ); // Cluster
422 CHECK( mk_write_uint( w->cluster, 0xe7, w->cluster_tc_scaled ) ); // Timecode
427 fsize = w->frame ? w->frame->d_cur : 0;
429 CHECK( mk_write_id( w->cluster, 0xa3 ) ); // SimpleBlock
430 CHECK( mk_write_size( w->cluster, fsize + 4 ) );
431 CHECK( mk_write_size( w->cluster, 1 ) ); // track number
433 c_delta_flags[0] = delta >> 8;
434 c_delta_flags[1] = delta;
435 c_delta_flags[2] = (w->keyframe << 7) | w->skippable;
436 CHECK( mk_append_context_data( w->cluster, c_delta_flags, 3 ) );
439 CHECK( mk_append_context_data( w->cluster, w->frame->data, w->frame->d_cur ) );
445 if( w->cluster->d_cur > CLSIZE )
446 CHECK( mk_close_cluster( w ) );
451 int mk_start_frame( mk_writer *w )
453 if( mk_flush_frame( w ) < 0 )
463 int mk_set_frame_flags( mk_writer *w, int64_t timestamp, int keyframe, int skippable )
468 w->frame_tc = timestamp;
469 w->keyframe = keyframe != 0;
470 w->skippable = skippable != 0;
472 if( w->max_frame_tc < timestamp )
473 w->max_frame_tc = timestamp;
478 int mk_add_frame_data( mk_writer *w, const void *data, unsigned size )
484 if( !(w->frame = mk_create_context( w, NULL, 0 )) )
487 return mk_append_context_data( w->frame, data, size );
490 int mk_close( mk_writer *w, int64_t last_delta )
493 if( mk_flush_frame( w ) < 0 || mk_close_cluster( w ) < 0 )
495 if( w->wrote_header && x264_is_regular_file( w->fp ) )
497 fseek( w->fp, w->duration_ptr, SEEK_SET );
498 int64_t last_frametime = w->def_duration ? w->def_duration : last_delta;
499 int64_t total_duration = w->max_frame_tc+last_frametime;
500 if( mk_write_float_raw( w->root, (float)((double)total_duration / w->timescale) ) < 0 ||
501 mk_flush_context_data( w->root ) < 0 )
504 mk_destroy_contexts( w );