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
40 const uint8_t *bytestream_start;
41 const uint8_t *bytestream;
42 const uint8_t *bytestream_end;
47 extern const uint8_t ff_h264_lps_range[64][4];
48 extern const uint8_t ff_h264_mps_state[64];
49 extern const uint8_t ff_h264_lps_state[64];
51 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
52 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
53 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
54 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
57 static inline void put_cabac_bit(CABACContext *c, int b){
58 put_bits(&c->pb, 1, b);
59 for(;c->outstanding_count; c->outstanding_count--){
60 put_bits(&c->pb, 1, 1-b);
64 static inline void renorm_cabac_encoder(CABACContext *c){
65 while(c->range < 0x100){
69 }else if(c->low<0x200){
70 c->outstanding_count++;
82 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
83 int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
85 if(bit == ((*state)&1)){
87 *state= c->mps_state[*state];
89 c->low += c->range - RangeLPS;
91 *state= c->lps_state[*state];
94 renorm_cabac_encoder(c);
101 static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
102 assert(c->range > RangeLPS);
105 c->range -= RangeLPS;
107 c->low += c->range - RangeLPS;
111 renorm_cabac_encoder(c);
119 * @param bit 0 -> write zero bit, !=0 write one bit
121 static inline void put_cabac_bypass(CABACContext *c, int bit){
130 }else if(c->low<0x400){
131 c->outstanding_count++;
145 * @return the number of bytes written
147 static inline int put_cabac_terminate(CABACContext *c, int bit){
151 renorm_cabac_encoder(c);
156 renorm_cabac_encoder(c);
158 assert(c->low <= 0x1FF);
159 put_cabac_bit(c, c->low>>9);
160 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
162 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
169 return (put_bits_count(&c->pb)+7)>>3;
173 * put (truncated) unary binarization.
175 static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
182 put_cabac(c, state, 1);
183 if(i < max_index) state++;
185 if(truncated==0 || v<max)
186 put_cabac(c, state, 0);
190 put_cabac(c, state+i, 1);
192 if(truncated==0 || v<max)
193 put_cabac(c, state+i, 0);
195 for(i=0; i<=max_index; i++){
196 put_cabac(c, state+i, 1);
199 put_cabac(c, state+max_index, 1);
201 if(truncated==0 || v<max)
202 put_cabac(c, state+max_index, 0);
208 * put unary exp golomb k-th order binarization.
210 static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
214 put_cabac(c, state, 0);
216 const int sign= v < 0;
218 if(is_signed) v= ABS(v);
222 put_cabac(c, state, 1);
223 if(i < max_index) state++;
226 put_cabac(c, state, 0);
230 for(i=0; i<max; i++){
231 put_cabac(c, state, 1);
232 if(i < max_index) state++;
236 while(v >= m){ //FIXME optimize
237 put_cabac_bypass(c, 1);
241 put_cabac_bypass(c, 0);
243 put_cabac_bypass(c, v&m);
248 put_cabac_bypass(c, sign);
252 static inline void renorm_cabac_decoder(CABACContext *c){
253 while(c->range < 0x10000){
256 if(--c->bits_left == 0){
257 if(c->bytestream < c->bytestream_end)
258 c->low+= *c->bytestream;
265 static inline int get_cabac(CABACContext *c, uint8_t * const state){
266 int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
269 c->range -= RangeLPS;
270 if(c->low < c->range){
272 *state= c->mps_state[*state];
277 *state= c->lps_state[*state];
279 renorm_cabac_decoder(c);
284 static inline int get_cabac_static(CABACContext *c, int RangeLPS){
287 c->range -= RangeLPS;
288 if(c->low < c->range){
295 renorm_cabac_decoder(c);
300 static inline int get_cabac_bypass(CABACContext *c){
303 if(--c->bits_left == 0){
304 if(c->bytestream < c->bytestream_end)
305 c->low+= *c->bytestream;
310 if(c->low < c->range){
320 * @return the number of bytes read or 0 if no end
322 static inline int get_cabac_terminate(CABACContext *c){
324 if(c->low < c->range){
325 renorm_cabac_decoder(c);
328 return c->bytestream - c->bytestream_start;
333 * get (truncated) unnary binarization.
335 static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
338 for(i=0; i<max; i++){
339 if(get_cabac(c, state)==0)
342 if(i< max_index) state++;
345 return truncated ? max : -1;
349 * get unary exp golomb k-th order binarization.
351 static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
355 if(get_cabac(c, state)==0)
358 if(0 < max_index) state++;
360 for(i=1; i<max; i++){
361 if(get_cabac(c, state)==0){
362 if(is_signed && get_cabac_bypass(c)){
368 if(i < max_index) state++;
371 while(get_cabac_bypass(c)){
378 v+= v + get_cabac_bypass(c);
382 if(is_signed && get_cabac_bypass(c)){