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
35 #define CMOV_IS_FAST 1
37 typedef struct CABACContext{
40 int outstanding_count;
44 const uint8_t *bytestream_start;
45 const uint8_t *bytestream;
46 const uint8_t *bytestream_end;
50 extern uint8_t ff_h264_lps_range[2*65][4]; ///< rangeTabLPS
51 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
52 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
53 extern const uint8_t ff_h264_norm_shift[128];
56 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
57 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
58 void ff_init_cabac_states(CABACContext *c);
61 static inline void put_cabac_bit(CABACContext *c, int b){
62 put_bits(&c->pb, 1, b);
63 for(;c->outstanding_count; c->outstanding_count--){
64 put_bits(&c->pb, 1, 1-b);
68 static inline void renorm_cabac_encoder(CABACContext *c){
69 while(c->range < 0x100){
73 }else if(c->low<0x200){
74 c->outstanding_count++;
86 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
87 int RangeLPS= ff_h264_lps_range[*state][c->range>>6];
89 if(bit == ((*state)&1)){
91 *state= ff_h264_mps_state[*state];
93 c->low += c->range - RangeLPS;
95 *state= ff_h264_lps_state[*state];
98 renorm_cabac_encoder(c);
105 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
106 assert(c->range > RangeLPS);
109 c->range -= RangeLPS;
111 c->low += c->range - RangeLPS;
115 renorm_cabac_encoder(c);
123 * @param bit 0 -> write zero bit, !=0 write one bit
125 static void put_cabac_bypass(CABACContext *c, int bit){
134 }else if(c->low<0x400){
135 c->outstanding_count++;
149 * @return the number of bytes written
151 static int put_cabac_terminate(CABACContext *c, int bit){
155 renorm_cabac_encoder(c);
160 renorm_cabac_encoder(c);
162 assert(c->low <= 0x1FF);
163 put_cabac_bit(c, c->low>>9);
164 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
166 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
173 return (put_bits_count(&c->pb)+7)>>3;
177 * put (truncated) unary binarization.
179 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
186 put_cabac(c, state, 1);
187 if(i < max_index) state++;
189 if(truncated==0 || v<max)
190 put_cabac(c, state, 0);
194 put_cabac(c, state+i, 1);
196 if(truncated==0 || v<max)
197 put_cabac(c, state+i, 0);
199 for(i=0; i<=max_index; i++){
200 put_cabac(c, state+i, 1);
203 put_cabac(c, state+max_index, 1);
205 if(truncated==0 || v<max)
206 put_cabac(c, state+max_index, 0);
212 * put unary exp golomb k-th order binarization.
214 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
218 put_cabac(c, state, 0);
220 const int sign= v < 0;
222 if(is_signed) v= ABS(v);
226 put_cabac(c, state, 1);
227 if(i < max_index) state++;
230 put_cabac(c, state, 0);
234 for(i=0; i<max; i++){
235 put_cabac(c, state, 1);
236 if(i < max_index) state++;
240 while(v >= m){ //FIXME optimize
241 put_cabac_bypass(c, 1);
245 put_cabac_bypass(c, 0);
247 put_cabac_bypass(c, v&m);
252 put_cabac_bypass(c, sign);
256 static void refill(CABACContext *c){
258 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
260 c->low+= c->bytestream[0]<<1;
262 c->low -= CABAC_MASK;
263 c->bytestream+= CABAC_BITS/8;
266 static void refill2(CABACContext *c){
269 x= c->low ^ (c->low-1);
270 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
275 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
277 x+= c->bytestream[0]<<1;
281 c->bytestream+= CABAC_BITS/8;
284 static inline void renorm_cabac_decoder(CABACContext *c){
285 while(c->range < (0x200 << CABAC_BITS)){
288 if(!(c->low & CABAC_MASK))
293 static inline void renorm_cabac_decoder_once(CABACContext *c){
294 #ifdef ARCH_X86_DISABLED
299 "lea -0x2000000(%0), %2 \n\t"
300 "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
303 : "+r"(c->range), "+r"(c->low), "+c"(temp)
308 "cmp $0x2000000, %0 \n\t"
309 "setb %%cl \n\t" //FIXME 31->63 for x86-64
312 : "+r"(c->range), "+r"(c->low), "+c"(temp)
318 "lea -0x2000000(%0), %%eax \n\t"
325 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
331 "cmp $0x2000000, %0 \n\t"
332 "sbb %%edx, %%edx \n\t"
338 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
344 "cmp $0x2000000, %0 \n\t"
345 "lea (%0, %0), %%eax \n\t"
346 "lea (%1, %1), %%edx \n\t"
347 "cmovb %%eax, %0 \n\t"
348 "cmovb %%edx, %1 \n\t"
349 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
354 int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31;
358 if(!(c->low & CABAC_MASK))
362 static int get_cabac(CABACContext *c, uint8_t * const state){
363 //FIXME gcc generates duplicate load/stores for c->low and c->range
369 #define BYTESTART "12"
372 #ifndef BRANCHLESS_CABAC_DECODER
374 "movzbl (%1), %%eax \n\t"
375 "movl "RANGE "(%2), %%ebx \n\t"
376 "movl "RANGE "(%2), %%edx \n\t"
377 "shrl $23, %%ebx \n\t"
378 "movzbl "MANGLE(ff_h264_lps_range)"(%%ebx, %%eax, 4), %%esi\n\t"
379 "shll $17, %%esi \n\t"
380 "movl "LOW "(%2), %%ebx \n\t"
381 //eax:state ebx:low, edx:range, esi:RangeLPS
382 "subl %%esi, %%edx \n\t"
383 "cmpl %%edx, %%ebx \n\t"
385 "cmp $0x2000000, %%edx \n\t" //FIXME avoidable
387 "shl %%cl, %%edx \n\t"
388 "shl %%cl, %%ebx \n\t"
389 "movzbl "MANGLE(ff_h264_mps_state)"(%%eax), %%ecx \n\t"
390 "movb %%cl, (%1) \n\t"
391 //eax:state ebx:low, edx:range, esi:RangeLPS
392 "test %%bx, %%bx \n\t"
394 "movl "BYTE "(%2), %%esi \n\t"
395 "subl $0xFFFF, %%ebx \n\t"
396 "movzwl (%%esi), %%ecx \n\t"
398 "shrl $15, %%ecx \n\t"
399 "addl $2, %%esi \n\t"
400 "addl %%ecx, %%ebx \n\t"
401 "movl %%esi, "BYTE "(%2) \n\t"
404 //eax:state ebx:low, edx:range, esi:RangeLPS
405 "subl %%edx, %%ebx \n\t"
406 "movl %%esi, %%edx \n\t"
407 "shr $19, %%esi \n\t"
408 "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
409 "shll %%cl, %%ebx \n\t"
410 "shll %%cl, %%edx \n\t"
411 "movzbl "MANGLE(ff_h264_lps_state)"(%%eax), %%ecx \n\t"
412 "movb %%cl, (%1) \n\t"
413 "addl $1, %%eax \n\t"
414 "test %%bx, %%bx \n\t"
417 "movl "BYTE "(%2), %%ecx \n\t"
418 "movzwl (%%ecx), %%esi \n\t"
420 "shrl $15, %%esi \n\t"
421 "subl $0xFFFF, %%esi \n\t"
422 "addl $2, %%ecx \n\t"
423 "movl %%ecx, "BYTE "(%2) \n\t"
425 "leal -1(%%ebx), %%ecx \n\t"
426 "xorl %%ebx, %%ecx \n\t"
427 "shrl $17, %%ecx \n\t"
428 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
432 "shll %%cl , %%esi \n\t"
433 "addl %%esi, %%ebx \n\t"
435 "movl %%edx, "RANGE "(%2) \n\t"
436 "movl %%ebx, "LOW "(%2) \n\t"
437 :"=&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
439 : "%ecx", "%ebx", "%edx", "%esi"
444 "movzbl (%1), %%eax \n\t"
445 "movl "RANGE "(%2), %%ebx \n\t"
446 "movl "RANGE "(%2), %%edx \n\t"
447 "shrl $23, %%ebx \n\t"
448 "movzbl "MANGLE(ff_h264_lps_range)"(%%ebx, %%eax, 4), %%esi\n\t"
449 "shll $17, %%esi \n\t"
450 "movl "LOW "(%2), %%ebx \n\t"
451 //eax:state ebx:low, edx:range, esi:RangeLPS
452 "subl %%esi, %%edx \n\t"
453 #ifdef CMOV_IS_FAST //FIXME actually define this somewhere
454 "cmpl %%ebx, %%edx \n\t"
455 "cmova %%edx, %%esi \n\t"
456 "sbbl %%ecx, %%ecx \n\t"
457 "andl %%ecx, %%edx \n\t"
458 "subl %%edx, %%ebx \n\t"
459 "xorl %%ecx, %%eax \n\t"
461 "movl %%edx, %%ecx \n\t"
462 "subl %%ebx, %%edx \n\t"
463 "sarl $31, %%edx \n\t" //lps_mask
464 "subl %%ecx, %%esi \n\t" //RangeLPS - range
465 "andl %%edx, %%esi \n\t" //(RangeLPS - range)&lps_mask
466 "addl %%ecx, %%esi \n\t" //new range
467 "andl %%edx, %%ecx \n\t"
468 "subl %%ecx, %%ebx \n\t"
469 "xorl %%edx, %%eax \n\t"
472 //eax:state ebx:low edx:mask esi:range
473 "movzbl "MANGLE(ff_h264_mps_state)"(%%eax), %%ecx \n\t"
474 "movb %%cl, (%1) \n\t"
476 "movl %%esi, %%edx \n\t"
477 //eax:bit ebx:low edx:range esi:range
479 "shr $19, %%esi \n\t"
480 "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx \n\t"
481 "shll %%cl, %%ebx \n\t"
482 "shll %%cl, %%edx \n\t"
483 "test %%bx, %%bx \n\t"
486 "movl "BYTE "(%2), %%ecx \n\t"
487 "movzwl (%%ecx), %%esi \n\t"
489 "shrl $15, %%esi \n\t"
490 "subl $0xFFFF, %%esi \n\t"
491 "addl $2, %%ecx \n\t"
492 "movl %%ecx, "BYTE "(%2) \n\t"
494 "leal -1(%%ebx), %%ecx \n\t"
495 "xorl %%ebx, %%ecx \n\t"
496 "shrl $17, %%ecx \n\t"
497 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"
501 "shll %%cl , %%esi \n\t"
502 "addl %%esi, %%ebx \n\t"
504 "movl %%edx, "RANGE "(%2) \n\t"
505 "movl %%ebx, "LOW "(%2) \n\t"
508 : "%ecx", "%ebx", "%edx", "%esi"
514 int RangeLPS= ff_h264_lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
515 int bit, lps_mask attribute_unused;
517 c->range -= RangeLPS;
518 #ifndef BRANCHLESS_CABAC_DECODER
519 if(c->low < c->range){
521 *state= ff_h264_mps_state[s];
522 renorm_cabac_decoder_once(c);
524 bit= ff_h264_norm_shift[RangeLPS>>19];
526 *state= ff_h264_lps_state[s];
527 c->range = RangeLPS<<bit;
531 if(!(c->low & 0xFFFF)){
536 lps_mask= (c->range - c->low)>>31;
538 c->low -= c->range & lps_mask;
539 c->range += (RangeLPS - c->range) & lps_mask;
542 *state= ff_h264_mps_state[s];
545 lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+3)];
546 c->range<<= lps_mask;
548 if(!(c->low & CABAC_MASK))
555 static int get_cabac_bypass(CABACContext *c){
558 if(!(c->low & CABAC_MASK))
561 if(c->low < c->range){
571 * @return the number of bytes read or 0 if no end
573 static int get_cabac_terminate(CABACContext *c){
574 c->range -= 4<<CABAC_BITS;
575 if(c->low < c->range){
576 renorm_cabac_decoder_once(c);
579 return c->bytestream - c->bytestream_start;
584 * get (truncated) unnary binarization.
586 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
589 for(i=0; i<max; i++){
590 if(get_cabac(c, state)==0)
593 if(i< max_index) state++;
596 return truncated ? max : -1;
600 * get unary exp golomb k-th order binarization.
602 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
606 if(get_cabac(c, state)==0)
609 if(0 < max_index) state++;
611 for(i=1; i<max; i++){
612 if(get_cabac(c, state)==0){
613 if(is_signed && get_cabac_bypass(c)){
619 if(i < max_index) state++;
622 while(get_cabac_bypass(c)){
629 v+= v + get_cabac_bypass(c);
633 if(is_signed && get_cabac_bypass(c)){