packetizer_helper.h 8.52 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * packetizer_helper.h: Packetizer helpers
3 4 5 6 7 8
 *****************************************************************************
 * Copyright (C) 2009 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25
#ifndef VLC_PACKETIZER_HELPER_H_
#define VLC_PACKETIZER_HELPER_H_
26 27 28 29 30 31

#include <vlc_block.h>

enum
{
    STATE_NOSYNC,
32 33 34 35 36
    STATE_SYNC,
    STATE_HEADER,
    STATE_NEXT_SYNC,
    STATE_GET_DATA,
    STATE_SEND_DATA
37 38 39 40 41 42 43 44 45 46 47 48 49 50
};

typedef void (*packetizer_reset_t)( void *p_private, bool b_broken );
typedef block_t *(*packetizer_parse_t)( void *p_private, bool *pb_ts_used, block_t * );
typedef int (*packetizer_validate_t)( void *p_private, block_t * );

typedef struct
{
    int i_state;
    block_bytestream_t bytestream;
    size_t i_offset;

    int i_startcode;
    const uint8_t *p_startcode;
51
    block_startcode_helper_t pf_startcode_helper;
52 53 54 55

    int i_au_prepend;
    const uint8_t *p_au_prepend;

56 57
    unsigned i_au_min_size;

58 59 60 61 62 63 64 65 66
    void *p_private;
    packetizer_reset_t    pf_reset;
    packetizer_parse_t    pf_parse;
    packetizer_validate_t pf_validate;

} packetizer_t;

static inline void packetizer_Init( packetizer_t *p_pack,
                                    const uint8_t *p_startcode, int i_startcode,
67
                                    block_startcode_helper_t pf_start_helper,
68
                                    const uint8_t *p_au_prepend, int i_au_prepend,
69
                                    unsigned i_au_min_size,
70 71 72 73 74 75
                                    packetizer_reset_t pf_reset,
                                    packetizer_parse_t pf_parse,
                                    packetizer_validate_t pf_validate,
                                    void *p_private )
{
    p_pack->i_state = STATE_NOSYNC;
76
    block_BytestreamInit( &p_pack->bytestream );
77 78 79 80
    p_pack->i_offset = 0;

    p_pack->i_au_prepend = i_au_prepend;
    p_pack->p_au_prepend = p_au_prepend;
81
    p_pack->i_au_min_size = i_au_min_size;
82 83 84

    p_pack->i_startcode = i_startcode;
    p_pack->p_startcode = p_startcode;
85
    p_pack->pf_startcode_helper = pf_start_helper;
86 87 88 89 90 91 92 93 94 95 96
    p_pack->pf_reset = pf_reset;
    p_pack->pf_parse = pf_parse;
    p_pack->pf_validate = pf_validate;
    p_pack->p_private = p_private;
}

static inline void packetizer_Clean( packetizer_t *p_pack )
{
    block_BytestreamRelease( &p_pack->bytestream );
}

97 98 99 100 101 102 103 104
static inline void packetizer_Flush( packetizer_t *p_pack )
{
    p_pack->i_state = STATE_NOSYNC;
    block_BytestreamEmpty( &p_pack->bytestream );
    p_pack->i_offset = 0;
    p_pack->pf_reset( p_pack->p_private, true );
}

105 106
static inline block_t *packetizer_Packetize( packetizer_t *p_pack, block_t **pp_block )
{
107
    block_t *p_block = ( pp_block ) ? *pp_block : NULL;
108

109 110 111 112
    if( p_block == NULL && p_pack->bytestream.p_block == NULL )
        return NULL; /* nothing to do */

    if( p_block && unlikely( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) )
113
    {
114 115 116 117 118
        block_t *p_drained = packetizer_Packetize( p_pack, NULL );
        if( p_drained )
            return p_drained;

        const bool b_broken = !!( p_block->i_flags&BLOCK_FLAG_CORRUPTED );
119 120 121 122
        p_pack->i_state = STATE_NOSYNC;
        block_BytestreamEmpty( &p_pack->bytestream );
        p_pack->i_offset = 0;
        p_pack->pf_reset( p_pack->p_private, b_broken );
123 124
        if( b_broken )
        {
125
            block_Release( p_block );
126
            return NULL;
127 128 129
        }
    }

130 131
    if( p_block )
        block_BytestreamPush( &p_pack->bytestream, p_block );
132 133 134 135 136 137 138 139 140 141 142

    for( ;; )
    {
        bool b_used_ts;
        block_t *p_pic;

        switch( p_pack->i_state )
        {
        case STATE_NOSYNC:
            /* Find a startcode */
            if( !block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
143
                                                p_pack->p_startcode, p_pack->i_startcode,
144
                                                p_pack->pf_startcode_helper, NULL ) )
145 146 147 148 149 150 151 152 153 154 155 156 157
                p_pack->i_state = STATE_NEXT_SYNC;

            if( p_pack->i_offset )
            {
                block_SkipBytes( &p_pack->bytestream, p_pack->i_offset );
                p_pack->i_offset = 0;
                block_BytestreamFlush( &p_pack->bytestream );
            }

            if( p_pack->i_state != STATE_NEXT_SYNC )
                return NULL; /* Need more data */

            p_pack->i_offset = 1; /* To find next startcode */
158
            /* fallthrough */
159 160 161 162

        case STATE_NEXT_SYNC:
            /* Find the next startcode */
            if( block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
163
                                               p_pack->p_startcode, p_pack->i_startcode,
164
                                               p_pack->pf_startcode_helper, NULL ) )
165
            {
166
                if( pp_block /* not flushing */ || !p_pack->bytestream.p_chain )
167 168 169 170 171 172
                    return NULL; /* Need more data */

                /* When flusing and we don't find a startcode, suppose that
                 * the data extend up to the end */
                block_ChainProperties( p_pack->bytestream.p_chain,
                                       NULL, &p_pack->i_offset, NULL );
173
                p_pack->i_offset -= p_pack->bytestream.i_block_offset;
174 175 176 177

                if( p_pack->i_offset <= (size_t)p_pack->i_startcode )
                    return NULL;
            }
178 179 180 181 182 183

            block_BytestreamFlush( &p_pack->bytestream );

            /* Get the new fragment and set the pts/dts */
            block_t *p_block_bytestream = p_pack->bytestream.p_block;

184
            p_pic = block_Alloc( p_pack->i_offset + p_pack->i_au_prepend );
185 186 187 188 189 190 191 192 193 194 195
            p_pic->i_pts = p_block_bytestream->i_pts;
            p_pic->i_dts = p_block_bytestream->i_dts;

            block_GetBytes( &p_pack->bytestream, &p_pic->p_buffer[p_pack->i_au_prepend],
                            p_pic->i_buffer - p_pack->i_au_prepend );
            if( p_pack->i_au_prepend > 0 )
                memcpy( p_pic->p_buffer, p_pack->p_au_prepend, p_pack->i_au_prepend );

            p_pack->i_offset = 0;

            /* Parse the NAL */
196 197 198 199 200 201
            if( p_pic->i_buffer < p_pack->i_au_min_size )
            {
                block_Release( p_pic );
                p_pic = NULL;
            }
            else
202
            {
203 204 205 206 207 208
                p_pic = p_pack->pf_parse( p_pack->p_private, &b_used_ts, p_pic );
                if( b_used_ts )
                {
                    p_block_bytestream->i_dts = VLC_TS_INVALID;
                    p_block_bytestream->i_pts = VLC_TS_INVALID;
                }
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
            }

            if( !p_pic )
            {
                p_pack->i_state = STATE_NOSYNC;
                break;
            }
            if( p_pack->pf_validate( p_pack->p_private, p_pic ) )
            {
                p_pack->i_state = STATE_NOSYNC;
                block_Release( p_pic );
                break;
            }

            /* So p_block doesn't get re-added several times */
224 225
            if( pp_block )
                *pp_block = block_BytestreamPop( &p_pack->bytestream );
226 227 228 229 230 231 232 233

            p_pack->i_state = STATE_NOSYNC;

            return p_pic;
        }
    }
}

234 235 236 237 238 239 240 241 242 243 244 245
static inline void packetizer_Header( packetizer_t *p_pack,
                                      const uint8_t *p_header, int i_header )
{
    block_t *p_init = block_Alloc( i_header );
    if( !p_init )
        return;

    memcpy( p_init->p_buffer, p_header, i_header );

    block_t *p_pic;
    while( ( p_pic = packetizer_Packetize( p_pack, &p_init ) ) )
        block_Release( p_pic ); /* Should not happen (only sequence header) */
246 247
    while( ( p_pic = packetizer_Packetize( p_pack, NULL ) ) )
        block_Release( p_pic );
248 249 250 251

    p_pack->i_state = STATE_NOSYNC;
    block_BytestreamEmpty( &p_pack->bytestream );
    p_pack->i_offset = 0;
252 253
}

254 255
#endif