use lookup tables instead of actual exp/pow for AQ
[SFUResearch.git] / common / bs.h
blob7ec1e104533077ca054a362a8cc252525b3bc554
1 /*****************************************************************************
2 * bs.h :
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Jason Garrett-Glaser <darkshikari@gmail.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 *****************************************************************************/
25 #ifndef X264_BS_H
26 #define X264_BS_H
28 typedef struct
30 uint8_t i_bits;
31 uint8_t i_size;
32 } vlc_t;
34 typedef struct
36 uint16_t i_bits;
37 uint8_t i_size;
38 /* Next level table to use */
39 uint8_t i_next;
40 } vlc_large_t;
42 typedef struct bs_s
44 uint8_t *p_start;
45 uint8_t *p;
46 uint8_t *p_end;
48 intptr_t cur_bits;
49 int i_left; /* i_count number of available bits */
50 int i_bits_encoded; /* RD only */
51 } bs_t;
53 extern const vlc_t x264_coeff_token[5][17*4];
54 extern const vlc_t x264_total_zeros[15][16];
55 extern const vlc_t x264_total_zeros_dc[3][4];
56 extern const vlc_t x264_run_before[7][15];
58 /* A larger level table size theoretically could help a bit at extremely
59 * high bitrates, but the cost in cache is usually too high for it to be
60 * useful.
61 * This size appears to be optimal for QP18 encoding on a Nehalem CPU.
62 * FIXME: Do further testing? */
63 #define LEVEL_TABLE_SIZE 128
64 extern vlc_large_t x264_level_token[7][LEVEL_TABLE_SIZE];
66 static inline void bs_init( bs_t *s, void *p_data, int i_data )
68 int offset = ((intptr_t)p_data & (WORD_SIZE-1));
69 s->p = s->p_start = (uint8_t*)p_data - offset;
70 s->p_end = (uint8_t*)p_data + i_data;
71 s->i_left = offset ? 8*offset : (WORD_SIZE*8);
72 s->cur_bits = endian_fix( *(intptr_t*)s->p );
74 static inline int bs_pos( bs_t *s )
76 return( 8 * (s->p - s->p_start) + (WORD_SIZE*8) - s->i_left );
79 /* Write the rest of cur_bits to the bitstream; results in a bitstream no longer 32/64-bit aligned. */
80 static inline void bs_flush( bs_t *s )
82 *(intptr_t*)s->p = endian_fix( s->cur_bits << s->i_left );
83 s->p += WORD_SIZE - s->i_left / 8;
84 s->i_left = WORD_SIZE*8;
87 static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
89 if( WORD_SIZE == 8 )
91 s->cur_bits = (s->cur_bits << i_count) | i_bits;
92 s->i_left -= i_count;
93 if( s->i_left <= 32 )
95 #ifdef WORDS_BIGENDIAN
96 *(uint32_t*)s->p = s->cur_bits >> (32 - s->i_left);
97 #else
98 *(uint32_t*)s->p = endian_fix( s->cur_bits << s->i_left );
99 #endif
100 s->i_left += 32;
101 s->p += 4;
104 else
106 if( i_count < s->i_left )
108 s->cur_bits = (s->cur_bits << i_count) | i_bits;
109 s->i_left -= i_count;
111 else
113 i_count -= s->i_left;
114 s->cur_bits = (s->cur_bits << s->i_left) | (i_bits >> i_count);
115 *(uint32_t*)s->p = endian_fix( s->cur_bits );
116 s->p += 4;
117 s->cur_bits = i_bits;
118 s->i_left = 32 - i_count;
123 /* Special case to eliminate branch in normal bs_write. */
124 /* Golomb never writes an even-size code, so this is only used in slice headers. */
125 static inline void bs_write32( bs_t *s, uint32_t i_bits )
127 bs_write( s, 16, i_bits >> 16 );
128 bs_write( s, 16, i_bits );
131 static inline void bs_write1( bs_t *s, uint32_t i_bit )
133 s->cur_bits <<= 1;
134 s->cur_bits |= i_bit;
135 s->i_left--;
136 if( s->i_left == WORD_SIZE*8-32 )
138 *(uint32_t*)s->p = endian_fix32( s->cur_bits );
139 s->p += 4;
140 s->i_left = WORD_SIZE*8;
144 static inline void bs_align_0( bs_t *s )
146 if( s->i_left&7 )
148 s->cur_bits <<= s->i_left&7;
149 s->i_left &= ~7;
151 bs_flush( s );
153 static inline void bs_align_1( bs_t *s )
155 if( s->i_left&7 )
157 s->cur_bits <<= s->i_left&7;
158 s->cur_bits |= (1 << (s->i_left&7)) - 1;
159 s->i_left &= ~7;
161 bs_flush( s );
164 /* golomb functions */
166 static const uint8_t x264_ue_size_tab[256] =
168 1, 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,
169 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
170 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
171 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
172 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
173 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
174 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
175 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
176 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
177 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
178 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
179 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
180 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
181 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
182 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
183 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
186 static inline void bs_write_ue_big( bs_t *s, unsigned int val )
188 int size = 0;
189 int tmp = ++val;
190 if( tmp >= 0x10000 )
192 size = 32;
193 tmp >>= 16;
195 if( tmp >= 0x100 )
197 size += 16;
198 tmp >>= 8;
200 size += x264_ue_size_tab[tmp];
201 bs_write( s, size>>1, 0 );
202 bs_write( s, (size>>1)+1, val );
205 /* Only works on values under 255. */
206 static inline void bs_write_ue( bs_t *s, int val )
208 bs_write( s, x264_ue_size_tab[val+1], val+1 );
211 static inline void bs_write_se( bs_t *s, int val )
213 int size = 0;
214 int tmp = val = val <= 0 ? -val*2+1 : val*2;
215 if( tmp >= 0x100 )
217 size = 16;
218 tmp >>= 8;
220 size += x264_ue_size_tab[tmp];
221 bs_write( s, size, val );
224 static inline void bs_write_te( bs_t *s, int x, int val )
226 if( x == 1 )
227 bs_write1( s, 1^val );
228 else if( x > 1 )
229 bs_write_ue( s, val );
232 static inline void bs_rbsp_trailing( bs_t *s )
234 bs_write1( s, 1 );
235 bs_flush( s );
238 static inline int bs_size_ue( unsigned int val )
240 return x264_ue_size_tab[val+1];
243 static inline int bs_size_ue_big( unsigned int val )
245 if( val < 255 )
246 return x264_ue_size_tab[val+1];
247 else
248 return x264_ue_size_tab[(val+1)>>8] + 16;
251 static inline int bs_size_se( int val )
253 return bs_size_ue_big( val <= 0 ? -val*2 : val*2-1 );
256 static inline int bs_size_te( int x, int val )
258 if( x == 1 )
259 return 1;
260 else if( x > 1 )
261 return x264_ue_size_tab[val+1];
262 else
263 return 0;
266 #endif