1 /*****************************************************************************
2 * flv_bytestream.c: flv muxer utilities
3 *****************************************************************************
4 * Copyright (C) 2009-2011 x264 project
6 * Authors: Kieran Kunhya <kieran@kunhya.com>
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 "flv_bytestream.h"
29 uint64_t flv_dbl2int( double value )
31 return (union {double f; uint64_t i;}){value}.i;
36 void flv_put_byte( flv_buffer *c, uint8_t b )
38 flv_append_data( c, &b, 1 );
41 void flv_put_be32( flv_buffer *c, uint32_t val )
43 flv_put_byte( c, val >> 24 );
44 flv_put_byte( c, val >> 16 );
45 flv_put_byte( c, val >> 8 );
46 flv_put_byte( c, val );
49 void flv_put_be64( flv_buffer *c, uint64_t val )
51 flv_put_be32( c, val >> 32 );
52 flv_put_be32( c, val );
55 void flv_put_be16( flv_buffer *c, uint16_t val )
57 flv_put_byte( c, val >> 8 );
58 flv_put_byte( c, val );
61 void flv_put_be24( flv_buffer *c, uint32_t val )
63 flv_put_be16( c, val >> 8 );
64 flv_put_byte( c, val );
67 void flv_put_tag( flv_buffer *c, const char *tag )
70 flv_put_byte( c, *tag++ );
73 void flv_put_amf_string( flv_buffer *c, const char *str )
75 uint16_t len = strlen( str );
76 flv_put_be16( c, len );
77 flv_append_data( c, (uint8_t*)str, len );
80 void flv_put_amf_double( flv_buffer *c, double d )
82 flv_put_byte( c, AMF_DATA_TYPE_NUMBER );
83 flv_put_be64( c, flv_dbl2int( d ) );
86 /* flv writing functions */
88 flv_buffer *flv_create_writer( const char *filename )
90 flv_buffer *c = malloc( sizeof(*c) );
94 memset( c, 0, sizeof(*c) );
96 if( !strcmp( filename, "-" ) )
99 c->fp = fopen( filename, "wb" );
109 int flv_append_data( flv_buffer *c, uint8_t *data, unsigned size )
111 unsigned ns = c->d_cur + size;
120 dp = realloc( c->data, dn );
128 memcpy( c->data + c->d_cur, data, size );
135 void flv_rewrite_amf_be24( flv_buffer *c, unsigned length, unsigned start )
137 *(c->data + start + 0) = length >> 16;
138 *(c->data + start + 1) = length >> 8;
139 *(c->data + start + 2) = length >> 0;
142 int flv_flush_data( flv_buffer *c )
147 if( fwrite( c->data, c->d_cur, 1, c->fp ) != 1 )
150 c->d_total += c->d_cur;