]> git.sesse.net Git - x264/blob - output/flv_bytestream.c
Periodic intra refresh
[x264] / output / flv_bytestream.c
1 /*****************************************************************************
2  * flv_bytestream.c:
3  *****************************************************************************
4  * Copyright (C) 2009 Kieran Kunhya
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #include "muxers.h"
22 #include "flv_bytestream.h"
23
24 uint64_t dbl2int( double value )
25 {
26     return (union {double f; uint64_t i;}){value}.i;
27 }
28
29 /* Put functions  */
30
31 void x264_put_byte( flv_buffer *c, uint8_t b )
32 {
33     flv_append_data( c, &b, 1 );
34 }
35
36 void x264_put_be32( flv_buffer *c, uint32_t val )
37 {
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 );
42 }
43
44 void x264_put_be64( flv_buffer *c, uint64_t val )
45 {
46     x264_put_be32( c, val >> 32 );
47     x264_put_be32( c, val );
48 }
49
50 void x264_put_be16( flv_buffer *c, uint16_t val )
51 {
52     x264_put_byte( c, val >> 8 );
53     x264_put_byte( c, val );
54 }
55
56 void x264_put_be24( flv_buffer *c, uint32_t val )
57 {
58     x264_put_be16( c, val >> 8 );
59     x264_put_byte( c, val );
60 }
61
62 void x264_put_tag( flv_buffer *c, const char *tag )
63 {
64     while( *tag )
65         x264_put_byte( c, *tag++ );
66 }
67
68 void x264_put_amf_string( flv_buffer *c, const char *str )
69 {
70     uint16_t len = strlen( str );
71     x264_put_be16( c, len );
72     flv_append_data( c, (uint8_t*)str, len );
73 }
74
75 void x264_put_amf_double( flv_buffer *c, double d )
76 {
77     x264_put_byte( c, AMF_DATA_TYPE_NUMBER );
78     x264_put_be64( c, dbl2int( d ) );
79 }
80
81 /* flv writing functions */
82
83 flv_buffer *flv_create_writer( const char *filename )
84 {
85     flv_buffer *c = malloc( sizeof(*c) );
86
87     if( !c )
88         return NULL;
89     memset( c, 0, sizeof(*c) );
90
91     if( !strcmp( filename, "-" ) )
92         c->fp = stdout;
93     else
94         c->fp = fopen( filename, "wb" );
95     if( !c->fp )
96     {
97         free( c );
98         return NULL;
99     }
100
101     return c;
102 }
103
104 int flv_append_data( flv_buffer *c, uint8_t *data, unsigned size )
105 {
106     unsigned ns = c->d_cur + size;
107
108     if( ns > c->d_max )
109     {
110         void *dp;
111         unsigned dn = 16;
112         while( ns > dn )
113             dn <<= 1;
114
115         dp = realloc( c->data, dn );
116         if( !dp )
117             return -1;
118
119         c->data = dp;
120         c->d_max = dn;
121     }
122
123     memcpy( c->data + c->d_cur, data, size );
124
125     c->d_cur = ns;
126
127     return 0;
128 }
129
130 void rewrite_amf_be24( flv_buffer *c, unsigned length, unsigned start )
131 {
132      *(c->data + start + 0) = length >> 16;
133      *(c->data + start + 1) = length >> 8;
134      *(c->data + start + 2) = length >> 0;
135 }
136
137 int flv_flush_data( flv_buffer *c )
138 {
139     if( !c->d_cur )
140         return 0;
141
142     if( fwrite( c->data, c->d_cur, 1, c->fp ) != 1 )
143         return -1;
144
145     c->d_total += c->d_cur;
146
147     c->d_cur = 0;
148
149     return 0;
150 }