Remove reference to toolbox
[vlc/asuraparaju-public.git] / include / vlc_block_helper.h
blob7aa1989af0042ff8127b219ba3e24951367f4a4d
1 /*****************************************************************************
2 * vlc_block_helper.h: Helper functions for data blocks management.
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
5 * $Id$
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
31 block_t *p_chain;
32 block_t *p_block;
33 size_t i_offset;
35 } block_bytestream_t;
37 /*****************************************************************************
38 * block_bytestream_t management
39 *****************************************************************************/
40 LIBVLC_USED
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;
48 return bytestream;
51 static inline void block_BytestreamRelease( block_bytestream_t *p_bytestream )
53 while( p_bytestream->p_chain )
55 block_t *p_next;
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;
64 /**
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();
74 /**
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 )
81 block_t *p_next;
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 )
89 block_t *p_next;
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,
98 block_t *p_block )
100 block_ChainAppend( &p_bytestream->p_chain, p_block );
101 if( !p_bytestream->p_block ) p_bytestream->p_block = p_block;
104 LIBVLC_USED
105 static inline block_t *block_BytestreamPop( block_bytestream_t *p_bytestream )
107 block_t *p_block;
109 block_BytestreamFlush( p_bytestream );
111 p_block = p_bytestream->p_block;
112 if( p_block == NULL )
114 return 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;
122 return p_block;
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;
134 return p_block;
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++;
143 return VLC_SUCCESS;
145 else
147 block_t *p_block;
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;
157 return VLC_SUCCESS;
162 /* Not enough data, bail out */
163 return VLC_EGENERIC;
166 static inline int block_PeekByte( block_bytestream_t *p_bytestream,
167 uint8_t *p_data )
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];
173 return VLC_SUCCESS;
175 else
177 block_t *p_block;
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];
186 return VLC_SUCCESS;
191 /* Not enough data, bail out */
192 return VLC_EGENERIC;
195 static inline int block_GetByte( block_bytestream_t *p_bytestream,
196 uint8_t *p_data )
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++;
203 return VLC_SUCCESS;
205 else
207 block_t *p_block;
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;
218 return VLC_SUCCESS;
223 /* Not enough data, bail out */
224 return VLC_EGENERIC;
227 static inline int block_WaitBytes( block_bytestream_t *p_bytestream,
228 size_t i_data )
230 block_t *p_block;
231 size_t i_offset, i_copy, i_size;
233 /* Check we have that much data */
234 i_offset = p_bytestream->i_offset;
235 i_size = i_data;
236 i_copy = 0;
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 );
241 i_size -= i_copy;
242 i_offset = 0;
244 if( !i_size ) break;
247 if( i_size )
249 /* Not enough data, bail out */
250 return VLC_EGENERIC;
252 return VLC_SUCCESS;
255 static inline int block_SkipBytes( block_bytestream_t *p_bytestream,
256 size_t i_data )
258 block_t *p_block;
259 size_t i_offset, i_copy;
261 /* Check we have that much data */
262 i_offset = p_bytestream->i_offset;
263 i_copy = 0;
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 );
268 i_data -= i_copy;
270 if( !i_data ) break;
272 i_offset = 0;
275 if( i_data )
277 /* Not enough data, bail out */
278 return VLC_EGENERIC;
281 p_bytestream->p_block = p_block;
282 p_bytestream->i_offset = i_offset + i_copy;
283 return VLC_SUCCESS;
286 static inline int block_PeekBytes( block_bytestream_t *p_bytestream,
287 uint8_t *p_data, size_t i_data )
289 block_t *p_block;
290 size_t i_offset, i_copy, i_size;
292 /* Check we have that much data */
293 i_offset = p_bytestream->i_offset;
294 i_size = i_data;
295 i_copy = 0;
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 );
300 i_size -= i_copy;
301 i_offset = 0;
303 if( !i_size ) break;
306 if( i_size )
308 /* Not enough data, bail out */
309 return VLC_EGENERIC;
312 /* Copy the data */
313 i_offset = p_bytestream->i_offset;
314 i_size = i_data;
315 i_copy = 0;
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 );
320 i_size -= i_copy;
322 if( i_copy )
324 memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
325 p_data += i_copy;
328 i_offset = 0;
330 if( !i_size ) break;
333 return VLC_SUCCESS;
336 static inline int block_GetBytes( block_bytestream_t *p_bytestream,
337 uint8_t *p_data, size_t i_data )
339 block_t *p_block;
340 size_t i_offset, i_copy, i_size;
342 /* Check we have that much data */
343 i_offset = p_bytestream->i_offset;
344 i_size = i_data;
345 i_copy = 0;
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 );
350 i_size -= i_copy;
351 i_offset = 0;
353 if( !i_size ) break;
356 if( i_size )
358 /* Not enough data, bail out */
359 return VLC_EGENERIC;
362 /* Copy the data */
363 i_offset = p_bytestream->i_offset;
364 i_size = i_data;
365 i_copy = 0;
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 );
370 i_size -= i_copy;
372 if( i_copy )
374 memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
375 p_data += i_copy;
378 if( !i_size ) break;
380 i_offset = 0;
383 /* No buffer given, just skip the data */
384 p_bytestream->p_block = p_block;
385 p_bytestream->i_offset = i_offset + i_copy;
387 return VLC_SUCCESS;
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 )
393 block_t *p_block;
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;
399 i_copy = 0;
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 );
404 i_size -= i_copy;
405 i_offset = 0;
407 if( !i_size ) break;
410 if( i_size )
412 /* Not enough data, bail out */
413 return VLC_EGENERIC;
416 /* Find the right place */
417 i_offset = p_bytestream->i_offset;
418 i_size = i_peek_offset;
419 i_copy = 0;
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 );
424 i_size -= i_copy;
426 if( !i_size ) break;
428 i_offset = 0;
431 /* Copy the data */
432 i_offset += i_copy;
433 i_size = i_data;
434 i_copy = 0;
435 for( ; p_block != NULL; p_block = p_block->p_next )
437 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
438 i_size -= i_copy;
440 if( i_copy )
442 memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
443 p_data += i_copy;
446 i_offset = 0;
448 if( !i_size ) break;
451 return VLC_SUCCESS;
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;
459 int i_size = 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;
472 if( i_size >= 0 )
474 /* Not enough data, bail out */
475 return VLC_EGENERIC;
478 /* Begin the search.
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;
483 i_match = 0;
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] )
490 if( !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 )
499 /* We have it */
500 *pi_offset += i_offset - i_match;
501 return VLC_SUCCESS;
504 i_match++;
506 else if ( i_match )
508 /* False positive */
509 p_block = p_block_backup;
510 i_offset = i_offset_backup;
511 *pi_offset = i_caller_offset_backup;
512 i_match = 0;
516 i_size = 0;
517 *pi_offset += i_offset;
520 *pi_offset -= i_match;
521 return VLC_EGENERIC;
524 #endif /* VLC_BLOCK_HELPER_H */