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
38 #define CABAC_MASK ((1<<CABAC_BITS)-1)
40 typedef struct CABACContext{
43 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_mlps_state[4*64];
51 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
52 extern const uint8_t ff_h264_norm_shift[512];
55 # include "x86/cabac.h"
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);
63 static void refill(CABACContext *c){
65 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
67 c->low+= c->bytestream[0]<<1;
70 c->bytestream+= CABAC_BITS/8;
73 static inline void renorm_cabac_decoder_once(CABACContext *c){
74 int shift= (uint32_t)(c->range - 0x100)>>31;
77 if(!(c->low & CABAC_MASK))
81 #ifndef get_cabac_inline
82 static void refill2(CABACContext *c){
85 x= c->low ^ (c->low-1);
86 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
91 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
93 x+= c->bytestream[0]<<1;
97 c->bytestream+= CABAC_BITS/8;
100 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
102 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
105 c->range -= RangeLPS;
106 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
108 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
109 c->range += (RangeLPS - c->range) & lps_mask;
112 *state= (ff_h264_mlps_state+128)[s];
115 lps_mask= ff_h264_norm_shift[c->range];
116 c->range<<= lps_mask;
118 if(!(c->low & CABAC_MASK))
124 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
125 return get_cabac_inline(c,state);
128 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
129 return get_cabac_inline(c,state);
132 static int av_unused get_cabac_bypass(CABACContext *c){
136 if(!(c->low & CABAC_MASK))
139 range= c->range<<(CABAC_BITS+1);
149 #ifndef get_cabac_bypass_sign
150 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
154 if(!(c->low & CABAC_MASK))
157 range= c->range<<(CABAC_BITS+1);
162 return (val^mask)-mask;
168 * @return the number of bytes read or 0 if no end
170 static int av_unused get_cabac_terminate(CABACContext *c){
172 if(c->low < c->range<<(CABAC_BITS+1)){
173 renorm_cabac_decoder_once(c);
176 return c->bytestream - c->bytestream_start;
180 #endif /* AVCODEC_CABAC_H */