The inconsistent syntax in these files has finally come to an end.
#include "common/osdep.h"
#include "matroska.h"
-#define CLSIZE 1048576
-#define CHECK(x) do { if ((x) < 0) return -1; } while (0)
-
-struct mk_Context {
- struct mk_Context *next, **prev, *parent;
- struct mk_Writer *owner;
- unsigned id;
+#define CLSIZE 1048576
+#define CHECK(x)\
+do\
+{\
+ if( (x) < 0 ) return -1;\
+}\
+while( 0 )
+
+struct mk_context
+{
+ struct mk_context *next, **prev, *parent;
+ struct mk_writer *owner;
+ unsigned id;
- void *data;
- unsigned d_cur, d_max;
+ void *data;
+ unsigned d_cur, d_max;
};
-typedef struct mk_Context mk_Context;
+typedef struct mk_context mk_context;
-struct mk_Writer {
- FILE *fp;
+struct mk_writer
+{
+ FILE *fp;
- unsigned duration_ptr;
+ unsigned duration_ptr;
- mk_Context *root, *cluster, *frame;
- mk_Context *freelist;
- mk_Context *actlist;
+ mk_context *root, *cluster, *frame;
+ mk_context *freelist;
+ mk_context *actlist;
- int64_t def_duration;
- int64_t timescale;
- int64_t cluster_tc_scaled;
- int64_t frame_tc, prev_frame_tc_scaled, max_frame_tc;
+ int64_t def_duration;
+ int64_t timescale;
+ int64_t cluster_tc_scaled;
+ int64_t frame_tc, prev_frame_tc_scaled, max_frame_tc;
- char wrote_header, in_frame, keyframe;
+ char wrote_header, in_frame, keyframe;
};
-static mk_Context *mk_createContext(mk_Writer *w, mk_Context *parent, unsigned id) {
- mk_Context *c;
-
- if (w->freelist) {
- c = w->freelist;
- w->freelist = w->freelist->next;
- } else {
- c = malloc(sizeof(*c));
- if (c == NULL)
- return NULL;
- memset(c, 0, sizeof(*c));
- }
-
- c->parent = parent;
- c->owner = w;
- c->id = id;
-
- if (c->owner->actlist)
- c->owner->actlist->prev = &c->next;
- c->next = c->owner->actlist;
- c->prev = &c->owner->actlist;
- c->owner->actlist = c;
-
- return c;
+static mk_context *mk_create_context( mk_writer *w, mk_context *parent, unsigned id )
+{
+ mk_context *c;
+
+ if( w->freelist )
+ {
+ c = w->freelist;
+ w->freelist = w->freelist->next;
+ }
+ else
+ {
+ c = malloc( sizeof(*c) );
+ if( !c )
+ return NULL;
+ memset( c, 0, sizeof(*c) );
+ }
+
+ c->parent = parent;
+ c->owner = w;
+ c->id = id;
+
+ if( c->owner->actlist )
+ c->owner->actlist->prev = &c->next;
+ c->next = c->owner->actlist;
+ c->prev = &c->owner->actlist;
+ c->owner->actlist = c;
+
+ return c;
}
-static int mk_appendContextData(mk_Context *c, const void *data, unsigned size) {
- unsigned ns = c->d_cur + size;
+static int mk_append_context_data( mk_context *c, const void *data, unsigned size )
+{
+ unsigned ns = c->d_cur + size;
- if (ns > c->d_max) {
- void *dp;
- unsigned dn = c->d_max ? c->d_max << 1 : 16;
- while (ns > dn)
- dn <<= 1;
+ if( ns > c->d_max )
+ {
+ void *dp;
+ unsigned dn = c->d_max ? c->d_max << 1 : 16;
+ while( ns > dn )
+ dn <<= 1;
- dp = realloc(c->data, dn);
- if (dp == NULL)
- return -1;
+ dp = realloc( c->data, dn );
+ if( !dp )
+ return -1;
- c->data = dp;
- c->d_max = dn;
- }
+ c->data = dp;
+ c->d_max = dn;
+ }
- memcpy((char*)c->data + c->d_cur, data, size);
+ memcpy( (char*)c->data + c->d_cur, data, size );
- c->d_cur = ns;
+ c->d_cur = ns;
- return 0;
+ return 0;
}
-static int mk_writeID(mk_Context *c, unsigned id) {
- unsigned char c_id[4] = { id >> 24, id >> 16, id >> 8, id };
-
- if (c_id[0])
- return mk_appendContextData(c, c_id, 4);
- if (c_id[1])
- return mk_appendContextData(c, c_id+1, 3);
- if (c_id[2])
- return mk_appendContextData(c, c_id+2, 2);
- return mk_appendContextData(c, c_id+3, 1);
+static int mk_write_id( mk_context *c, unsigned id )
+{
+ unsigned char c_id[4] = { id >> 24, id >> 16, id >> 8, id };
+
+ if( c_id[0] )
+ return mk_append_context_data( c, c_id, 4 );
+ if( c_id[1] )
+ return mk_append_context_data( c, c_id+1, 3 );
+ if( c_id[2] )
+ return mk_append_context_data( c, c_id+2, 2 );
+ return mk_append_context_data( c, c_id+3, 1 );
}
-static int mk_writeSize(mk_Context *c, unsigned size) {
- unsigned char c_size[5] = { 0x08, size >> 24, size >> 16, size >> 8, size };
-
- if (size < 0x7f) {
- c_size[4] |= 0x80;
- return mk_appendContextData(c, c_size+4, 1);
- }
- if (size < 0x3fff) {
- c_size[3] |= 0x40;
- return mk_appendContextData(c, c_size+3, 2);
- }
- if (size < 0x1fffff) {
- c_size[2] |= 0x20;
- return mk_appendContextData(c, c_size+2, 3);
- }
- if (size < 0x0fffffff) {
- c_size[1] |= 0x10;
- return mk_appendContextData(c, c_size+1, 4);
- }
- return mk_appendContextData(c, c_size, 5);
+static int mk_write_size( mk_context *c, unsigned size )
+{
+ unsigned char c_size[5] = { 0x08, size >> 24, size >> 16, size >> 8, size };
+
+ if( size < 0x7f )
+ {
+ c_size[4] |= 0x80;
+ return mk_append_context_data( c, c_size+4, 1 );
+ }
+ if( size < 0x3fff )
+ {
+ c_size[3] |= 0x40;
+ return mk_append_context_data( c, c_size+3, 2 );
+ }
+ if( size < 0x1fffff )
+ {
+ c_size[2] |= 0x20;
+ return mk_append_context_data( c, c_size+2, 3 );
+ }
+ if( size < 0x0fffffff )
+ {
+ c_size[1] |= 0x10;
+ return mk_append_context_data( c, c_size+1, 4 );
+ }
+ return mk_append_context_data( c, c_size, 5 );
}
-static int mk_flushContextID(mk_Context *c) {
- unsigned char ff = 0xff;
+static int mk_flush_context_id( mk_context *c )
+{
+ unsigned char ff = 0xff;
- if (c->id == 0)
- return 0;
+ if( !c->id )
+ return 0;
- CHECK(mk_writeID(c->parent, c->id));
- CHECK(mk_appendContextData(c->parent, &ff, 1));
+ CHECK( mk_write_id( c->parent, c->id ) );
+ CHECK( mk_append_context_data( c->parent, &ff, 1 ) );
- c->id = 0;
+ c->id = 0;
- return 0;
+ return 0;
}
-static int mk_flushContextData(mk_Context *c) {
- if (c->d_cur == 0)
- return 0;
+static int mk_flush_context_data(mk_context *c)
+{
+ if( !c->d_cur )
+ return 0;
- if (c->parent)
- CHECK(mk_appendContextData(c->parent, c->data, c->d_cur));
- else
- if (fwrite(c->data, c->d_cur, 1, c->owner->fp) != 1)
- return -1;
+ if( c->parent )
+ CHECK( mk_append_context_data( c->parent, c->data, c->d_cur ) );
+ else if( fwrite( c->data, c->d_cur, 1, c->owner->fp ) != 1 )
+ return -1;
- c->d_cur = 0;
+ c->d_cur = 0;
- return 0;
+ return 0;
}
-static int mk_closeContext(mk_Context *c, unsigned *off) {
- if (c->id) {
- CHECK(mk_writeID(c->parent, c->id));
- CHECK(mk_writeSize(c->parent, c->d_cur));
- }
+static int mk_close_context(mk_context *c, unsigned *off)
+{
+ if( c->id )
+ {
+ CHECK( mk_write_id( c->parent, c->id ) );
+ CHECK( mk_write_size( c->parent, c->d_cur ) );
+ }
- if (c->parent && off != NULL)
- *off += c->parent->d_cur;
+ if( c->parent && off )
+ *off += c->parent->d_cur;
- CHECK(mk_flushContextData(c));
+ CHECK( mk_flush_context_data( c ) );
- if (c->next)
- c->next->prev = c->prev;
- *(c->prev) = c->next;
- c->next = c->owner->freelist;
- c->owner->freelist = c;
+ if( c->next )
+ c->next->prev = c->prev;
+ *(c->prev) = c->next;
+ c->next = c->owner->freelist;
+ c->owner->freelist = c;
- return 0;
+ return 0;
}
-static void mk_destroyContexts(mk_Writer *w) {
- mk_Context *cur, *next;
-
- for (cur = w->freelist; cur; cur = next) {
- next = cur->next;
- free(cur->data);
- free(cur);
- }
-
- for (cur = w->actlist; cur; cur = next) {
- next = cur->next;
- free(cur->data);
- free(cur);
- }
-
- w->freelist = w->actlist = w->root = NULL;
+static void mk_destroy_contexts( mk_writer *w )
+{
+ mk_context *cur, *next;
+
+ for( cur = w->freelist; cur; cur = next )
+ {
+ next = cur->next;
+ free( cur->data );
+ free( cur );
+ }
+
+ for( cur = w->actlist; cur; cur = next )
+ {
+ next = cur->next;
+ free( cur->data );
+ free( cur );
+ }
+
+ w->freelist = w->actlist = w->root = NULL;
}
-static int mk_writeStr(mk_Context *c, unsigned id, const char *str) {
- size_t len = strlen(str);
+static int mk_write_string( mk_context *c, unsigned id, const char *str )
+{
+ size_t len = strlen( str );
- CHECK(mk_writeID(c, id));
- CHECK(mk_writeSize(c, len));
- CHECK(mk_appendContextData(c, str, len));
- return 0;
+ CHECK( mk_write_id( c, id ) );
+ CHECK( mk_write_size( c, len ) );
+ CHECK( mk_append_context_data( c, str, len ) );
+ return 0;
}
-static int mk_writeBin(mk_Context *c, unsigned id, const void *data, unsigned size) {
- CHECK(mk_writeID(c, id));
- CHECK(mk_writeSize(c, size));
- CHECK(mk_appendContextData(c, data, size));
- return 0;
+static int mk_write_bin( mk_context *c, unsigned id, const void *data, unsigned size )
+{
+ CHECK( mk_write_id( c, id ) );
+ CHECK( mk_write_size( c, size ) );
+ CHECK( mk_append_context_data( c, data, size ) ) ;
+ return 0;
}
-static int mk_writeUInt(mk_Context *c, unsigned id, int64_t ui) {
- unsigned char c_ui[8] = { ui >> 56, ui >> 48, ui >> 40, ui >> 32, ui >> 24, ui >> 16, ui >> 8, ui };
- unsigned i = 0;
-
- CHECK(mk_writeID(c, id));
- while (i < 7 && c_ui[i] == 0)
- ++i;
- CHECK(mk_writeSize(c, 8 - i));
- CHECK(mk_appendContextData(c, c_ui+i, 8 - i));
- return 0;
+static int mk_write_uint( mk_context *c, unsigned id, int64_t ui )
+{
+ unsigned char c_ui[8] = { ui >> 56, ui >> 48, ui >> 40, ui >> 32, ui >> 24, ui >> 16, ui >> 8, ui };
+ unsigned i = 0;
+
+ CHECK( mk_write_id( c, id ) );
+ while( i < 7 && !c_ui[i] )
+ ++i;
+ CHECK( mk_write_size( c, 8 - i ) );
+ CHECK( mk_append_context_data( c, c_ui+i, 8 - i ) );
+ return 0;
}
-static int mk_writeSInt(mk_Context *c, unsigned id, int64_t si) {
- unsigned char c_si[8] = { si >> 56, si >> 48, si >> 40, si >> 32, si >> 24, si >> 16, si >> 8, si };
- unsigned i = 0;
-
- CHECK(mk_writeID(c, id));
- if (si < 0)
- while (i < 7 && c_si[i] == 0xff && c_si[i+1] & 0x80)
- ++i;
- else
- while (i < 7 && c_si[i] == 0 && !(c_si[i+1] & 0x80))
- ++i;
- CHECK(mk_writeSize(c, 8 - i));
- CHECK(mk_appendContextData(c, c_si+i, 8 - i));
- return 0;
+static int mk_write_sint( mk_context *c, unsigned id, int64_t si )
+{
+ unsigned char c_si[8] = { si >> 56, si >> 48, si >> 40, si >> 32, si >> 24, si >> 16, si >> 8, si };
+ unsigned i = 0;
+
+ CHECK( mk_write_id( c, id ) );
+ if( si < 0 )
+ while( i < 7 && c_si[i] == 0xff && c_si[i+1] & 0x80 )
+ ++i;
+ else
+ while( i < 7 && c_si[i] == 0 && !(c_si[i+1] & 0x80 ) )
+ ++i;
+ CHECK( mk_write_size( c, 8 - i ) );
+ CHECK( mk_append_context_data( c, c_si+i, 8 - i ) );
+ return 0;
}
-static int mk_writeFloatRaw(mk_Context *c, float f) {
- union {
- float f;
- unsigned u;
- } u;
- unsigned char c_f[4];
-
- u.f = f;
- c_f[0] = u.u >> 24;
- c_f[1] = u.u >> 16;
- c_f[2] = u.u >> 8;
- c_f[3] = u.u;
-
- return mk_appendContextData(c, c_f, 4);
+static int mk_write_float_raw( mk_context *c, float f )
+{
+ union
+ {
+ float f;
+ unsigned u;
+ } u;
+ unsigned char c_f[4];
+
+ u.f = f;
+ c_f[0] = u.u >> 24;
+ c_f[1] = u.u >> 16;
+ c_f[2] = u.u >> 8;
+ c_f[3] = u.u;
+
+ return mk_append_context_data( c, c_f, 4 );
}
-static int mk_writeFloat(mk_Context *c, unsigned id, float f) {
- CHECK(mk_writeID(c, id));
- CHECK(mk_writeSize(c, 4));
- CHECK(mk_writeFloatRaw(c, f));
- return 0;
+static int mk_write_float( mk_context *c, unsigned id, float f )
+{
+ CHECK( mk_write_id( c, id ) );
+ CHECK( mk_write_size( c, 4 ) );
+ CHECK( mk_write_float_raw( c, f ) );
+ return 0;
}
-static unsigned mk_ebmlSizeSize(unsigned s) {
- if (s < 0x7f)
- return 1;
- if (s < 0x3fff)
- return 2;
- if (s < 0x1fffff)
- return 3;
- if (s < 0x0fffffff)
- return 4;
- return 5;
+static unsigned mk_ebml_size_size( unsigned s )
+{
+ if( s < 0x7f )
+ return 1;
+ if( s < 0x3fff )
+ return 2;
+ if( s < 0x1fffff )
+ return 3;
+ if( s < 0x0fffffff )
+ return 4;
+ return 5;
}
-static unsigned mk_ebmlSIntSize(int64_t si) {
- unsigned char c_si[8] = { si >> 56, si >> 48, si >> 40, si >> 32, si >> 24, si >> 16, si >> 8, si };
- unsigned i = 0;
+static unsigned mk_ebml_sint_size( int64_t si )
+{
+ unsigned char c_si[8] = { si >> 56, si >> 48, si >> 40, si >> 32, si >> 24, si >> 16, si >> 8, si };
+ unsigned i = 0;
- if (si < 0)
- while (i < 7 && c_si[i] == 0xff && c_si[i+1] & 0x80)
- ++i;
- else
- while (i < 7 && c_si[i] == 0 && !(c_si[i+1] & 0x80))
- ++i;
+ if( si < 0 )
+ while( i < 7 && c_si[i] == 0xff && c_si[i+1] & 0x80 )
+ ++i;
+ else
+ while( i < 7 && c_si[i] == 0 && !(c_si[i+1] & 0x80) )
+ ++i;
- return 8 - i;
+ return 8 - i;
}
-mk_Writer *mk_createWriter(const char *filename) {
- mk_Writer *w = malloc(sizeof(*w));
- if (w == NULL)
- return NULL;
-
- memset(w, 0, sizeof(*w));
-
- w->root = mk_createContext(w, NULL, 0);
- if (w->root == NULL) {
- free(w);
- return NULL;
- }
-
- w->fp = fopen(filename, "wb");
- if (w->fp == NULL) {
- mk_destroyContexts(w);
- free(w);
- return NULL;
- }
-
- w->timescale = 1000000;
-
- return w;
+mk_writer *mk_create_writer( const char *filename )
+{
+ mk_writer *w = malloc( sizeof(*w) );
+ if( !w )
+ return NULL;
+
+ memset( w, 0, sizeof(*w) );
+
+ w->root = mk_create_context( w, NULL, 0 );
+ if( !w->root )
+ {
+ free( w );
+ return NULL;
+ }
+
+ w->fp = fopen( filename, "wb" );
+ if( !w->fp )
+ {
+ mk_destroy_contexts( w );
+ free( w );
+ return NULL;
+ }
+
+ w->timescale = 1000000;
+
+ return w;
}
-int mk_writeHeader(mk_Writer *w, const char *writingApp,
- const char *codecID,
- const void *codecPrivate, unsigned codecPrivateSize,
- int64_t default_frame_duration,
- int64_t timescale,
- unsigned width, unsigned height,
- unsigned d_width, unsigned d_height)
+int mk_writeHeader( mk_writer *w, const char *writing_app,
+ const char *codec_id,
+ const void *codec_private, unsigned codec_private_size,
+ int64_t default_frame_duration,
+ int64_t timescale,
+ unsigned width, unsigned height,
+ unsigned d_width, unsigned d_height )
{
- mk_Context *c, *ti, *v;
-
- if (w->wrote_header)
- return -1;
-
- w->timescale = timescale;
- w->def_duration = default_frame_duration;
-
- if ((c = mk_createContext(w, w->root, 0x1a45dfa3)) == NULL) // EBML
- return -1;
- CHECK(mk_writeUInt(c, 0x4286, 1)); // EBMLVersion
- CHECK(mk_writeUInt(c, 0x42f7, 1)); // EBMLReadVersion
- CHECK(mk_writeUInt(c, 0x42f2, 4)); // EBMLMaxIDLength
- CHECK(mk_writeUInt(c, 0x42f3, 8)); // EBMLMaxSizeLength
- CHECK(mk_writeStr(c, 0x4282, "matroska")); // DocType
- CHECK(mk_writeUInt(c, 0x4287, 1)); // DocTypeVersion
- CHECK(mk_writeUInt(c, 0x4285, 1)); // DocTypeReadversion
- CHECK(mk_closeContext(c, 0));
-
- if ((c = mk_createContext(w, w->root, 0x18538067)) == NULL) // Segment
- return -1;
- CHECK(mk_flushContextID(c));
- CHECK(mk_closeContext(c, 0));
-
- if ((c = mk_createContext(w, w->root, 0x1549a966)) == NULL) // SegmentInfo
- return -1;
- CHECK(mk_writeStr(c, 0x4d80, "Haali Matroska Writer b0"));
- CHECK(mk_writeStr(c, 0x5741, writingApp));
- CHECK(mk_writeUInt(c, 0x2ad7b1, w->timescale));
- CHECK(mk_writeFloat(c, 0x4489, 0));
- w->duration_ptr = c->d_cur - 4;
- CHECK(mk_closeContext(c, &w->duration_ptr));
-
- if ((c = mk_createContext(w, w->root, 0x1654ae6b)) == NULL) // tracks
- return -1;
- if ((ti = mk_createContext(w, c, 0xae)) == NULL) // TrackEntry
- return -1;
- CHECK(mk_writeUInt(ti, 0xd7, 1)); // TrackNumber
- CHECK(mk_writeUInt(ti, 0x73c5, 1)); // TrackUID
- CHECK(mk_writeUInt(ti, 0x83, 1)); // TrackType
- CHECK(mk_writeUInt(ti, 0x9c, 0)); // FlagLacing
- CHECK(mk_writeStr(ti, 0x86, codecID)); // CodecID
- if (codecPrivateSize)
- CHECK(mk_writeBin(ti, 0x63a2, codecPrivate, codecPrivateSize)); // CodecPrivate
- if (default_frame_duration)
- CHECK(mk_writeUInt(ti, 0x23e383, default_frame_duration)); // DefaultDuration
-
- if ((v = mk_createContext(w, ti, 0xe0)) == NULL) // Video
- return -1;
- CHECK(mk_writeUInt(v, 0xb0, width));
- CHECK(mk_writeUInt(v, 0xba, height));
- CHECK(mk_writeUInt(v, 0x54b0, d_width));
- CHECK(mk_writeUInt(v, 0x54ba, d_height));
- CHECK(mk_closeContext(v, 0));
-
- CHECK(mk_closeContext(ti, 0));
-
- CHECK(mk_closeContext(c, 0));
-
- CHECK(mk_flushContextData(w->root));
-
- w->wrote_header = 1;
-
- return 0;
+ mk_context *c, *ti, *v;
+
+ if( w->wrote_header )
+ return -1;
+
+ w->timescale = timescale;
+ w->def_duration = default_frame_duration;
+
+ if( !(c = mk_create_context(w, w->root, 0x1a45dfa3)) ) // EBML
+ return -1;
+ CHECK( mk_write_uint( c, 0x4286, 1 ) ); // EBMLVersion
+ CHECK( mk_write_uint( c, 0x42f7, 1 ) ); // EBMLReadVersion
+ CHECK( mk_write_uint( c, 0x42f2, 4 ) ); // EBMLMaxIDLength
+ CHECK( mk_write_uint( c, 0x42f3, 8 ) ); // EBMLMaxSizeLength
+ CHECK( mk_write_string( c, 0x4282, "matroska") ); // DocType
+ CHECK( mk_write_uint( c, 0x4287, 1 ) ); // DocTypeVersion
+ CHECK( mk_write_uint( c, 0x4285, 1 ) ); // DocTypeReadversion
+ CHECK( mk_close_context( c, 0 ) );
+
+ if( !(c = mk_create_context( w, w->root, 0x18538067 )) ) // Segment
+ return -1;
+ CHECK( mk_flush_context_id( c ) );
+ CHECK( mk_close_context( c, 0 ) );
+
+ if( !(c = mk_create_context(w, w->root, 0x1549a966)) ) // SegmentInfo
+ return -1;
+ CHECK( mk_write_string( c, 0x4d80, "Haali Matroska Writer b0" ) );
+ CHECK( mk_write_string( c, 0x5741, writing_app ) );
+ CHECK( mk_write_uint( c, 0x2ad7b1, w->timescale ) );
+ CHECK( mk_write_float( c, 0x4489, 0) );
+ w->duration_ptr = c->d_cur - 4;
+ CHECK( mk_close_context( c, &w->duration_ptr ) );
+
+ if( !(c = mk_create_context( w, w->root, 0x1654ae6b )) ) // tracks
+ return -1;
+ if( !(ti = mk_create_context( w, c, 0xae )) ) // TrackEntry
+ return -1;
+ CHECK( mk_write_uint( ti, 0xd7, 1 ) ); // TrackNumber
+ CHECK( mk_write_uint( ti, 0x73c5, 1 ) ); // TrackUID
+ CHECK( mk_write_uint( ti, 0x83, 1 ) ); // TrackType
+ CHECK( mk_write_uint( ti, 0x9c, 0 ) ); // FlagLacing
+ CHECK( mk_write_string( ti, 0x86, codec_id ) ); // codec_id
+ if( codec_private_size )
+ CHECK( mk_write_bin( ti, 0x63a2, codec_private, codec_private_size ) ); // codec_private
+ if( default_frame_duration )
+ CHECK( mk_write_uint( ti, 0x23e383, default_frame_duration ) ); // DefaultDuration
+
+ if( !(v = mk_create_context( w, ti, 0xe0 ) ) ) // Video
+ return -1;
+ CHECK( mk_write_uint( v, 0xb0, width ) );
+ CHECK( mk_write_uint( v, 0xba, height ) );
+ CHECK( mk_write_uint( v, 0x54b0, d_width ) );
+ CHECK( mk_write_uint( v, 0x54ba, d_height ) );
+ CHECK( mk_close_context( v, 0 ) );
+
+ CHECK( mk_close_context( ti, 0 ) );
+
+ CHECK( mk_close_context( c, 0 ) );
+
+ CHECK( mk_flush_context_data( w->root ) );
+
+ w->wrote_header = 1;
+
+ return 0;
}
-static int mk_closeCluster(mk_Writer *w) {
- if (w->cluster == NULL)
+static int mk_close_cluster( mk_writer *w )
+{
+ if( w->cluster == NULL )
+ return 0;
+ CHECK( mk_close_context( w->cluster, 0 ) );
+ w->cluster = NULL;
+ CHECK( mk_flush_context_data( w->root ) );
return 0;
- CHECK(mk_closeContext(w->cluster, 0));
- w->cluster = NULL;
- CHECK(mk_flushContextData(w->root));
- return 0;
}
-static int mk_flushFrame(mk_Writer *w) {
- int64_t delta, ref = 0;
- unsigned fsize, bgsize;
- unsigned char c_delta_flags[3];
+static int mk_flush_frame( mk_writer *w )
+{
+ int64_t delta, ref = 0;
+ unsigned fsize, bgsize;
+ unsigned char c_delta_flags[3];
+
+ if( !w->in_frame )
+ return 0;
+
+ delta = w->frame_tc/w->timescale - w->cluster_tc_scaled;
+ if( delta > 32767ll || delta < -32768ll )
+ CHECK( mk_close_cluster( w ) );
+
+ if( !w->cluster )
+ {
+ w->cluster_tc_scaled = w->frame_tc / w->timescale;
+ w->cluster = mk_create_context( w, w->root, 0x1f43b675 ); // Cluster
+ if( !w->cluster )
+ return -1;
+
+ CHECK( mk_write_uint( w->cluster, 0xe7, w->cluster_tc_scaled ) ); // Timecode
+
+ delta = 0;
+ }
+
+ fsize = w->frame ? w->frame->d_cur : 0;
+ bgsize = fsize + 4 + mk_ebml_size_size( fsize + 4 ) + 1;
+ if( !w->keyframe )
+ {
+ ref = w->prev_frame_tc_scaled - w->cluster_tc_scaled - delta;
+ bgsize += 1 + 1 + mk_ebml_sint_size( ref );
+ }
+
+ CHECK( mk_write_id( w->cluster, 0xa0 ) ); // BlockGroup
+ CHECK( mk_write_size( w->cluster, bgsize ) );
+ CHECK( mk_write_id( w->cluster, 0xa1 ) ); // Block
+ CHECK( mk_write_size( w->cluster, fsize + 4 ) );
+ CHECK( mk_write_size( w->cluster, 1 ) ); // track number
+
+ c_delta_flags[0] = delta >> 8;
+ c_delta_flags[1] = delta;
+ c_delta_flags[2] = 0;
+ CHECK( mk_append_context_data( w->cluster, c_delta_flags, 3 ) );
+ if( w->frame )
+ {
+ CHECK( mk_append_context_data(w->cluster, w->frame->data, w->frame->d_cur));
+ w->frame->d_cur = 0;
+ }
+ if( !w->keyframe )
+ CHECK( mk_write_sint(w->cluster, 0xfb, ref ) ); // ReferenceBlock
+
+ w->in_frame = 0;
+ w->prev_frame_tc_scaled = w->cluster_tc_scaled + delta;
+
+ if( w->cluster->d_cur > CLSIZE )
+ CHECK( mk_close_cluster( w ) );
- if (!w->in_frame)
return 0;
-
- delta = w->frame_tc/w->timescale - w->cluster_tc_scaled;
- if (delta > 32767ll || delta < -32768ll)
- CHECK(mk_closeCluster(w));
-
- if (w->cluster == NULL) {
- w->cluster_tc_scaled = w->frame_tc / w->timescale;
- w->cluster = mk_createContext(w, w->root, 0x1f43b675); // Cluster
- if (w->cluster == NULL)
- return -1;
-
- CHECK(mk_writeUInt(w->cluster, 0xe7, w->cluster_tc_scaled)); // Timecode
-
- delta = 0;
- }
-
- fsize = w->frame ? w->frame->d_cur : 0;
- bgsize = fsize + 4 + mk_ebmlSizeSize(fsize + 4) + 1;
- if (!w->keyframe) {
- ref = w->prev_frame_tc_scaled - w->cluster_tc_scaled - delta;
- bgsize += 1 + 1 + mk_ebmlSIntSize(ref);
- }
-
- CHECK(mk_writeID(w->cluster, 0xa0)); // BlockGroup
- CHECK(mk_writeSize(w->cluster, bgsize));
- CHECK(mk_writeID(w->cluster, 0xa1)); // Block
- CHECK(mk_writeSize(w->cluster, fsize + 4));
- CHECK(mk_writeSize(w->cluster, 1)); // track number
-
- c_delta_flags[0] = delta >> 8;
- c_delta_flags[1] = delta;
- c_delta_flags[2] = 0;
- CHECK(mk_appendContextData(w->cluster, c_delta_flags, 3));
- if (w->frame) {
- CHECK(mk_appendContextData(w->cluster, w->frame->data, w->frame->d_cur));
- w->frame->d_cur = 0;
- }
- if (!w->keyframe)
- CHECK(mk_writeSInt(w->cluster, 0xfb, ref)); // ReferenceBlock
-
- w->in_frame = 0;
- w->prev_frame_tc_scaled = w->cluster_tc_scaled + delta;
-
- if (w->cluster->d_cur > CLSIZE)
- CHECK(mk_closeCluster(w));
-
- return 0;
}
-int mk_startFrame(mk_Writer *w) {
- if (mk_flushFrame(w) < 0)
- return -1;
+int mk_start_frame( mk_writer *w )
+{
+ if( mk_flush_frame( w ) < 0 )
+ return -1;
- w->in_frame = 1;
- w->keyframe = 0;
+ w->in_frame = 1;
+ w->keyframe = 0;
- return 0;
+ return 0;
}
-int mk_setFrameFlags(mk_Writer *w,int64_t timestamp, int keyframe) {
- if (!w->in_frame)
- return -1;
+int mk_set_frame_flags( mk_writer *w,int64_t timestamp, int keyframe )
+{
+ if( !w->in_frame )
+ return -1;
- w->frame_tc = timestamp;
- w->keyframe = keyframe != 0;
+ w->frame_tc = timestamp;
+ w->keyframe = keyframe != 0;
- if (w->max_frame_tc < timestamp)
- w->max_frame_tc = timestamp;
+ if( w->max_frame_tc < timestamp )
+ w->max_frame_tc = timestamp;
- return 0;
+ return 0;
}
-int mk_addFrameData(mk_Writer *w, const void *data, unsigned size) {
- if (!w->in_frame)
- return -1;
+int mk_add_frame_data( mk_writer *w, const void *data, unsigned size )
+{
+ if( !w->in_frame )
+ return -1;
- if (w->frame == NULL)
- if ((w->frame = mk_createContext(w, NULL, 0)) == NULL)
- return -1;
+ if( !w->frame )
+ if( !(w->frame = mk_create_context( w, NULL, 0 )) )
+ return -1;
- return mk_appendContextData(w->frame, data, size);
+ return mk_append_context_data( w->frame, data, size );
}
-int mk_close(mk_Writer *w) {
- int ret = 0;
- if (mk_flushFrame(w) < 0 || mk_closeCluster(w) < 0)
- ret = -1;
- if (w->wrote_header) {
- fseek(w->fp, w->duration_ptr, SEEK_SET);
- if (mk_writeFloatRaw(w->root, (float)((double)(w->max_frame_tc+w->def_duration) / w->timescale)) < 0 ||
- mk_flushContextData(w->root) < 0)
- ret = -1;
- }
- mk_destroyContexts(w);
- fclose(w->fp);
- free(w);
- return ret;
+int mk_close( mk_writer *w )
+{
+ int ret = 0;
+ if( mk_flush_frame( w ) < 0 || mk_close_cluster( w ) < 0 )
+ ret = -1;
+ if( w->wrote_header )
+ {
+ fseek( w->fp, w->duration_ptr, SEEK_SET );
+ if( mk_write_float_raw( w->root, (float)((double)(w->max_frame_tc+w->def_duration) / w->timescale) ) < 0 ||
+ mk_flush_context_data(w->root) < 0 )
+ ret = -1;
+ }
+ mk_destroy_contexts( w );
+ fclose( w->fp );
+ free( w );
+ return ret;
}
#ifndef X264_MATROSKA_H
#define X264_MATROSKA_H
-typedef struct mk_Writer mk_Writer;
+typedef struct mk_writer mk_writer;
-mk_Writer *mk_createWriter( const char *filename );
+mk_writer *mk_create_writer( const char *filename );
-int mk_writeHeader( mk_Writer *w, const char *writingApp,
- const char *codecID,
- const void *codecPrivate, unsigned codecPrivateSize,
- int64_t default_frame_duration,
- int64_t timescale,
- unsigned width, unsigned height,
- unsigned d_width, unsigned d_height );
+int mk_writeHeader( mk_writer *w, const char *writing_app,
+ const char *codec_id,
+ const void *codec_private, unsigned codec_private_size,
+ int64_t default_frame_duration,
+ int64_t timescale,
+ unsigned width, unsigned height,
+ unsigned d_width, unsigned d_height );
-int mk_startFrame( mk_Writer *w );
-int mk_addFrameData( mk_Writer *w, const void *data, unsigned size );
-int mk_setFrameFlags( mk_Writer *w, int64_t timestamp, int keyframe );
-int mk_close( mk_Writer *w );
+int mk_start_frame( mk_writer *w );
+int mk_add_frame_data( mk_writer *w, const void *data, unsigned size );
+int mk_set_frame_flags( mk_writer *w, int64_t timestamp, int keyframe );
+int mk_close( mk_writer *w );
#endif
static int64_t gcd( int64_t a, int64_t b )
{
- while (1)
+ while( 1 )
{
int64_t c = a % b;
if( !c )
}
}
-typedef struct {
+typedef struct
+{
FILE *fh;
int width, height;
int next_frame;
/* raw 420 yuv file operation */
int open_file_yuv( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
{
- yuv_input_t *h = malloc(sizeof(yuv_input_t));
+ yuv_input_t *h = malloc( sizeof(yuv_input_t) );
if( !h )
return -1;
h->width = p_param->i_width;
h->height = p_param->i_height;
h->next_frame = 0;
- if( !strcmp(psz_filename, "-") )
+ if( !strcmp( psz_filename, "-" ) )
h->fh = stdin;
else
- h->fh = fopen(psz_filename, "rb");
+ h->fh = fopen( psz_filename, "rb" );
if( h->fh == NULL )
return -1;
return -1;
if( fread( p_pic->img.plane[0], 1, h->width * h->height, h->fh ) <= 0
- || fread( p_pic->img.plane[1], 1, h->width * h->height / 4, h->fh ) <= 0
- || fread( p_pic->img.plane[2], 1, h->width * h->height / 4, h->fh ) <= 0 )
+ || fread( p_pic->img.plane[1], 1, h->width * h->height / 4, h->fh ) <= 0
+ || fread( p_pic->img.plane[2], 1, h->width * h->height / 4, h->fh ) <= 0 )
return -1;
h->next_frame = i_frame+1;
}
/* YUV4MPEG2 raw 420 yuv file operation */
-typedef struct {
+typedef struct
+{
FILE *fh;
int width, height;
int next_frame;
int i, n, d;
char header[MAX_YUV4_HEADER+10];
char *tokstart, *tokend, *header_end;
- y4m_input_t *h = malloc(sizeof(y4m_input_t));
+ y4m_input_t *h = malloc( sizeof(y4m_input_t) );
if( !h )
return -1;
h->next_frame = 0;
- if( !strcmp(psz_filename, "-") )
+ if( !strcmp( psz_filename, "-" ) )
h->fh = stdin;
else
h->fh = fopen(psz_filename, "rb");
h->frame_header_len = strlen(Y4M_FRAME_MAGIC)+1;
/* Read header */
- for( i=0; i<MAX_YUV4_HEADER; i++ )
+ for( i = 0; i < MAX_YUV4_HEADER; i++ )
{
header[i] = fgetc(h->fh);
if( header[i] == '\n' )
h->seq_header_len = i+1;
for( tokstart = &header[strlen(Y4M_MAGIC)+1]; tokstart < header_end; tokstart++ )
{
- if(*tokstart==0x20) continue;
+ if( *tokstart == 0x20 )
+ continue;
switch(*tokstart++)
{
- case 'W': /* Width. Required. */
- h->width = p_param->i_width = strtol(tokstart, &tokend, 10);
- tokstart=tokend;
- break;
- case 'H': /* Height. Required. */
- h->height = p_param->i_height = strtol(tokstart, &tokend, 10);
- tokstart=tokend;
- break;
- case 'C': /* Color space */
- if( strncmp("420", tokstart, 3) )
- {
- fprintf(stderr, "Colorspace unhandled\n");
- return -1;
- }
- tokstart = strchr(tokstart, 0x20);
- break;
- case 'I': /* Interlace type */
- switch(*tokstart++)
- {
- case 'p': break;
- case '?':
- case 't':
- case 'b':
- case 'm':
- default:
- fprintf(stderr, "Warning, this sequence might be interlaced\n");
- }
- break;
- case 'F': /* Frame rate - 0:0 if unknown */
- if( sscanf(tokstart, "%d:%d", &n, &d) == 2 && n && d )
- {
- x264_reduce_fraction( &n, &d );
- p_param->i_fps_num = n;
- p_param->i_fps_den = d;
- }
- tokstart = strchr(tokstart, 0x20);
- break;
- case 'A': /* Pixel aspect - 0:0 if unknown */
- /* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
- if( sscanf(tokstart, "%d:%d", &n, &d) == 2 && n && d && !p_param->vui.i_sar_width && !p_param->vui.i_sar_height )
- {
- x264_reduce_fraction( &n, &d );
- p_param->vui.i_sar_width = n;
- p_param->vui.i_sar_height = d;
- }
- tokstart = strchr(tokstart, 0x20);
- break;
- case 'X': /* Vendor extensions */
- if( !strncmp("YSCSS=",tokstart,6) )
- {
- /* Older nonstandard pixel format representation */
- tokstart += 6;
- if( strncmp("420JPEG",tokstart,7) &&
- strncmp("420MPEG2",tokstart,8) &&
- strncmp("420PALDV",tokstart,8) )
+ case 'W': /* Width. Required. */
+ h->width = p_param->i_width = strtol( tokstart, &tokend, 10 );
+ tokstart=tokend;
+ break;
+ case 'H': /* Height. Required. */
+ h->height = p_param->i_height = strtol( tokstart, &tokend, 10 );
+ tokstart=tokend;
+ break;
+ case 'C': /* Color space */
+ if( strncmp( "420", tokstart, 3 ) )
{
- fprintf(stderr, "Unsupported extended colorspace\n");
+ fprintf( stderr, "Colorspace unhandled\n" );
return -1;
}
- }
- tokstart = strchr(tokstart, 0x20);
- break;
+ tokstart = strchr( tokstart, 0x20 );
+ break;
+ case 'I': /* Interlace type */
+ switch( *tokstart++ )
+ {
+ case 'p': break;
+ case '?':
+ case 't':
+ case 'b':
+ case 'm':
+ default:
+ fprintf( stderr, "Warning, this sequence might be interlaced\n" );
+ }
+ break;
+ case 'F': /* Frame rate - 0:0 if unknown */
+ if( sscanf( tokstart, "%d:%d", &n, &d ) == 2 && n && d )
+ {
+ x264_reduce_fraction( &n, &d );
+ p_param->i_fps_num = n;
+ p_param->i_fps_den = d;
+ }
+ tokstart = strchr( tokstart, 0x20 );
+ break;
+ case 'A': /* Pixel aspect - 0:0 if unknown */
+ /* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
+ if( sscanf( tokstart, "%d:%d", &n, &d ) == 2 && n && d && !p_param->vui.i_sar_width && !p_param->vui.i_sar_height )
+ {
+ x264_reduce_fraction( &n, &d );
+ p_param->vui.i_sar_width = n;
+ p_param->vui.i_sar_height = d;
+ }
+ tokstart = strchr( tokstart, 0x20 );
+ break;
+ case 'X': /* Vendor extensions */
+ if( !strncmp( "YSCSS=", tokstart, 6 ) )
+ {
+ /* Older nonstandard pixel format representation */
+ tokstart += 6;
+ if( strncmp( "420JPEG",tokstart, 7 ) &&
+ strncmp( "420MPEG2",tokstart, 8 ) &&
+ strncmp( "420PALDV",tokstart, 8 ) )
+ {
+ fprintf( stderr, "Unsupported extended colorspace\n" );
+ return -1;
+ }
+ }
+ tokstart = strchr(tokstart, 0x20);
+ break;
}
}
- fprintf(stderr, "yuv4mpeg: %ix%i@%i/%ifps, %i:%i\n",
- h->width, h->height, p_param->i_fps_num, p_param->i_fps_den,
- p_param->vui.i_sar_width, p_param->vui.i_sar_height);
+ fprintf( stderr, "yuv4mpeg: %ix%i@%i/%ifps, %i:%i\n",
+ h->width, h->height, p_param->i_fps_num, p_param->i_fps_den,
+ p_param->vui.i_sar_width, p_param->vui.i_sar_height );
*p_handle = (hnd_t)h;
return 0;
if( i_frame != h->next_frame )
{
- if (fseek(h->fh, (uint64_t)i_frame*(3*(h->width*h->height)/2+h->frame_header_len)
- + h->seq_header_len, SEEK_SET))
+ if( fseek( h->fh, (uint64_t)i_frame*(3*(h->width*h->height)/2+h->frame_header_len)
+ + h->seq_header_len, SEEK_SET ) )
return -1;
}
/* Read frame header - without terminating '\n' */
- if (fread(header, 1, slen, h->fh) != slen)
+ if( fread( header, 1, slen, h->fh ) != slen )
return -1;
header[slen] = 0;
- if (strncmp(header, Y4M_FRAME_MAGIC, slen))
+ if( strncmp( header, Y4M_FRAME_MAGIC, slen ) )
{
- fprintf(stderr, "Bad header magic (%"PRIx32" <=> %s)\n",
- *((uint32_t*)header), header);
+ fprintf( stderr, "Bad header magic (%"PRIx32" <=> %s)\n",
+ *((uint32_t*)header), header );
return -1;
}
/* Skip most of it */
- while (i<MAX_FRAME_HEADER && fgetc(h->fh) != '\n')
+ while( i<MAX_FRAME_HEADER && fgetc(h->fh) != '\n' )
i++;
- if (i == MAX_FRAME_HEADER)
+ if( i == MAX_FRAME_HEADER )
{
- fprintf(stderr, "Bad frame header!\n");
+ fprintf( stderr, "Bad frame header!\n" );
return -1;
}
h->frame_header_len = i+slen+1;
- if( fread(p_pic->img.plane[0], 1, h->width*h->height, h->fh) <= 0
- || fread(p_pic->img.plane[1], 1, h->width * h->height / 4, h->fh) <= 0
- || fread(p_pic->img.plane[2], 1, h->width * h->height / 4, h->fh) <= 0)
+ if( fread( p_pic->img.plane[0], 1, h->width*h->height, h->fh ) <= 0
+ || fread( p_pic->img.plane[1], 1, h->width * h->height / 4, h->fh ) <= 0
+ || fread( p_pic->img.plane[2], 1, h->width * h->height / 4, h->fh ) <= 0)
return -1;
h->next_frame = i_frame+1;
/* avs/avi input file support under cygwin */
#ifdef AVIS_INPUT
-typedef struct {
+typedef struct
+{
PAVISTREAM p_avi;
int width, height;
} avis_input_t;
int open_file_avis( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
{
- avis_input_t *h = malloc(sizeof(avis_input_t));
+ avis_input_t *h = malloc( sizeof(avis_input_t) );
if( !h )
return -1;
AVISTREAMINFO info;
return -1;
}
- if( AVIStreamInfo(h->p_avi, &info, sizeof(AVISTREAMINFO)) )
+ if( AVIStreamInfo( h->p_avi, &info, sizeof(AVISTREAMINFO) ) )
{
AVIStreamRelease(h->p_avi);
AVIFileExit();
}
// check input format
- if (info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2'))
+ if( info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2') )
{
fprintf( stderr, "avis [error]: unsupported input format (%c%c%c%c)\n",
(char)(info.fccHandler & 0xff), (char)((info.fccHandler >> 8) & 0xff),
(char)((info.fccHandler >> 16) & 0xff), (char)((info.fccHandler >> 24)) );
- AVIStreamRelease(h->p_avi);
+ AVIStreamRelease( h->p_avi );
AVIFileExit();
return -1;
p_param->i_fps_num = info.dwRate / i;
fprintf( stderr, "avis [info]: %dx%d @ %.2f fps (%d frames)\n",
- p_param->i_width, p_param->i_height,
- (double)p_param->i_fps_num / (double)p_param->i_fps_den,
- (int)info.dwLength );
+ p_param->i_width, p_param->i_height,
+ (double)p_param->i_fps_num / (double)p_param->i_fps_den,
+ (int)info.dwLength );
return 0;
}
avis_input_t *h = handle;
AVISTREAMINFO info;
- if( AVIStreamInfo(h->p_avi, &info, sizeof(AVISTREAMINFO)) )
+ if( AVIStreamInfo( h->p_avi, &info, sizeof(AVISTREAMINFO) ) )
return -1;
return info.dwLength;
p_pic->img.i_csp = X264_CSP_YV12;
- if( AVIStreamRead(h->p_avi, i_frame, 1, p_pic->img.plane[0], h->width * h->height * 3 / 2, NULL, NULL ) )
+ if( AVIStreamRead( h->p_avi, i_frame, 1, p_pic->img.plane[0], h->width * h->height * 3 / 2, NULL, NULL ) )
return -1;
return 0;
int close_file_avis( hnd_t handle )
{
avis_input_t *h = handle;
- AVIStreamRelease(h->p_avi);
+ AVIStreamRelease( h->p_avi );
AVIFileExit();
- free(h);
+ free( h );
return 0;
}
#endif
#ifdef HAVE_PTHREAD
-typedef struct {
+typedef struct
+{
int (*p_read_frame)( x264_picture_t *p_pic, hnd_t handle, int i_frame );
int (*p_close_infile)( hnd_t handle );
hnd_t p_handle;
struct thread_input_arg_t *next_args;
} thread_input_t;
-typedef struct thread_input_arg_t {
+typedef struct thread_input_arg_t
+{
thread_input_t *h;
x264_picture_t *pic;
int i_frame;
int open_file_thread( char *psz_filename, hnd_t *p_handle, x264_param_t *p_param )
{
- thread_input_t *h = malloc(sizeof(thread_input_t));
+ thread_input_t *h = malloc( sizeof(thread_input_t) );
if( !h || x264_picture_alloc( &h->pic, X264_CSP_I420, p_param->i_width, p_param->i_height ) < 0 )
{
fprintf( stderr, "x264 [error]: malloc failed\n" );
int open_file_bsf( char *psz_filename, hnd_t *p_handle )
{
- if ((*p_handle = fopen(psz_filename, "w+b")) == NULL)
+ if( !(*p_handle = fopen(psz_filename, "w+b")) )
return -1;
return 0;
int write_nalu_bsf( hnd_t handle, uint8_t *p_nalu, int i_size )
{
- if (fwrite(p_nalu, i_size, 1, (FILE *)handle) > 0)
+ if( fwrite( p_nalu, i_size, 1, (FILE*)handle ) > 0 )
return i_size;
return -1;
}
int close_file_bsf( hnd_t handle )
{
- if ((handle == NULL) || (handle == stdout))
+ if( !handle || handle == stdout )
return 0;
- return fclose((FILE *)handle);
+ return fclose( (FILE*)handle );
}
/* -- mp4 muxing support ------------------------------------------------- */
Double br;
GF_ESD *esd;
- esd = gf_isom_get_esd(p_file, i_track, 1);
- if (!esd) return;
+ esd = gf_isom_get_esd( p_file, i_track, 1 );
+ if( !esd )
+ return;
esd->decoderConfig->avgBitrate = 0;
esd->decoderConfig->maxBitrate = 0;
rate = time_wnd = 0;
- timescale = gf_isom_get_media_timescale(p_file, i_track);
- count = gf_isom_get_sample_count(p_file, i_track);
- for (i=0; i<count; i++) {
- GF_ISOSample *samp = gf_isom_get_sample_info(p_file, i_track, i+1, &di, &offset);
+ timescale = gf_isom_get_media_timescale( p_file, i_track );
+ count = gf_isom_get_sample_count( p_file, i_track );
+ for( i = 0; i < count; i++ )
+ {
+ GF_ISOSample *samp = gf_isom_get_sample_info( p_file, i_track, i+1, &di, &offset );
+
+ if( samp->dataLength>esd->decoderConfig->bufferSizeDB )
+ esd->decoderConfig->bufferSizeDB = samp->dataLength;
- if (samp->dataLength>esd->decoderConfig->bufferSizeDB) esd->decoderConfig->bufferSizeDB = samp->dataLength;
+ if( esd->decoderConfig->bufferSizeDB < samp->dataLength )
+ esd->decoderConfig->bufferSizeDB = samp->dataLength;
- if (esd->decoderConfig->bufferSizeDB < samp->dataLength) esd->decoderConfig->bufferSizeDB = samp->dataLength;
esd->decoderConfig->avgBitrate += samp->dataLength;
rate += samp->dataLength;
- if (samp->DTS > time_wnd + timescale) {
- if (rate > esd->decoderConfig->maxBitrate) esd->decoderConfig->maxBitrate = rate;
+ if( samp->DTS > time_wnd + timescale )
+ {
+ if( rate > esd->decoderConfig->maxBitrate )
+ esd->decoderConfig->maxBitrate = rate;
time_wnd = samp->DTS;
rate = 0;
}
- gf_isom_sample_del(&samp);
+ gf_isom_sample_del( &samp );
}
- br = (Double) (s64) gf_isom_get_media_duration(p_file, i_track);
+ br = (Double)(s64)gf_isom_get_media_duration( p_file, i_track );
br /= timescale;
- esd->decoderConfig->avgBitrate = (u32) (esd->decoderConfig->avgBitrate / br);
+ esd->decoderConfig->avgBitrate = (u32)(esd->decoderConfig->avgBitrate / br);
/*move to bps*/
esd->decoderConfig->avgBitrate *= 8;
esd->decoderConfig->maxBitrate *= 8;
- gf_isom_change_mpeg4_description(p_file, i_track, 1, esd);
- gf_odf_desc_del((GF_Descriptor *) esd);
+ gf_isom_change_mpeg4_description( p_file, i_track, 1, esd );
+ gf_odf_desc_del( (GF_Descriptor*)esd );
}
int close_file_mp4( hnd_t handle )
{
- mp4_t *p_mp4 = (mp4_t *)handle;
+ mp4_t *p_mp4 = (mp4_t*)handle;
- if (p_mp4 == NULL)
+ if( !p_mp4 )
return 0;
- if (p_mp4->p_config)
- gf_odf_avc_cfg_del(p_mp4->p_config);
+ if( p_mp4->p_config )
+ gf_odf_avc_cfg_del( p_mp4->p_config );
- if (p_mp4->p_sample)
+ if( p_mp4->p_sample )
{
- if (p_mp4->p_sample->data)
- free(p_mp4->p_sample->data);
+ if( p_mp4->p_sample->data )
+ free( p_mp4->p_sample->data );
- gf_isom_sample_del(&p_mp4->p_sample);
+ gf_isom_sample_del( &p_mp4->p_sample );
}
- if (p_mp4->p_file)
+ if( p_mp4->p_file )
{
- recompute_bitrate_mp4(p_mp4->p_file, p_mp4->i_track);
- gf_isom_set_pl_indication(p_mp4->p_file, GF_ISOM_PL_VISUAL, 0x15);
- gf_isom_set_storage_mode(p_mp4->p_file, GF_ISOM_STORE_FLAT);
- gf_isom_close(p_mp4->p_file);
+ recompute_bitrate_mp4( p_mp4->p_file, p_mp4->i_track );
+ gf_isom_set_pl_indication( p_mp4->p_file, GF_ISOM_PL_VISUAL, 0x15 );
+ gf_isom_set_storage_mode( p_mp4->p_file, GF_ISOM_STORE_FLAT );
+ gf_isom_close( p_mp4->p_file );
}
- free(p_mp4);
+ free( p_mp4 );
return 0;
}
*p_handle = NULL;
- if ((p_mp4 = (mp4_t *)malloc(sizeof(mp4_t))) == NULL)
+ if( !(p_mp4 = malloc(sizeof(mp4_t))) )
return -1;
- memset(p_mp4, 0, sizeof(mp4_t));
- p_mp4->p_file = gf_isom_open(psz_filename, GF_ISOM_OPEN_WRITE, NULL);
+ memset( p_mp4, 0, sizeof(mp4_t) );
+ p_mp4->p_file = gf_isom_open( psz_filename, GF_ISOM_OPEN_WRITE, NULL );
- if ((p_mp4->p_sample = gf_isom_sample_new()) == NULL)
+ if( !(p_mp4->p_sample = gf_isom_sample_new()) )
{
close_file_mp4( p_mp4 );
return -1;
}
- gf_isom_set_brand_info(p_mp4->p_file, GF_ISOM_BRAND_AVC1, 0);
+ gf_isom_set_brand_info( p_mp4->p_file, GF_ISOM_BRAND_AVC1, 0 );
*p_handle = p_mp4;
int set_param_mp4( hnd_t handle, x264_param_t *p_param )
{
- mp4_t *p_mp4 = (mp4_t *)handle;
+ mp4_t *p_mp4 = (mp4_t*)handle;
- p_mp4->i_track = gf_isom_new_track(p_mp4->p_file, 0, GF_ISOM_MEDIA_VISUAL,
- p_param->i_fps_num);
+ p_mp4->i_track = gf_isom_new_track( p_mp4->p_file, 0, GF_ISOM_MEDIA_VISUAL,
+ p_param->i_fps_num );
p_mp4->p_config = gf_odf_avc_cfg_new();
- gf_isom_avc_config_new(p_mp4->p_file, p_mp4->i_track, p_mp4->p_config,
- NULL, NULL, &p_mp4->i_descidx);
+ gf_isom_avc_config_new( p_mp4->p_file, p_mp4->i_track, p_mp4->p_config,
+ NULL, NULL, &p_mp4->i_descidx );
- gf_isom_set_track_enabled(p_mp4->p_file, p_mp4->i_track, 1);
+ gf_isom_set_track_enabled( p_mp4->p_file, p_mp4->i_track, 1 );
- gf_isom_set_visual_info(p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx,
- p_param->i_width, p_param->i_height);
+ gf_isom_set_visual_info( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx,
+ p_param->i_width, p_param->i_height );
if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
{
gf_isom_set_track_layout_info( p_mp4->p_file, p_mp4->i_track, dw, dh, 0, 0, 0 );
}
- p_mp4->p_sample->data = (char *)malloc(p_param->i_width * p_param->i_height * 3 / 2);
- if (p_mp4->p_sample->data == NULL)
+ p_mp4->p_sample->data = malloc( p_param->i_width * p_param->i_height * 3 / 2 );
+ if( !p_mp4->p_sample->data )
return -1;
p_mp4->i_time_res = p_param->i_fps_num;
p_mp4->i_time_inc = p_param->i_fps_den;
p_mp4->i_init_delay = p_param->i_bframe ? (p_param->b_bframe_pyramid ? 2 : 1) : 0;
p_mp4->i_init_delay *= p_mp4->i_time_inc;
- fprintf(stderr, "mp4 [info]: initial delay %d (scale %d)\n",
- p_mp4->i_init_delay, p_mp4->i_time_res);
+ fprintf( stderr, "mp4 [info]: initial delay %d (scale %d)\n",
+ p_mp4->i_init_delay, p_mp4->i_time_res );
return 0;
}
int write_nalu_mp4( hnd_t handle, uint8_t *p_nalu, int i_size )
{
- mp4_t *p_mp4 = (mp4_t *)handle;
+ mp4_t *p_mp4 = (mp4_t*)handle;
GF_AVCConfigSlot *p_slot;
uint8_t type = p_nalu[4] & 0x1f;
int psize;
- switch(type)
+ switch( type )
{
- // sps
- case 0x07:
- if (!p_mp4->b_sps)
- {
- p_mp4->p_config->configurationVersion = 1;
- p_mp4->p_config->AVCProfileIndication = p_nalu[5];
- p_mp4->p_config->profile_compatibility = p_nalu[6];
- p_mp4->p_config->AVCLevelIndication = p_nalu[7];
- p_slot = (GF_AVCConfigSlot *)malloc(sizeof(GF_AVCConfigSlot));
- if( !p_slot )
- return -1;
- p_slot->size = i_size - 4;
- p_slot->data = (char *)malloc(p_slot->size);
- if( !p_slot->data )
- return -1;
- memcpy(p_slot->data, p_nalu + 4, i_size - 4);
- gf_list_add(p_mp4->p_config->sequenceParameterSets, p_slot);
- p_slot = NULL;
- p_mp4->b_sps = 1;
- }
- break;
+ // sps
+ case 0x07:
+ if( !p_mp4->b_sps )
+ {
+ p_mp4->p_config->configurationVersion = 1;
+ p_mp4->p_config->AVCProfileIndication = p_nalu[5];
+ p_mp4->p_config->profile_compatibility = p_nalu[6];
+ p_mp4->p_config->AVCLevelIndication = p_nalu[7];
+ p_slot = malloc( sizeof(GF_AVCConfigSlot) );
+ if( !p_slot )
+ return -1;
+ p_slot->size = i_size - 4;
+ p_slot->data = malloc( p_slot->size );
+ if( !p_slot->data )
+ return -1;
+ memcpy( p_slot->data, p_nalu + 4, i_size - 4 );
+ gf_list_add( p_mp4->p_config->sequenceParameterSets, p_slot );
+ p_slot = NULL;
+ p_mp4->b_sps = 1;
+ }
+ break;
- // pps
- case 0x08:
- if (!p_mp4->b_pps)
- {
- p_slot = (GF_AVCConfigSlot *)malloc(sizeof(GF_AVCConfigSlot));
- if( !p_slot )
- return -1;
- p_slot->size = i_size - 4;
- p_slot->data = (char *)malloc(p_slot->size);
- if( !p_slot->data )
- return -1;
- memcpy(p_slot->data, p_nalu + 4, i_size - 4);
- gf_list_add(p_mp4->p_config->pictureParameterSets, p_slot);
- p_slot = NULL;
- p_mp4->b_pps = 1;
- if (p_mp4->b_sps)
- gf_isom_avc_config_update(p_mp4->p_file, p_mp4->i_track, 1, p_mp4->p_config);
- }
- break;
-
- // slice, sei
- case 0x1:
- case 0x5:
- case 0x6:
- psize = i_size - 4 ;
- memcpy(p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size);
- p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 0] = (psize >> 24) & 0xff;
- p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 1] = (psize >> 16) & 0xff;
- p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 2] = (psize >> 8) & 0xff;
- p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 3] = (psize >> 0) & 0xff;
- p_mp4->p_sample->dataLength += i_size;
- break;
+ // pps
+ case 0x08:
+ if( !p_mp4->b_pps )
+ {
+ p_slot = malloc( sizeof(GF_AVCConfigSlot) );
+ if( !p_slot )
+ return -1;
+ p_slot->size = i_size - 4;
+ p_slot->data = malloc( p_slot->size );
+ if( !p_slot->data )
+ return -1;
+ memcpy( p_slot->data, p_nalu + 4, i_size - 4 );
+ gf_list_add( p_mp4->p_config->pictureParameterSets, p_slot );
+ p_slot = NULL;
+ p_mp4->b_pps = 1;
+ if( p_mp4->b_sps )
+ gf_isom_avc_config_update( p_mp4->p_file, p_mp4->i_track, 1, p_mp4->p_config );
+ }
+ break;
+
+ // slice, sei
+ case 0x1:
+ case 0x5:
+ case 0x6:
+ psize = i_size - 4 ;
+ memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size );
+ p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 0] = psize >> 24;
+ p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 1] = psize >> 16;
+ p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 2] = psize >> 8;
+ p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 3] = psize >> 0;
+ p_mp4->p_sample->dataLength += i_size;
+ break;
}
return i_size;
int set_eop_mp4( hnd_t handle, x264_picture_t *p_picture )
{
- mp4_t *p_mp4 = (mp4_t *)handle;
+ mp4_t *p_mp4 = (mp4_t*)handle;
uint64_t dts = (uint64_t)p_mp4->i_numframe * p_mp4->i_time_inc;
uint64_t pts = (uint64_t)p_picture->i_pts;
int32_t offset = p_mp4->i_init_delay + pts - dts;
p_mp4->p_sample->IsRAP = p_picture->i_type == X264_TYPE_IDR ? 1 : 0;
p_mp4->p_sample->DTS = dts;
p_mp4->p_sample->CTS_Offset = offset;
- gf_isom_add_sample(p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_mp4->p_sample);
+ gf_isom_add_sample( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_mp4->p_sample );
p_mp4->p_sample->dataLength = 0;
p_mp4->i_numframe++;
/* -- mkv muxing support ------------------------------------------------- */
typedef struct
{
- mk_Writer *w;
+ mk_writer *w;
- uint8_t *sps, *pps;
- int sps_len, pps_len;
+ uint8_t *sps, *pps;
+ int sps_len, pps_len;
- int width, height, d_width, d_height;
+ int width, height, d_width, d_height;
- int64_t frame_duration;
- int fps_num;
+ int64_t frame_duration;
+ int fps_num;
- int b_header_written;
- char b_writing_frame;
+ int b_header_written;
+ char b_writing_frame;
} mkv_t;
static int write_header_mkv( mkv_t *p_mkv )
{
- int ret;
- uint8_t *avcC;
- int avcC_len;
+ int ret;
+ uint8_t *avcC;
+ int avcC_len;
- if( p_mkv->sps == NULL || p_mkv->pps == NULL ||
- p_mkv->width == 0 || p_mkv->height == 0 ||
- p_mkv->d_width == 0 || p_mkv->d_height == 0)
+ if( !p_mkv->sps || !p_mkv->pps ||
+ !p_mkv->width || !p_mkv->height ||
+ !p_mkv->d_width || !p_mkv->d_height )
return -1;
avcC_len = 5 + 1 + 2 + p_mkv->sps_len + 1 + 2 + p_mkv->pps_len;
- avcC = malloc(avcC_len);
- if (avcC == NULL)
+ avcC = malloc( avcC_len );
+ if( !avcC )
return -1;
avcC[0] = 1;
avcC[6] = p_mkv->sps_len >> 8;
avcC[7] = p_mkv->sps_len;
- memcpy(avcC+8, p_mkv->sps, p_mkv->sps_len);
+ memcpy( avcC+8, p_mkv->sps, p_mkv->sps_len );
avcC[8+p_mkv->sps_len] = 1; // one pps
avcC[9+p_mkv->sps_len] = p_mkv->pps_len >> 8;
*p_handle = NULL;
- p_mkv = malloc(sizeof(*p_mkv));
- if (p_mkv == NULL)
+ p_mkv = malloc( sizeof(*p_mkv) );
+ if( !p_mkv )
return -1;
- memset(p_mkv, 0, sizeof(*p_mkv));
+ memset( p_mkv, 0, sizeof(*p_mkv) );
- p_mkv->w = mk_createWriter(psz_filename);
- if (p_mkv->w == NULL)
+ p_mkv->w = mk_create_writer( psz_filename );
+ if( !p_mkv->w )
{
- free(p_mkv);
+ free( p_mkv );
return -1;
}
switch( type )
{
- // sps
- case 0x07:
- if( !p_mkv->sps )
- {
- p_mkv->sps = malloc(i_size - 4);
- if (p_mkv->sps == NULL)
- return -1;
- p_mkv->sps_len = i_size - 4;
- memcpy(p_mkv->sps, p_nalu + 4, i_size - 4);
- }
- break;
+ // sps
+ case 0x07:
+ if( !p_mkv->sps )
+ {
+ p_mkv->sps = malloc( i_size - 4 );
+ if( !p_mkv->sps )
+ return -1;
+ p_mkv->sps_len = i_size - 4;
+ memcpy( p_mkv->sps, p_nalu + 4, i_size - 4 );
+ }
+ break;
- // pps
- case 0x08:
- if( !p_mkv->pps )
- {
- p_mkv->pps = malloc(i_size - 4);
- if (p_mkv->pps == NULL)
- return -1;
- p_mkv->pps_len = i_size - 4;
- memcpy(p_mkv->pps, p_nalu + 4, i_size - 4);
- }
- break;
+ // pps
+ case 0x08:
+ if( !p_mkv->pps )
+ {
+ p_mkv->pps = malloc( i_size - 4 );
+ if( !p_mkv->pps )
+ return -1;
+ p_mkv->pps_len = i_size - 4;
+ memcpy( p_mkv->pps, p_nalu + 4, i_size - 4 );
+ }
+ break;
- // slice, sei
- case 0x1:
- case 0x5:
- case 0x6:
- if( !p_mkv->b_writing_frame )
- {
- if( mk_startFrame(p_mkv->w) < 0 )
+ // slice, sei
+ case 0x1:
+ case 0x5:
+ case 0x6:
+ if( !p_mkv->b_writing_frame )
+ {
+ if( mk_start_frame( p_mkv->w ) < 0 )
+ return -1;
+ p_mkv->b_writing_frame = 1;
+ }
+ psize = i_size - 4 ;
+ dsize[0] = psize >> 24;
+ dsize[1] = psize >> 16;
+ dsize[2] = psize >> 8;
+ dsize[3] = psize;
+ if( mk_add_frame_data( p_mkv->w, dsize, 4 ) < 0 ||
+ mk_add_frame_data( p_mkv->w, p_nalu + 4, i_size - 4 ) < 0 )
return -1;
- p_mkv->b_writing_frame = 1;
- }
- psize = i_size - 4 ;
- dsize[0] = psize >> 24;
- dsize[1] = psize >> 16;
- dsize[2] = psize >> 8;
- dsize[3] = psize;
- if( mk_addFrameData(p_mkv->w, dsize, 4) < 0 ||
- mk_addFrameData(p_mkv->w, p_nalu + 4, i_size - 4) < 0 )
- return -1;
- break;
+ break;
- default:
- break;
+ default:
+ break;
}
if( !p_mkv->b_header_written && p_mkv->pps && p_mkv->sps &&
- write_header_mkv(p_mkv) < 0 )
+ write_header_mkv( p_mkv ) < 0 )
return -1;
return i_size;
p_mkv->b_writing_frame = 0;
- return mk_setFrameFlags( p_mkv->w, i_stamp,
- p_picture->i_type == X264_TYPE_IDR );
+ return mk_set_frame_flags( p_mkv->w, i_stamp, p_picture->i_type == X264_TYPE_IDR );
}
int close_file_mkv( hnd_t handle )
if( p_mkv->pps )
free( p_mkv->pps );
- ret = mk_close(p_mkv->w);
+ ret = mk_close( p_mkv->w );
free( p_mkv );