2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Context Adaptive Binary Arithmetic Coder.
30 typedef struct CABACContext{
33 int outstanding_count;
37 uint8_t lps_range[2*64][4]; ///< rangeTabLPS
38 uint8_t lps_state[2*64]; ///< transIdxLPS
39 uint8_t mps_state[2*64]; ///< transIdxMPS
45 const uint8_t ff_h264_lps_range[64][4];
46 const uint8_t ff_h264_mps_state[64];
47 const uint8_t ff_h264_lps_state[64];
49 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
50 void ff_init_cabac_decoder(CABACContext *c, uint8_t *buf, int buf_size);
51 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
52 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
55 static inline void put_cabac_bit(CABACContext *c, int b){
56 put_bits(&c->pb, 1, b);
57 for(;c->outstanding_count; c->outstanding_count--){
58 put_bits(&c->pb, 1, 1-b);
62 static inline void renorm_cabac_encoder(CABACContext *c){
63 while(c->range < 0x100){
67 }else if(c->low<0x200){
68 c->outstanding_count++;
80 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
81 int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
83 if(bit == ((*state)&1)){
85 *state= c->mps_state[*state];
87 c->low += c->range - RangeLPS;
89 *state= c->lps_state[*state];
92 renorm_cabac_encoder(c);
99 static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
100 assert(c->range > RangeLPS);
103 c->range -= RangeLPS;
105 c->low += c->range - RangeLPS;
109 renorm_cabac_encoder(c);
116 static inline void put_cabac_bypass(CABACContext *c, int bit){
125 }else if(c->low<0x400){
126 c->outstanding_count++;
138 static inline void put_cabac_terminate(CABACContext *c, int bit){
142 renorm_cabac_encoder(c);
147 renorm_cabac_encoder(c);
149 assert(c->low <= 0x1FF);
150 put_cabac_bit(c, c->low>>9);
151 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
153 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
161 static inline void renorm_cabac_decoder(CABACContext *c){
162 while(c->range < 0x10000){
165 if(--c->bits_left == 0){
166 c->low+= *c->bytestream++;
172 static inline int get_cabac(CABACContext *c, uint8_t * const state){
173 int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
176 c->range -= RangeLPS;
177 if(c->low < c->range){
179 *state= c->mps_state[*state];
184 *state= c->lps_state[*state];
186 renorm_cabac_decoder(c);
191 static inline int get_cabac_static(CABACContext *c, int RangeLPS){
194 c->range -= RangeLPS;
195 if(c->low < c->range){
202 renorm_cabac_decoder(c);
207 static inline int get_cabac_bypass(CABACContext *c){
210 if(--c->bits_left == 0){
211 c->low+= *c->bytestream++;
215 if(c->low < c->range){
223 static inline int get_cabac_terminate(CABACContext *c){
225 if(c->low < c->range){
226 renorm_cabac_decoder(c);