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
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;
47 const uint8_t *bytestream_start;
48 const uint8_t *bytestream;
49 const uint8_t *bytestream_end;
53 extern uint8_t ff_h264_mlps_state[4*64];
54 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
55 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
56 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
57 extern const uint8_t ff_h264_norm_shift[512];
60 # include "x86/cabac.h"
63 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
64 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
65 void ff_init_cabac_states(CABACContext *c);
68 static inline void put_cabac_bit(CABACContext *c, int b){
69 put_bits(&c->pb, 1, b);
70 for(;c->outstanding_count; c->outstanding_count--){
71 put_bits(&c->pb, 1, 1-b);
75 static inline void renorm_cabac_encoder(CABACContext *c){
76 while(c->range < 0x100){
80 }else if(c->low<0x200){
81 c->outstanding_count++;
93 static void refill(CABACContext *c){
95 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
97 c->low+= c->bytestream[0]<<1;
100 c->bytestream+= CABAC_BITS/8;
103 static inline void renorm_cabac_decoder(CABACContext *c){
104 while(c->range < 0x100){
107 if(!(c->low & CABAC_MASK))
112 static inline void renorm_cabac_decoder_once(CABACContext *c){
113 int shift= (uint32_t)(c->range - 0x100)>>31;
116 if(!(c->low & CABAC_MASK))
120 #ifndef get_cabac_inline
121 static void refill2(CABACContext *c){
124 x= c->low ^ (c->low-1);
125 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
130 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
132 x+= c->bytestream[0]<<1;
136 c->bytestream+= CABAC_BITS/8;
139 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
141 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
144 c->range -= RangeLPS;
145 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
147 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
148 c->range += (RangeLPS - c->range) & lps_mask;
151 *state= (ff_h264_mlps_state+128)[s];
154 lps_mask= ff_h264_norm_shift[c->range];
155 c->range<<= lps_mask;
157 if(!(c->low & CABAC_MASK))
163 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
164 return get_cabac_inline(c,state);
167 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
168 return get_cabac_inline(c,state);
171 static int av_unused get_cabac_bypass(CABACContext *c){
175 if(!(c->low & CABAC_MASK))
178 range= c->range<<(CABAC_BITS+1);
188 #ifndef get_cabac_bypass_sign
189 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
193 if(!(c->low & CABAC_MASK))
196 range= c->range<<(CABAC_BITS+1);
201 return (val^mask)-mask;
207 * @return the number of bytes read or 0 if no end
209 static int av_unused get_cabac_terminate(CABACContext *c){
211 if(c->low < c->range<<(CABAC_BITS+1)){
212 renorm_cabac_decoder_once(c);
215 return c->bytestream - c->bytestream_start;
221 * Get (truncated) unary binarization.
223 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
226 for(i=0; i<max; i++){
227 if(get_cabac(c, state)==0)
230 if(i< max_index) state++;
233 return truncated ? max : -1;
237 * get unary exp golomb k-th order binarization.
239 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
243 if(get_cabac(c, state)==0)
246 if(0 < max_index) state++;
248 for(i=1; i<max; i++){
249 if(get_cabac(c, state)==0){
250 if(is_signed && get_cabac_bypass(c)){
256 if(i < max_index) state++;
259 while(get_cabac_bypass(c)){
266 v+= v + get_cabac_bypass(c);
270 if(is_signed && get_cabac_bypass(c)){
277 #endif /* AVCODEC_CABAC_H */