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
24 * Context Adaptive Binary Arithmetic Coder.
27 #ifndef FFMPEG_CABAC_H
28 #define FFMPEG_CABAC_H
30 #include "bitstream.h"
35 #include "libavutil/x86_cpu.h"
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
++;
94 static void put_cabac(CABACContext
*c
, uint8_t * const state
, int bit
){
95 int RangeLPS
= ff_h264_lps_range
[2*(c
->range
&0xC0) + *state
];
97 if(bit
== ((*state
)&1)){
99 *state
= ff_h264_mps_state
[*state
];
101 c
->low
+= c
->range
- RangeLPS
;
103 *state
= ff_h264_lps_state
[*state
];
106 renorm_cabac_encoder(c
);
113 static void put_cabac_static(CABACContext
*c
, int RangeLPS
, int bit
){
114 assert(c
->range
> RangeLPS
);
117 c
->range
-= RangeLPS
;
119 c
->low
+= c
->range
- RangeLPS
;
123 renorm_cabac_encoder(c
);
131 * @param bit 0 -> write zero bit, !=0 write one bit
133 static void put_cabac_bypass(CABACContext
*c
, int bit
){
142 }else if(c
->low
<0x400){
143 c
->outstanding_count
++;
157 * @return the number of bytes written
159 static int put_cabac_terminate(CABACContext
*c
, int bit
){
163 renorm_cabac_encoder(c
);
168 renorm_cabac_encoder(c
);
170 assert(c
->low
<= 0x1FF);
171 put_cabac_bit(c
, c
->low
>>9);
172 put_bits(&c
->pb
, 2, ((c
->low
>>7)&3)|1);
174 flush_put_bits(&c
->pb
); //FIXME FIXME FIXME XXX wrong
181 return (put_bits_count(&c
->pb
)+7)>>3;
185 * put (truncated) unary binarization.
187 static void put_cabac_u(CABACContext
*c
, uint8_t * state
, int v
, int max
, int max_index
, int truncated
){
194 put_cabac(c
, state
, 1);
195 if(i
< max_index
) state
++;
197 if(truncated
==0 || v
<max
)
198 put_cabac(c
, state
, 0);
202 put_cabac(c
, state
+i
, 1);
204 if(truncated
==0 || v
<max
)
205 put_cabac(c
, state
+i
, 0);
207 for(i
=0; i
<=max_index
; i
++){
208 put_cabac(c
, state
+i
, 1);
211 put_cabac(c
, state
+max_index
, 1);
213 if(truncated
==0 || v
<max
)
214 put_cabac(c
, state
+max_index
, 0);
220 * put unary exp golomb k-th order binarization.
222 static void put_cabac_ueg(CABACContext
*c
, uint8_t * state
, int v
, int max
, int is_signed
, int k
, int max_index
){
226 put_cabac(c
, state
, 0);
228 const int sign
= v
< 0;
230 if(is_signed
) v
= FFABS(v
);
234 put_cabac(c
, state
, 1);
235 if(i
< max_index
) state
++;
238 put_cabac(c
, state
, 0);
242 for(i
=0; i
<max
; i
++){
243 put_cabac(c
, state
, 1);
244 if(i
< max_index
) state
++;
248 while(v
>= m
){ //FIXME optimize
249 put_cabac_bypass(c
, 1);
253 put_cabac_bypass(c
, 0);
255 put_cabac_bypass(c
, v
&m
);
260 put_cabac_bypass(c
, sign
);
265 static void refill(CABACContext
*c
){
267 c
->low
+= (c
->bytestream
[0]<<9) + (c
->bytestream
[1]<<1);
269 c
->low
+= c
->bytestream
[0]<<1;
271 c
->low
-= CABAC_MASK
;
272 c
->bytestream
+= CABAC_BITS
/8;
275 #if ! ( defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) )
276 static void refill2(CABACContext
*c
){
279 x
= c
->low
^ (c
->low
-1);
280 i
= 7 - ff_h264_norm_shift
[x
>>(CABAC_BITS
-1)];
285 x
+= (c
->bytestream
[0]<<9) + (c
->bytestream
[1]<<1);
287 x
+= c
->bytestream
[0]<<1;
291 c
->bytestream
+= CABAC_BITS
/8;
295 static inline void renorm_cabac_decoder(CABACContext
*c
){
296 while(c
->range
< 0x100){
299 if(!(c
->low
& CABAC_MASK
))
304 static inline void renorm_cabac_decoder_once(CABACContext
*c
){
305 #ifdef ARCH_X86_DISABLED
310 "lea -0x100(%0), %2 \n\t"
311 "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
314 : "+r"(c
->range
), "+r"(c
->low
), "+c"(temp
)
319 "cmp $0x100, %0 \n\t"
320 "setb %%cl \n\t" //FIXME 31->63 for x86-64
323 : "+r"(c
->range
), "+r"(c
->low
), "+c"(temp
)
329 "lea -0x100(%0), %%eax \n\t"
336 : "+r"(c
->range
), "+r"(c
->low
), "+a"(temp
), "+d"(temp2
)
342 "cmp $0x100, %0 \n\t"
343 "sbb %%edx, %%edx \n\t"
349 : "+r"(c
->range
), "+r"(c
->low
), "+a"(temp
), "+d"(temp2
)
355 "cmp $0x100, %0 \n\t"
356 "lea (%0, %0), %%eax \n\t"
357 "lea (%1, %1), %%edx \n\t"
358 "cmovb %%eax, %0 \n\t"
359 "cmovb %%edx, %1 \n\t"
360 : "+r"(c
->range
), "+r"(c
->low
), "+a"(temp
), "+d"(temp2
)
365 int shift
= (uint32_t)(c
->range
- 0x100)>>31;
369 if(!(c
->low
& CABAC_MASK
))
373 static av_always_inline
int get_cabac_inline(CABACContext
*c
, uint8_t * const state
){
374 //FIXME gcc generates duplicate load/stores for c->low and c->range
378 #define BYTESTART "16"
382 #define BYTESTART "12"
386 #if defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
389 #ifndef BRANCHLESS_CABAC_DECODER
391 "movzbl (%1), %0 \n\t"
392 "movl "RANGE
"(%2), %%ebx \n\t"
393 "movl "RANGE
"(%2), %%edx \n\t"
394 "andl $0xC0, %%ebx \n\t"
395 "movzbl "MANGLE(ff_h264_lps_range
)"(%0, %%ebx, 2), %%esi\n\t"
396 "movl "LOW
"(%2), %%ebx \n\t"
397 //eax:state ebx:low, edx:range, esi:RangeLPS
398 "subl %%esi, %%edx \n\t"
399 "movl %%edx, %%ecx \n\t"
400 "shll $17, %%ecx \n\t"
401 "cmpl %%ecx, %%ebx \n\t"
405 //athlon:4067 P3:4110
406 "lea -0x100(%%edx), %%ecx \n\t"
407 "shr $31, %%ecx \n\t"
408 "shl %%cl, %%edx \n\t"
409 "shl %%cl, %%ebx \n\t"
411 //athlon:4057 P3:4130
412 "cmp $0x100, %%edx \n\t" //FIXME avoidable
414 "shl %%cl, %%edx \n\t"
415 "shl %%cl, %%ebx \n\t"
417 "movzbl "MANGLE(ff_h264_mps_state
)"(%0), %%ecx \n\t"
418 "movb %%cl, (%1) \n\t"
419 //eax:state ebx:low, edx:range, esi:RangeLPS
420 "test %%bx, %%bx \n\t"
422 "mov "BYTE
"(%2), %%"REG_S
" \n\t"
423 "subl $0xFFFF, %%ebx \n\t"
424 "movzwl (%%"REG_S
"), %%ecx \n\t"
426 "shrl $15, %%ecx \n\t"
427 "add $2, %%"REG_S
" \n\t"
428 "addl %%ecx, %%ebx \n\t"
429 "mov %%"REG_S
", "BYTE
"(%2) \n\t"
432 //eax:state ebx:low, edx:range, esi:RangeLPS
433 "subl %%ecx, %%ebx \n\t"
434 "movl %%esi, %%edx \n\t"
435 "movzbl " MANGLE(ff_h264_norm_shift
) "(%%esi), %%ecx \n\t"
436 "shll %%cl, %%ebx \n\t"
437 "shll %%cl, %%edx \n\t"
438 "movzbl "MANGLE(ff_h264_lps_state
)"(%0), %%ecx \n\t"
439 "movb %%cl, (%1) \n\t"
441 "test %%bx, %%bx \n\t"
444 "mov "BYTE
"(%2), %%"REG_c
" \n\t"
445 "movzwl (%%"REG_c
"), %%esi \n\t"
447 "shrl $15, %%esi \n\t"
448 "subl $0xFFFF, %%esi \n\t"
449 "add $2, %%"REG_c
" \n\t"
450 "mov %%"REG_c
", "BYTE
"(%2) \n\t"
452 "leal -1(%%ebx), %%ecx \n\t"
453 "xorl %%ebx, %%ecx \n\t"
454 "shrl $15, %%ecx \n\t"
455 "movzbl " MANGLE(ff_h264_norm_shift
) "(%%ecx), %%ecx \n\t"
459 "shll %%cl , %%esi \n\t"
460 "addl %%esi, %%ebx \n\t"
462 "movl %%edx, "RANGE
"(%2) \n\t"
463 "movl %%ebx, "LOW
"(%2) \n\t"
464 :"=&a"(bit
) //FIXME this is fragile gcc either runs out of registers or miscompiles it (for example if "+a"(bit) or "+m"(*state) is used
466 : "%"REG_c
, "%ebx", "%edx", "%"REG_S
, "memory"
469 #else /* BRANCHLESS_CABAC_DECODER */
472 #if defined HAVE_FAST_CMOV
473 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
474 "mov "tmp" , %%ecx \n\t"\
475 "shl $17 , "tmp" \n\t"\
476 "cmp "low" , "tmp" \n\t"\
477 "cmova %%ecx , "range" \n\t"\
478 "sbb %%ecx , %%ecx \n\t"\
479 "and %%ecx , "tmp" \n\t"\
480 "sub "tmp" , "low" \n\t"\
481 "xor %%ecx , "ret" \n\t"
482 #else /* HAVE_FAST_CMOV */
483 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
484 "mov "tmp" , %%ecx \n\t"\
485 "shl $17 , "tmp" \n\t"\
486 "sub "low" , "tmp" \n\t"\
487 "sar $31 , "tmp" \n\t" /*lps_mask*/\
488 "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
489 "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
490 "add %%ecx , "range" \n\t" /*new range*/\
491 "shl $17 , %%ecx \n\t"\
492 "and "tmp" , %%ecx \n\t"\
493 "sub %%ecx , "low" \n\t"\
494 "xor "tmp" , "ret" \n\t"
495 #endif /* HAVE_FAST_CMOV */
498 #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
499 "movzbl "statep" , "ret" \n\t"\
500 "mov "range" , "tmp" \n\t"\
501 "and $0xC0 , "range" \n\t"\
502 "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
503 "sub "range" , "tmp" \n\t"\
504 BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
505 "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
506 "shl %%cl , "range" \n\t"\
507 "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
508 "mov "tmpbyte" , "statep" \n\t"\
509 "shl %%cl , "low" \n\t"\
510 "test "lowword" , "lowword" \n\t"\
512 "mov "BYTE"("cabac"), %%"REG_c" \n\t"\
513 "movzwl (%%"REG_c") , "tmp" \n\t"\
515 "shr $15 , "tmp" \n\t"\
516 "sub $0xFFFF , "tmp" \n\t"\
517 "add $2 , %%"REG_c" \n\t"\
518 "mov %%"REG_c" , "BYTE "("cabac") \n\t"\
519 "lea -1("low") , %%ecx \n\t"\
520 "xor "low" , %%ecx \n\t"\
521 "shr $15 , %%ecx \n\t"\
522 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
524 "add $7 , %%ecx \n\t"\
525 "shl %%cl , "tmp" \n\t"\
526 "add "tmp" , "low" \n\t"\
530 "movl "RANGE
"(%2), %%esi \n\t"
531 "movl "LOW
"(%2), %%ebx \n\t"
532 BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl")
533 "movl %%esi, "RANGE
"(%2) \n\t"
534 "movl %%ebx, "LOW
"(%2) \n\t"
538 : "%"REG_c
, "%ebx", "%edx", "%esi", "memory"
541 #endif /* BRANCHLESS_CABAC_DECODER */
542 #else /* defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) */
544 int RangeLPS
= ff_h264_lps_range
[2*(c
->range
&0xC0) + s
];
545 int bit
, lps_mask av_unused
;
547 c
->range
-= RangeLPS
;
548 #ifndef BRANCHLESS_CABAC_DECODER
549 if(c
->low
< (c
->range
<<(CABAC_BITS
+1))){
551 *state
= ff_h264_mps_state
[s
];
552 renorm_cabac_decoder_once(c
);
554 bit
= ff_h264_norm_shift
[RangeLPS
];
555 c
->low
-= (c
->range
<<(CABAC_BITS
+1));
556 *state
= ff_h264_lps_state
[s
];
557 c
->range
= RangeLPS
<<bit
;
561 if(!(c
->low
& CABAC_MASK
)){
565 #else /* BRANCHLESS_CABAC_DECODER */
566 lps_mask
= ((c
->range
<<(CABAC_BITS
+1)) - c
->low
)>>31;
568 c
->low
-= (c
->range
<<(CABAC_BITS
+1)) & lps_mask
;
569 c
->range
+= (RangeLPS
- c
->range
) & lps_mask
;
572 *state
= (ff_h264_mlps_state
+128)[s
];
575 lps_mask
= ff_h264_norm_shift
[c
->range
];
576 c
->range
<<= lps_mask
;
578 if(!(c
->low
& CABAC_MASK
))
580 #endif /* BRANCHLESS_CABAC_DECODER */
581 #endif /* defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) */
585 static int av_noinline
get_cabac_noinline(CABACContext
*c
, uint8_t * const state
){
586 return get_cabac_inline(c
,state
);
589 static int get_cabac(CABACContext
*c
, uint8_t * const state
){
590 return get_cabac_inline(c
,state
);
593 static int get_cabac_bypass(CABACContext
*c
){
597 "movl "RANGE
"(%1), %%ebx \n\t"
598 "movl "LOW
"(%1), %%eax \n\t"
599 "shl $17, %%ebx \n\t"
600 "add %%eax, %%eax \n\t"
601 "sub %%ebx, %%eax \n\t"
603 "and %%edx, %%ebx \n\t"
604 "add %%ebx, %%eax \n\t"
605 "test %%ax, %%ax \n\t"
607 "movl "BYTE
"(%1), %%"REG_b
" \n\t"
608 "subl $0xFFFF, %%eax \n\t"
609 "movzwl (%%"REG_b
"), %%ecx \n\t"
611 "shrl $15, %%ecx \n\t"
612 "addl $2, %%"REG_b
" \n\t"
613 "addl %%ecx, %%eax \n\t"
614 "movl %%"REG_b
", "BYTE
"(%1) \n\t"
616 "movl %%eax, "LOW
"(%1) \n\t"
620 : "%eax", "%"REG_b
, "%ecx", "memory"
627 if(!(c
->low
& CABAC_MASK
))
630 range
= c
->range
<<(CABAC_BITS
+1);
641 static av_always_inline
int get_cabac_bypass_sign(CABACContext
*c
, int val
){
642 #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
644 "movl "RANGE
"(%1), %%ebx \n\t"
645 "movl "LOW
"(%1), %%eax \n\t"
646 "shl $17, %%ebx \n\t"
647 "add %%eax, %%eax \n\t"
648 "sub %%ebx, %%eax \n\t"
650 "and %%edx, %%ebx \n\t"
651 "add %%ebx, %%eax \n\t"
652 "xor %%edx, %%ecx \n\t"
653 "sub %%edx, %%ecx \n\t"
654 "test %%ax, %%ax \n\t"
656 "mov "BYTE
"(%1), %%"REG_b
" \n\t"
657 "subl $0xFFFF, %%eax \n\t"
658 "movzwl (%%"REG_b
"), %%edx \n\t"
660 "shrl $15, %%edx \n\t"
661 "add $2, %%"REG_b
" \n\t"
662 "addl %%edx, %%eax \n\t"
663 "mov %%"REG_b
", "BYTE
"(%1) \n\t"
665 "movl %%eax, "LOW
"(%1) \n\t"
669 : "%eax", "%"REG_b
, "%edx", "memory"
676 if(!(c
->low
& CABAC_MASK
))
679 range
= c
->range
<<(CABAC_BITS
+1);
684 return (val
^mask
)-mask
;
690 * @return the number of bytes read or 0 if no end
692 static int get_cabac_terminate(CABACContext
*c
){
694 if(c
->low
< c
->range
<<(CABAC_BITS
+1)){
695 renorm_cabac_decoder_once(c
);
698 return c
->bytestream
- c
->bytestream_start
;
704 * Get (truncated) unary binarization.
706 static int get_cabac_u(CABACContext
*c
, uint8_t * state
, int max
, int max_index
, int truncated
){
709 for(i
=0; i
<max
; i
++){
710 if(get_cabac(c
, state
)==0)
713 if(i
< max_index
) state
++;
716 return truncated
? max
: -1;
720 * get unary exp golomb k-th order binarization.
722 static int get_cabac_ueg(CABACContext
*c
, uint8_t * state
, int max
, int is_signed
, int k
, int max_index
){
726 if(get_cabac(c
, state
)==0)
729 if(0 < max_index
) state
++;
731 for(i
=1; i
<max
; i
++){
732 if(get_cabac(c
, state
)==0){
733 if(is_signed
&& get_cabac_bypass(c
)){
739 if(i
< max_index
) state
++;
742 while(get_cabac_bypass(c
)){
749 v
+= v
+ get_cabac_bypass(c
);
753 if(is_signed
&& get_cabac_bypass(c
)){
760 #endif /* FFMPEG_CABAC_H */