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 *****************************************************************************/
38 /* Next level table to use */
49 int i_left; /* i_count number of available bits */
50 int i_bits_encoded; /* RD only */
60 extern const vlc_t x264_coeff0_token[5];
61 extern const vlc_t x264_coeff_token[5][16][4];
62 extern const vlc_t x264_total_zeros[15][16];
63 extern const vlc_t x264_total_zeros_dc[3][4];
64 extern const vlc_t x264_run_before[7][16];
66 /* A larger level table size theoretically could help a bit at extremely
67 * high bitrates, but the cost in cache is usually too high for it to be
69 * This size appears to be optimal for QP18 encoding on a Nehalem CPU.
70 * FIXME: Do further testing? */
71 #define LEVEL_TABLE_SIZE 128
72 extern vlc_large_t x264_level_token[7][LEVEL_TABLE_SIZE];
74 static inline void bs_init( bs_t *s, void *p_data, int i_data )
76 int offset = ((intptr_t)p_data & 3);
77 s->p = s->p_start = (uint8_t*)p_data - offset;
78 s->p_end = (uint8_t*)p_data + i_data;
79 s->i_left = (WORD_SIZE - offset)*8;
80 s->cur_bits = endian_fix32( M32(s->p) );
81 s->cur_bits >>= (4-offset)*8;
83 static inline int bs_pos( bs_t *s )
85 return( 8 * (s->p - s->p_start) + (WORD_SIZE*8) - s->i_left );
88 /* Write the rest of cur_bits to the bitstream; results in a bitstream no longer 32-bit aligned. */
89 static inline void bs_flush( bs_t *s )
91 M32( s->p ) = endian_fix32( s->cur_bits << (s->i_left&31) );
92 s->p += WORD_SIZE - s->i_left / 8;
93 s->i_left = WORD_SIZE*8;
95 /* The inverse of bs_flush: prepare the bitstream to be written to again. */
96 static inline void bs_realign( bs_t *s )
98 int offset = ((intptr_t)s->p & 3);
101 s->p = (uint8_t*)s->p - offset;
102 s->i_left = (WORD_SIZE - offset)*8;
103 s->cur_bits = endian_fix32( M32(s->p) );
104 s->cur_bits >>= (4-offset)*8;
108 static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
112 s->cur_bits = (s->cur_bits << i_count) | i_bits;
113 s->i_left -= i_count;
114 if( s->i_left <= 32 )
116 #ifdef WORDS_BIGENDIAN
117 M32( s->p ) = s->cur_bits >> (32 - s->i_left);
119 M32( s->p ) = endian_fix( s->cur_bits << s->i_left );
127 if( i_count < s->i_left )
129 s->cur_bits = (s->cur_bits << i_count) | i_bits;
130 s->i_left -= i_count;
134 i_count -= s->i_left;
135 s->cur_bits = (s->cur_bits << s->i_left) | (i_bits >> i_count);
136 M32( s->p ) = endian_fix( s->cur_bits );
138 s->cur_bits = i_bits;
139 s->i_left = 32 - i_count;
144 /* Special case to eliminate branch in normal bs_write. */
145 /* Golomb never writes an even-size code, so this is only used in slice headers. */
146 static inline void bs_write32( bs_t *s, uint32_t i_bits )
148 bs_write( s, 16, i_bits >> 16 );
149 bs_write( s, 16, i_bits );
152 static inline void bs_write1( bs_t *s, uint32_t i_bit )
155 s->cur_bits |= i_bit;
157 if( s->i_left == WORD_SIZE*8-32 )
159 M32( s->p ) = endian_fix32( s->cur_bits );
161 s->i_left = WORD_SIZE*8;
165 static inline void bs_align_0( bs_t *s )
167 bs_write( s, s->i_left&7, 0 );
170 static inline void bs_align_1( bs_t *s )
172 bs_write( s, s->i_left&7, (1 << (s->i_left&7)) - 1 );
175 static inline void bs_align_10( bs_t *s )
178 bs_write( s, s->i_left&7, 1 << ( (s->i_left&7) - 1 ) );
181 /* golomb functions */
183 static const uint8_t x264_ue_size_tab[256] =
185 1, 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,
188 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
189 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,
191 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
192 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
193 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,
195 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,
197 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
198 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
199 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
200 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
203 static inline void bs_write_ue_big( bs_t *s, unsigned int val )
217 size += x264_ue_size_tab[tmp];
218 bs_write( s, size>>1, 0 );
219 bs_write( s, (size>>1)+1, val );
222 /* Only works on values under 255. */
223 static inline void bs_write_ue( bs_t *s, int val )
225 bs_write( s, x264_ue_size_tab[val+1], val+1 );
228 static inline void bs_write_se( bs_t *s, int val )
231 /* Faster than (val <= 0 ? -val*2+1 : val*2) */
232 /* 4 instructions on x86, 3 on ARM */
234 if( tmp < 0 ) tmp = val*2;
242 size += x264_ue_size_tab[tmp];
243 bs_write( s, size, val );
246 static inline void bs_write_te( bs_t *s, int x, int val )
249 bs_write1( s, 1^val );
251 bs_write_ue( s, val );
254 static inline void bs_rbsp_trailing( bs_t *s )
257 bs_write( s, s->i_left&7, 0 );
260 static ALWAYS_INLINE int bs_size_ue( unsigned int val )
262 return x264_ue_size_tab[val+1];
265 static ALWAYS_INLINE int bs_size_ue_big( unsigned int val )
268 return x264_ue_size_tab[val+1];
270 return x264_ue_size_tab[(val+1)>>8] + 16;
273 static ALWAYS_INLINE int bs_size_se( int val )
276 if( tmp < 0 ) tmp = val*2;
278 return x264_ue_size_tab[tmp];
280 return x264_ue_size_tab[tmp>>8]+16;
283 static ALWAYS_INLINE int bs_size_te( int x, int val )
288 return x264_ue_size_tab[val+1];