x86: cabac: remove hardcoded struct offsets from inline asm
[libav.git] / libavcodec / cabac.h
1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Context Adaptive Binary Arithmetic Coder.
25 */
26
27 #ifndef AVCODEC_CABAC_H
28 #define AVCODEC_CABAC_H
29
30 #include <stddef.h>
31
32 #include "put_bits.h"
33
34 //#undef NDEBUG
35 #include <assert.h>
36 #include "libavutil/x86_cpu.h"
37
38 #define CABAC_BITS 16
39 #define CABAC_MASK ((1<<CABAC_BITS)-1)
40
41 typedef struct CABACContext{
42 int low;
43 int range;
44 int outstanding_count;
45 #ifdef STRICT_LIMITS
46 int symCount;
47 #endif
48 const uint8_t *bytestream_start;
49 const uint8_t *bytestream;
50 const uint8_t *bytestream_end;
51 PutBitContext pb;
52 }CABACContext;
53
54 extern uint8_t ff_h264_mlps_state[4*64];
55 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
56 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
57 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
58 extern const uint8_t ff_h264_norm_shift[512];
59
60
61 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
62 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
63 void ff_init_cabac_states(CABACContext *c);
64
65
66 static inline void put_cabac_bit(CABACContext *c, int b){
67 put_bits(&c->pb, 1, b);
68 for(;c->outstanding_count; c->outstanding_count--){
69 put_bits(&c->pb, 1, 1-b);
70 }
71 }
72
73 static inline void renorm_cabac_encoder(CABACContext *c){
74 while(c->range < 0x100){
75 //FIXME optimize
76 if(c->low<0x100){
77 put_cabac_bit(c, 0);
78 }else if(c->low<0x200){
79 c->outstanding_count++;
80 c->low -= 0x100;
81 }else{
82 put_cabac_bit(c, 1);
83 c->low -= 0x200;
84 }
85
86 c->range+= c->range;
87 c->low += c->low;
88 }
89 }
90
91 #ifdef TEST
92 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
93 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
94
95 if(bit == ((*state)&1)){
96 c->range -= RangeLPS;
97 *state= ff_h264_mps_state[*state];
98 }else{
99 c->low += c->range - RangeLPS;
100 c->range = RangeLPS;
101 *state= ff_h264_lps_state[*state];
102 }
103
104 renorm_cabac_encoder(c);
105
106 #ifdef STRICT_LIMITS
107 c->symCount++;
108 #endif
109 }
110
111 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
112 assert(c->range > RangeLPS);
113
114 if(!bit){
115 c->range -= RangeLPS;
116 }else{
117 c->low += c->range - RangeLPS;
118 c->range = RangeLPS;
119 }
120
121 renorm_cabac_encoder(c);
122
123 #ifdef STRICT_LIMITS
124 c->symCount++;
125 #endif
126 }
127
128 /**
129 * @param bit 0 -> write zero bit, !=0 write one bit
130 */
131 static void put_cabac_bypass(CABACContext *c, int bit){
132 c->low += c->low;
133
134 if(bit){
135 c->low += c->range;
136 }
137 //FIXME optimize
138 if(c->low<0x200){
139 put_cabac_bit(c, 0);
140 }else if(c->low<0x400){
141 c->outstanding_count++;
142 c->low -= 0x200;
143 }else{
144 put_cabac_bit(c, 1);
145 c->low -= 0x400;
146 }
147
148 #ifdef STRICT_LIMITS
149 c->symCount++;
150 #endif
151 }
152
153 /**
154 *
155 * @return the number of bytes written
156 */
157 static int put_cabac_terminate(CABACContext *c, int bit){
158 c->range -= 2;
159
160 if(!bit){
161 renorm_cabac_encoder(c);
162 }else{
163 c->low += c->range;
164 c->range= 2;
165
166 renorm_cabac_encoder(c);
167
168 assert(c->low <= 0x1FF);
169 put_cabac_bit(c, c->low>>9);
170 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
171
172 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
173 }
174
175 #ifdef STRICT_LIMITS
176 c->symCount++;
177 #endif
178
179 return (put_bits_count(&c->pb)+7)>>3;
180 }
181
182 /**
183 * put (truncated) unary binarization.
184 */
185 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
186 int i;
187
188 assert(v <= max);
189
190 #if 1
191 for(i=0; i<v; i++){
192 put_cabac(c, state, 1);
193 if(i < max_index) state++;
194 }
195 if(truncated==0 || v<max)
196 put_cabac(c, state, 0);
197 #else
198 if(v <= max_index){
199 for(i=0; i<v; i++){
200 put_cabac(c, state+i, 1);
201 }
202 if(truncated==0 || v<max)
203 put_cabac(c, state+i, 0);
204 }else{
205 for(i=0; i<=max_index; i++){
206 put_cabac(c, state+i, 1);
207 }
208 for(; i<v; i++){
209 put_cabac(c, state+max_index, 1);
210 }
211 if(truncated==0 || v<max)
212 put_cabac(c, state+max_index, 0);
213 }
214 #endif
215 }
216
217 /**
218 * put unary exp golomb k-th order binarization.
219 */
220 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
221 int i;
222
223 if(v==0)
224 put_cabac(c, state, 0);
225 else{
226 const int sign= v < 0;
227
228 if(is_signed) v= FFABS(v);
229
230 if(v<max){
231 for(i=0; i<v; i++){
232 put_cabac(c, state, 1);
233 if(i < max_index) state++;
234 }
235
236 put_cabac(c, state, 0);
237 }else{
238 int m= 1<<k;
239
240 for(i=0; i<max; i++){
241 put_cabac(c, state, 1);
242 if(i < max_index) state++;
243 }
244
245 v -= max;
246 while(v >= m){ //FIXME optimize
247 put_cabac_bypass(c, 1);
248 v-= m;
249 m+= m;
250 }
251 put_cabac_bypass(c, 0);
252 while(m>>=1){
253 put_cabac_bypass(c, v&m);
254 }
255 }
256
257 if(is_signed)
258 put_cabac_bypass(c, sign);
259 }
260 }
261 #endif /* TEST */
262
263 static void refill(CABACContext *c){
264 #if CABAC_BITS == 16
265 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
266 #else
267 c->low+= c->bytestream[0]<<1;
268 #endif
269 c->low -= CABAC_MASK;
270 c->bytestream+= CABAC_BITS/8;
271 }
272
273 #if ! ( ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS) )
274 static void refill2(CABACContext *c){
275 int i, x;
276
277 x= c->low ^ (c->low-1);
278 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
279
280 x= -CABAC_MASK;
281
282 #if CABAC_BITS == 16
283 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
284 #else
285 x+= c->bytestream[0]<<1;
286 #endif
287
288 c->low += x<<i;
289 c->bytestream+= CABAC_BITS/8;
290 }
291 #endif
292
293 static inline void renorm_cabac_decoder(CABACContext *c){
294 while(c->range < 0x100){
295 c->range+= c->range;
296 c->low+= c->low;
297 if(!(c->low & CABAC_MASK))
298 refill(c);
299 }
300 }
301
302 static inline void renorm_cabac_decoder_once(CABACContext *c){
303 int shift= (uint32_t)(c->range - 0x100)>>31;
304 c->range<<= shift;
305 c->low <<= shift;
306 if(!(c->low & CABAC_MASK))
307 refill(c);
308 }
309
310 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
311 //FIXME gcc generates duplicate load/stores for c->low and c->range
312 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS)
313 int bit;
314
315 #if HAVE_FAST_CMOV
316 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
317 "mov "tmp" , %%ecx \n\t"\
318 "shl $17 , "tmp" \n\t"\
319 "cmp "low" , "tmp" \n\t"\
320 "cmova %%ecx , "range" \n\t"\
321 "sbb %%ecx , %%ecx \n\t"\
322 "and %%ecx , "tmp" \n\t"\
323 "sub "tmp" , "low" \n\t"\
324 "xor %%ecx , "ret" \n\t"
325 #else /* HAVE_FAST_CMOV */
326 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
327 "mov "tmp" , %%ecx \n\t"\
328 "shl $17 , "tmp" \n\t"\
329 "sub "low" , "tmp" \n\t"\
330 "sar $31 , "tmp" \n\t" /*lps_mask*/\
331 "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
332 "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
333 "add %%ecx , "range" \n\t" /*new range*/\
334 "shl $17 , %%ecx \n\t"\
335 "and "tmp" , %%ecx \n\t"\
336 "sub %%ecx , "low" \n\t"\
337 "xor "tmp" , "ret" \n\t"
338 #endif /* HAVE_FAST_CMOV */
339
340
341 #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte, byte) \
342 "movzbl "statep" , "ret" \n\t"\
343 "mov "range" , "tmp" \n\t"\
344 "and $0xC0 , "range" \n\t"\
345 "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
346 "sub "range" , "tmp" \n\t"\
347 BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
348 "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
349 "shl %%cl , "range" \n\t"\
350 "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
351 "mov "tmpbyte" , "statep" \n\t"\
352 "shl %%cl , "low" \n\t"\
353 "test "lowword" , "lowword" \n\t"\
354 " jnz 1f \n\t"\
355 "mov "byte"("cabac"), %%"REG_c" \n\t"\
356 "movzwl (%%"REG_c") , "tmp" \n\t"\
357 "bswap "tmp" \n\t"\
358 "shr $15 , "tmp" \n\t"\
359 "sub $0xFFFF , "tmp" \n\t"\
360 "add $2 , %%"REG_c" \n\t"\
361 "mov %%"REG_c" , "byte "("cabac") \n\t"\
362 "lea -1("low") , %%ecx \n\t"\
363 "xor "low" , %%ecx \n\t"\
364 "shr $15 , %%ecx \n\t"\
365 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
366 "neg %%ecx \n\t"\
367 "add $7 , %%ecx \n\t"\
368 "shl %%cl , "tmp" \n\t"\
369 "add "tmp" , "low" \n\t"\
370 "1: \n\t"
371
372 __asm__ volatile(
373 "movl %a3(%2), %%esi \n\t"
374 "movl %a4(%2), %%ebx \n\t"
375 BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl", "%a5")
376 "movl %%esi, %a3(%2) \n\t"
377 "movl %%ebx, %a4(%2) \n\t"
378
379 :"=&a"(bit)
380 :"r"(state), "r"(c),
381 "i"(offsetof(CABACContext, range)), "i"(offsetof(CABACContext, low)),
382 "i"(offsetof(CABACContext, bytestream))
383 : "%"REG_c, "%ebx", "%edx", "%esi", "memory"
384 );
385 bit&=1;
386 #else /* ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS) */
387 int s = *state;
388 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
389 int bit, lps_mask;
390
391 c->range -= RangeLPS;
392 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
393
394 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
395 c->range += (RangeLPS - c->range) & lps_mask;
396
397 s^=lps_mask;
398 *state= (ff_h264_mlps_state+128)[s];
399 bit= s&1;
400
401 lps_mask= ff_h264_norm_shift[c->range];
402 c->range<<= lps_mask;
403 c->low <<= lps_mask;
404 if(!(c->low & CABAC_MASK))
405 refill2(c);
406 #endif /* ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS) */
407 return bit;
408 }
409
410 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
411 return get_cabac_inline(c,state);
412 }
413
414 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
415 return get_cabac_inline(c,state);
416 }
417
418 static int av_unused get_cabac_bypass(CABACContext *c){
419 int range;
420 c->low += c->low;
421
422 if(!(c->low & CABAC_MASK))
423 refill(c);
424
425 range= c->range<<(CABAC_BITS+1);
426 if(c->low < range){
427 return 0;
428 }else{
429 c->low -= range;
430 return 1;
431 }
432 }
433
434
435 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
436 #if ARCH_X86 && HAVE_EBX_AVAILABLE
437 __asm__ volatile(
438 "movl %a2(%1), %%ebx \n\t"
439 "movl %a3(%1), %%eax \n\t"
440 "shl $17, %%ebx \n\t"
441 "add %%eax, %%eax \n\t"
442 "sub %%ebx, %%eax \n\t"
443 "cltd \n\t"
444 "and %%edx, %%ebx \n\t"
445 "add %%ebx, %%eax \n\t"
446 "xor %%edx, %%ecx \n\t"
447 "sub %%edx, %%ecx \n\t"
448 "test %%ax, %%ax \n\t"
449 " jnz 1f \n\t"
450 "mov %a4(%1), %%"REG_b" \n\t"
451 "subl $0xFFFF, %%eax \n\t"
452 "movzwl (%%"REG_b"), %%edx \n\t"
453 "bswap %%edx \n\t"
454 "shrl $15, %%edx \n\t"
455 "add $2, %%"REG_b" \n\t"
456 "addl %%edx, %%eax \n\t"
457 "mov %%"REG_b", %a4(%1) \n\t"
458 "1: \n\t"
459 "movl %%eax, %a3(%1) \n\t"
460
461 :"+c"(val)
462 :"r"(c),
463 "i"(offsetof(CABACContext, range)), "i"(offsetof(CABACContext, low)),
464 "i"(offsetof(CABACContext, bytestream))
465 : "%eax", "%"REG_b, "%edx", "memory"
466 );
467 return val;
468 #else
469 int range, mask;
470 c->low += c->low;
471
472 if(!(c->low & CABAC_MASK))
473 refill(c);
474
475 range= c->range<<(CABAC_BITS+1);
476 c->low -= range;
477 mask= c->low >> 31;
478 range &= mask;
479 c->low += range;
480 return (val^mask)-mask;
481 #endif
482 }
483
484 /**
485 *
486 * @return the number of bytes read or 0 if no end
487 */
488 static int av_unused get_cabac_terminate(CABACContext *c){
489 c->range -= 2;
490 if(c->low < c->range<<(CABAC_BITS+1)){
491 renorm_cabac_decoder_once(c);
492 return 0;
493 }else{
494 return c->bytestream - c->bytestream_start;
495 }
496 }
497
498 #if 0
499 /**
500 * Get (truncated) unary binarization.
501 */
502 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
503 int i;
504
505 for(i=0; i<max; i++){
506 if(get_cabac(c, state)==0)
507 return i;
508
509 if(i< max_index) state++;
510 }
511
512 return truncated ? max : -1;
513 }
514
515 /**
516 * get unary exp golomb k-th order binarization.
517 */
518 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
519 int i, v;
520 int m= 1<<k;
521
522 if(get_cabac(c, state)==0)
523 return 0;
524
525 if(0 < max_index) state++;
526
527 for(i=1; i<max; i++){
528 if(get_cabac(c, state)==0){
529 if(is_signed && get_cabac_bypass(c)){
530 return -i;
531 }else
532 return i;
533 }
534
535 if(i < max_index) state++;
536 }
537
538 while(get_cabac_bypass(c)){
539 i+= m;
540 m+= m;
541 }
542
543 v=0;
544 while(m>>=1){
545 v+= v + get_cabac_bypass(c);
546 }
547 i += v;
548
549 if(is_signed && get_cabac_bypass(c)){
550 return -i;
551 }else
552 return i;
553 }
554 #endif /* 0 */
555
556 #endif /* AVCODEC_CABAC_H */