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
36 #include "libavutil/x86_cpu.h"
39 #define CABAC_MASK ((1<<CABAC_BITS)-1)
41 typedef struct CABACContext{
44 int outstanding_count;
48 const uint8_t *bytestream_start;
49 const uint8_t *bytestream;
50 const uint8_t *bytestream_end;
54 extern uint8_t ff_h264_mlps_state[4*64];
55 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
56 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
57 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
58 extern const uint8_t ff_h264_norm_shift[512];
61 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
62 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
63 void ff_init_cabac_states(CABACContext *c);
66 static inline void put_cabac_bit(CABACContext *c, int b){
67 put_bits(&c->pb, 1, b);
68 for(;c->outstanding_count; c->outstanding_count--){
69 put_bits(&c->pb, 1, 1-b);
73 static inline void renorm_cabac_encoder(CABACContext *c){
74 while(c->range < 0x100){
78 }else if(c->low<0x200){
79 c->outstanding_count++;
92 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
93 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
95 if(bit == ((*state)&1)){
97 *state= ff_h264_mps_state[*state];
99 c->low += c->range - RangeLPS;
101 *state= ff_h264_lps_state[*state];
104 renorm_cabac_encoder(c);
111 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
112 assert(c->range > RangeLPS);
115 c->range -= RangeLPS;
117 c->low += c->range - RangeLPS;
121 renorm_cabac_encoder(c);
129 * @param bit 0 -> write zero bit, !=0 write one bit
131 static void put_cabac_bypass(CABACContext *c, int bit){
140 }else if(c->low<0x400){
141 c->outstanding_count++;
155 * @return the number of bytes written
157 static int put_cabac_terminate(CABACContext *c, int bit){
161 renorm_cabac_encoder(c);
166 renorm_cabac_encoder(c);
168 assert(c->low <= 0x1FF);
169 put_cabac_bit(c, c->low>>9);
170 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
172 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
179 return (put_bits_count(&c->pb)+7)>>3;
183 * put (truncated) unary binarization.
185 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
192 put_cabac(c, state, 1);
193 if(i < max_index) state++;
195 if(truncated==0 || v<max)
196 put_cabac(c, state, 0);
200 put_cabac(c, state+i, 1);
202 if(truncated==0 || v<max)
203 put_cabac(c, state+i, 0);
205 for(i=0; i<=max_index; i++){
206 put_cabac(c, state+i, 1);
209 put_cabac(c, state+max_index, 1);
211 if(truncated==0 || v<max)
212 put_cabac(c, state+max_index, 0);
218 * put unary exp golomb k-th order binarization.
220 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
224 put_cabac(c, state, 0);
226 const int sign= v < 0;
228 if(is_signed) v= FFABS(v);
232 put_cabac(c, state, 1);
233 if(i < max_index) state++;
236 put_cabac(c, state, 0);
240 for(i=0; i<max; i++){
241 put_cabac(c, state, 1);
242 if(i < max_index) state++;
246 while(v >= m){ //FIXME optimize
247 put_cabac_bypass(c, 1);
251 put_cabac_bypass(c, 0);
253 put_cabac_bypass(c, v&m);
258 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 #if ! ( ARCH_X86 && HAVE_7REGS && !defined(BROKEN_RELOCATIONS) )
274 static void refill2(CABACContext *c){
277 x= c->low ^ (c->low-1);
278 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
283 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
285 x+= c->bytestream[0]<<1;
289 c->bytestream+= CABAC_BITS/8;
293 static inline void renorm_cabac_decoder(CABACContext *c){
294 while(c->range < 0x100){
297 if(!(c->low & CABAC_MASK))
302 static inline void renorm_cabac_decoder_once(CABACContext *c){
303 int shift= (uint32_t)(c->range - 0x100)>>31;
306 if(!(c->low & CABAC_MASK))
310 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
311 //FIXME gcc generates duplicate load/stores for c->low and c->range
312 #if ARCH_X86 && HAVE_7REGS && !defined(BROKEN_RELOCATIONS)
316 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp)\
317 "mov "tmp" , %%ecx \n\t"\
318 "shl $17 , "tmp" \n\t"\
319 "cmp "low" , "tmp" \n\t"\
320 "cmova %%ecx , "range" \n\t"\
321 "sbb %%ecx , %%ecx \n\t"\
322 "and %%ecx , "tmp" \n\t"\
323 "sub "tmp" , "low" \n\t"\
324 "xor %%ecx , "ret" \n\t"
325 #else /* HAVE_FAST_CMOV */
326 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp)\
327 "mov "tmp" , %%ecx \n\t"\
328 "shl $17 , "tmp" \n\t"\
329 "sub "low" , "tmp" \n\t"\
330 "sar $31 , "tmp" \n\t" /*lps_mask*/\
331 "sub %%ecx , "range" \n\t" /*RangeLPS - range*/\
332 "and "tmp" , "range" \n\t" /*(RangeLPS - range)&lps_mask*/\
333 "add %%ecx , "range" \n\t" /*new range*/\
334 "shl $17 , %%ecx \n\t"\
335 "and "tmp" , %%ecx \n\t"\
336 "sub %%ecx , "low" \n\t"\
337 "xor "tmp" , "ret" \n\t"
338 #endif /* HAVE_FAST_CMOV */
341 #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte, byte) \
342 "movzbl "statep" , "ret" \n\t"\
343 "mov "range" , "tmp" \n\t"\
344 "and $0xC0 , "range" \n\t"\
345 "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
346 "sub "range" , "tmp" \n\t"\
347 BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp)\
348 "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx \n\t"\
349 "shl %%cl , "range" \n\t"\
350 "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp" \n\t"\
351 "mov "tmpbyte" , "statep" \n\t"\
352 "shl %%cl , "low" \n\t"\
353 "test "lowword" , "lowword" \n\t"\
355 "mov "byte"("cabac"), %%"REG_c" \n\t"\
356 "movzwl (%%"REG_c") , "tmp" \n\t"\
358 "shr $15 , "tmp" \n\t"\
359 "sub $0xFFFF , "tmp" \n\t"\
360 "add $2 , %%"REG_c" \n\t"\
361 "mov %%"REG_c" , "byte "("cabac") \n\t"\
362 "lea -1("low") , %%ecx \n\t"\
363 "xor "low" , %%ecx \n\t"\
364 "shr $15 , %%ecx \n\t"\
365 "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx \n\t"\
367 "add $7 , %%ecx \n\t"\
368 "shl %%cl , "tmp" \n\t"\
369 "add "tmp" , "low" \n\t"\
373 "movl %a5(%4), %%esi \n\t"
374 "movl %a6(%4), %1 \n\t"
375 BRANCHLESS_GET_CABAC("%0", "%4", "(%3)", "%1", "%w1", "%%esi", "%2", "%b2", "%a7")
376 "movl %%esi, %a5(%4) \n\t"
377 "movl %1, %a6(%4) \n\t"
379 :"=&a"(bit), "=&r"(low), "=&r"(tmp)
381 "i"(offsetof(CABACContext, range)), "i"(offsetof(CABACContext, low)),
382 "i"(offsetof(CABACContext, bytestream))
383 : "%"REG_c, "%esi", "memory"
386 #else /* ARCH_X86 && HAVE_7REGS && !defined(BROKEN_RELOCATIONS) */
388 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
391 c->range -= RangeLPS;
392 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
394 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
395 c->range += (RangeLPS - c->range) & lps_mask;
398 *state= (ff_h264_mlps_state+128)[s];
401 lps_mask= ff_h264_norm_shift[c->range];
402 c->range<<= lps_mask;
404 if(!(c->low & CABAC_MASK))
406 #endif /* ARCH_X86 && HAVE_7REGS && !defined(BROKEN_RELOCATIONS) */
410 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
411 return get_cabac_inline(c,state);
414 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
415 return get_cabac_inline(c,state);
418 static int av_unused get_cabac_bypass(CABACContext *c){
422 if(!(c->low & CABAC_MASK))
425 range= c->range<<(CABAC_BITS+1);
435 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
439 "movl %a3(%2), %k1 \n\t"
440 "movl %a4(%2), %%eax \n\t"
442 "add %%eax, %%eax \n\t"
443 "sub %k1, %%eax \n\t"
445 "and %%edx, %k1 \n\t"
446 "add %k1, %%eax \n\t"
447 "xor %%edx, %%ecx \n\t"
448 "sub %%edx, %%ecx \n\t"
449 "test %%ax, %%ax \n\t"
451 "mov %a5(%2), %1 \n\t"
452 "subl $0xFFFF, %%eax \n\t"
453 "movzwl (%1), %%edx \n\t"
455 "shrl $15, %%edx \n\t"
457 "addl %%edx, %%eax \n\t"
458 "mov %1, %a5(%2) \n\t"
460 "movl %%eax, %a4(%2) \n\t"
462 :"+c"(val), "=&r"(tmp)
464 "i"(offsetof(CABACContext, range)), "i"(offsetof(CABACContext, low)),
465 "i"(offsetof(CABACContext, bytestream))
466 : "%eax", "%edx", "memory"
473 if(!(c->low & CABAC_MASK))
476 range= c->range<<(CABAC_BITS+1);
481 return (val^mask)-mask;
487 * @return the number of bytes read or 0 if no end
489 static int av_unused get_cabac_terminate(CABACContext *c){
491 if(c->low < c->range<<(CABAC_BITS+1)){
492 renorm_cabac_decoder_once(c);
495 return c->bytestream - c->bytestream_start;
501 * Get (truncated) unary binarization.
503 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
506 for(i=0; i<max; i++){
507 if(get_cabac(c, state)==0)
510 if(i< max_index) state++;
513 return truncated ? max : -1;
517 * get unary exp golomb k-th order binarization.
519 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
523 if(get_cabac(c, state)==0)
526 if(0 < max_index) state++;
528 for(i=1; i<max; i++){
529 if(get_cabac(c, state)==0){
530 if(is_signed && get_cabac_bypass(c)){
536 if(i < max_index) state++;
539 while(get_cabac_bypass(c)){
546 v+= v + get_cabac_bypass(c);
550 if(is_signed && get_cabac_bypass(c)){
557 #endif /* AVCODEC_CABAC_H */