]> git.sesse.net Git - x264/blob - common/bitstream.h
MBAFF: CABAC encoding of skips
[x264] / common / bitstream.h
1 /*****************************************************************************
2  * bitstream.h: bitstream writing
3  *****************************************************************************
4  * Copyright (C) 2003-2011 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Fiona Glaser <fiona@x264.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #ifndef X264_BS_H
29 #define X264_BS_H
30
31 typedef struct
32 {
33     uint8_t i_bits;
34     uint8_t i_size;
35 } vlc_t;
36
37 typedef struct
38 {
39     uint16_t i_bits;
40     uint8_t  i_size;
41     /* Next level table to use */
42     uint8_t  i_next;
43 } vlc_large_t;
44
45 typedef struct bs_s
46 {
47     uint8_t *p_start;
48     uint8_t *p;
49     uint8_t *p_end;
50
51     intptr_t cur_bits;
52     int     i_left;    /* i_count number of available bits */
53     int     i_bits_encoded; /* RD only */
54 } bs_t;
55
56 typedef struct
57 {
58     int     last;
59     dctcoef level[16];
60     uint8_t run[16];
61 } x264_run_level_t;
62
63 extern const vlc_t x264_coeff0_token[5];
64 extern const vlc_t x264_coeff_token[5][16][4];
65 extern const vlc_t x264_total_zeros[15][16];
66 extern const vlc_t x264_total_zeros_dc[3][4];
67 extern const vlc_t x264_run_before[7][16];
68
69 typedef struct
70 {
71     uint8_t *(*nal_escape) ( uint8_t *dst, uint8_t *src, uint8_t *end );
72 } x264_bitstream_function_t;
73
74 void x264_bitstream_init( int cpu, x264_bitstream_function_t *pf );
75
76 /* A larger level table size theoretically could help a bit at extremely
77  * high bitrates, but the cost in cache is usually too high for it to be
78  * useful.
79  * This size appears to be optimal for QP18 encoding on a Nehalem CPU.
80  * FIXME: Do further testing? */
81 #define LEVEL_TABLE_SIZE 128
82 extern vlc_large_t x264_level_token[7][LEVEL_TABLE_SIZE];
83
84 static inline void bs_init( bs_t *s, void *p_data, int i_data )
85 {
86     int offset = ((intptr_t)p_data & 3);
87     s->p       = s->p_start = (uint8_t*)p_data - offset;
88     s->p_end   = (uint8_t*)p_data + i_data;
89     s->i_left  = (WORD_SIZE - offset)*8;
90     s->cur_bits = endian_fix32( M32(s->p) );
91     s->cur_bits >>= (4-offset)*8;
92 }
93 static inline int bs_pos( bs_t *s )
94 {
95     return( 8 * (s->p - s->p_start) + (WORD_SIZE*8) - s->i_left );
96 }
97
98 /* Write the rest of cur_bits to the bitstream; results in a bitstream no longer 32-bit aligned. */
99 static inline void bs_flush( bs_t *s )
100 {
101     M32( s->p ) = endian_fix32( s->cur_bits << (s->i_left&31) );
102     s->p += WORD_SIZE - (s->i_left >> 3);
103     s->i_left = WORD_SIZE*8;
104 }
105 /* The inverse of bs_flush: prepare the bitstream to be written to again. */
106 static inline void bs_realign( bs_t *s )
107 {
108     int offset = ((intptr_t)s->p & 3);
109     if( offset )
110     {
111         s->p       = (uint8_t*)s->p - offset;
112         s->i_left  = (WORD_SIZE - offset)*8;
113         s->cur_bits = endian_fix32( M32(s->p) );
114         s->cur_bits >>= (4-offset)*8;
115     }
116 }
117
118 static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
119 {
120     if( WORD_SIZE == 8 )
121     {
122         s->cur_bits = (s->cur_bits << i_count) | i_bits;
123         s->i_left -= i_count;
124         if( s->i_left <= 32 )
125         {
126 #if WORDS_BIGENDIAN
127             M32( s->p ) = s->cur_bits >> (32 - s->i_left);
128 #else
129             M32( s->p ) = endian_fix( s->cur_bits << s->i_left );
130 #endif
131             s->i_left += 32;
132             s->p += 4;
133         }
134     }
135     else
136     {
137         if( i_count < s->i_left )
138         {
139             s->cur_bits = (s->cur_bits << i_count) | i_bits;
140             s->i_left -= i_count;
141         }
142         else
143         {
144             i_count -= s->i_left;
145             s->cur_bits = (s->cur_bits << s->i_left) | (i_bits >> i_count);
146             M32( s->p ) = endian_fix( s->cur_bits );
147             s->p += 4;
148             s->cur_bits = i_bits;
149             s->i_left = 32 - i_count;
150         }
151     }
152 }
153
154 /* Special case to eliminate branch in normal bs_write. */
155 /* Golomb never writes an even-size code, so this is only used in slice headers. */
156 static inline void bs_write32( bs_t *s, uint32_t i_bits )
157 {
158     bs_write( s, 16, i_bits >> 16 );
159     bs_write( s, 16, i_bits );
160 }
161
162 static inline void bs_write1( bs_t *s, uint32_t i_bit )
163 {
164     s->cur_bits <<= 1;
165     s->cur_bits |= i_bit;
166     s->i_left--;
167     if( s->i_left == WORD_SIZE*8-32 )
168     {
169         M32( s->p ) = endian_fix32( s->cur_bits );
170         s->p += 4;
171         s->i_left = WORD_SIZE*8;
172     }
173 }
174
175 static inline void bs_align_0( bs_t *s )
176 {
177     bs_write( s, s->i_left&7, 0 );
178     bs_flush( s );
179 }
180 static inline void bs_align_1( bs_t *s )
181 {
182     bs_write( s, s->i_left&7, (1 << (s->i_left&7)) - 1 );
183     bs_flush( s );
184 }
185 static inline void bs_align_10( bs_t *s )
186 {
187     if( s->i_left&7 )
188         bs_write( s, s->i_left&7, 1 << ( (s->i_left&7) - 1 ) );
189 }
190
191 /* golomb functions */
192
193 static const uint8_t x264_ue_size_tab[256] =
194 {
195      1, 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,
196      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
197     11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
198     11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
199     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
200     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
201     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
202     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
203     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
204     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
205     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
206     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
207     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
208     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
209     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
210     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
211 };
212
213 static inline void bs_write_ue_big( bs_t *s, unsigned int val )
214 {
215     int size = 0;
216     int tmp = ++val;
217     if( tmp >= 0x10000 )
218     {
219         size = 32;
220         tmp >>= 16;
221     }
222     if( tmp >= 0x100 )
223     {
224         size += 16;
225         tmp >>= 8;
226     }
227     size += x264_ue_size_tab[tmp];
228     bs_write( s, size>>1, 0 );
229     bs_write( s, (size>>1)+1, val );
230 }
231
232 /* Only works on values under 255. */
233 static inline void bs_write_ue( bs_t *s, int val )
234 {
235     bs_write( s, x264_ue_size_tab[val+1], val+1 );
236 }
237
238 static inline void bs_write_se( bs_t *s, int val )
239 {
240     int size = 0;
241     /* Faster than (val <= 0 ? -val*2+1 : val*2) */
242     /* 4 instructions on x86, 3 on ARM */
243     int tmp = 1 - val*2;
244     if( tmp < 0 ) tmp = val*2;
245     val = tmp;
246
247     if( tmp >= 0x100 )
248     {
249         size = 16;
250         tmp >>= 8;
251     }
252     size += x264_ue_size_tab[tmp];
253     bs_write( s, size, val );
254 }
255
256 static inline void bs_write_te( bs_t *s, int x, int val )
257 {
258     if( x == 1 )
259         bs_write1( s, 1^val );
260     else //if( x > 1 )
261         bs_write_ue( s, val );
262 }
263
264 static inline void bs_rbsp_trailing( bs_t *s )
265 {
266     bs_write1( s, 1 );
267     bs_write( s, s->i_left&7, 0  );
268 }
269
270 static ALWAYS_INLINE int bs_size_ue( unsigned int val )
271 {
272     return x264_ue_size_tab[val+1];
273 }
274
275 static ALWAYS_INLINE int bs_size_ue_big( unsigned int val )
276 {
277     if( val < 255 )
278         return x264_ue_size_tab[val+1];
279     else
280         return x264_ue_size_tab[(val+1)>>8] + 16;
281 }
282
283 static ALWAYS_INLINE int bs_size_se( int val )
284 {
285     int tmp = 1 - val*2;
286     if( tmp < 0 ) tmp = val*2;
287     if( tmp < 256 )
288         return x264_ue_size_tab[tmp];
289     else
290         return x264_ue_size_tab[tmp>>8]+16;
291 }
292
293 static ALWAYS_INLINE int bs_size_te( int x, int val )
294 {
295     if( x == 1 )
296         return 1;
297     else //if( x > 1 )
298         return x264_ue_size_tab[val+1];
299 }
300
301 #endif