2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Context Adaptive Binary Arithmetic Coder.
39 #define CABAC_MASK ((1<<CABAC_BITS)-1)
40 #define BRANCHLESS_CABAC_DECODER 1
41 //#define ARCH_X86_DISABLED 1
43 typedef struct CABACContext{
46 int outstanding_count;
50 const uint8_t *bytestream_start;
51 const uint8_t *bytestream;
52 const uint8_t *bytestream_end;
56 extern uint8_t ff_h264_mlps_state[4*64];
57 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
58 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
59 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
60 extern const uint8_t ff_h264_norm_shift[512];
63 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
64 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
65 void ff_init_cabac_states(CABACContext *c);
68 static inline void put_cabac_bit(CABACContext *c, int b){
69 put_bits(&c->pb, 1, b);
70 for(;c->outstanding_count; c->outstanding_count--){
71 put_bits(&c->pb, 1, 1-b);
75 static inline void renorm_cabac_encoder(CABACContext *c){
76 while(c->range < 0x100){
80 }else if(c->low<0x200){
81 c->outstanding_count++;
93 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
94 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
96 if(bit == ((*state)&1)){
98 *state= ff_h264_mps_state[*state];
100 c->low += c->range - RangeLPS;
102 *state= ff_h264_lps_state[*state];
105 renorm_cabac_encoder(c);
112 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
113 assert(c->range > RangeLPS);
116 c->range -= RangeLPS;
118 c->low += c->range - RangeLPS;
122 renorm_cabac_encoder(c);
130 * @param bit 0 -> write zero bit, !=0 write one bit
132 static void put_cabac_bypass(CABACContext *c, int bit){
141 }else if(c->low<0x400){
142 c->outstanding_count++;
156 * @return the number of bytes written
158 static int put_cabac_terminate(CABACContext *c, int bit){
162 renorm_cabac_encoder(c);
167 renorm_cabac_encoder(c);
169 assert(c->low <= 0x1FF);
170 put_cabac_bit(c, c->low>>9);
171 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
173 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
180 return (put_bits_count(&c->pb)+7)>>3;
184 * put (truncated) unary binarization.
186 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
193 put_cabac(c, state, 1);
194 if(i < max_index) state++;
196 if(truncated==0 || v<max)
197 put_cabac(c, state, 0);
201 put_cabac(c, state+i, 1);
203 if(truncated==0 || v<max)
204 put_cabac(c, state+i, 0);
206 for(i=0; i<=max_index; i++){
207 put_cabac(c, state+i, 1);
210 put_cabac(c, state+max_index, 1);
212 if(truncated==0 || v<max)
213 put_cabac(c, state+max_index, 0);
219 * put unary exp golomb k-th order binarization.
221 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
225 put_cabac(c, state, 0);
227 const int sign= v < 0;
229 if(is_signed) v= FFABS(v);
233 put_cabac(c, state, 1);
234 if(i < max_index) state++;
237 put_cabac(c, state, 0);
241 for(i=0; i<max; i++){
242 put_cabac(c, state, 1);
243 if(i < max_index) state++;
247 while(v >= m){ //FIXME optimize
248 put_cabac_bypass(c, 1);
252 put_cabac_bypass(c, 0);
254 put_cabac_bypass(c, v&m);
259 put_cabac_bypass(c, sign);
263 static void refill(CABACContext *c){
265 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
267 c->low+= c->bytestream[0]<<1;
269 c->low -= CABAC_MASK;
270 c->bytestream+= CABAC_BITS/8;
273 static void refill2(CABACContext *c){
276 x= c->low ^ (c->low-1);
277 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
282 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
284 x+= c->bytestream[0]<<1;
288 c->bytestream+= CABAC_BITS/8;
291 static inline void renorm_cabac_decoder(CABACContext *c){
292 while(c->range < 0x100){
295 if(!(c->low & CABAC_MASK))
300 static inline void renorm_cabac_decoder_once(CABACContext *c){
301 #ifdef ARCH_X86_DISABLED
306 "lea -0x100(%0), %2 \n\t"
307 "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
310 : "+r"(c->range), "+r"(c->low), "+c"(temp)
315 "cmp $0x100, %0 \n\t"
316 "setb %%cl \n\t" //FIXME 31->63 for x86-64
319 : "+r"(c->range), "+r"(c->low), "+c"(temp)
325 "lea -0x100(%0), %%eax \n\t"
332 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
338 "cmp $0x100, %0 \n\t"
339 "sbb %%edx, %%edx \n\t"
345 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
351 "cmp $0x100, %0 \n\t"
352 "lea (%0, %0), %%eax \n\t"
353 "lea (%1, %1), %%edx \n\t"
354 "cmovb %%eax, %0 \n\t"
355 "cmovb %%edx, %1 \n\t"
356 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
361 int shift= (uint32_t)(c->range - 0x100)>>31;
365 if(!(c->low & CABAC_MASK))
369 static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state){
370 //FIXME gcc generates duplicate load/stores for c->low and c->range
374 #define BYTESTART "16"
378 #define BYTESTART "12"
382 #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
385 #ifndef BRANCHLESS_CABAC_DECODER
387 "movzbl (%1), %0 \n\t"
388 "movl "RANGE "(%2), %%ebx \n\t"
389 "movl "RANGE "(%2), %%edx \n\t"
390 "andl $0xC0, %%ebx \n\t"
391 "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
392 "movl "LOW "(%2), %%ebx \n\t"
393 //eax:state ebx:low, edx:range, esi:RangeLPS
394 "subl %%esi, %%edx \n\t"
395 "movl %%edx, %%ecx \n\t"
396 "shll $17, %%ecx \n\t"
397 "cmpl %%ecx, %%ebx \n\t"
401 //athlon:4067 P3:4110
402 "lea -0x100(%%edx), %%ecx \n\t"
403 "shr $31, %%ecx \n\t"
404 "shl %%cl, %%edx \n\t"
405 "shl %%cl, %%ebx \n\t"
407 //athlon:4057 P3:4130
408 "cmp $0x100, %%edx \n\t" //FIXME avoidable
410 "shl %%cl, %%edx \n\t"
411 "shl %%cl, %%ebx \n\t"
413 "movzbl "MANGLE(ff_h264_mps_state)"(%0), %%ecx \n\t"
414 "movb %%cl, (%1) \n\t"
415 //eax:state ebx:low, edx:range, esi:RangeLPS
416 "test %%bx, %%bx \n\t"
418 "mov "BYTE "(%2), %%"REG_S" \n\t"
419 "subl $0xFFFF, %%ebx \n\t"
420 "movzwl (%%"REG_S"), %%ecx \n\t"
422 "shrl $15, %%ecx \n\t"
423 "add $2, %%"REG_S" \n\t"
424 "addl %%ecx, %%ebx \n\t"
425 "mov %%"REG_S", "BYTE "(%2) \n\t"
428 //eax:state ebx:low, edx:range, esi:RangeLPS
429 "subl %%ecx, %%ebx \n\t"
430 "movl %%esi, %%edx \n\t"
431 "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
432 "shll %%cl, %%ebx \n\t"
433 "shll %%cl, %%edx \n\t"
434 "movzbl "MANGLE(ff_h264_lps_state)"(%0), %%ecx \n\t"
435 "movb %%cl, (%1) \n\t"
437 "test %%bx, %%bx \n\t"
440 "mov "BYTE "(%2), %%"REG_c" \n\t"
441 "movzwl (%%"REG_c"), %%esi \n\t"
443 "shrl $15, %%esi \n\t"
444 "subl $0xFFFF, %%esi \n\t"
445 "add $2, %%"REG_c" \n\t"
446 "mov %%"REG_c", "BYTE "(%2) \n\t"
448 "leal -1(%%ebx), %%ecx \n\t"
449 "xorl %%ebx, %%ecx \n\t"
450 "shrl $15, %%ecx \n\t"
451 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
455 "shll %%cl , %%esi \n\t"
456 "addl %%esi, %%ebx \n\t"
458 "movl %%edx, "RANGE "(%2) \n\t"
459 "movl %%ebx, "LOW "(%2) \n\t"
460 :"=&a"(bit) //FIXME this is fragile gcc either runs out of registers or misscompiles it (for example if "+a"(bit) or "+m"(*state) is used
462 : "%"REG_c, "%ebx", "%edx", "%"REG_S, "memory"
465 #else /* BRANCHLESS_CABAC_DECODER */
468 #if defined CMOV_IS_FAST
469 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
470 "mov "tmp" , %%ecx \n\t"\
471 "shl $17 , "tmp" \n\t"\
472 "cmp "low" , "tmp" \n\t"\
473 "cmova %%ecx , "range" \n\t"\
474 "sbb %%ecx , %%ecx \n\t"\
475 "and %%ecx , "tmp" \n\t"\
476 "sub "tmp" , "low" \n\t"\
477 "xor %%ecx , "ret" \n\t"
478 #else /* CMOV_IS_FAST */
479 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
480 "mov "tmp" , %%ecx \n\t"\
481 "shl $17 , "tmp" \n\t"\
482 "sub "low" , "tmp" \n\t"\
483 "sar $31 , "tmp" \n\t" /*lps_mask*/\
484 "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
485 "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
486 "add %%ecx , "range" \n\t" /*new range*/\
487 "shl $17 , %%ecx \n\t"\
488 "and "tmp" , %%ecx \n\t"\
489 "sub %%ecx , "low" \n\t"\
490 "xor "tmp" , "ret" \n\t"
491 #endif /* CMOV_IS_FAST */
494 #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
495 "movzbl "statep" , "ret" \n\t"\
496 "mov "range" , "tmp" \n\t"\
497 "and $0xC0 , "range" \n\t"\
498 "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
499 "sub "range" , "tmp" \n\t"\
500 BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
501 "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
502 "shl %%cl , "range" \n\t"\
503 "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
504 "mov "tmpbyte" , "statep" \n\t"\
505 "shl %%cl , "low" \n\t"\
506 "test "lowword" , "lowword" \n\t"\
508 "mov "BYTE"("cabac"), %%"REG_c" \n\t"\
509 "movzwl (%%"REG_c") , "tmp" \n\t"\
511 "shr $15 , "tmp" \n\t"\
512 "sub $0xFFFF , "tmp" \n\t"\
513 "add $2 , %%"REG_c" \n\t"\
514 "mov %%"REG_c" , "BYTE "("cabac") \n\t"\
515 "lea -1("low") , %%ecx \n\t"\
516 "xor "low" , %%ecx \n\t"\
517 "shr $15 , %%ecx \n\t"\
518 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
520 "add $7 , %%ecx \n\t"\
521 "shl %%cl , "tmp" \n\t"\
522 "add "tmp" , "low" \n\t"\
526 "movl "RANGE "(%2), %%esi \n\t"
527 "movl "LOW "(%2), %%ebx \n\t"
528 BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl")
529 "movl %%esi, "RANGE "(%2) \n\t"
530 "movl %%ebx, "LOW "(%2) \n\t"
534 : "%"REG_c, "%ebx", "%edx", "%esi", "memory"
537 #endif /* BRANCHLESS_CABAC_DECODER */
538 #else /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
540 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
541 int bit, lps_mask attribute_unused;
543 c->range -= RangeLPS;
544 #ifndef BRANCHLESS_CABAC_DECODER
545 if(c->low < (c->range<<17)){
547 *state= ff_h264_mps_state[s];
548 renorm_cabac_decoder_once(c);
550 bit= ff_h264_norm_shift[RangeLPS];
551 c->low -= (c->range<<17);
552 *state= ff_h264_lps_state[s];
553 c->range = RangeLPS<<bit;
557 if(!(c->low & 0xFFFF)){
561 #else /* BRANCHLESS_CABAC_DECODER */
562 lps_mask= ((c->range<<17) - c->low)>>31;
564 c->low -= (c->range<<17) & lps_mask;
565 c->range += (RangeLPS - c->range) & lps_mask;
568 *state= (ff_h264_mlps_state+128)[s];
571 lps_mask= ff_h264_norm_shift[c->range];
572 c->range<<= lps_mask;
574 if(!(c->low & CABAC_MASK))
576 #endif /* BRANCHLESS_CABAC_DECODER */
577 #endif /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
581 static int __attribute((noinline)) get_cabac_noinline(CABACContext *c, uint8_t * const state){
582 return get_cabac_inline(c,state);
585 static int get_cabac(CABACContext *c, uint8_t * const state){
586 return get_cabac_inline(c,state);
589 static int get_cabac_bypass(CABACContext *c){
593 "movl "RANGE "(%1), %%ebx \n\t"
594 "movl "LOW "(%1), %%eax \n\t"
595 "shl $17, %%ebx \n\t"
596 "add %%eax, %%eax \n\t"
597 "sub %%ebx, %%eax \n\t"
599 "and %%edx, %%ebx \n\t"
600 "add %%ebx, %%eax \n\t"
601 "test %%ax, %%ax \n\t"
603 "movl "BYTE "(%1), %%"REG_b" \n\t"
604 "subl $0xFFFF, %%eax \n\t"
605 "movzwl (%%"REG_b"), %%ecx \n\t"
607 "shrl $15, %%ecx \n\t"
608 "addl $2, %%"REG_b" \n\t"
609 "addl %%ecx, %%eax \n\t"
610 "movl %%"REG_b", "BYTE "(%1) \n\t"
612 "movl %%eax, "LOW "(%1) \n\t"
616 : "%eax", "%"REG_b, "%ecx", "memory"
623 if(!(c->low & CABAC_MASK))
637 static always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
640 "movl "RANGE "(%1), %%ebx \n\t"
641 "movl "LOW "(%1), %%eax \n\t"
642 "shl $17, %%ebx \n\t"
643 "add %%eax, %%eax \n\t"
644 "sub %%ebx, %%eax \n\t"
646 "and %%edx, %%ebx \n\t"
647 "add %%ebx, %%eax \n\t"
648 "xor %%edx, %%ecx \n\t"
649 "sub %%edx, %%ecx \n\t"
650 "test %%ax, %%ax \n\t"
652 "mov "BYTE "(%1), %%"REG_b" \n\t"
653 "subl $0xFFFF, %%eax \n\t"
654 "movzwl (%%"REG_b"), %%edx \n\t"
656 "shrl $15, %%edx \n\t"
657 "add $2, %%"REG_b" \n\t"
658 "addl %%edx, %%eax \n\t"
659 "mov %%"REG_b", "BYTE "(%1) \n\t"
661 "movl %%eax, "LOW "(%1) \n\t"
665 : "%eax", "%"REG_b, "%edx", "memory"
672 if(!(c->low & CABAC_MASK))
680 return (val^mask)-mask;
684 //FIXME the x86 code from this file should be moved into i386/h264 or cabac something.c/h (note ill kill you if you move my code away from under my fingers before iam finished with it!)
685 //FIXME use some macros to avoid duplicatin get_cabac (cant be done yet as that would make optimization work hard)
687 static int decode_significance_x86(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index){
688 void *end= significant_coeff_ctx_base + max_coeff - 1;
689 int minusstart= -(int)significant_coeff_ctx_base;
690 int minusindex= 4-(int)index;
693 "movl "RANGE "(%3), %%esi \n\t"
694 "movl "LOW "(%3), %%ebx \n\t"
698 BRANCHLESS_GET_CABAC("%%edx", "%3", "(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
700 "test $1, %%edx \n\t"
703 BRANCHLESS_GET_CABAC("%%edx", "%3", "61(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
705 "mov %2, %%"REG_a" \n\t"
706 "movl %4, %%ecx \n\t"
707 "add %1, %%"REG_c" \n\t"
708 "movl %%ecx, (%%"REG_a") \n\t"
710 "test $1, %%edx \n\t"
713 "add $4, %%"REG_a" \n\t"
714 "mov %%"REG_a", %2 \n\t"
720 "mov %2, %%"REG_a" \n\t"
721 "movl %4, %%ecx \n\t"
722 "add %1, %%"REG_c" \n\t"
723 "movl %%ecx, (%%"REG_a") \n\t"
728 "movl %%esi, "RANGE "(%3) \n\t"
729 "movl %%ebx, "LOW "(%3) \n\t"
730 :"=&a"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index)\
731 :"r"(c), "m"(minusstart), "m"(end), "m"(minusindex)\
732 : "%"REG_c, "%ebx", "%edx", "%esi", "memory"\
737 static int decode_significance_8x8_x86(CABACContext *c, uint8_t *significant_coeff_ctx_base, int *index, uint8_t *sig_off){
738 int minusindex= 4-(int)index;
742 "movl "RANGE "(%3), %%esi \n\t"
743 "movl "LOW "(%3), %%ebx \n\t"
745 "mov %1, %%"REG_D" \n\t"
748 "mov %6, %%"REG_a" \n\t"
749 "movzbl (%%"REG_a", %%"REG_D"), %%edi \n\t"
750 "add %5, %%"REG_D" \n\t"
752 BRANCHLESS_GET_CABAC("%%edx", "%3", "(%%"REG_D")", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
755 "test $1, %%edx \n\t"
758 "movzbl "MANGLE(last_coeff_flag_offset_8x8)"(%%edi), %%edi\n\t"
759 "add %5, %%"REG_D" \n\t"
761 BRANCHLESS_GET_CABAC("%%edx", "%3", "15(%%"REG_D")", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
763 "mov %2, %%"REG_a" \n\t"
765 "movl %%edi, (%%"REG_a") \n\t"
767 "test $1, %%edx \n\t"
770 "add $4, %%"REG_a" \n\t"
771 "mov %%"REG_a", %2 \n\t"
774 "addl $1, %%edi \n\t"
776 "cmpl $63, %%edi \n\t"
778 "mov %2, %%"REG_a" \n\t"
779 "movl %%edi, (%%"REG_a") \n\t"
781 "addl %4, %%eax \n\t"
784 "movl %%esi, "RANGE "(%3) \n\t"
785 "movl %%ebx, "LOW "(%3) \n\t"
786 :"=&a"(coeff_count),"+m"(last), "+m"(index)\
787 :"r"(c), "m"(minusindex), "m"(significant_coeff_ctx_base), "m"(sig_off)\
788 : "%"REG_c, "%ebx", "%edx", "%esi", "%"REG_D, "memory"\
796 * @return the number of bytes read or 0 if no end
798 static int get_cabac_terminate(CABACContext *c){
800 if(c->low < c->range<<17){
801 renorm_cabac_decoder_once(c);
804 return c->bytestream - c->bytestream_start;
809 * get (truncated) unnary binarization.
811 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
814 for(i=0; i<max; i++){
815 if(get_cabac(c, state)==0)
818 if(i< max_index) state++;
821 return truncated ? max : -1;
825 * get unary exp golomb k-th order binarization.
827 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
831 if(get_cabac(c, state)==0)
834 if(0 < max_index) state++;
836 for(i=1; i<max; i++){
837 if(get_cabac(c, state)==0){
838 if(is_signed && get_cabac_bypass(c)){
844 if(i < max_index) state++;
847 while(get_cabac_bypass(c)){
854 v+= v + get_cabac_bypass(c);
858 if(is_signed && get_cabac_bypass(c)){