]> git.sesse.net Git - vlc/blob - include/vlc_bits.h
vlc_bits: Differentiate between writable bits stream and read only.
[vlc] / include / vlc_bits.h
1 /*****************************************************************************
2  * bits.h : Bit handling helpers
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifndef VLC_BITS_H
25 #define VLC_BITS_H 1
26
27 /**
28  * \file
29  * This file defines functions, structures for handling streams of bits in vlc
30  */
31
32 typedef struct bs_s
33 {
34     const uint8_t *p_start;
35     const uint8_t *p;
36     const uint8_t *p_end;
37
38     unsigned     i_left;    /* i_count number of available bits */
39 } bs_t;
40
41 typedef struct bs_writable_s
42 {
43     uint8_t *p_start;
44     uint8_t *p;
45     uint8_t *p_end;
46     
47     unsigned     i_left;    /* i_count number of available bits */
48 } bsw_t;
49
50
51 static inline void bs_init( bs_t *s, const void *p_data, unsigned i_data )
52 {
53     s->p_start = p_data;
54     s->p       = p_data;
55     s->p_end   = s->p + i_data;
56     s->i_left  = 8;
57 }
58
59 static inline int bs_pos( bs_t *s )
60 {
61     return( 8 * ( s->p - s->p_start ) + 8 - s->i_left );
62 }
63
64 static inline int bs_eof( bs_t *s )
65 {
66     return( s->p >= s->p_end ? 1: 0 );
67 }
68
69 static inline uint32_t bs_read( bs_t *s, int i_count )
70 {
71      static const uint32_t i_mask[33] =
72      {  0x00,
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};
81     int      i_shr;
82     uint32_t i_result = 0;
83
84     while( i_count > 0 )
85     {
86         if( s->p >= s->p_end )
87         {
88             break;
89         }
90
91         if( ( i_shr = s->i_left - i_count ) >= 0 )
92         {
93             /* more in the buffer than requested */
94             i_result |= ( *s->p >> i_shr )&i_mask[i_count];
95             s->i_left -= i_count;
96             if( s->i_left == 0 )
97             {
98                 s->p++;
99                 s->i_left = 8;
100             }
101             return( i_result );
102         }
103         else
104         {
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;
108            s->p++;
109            s->i_left = 8;
110         }
111     }
112
113     return( i_result );
114 }
115
116 static inline uint32_t bs_read1( bs_t *s )
117 {
118     if( s->p < s->p_end )
119     {
120         unsigned int i_result;
121
122         s->i_left--;
123         i_result = ( *s->p >> s->i_left )&0x01;
124         if( s->i_left == 0 )
125         {
126             s->p++;
127             s->i_left = 8;
128         }
129         return i_result;
130     }
131
132     return 0;
133 }
134
135 static inline uint32_t bs_show( bs_t *s, int i_count )
136 {
137     bs_t     s_tmp = *s;
138     return bs_read( &s_tmp, i_count );
139 }
140
141 static inline void bs_skip( bs_t *s, int i_count )
142 {
143     s->i_left -= i_count;
144
145     if( s->i_left <= 0 )
146     {
147         const int i_bytes = ( -s->i_left + 8 ) / 8;
148
149         s->p += i_bytes;
150         s->i_left += 8 * i_bytes;
151     }
152 }
153
154 /*
155  * Writable bits stream
156  */
157 static inline void bsw_init_writable( bsw_t *s, void *p_data, unsigned i_data )
158 {
159     s->p_start = p_data;
160     s->p       = p_data;
161     s->p_end   = s->p + i_data;
162     s->i_left  = 8;
163 }
164
165 static inline bs_t * bs_from_writable( bsw_t *s )
166 {
167     return (bs_t *)s;
168 }
169
170 static inline void bsw_skip( bsw_t *s, int count )
171 {
172     return bs_skip(bs_from_writable(s), count);
173 }
174
175 static inline uint32_t bsw_show( bsw_t *s, int count )
176 {
177     return bs_show(bs_from_writable(s), count);
178 }
179
180 static inline uint32_t bsw_read1( bsw_t *s )
181 {
182     return bs_read1(bs_from_writable(s));
183 }
184
185 static inline uint32_t bsw_read( bsw_t *s, int count )
186 {
187     return bs_read(bs_from_writable(s), count);
188 }
189
190 static inline int bsw_pos( bsw_t *s )
191 {
192     return bs_pos(bs_from_writable(s));
193 }
194
195 static inline int bsw_eof( bsw_t *s )
196 {
197     return bs_eof(bs_from_writable(s));
198 }
199
200
201 static inline void bsw_write( bsw_t *s, int i_count, uint32_t i_bits )
202 {
203     while( i_count > 0 )
204     {
205         if( s->p >= s->p_end )
206             break;
207
208         i_count--;
209
210         if( ( i_bits >> i_count )&0x01 )
211             *s->p |= 1 << ( s->i_left - 1 );
212         else
213             *s->p &= ~( 1 << ( s->i_left - 1 ) );
214
215         s->i_left--;
216         if( s->i_left == 0 )
217         {
218             s->p++;
219             s->i_left = 8;
220         }
221     }
222 }
223
224 static inline void bsw_align( bsw_t *s )
225 {
226     if( s->i_left != 8 )
227     {
228         s->i_left = 8;
229         s->p++;
230     }
231 }
232
233 static inline void bsw_align_0( bsw_t *s )
234 {
235     if( s->i_left != 8 )
236         bsw_write( s, s->i_left, 0 );
237 }
238
239 static inline void bsw_align_1( bsw_t *s )
240 {
241     while( s->i_left != 8 )
242         bsw_write( s, 1, 1 );
243 }
244
245 #endif