1 /*****************************************************************************
2 * vlc_block_helper.h: Helper functions for data blocks management.
3 *****************************************************************************
4 * Copyright (C) 2003 VLC authors and VideoLAN
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? */
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
);
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
);
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
);
81 while( block
!= NULL
&& block
->i_buffer
== p_bytestream
->i_offset
)
83 block_t
*p_next
= block
->p_next
;
85 block_Release( block
);
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
,
96 block_ChainAppend( &p_bytestream
->p_chain
, p_block
);
97 if( !p_bytestream
->p_block
) p_bytestream
->p_block
= p_block
;
101 static inline block_t
*block_BytestreamPop( block_bytestream_t
*p_bytestream
)
105 block_BytestreamFlush( p_bytestream
);
107 p_block
= p_bytestream
->p_block
;
108 if( p_block
== 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
;
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
;
131 static inline int block_SkipByte( block_bytestream_t
*p_bytestream
)
133 /* Most common case first */
134 if( p_bytestream
->p_block
->i_buffer
- p_bytestream
->i_offset
)
136 p_bytestream
->i_offset
++;
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
;
156 /* Not enough data, bail out */
160 static inline int block_PeekByte( block_bytestream_t
*p_bytestream
,
163 /* Most common case first */
164 if( p_bytestream
->p_block
->i_buffer
- p_bytestream
->i_offset
)
166 *p_data
= p_bytestream
->p_block
->p_buffer
[p_bytestream
->i_offset
];
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];
185 /* Not enough data, bail out */
189 static inline int block_GetByte( block_bytestream_t
*p_bytestream
,
192 /* Most common case first */
193 if( 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
++;
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
;
217 /* Not enough data, bail out */
221 static inline int block_WaitBytes( block_bytestream_t
*p_bytestream
,
225 size_t i_offset
, i_copy
, i_size
;
227 /* Check we have that much data */
228 i_offset
= p_bytestream
->i_offset
;
231 for( p_block
= p_bytestream
->p_block
;
232 p_block
!= NULL
; p_block
= p_block
->p_next
)
234 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
243 /* Not enough data, bail out */
249 static inline int block_SkipBytes( block_bytestream_t
*p_bytestream
,
253 size_t i_offset
, i_copy
;
255 /* Check we have that much data */
256 i_offset
= p_bytestream
->i_offset
;
258 for( p_block
= p_bytestream
->p_block
;
259 p_block
!= NULL
; p_block
= p_block
->p_next
)
261 i_copy
= __MIN( i_data
, p_block
->i_buffer
- i_offset
);
271 /* Not enough data, bail out */
275 p_bytestream
->p_block
= p_block
;
276 p_bytestream
->i_offset
= i_offset
+ i_copy
;
280 static inline int block_PeekBytes( block_bytestream_t
*p_bytestream
,
281 uint8_t *p_data
, size_t i_data
)
284 size_t i_offset
, i_copy
, i_size
;
286 /* Check we have that much data */
287 i_offset
= p_bytestream
->i_offset
;
290 for( p_block
= p_bytestream
->p_block
;
291 p_block
!= NULL
; p_block
= p_block
->p_next
)
293 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
302 /* Not enough data, bail out */
307 i_offset
= p_bytestream
->i_offset
;
310 for( p_block
= p_bytestream
->p_block
;
311 p_block
!= NULL
; p_block
= p_block
->p_next
)
313 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
318 memcpy( p_data
, p_block
->p_buffer
+ i_offset
, i_copy
);
330 static inline int block_GetBytes( block_bytestream_t
*p_bytestream
,
331 uint8_t *p_data
, size_t i_data
)
334 size_t i_offset
, i_copy
, i_size
;
336 /* Check we have that much data */
337 i_offset
= p_bytestream
->i_offset
;
340 for( p_block
= p_bytestream
->p_block
;
341 p_block
!= NULL
; p_block
= p_block
->p_next
)
343 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
352 /* Not enough data, bail out */
357 i_offset
= p_bytestream
->i_offset
;
360 for( p_block
= p_bytestream
->p_block
;
361 p_block
!= NULL
; p_block
= p_block
->p_next
)
363 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
368 memcpy( p_data
, p_block
->p_buffer
+ i_offset
, i_copy
);
377 p_bytestream
->p_block
= p_block
;
378 p_bytestream
->i_offset
= i_offset
+ i_copy
;
383 static inline int block_PeekOffsetBytes( block_bytestream_t
*p_bytestream
,
384 size_t i_peek_offset
, uint8_t *p_data
, size_t i_data
)
387 size_t i_offset
, i_copy
, i_size
;
389 /* Check we have that much data */
390 i_offset
= p_bytestream
->i_offset
;
391 i_size
= i_data
+ i_peek_offset
;
393 for( p_block
= p_bytestream
->p_block
;
394 p_block
!= NULL
; p_block
= p_block
->p_next
)
396 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
405 /* Not enough data, bail out */
409 /* Find the right place */
410 i_offset
= p_bytestream
->i_offset
;
411 i_size
= i_peek_offset
;
413 for( p_block
= p_bytestream
->p_block
;
414 p_block
!= NULL
; p_block
= p_block
->p_next
)
416 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
428 for( ; p_block
!= NULL
; p_block
= p_block
->p_next
)
430 i_copy
= __MIN( i_size
, p_block
->i_buffer
- i_offset
);
435 memcpy( p_data
, p_block
->p_buffer
+ i_offset
, i_copy
);
447 static inline int block_FindStartcodeFromOffset(
448 block_bytestream_t
*p_bytestream
, size_t *pi_offset
,
449 const uint8_t *p_startcode
, int i_startcode_length
)
451 block_t
*p_block
, *p_block_backup
= 0;
453 size_t i_offset
, i_offset_backup
= 0;
454 int i_caller_offset_backup
= 0, i_match
;
456 /* Find the right place */
457 i_size
= *pi_offset
+ p_bytestream
->i_offset
;
458 for( p_block
= p_bytestream
->p_block
;
459 p_block
!= NULL
; p_block
= p_block
->p_next
)
461 i_size
-= p_block
->i_buffer
;
462 if( i_size
< 0 ) break;
467 /* Not enough data, bail out */
472 * We first look for an occurrence of the 1st startcode byte and
473 * if found, we do a more thorough check. */
474 i_size
+= p_block
->i_buffer
;
475 *pi_offset
-= i_size
;
477 for( ; p_block
!= NULL
; p_block
= p_block
->p_next
)
479 for( i_offset
= i_size
; i_offset
< p_block
->i_buffer
; i_offset
++ )
481 if( p_block
->p_buffer
[i_offset
] == p_startcode
[i_match
] )
485 p_block_backup
= p_block
;
486 i_offset_backup
= i_offset
;
487 i_caller_offset_backup
= *pi_offset
;
490 if( i_match
+ 1 == i_startcode_length
)
493 *pi_offset
+= i_offset
- i_match
;
502 p_block
= p_block_backup
;
503 i_offset
= i_offset_backup
;
504 *pi_offset
= i_caller_offset_backup
;
510 *pi_offset
+= i_offset
;
513 *pi_offset
-= i_match
;
517 #endif /* VLC_BLOCK_HELPER_H */