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
25 * Context Adaptive Binary Arithmetic Coder.
33 #define CABAC_MASK ((1<<CABAC_BITS)-1)
35 typedef struct CABACContext{
38 int outstanding_count;
42 uint8_t lps_range[2*65][4]; ///< rangeTabLPS
43 uint8_t lps_state[2*64]; ///< transIdxLPS
44 uint8_t mps_state[2*64]; ///< transIdxMPS
45 const uint8_t *bytestream_start;
46 const uint8_t *bytestream;
47 const uint8_t *bytestream_end;
51 extern const uint8_t ff_h264_lps_range[64][4];
52 extern const uint8_t ff_h264_mps_state[64];
53 extern const uint8_t ff_h264_lps_state[64];
54 extern const uint8_t ff_h264_norm_shift[256];
57 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
58 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
59 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
60 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
63 static inline void put_cabac_bit(CABACContext *c, int b){
64 put_bits(&c->pb, 1, b);
65 for(;c->outstanding_count; c->outstanding_count--){
66 put_bits(&c->pb, 1, 1-b);
70 static inline void renorm_cabac_encoder(CABACContext *c){
71 while(c->range < 0x100){
75 }else if(c->low<0x200){
76 c->outstanding_count++;
88 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
89 int RangeLPS= c->lps_range[*state][c->range>>6];
91 if(bit == ((*state)&1)){
93 *state= c->mps_state[*state];
95 c->low += c->range - RangeLPS;
97 *state= c->lps_state[*state];
100 renorm_cabac_encoder(c);
107 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
108 assert(c->range > RangeLPS);
111 c->range -= RangeLPS;
113 c->low += c->range - RangeLPS;
117 renorm_cabac_encoder(c);
125 * @param bit 0 -> write zero bit, !=0 write one bit
127 static void put_cabac_bypass(CABACContext *c, int bit){
136 }else if(c->low<0x400){
137 c->outstanding_count++;
151 * @return the number of bytes written
153 static int put_cabac_terminate(CABACContext *c, int bit){
157 renorm_cabac_encoder(c);
162 renorm_cabac_encoder(c);
164 assert(c->low <= 0x1FF);
165 put_cabac_bit(c, c->low>>9);
166 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
168 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
175 return (put_bits_count(&c->pb)+7)>>3;
179 * put (truncated) unary binarization.
181 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
188 put_cabac(c, state, 1);
189 if(i < max_index) state++;
191 if(truncated==0 || v<max)
192 put_cabac(c, state, 0);
196 put_cabac(c, state+i, 1);
198 if(truncated==0 || v<max)
199 put_cabac(c, state+i, 0);
201 for(i=0; i<=max_index; i++){
202 put_cabac(c, state+i, 1);
205 put_cabac(c, state+max_index, 1);
207 if(truncated==0 || v<max)
208 put_cabac(c, state+max_index, 0);
214 * put unary exp golomb k-th order binarization.
216 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
220 put_cabac(c, state, 0);
222 const int sign= v < 0;
224 if(is_signed) v= ABS(v);
228 put_cabac(c, state, 1);
229 if(i < max_index) state++;
232 put_cabac(c, state, 0);
236 for(i=0; i<max; i++){
237 put_cabac(c, state, 1);
238 if(i < max_index) state++;
242 while(v >= m){ //FIXME optimize
243 put_cabac_bypass(c, 1);
247 put_cabac_bypass(c, 0);
249 put_cabac_bypass(c, v&m);
254 put_cabac_bypass(c, sign);
258 static void refill(CABACContext *c){
259 if(c->bytestream <= c->bytestream_end)
261 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
263 c->low+= c->bytestream[0]<<1;
265 c->low -= CABAC_MASK;
266 c->bytestream+= CABAC_BITS/8;
269 #if 0 /* all use commented */
270 static void refill2(CABACContext *c){
273 x= c->low ^ (c->low-1);
274 i= 8 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
278 if(c->bytestream < c->bytestream_end)
280 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
282 x+= c->bytestream[0]<<1;
286 c->bytestream+= CABAC_BITS/8;
290 static inline void renorm_cabac_decoder(CABACContext *c){
291 while(c->range < (0x200 << CABAC_BITS)){
294 if(!(c->low & CABAC_MASK))
299 static inline void renorm_cabac_decoder_once(CABACContext *c){
305 "lea -0x2000000(%0), %2 \n\t"
306 "shr $31, %2 \n\t" //FIXME 31->63 for x86-64
309 : "+r"(c->range), "+r"(c->low), "+c"(temp)
314 "cmp $0x2000000, %0 \n\t"
315 "setb %%cl \n\t" //FIXME 31->63 for x86-64
318 : "+r"(c->range), "+r"(c->low), "+c"(temp)
324 "lea -0x2000000(%0), %%eax \n\t"
331 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
337 "cmp $0x2000000, %0 \n\t"
338 "sbb %%edx, %%edx \n\t"
344 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
350 "cmp $0x2000000, %0 \n\t"
351 "lea (%0, %0), %%eax \n\t"
352 "lea (%1, %1), %%edx \n\t"
353 "cmovb %%eax, %0 \n\t"
354 "cmovb %%edx, %1 \n\t"
355 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
360 int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31;
364 if(!(c->low & CABAC_MASK))
368 static int get_cabac(CABACContext *c, uint8_t * const state){
369 //FIXME gcc generates duplicate load/stores for c->low and c->range
372 int RangeLPS= c->lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
373 int bit, lps_mask attribute_unused;
375 c->range -= RangeLPS;
377 if(c->low < c->range){
379 *state= c->mps_state[s];
380 renorm_cabac_decoder_once(c);
382 // int shift= ff_h264_norm_shift[RangeLPS>>17];
385 *state= c->lps_state[s];
387 renorm_cabac_decoder(c);
388 /* c->range = RangeLPS<<shift;
390 if(!(c->low & 0xFFFF)){
395 lps_mask= (c->range - c->low)>>31;
397 c->low -= c->range & lps_mask;
398 c->range += (RangeLPS - c->range) & lps_mask;
401 *state= c->mps_state[s - (128&lps_mask)];
403 lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)];
404 c->range<<= lps_mask;
406 if(!(c->low & CABAC_MASK))
409 STOP_TIMER("get_cabac")
413 static int get_cabac_bypass(CABACContext *c){
416 if(!(c->low & CABAC_MASK))
419 if(c->low < c->range){
429 * @return the number of bytes read or 0 if no end
431 static int get_cabac_terminate(CABACContext *c){
432 c->range -= 4<<CABAC_BITS;
433 if(c->low < c->range){
434 renorm_cabac_decoder_once(c);
437 return c->bytestream - c->bytestream_start;
442 * get (truncated) unnary binarization.
444 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
447 for(i=0; i<max; i++){
448 if(get_cabac(c, state)==0)
451 if(i< max_index) state++;
454 return truncated ? max : -1;
458 * get unary exp golomb k-th order binarization.
460 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
464 if(get_cabac(c, state)==0)
467 if(0 < max_index) state++;
469 for(i=1; i<max; i++){
470 if(get_cabac(c, state)==0){
471 if(is_signed && get_cabac_bypass(c)){
477 if(i < max_index) state++;
480 while(get_cabac_bypass(c)){
487 v+= v + get_cabac_bypass(c);
491 if(is_signed && get_cabac_bypass(c)){