1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2009 Kieran Kunhya
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 "flv_bytestream.h"
24 uint64_t dbl2int( double value )
26 return (union {double f; uint64_t i;}){value}.i;
31 void x264_put_byte( flv_buffer *c, uint8_t b )
33 flv_append_data( c, &b, 1 );
36 void x264_put_be32( flv_buffer *c, uint32_t val )
38 x264_put_byte( c, val >> 24 );
39 x264_put_byte( c, val >> 16 );
40 x264_put_byte( c, val >> 8 );
41 x264_put_byte( c, val );
44 void x264_put_be64( flv_buffer *c, uint64_t val )
46 x264_put_be32( c, val >> 32 );
47 x264_put_be32( c, val );
50 void x264_put_be16( flv_buffer *c, uint16_t val )
52 x264_put_byte( c, val >> 8 );
53 x264_put_byte( c, val );
56 void x264_put_be24( flv_buffer *c, uint32_t val )
58 x264_put_be16( c, val >> 8 );
59 x264_put_byte( c, val );
62 void x264_put_tag( flv_buffer *c, const char *tag )
65 x264_put_byte( c, *tag++ );
68 void x264_put_amf_string( flv_buffer *c, const char *str )
70 uint16_t len = strlen( str );
71 x264_put_be16( c, len );
72 flv_append_data( c, (uint8_t*)str, len );
75 void x264_put_amf_double( flv_buffer *c, double d )
77 x264_put_byte( c, AMF_DATA_TYPE_NUMBER );
78 x264_put_be64( c, dbl2int( d ) );
81 /* flv writing functions */
83 flv_buffer *flv_create_writer( const char *filename )
85 flv_buffer *c = malloc( sizeof(*c) );
89 memset( c, 0, sizeof(*c) );
91 if( !strcmp( filename, "-" ) )
94 c->fp = fopen( filename, "wb" );
104 int flv_append_data( flv_buffer *c, uint8_t *data, unsigned size )
106 unsigned ns = c->d_cur + size;
115 dp = realloc( c->data, dn );
123 memcpy( c->data + c->d_cur, data, size );
130 void rewrite_amf_be24( flv_buffer *c, unsigned length, unsigned start )
132 *(c->data + start + 0) = length >> 16;
133 *(c->data + start + 1) = length >> 8;
134 *(c->data + start + 2) = length >> 0;
137 int flv_flush_data( flv_buffer *c )
142 if( fwrite( c->data, c->d_cur, 1, c->fp ) != 1 )
145 c->d_total += c->d_cur;