direct3d11: use the D3D11 types as D3D9 is not available on some platforms
[vlc.git] / include / vlc_block_helper.h
blob4414307503689e171746cf88ab549d01511f9db4
1 /*****************************************************************************
2 * vlc_block_helper.h: Helper functions for data blocks management.
3 *****************************************************************************
4 * Copyright (C) 2003 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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; /**< byte stream head block */
32 block_t *p_block; /**< byte stream read pointer block */
33 size_t i_offset; /**< byte stream read pointer offset within block */
34 /* TODO? add tail pointer for faster push? */
35 } block_bytestream_t;
37 /*****************************************************************************
38 * block_bytestream_t management
39 *****************************************************************************/
40 static inline void block_BytestreamInit( block_bytestream_t *p_bytestream )
42 p_bytestream->p_chain = p_bytestream->p_block = NULL;
43 p_bytestream->i_offset = 0;
46 static inline void block_BytestreamRelease( block_bytestream_t *p_bytestream )
48 for( block_t *block = p_bytestream->p_chain; block != NULL; )
50 block_t *p_next = block->p_next;
52 block_Release( block );
53 block = p_next;
57 /**
58 * It flush all data (read and unread) from a block_bytestream_t.
60 static inline void block_BytestreamEmpty( block_bytestream_t *p_bytestream )
62 block_BytestreamRelease( p_bytestream );
63 block_BytestreamInit( p_bytestream );
66 /**
67 * It flushes all already read data from a block_bytestream_t.
69 static inline void block_BytestreamFlush( block_bytestream_t *p_bytestream )
71 block_t *block = p_bytestream->p_chain;
73 while( block != p_bytestream->p_block )
75 block_t *p_next = block->p_next;
77 block_Release( block );
78 block = p_next;
81 while( block != NULL && block->i_buffer == p_bytestream->i_offset )
83 block_t *p_next = block->p_next;
85 block_Release( block );
86 block = p_next;
87 p_bytestream->i_offset = 0;
90 p_bytestream->p_chain = p_bytestream->p_block = block;
93 static inline void block_BytestreamPush( block_bytestream_t *p_bytestream,
94 block_t *p_block )
96 block_ChainAppend( &p_bytestream->p_chain, p_block );
97 if( !p_bytestream->p_block ) p_bytestream->p_block = p_block;
100 VLC_USED
101 static inline block_t *block_BytestreamPop( block_bytestream_t *p_bytestream )
103 block_t *p_block;
105 block_BytestreamFlush( p_bytestream );
107 p_block = p_bytestream->p_block;
108 if( unlikely( p_block == NULL ) )
110 return NULL;
112 else if( !p_block->p_next )
114 p_block->p_buffer += p_bytestream->i_offset;
115 p_block->i_buffer -= p_bytestream->i_offset;
116 p_bytestream->i_offset = 0;
117 p_bytestream->p_chain = p_bytestream->p_block = NULL;
118 return p_block;
121 while( p_block->p_next && p_block->p_next->p_next )
122 p_block = p_block->p_next;
124 block_t *p_block_old = p_block;
125 p_block = p_block->p_next;
126 p_block_old->p_next = NULL;
128 return p_block;
131 static inline int block_SkipByte( block_bytestream_t *p_bytestream )
133 /* Most common case first */
134 if( likely( p_bytestream->p_block->i_buffer - p_bytestream->i_offset ) )
136 p_bytestream->i_offset++;
137 return VLC_SUCCESS;
139 else
141 block_t *p_block;
143 /* Less common case which is also slower */
144 for( p_block = p_bytestream->p_block->p_next;
145 p_block != NULL; p_block = p_block->p_next )
147 if( p_block->i_buffer )
149 p_bytestream->i_offset = 1;
150 p_bytestream->p_block = p_block;
151 return VLC_SUCCESS;
156 /* Not enough data, bail out */
157 return VLC_EGENERIC;
160 static inline int block_PeekByte( block_bytestream_t *p_bytestream,
161 uint8_t *p_data )
163 /* Most common case first */
164 if( likely( p_bytestream->p_block->i_buffer - p_bytestream->i_offset ) )
166 *p_data = p_bytestream->p_block->p_buffer[p_bytestream->i_offset];
167 return VLC_SUCCESS;
169 else
171 block_t *p_block;
173 /* Less common case which is also slower */
174 for( p_block = p_bytestream->p_block->p_next;
175 p_block != NULL; p_block = p_block->p_next )
177 if( p_block->i_buffer )
179 *p_data = p_block->p_buffer[0];
180 return VLC_SUCCESS;
185 /* Not enough data, bail out */
186 return VLC_EGENERIC;
189 static inline int block_GetByte( block_bytestream_t *p_bytestream,
190 uint8_t *p_data )
192 /* Most common case first */
193 if( likely( p_bytestream->p_block->i_buffer - p_bytestream->i_offset ) )
195 *p_data = p_bytestream->p_block->p_buffer[p_bytestream->i_offset];
196 p_bytestream->i_offset++;
197 return VLC_SUCCESS;
199 else
201 block_t *p_block;
203 /* Less common case which is also slower */
204 for( p_block = p_bytestream->p_block->p_next;
205 p_block != NULL; p_block = p_block->p_next )
207 if( p_block->i_buffer )
209 *p_data = p_block->p_buffer[0];
210 p_bytestream->i_offset = 1;
211 p_bytestream->p_block = p_block;
212 return VLC_SUCCESS;
217 /* Not enough data, bail out */
218 return VLC_EGENERIC;
221 static inline int block_WaitBytes( block_bytestream_t *p_bytestream,
222 size_t i_data )
224 block_t *p_block;
225 size_t i_offset, i_copy, i_size;
227 /* Check we have that much data */
228 i_offset = p_bytestream->i_offset;
229 i_size = i_data;
230 for( p_block = p_bytestream->p_block;
231 p_block != NULL; p_block = p_block->p_next )
233 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
234 i_size -= i_copy;
235 i_offset = 0;
237 if( !i_size ) break;
240 if( i_size )
242 /* Not enough data, bail out */
243 return VLC_EGENERIC;
245 return VLC_SUCCESS;
248 static inline int block_SkipBytes( block_bytestream_t *p_bytestream,
249 size_t i_data )
251 block_t *p_block;
252 size_t i_offset, i_copy;
254 /* Check we have that much data */
255 i_offset = p_bytestream->i_offset;
256 i_copy = 0;
257 for( p_block = p_bytestream->p_block;
258 p_block != NULL; p_block = p_block->p_next )
260 i_copy = __MIN( i_data, p_block->i_buffer - i_offset );
261 i_data -= i_copy;
263 if( !i_data ) break;
265 i_offset = 0;
268 if( i_data )
270 /* Not enough data, bail out */
271 return VLC_EGENERIC;
274 p_bytestream->p_block = p_block;
275 p_bytestream->i_offset = i_offset + i_copy;
276 return VLC_SUCCESS;
279 static inline int block_PeekBytes( block_bytestream_t *p_bytestream,
280 uint8_t *p_data, size_t i_data )
282 block_t *p_block;
283 size_t i_offset, i_copy, i_size;
285 /* Check we have that much data */
286 i_offset = p_bytestream->i_offset;
287 i_size = i_data;
288 for( p_block = p_bytestream->p_block;
289 p_block != NULL; p_block = p_block->p_next )
291 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
292 i_size -= i_copy;
293 i_offset = 0;
295 if( !i_size ) break;
298 if( i_size )
300 /* Not enough data, bail out */
301 return VLC_EGENERIC;
304 /* Copy the data */
305 i_offset = p_bytestream->i_offset;
306 i_size = i_data;
307 for( p_block = p_bytestream->p_block;
308 p_block != NULL; p_block = p_block->p_next )
310 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
311 i_size -= i_copy;
313 if( i_copy )
315 memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
316 p_data += i_copy;
319 i_offset = 0;
321 if( !i_size ) break;
324 return VLC_SUCCESS;
327 static inline int block_GetBytes( block_bytestream_t *p_bytestream,
328 uint8_t *p_data, size_t i_data )
330 block_t *p_block;
331 size_t i_offset, i_copy, i_size;
333 /* Check we have that much data */
334 i_offset = p_bytestream->i_offset;
335 i_size = i_data;
336 for( p_block = p_bytestream->p_block;
337 p_block != NULL; p_block = p_block->p_next )
339 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
340 i_size -= i_copy;
341 i_offset = 0;
343 if( !i_size ) break;
346 if( i_size )
348 /* Not enough data, bail out */
349 return VLC_EGENERIC;
352 /* Copy the data */
353 i_offset = p_bytestream->i_offset;
354 i_size = i_data;
355 i_copy = 0;
356 for( p_block = p_bytestream->p_block;
357 p_block != NULL; p_block = p_block->p_next )
359 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
360 i_size -= i_copy;
362 if( i_copy )
364 memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
365 p_data += i_copy;
368 if( !i_size ) break;
370 i_offset = 0;
373 p_bytestream->p_block = p_block;
374 p_bytestream->i_offset = i_offset + i_copy;
376 return VLC_SUCCESS;
379 static inline int block_PeekOffsetBytes( block_bytestream_t *p_bytestream,
380 size_t i_peek_offset, uint8_t *p_data, size_t i_data )
382 block_t *p_block;
383 size_t i_offset, i_copy, i_size;
385 /* Check we have that much data */
386 i_offset = p_bytestream->i_offset;
387 i_size = i_data + i_peek_offset;
388 for( p_block = p_bytestream->p_block;
389 p_block != NULL; p_block = p_block->p_next )
391 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
392 i_size -= i_copy;
393 i_offset = 0;
395 if( !i_size ) break;
398 if( i_size )
400 /* Not enough data, bail out */
401 return VLC_EGENERIC;
404 /* Find the right place */
405 i_offset = p_bytestream->i_offset;
406 i_size = i_peek_offset;
407 i_copy = 0;
408 for( p_block = p_bytestream->p_block;
409 p_block != NULL; p_block = p_block->p_next )
411 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
412 i_size -= i_copy;
414 if( !i_size ) break;
416 i_offset = 0;
419 /* Copy the data */
420 i_offset += i_copy;
421 i_size = i_data;
422 for( ; p_block != NULL; p_block = p_block->p_next )
424 i_copy = __MIN( i_size, p_block->i_buffer - i_offset );
425 i_size -= i_copy;
427 if( i_copy )
429 memcpy( p_data, p_block->p_buffer + i_offset, i_copy );
430 p_data += i_copy;
433 i_offset = 0;
435 if( !i_size ) break;
438 return VLC_SUCCESS;
441 static inline int block_FindStartcodeFromOffset(
442 block_bytestream_t *p_bytestream, size_t *pi_offset,
443 const uint8_t *p_startcode, int i_startcode_length )
445 block_t *p_block, *p_block_backup = 0;
446 int i_size = 0;
447 size_t i_offset, i_offset_backup = 0;
448 int i_caller_offset_backup = 0, i_match;
450 /* Find the right place */
451 i_size = *pi_offset + p_bytestream->i_offset;
452 for( p_block = p_bytestream->p_block;
453 p_block != NULL; p_block = p_block->p_next )
455 i_size -= p_block->i_buffer;
456 if( i_size < 0 ) break;
459 if( unlikely( i_size >= 0 ) )
461 /* Not enough data, bail out */
462 return VLC_EGENERIC;
465 /* Begin the search.
466 * We first look for an occurrence of the 1st startcode byte and
467 * if found, we do a more thorough check. */
468 i_size += p_block->i_buffer;
469 *pi_offset -= i_size;
470 i_match = 0;
471 for( ; p_block != NULL; p_block = p_block->p_next )
473 for( i_offset = i_size; i_offset < p_block->i_buffer; i_offset++ )
475 if( p_block->p_buffer[i_offset] == p_startcode[i_match] )
477 if( !i_match )
479 p_block_backup = p_block;
480 i_offset_backup = i_offset;
481 i_caller_offset_backup = *pi_offset;
484 if( i_match + 1 == i_startcode_length )
486 /* We have it */
487 *pi_offset += i_offset - i_match;
488 return VLC_SUCCESS;
491 i_match++;
493 else if ( i_match )
495 /* False positive */
496 p_block = p_block_backup;
497 i_offset = i_offset_backup;
498 *pi_offset = i_caller_offset_backup;
499 i_match = 0;
503 i_size = 0;
504 *pi_offset += i_offset;
507 *pi_offset -= i_match;
508 return VLC_EGENERIC;
511 #endif /* VLC_BLOCK_HELPER_H */