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 *****************************************************************************/
40 static inline block_bytestream_t
block_BytestreamInit( void )
42 block_bytestream_t bytestream
;
44 bytestream
.i_offset
= 0;
45 bytestream
.p_chain
= bytestream
.p_block
= NULL
;
50 static inline void block_BytestreamRelease( block_bytestream_t
*p_bytestream
)
52 while( p_bytestream
->p_chain
)
55 p_next
= p_bytestream
->p_chain
->p_next
;
56 p_bytestream
->p_chain
->pf_release( p_bytestream
->p_chain
);
57 p_bytestream
->p_chain
= p_next
;
59 p_bytestream
->i_offset
= 0;
60 p_bytestream
->p_chain
= p_bytestream
->p_block
= NULL
;
63 static inline void block_BytestreamFlush( block_bytestream_t
*p_bytestream
)
65 while( p_bytestream
->p_chain
!= p_bytestream
->p_block
)
68 p_next
= p_bytestream
->p_chain
->p_next
;
69 p_bytestream
->p_chain
->pf_release( p_bytestream
->p_chain
);
70 p_bytestream
->p_chain
= p_next
;
72 while( p_bytestream
->p_block
&&
73 (p_bytestream
->p_block
->i_buffer
- p_bytestream
->i_offset
) == 0 )
76 p_next
= p_bytestream
->p_chain
->p_next
;
77 p_bytestream
->p_chain
->pf_release( p_bytestream
->p_chain
);
78 p_bytestream
->p_chain
= p_bytestream
->p_block
= p_next
;
79 p_bytestream
->i_offset
= 0;
83 static inline void block_BytestreamPush( block_bytestream_t
*p_bytestream
,
86 block_ChainAppend( &p_bytestream
->p_chain
, p_block
);
87 if( !p_bytestream
->p_block
) p_bytestream
->p_block
= p_block
;
90 static inline block_t
*block_BytestreamPop( block_bytestream_t
*p_bytestream
)
94 block_BytestreamFlush( p_bytestream
);
96 p_block
= p_bytestream
->p_block
;
101 else if( !p_block
->p_next
)
103 p_block
->p_buffer
+= p_bytestream
->i_offset
;
104 p_block
->i_buffer
-= p_bytestream
->i_offset
;
105 p_bytestream
->i_offset
= 0;
106 p_bytestream
->p_chain
= p_bytestream
->p_block
= NULL
;
110 while( p_block
->p_next
&& p_block
->p_next
->p_next
)
111 p_block
= p_block
->p_next
;
114 block_t
*p_block_old
= p_block
;
115 p_block
= p_block
->p_next
;
116 p_block_old
->p_next
= NULL
;
122 static inline int block_SkipByte( block_bytestream_t
*p_bytestream
)
124 /* Most common case first */
125 if( p_bytestream
->p_block
->i_buffer
- p_bytestream
->i_offset
)
127 p_bytestream
->i_offset
++;
134 /* Less common case which is also slower */
135 for( p_block
= p_bytestream
->p_block
->p_next
;
136 p_block
!= NULL
; p_block
= p_block
->p_next
)
138 if( p_block
->i_buffer
)
140 p_bytestream
->i_offset
= 1;
141 p_bytestream
->p_block
= p_block
;
147 /* Not enough data, bail out */
151 static inline int block_PeekByte( block_bytestream_t
*p_bytestream
,
154 /* Most common case first */
155 if( p_bytestream
->p_block
->i_buffer
- p_bytestream
->i_offset
)
157 *p_data
= p_bytestream
->p_block
->p_buffer
[p_bytestream
->i_offset
];
164 /* Less common case which is also slower */
165 for( p_block
= p_bytestream
->p_block
->p_next
;
166 p_block
!= NULL
; p_block
= p_block
->p_next
)
168 if( p_block
->i_buffer
)
170 *p_data
= p_block
->p_buffer
[0];
176 /* Not enough data, bail out */
180 static inline int block_GetByte( block_bytestream_t
*p_bytestream
,
183 /* Most common case first */
184 if( p_bytestream
->p_block
->i_buffer
- p_bytestream
->i_offset
)
186 *p_data
= p_bytestream
->p_block
->p_buffer
[p_bytestream
->i_offset
];
187 p_bytestream
->i_offset
++;
194 /* Less common case which is also slower */
195 for( p_block
= p_bytestream
->p_block
->p_next
;
196 p_block
!= NULL
; p_block
= p_block
->p_next
)
198 if( p_block
->i_buffer
)
200 *p_data
= p_block
->p_buffer
[0];
201 p_bytestream
->i_offset
= 1;
202 p_bytestream
->p_block
= p_block
;
208 /* Not enough data, bail out */
212 static inline int block_WaitBytes( block_bytestream_t
*p_bytestream
,
216 size_t i_offset
, i_copy
, i_size
;
218 /* Check we have that much data */
219 i_offset
= p_bytestream
->i_offset
;
222 for( p_block
= p_bytestream
->p_block
;
223 p_block
!= NULL
; p_block
= p_block
->p_next
)
225 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
234 /* Not enough data, bail out */
240 static inline int block_SkipBytes( block_bytestream_t
*p_bytestream
,
244 size_t i_offset
, i_copy
;
246 /* Check we have that much data */
247 i_offset
= p_bytestream
->i_offset
;
249 for( p_block
= p_bytestream
->p_block
;
250 p_block
!= NULL
; p_block
= p_block
->p_next
)
252 i_copy
= __MIN( i_data
, p_block
->i_buffer
- i_offset
);
262 /* Not enough data, bail out */
266 p_bytestream
->p_block
= p_block
;
267 p_bytestream
->i_offset
= i_offset
+ i_copy
;
271 static inline int block_PeekBytes( block_bytestream_t
*p_bytestream
,
272 uint8_t *p_data
, size_t i_data
)
275 size_t i_offset
, i_copy
, i_size
;
277 /* Check we have that much data */
278 i_offset
= p_bytestream
->i_offset
;
281 for( p_block
= p_bytestream
->p_block
;
282 p_block
!= NULL
; p_block
= p_block
->p_next
)
284 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
293 /* Not enough data, bail out */
298 i_offset
= p_bytestream
->i_offset
;
301 for( p_block
= p_bytestream
->p_block
;
302 p_block
!= NULL
; p_block
= p_block
->p_next
)
304 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
309 memcpy( p_data
, p_block
->p_buffer
+ i_offset
, i_copy
);
321 static inline int block_GetBytes( block_bytestream_t
*p_bytestream
,
322 uint8_t *p_data
, size_t i_data
)
325 size_t i_offset
, i_copy
, i_size
;
327 /* Check we have that much data */
328 i_offset
= p_bytestream
->i_offset
;
331 for( p_block
= p_bytestream
->p_block
;
332 p_block
!= NULL
; p_block
= p_block
->p_next
)
334 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
343 /* Not enough data, bail out */
348 i_offset
= p_bytestream
->i_offset
;
351 for( p_block
= p_bytestream
->p_block
;
352 p_block
!= NULL
; p_block
= p_block
->p_next
)
354 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
359 memcpy( p_data
, p_block
->p_buffer
+ i_offset
, i_copy
);
368 /* No buffer given, just skip the data */
369 p_bytestream
->p_block
= p_block
;
370 p_bytestream
->i_offset
= i_offset
+ i_copy
;
375 static inline int block_PeekOffsetBytes( block_bytestream_t
*p_bytestream
,
376 size_t i_peek_offset
, uint8_t *p_data
, size_t i_data
)
379 size_t i_offset
, i_copy
, i_size
;
381 /* Check we have that much data */
382 i_offset
= p_bytestream
->i_offset
;
383 i_size
= i_data
+ i_peek_offset
;
385 for( p_block
= p_bytestream
->p_block
;
386 p_block
!= NULL
; p_block
= p_block
->p_next
)
388 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
397 /* Not enough data, bail out */
401 /* Find the right place */
402 i_offset
= p_bytestream
->i_offset
;
403 i_size
= i_peek_offset
;
405 for( p_block
= p_bytestream
->p_block
;
406 p_block
!= NULL
; p_block
= p_block
->p_next
)
408 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
420 for( ; p_block
!= NULL
; p_block
= p_block
->p_next
)
422 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
427 memcpy( p_data
, p_block
->p_buffer
+ i_offset
, i_copy
);
439 static inline int block_FindStartcodeFromOffset(
440 block_bytestream_t
*p_bytestream
, size_t *pi_offset
,
441 uint8_t *p_startcode
, int i_startcode_length
)
443 block_t
*p_block
, *p_block_backup
= 0;
445 size_t i_offset
, i_offset_backup
= 0;
446 int i_caller_offset_backup
= 0, i_match
;
448 /* Find the right place */
449 i_size
= *pi_offset
+ p_bytestream
->i_offset
;
450 for( p_block
= p_bytestream
->p_block
;
451 p_block
!= NULL
; p_block
= p_block
->p_next
)
453 i_size
-= p_block
->i_buffer
;
454 if( i_size
< 0 ) break;
459 /* Not enough data, bail out */
464 * We first look for an occurrence of the 1st startcode byte and
465 * if found, we do a more thorough check. */
466 i_size
+= p_block
->i_buffer
;
467 *pi_offset
-= i_size
;
469 for( ; p_block
!= NULL
; p_block
= p_block
->p_next
)
471 for( i_offset
= i_size
; i_offset
< p_block
->i_buffer
; i_offset
++ )
473 if( p_block
->p_buffer
[i_offset
] == p_startcode
[i_match
] )
477 p_block_backup
= p_block
;
478 i_offset_backup
= i_offset
;
479 i_caller_offset_backup
= *pi_offset
;
482 if( i_match
+ 1 == i_startcode_length
)
485 *pi_offset
+= i_offset
- i_match
;
494 p_block
= p_block_backup
;
495 i_offset
= i_offset_backup
;
496 *pi_offset
= i_caller_offset_backup
;
502 *pi_offset
+= i_offset
;
505 *pi_offset
-= i_match
;
509 #endif /* VLC_BLOCK_HELPER_H */