1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: bs.h,v 1.1 2004/06/03 19:27:06 fenrir Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
33 int i_left; /* i_count number of available bits */
34 int i_bits_encoded; /* RD only */
37 static inline void bs_init( bs_t *s, void *p_data, int i_data )
41 s->p_end = s->p + i_data;
44 static inline int bs_pos( bs_t *s )
46 return( 8 * ( s->p - s->p_start ) + 8 - s->i_left );
49 static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
51 if( s->p >= s->p_end - 4 )
56 i_bits &= (1<<i_count)-1;
57 if( i_count < s->i_left )
59 *s->p = (*s->p << i_count) | i_bits;
65 *s->p = (*s->p << s->i_left) | (i_bits >> (i_count - s->i_left));
73 static inline void bs_write1( bs_t *s, uint32_t i_bit )
88 static inline void bs_align_0( bs_t *s )
97 static inline void bs_align_1( bs_t *s )
102 *s->p |= (1 << s->i_left) - 1;
107 static inline void bs_align( bs_t *s )
114 /* golomb functions */
116 static inline void bs_write_ue( bs_t *s, unsigned int val )
119 static const uint8_t i_size0_255[256] =
121 1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
122 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
123 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
124 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
125 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
126 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
127 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
128 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
137 unsigned int tmp = ++val;
139 if( tmp >= 0x00010000 )
149 i_size += i_size0_255[tmp];
151 bs_write( s, 2 * i_size - 1, val );
155 static inline void bs_write_se( bs_t *s, int val )
157 bs_write_ue( s, val <= 0 ? -val * 2 : val * 2 - 1);
160 static inline void bs_write_te( bs_t *s, int x, int val )
164 bs_write1( s, 1&~val );
168 bs_write_ue( s, val );
172 static inline void bs_rbsp_trailing( bs_t *s )
177 bs_write( s, s->i_left, 0x00 );
181 static inline int bs_size_ue( unsigned int val )
183 static const uint8_t i_size0_254[255] =
185 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,
186 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
187 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
188 11,11,11,11,11,11,11,11,11,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
189 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
190 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
191 13,13,13,13,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
192 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
193 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
194 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
195 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
196 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
201 return i_size0_254[val];
212 val = (val >> 16) - 1;
217 val = (val >> 8) - 1;
219 return i_size0_254[val] + i_size;
223 static inline int bs_size_se( int val )
225 return bs_size_ue( val <= 0 ? -val * 2 : val * 2 - 1);
228 static inline int bs_size_te( int x, int val )
236 return bs_size_ue( val );