]> git.sesse.net Git - x264/blob - common/bs.h
force unroll macroblock_load_pic_pointers
[x264] / common / bs.h
1 /*****************************************************************************
2  * bs.h :
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: bs.h,v 1.1 2004/06/03 19:27:06 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #ifndef X264_BS_H
25 #define X264_BS_H
26
27 typedef struct bs_s
28 {
29     uint8_t *p_start;
30     uint8_t *p;
31     uint8_t *p_end;
32
33     int     i_left;    /* i_count number of available bits */
34     int     i_bits_encoded; /* RD only */
35 } bs_t;
36
37 static inline void bs_init( bs_t *s, void *p_data, int i_data )
38 {
39     s->p_start = p_data;
40     s->p       = p_data;
41     s->p_end   = s->p + i_data;
42     s->i_left  = 8;
43 }
44 static inline int bs_pos( bs_t *s )
45 {
46     return( 8 * ( s->p - s->p_start ) + 8 - s->i_left );
47 }
48
49 static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
50 {
51     if( s->p >= s->p_end - 4 )
52         return;
53     while( i_count > 0 )
54     {
55         if( i_count < 32 )
56             i_bits &= (1<<i_count)-1;
57         if( i_count < s->i_left )
58         {
59             *s->p = (*s->p << i_count) | i_bits;
60             s->i_left -= i_count;
61             break;
62         }
63         else
64         {
65             *s->p = (*s->p << s->i_left) | (i_bits >> (i_count - s->i_left));
66             i_count -= s->i_left;
67             s->p++;
68             s->i_left = 8;
69         }
70     }
71 }
72
73 static inline void bs_write1( bs_t *s, uint32_t i_bit )
74 {
75     if( s->p < s->p_end )
76     {
77         *s->p <<= 1;
78         *s->p |= i_bit;
79         s->i_left--;
80         if( s->i_left == 0 )
81         {
82             s->p++;
83             s->i_left = 8;
84         }
85     }
86 }
87
88 static inline void bs_align_0( bs_t *s )
89 {
90     if( s->i_left != 8 )
91     {
92         *s->p <<= s->i_left;
93         s->i_left = 8;
94         s->p++;
95     }
96 }
97 static inline void bs_align_1( bs_t *s )
98 {
99     if( s->i_left != 8 )
100     {
101         *s->p <<= s->i_left;
102         *s->p |= (1 << s->i_left) - 1;
103         s->i_left = 8;
104         s->p++;
105     }
106 }
107 static inline void bs_align( bs_t *s )
108 {
109     bs_align_0( s );
110 }
111
112
113
114 /* golomb functions */
115
116 static inline void bs_write_ue( bs_t *s, unsigned int val )
117 {
118     int i_size = 0;
119     static const uint8_t i_size0_255[256] =
120     {
121         1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
122         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
123         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
124         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
125         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
126         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
127         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
128         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
129     };
130
131     if( val == 0 )
132     {
133         bs_write1( s, 1 );
134     }
135     else
136     {
137         unsigned int tmp = ++val;
138
139         if( tmp >= 0x00010000 )
140         {
141             i_size += 16;
142             tmp >>= 16;
143         }
144         if( tmp >= 0x100 )
145         {
146             i_size += 8;
147             tmp >>= 8;
148         }
149         i_size += i_size0_255[tmp];
150
151         bs_write( s, 2 * i_size - 1, val );
152     }
153 }
154
155 static inline void bs_write_se( bs_t *s, int val )
156 {
157     bs_write_ue( s, val <= 0 ? -val * 2 : val * 2 - 1);
158 }
159
160 static inline void bs_write_te( bs_t *s, int x, int val )
161 {
162     if( x == 1 )
163     {
164         bs_write1( s, 1&~val );
165     }
166     else if( x > 1 )
167     {
168         bs_write_ue( s, val );
169     }
170 }
171
172 static inline void bs_rbsp_trailing( bs_t *s )
173 {
174     bs_write1( s, 1 );
175     if( s->i_left != 8 )
176     {
177         bs_write( s, s->i_left, 0x00 );
178     }
179 }
180
181 static inline int bs_size_ue( unsigned int val )
182 {
183     static const uint8_t i_size0_254[255] =
184     {
185         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,11,11,11,11,11,11,11,
188         11,11,11,11,11,11,11,11,11,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
189         13,13,13,13,13,13,13,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,13,13,13,13,13,13,13,
191         13,13,13,13,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
192         15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
193         15,15,15,15,15,15,15,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,15,15,15,15,15,15,15,
195         15,15,15,15,15,15,15,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,15
197     };
198
199     if( val < 255 )
200     {
201         return i_size0_254[val];
202     }
203     else
204     {
205         int i_size = 0;
206
207         val++;
208
209         if( val >= 0x10000 )
210         {
211             i_size += 32;
212             val = (val >> 16) - 1;
213         }
214         if( val >= 0x100 )
215         {
216             i_size += 16;
217             val = (val >> 8) - 1;
218         }
219         return i_size0_254[val] + i_size;
220     }
221 }
222
223 static inline int bs_size_se( int val )
224 {
225     return bs_size_ue( val <= 0 ? -val * 2 : val * 2 - 1);
226 }
227
228 static inline int bs_size_te( int x, int val )
229 {
230     if( x == 1 )
231     {
232         return 1;
233     }
234     else if( x > 1 )
235     {
236         return bs_size_ue( val );
237     }
238     return 0;
239 }
240
241 #endif