annotate cabac.h @ 1290:dae280b939ca libavcodec

(truncated) unary binerization unary k-th order exp golomb binarization
author michaelni
date Fri, 30 May 2003 01:05:48 +0000
parents 9211fbd31353
children 5bc3184810dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1287
michaelni
parents:
diff changeset
1 /*
michaelni
parents:
diff changeset
2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
michaelni
parents:
diff changeset
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
michaelni
parents:
diff changeset
4 *
michaelni
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
michaelni
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
michaelni
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
michaelni
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
michaelni
parents:
diff changeset
9 *
michaelni
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
michaelni
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
michaelni
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
michaelni
parents:
diff changeset
13 * Lesser General Public License for more details.
michaelni
parents:
diff changeset
14 *
michaelni
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
michaelni
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
michaelni
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
michaelni
parents:
diff changeset
18 *
michaelni
parents:
diff changeset
19 */
michaelni
parents:
diff changeset
20
michaelni
parents:
diff changeset
21 /**
michaelni
parents:
diff changeset
22 * @file cabac.h
michaelni
parents:
diff changeset
23 * Context Adaptive Binary Arithmetic Coder.
michaelni
parents:
diff changeset
24 */
michaelni
parents:
diff changeset
25
michaelni
parents:
diff changeset
26
michaelni
parents:
diff changeset
27 #undef NDEBUG
michaelni
parents:
diff changeset
28 #include <assert.h>
michaelni
parents:
diff changeset
29
michaelni
parents:
diff changeset
30 typedef struct CABACContext{
michaelni
parents:
diff changeset
31 int low;
michaelni
parents:
diff changeset
32 int range;
michaelni
parents:
diff changeset
33 int outstanding_count;
michaelni
parents:
diff changeset
34 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
35 int symCount;
michaelni
parents:
diff changeset
36 #endif
michaelni
parents:
diff changeset
37 uint8_t lps_range[2*64][4]; ///< rangeTabLPS
michaelni
parents:
diff changeset
38 uint8_t lps_state[2*64]; ///< transIdxLPS
michaelni
parents:
diff changeset
39 uint8_t mps_state[2*64]; ///< transIdxMPS
michaelni
parents:
diff changeset
40 uint8_t *bytestream;
michaelni
parents:
diff changeset
41 int bits_left; ///<
michaelni
parents:
diff changeset
42 PutBitContext pb;
michaelni
parents:
diff changeset
43 }CABACContext;
michaelni
parents:
diff changeset
44
michaelni
parents:
diff changeset
45 const uint8_t ff_h264_lps_range[64][4];
michaelni
parents:
diff changeset
46 const uint8_t ff_h264_mps_state[64];
michaelni
parents:
diff changeset
47 const uint8_t ff_h264_lps_state[64];
michaelni
parents:
diff changeset
48
michaelni
parents:
diff changeset
49 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
michaelni
parents:
diff changeset
50 void ff_init_cabac_decoder(CABACContext *c, uint8_t *buf, int buf_size);
michaelni
parents:
diff changeset
51 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
michaelni
parents:
diff changeset
52 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
michaelni
parents:
diff changeset
53
michaelni
parents:
diff changeset
54
michaelni
parents:
diff changeset
55 static inline void put_cabac_bit(CABACContext *c, int b){
michaelni
parents:
diff changeset
56 put_bits(&c->pb, 1, b);
michaelni
parents:
diff changeset
57 for(;c->outstanding_count; c->outstanding_count--){
michaelni
parents:
diff changeset
58 put_bits(&c->pb, 1, 1-b);
michaelni
parents:
diff changeset
59 }
michaelni
parents:
diff changeset
60 }
michaelni
parents:
diff changeset
61
michaelni
parents:
diff changeset
62 static inline void renorm_cabac_encoder(CABACContext *c){
michaelni
parents:
diff changeset
63 while(c->range < 0x100){
michaelni
parents:
diff changeset
64 //FIXME optimize
michaelni
parents:
diff changeset
65 if(c->low<0x100){
michaelni
parents:
diff changeset
66 put_cabac_bit(c, 0);
michaelni
parents:
diff changeset
67 }else if(c->low<0x200){
michaelni
parents:
diff changeset
68 c->outstanding_count++;
michaelni
parents:
diff changeset
69 c->low -= 0x100;
michaelni
parents:
diff changeset
70 }else{
michaelni
parents:
diff changeset
71 put_cabac_bit(c, 1);
michaelni
parents:
diff changeset
72 c->low -= 0x200;
michaelni
parents:
diff changeset
73 }
michaelni
parents:
diff changeset
74
michaelni
parents:
diff changeset
75 c->range+= c->range;
michaelni
parents:
diff changeset
76 c->low += c->low;
michaelni
parents:
diff changeset
77 }
michaelni
parents:
diff changeset
78 }
michaelni
parents:
diff changeset
79
michaelni
parents:
diff changeset
80 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
michaelni
parents:
diff changeset
81 int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
michaelni
parents:
diff changeset
82
michaelni
parents:
diff changeset
83 if(bit == ((*state)&1)){
michaelni
parents:
diff changeset
84 c->range -= RangeLPS;
michaelni
parents:
diff changeset
85 *state= c->mps_state[*state];
michaelni
parents:
diff changeset
86 }else{
michaelni
parents:
diff changeset
87 c->low += c->range - RangeLPS;
michaelni
parents:
diff changeset
88 c->range = RangeLPS;
michaelni
parents:
diff changeset
89 *state= c->lps_state[*state];
michaelni
parents:
diff changeset
90 }
michaelni
parents:
diff changeset
91
michaelni
parents:
diff changeset
92 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
93
michaelni
parents:
diff changeset
94 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
95 c->symCount++;
michaelni
parents:
diff changeset
96 #endif
michaelni
parents:
diff changeset
97 }
michaelni
parents:
diff changeset
98
michaelni
parents:
diff changeset
99 static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
michaelni
parents:
diff changeset
100 assert(c->range > RangeLPS);
michaelni
parents:
diff changeset
101
michaelni
parents:
diff changeset
102 if(!bit){
michaelni
parents:
diff changeset
103 c->range -= RangeLPS;
michaelni
parents:
diff changeset
104 }else{
michaelni
parents:
diff changeset
105 c->low += c->range - RangeLPS;
michaelni
parents:
diff changeset
106 c->range = RangeLPS;
michaelni
parents:
diff changeset
107 }
michaelni
parents:
diff changeset
108
michaelni
parents:
diff changeset
109 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
110
michaelni
parents:
diff changeset
111 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
112 c->symCount++;
michaelni
parents:
diff changeset
113 #endif
michaelni
parents:
diff changeset
114 }
michaelni
parents:
diff changeset
115
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
116 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
117 * @param bit 0 -> write zero bit, !=0 write one bit
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
118 */
1287
michaelni
parents:
diff changeset
119 static inline void put_cabac_bypass(CABACContext *c, int bit){
michaelni
parents:
diff changeset
120 c->low += c->low;
michaelni
parents:
diff changeset
121
michaelni
parents:
diff changeset
122 if(bit){
michaelni
parents:
diff changeset
123 c->low += c->range;
michaelni
parents:
diff changeset
124 }
michaelni
parents:
diff changeset
125 //FIXME optimize
michaelni
parents:
diff changeset
126 if(c->low<0x200){
michaelni
parents:
diff changeset
127 put_cabac_bit(c, 0);
michaelni
parents:
diff changeset
128 }else if(c->low<0x400){
michaelni
parents:
diff changeset
129 c->outstanding_count++;
michaelni
parents:
diff changeset
130 c->low -= 0x200;
michaelni
parents:
diff changeset
131 }else{
michaelni
parents:
diff changeset
132 put_cabac_bit(c, 1);
michaelni
parents:
diff changeset
133 c->low -= 0x400;
michaelni
parents:
diff changeset
134 }
michaelni
parents:
diff changeset
135
michaelni
parents:
diff changeset
136 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
137 c->symCount++;
michaelni
parents:
diff changeset
138 #endif
michaelni
parents:
diff changeset
139 }
michaelni
parents:
diff changeset
140
michaelni
parents:
diff changeset
141 static inline void put_cabac_terminate(CABACContext *c, int bit){
michaelni
parents:
diff changeset
142 c->range -= 2;
michaelni
parents:
diff changeset
143
michaelni
parents:
diff changeset
144 if(!bit){
michaelni
parents:
diff changeset
145 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
146 }else{
michaelni
parents:
diff changeset
147 c->low += c->range;
michaelni
parents:
diff changeset
148 c->range= 2;
michaelni
parents:
diff changeset
149
michaelni
parents:
diff changeset
150 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
151
michaelni
parents:
diff changeset
152 assert(c->low <= 0x1FF);
michaelni
parents:
diff changeset
153 put_cabac_bit(c, c->low>>9);
michaelni
parents:
diff changeset
154 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
michaelni
parents:
diff changeset
155
michaelni
parents:
diff changeset
156 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
michaelni
parents:
diff changeset
157 }
michaelni
parents:
diff changeset
158
michaelni
parents:
diff changeset
159 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
160 c->symCount++;
michaelni
parents:
diff changeset
161 #endif
michaelni
parents:
diff changeset
162 }
michaelni
parents:
diff changeset
163
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
164 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
165 * put (truncated) unary binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
166 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
167 static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
168 int i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
169
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
170 assert(v <= max);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
171
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
172 #if 1
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
173 for(i=0; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
174 put_cabac(c, state, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
175 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
176 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
177 if(truncated==0 || v<max)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
178 put_cabac(c, state, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
179 #else
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
180 if(v <= max_index){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
181 for(i=0; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
182 put_cabac(c, state+i, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
183 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
184 if(truncated==0 || v<max)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
185 put_cabac(c, state+i, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
186 }else{
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
187 for(i=0; i<=max_index; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
188 put_cabac(c, state+i, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
189 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
190 for(; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
191 put_cabac(c, state+max_index, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
192 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
193 if(truncated==0 || v<max)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
194 put_cabac(c, state+max_index, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
195 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
196 #endif
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
197 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
198
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
199 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
200 * put unary exp golomb k-th order binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
201 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
202 static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int sign, int max, int is_signed, int k, int max_index){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
203 int i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
204
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
205 if(v==0)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
206 put_cabac(c, state, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
207 else{
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
208 if(v<max){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
209 for(i=0; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
210 put_cabac(c, state, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
211 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
212 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
213
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
214 put_cabac(c, state, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
215 }else{
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
216 int m= 1<<k;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
217
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
218 for(i=0; i<max; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
219 put_cabac(c, state, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
220 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
221 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
222
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
223 v -= max;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
224 while(v >= m){ //FIXME optimize
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
225 put_cabac_bypass(c, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
226 v-= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
227 m+= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
228 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
229 put_cabac_bypass(c, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
230 while(m>>=1){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
231 put_cabac_bypass(c, v&m);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
232 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
233 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
234
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
235 if(is_signed)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
236 put_cabac_bypass(c, sign);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
237 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
238 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
239
1287
michaelni
parents:
diff changeset
240 static inline void renorm_cabac_decoder(CABACContext *c){
michaelni
parents:
diff changeset
241 while(c->range < 0x10000){
michaelni
parents:
diff changeset
242 c->range+= c->range;
michaelni
parents:
diff changeset
243 c->low+= c->low;
michaelni
parents:
diff changeset
244 if(--c->bits_left == 0){
michaelni
parents:
diff changeset
245 c->low+= *c->bytestream++;
michaelni
parents:
diff changeset
246 c->bits_left= 8;
michaelni
parents:
diff changeset
247 }
michaelni
parents:
diff changeset
248 }
michaelni
parents:
diff changeset
249 }
michaelni
parents:
diff changeset
250
michaelni
parents:
diff changeset
251 static inline int get_cabac(CABACContext *c, uint8_t * const state){
michaelni
parents:
diff changeset
252 int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
michaelni
parents:
diff changeset
253 int bit;
michaelni
parents:
diff changeset
254
michaelni
parents:
diff changeset
255 c->range -= RangeLPS;
michaelni
parents:
diff changeset
256 if(c->low < c->range){
michaelni
parents:
diff changeset
257 bit= (*state)&1;
michaelni
parents:
diff changeset
258 *state= c->mps_state[*state];
michaelni
parents:
diff changeset
259 }else{
michaelni
parents:
diff changeset
260 bit= ((*state)&1)^1;
michaelni
parents:
diff changeset
261 c->low -= c->range;
michaelni
parents:
diff changeset
262 c->range = RangeLPS;
michaelni
parents:
diff changeset
263 *state= c->lps_state[*state];
michaelni
parents:
diff changeset
264 }
michaelni
parents:
diff changeset
265 renorm_cabac_decoder(c);
michaelni
parents:
diff changeset
266
michaelni
parents:
diff changeset
267 return bit;
michaelni
parents:
diff changeset
268 }
michaelni
parents:
diff changeset
269
michaelni
parents:
diff changeset
270 static inline int get_cabac_static(CABACContext *c, int RangeLPS){
michaelni
parents:
diff changeset
271 int bit;
michaelni
parents:
diff changeset
272
michaelni
parents:
diff changeset
273 c->range -= RangeLPS;
michaelni
parents:
diff changeset
274 if(c->low < c->range){
michaelni
parents:
diff changeset
275 bit= 0;
michaelni
parents:
diff changeset
276 }else{
michaelni
parents:
diff changeset
277 bit= 1;
michaelni
parents:
diff changeset
278 c->low -= c->range;
michaelni
parents:
diff changeset
279 c->range = RangeLPS;
michaelni
parents:
diff changeset
280 }
michaelni
parents:
diff changeset
281 renorm_cabac_decoder(c);
michaelni
parents:
diff changeset
282
michaelni
parents:
diff changeset
283 return bit;
michaelni
parents:
diff changeset
284 }
michaelni
parents:
diff changeset
285
michaelni
parents:
diff changeset
286 static inline int get_cabac_bypass(CABACContext *c){
michaelni
parents:
diff changeset
287 c->low += c->low;
michaelni
parents:
diff changeset
288
michaelni
parents:
diff changeset
289 if(--c->bits_left == 0){
michaelni
parents:
diff changeset
290 c->low+= *c->bytestream++;
michaelni
parents:
diff changeset
291 c->bits_left= 8;
michaelni
parents:
diff changeset
292 }
michaelni
parents:
diff changeset
293
michaelni
parents:
diff changeset
294 if(c->low < c->range){
michaelni
parents:
diff changeset
295 return 0;
michaelni
parents:
diff changeset
296 }else{
michaelni
parents:
diff changeset
297 c->low -= c->range;
michaelni
parents:
diff changeset
298 return 1;
michaelni
parents:
diff changeset
299 }
michaelni
parents:
diff changeset
300 }
michaelni
parents:
diff changeset
301
michaelni
parents:
diff changeset
302 static inline int get_cabac_terminate(CABACContext *c){
michaelni
parents:
diff changeset
303 c->range -= 2<<8;
michaelni
parents:
diff changeset
304 if(c->low < c->range){
michaelni
parents:
diff changeset
305 renorm_cabac_decoder(c);
michaelni
parents:
diff changeset
306 return 0;
michaelni
parents:
diff changeset
307 }else{
michaelni
parents:
diff changeset
308 return 1;
michaelni
parents:
diff changeset
309 }
michaelni
parents:
diff changeset
310 }
michaelni
parents:
diff changeset
311
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
312 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
313 * get (truncated) unnary binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
314 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
315 static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
316 int i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
317
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
318 for(i=0; i<max; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
319 if(get_cabac(c, state)==0)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
320 return i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
321
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
322 if(i< max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
323 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
324
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
325 return truncated ? max : -1;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
326 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
327
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
328 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
329 * get unary exp golomb k-th order binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
330 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
331 static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
332 int i, v;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
333 int m= 1<<k;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
334
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
335 if(get_cabac(c, state)==0)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
336 return 0;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
337
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
338 if(0 < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
339
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
340 for(i=1; i<max; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
341 if(get_cabac(c, state)==0){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
342 if(is_signed && get_cabac_bypass(c)){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
343 return -i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
344 }else
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
345 return i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
346 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
347
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
348 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
349 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
350
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
351 while(get_cabac_bypass(c)){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
352 i+= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
353 m+= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
354 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
355
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
356 v=0;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
357 while(m>>=1){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
358 v+= v + get_cabac_bypass(c);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
359 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
360 i += v;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
361
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
362 if(is_signed && get_cabac_bypass(c)){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
363 return -i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
364 }else
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
365 return i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
366 }