1 /*****************************************************************************
2 * packetizer.h: Packetizer helpers
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #define _PACKETIZER_H 1
27 #include <vlc_block.h>
35 typedef void (*packetizer_reset_t)( void *p_private, bool b_broken );
36 typedef block_t *(*packetizer_parse_t)( void *p_private, bool *pb_ts_used, block_t * );
37 typedef int (*packetizer_validate_t)( void *p_private, block_t * );
42 block_bytestream_t bytestream;
47 const uint8_t *p_startcode;
50 const uint8_t *p_au_prepend;
53 packetizer_reset_t pf_reset;
54 packetizer_parse_t pf_parse;
55 packetizer_validate_t pf_validate;
59 static inline void packetizer_Init( packetizer_t *p_pack,
60 const uint8_t *p_startcode, int i_startcode,
61 const uint8_t *p_au_prepend, int i_au_prepend,
62 packetizer_reset_t pf_reset,
63 packetizer_parse_t pf_parse,
64 packetizer_validate_t pf_validate,
67 p_pack->i_state = STATE_NOSYNC;
68 p_pack->bytestream = block_BytestreamInit();
70 p_pack->b_flushing = false;
72 p_pack->i_au_prepend = i_au_prepend;
73 p_pack->p_au_prepend = p_au_prepend;
75 p_pack->i_startcode = i_startcode;
76 p_pack->p_startcode = p_startcode;
77 p_pack->pf_reset = pf_reset;
78 p_pack->pf_parse = pf_parse;
79 p_pack->pf_validate = pf_validate;
80 p_pack->p_private = p_private;
83 static inline void packetizer_Clean( packetizer_t *p_pack )
85 block_BytestreamRelease( &p_pack->bytestream );
88 static inline block_t *packetizer_Packetize( packetizer_t *p_pack, block_t **pp_block )
90 if( !pp_block || !*pp_block )
93 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
95 const bool b_broken = ( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED ) != 0;
98 p_pack->i_state = STATE_NOSYNC;
99 block_BytestreamEmpty( &p_pack->bytestream );
100 p_pack->i_offset = 0;
102 p_pack->pf_reset( p_pack->p_private, b_broken );
104 block_Release( *pp_block );
108 block_BytestreamPush( &p_pack->bytestream, *pp_block );
115 switch( p_pack->i_state )
118 /* Find a startcode */
119 if( !block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
120 p_pack->p_startcode, p_pack->i_startcode ) )
121 p_pack->i_state = STATE_NEXT_SYNC;
123 if( p_pack->i_offset )
125 block_SkipBytes( &p_pack->bytestream, p_pack->i_offset );
126 p_pack->i_offset = 0;
127 block_BytestreamFlush( &p_pack->bytestream );
130 if( p_pack->i_state != STATE_NEXT_SYNC )
131 return NULL; /* Need more data */
133 p_pack->i_offset = 1; /* To find next startcode */
135 case STATE_NEXT_SYNC:
136 /* Find the next startcode */
137 if( block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
138 p_pack->p_startcode, p_pack->i_startcode ) )
140 if( !p_pack->b_flushing || !p_pack->bytestream.p_chain )
141 return NULL; /* Need more data */
143 /* When flusing and we don't find a startcode, suppose that
144 * the data extend up to the end */
145 block_ChainProperties( p_pack->bytestream.p_chain,
146 NULL, &p_pack->i_offset, NULL );
147 p_pack->i_offset -= p_pack->bytestream .i_offset;
149 if( p_pack->i_offset <= (size_t)p_pack->i_startcode )
153 block_BytestreamFlush( &p_pack->bytestream );
155 /* Get the new fragment and set the pts/dts */
156 block_t *p_block_bytestream = p_pack->bytestream.p_block;
158 p_pic = block_New( p_dec, p_pack->i_offset + p_pack->i_au_prepend );
159 p_pic->i_pts = p_block_bytestream->i_pts;
160 p_pic->i_dts = p_block_bytestream->i_dts;
162 block_GetBytes( &p_pack->bytestream, &p_pic->p_buffer[p_pack->i_au_prepend],
163 p_pic->i_buffer - p_pack->i_au_prepend );
164 if( p_pack->i_au_prepend > 0 )
165 memcpy( p_pic->p_buffer, p_pack->p_au_prepend, p_pack->i_au_prepend );
167 p_pack->i_offset = 0;
170 p_pic = p_pack->pf_parse( p_pack->p_private, &b_used_ts, p_pic );
173 p_block_bytestream->i_dts = VLC_TS_INVALID;
174 p_block_bytestream->i_pts = VLC_TS_INVALID;
179 p_pack->i_state = STATE_NOSYNC;
182 if( p_pack->pf_validate( p_pack->p_private, p_pic ) )
184 p_pack->i_state = STATE_NOSYNC;
185 block_Release( p_pic );
189 /* So p_block doesn't get re-added several times */
190 *pp_block = block_BytestreamPop( &p_pack->bytestream );
192 p_pack->i_state = STATE_NOSYNC;
199 static inline void packetizer_Header( packetizer_t *p_pack,
200 const uint8_t *p_header, int i_header )
202 block_t *p_init = block_Alloc( i_header );
206 memcpy( p_init->p_buffer, p_header, i_header );
208 p_pack->b_flushing = true;
211 while( ( p_pic = packetizer_Packetize( p_pack, &p_init ) ) )
212 block_Release( p_pic ); /* Should not happen (only sequence header) */
214 p_pack->i_state = STATE_NOSYNC;
215 block_BytestreamEmpty( &p_pack->bytestream );
216 p_pack->i_offset = 0;
217 p_pack->b_flushing = false;