Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
52867 views
1
/*****************************************************************************
2
* flv_bytestream.c: flv muxer utilities
3
*****************************************************************************
4
* Copyright (C) 2009-2016 x264 project
5
*
6
* Authors: Kieran Kunhya <[email protected]>
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 [email protected].
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 = calloc( 1, sizeof(flv_buffer) );
91
if( !c )
92
return NULL;
93
94
if( !strcmp( filename, "-" ) )
95
c->fp = stdout;
96
else
97
c->fp = x264_fopen( filename, "wb" );
98
if( !c->fp )
99
{
100
free( c );
101
return NULL;
102
}
103
104
return c;
105
}
106
107
int flv_append_data( flv_buffer *c, uint8_t *data, unsigned size )
108
{
109
unsigned ns = c->d_cur + size;
110
111
if( ns > c->d_max )
112
{
113
void *dp;
114
unsigned dn = 16;
115
while( ns > dn )
116
dn <<= 1;
117
118
dp = realloc( c->data, dn );
119
if( !dp )
120
return -1;
121
122
c->data = dp;
123
c->d_max = dn;
124
}
125
126
memcpy( c->data + c->d_cur, data, size );
127
128
c->d_cur = ns;
129
130
return 0;
131
}
132
133
void flv_rewrite_amf_be24( flv_buffer *c, unsigned length, unsigned start )
134
{
135
*(c->data + start + 0) = length >> 16;
136
*(c->data + start + 1) = length >> 8;
137
*(c->data + start + 2) = length >> 0;
138
}
139
140
int flv_flush_data( flv_buffer *c )
141
{
142
if( !c->d_cur )
143
return 0;
144
145
if( fwrite( c->data, c->d_cur, 1, c->fp ) != 1 )
146
return -1;
147
148
c->d_total += c->d_cur;
149
150
c->d_cur = 0;
151
152
return 0;
153
}
154
155