1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar <fenrir@via.ecp.fr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *****************************************************************************/
30 int i_left; /* i_count number of available bits */
31 int i_bits_encoded; /* RD only */
34 static inline void bs_init( bs_t *s, void *p_data, int i_data )
38 s->p_end = s->p + i_data;
41 static inline int bs_pos( bs_t *s )
43 return( 8 * ( s->p - s->p_start ) + 8 - s->i_left );
46 static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
51 i_bits &= (1<<i_count)-1;
52 if( i_count < s->i_left )
54 *s->p = (*s->p << i_count) | i_bits;
60 *s->p = (*s->p << s->i_left) | (i_bits >> (i_count - s->i_left));
68 static inline void bs_write1( bs_t *s, uint32_t i_bit )
80 static inline void bs_align_0( bs_t *s )
89 static inline void bs_align_1( bs_t *s )
94 *s->p |= (1 << s->i_left) - 1;
99 static inline void bs_align( bs_t *s )
106 /* golomb functions */
108 static inline void bs_write_ue( bs_t *s, unsigned int val )
111 static const uint8_t i_size0_255[256] =
113 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,
114 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,
115 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,
116 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,
117 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,
118 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,
119 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,
120 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
129 unsigned int tmp = ++val;
131 if( tmp >= 0x00010000 )
141 i_size += i_size0_255[tmp];
143 bs_write( s, 2 * i_size - 1, val );
147 static inline void bs_write_se( bs_t *s, int val )
149 bs_write_ue( s, val <= 0 ? -val * 2 : val * 2 - 1);
152 static inline void bs_write_te( bs_t *s, int x, int val )
156 bs_write1( s, 1&~val );
160 bs_write_ue( s, val );
164 static inline void bs_rbsp_trailing( bs_t *s )
169 bs_write( s, s->i_left, 0x00 );
173 static inline int bs_size_ue( unsigned int val )
175 static const uint8_t i_size0_254[255] =
177 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,
178 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
179 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
180 11,11,11,11,11,11,11,11,11,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
181 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
182 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
183 13,13,13,13,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
184 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
185 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
186 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
187 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
188 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
193 return i_size0_254[val];
204 val = (val >> 16) - 1;
209 val = (val >> 8) - 1;
211 return i_size0_254[val] + i_size;
215 static inline int bs_size_se( int val )
217 return bs_size_ue( val <= 0 ? -val * 2 : val * 2 - 1);
220 static inline int bs_size_te( int x, int val )
228 return bs_size_ue( val );