]> git.sesse.net Git - x264/blob - common/bitstream.h
High bit depth intra_sad_x3_4x4
[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     uintptr_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     int     mask;
60     dctcoef level[16];
61     uint8_t run[16];
62 } x264_run_level_t;
63
64 extern const vlc_t x264_coeff0_token[6];
65 extern const vlc_t x264_coeff_token[6][16][4];
66 extern const vlc_t x264_total_zeros[15][16];
67 extern const vlc_t x264_total_zeros_2x2_dc[3][4];
68 extern const vlc_t x264_total_zeros_2x4_dc[7][8];
69
70 typedef struct
71 {
72     uint8_t *(*nal_escape) ( uint8_t *dst, uint8_t *src, uint8_t *end );
73 } x264_bitstream_function_t;
74
75 void x264_bitstream_init( int cpu, x264_bitstream_function_t *pf );
76
77 /* A larger level table size theoretically could help a bit at extremely
78  * high bitrates, but the cost in cache is usually too high for it to be
79  * useful.
80  * This size appears to be optimal for QP18 encoding on a Nehalem CPU.
81  * FIXME: Do further testing? */
82 #define LEVEL_TABLE_SIZE 128
83 extern vlc_large_t x264_level_token[7][LEVEL_TABLE_SIZE];
84
85 /* The longest possible set of zero run codes sums to 25 bits.  This leaves
86  * plenty of room for both the code (25 bits) and size (5 bits) in a uint32_t. */
87
88 extern uint32_t x264_run_before[1<<16];
89
90 static inline void bs_init( bs_t *s, void *p_data, int i_data )
91 {
92     int offset = ((intptr_t)p_data & 3);
93     s->p       = s->p_start = (uint8_t*)p_data - offset;
94     s->p_end   = (uint8_t*)p_data + i_data;
95     s->i_left  = (WORD_SIZE - offset)*8;
96     s->cur_bits = endian_fix32( M32(s->p) );
97     s->cur_bits >>= (4-offset)*8;
98 }
99 static inline int bs_pos( bs_t *s )
100 {
101     return( 8 * (s->p - s->p_start) + (WORD_SIZE*8) - s->i_left );
102 }
103
104 /* Write the rest of cur_bits to the bitstream; results in a bitstream no longer 32-bit aligned. */
105 static inline void bs_flush( bs_t *s )
106 {
107     M32( s->p ) = endian_fix32( s->cur_bits << (s->i_left&31) );
108     s->p += WORD_SIZE - (s->i_left >> 3);
109     s->i_left = WORD_SIZE*8;
110 }
111 /* The inverse of bs_flush: prepare the bitstream to be written to again. */
112 static inline void bs_realign( bs_t *s )
113 {
114     int offset = ((intptr_t)s->p & 3);
115     if( offset )
116     {
117         s->p       = (uint8_t*)s->p - offset;
118         s->i_left  = (WORD_SIZE - offset)*8;
119         s->cur_bits = endian_fix32( M32(s->p) );
120         s->cur_bits >>= (4-offset)*8;
121     }
122 }
123
124 static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
125 {
126     if( WORD_SIZE == 8 )
127     {
128         s->cur_bits = (s->cur_bits << i_count) | i_bits;
129         s->i_left -= i_count;
130         if( s->i_left <= 32 )
131         {
132 #if WORDS_BIGENDIAN
133             M32( s->p ) = s->cur_bits >> (32 - s->i_left);
134 #else
135             M32( s->p ) = endian_fix( s->cur_bits << s->i_left );
136 #endif
137             s->i_left += 32;
138             s->p += 4;
139         }
140     }
141     else
142     {
143         if( i_count < s->i_left )
144         {
145             s->cur_bits = (s->cur_bits << i_count) | i_bits;
146             s->i_left -= i_count;
147         }
148         else
149         {
150             i_count -= s->i_left;
151             s->cur_bits = (s->cur_bits << s->i_left) | (i_bits >> i_count);
152             M32( s->p ) = endian_fix( s->cur_bits );
153             s->p += 4;
154             s->cur_bits = i_bits;
155             s->i_left = 32 - i_count;
156         }
157     }
158 }
159
160 /* Special case to eliminate branch in normal bs_write. */
161 /* Golomb never writes an even-size code, so this is only used in slice headers. */
162 static inline void bs_write32( bs_t *s, uint32_t i_bits )
163 {
164     bs_write( s, 16, i_bits >> 16 );
165     bs_write( s, 16, i_bits );
166 }
167
168 static inline void bs_write1( bs_t *s, uint32_t i_bit )
169 {
170     s->cur_bits <<= 1;
171     s->cur_bits |= i_bit;
172     s->i_left--;
173     if( s->i_left == WORD_SIZE*8-32 )
174     {
175         M32( s->p ) = endian_fix32( s->cur_bits );
176         s->p += 4;
177         s->i_left = WORD_SIZE*8;
178     }
179 }
180
181 static inline void bs_align_0( bs_t *s )
182 {
183     bs_write( s, s->i_left&7, 0 );
184     bs_flush( s );
185 }
186 static inline void bs_align_1( bs_t *s )
187 {
188     bs_write( s, s->i_left&7, (1 << (s->i_left&7)) - 1 );
189     bs_flush( s );
190 }
191 static inline void bs_align_10( bs_t *s )
192 {
193     if( s->i_left&7 )
194         bs_write( s, s->i_left&7, 1 << ( (s->i_left&7) - 1 ) );
195 }
196
197 /* golomb functions */
198
199 static const uint8_t x264_ue_size_tab[256] =
200 {
201      1, 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,
202      9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
203     11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
204     11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
205     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
206     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
207     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
208     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
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     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
212     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
213     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
214     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
215     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
216     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
217 };
218
219 static inline void bs_write_ue_big( bs_t *s, unsigned int val )
220 {
221     int size = 0;
222     int tmp = ++val;
223     if( tmp >= 0x10000 )
224     {
225         size = 32;
226         tmp >>= 16;
227     }
228     if( tmp >= 0x100 )
229     {
230         size += 16;
231         tmp >>= 8;
232     }
233     size += x264_ue_size_tab[tmp];
234     bs_write( s, size>>1, 0 );
235     bs_write( s, (size>>1)+1, val );
236 }
237
238 /* Only works on values under 255. */
239 static inline void bs_write_ue( bs_t *s, int val )
240 {
241     bs_write( s, x264_ue_size_tab[val+1], val+1 );
242 }
243
244 static inline void bs_write_se( bs_t *s, int val )
245 {
246     int size = 0;
247     /* Faster than (val <= 0 ? -val*2+1 : val*2) */
248     /* 4 instructions on x86, 3 on ARM */
249     int tmp = 1 - val*2;
250     if( tmp < 0 ) tmp = val*2;
251     val = tmp;
252
253     if( tmp >= 0x100 )
254     {
255         size = 16;
256         tmp >>= 8;
257     }
258     size += x264_ue_size_tab[tmp];
259     bs_write( s, size, val );
260 }
261
262 static inline void bs_write_te( bs_t *s, int x, int val )
263 {
264     if( x == 1 )
265         bs_write1( s, 1^val );
266     else //if( x > 1 )
267         bs_write_ue( s, val );
268 }
269
270 static inline void bs_rbsp_trailing( bs_t *s )
271 {
272     bs_write1( s, 1 );
273     bs_write( s, s->i_left&7, 0  );
274 }
275
276 static ALWAYS_INLINE int bs_size_ue( unsigned int val )
277 {
278     return x264_ue_size_tab[val+1];
279 }
280
281 static ALWAYS_INLINE int bs_size_ue_big( unsigned int val )
282 {
283     if( val < 255 )
284         return x264_ue_size_tab[val+1];
285     else
286         return x264_ue_size_tab[(val+1)>>8] + 16;
287 }
288
289 static ALWAYS_INLINE int bs_size_se( int val )
290 {
291     int tmp = 1 - val*2;
292     if( tmp < 0 ) tmp = val*2;
293     if( tmp < 256 )
294         return x264_ue_size_tab[tmp];
295     else
296         return x264_ue_size_tab[tmp>>8]+16;
297 }
298
299 static ALWAYS_INLINE int bs_size_te( int x, int val )
300 {
301     if( x == 1 )
302         return 1;
303     else //if( x > 1 )
304         return x264_ue_size_tab[val+1];
305 }
306
307 #endif