1 /*****************************************************************************
2 * vlc_bits.h : Bit handling helpers
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003, 2006, 2015 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin at videolan dot org>
9 * Rafaël Carré <funman at videolan dot org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
29 #include <vlc_block.h>
33 * This file defines functions, structures for handling streams of bits in vlc
48 ssize_t i_left; /* i_count number of available bits */
51 static inline void bs_init( bs_t *s, const void *p_data, size_t i_data )
53 s->p_start = (void *)p_data;
55 s->p_end = s->p_start + i_data;
59 static inline int bs_pos( const bs_t *s )
61 return( 8 * ( s->p - s->p_start ) + 8 - s->i_left );
64 static inline int bs_eof( const bs_t *s )
66 return( s->p >= s->p_end ? 1: 0 );
69 static inline uint32_t bs_read( bs_t *s, int i_count )
71 static const uint32_t i_mask[33] =
73 0x01, 0x03, 0x07, 0x0f,
74 0x1f, 0x3f, 0x7f, 0xff,
75 0x1ff, 0x3ff, 0x7ff, 0xfff,
76 0x1fff, 0x3fff, 0x7fff, 0xffff,
77 0x1ffff, 0x3ffff, 0x7ffff, 0xfffff,
78 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff,
79 0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff,
80 0x1fffffff,0x3fffffff,0x7fffffff,0xffffffff};
82 uint32_t i_result = 0;
86 if( s->p >= s->p_end )
91 if( ( i_shr = s->i_left - i_count ) >= 0 )
93 /* more in the buffer than requested */
94 i_result |= ( *s->p >> i_shr )&i_mask[i_count];
105 /* less in the buffer than requested */
106 i_result |= (*s->p&i_mask[s->i_left]) << -i_shr;
107 i_count -= s->i_left;
116 static inline uint32_t bs_read1( bs_t *s )
118 if( s->p < s->p_end )
120 unsigned int i_result;
123 i_result = ( *s->p >> s->i_left )&0x01;
135 static inline uint32_t bs_show( bs_t *s, int i_count )
138 return bs_read( &s_tmp, i_count );
141 static inline void bs_skip( bs_t *s, ssize_t i_count )
143 s->i_left -= i_count;
147 const int i_bytes = ( -s->i_left + 8 ) / 8;
150 s->i_left += 8 * i_bytes;
154 static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
158 if( s->p >= s->p_end )
165 if( ( i_bits >> i_count )&0x01 )
167 *s->p |= 1 << ( s->i_left - 1 );
171 *s->p &= ~( 1 << ( s->i_left - 1 ) );
182 static inline void bs_align( bs_t *s )
191 static inline void bs_align_0( bs_t *s )
195 bs_write( s, s->i_left, 0 );
199 static inline void bs_align_1( bs_t *s )
201 while( s->i_left != 8 )
207 static inline int bo_init(bo_t *p_bo, int i_size)
209 p_bo->b = block_Alloc(i_size);
213 p_bo->b->i_buffer = 0;
214 p_bo->basesize = i_size;
219 static inline void bo_set_8(bo_t *p_bo, size_t i_offset, uint8_t i)
221 size_t i_size = p_bo->b->i_size - (p_bo->b->p_buffer - p_bo->b->p_start);
222 if (i_offset >= i_size)
224 int i_growth = p_bo->basesize;
225 while(i_offset >= i_size + i_growth)
226 i_growth += p_bo->basesize;
228 int i = p_bo->b->i_buffer; /* Realloc would set payload size == buffer size */
229 p_bo->b = block_Realloc(p_bo->b, 0, i_size + i_growth);
232 p_bo->b->i_buffer = i;
234 p_bo->b->p_buffer[i_offset] = i;
237 static inline void bo_add_8(bo_t *p_bo, uint8_t i)
239 bo_set_8( p_bo, p_bo->b->i_buffer, i );
243 static inline void bo_add_16be(bo_t *p_bo, uint16_t i)
245 bo_add_8(p_bo, ((i >> 8) &0xff));
246 bo_add_8(p_bo, i &0xff);
249 static inline void bo_add_16le(bo_t *p_bo, uint16_t i)
251 bo_add_8(p_bo, i &0xff);
252 bo_add_8(p_bo, ((i >> 8) &0xff));
255 static inline void bo_set_16be(bo_t *p_bo, int i_offset, uint16_t i)
257 bo_set_8(p_bo, i_offset, ((i >> 8) &0xff));
258 bo_set_8(p_bo, i_offset + 1, i &0xff);
261 static inline void bo_set_16le(bo_t *p_bo, int i_offset, uint16_t i)
263 bo_set_8(p_bo, i_offset, i &0xff);
264 bo_set_8(p_bo, i_offset + 1, ((i >> 8) &0xff));
267 static inline void bo_add_24be(bo_t *p_bo, uint32_t i)
269 bo_add_8(p_bo, ((i >> 16) &0xff));
270 bo_add_8(p_bo, ((i >> 8) &0xff));
271 bo_add_8(p_bo, (i &0xff));
274 static inline void bo_add_32be(bo_t *p_bo, uint32_t i)
276 bo_add_16be(p_bo, ((i >> 16) &0xffff));
277 bo_add_16be(p_bo, i &0xffff);
280 static inline void bo_add_32le(bo_t *p_bo, uint32_t i)
282 bo_add_16le(p_bo, i &0xffff);
283 bo_add_16le(p_bo, ((i >> 16) &0xffff));
286 static inline void bo_set_32be(bo_t *p_bo, int i_offset, uint32_t i)
288 bo_set_16be(p_bo, i_offset, ((i >> 16) &0xffff));
289 bo_set_16be(p_bo, i_offset + 2, i &0xffff);
292 static inline void bo_set_32le(bo_t *p_bo, int i_offset, uint32_t i)
294 bo_set_16le(p_bo, i_offset, i &0xffff);
295 bo_set_16le(p_bo, i_offset + 2, ((i >> 16) &0xffff));
298 static inline void bo_swap_32be (bo_t *p_bo, int i_pos, uint32_t i)
300 p_bo->b->p_buffer[i_pos ] = (i >> 24)&0xff;
301 p_bo->b->p_buffer[i_pos + 1] = (i >> 16)&0xff;
302 p_bo->b->p_buffer[i_pos + 2] = (i >> 8)&0xff;
303 p_bo->b->p_buffer[i_pos + 3] = (i )&0xff;
306 static inline void bo_set_64be(bo_t *p_bo, int i_offset, uint64_t i)
308 bo_set_32be(p_bo, i_offset, ((i >> 32) &0xffffffff));
309 bo_set_32be(p_bo, i_offset + 4, i &0xffffffff);
312 static inline void bo_add_64be(bo_t *p_bo, uint64_t i)
314 bo_add_32be(p_bo, ((i >> 32) &0xffffffff));
315 bo_add_32be(p_bo, i &0xffffffff);
318 static inline void bo_add_64le(bo_t *p_bo, uint64_t i)
320 bo_add_32le(p_bo, i &0xffffffff);
321 bo_add_32le(p_bo, ((i >> 32) &0xffffffff));
324 static inline void bo_add_fourcc(bo_t *p_bo, const char *fcc)
326 bo_add_8(p_bo, fcc[0]);
327 bo_add_8(p_bo, fcc[1]);
328 bo_add_8(p_bo, fcc[2]);
329 bo_add_8(p_bo, fcc[3]);
332 static inline void bo_add_mem(bo_t *p_bo, int i_size, const uint8_t *p_mem)
334 for (int i = 0; i < i_size; i++)
335 bo_add_8(p_bo, p_mem[i]);