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 Libav.
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.
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.
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
24 * Context Adaptive Binary Arithmetic Coder.
27 #ifndef AVCODEC_CABAC_H
28 #define AVCODEC_CABAC_H
34 #include "libavutil/x86_cpu.h"
37 #define CABAC_MASK ((1<<CABAC_BITS)-1)
39 typedef struct CABACContext{
42 int outstanding_count;
46 const uint8_t *bytestream_start;
47 const uint8_t *bytestream;
48 const uint8_t *bytestream_end;
52 extern uint8_t ff_h264_mlps_state[4*64];
53 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
54 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
55 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
56 extern const uint8_t ff_h264_norm_shift[512];
59 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
60 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
61 void ff_init_cabac_states(CABACContext *c);
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++;
90 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
91 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
93 if(bit == ((*state)&1)){
95 *state= ff_h264_mps_state[*state];
97 c->low += c->range - RangeLPS;
99 *state= ff_h264_lps_state[*state];
102 renorm_cabac_encoder(c);
109 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
110 assert(c->range > RangeLPS);
113 c->range -= RangeLPS;
115 c->low += c->range - RangeLPS;
119 renorm_cabac_encoder(c);
127 * @param bit 0 -> write zero bit, !=0 write one bit
129 static void put_cabac_bypass(CABACContext *c, int bit){
138 }else if(c->low<0x400){
139 c->outstanding_count++;
153 * @return the number of bytes written
155 static int put_cabac_terminate(CABACContext *c, int bit){
159 renorm_cabac_encoder(c);
164 renorm_cabac_encoder(c);
166 assert(c->low <= 0x1FF);
167 put_cabac_bit(c, c->low>>9);
168 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
170 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
177 return (put_bits_count(&c->pb)+7)>>3;
181 * put (truncated) unary binarization.
183 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
190 put_cabac(c, state, 1);
191 if(i < max_index) state++;
193 if(truncated==0 || v<max)
194 put_cabac(c, state, 0);
198 put_cabac(c, state+i, 1);
200 if(truncated==0 || v<max)
201 put_cabac(c, state+i, 0);
203 for(i=0; i<=max_index; i++){
204 put_cabac(c, state+i, 1);
207 put_cabac(c, state+max_index, 1);
209 if(truncated==0 || v<max)
210 put_cabac(c, state+max_index, 0);
216 * put unary exp golomb k-th order binarization.
218 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
222 put_cabac(c, state, 0);
224 const int sign= v < 0;
226 if(is_signed) v= FFABS(v);
230 put_cabac(c, state, 1);
231 if(i < max_index) state++;
234 put_cabac(c, state, 0);
238 for(i=0; i<max; i++){
239 put_cabac(c, state, 1);
240 if(i < max_index) state++;
244 while(v >= m){ //FIXME optimize
245 put_cabac_bypass(c, 1);
249 put_cabac_bypass(c, 0);
251 put_cabac_bypass(c, v&m);
256 put_cabac_bypass(c, sign);
261 static void refill(CABACContext *c){
263 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
265 c->low+= c->bytestream[0]<<1;
267 c->low -= CABAC_MASK;
268 c->bytestream+= CABAC_BITS/8;
271 #if ! ( ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS) )
272 static void refill2(CABACContext *c){
275 x= c->low ^ (c->low-1);
276 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
281 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
283 x+= c->bytestream[0]<<1;
287 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 int shift= (uint32_t)(c->range - 0x100)>>31;
304 if(!(c->low & CABAC_MASK))
308 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
309 //FIXME gcc generates duplicate load/stores for c->low and c->range
313 #define BYTESTART "16"
317 #define BYTESTART "12"
321 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS)
325 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
326 "mov "tmp" , %%ecx \n\t"\
327 "shl $17 , "tmp" \n\t"\
328 "cmp "low" , "tmp" \n\t"\
329 "cmova %%ecx , "range" \n\t"\
330 "sbb %%ecx , %%ecx \n\t"\
331 "and %%ecx , "tmp" \n\t"\
332 "sub "tmp" , "low" \n\t"\
333 "xor %%ecx , "ret" \n\t"
334 #else /* HAVE_FAST_CMOV */
335 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
336 "mov "tmp" , %%ecx \n\t"\
337 "shl $17 , "tmp" \n\t"\
338 "sub "low" , "tmp" \n\t"\
339 "sar $31 , "tmp" \n\t" /*lps_mask*/\
340 "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
341 "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
342 "add %%ecx , "range" \n\t" /*new range*/\
343 "shl $17 , %%ecx \n\t"\
344 "and "tmp" , %%ecx \n\t"\
345 "sub %%ecx , "low" \n\t"\
346 "xor "tmp" , "ret" \n\t"
347 #endif /* HAVE_FAST_CMOV */
350 #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
351 "movzbl "statep" , "ret" \n\t"\
352 "mov "range" , "tmp" \n\t"\
353 "and $0xC0 , "range" \n\t"\
354 "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
355 "sub "range" , "tmp" \n\t"\
356 BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
357 "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
358 "shl %%cl , "range" \n\t"\
359 "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
360 "mov "tmpbyte" , "statep" \n\t"\
361 "shl %%cl , "low" \n\t"\
362 "test "lowword" , "lowword" \n\t"\
364 "mov "BYTE"("cabac"), %%"REG_c" \n\t"\
365 "movzwl (%%"REG_c") , "tmp" \n\t"\
367 "shr $15 , "tmp" \n\t"\
368 "sub $0xFFFF , "tmp" \n\t"\
369 "add $2 , %%"REG_c" \n\t"\
370 "mov %%"REG_c" , "BYTE "("cabac") \n\t"\
371 "lea -1("low") , %%ecx \n\t"\
372 "xor "low" , %%ecx \n\t"\
373 "shr $15 , %%ecx \n\t"\
374 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
376 "add $7 , %%ecx \n\t"\
377 "shl %%cl , "tmp" \n\t"\
378 "add "tmp" , "low" \n\t"\
382 "movl "RANGE "(%2), %%esi \n\t"
383 "movl "LOW "(%2), %%ebx \n\t"
384 BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl")
385 "movl %%esi, "RANGE "(%2) \n\t"
386 "movl %%ebx, "LOW "(%2) \n\t"
390 : "%"REG_c, "%ebx", "%edx", "%esi", "memory"
393 #else /* ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS) */
395 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
398 c->range -= RangeLPS;
399 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
401 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
402 c->range += (RangeLPS - c->range) & lps_mask;
405 *state= (ff_h264_mlps_state+128)[s];
408 lps_mask= ff_h264_norm_shift[c->range];
409 c->range<<= lps_mask;
411 if(!(c->low & CABAC_MASK))
413 #endif /* ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS) */
417 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
418 return get_cabac_inline(c,state);
421 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
422 return get_cabac_inline(c,state);
425 static int av_unused get_cabac_bypass(CABACContext *c){
429 "movl "RANGE "(%1), %%ebx \n\t"
430 "movl "LOW "(%1), %%eax \n\t"
431 "shl $17, %%ebx \n\t"
432 "add %%eax, %%eax \n\t"
433 "sub %%ebx, %%eax \n\t"
435 "and %%edx, %%ebx \n\t"
436 "add %%ebx, %%eax \n\t"
437 "test %%ax, %%ax \n\t"
439 "movl "BYTE "(%1), %%"REG_b" \n\t"
440 "subl $0xFFFF, %%eax \n\t"
441 "movzwl (%%"REG_b"), %%ecx \n\t"
443 "shrl $15, %%ecx \n\t"
444 "addl $2, %%"REG_b" \n\t"
445 "addl %%ecx, %%eax \n\t"
446 "movl %%"REG_b", "BYTE "(%1) \n\t"
448 "movl %%eax, "LOW "(%1) \n\t"
452 : "%eax", "%"REG_b, "%ecx", "memory"
459 if(!(c->low & CABAC_MASK))
462 range= c->range<<(CABAC_BITS+1);
473 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
474 #if ARCH_X86 && HAVE_EBX_AVAILABLE
476 "movl "RANGE "(%1), %%ebx \n\t"
477 "movl "LOW "(%1), %%eax \n\t"
478 "shl $17, %%ebx \n\t"
479 "add %%eax, %%eax \n\t"
480 "sub %%ebx, %%eax \n\t"
482 "and %%edx, %%ebx \n\t"
483 "add %%ebx, %%eax \n\t"
484 "xor %%edx, %%ecx \n\t"
485 "sub %%edx, %%ecx \n\t"
486 "test %%ax, %%ax \n\t"
488 "mov "BYTE "(%1), %%"REG_b" \n\t"
489 "subl $0xFFFF, %%eax \n\t"
490 "movzwl (%%"REG_b"), %%edx \n\t"
492 "shrl $15, %%edx \n\t"
493 "add $2, %%"REG_b" \n\t"
494 "addl %%edx, %%eax \n\t"
495 "mov %%"REG_b", "BYTE "(%1) \n\t"
497 "movl %%eax, "LOW "(%1) \n\t"
501 : "%eax", "%"REG_b, "%edx", "memory"
508 if(!(c->low & CABAC_MASK))
511 range= c->range<<(CABAC_BITS+1);
516 return (val^mask)-mask;
522 * @return the number of bytes read or 0 if no end
524 static int av_unused get_cabac_terminate(CABACContext *c){
526 if(c->low < c->range<<(CABAC_BITS+1)){
527 renorm_cabac_decoder_once(c);
530 return c->bytestream - c->bytestream_start;
536 * Get (truncated) unary binarization.
538 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
541 for(i=0; i<max; i++){
542 if(get_cabac(c, state)==0)
545 if(i< max_index) state++;
548 return truncated ? max : -1;
552 * get unary exp golomb k-th order binarization.
554 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
558 if(get_cabac(c, state)==0)
561 if(0 < max_index) state++;
563 for(i=1; i<max; i++){
564 if(get_cabac(c, state)==0){
565 if(is_signed && get_cabac_bypass(c)){
571 if(i < max_index) state++;
574 while(get_cabac_bypass(c)){
581 v+= v + get_cabac_bypass(c);
585 if(is_signed && get_cabac_bypass(c)){
592 #endif /* AVCODEC_CABAC_H */