1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Fiona Glaser <fiona@x264.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 *****************************************************************************/
35 int i_left; /* i_count number of available bits */
36 int i_bits_encoded; /* RD only */
39 static inline void bs_init( bs_t *s, void *p_data, int i_data )
41 int offset = ((intptr_t)p_data & (WORD_SIZE-1));
42 s->p = s->p_start = (uint8_t*)p_data - offset;
43 s->p_end = (uint8_t*)p_data + i_data;
44 s->i_left = offset ? 8*offset : (WORD_SIZE*8);
45 s->cur_bits = endian_fix( *(intptr_t*)s->p );
47 static inline int bs_pos( bs_t *s )
49 return( 8 * (s->p - s->p_start) + (WORD_SIZE*8) - s->i_left );
52 /* Write the rest of cur_bits to the bitstream; results in a bitstream no longer 32/64-bit aligned. */
53 static inline void bs_flush( bs_t *s )
55 *(intptr_t*)s->p = endian_fix( s->cur_bits << s->i_left );
56 s->p += WORD_SIZE - s->i_left / 8;
57 s->i_left = WORD_SIZE*8;
60 static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
64 s->cur_bits = (s->cur_bits << i_count) | i_bits;
68 *(uint32_t*)s->p = endian_fix( s->cur_bits << s->i_left );
75 if( i_count < s->i_left )
77 s->cur_bits = (s->cur_bits << i_count) | i_bits;
83 s->cur_bits = (s->cur_bits << s->i_left) | (i_bits >> i_count);
84 *(uint32_t*)s->p = endian_fix( s->cur_bits );
87 s->i_left = 32 - i_count;
92 /* Special case to eliminate branch in normal bs_write. */
93 /* Golomb never writes an even-size code, so this is only used in slice headers. */
94 static inline void bs_write32( bs_t *s, uint32_t i_bits )
96 bs_write( s, 16, i_bits >> 16 );
97 bs_write( s, 16, i_bits );
100 static inline void bs_write1( bs_t *s, uint32_t i_bit )
103 s->cur_bits |= i_bit;
105 if( s->i_left == WORD_SIZE*8-32 )
107 *(uint32_t*)s->p = endian_fix32( s->cur_bits );
109 s->i_left = WORD_SIZE*8;
113 static inline void bs_align_0( bs_t *s )
117 s->cur_bits <<= s->i_left&7;
122 static inline void bs_align_1( bs_t *s )
126 s->cur_bits <<= s->i_left&7;
127 s->cur_bits |= (1 << (s->i_left&7)) - 1;
133 /* golomb functions */
135 static const uint8_t x264_ue_size_tab[256] =
137 1, 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,
138 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
139 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
140 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
141 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
142 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
143 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
144 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
145 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
146 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
147 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
148 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
149 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
150 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
151 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
152 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
155 static inline void bs_write_ue_big( bs_t *s, unsigned int val )
169 size += x264_ue_size_tab[tmp];
170 bs_write( s, size, val );
173 /* Only works on values under 255. */
174 static inline void bs_write_ue( bs_t *s, int val )
176 bs_write( s, x264_ue_size_tab[val+1], val+1 );
179 static inline void bs_write_se( bs_t *s, int val )
182 int tmp = val = val <= 0 ? -val*2+1 : val*2;
188 size += x264_ue_size_tab[tmp];
189 bs_write( s, size, val );
192 static inline void bs_write_te( bs_t *s, int x, int val )
195 bs_write1( s, 1^val );
197 bs_write_ue( s, val );
200 static inline void bs_rbsp_trailing( bs_t *s )
206 static inline int bs_size_ue( unsigned int val )
208 return x264_ue_size_tab[val+1];
211 static inline int bs_size_ue_big( unsigned int val )
214 return x264_ue_size_tab[val+1];
216 return x264_ue_size_tab[(val+1)>>8] + 16;
219 static inline int bs_size_se( int val )
221 return bs_size_ue_big( val <= 0 ? -val*2 : val*2-1 );
224 static inline int bs_size_te( int x, int val )
229 return x264_ue_size_tab[val+1];