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;
52 unsigned i_au_min_size;
55 packetizer_reset_t pf_reset;
56 packetizer_parse_t pf_parse;
57 packetizer_validate_t pf_validate;
61 static inline void packetizer_Init( packetizer_t *p_pack,
62 const uint8_t *p_startcode, int i_startcode,
63 const uint8_t *p_au_prepend, int i_au_prepend,
64 unsigned i_au_min_size,
65 packetizer_reset_t pf_reset,
66 packetizer_parse_t pf_parse,
67 packetizer_validate_t pf_validate,
70 p_pack->i_state = STATE_NOSYNC;
71 p_pack->bytestream = block_BytestreamInit();
73 p_pack->b_flushing = false;
75 p_pack->i_au_prepend = i_au_prepend;
76 p_pack->p_au_prepend = p_au_prepend;
77 p_pack->i_au_min_size = i_au_min_size;
79 p_pack->i_startcode = i_startcode;
80 p_pack->p_startcode = p_startcode;
81 p_pack->pf_reset = pf_reset;
82 p_pack->pf_parse = pf_parse;
83 p_pack->pf_validate = pf_validate;
84 p_pack->p_private = p_private;
87 static inline void packetizer_Clean( packetizer_t *p_pack )
89 block_BytestreamRelease( &p_pack->bytestream );
92 static inline block_t *packetizer_Packetize( packetizer_t *p_pack, block_t **pp_block )
94 if( !pp_block || !*pp_block )
97 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
99 const bool b_broken = ( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED ) != 0;
102 p_pack->i_state = STATE_NOSYNC;
103 block_BytestreamEmpty( &p_pack->bytestream );
104 p_pack->i_offset = 0;
106 p_pack->pf_reset( p_pack->p_private, b_broken );
108 block_Release( *pp_block );
112 block_BytestreamPush( &p_pack->bytestream, *pp_block );
119 switch( p_pack->i_state )
122 /* Find a startcode */
123 if( !block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
124 p_pack->p_startcode, p_pack->i_startcode ) )
125 p_pack->i_state = STATE_NEXT_SYNC;
127 if( p_pack->i_offset )
129 block_SkipBytes( &p_pack->bytestream, p_pack->i_offset );
130 p_pack->i_offset = 0;
131 block_BytestreamFlush( &p_pack->bytestream );
134 if( p_pack->i_state != STATE_NEXT_SYNC )
135 return NULL; /* Need more data */
137 p_pack->i_offset = 1; /* To find next startcode */
139 case STATE_NEXT_SYNC:
140 /* Find the next startcode */
141 if( block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
142 p_pack->p_startcode, p_pack->i_startcode ) )
144 if( !p_pack->b_flushing || !p_pack->bytestream.p_chain )
145 return NULL; /* Need more data */
147 /* When flusing and we don't find a startcode, suppose that
148 * the data extend up to the end */
149 block_ChainProperties( p_pack->bytestream.p_chain,
150 NULL, &p_pack->i_offset, NULL );
151 p_pack->i_offset -= p_pack->bytestream .i_offset;
153 if( p_pack->i_offset <= (size_t)p_pack->i_startcode )
157 block_BytestreamFlush( &p_pack->bytestream );
159 /* Get the new fragment and set the pts/dts */
160 block_t *p_block_bytestream = p_pack->bytestream.p_block;
162 p_pic = block_New( p_dec, p_pack->i_offset + p_pack->i_au_prepend );
163 p_pic->i_pts = p_block_bytestream->i_pts;
164 p_pic->i_dts = p_block_bytestream->i_dts;
166 block_GetBytes( &p_pack->bytestream, &p_pic->p_buffer[p_pack->i_au_prepend],
167 p_pic->i_buffer - p_pack->i_au_prepend );
168 if( p_pack->i_au_prepend > 0 )
169 memcpy( p_pic->p_buffer, p_pack->p_au_prepend, p_pack->i_au_prepend );
171 p_pack->i_offset = 0;
174 if( p_pic->i_buffer < p_pack->i_au_min_size )
176 block_Release( p_pic );
181 p_pic = p_pack->pf_parse( p_pack->p_private, &b_used_ts, p_pic );
184 p_block_bytestream->i_dts = VLC_TS_INVALID;
185 p_block_bytestream->i_pts = VLC_TS_INVALID;
191 p_pack->i_state = STATE_NOSYNC;
194 if( p_pack->pf_validate( p_pack->p_private, p_pic ) )
196 p_pack->i_state = STATE_NOSYNC;
197 block_Release( p_pic );
201 /* So p_block doesn't get re-added several times */
202 *pp_block = block_BytestreamPop( &p_pack->bytestream );
204 p_pack->i_state = STATE_NOSYNC;
211 static inline void packetizer_Header( packetizer_t *p_pack,
212 const uint8_t *p_header, int i_header )
214 block_t *p_init = block_Alloc( i_header );
218 memcpy( p_init->p_buffer, p_header, i_header );
220 p_pack->b_flushing = true;
223 while( ( p_pic = packetizer_Packetize( p_pack, &p_init ) ) )
224 block_Release( p_pic ); /* Should not happen (only sequence header) */
226 p_pack->i_state = STATE_NOSYNC;
227 block_BytestreamEmpty( &p_pack->bytestream );
228 p_pack->i_offset = 0;
229 p_pack->b_flushing = false;