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 uint8_t *bytestream_start;
46 extern const uint8_t ff_h264_lps_range[64][4];
47 extern const uint8_t ff_h264_mps_state[64];
48 extern const uint8_t ff_h264_lps_state[64];
50 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
51 void ff_init_cabac_decoder(CABACContext *c, uint8_t *buf, int buf_size);
52 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
53 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
56 static inline void put_cabac_bit(CABACContext *c, int b){
57 put_bits(&c->pb, 1, b);
58 for(;c->outstanding_count; c->outstanding_count--){
59 put_bits(&c->pb, 1, 1-b);
63 static inline void renorm_cabac_encoder(CABACContext *c){
64 while(c->range < 0x100){
68 }else if(c->low<0x200){
69 c->outstanding_count++;
81 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
82 int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
84 if(bit == ((*state)&1)){
86 *state= c->mps_state[*state];
88 c->low += c->range - RangeLPS;
90 *state= c->lps_state[*state];
93 renorm_cabac_encoder(c);
100 static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
101 assert(c->range > RangeLPS);
104 c->range -= RangeLPS;
106 c->low += c->range - RangeLPS;
110 renorm_cabac_encoder(c);
118 * @param bit 0 -> write zero bit, !=0 write one bit
120 static inline void put_cabac_bypass(CABACContext *c, int bit){
129 }else if(c->low<0x400){
130 c->outstanding_count++;
144 * @return the number of bytes written
146 static inline int put_cabac_terminate(CABACContext *c, int bit){
150 renorm_cabac_encoder(c);
155 renorm_cabac_encoder(c);
157 assert(c->low <= 0x1FF);
158 put_cabac_bit(c, c->low>>9);
159 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
161 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
168 return (get_bit_count(&c->pb)+7)>>3;
172 * put (truncated) unary binarization.
174 static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
181 put_cabac(c, state, 1);
182 if(i < max_index) state++;
184 if(truncated==0 || v<max)
185 put_cabac(c, state, 0);
189 put_cabac(c, state+i, 1);
191 if(truncated==0 || v<max)
192 put_cabac(c, state+i, 0);
194 for(i=0; i<=max_index; i++){
195 put_cabac(c, state+i, 1);
198 put_cabac(c, state+max_index, 1);
200 if(truncated==0 || v<max)
201 put_cabac(c, state+max_index, 0);
207 * put unary exp golomb k-th order binarization.
209 static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
213 put_cabac(c, state, 0);
215 const int sign= v < 0;
217 if(is_signed) v= ABS(v);
221 put_cabac(c, state, 1);
222 if(i < max_index) state++;
225 put_cabac(c, state, 0);
229 for(i=0; i<max; i++){
230 put_cabac(c, state, 1);
231 if(i < max_index) state++;
235 while(v >= m){ //FIXME optimize
236 put_cabac_bypass(c, 1);
240 put_cabac_bypass(c, 0);
242 put_cabac_bypass(c, v&m);
247 put_cabac_bypass(c, sign);
251 static inline void renorm_cabac_decoder(CABACContext *c){
252 while(c->range < 0x10000){
255 if(--c->bits_left == 0){
256 c->low+= *c->bytestream++;
262 static inline int get_cabac(CABACContext *c, uint8_t * const state){
263 int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
266 c->range -= RangeLPS;
267 if(c->low < c->range){
269 *state= c->mps_state[*state];
274 *state= c->lps_state[*state];
276 renorm_cabac_decoder(c);
281 static inline int get_cabac_static(CABACContext *c, int RangeLPS){
284 c->range -= RangeLPS;
285 if(c->low < c->range){
292 renorm_cabac_decoder(c);
297 static inline int get_cabac_bypass(CABACContext *c){
300 if(--c->bits_left == 0){
301 c->low+= *c->bytestream++;
305 if(c->low < c->range){
315 * @return the number of bytes read or 0 if no end
317 static inline int get_cabac_terminate(CABACContext *c){
319 if(c->low < c->range){
320 renorm_cabac_decoder(c);
323 return c->bytestream - c->bytestream_start;
328 * get (truncated) unnary binarization.
330 static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
333 for(i=0; i<max; i++){
334 if(get_cabac(c, state)==0)
337 if(i< max_index) state++;
340 return truncated ? max : -1;
344 * get unary exp golomb k-th order binarization.
346 static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
350 if(get_cabac(c, state)==0)
353 if(0 < max_index) state++;
355 for(i=1; i<max; i++){
356 if(get_cabac(c, state)==0){
357 if(is_signed && get_cabac_bypass(c)){
363 if(i < max_index) state++;
366 while(get_cabac_bypass(c)){
373 v+= v + get_cabac_bypass(c);
377 if(is_signed && get_cabac_bypass(c)){