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.
33 #define CABAC_MASK ((1<<CABAC_BITS)-1)
34 #define BRANCHLESS_CABAC_DECODER 1
36 typedef struct CABACContext{
39 int outstanding_count;
43 uint8_t lps_range[2*65][4]; ///< rangeTabLPS
44 uint8_t lps_state[2*64]; ///< transIdxLPS
45 uint8_t mps_state[2*64]; ///< transIdxMPS
46 const uint8_t *bytestream_start;
47 const uint8_t *bytestream;
48 const uint8_t *bytestream_end;
52 extern const uint8_t ff_h264_lps_range[64][4];
53 extern const uint8_t ff_h264_mps_state[64];
54 extern const uint8_t ff_h264_lps_state[64];
55 extern const uint8_t ff_h264_norm_shift[128];
58 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
59 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
60 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
61 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
64 static inline void put_cabac_bit(CABACContext *c, int b){
65 put_bits(&c->pb, 1, b);
66 for(;c->outstanding_count; c->outstanding_count--){
67 put_bits(&c->pb, 1, 1-b);
71 static inline void renorm_cabac_encoder(CABACContext *c){
72 while(c->range < 0x100){
76 }else if(c->low<0x200){
77 c->outstanding_count++;
89 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
90 int RangeLPS= c->lps_range[*state][c->range>>6];
92 if(bit == ((*state)&1)){
94 *state= c->mps_state[*state];
96 c->low += c->range - RangeLPS;
98 *state= c->lps_state[*state];
101 renorm_cabac_encoder(c);
108 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
109 assert(c->range > RangeLPS);
112 c->range -= RangeLPS;
114 c->low += c->range - RangeLPS;
118 renorm_cabac_encoder(c);
126 * @param bit 0 -> write zero bit, !=0 write one bit
128 static void put_cabac_bypass(CABACContext *c, int bit){
137 }else if(c->low<0x400){
138 c->outstanding_count++;
152 * @return the number of bytes written
154 static int put_cabac_terminate(CABACContext *c, int bit){
158 renorm_cabac_encoder(c);
163 renorm_cabac_encoder(c);
165 assert(c->low <= 0x1FF);
166 put_cabac_bit(c, c->low>>9);
167 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
169 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
176 return (put_bits_count(&c->pb)+7)>>3;
180 * put (truncated) unary binarization.
182 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
189 put_cabac(c, state, 1);
190 if(i < max_index) state++;
192 if(truncated==0 || v<max)
193 put_cabac(c, state, 0);
197 put_cabac(c, state+i, 1);
199 if(truncated==0 || v<max)
200 put_cabac(c, state+i, 0);
202 for(i=0; i<=max_index; i++){
203 put_cabac(c, state+i, 1);
206 put_cabac(c, state+max_index, 1);
208 if(truncated==0 || v<max)
209 put_cabac(c, state+max_index, 0);
215 * put unary exp golomb k-th order binarization.
217 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
221 put_cabac(c, state, 0);
223 const int sign= v < 0;
225 if(is_signed) v= ABS(v);
229 put_cabac(c, state, 1);
230 if(i < max_index) state++;
233 put_cabac(c, state, 0);
237 for(i=0; i<max; i++){
238 put_cabac(c, state, 1);
239 if(i < max_index) state++;
243 while(v >= m){ //FIXME optimize
244 put_cabac_bypass(c, 1);
248 put_cabac_bypass(c, 0);
250 put_cabac_bypass(c, v&m);
255 put_cabac_bypass(c, sign);
259 static void refill(CABACContext *c){
261 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
263 c->low+= c->bytestream[0]<<1;
265 c->low -= CABAC_MASK;
266 c->bytestream+= CABAC_BITS/8;
269 static void refill2(CABACContext *c){
272 x= c->low ^ (c->low-1);
273 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
278 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
280 x+= c->bytestream[0]<<1;
284 c->bytestream+= CABAC_BITS/8;
287 static inline void renorm_cabac_decoder(CABACContext *c){
288 while(c->range < (0x200 << CABAC_BITS)){
291 if(!(c->low & CABAC_MASK))
296 static inline void renorm_cabac_decoder_once(CABACContext *c){
297 #ifdef ARCH_X86_DISABLED
302 "lea -0x2000000(%0), %2 \n\t"
303 "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
306 : "+r"(c->range), "+r"(c->low), "+c"(temp)
311 "cmp $0x2000000, %0 \n\t"
312 "setb %%cl \n\t" //FIXME 31->63 for x86-64
315 : "+r"(c->range), "+r"(c->low), "+c"(temp)
321 "lea -0x2000000(%0), %%eax \n\t"
328 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
334 "cmp $0x2000000, %0 \n\t"
335 "sbb %%edx, %%edx \n\t"
341 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
347 "cmp $0x2000000, %0 \n\t"
348 "lea (%0, %0), %%eax \n\t"
349 "lea (%1, %1), %%edx \n\t"
350 "cmovb %%eax, %0 \n\t"
351 "cmovb %%edx, %1 \n\t"
352 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
357 int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31;
361 if(!(c->low & CABAC_MASK))
365 static int get_cabac(CABACContext *c, uint8_t * const state){
366 //FIXME gcc generates duplicate load/stores for c->low and c->range
372 #define LPS_RANGE "12"
373 #define LPS_STATE "12+2*65*4"
374 #define MPS_STATE "12+2*65*4+2*64"
375 #define BYTESTART "12+2*65*4+4*64"
376 #define BYTE "16+2*65*4+4*64"
377 #define BYTEEND "20+2*65*4+4*64"
378 #ifndef BRANCHLESS_CABAC_DECODER
380 "movzbl (%1), %%eax \n\t"
381 "movl "RANGE "(%2), %%ebx \n\t"
382 "movl "RANGE "(%2), %%edx \n\t"
383 "shrl $23, %%ebx \n\t"
384 "leal "LPS_RANGE"(%2, %%eax, 4), %%esi \n\t"
385 "movzbl (%%ebx, %%esi), %%esi \n\t"
386 "shll $17, %%esi \n\t"
387 "movl "LOW "(%2), %%ebx \n\t"
388 //eax:state ebx:low, edx:range, esi:RangeLPS
389 "subl %%esi, %%edx \n\t"
390 "cmpl %%edx, %%ebx \n\t"
392 "cmp $0x2000000, %%edx \n\t" //FIXME avoidable
394 "shl %%cl, %%edx \n\t"
395 "shl %%cl, %%ebx \n\t"
396 "movzbl "MPS_STATE"(%2, %%eax), %%ecx \n\t"
397 "movb %%cl, (%1) \n\t"
398 //eax:state ebx:low, edx:range, esi:RangeLPS
399 "test %%bx, %%bx \n\t"
401 "movl "BYTE "(%2), %%esi \n\t"
402 "subl $0xFFFF, %%ebx \n\t"
403 "movzwl (%%esi), %%ecx \n\t"
405 "shrl $15, %%ecx \n\t"
406 "addl $2, %%esi \n\t"
407 "addl %%ecx, %%ebx \n\t"
408 "movl %%esi, "BYTE "(%2) \n\t"
411 //eax:state ebx:low, edx:range, esi:RangeLPS
412 "subl %%edx, %%ebx \n\t"
413 "movl %%esi, %%edx \n\t"
414 "shr $19, %%esi \n\t"
415 "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
416 "shll %%cl, %%ebx \n\t"
417 "shll %%cl, %%edx \n\t"
418 "movzbl "LPS_STATE"(%2, %%eax), %%ecx \n\t"
419 "movb %%cl, (%1) \n\t"
420 "addl $1, %%eax \n\t"
421 "test %%bx, %%bx \n\t"
424 "movl "BYTE "(%2), %%ecx \n\t"
425 "movzwl (%%ecx), %%esi \n\t"
427 "shrl $15, %%esi \n\t"
428 "subl $0xFFFF, %%esi \n\t"
429 "addl $2, %%ecx \n\t"
430 "movl %%ecx, "BYTE "(%2) \n\t"
432 "leal -1(%%ebx), %%ecx \n\t"
433 "xorl %%ebx, %%ecx \n\t"
434 "shrl $17, %%ecx \n\t"
435 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
439 "shll %%cl , %%esi \n\t"
440 "addl %%esi, %%ebx \n\t"
442 "movl %%edx, "RANGE "(%2) \n\t"
443 "movl %%ebx, "LOW "(%2) \n\t"
444 :"=&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
446 : "%ecx", "%ebx", "%edx", "%esi"
451 "movzbl (%1), %%eax \n\t"
452 "movl "RANGE "(%2), %%ebx \n\t"
453 "movl "RANGE "(%2), %%edx \n\t"
454 "shrl $23, %%ebx \n\t"
455 "leal "LPS_RANGE"(%2, %%eax, 4), %%esi \n\t"
456 "movzbl (%%ebx, %%esi), %%esi \n\t"
457 "shll $17, %%esi \n\t"
458 "movl "LOW "(%2), %%ebx \n\t"
459 //eax:state ebx:low, edx:range, esi:RangeLPS
460 "subl %%esi, %%edx \n\t"
461 #ifdef CMOV_IS_FAST //FIXME actually define this somewhere
462 "cmpl %%ebx, %%edx \n\t"
463 "cmova %%edx, %%esi \n\t"
464 "sbbl %%ecx, %%ecx \n\t"
465 "andl %%ecx, %%edx \n\t"
466 "subl %%edx, %%ebx \n\t"
467 "xorl %%ecx, %%eax \n\t"
469 "movl %%edx, %%ecx \n\t"
470 "subl %%ebx, %%edx \n\t"
471 "sarl $31, %%edx \n\t" //lps_mask
472 "subl %%ecx, %%esi \n\t" //RangeLPS - range
473 "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
474 "addl %%ecx, %%esi \n\t" //new range
475 "andl %%edx, %%ecx \n\t"
476 "subl %%ecx, %%ebx \n\t"
477 "xorl %%edx, %%eax \n\t"
480 //eax:state ebx:low edx:mask esi:range
481 "movzbl "MPS_STATE"(%2, %%eax), %%ecx \n\t"
482 "movb %%cl, (%1) \n\t"
484 "movl %%esi, %%edx \n\t"
485 //eax:bit ebx:low edx:range esi:range
487 "shr $19, %%esi \n\t"
488 "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
489 "shll %%cl, %%ebx \n\t"
490 "shll %%cl, %%edx \n\t"
491 "test %%bx, %%bx \n\t"
494 "movl "BYTE "(%2), %%ecx \n\t"
495 "movzwl (%%ecx), %%esi \n\t"
497 "shrl $15, %%esi \n\t"
498 "subl $0xFFFF, %%esi \n\t"
499 "addl $2, %%ecx \n\t"
500 "movl %%ecx, "BYTE "(%2) \n\t"
502 "leal -1(%%ebx), %%ecx \n\t"
503 "xorl %%ebx, %%ecx \n\t"
504 "shrl $17, %%ecx \n\t"
505 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
509 "shll %%cl , %%esi \n\t"
510 "addl %%esi, %%ebx \n\t"
512 "movl %%edx, "RANGE "(%2) \n\t"
513 "movl %%ebx, "LOW "(%2) \n\t"
516 : "%ecx", "%ebx", "%edx", "%esi"
522 int RangeLPS= c->lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
523 int bit, lps_mask attribute_unused;
525 c->range -= RangeLPS;
526 #ifndef BRANCHLESS_CABAC_DECODER
527 if(c->low < c->range){
529 *state= c->mps_state[s];
530 renorm_cabac_decoder_once(c);
532 bit= ff_h264_norm_shift[RangeLPS>>19];
534 *state= c->lps_state[s];
535 c->range = RangeLPS<<bit;
539 if(!(c->low & 0xFFFF)){
544 lps_mask= (c->range - c->low)>>31;
546 c->low -= c->range & lps_mask;
547 c->range += (RangeLPS - c->range) & lps_mask;
550 *state= c->mps_state[s];
553 lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+3)];
554 c->range<<= lps_mask;
556 if(!(c->low & CABAC_MASK))
563 static int get_cabac_bypass(CABACContext *c){
566 if(!(c->low & CABAC_MASK))
569 if(c->low < c->range){
579 * @return the number of bytes read or 0 if no end
581 static int get_cabac_terminate(CABACContext *c){
582 c->range -= 4<<CABAC_BITS;
583 if(c->low < c->range){
584 renorm_cabac_decoder_once(c);
587 return c->bytestream - c->bytestream_start;
592 * get (truncated) unnary binarization.
594 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
597 for(i=0; i<max; i++){
598 if(get_cabac(c, state)==0)
601 if(i< max_index) state++;
604 return truncated ? max : -1;
608 * get unary exp golomb k-th order binarization.
610 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
614 if(get_cabac(c, state)==0)
617 if(0 < max_index) state++;
619 for(i=1; i<max; i++){
620 if(get_cabac(c, state)==0){
621 if(is_signed && get_cabac_bypass(c)){
627 if(i < max_index) state++;
630 while(get_cabac_bypass(c)){
637 v+= v + get_cabac_bypass(c);
641 if(is_signed && get_cabac_bypass(c)){