1 /*****************************************************************************
2 * vlc_block_helper.h: Helper functions for data blocks management.
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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 *****************************************************************************/
24 #ifndef VLC_BLOCK_HELPER_H
25 #define VLC_BLOCK_HELPER_H 1
27 #include <vlc_block.h>
29 typedef struct block_bytestream_t
37 /*****************************************************************************
38 * block_bytestream_t management
39 *****************************************************************************/
41 static inline block_bytestream_t block_BytestreamInit( void )
43 block_bytestream_t bytestream;
45 bytestream.i_offset = 0;
46 bytestream.p_chain = bytestream.p_block = NULL;
51 static inline void block_BytestreamRelease( block_bytestream_t *p_bytestream )
53 while( p_bytestream->p_chain )
56 p_next = p_bytestream->p_chain->p_next;
57 p_bytestream->p_chain->pf_release( p_bytestream->p_chain );
58 p_bytestream->p_chain = p_next;
60 p_bytestream->i_offset = 0;
61 p_bytestream->p_chain = p_bytestream->p_block = NULL;
65 * It flush all data (read and unread) from a block_bytestream_t.
67 static inline void block_BytestreamEmpty( block_bytestream_t *p_bytestream )
69 block_BytestreamRelease( p_bytestream );
71 *p_bytestream = block_BytestreamInit();
75 * It flushes all already read data from a block_bytestream_t.
77 static inline void block_BytestreamFlush( block_bytestream_t *p_bytestream )
79 while( p_bytestream->p_chain != p_bytestream->p_block )
82 p_next = p_bytestream->p_chain->p_next;
83 p_bytestream->p_chain->pf_release( p_bytestream->p_chain );
84 p_bytestream->p_chain = p_next;
86 while( p_bytestream->p_block &&
87 (p_bytestream->p_block->i_buffer - p_bytestream->i_offset) == 0 )
90 p_next = p_bytestream->p_chain->p_next;
91 p_bytestream->p_chain->pf_release( p_bytestream->p_chain );
92 p_bytestream->p_chain = p_bytestream->p_block = p_next;
93 p_bytestream->i_offset = 0;
97 static inline void block_BytestreamPush( block_bytestream_t *p_bytestream,
100 block_ChainAppend( &p_bytestream->p_chain, p_block );
101 if( !p_bytestream->p_block ) p_bytestream->p_block = p_block;
105 static inline block_t *block_BytestreamPop( block_bytestream_t *p_bytestream )
109 block_BytestreamFlush( p_bytestream );
111 p_block = p_bytestream->p_block;
112 if( p_block == NULL )
116 else if( !p_block->p_next )
118 p_block->p_buffer += p_bytestream->i_offset;
119 p_block->i_buffer -= p_bytestream->i_offset;
120 p_bytestream->i_offset = 0;
121 p_bytestream->p_chain = p_bytestream->p_block = NULL;
125 while( p_block->p_next && p_block->p_next->p_next )
126 p_block = p_block->p_next;
129 block_t *p_block_old = p_block;
130 p_block = p_block->p_next;
131 p_block_old->p_next = NULL;
137 static inline int block_SkipByte( block_bytestream_t *p_bytestream )
139 /* Most common case first */
140 if( p_bytestream->p_block->i_buffer - p_bytestream->i_offset )
142 p_bytestream->i_offset++;
149 /* Less common case which is also slower */
150 for( p_block = p_bytestream->p_block->p_next;
151 p_block != NULL; p_block = p_block->p_next )
153 if( p_block->i_buffer )
155 p_bytestream->i_offset = 1;
156 p_bytestream->p_block = p_block;
162 /* Not enough data, bail out */
166 static inline int block_PeekByte( block_bytestream_t *p_bytestream,
169 /* Most common case first */
170 if( p_bytestream->p_block->i_buffer - p_bytestream->i_offset )
172 *p_data = p_bytestream->p_block->p_buffer[p_bytestream->i_offset];
179 /* Less common case which is also slower */
180 for( p_block = p_bytestream->p_block->p_next;
181 p_block != NULL; p_block = p_block->p_next )
183 if( p_block->i_buffer )
185 *p_data = p_block->p_buffer[0];
191 /* Not enough data, bail out */
195 static inline int block_GetByte( block_bytestream_t *p_bytestream,
198 /* Most common case first */
199 if( p_bytestream->p_block->i_buffer - p_bytestream->i_offset )
201 *p_data = p_bytestream->p_block->p_buffer[p_bytestream->i_offset];
202 p_bytestream->i_offset++;
209 /* Less common case which is also slower */
210 for( p_block = p_bytestream->p_block->p_next;
211 p_block != NULL; p_block = p_block->p_next )
213 if( p_block->i_buffer )
215 *p_data = p_block->p_buffer[0];
216 p_bytestream->i_offset = 1;
217 p_bytestream->p_block = p_block;
223 /* Not enough data, bail out */
227 static inline int block_WaitBytes( block_bytestream_t *p_bytestream,
231 size_t i_offset, i_copy, i_size;
233 /* Check we have that much data */
234 i_offset = p_bytestream->i_offset;
237 for( p_block = p_bytestream->p_block;
238 p_block != NULL; p_block = p_block->p_next )
240 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
249 /* Not enough data, bail out */
255 static inline int block_SkipBytes( block_bytestream_t *p_bytestream,
259 size_t i_offset, i_copy;
261 /* Check we have that much data */
262 i_offset = p_bytestream->i_offset;
264 for( p_block = p_bytestream->p_block;
265 p_block != NULL; p_block = p_block->p_next )
267 i_copy = __MIN( i_data, p_block->i_buffer - i_offset );
277 /* Not enough data, bail out */
281 p_bytestream->p_block = p_block;
282 p_bytestream->i_offset = i_offset + i_copy;
286 static inline int block_PeekBytes( block_bytestream_t *p_bytestream,
287 uint8_t *p_data, size_t i_data )
290 size_t i_offset, i_copy, i_size;
292 /* Check we have that much data */
293 i_offset = p_bytestream->i_offset;
296 for( p_block = p_bytestream->p_block;
297 p_block != NULL; p_block = p_block->p_next )
299 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
308 /* Not enough data, bail out */
313 i_offset = p_bytestream->i_offset;
316 for( p_block = p_bytestream->p_block;
317 p_block != NULL; p_block = p_block->p_next )
319 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
324 memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
336 static inline int block_GetBytes( block_bytestream_t *p_bytestream,
337 uint8_t *p_data, size_t i_data )
340 size_t i_offset, i_copy, i_size;
342 /* Check we have that much data */
343 i_offset = p_bytestream->i_offset;
346 for( p_block = p_bytestream->p_block;
347 p_block != NULL; p_block = p_block->p_next )
349 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
358 /* Not enough data, bail out */
363 i_offset = p_bytestream->i_offset;
366 for( p_block = p_bytestream->p_block;
367 p_block != NULL; p_block = p_block->p_next )
369 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
374 memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
383 /* No buffer given, just skip the data */
384 p_bytestream->p_block = p_block;
385 p_bytestream->i_offset = i_offset + i_copy;
390 static inline int block_PeekOffsetBytes( block_bytestream_t *p_bytestream,
391 size_t i_peek_offset, uint8_t *p_data, size_t i_data )
394 size_t i_offset, i_copy, i_size;
396 /* Check we have that much data */
397 i_offset = p_bytestream->i_offset;
398 i_size = i_data + i_peek_offset;
400 for( p_block = p_bytestream->p_block;
401 p_block != NULL; p_block = p_block->p_next )
403 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
412 /* Not enough data, bail out */
416 /* Find the right place */
417 i_offset = p_bytestream->i_offset;
418 i_size = i_peek_offset;
420 for( p_block = p_bytestream->p_block;
421 p_block != NULL; p_block = p_block->p_next )
423 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
435 for( ; p_block != NULL; p_block = p_block->p_next )
437 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
442 memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
454 static inline int block_FindStartcodeFromOffset(
455 block_bytestream_t *p_bytestream, size_t *pi_offset,
456 const uint8_t *p_startcode, int i_startcode_length )
458 block_t *p_block, *p_block_backup = 0;
460 size_t i_offset, i_offset_backup = 0;
461 int i_caller_offset_backup = 0, i_match;
463 /* Find the right place */
464 i_size = *pi_offset + p_bytestream->i_offset;
465 for( p_block = p_bytestream->p_block;
466 p_block != NULL; p_block = p_block->p_next )
468 i_size -= p_block->i_buffer;
469 if( i_size < 0 ) break;
474 /* Not enough data, bail out */
479 * We first look for an occurrence of the 1st startcode byte and
480 * if found, we do a more thorough check. */
481 i_size += p_block->i_buffer;
482 *pi_offset -= i_size;
484 for( ; p_block != NULL; p_block = p_block->p_next )
486 for( i_offset = i_size; i_offset < p_block->i_buffer; i_offset++ )
488 if( p_block->p_buffer[i_offset] == p_startcode[i_match] )
492 p_block_backup = p_block;
493 i_offset_backup = i_offset;
494 i_caller_offset_backup = *pi_offset;
497 if( i_match + 1 == i_startcode_length )
500 *pi_offset += i_offset - i_match;
509 p_block = p_block_backup;
510 i_offset = i_offset_backup;
511 *pi_offset = i_caller_offset_backup;
517 *pi_offset += i_offset;
520 *pi_offset -= i_match;
524 #endif /* VLC_BLOCK_HELPER_H */