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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Context Adaptive Binary Arithmetic Coder.
31 #define CABAC_MASK ((1<<CABAC_BITS)-1)
33 typedef struct CABACContext{
36 int outstanding_count;
40 uint8_t lps_range[2*65][4]; ///< rangeTabLPS
41 uint8_t lps_state[2*64]; ///< transIdxLPS
42 uint8_t mps_state[2*64]; ///< transIdxMPS
43 const uint8_t *bytestream_start;
44 const uint8_t *bytestream;
45 const uint8_t *bytestream_end;
49 extern const uint8_t ff_h264_lps_range[64][4];
50 extern const uint8_t ff_h264_mps_state[64];
51 extern const uint8_t ff_h264_lps_state[64];
52 extern const uint8_t ff_h264_norm_shift[256];
55 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
56 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
57 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
58 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
61 static inline void put_cabac_bit(CABACContext *c, int b){
62 put_bits(&c->pb, 1, b);
63 for(;c->outstanding_count; c->outstanding_count--){
64 put_bits(&c->pb, 1, 1-b);
68 static inline void renorm_cabac_encoder(CABACContext *c){
69 while(c->range < 0x100){
73 }else if(c->low<0x200){
74 c->outstanding_count++;
86 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
87 int RangeLPS= c->lps_range[*state][c->range>>6];
89 if(bit == ((*state)&1)){
91 *state= c->mps_state[*state];
93 c->low += c->range - RangeLPS;
95 *state= c->lps_state[*state];
98 renorm_cabac_encoder(c);
105 static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
106 assert(c->range > RangeLPS);
109 c->range -= RangeLPS;
111 c->low += c->range - RangeLPS;
115 renorm_cabac_encoder(c);
123 * @param bit 0 -> write zero bit, !=0 write one bit
125 static inline void put_cabac_bypass(CABACContext *c, int bit){
134 }else if(c->low<0x400){
135 c->outstanding_count++;
149 * @return the number of bytes written
151 static inline int put_cabac_terminate(CABACContext *c, int bit){
155 renorm_cabac_encoder(c);
160 renorm_cabac_encoder(c);
162 assert(c->low <= 0x1FF);
163 put_cabac_bit(c, c->low>>9);
164 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
166 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
173 return (put_bits_count(&c->pb)+7)>>3;
177 * put (truncated) unary binarization.
179 static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
186 put_cabac(c, state, 1);
187 if(i < max_index) state++;
189 if(truncated==0 || v<max)
190 put_cabac(c, state, 0);
194 put_cabac(c, state+i, 1);
196 if(truncated==0 || v<max)
197 put_cabac(c, state+i, 0);
199 for(i=0; i<=max_index; i++){
200 put_cabac(c, state+i, 1);
203 put_cabac(c, state+max_index, 1);
205 if(truncated==0 || v<max)
206 put_cabac(c, state+max_index, 0);
212 * put unary exp golomb k-th order binarization.
214 static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
218 put_cabac(c, state, 0);
220 const int sign= v < 0;
222 if(is_signed) v= ABS(v);
226 put_cabac(c, state, 1);
227 if(i < max_index) state++;
230 put_cabac(c, state, 0);
234 for(i=0; i<max; i++){
235 put_cabac(c, state, 1);
236 if(i < max_index) state++;
240 while(v >= m){ //FIXME optimize
241 put_cabac_bypass(c, 1);
245 put_cabac_bypass(c, 0);
247 put_cabac_bypass(c, v&m);
252 put_cabac_bypass(c, sign);
256 static void refill(CABACContext *c){
257 if(c->bytestream <= c->bytestream_end)
259 c->low+= ((c->bytestream[0]<<9) + (c->bytestream[1])<<1);
261 c->low+= c->bytestream[0]<<1;
263 c->low -= CABAC_MASK;
264 c->bytestream+= CABAC_BITS/8;
267 #if 0 /* all use commented */
268 static void refill2(CABACContext *c){
271 x= c->low ^ (c->low-1);
272 i= 8 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
276 if(c->bytestream < c->bytestream_end)
278 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
280 x+= c->bytestream[0]<<1;
284 c->bytestream+= CABAC_BITS/8;
288 static inline void renorm_cabac_decoder(CABACContext *c){
289 while(c->range < (0x200 << CABAC_BITS)){
292 if(!(c->low & CABAC_MASK))
297 static inline void renorm_cabac_decoder_once(CABACContext *c){
298 int mask= (c->range - (0x200 << CABAC_BITS))>>31;
299 c->range+= c->range&mask;
300 c->low += c->low &mask;
301 if(!(c->low & CABAC_MASK))
305 static inline int get_cabac(CABACContext *c, uint8_t * const state){
306 int RangeLPS= c->lps_range[*state][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
307 int bit, lps_mask attribute_unused;
309 c->range -= RangeLPS;
311 if(c->low < c->range){
313 *state= c->mps_state[*state];
314 renorm_cabac_decoder_once(c);
316 // int shift= ff_h264_norm_shift[RangeLPS>>17];
319 *state= c->lps_state[*state];
321 renorm_cabac_decoder(c);
322 /* c->range = RangeLPS<<shift;
324 if(!(c->low & 0xFFFF)){
329 lps_mask= (c->range - c->low)>>31;
331 c->low -= c->range & lps_mask;
332 c->range += (RangeLPS - c->range) & lps_mask;
334 bit= ((*state)^lps_mask)&1;
335 *state= c->mps_state[(*state) - (128&lps_mask)];
337 lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)];
338 c->range<<= lps_mask;
340 if(!(c->low & CABAC_MASK))
347 static inline int get_cabac_bypass(CABACContext *c){
350 if(!(c->low & CABAC_MASK))
353 if(c->low < c->range){
363 * @return the number of bytes read or 0 if no end
365 static inline int get_cabac_terminate(CABACContext *c){
366 c->range -= 4<<CABAC_BITS;
367 if(c->low < c->range){
368 renorm_cabac_decoder_once(c);
371 return c->bytestream - c->bytestream_start;
376 * get (truncated) unnary binarization.
378 static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
381 for(i=0; i<max; i++){
382 if(get_cabac(c, state)==0)
385 if(i< max_index) state++;
388 return truncated ? max : -1;
392 * get unary exp golomb k-th order binarization.
394 static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
398 if(get_cabac(c, state)==0)
401 if(0 < max_index) state++;
403 for(i=1; i<max; i++){
404 if(get_cabac(c, state)==0){
405 if(is_signed && get_cabac_bypass(c)){
411 if(i < max_index) state++;
414 while(get_cabac_bypass(c)){
421 v+= v + get_cabac_bypass(c);
425 if(is_signed && get_cabac_bypass(c)){