1 /*****************************************************************************
2 * bits.h : Bit handling helpers
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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 *****************************************************************************/
29 * This file defines functions, structures for handling streams of bits in vlc
34 const uint8_t *p_start;
38 unsigned i_left; /* i_count number of available bits */
41 typedef struct bs_writable_s
47 unsigned i_left; /* i_count number of available bits */
51 static inline void bs_init( bs_t *s, const void *p_data, unsigned i_data )
55 s->p_end = s->p + i_data;
59 static inline int bs_pos( bs_t *s )
61 return( 8 * ( s->p - s->p_start ) + 8 - s->i_left );
64 static inline int bs_eof( 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, int 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;
155 * Writable bits stream
157 static inline void bsw_init_writable( bsw_t *s, void *p_data, unsigned i_data )
161 s->p_end = s->p + i_data;
165 static inline bs_t * bs_from_writable( bsw_t *s )
170 static inline void bsw_skip( bsw_t *s, int count )
172 return bs_skip(bs_from_writable(s), count);
175 static inline uint32_t bsw_show( bsw_t *s, int count )
177 return bs_show(bs_from_writable(s), count);
180 static inline uint32_t bsw_read1( bsw_t *s )
182 return bs_read1(bs_from_writable(s));
185 static inline uint32_t bsw_read( bsw_t *s, int count )
187 return bs_read(bs_from_writable(s), count);
190 static inline int bsw_pos( bsw_t *s )
192 return bs_pos(bs_from_writable(s));
195 static inline int bsw_eof( bsw_t *s )
197 return bs_eof(bs_from_writable(s));
201 static inline void bsw_write( bsw_t *s, int i_count, uint32_t i_bits )
205 if( s->p >= s->p_end )
210 if( ( i_bits >> i_count )&0x01 )
211 *s->p |= 1 << ( s->i_left - 1 );
213 *s->p &= ~( 1 << ( s->i_left - 1 ) );
224 static inline void bsw_align( bsw_t *s )
233 static inline void bsw_align_0( bsw_t *s )
236 bsw_write( s, s->i_left, 0 );
239 static inline void bsw_align_1( bsw_t *s )
241 while( s->i_left != 8 )
242 bsw_write( s, 1, 1 );