flv_bytestream.c 3.58 KB
Newer Older
Kieran Kunhya's avatar
Kieran Kunhya committed
1
/*****************************************************************************
2
 * flv_bytestream.c: flv muxer utilities
Kieran Kunhya's avatar
Kieran Kunhya committed
3
 *****************************************************************************
Henrik Gramner's avatar
Henrik Gramner committed
4
 * Copyright (C) 2009-2019 x264 project
5 6
 *
 * Authors: Kieran Kunhya <kieran@kunhya.com>
Kieran Kunhya's avatar
Kieran Kunhya committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
21 22 23
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
Kieran Kunhya's avatar
Kieran Kunhya committed
24 25
 *****************************************************************************/

26
#include "output.h"
Kieran Kunhya's avatar
Kieran Kunhya committed
27 28
#include "flv_bytestream.h"

Anton Mitrofanov's avatar
Anton Mitrofanov committed
29
uint64_t flv_dbl2int( double value )
Kieran Kunhya's avatar
Kieran Kunhya committed
30 31 32 33 34 35
{
    return (union {double f; uint64_t i;}){value}.i;
}

/* Put functions  */

Anton Mitrofanov's avatar
Anton Mitrofanov committed
36
void flv_put_byte( flv_buffer *c, uint8_t b )
Kieran Kunhya's avatar
Kieran Kunhya committed
37 38 39 40
{
    flv_append_data( c, &b, 1 );
}

Anton Mitrofanov's avatar
Anton Mitrofanov committed
41
void flv_put_be32( flv_buffer *c, uint32_t val )
Kieran Kunhya's avatar
Kieran Kunhya committed
42
{
Anton Mitrofanov's avatar
Anton Mitrofanov committed
43 44 45 46
    flv_put_byte( c, val >> 24 );
    flv_put_byte( c, val >> 16 );
    flv_put_byte( c, val >> 8 );
    flv_put_byte( c, val );
Kieran Kunhya's avatar
Kieran Kunhya committed
47 48
}

Anton Mitrofanov's avatar
Anton Mitrofanov committed
49
void flv_put_be64( flv_buffer *c, uint64_t val )
Kieran Kunhya's avatar
Kieran Kunhya committed
50
{
Anton Mitrofanov's avatar
Anton Mitrofanov committed
51 52
    flv_put_be32( c, val >> 32 );
    flv_put_be32( c, val );
Kieran Kunhya's avatar
Kieran Kunhya committed
53 54
}

Anton Mitrofanov's avatar
Anton Mitrofanov committed
55
void flv_put_be16( flv_buffer *c, uint16_t val )
Kieran Kunhya's avatar
Kieran Kunhya committed
56
{
Anton Mitrofanov's avatar
Anton Mitrofanov committed
57 58
    flv_put_byte( c, val >> 8 );
    flv_put_byte( c, val );
Kieran Kunhya's avatar
Kieran Kunhya committed
59 60
}

Anton Mitrofanov's avatar
Anton Mitrofanov committed
61
void flv_put_be24( flv_buffer *c, uint32_t val )
Kieran Kunhya's avatar
Kieran Kunhya committed
62
{
Anton Mitrofanov's avatar
Anton Mitrofanov committed
63 64
    flv_put_be16( c, val >> 8 );
    flv_put_byte( c, val );
Kieran Kunhya's avatar
Kieran Kunhya committed
65 66
}

Anton Mitrofanov's avatar
Anton Mitrofanov committed
67
void flv_put_tag( flv_buffer *c, const char *tag )
Kieran Kunhya's avatar
Kieran Kunhya committed
68 69
{
    while( *tag )
Anton Mitrofanov's avatar
Anton Mitrofanov committed
70
        flv_put_byte( c, *tag++ );
Kieran Kunhya's avatar
Kieran Kunhya committed
71 72
}

Anton Mitrofanov's avatar
Anton Mitrofanov committed
73
void flv_put_amf_string( flv_buffer *c, const char *str )
Kieran Kunhya's avatar
Kieran Kunhya committed
74 75
{
    uint16_t len = strlen( str );
Anton Mitrofanov's avatar
Anton Mitrofanov committed
76
    flv_put_be16( c, len );
Kieran Kunhya's avatar
Kieran Kunhya committed
77 78 79
    flv_append_data( c, (uint8_t*)str, len );
}

Anton Mitrofanov's avatar
Anton Mitrofanov committed
80
void flv_put_amf_double( flv_buffer *c, double d )
Kieran Kunhya's avatar
Kieran Kunhya committed
81
{
Anton Mitrofanov's avatar
Anton Mitrofanov committed
82 83
    flv_put_byte( c, AMF_DATA_TYPE_NUMBER );
    flv_put_be64( c, flv_dbl2int( d ) );
Kieran Kunhya's avatar
Kieran Kunhya committed
84 85 86 87 88 89
}

/* flv writing functions */

flv_buffer *flv_create_writer( const char *filename )
{
90
    flv_buffer *c = calloc( 1, sizeof(flv_buffer) );
Kieran Kunhya's avatar
Kieran Kunhya committed
91 92 93 94 95 96
    if( !c )
        return NULL;

    if( !strcmp( filename, "-" ) )
        c->fp = stdout;
    else
Henrik Gramner's avatar
Henrik Gramner committed
97
        c->fp = x264_fopen( filename, "wb" );
Kieran Kunhya's avatar
Kieran Kunhya committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    if( !c->fp )
    {
        free( c );
        return NULL;
    }

    return c;
}

int flv_append_data( flv_buffer *c, uint8_t *data, unsigned size )
{
    unsigned ns = c->d_cur + size;

    if( ns > c->d_max )
    {
        void *dp;
        unsigned dn = 16;
        while( ns > dn )
            dn <<= 1;

        dp = realloc( c->data, dn );
        if( !dp )
            return -1;

        c->data = dp;
        c->d_max = dn;
    }

    memcpy( c->data + c->d_cur, data, size );

    c->d_cur = ns;

    return 0;
}

Anton Mitrofanov's avatar
Anton Mitrofanov committed
133
void flv_rewrite_amf_be24( flv_buffer *c, unsigned length, unsigned start )
Kieran Kunhya's avatar
Kieran Kunhya committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
{
     *(c->data + start + 0) = length >> 16;
     *(c->data + start + 1) = length >> 8;
     *(c->data + start + 2) = length >> 0;
}

int flv_flush_data( flv_buffer *c )
{
    if( !c->d_cur )
        return 0;

    if( fwrite( c->data, c->d_cur, 1, c->fp ) != 1 )
        return -1;

    c->d_total += c->d_cur;

    c->d_cur = 0;

    return 0;
}