]> git.sesse.net Git - x264/blob - output/flv_bytestream.c
Bump dates to 2012
[x264] / output / flv_bytestream.c
1 /*****************************************************************************
2  * flv_bytestream.c: flv muxer utilities
3  *****************************************************************************
4  * Copyright (C) 2009-2012 x264 project
5  *
6  * Authors: Kieran Kunhya <kieran@kunhya.com>
7  *
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.
12  *
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.
17  *
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.
21  *
22  * This program is also available under a commercial proprietary license.
23  * For more information, contact us at licensing@x264.com.
24  *****************************************************************************/
25
26 #include "output.h"
27 #include "flv_bytestream.h"
28
29 uint64_t flv_dbl2int( double value )
30 {
31     return (union {double f; uint64_t i;}){value}.i;
32 }
33
34 /* Put functions  */
35
36 void flv_put_byte( flv_buffer *c, uint8_t b )
37 {
38     flv_append_data( c, &b, 1 );
39 }
40
41 void flv_put_be32( flv_buffer *c, uint32_t val )
42 {
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 );
47 }
48
49 void flv_put_be64( flv_buffer *c, uint64_t val )
50 {
51     flv_put_be32( c, val >> 32 );
52     flv_put_be32( c, val );
53 }
54
55 void flv_put_be16( flv_buffer *c, uint16_t val )
56 {
57     flv_put_byte( c, val >> 8 );
58     flv_put_byte( c, val );
59 }
60
61 void flv_put_be24( flv_buffer *c, uint32_t val )
62 {
63     flv_put_be16( c, val >> 8 );
64     flv_put_byte( c, val );
65 }
66
67 void flv_put_tag( flv_buffer *c, const char *tag )
68 {
69     while( *tag )
70         flv_put_byte( c, *tag++ );
71 }
72
73 void flv_put_amf_string( flv_buffer *c, const char *str )
74 {
75     uint16_t len = strlen( str );
76     flv_put_be16( c, len );
77     flv_append_data( c, (uint8_t*)str, len );
78 }
79
80 void flv_put_amf_double( flv_buffer *c, double d )
81 {
82     flv_put_byte( c, AMF_DATA_TYPE_NUMBER );
83     flv_put_be64( c, flv_dbl2int( d ) );
84 }
85
86 /* flv writing functions */
87
88 flv_buffer *flv_create_writer( const char *filename )
89 {
90     flv_buffer *c = malloc( sizeof(*c) );
91
92     if( !c )
93         return NULL;
94     memset( c, 0, sizeof(*c) );
95
96     if( !strcmp( filename, "-" ) )
97         c->fp = stdout;
98     else
99         c->fp = fopen( filename, "wb" );
100     if( !c->fp )
101     {
102         free( c );
103         return NULL;
104     }
105
106     return c;
107 }
108
109 int flv_append_data( flv_buffer *c, uint8_t *data, unsigned size )
110 {
111     unsigned ns = c->d_cur + size;
112
113     if( ns > c->d_max )
114     {
115         void *dp;
116         unsigned dn = 16;
117         while( ns > dn )
118             dn <<= 1;
119
120         dp = realloc( c->data, dn );
121         if( !dp )
122             return -1;
123
124         c->data = dp;
125         c->d_max = dn;
126     }
127
128     memcpy( c->data + c->d_cur, data, size );
129
130     c->d_cur = ns;
131
132     return 0;
133 }
134
135 void flv_rewrite_amf_be24( flv_buffer *c, unsigned length, unsigned start )
136 {
137      *(c->data + start + 0) = length >> 16;
138      *(c->data + start + 1) = length >> 8;
139      *(c->data + start + 2) = length >> 0;
140 }
141
142 int flv_flush_data( flv_buffer *c )
143 {
144     if( !c->d_cur )
145         return 0;
146
147     if( fwrite( c->data, c->d_cur, 1, c->fp ) != 1 )
148         return -1;
149
150     c->d_total += c->d_cur;
151
152     c->d_cur = 0;
153
154     return 0;
155 }