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
24 * Context Adaptive Binary Arithmetic Coder.
27 #ifndef AVCODEC_CABAC_H
28 #define AVCODEC_CABAC_H
38 #define CABAC_MASK ((1<<CABAC_BITS)-1)
40 typedef struct CABACContext{
43 int outstanding_count;
47 const uint8_t *bytestream_start;
48 const uint8_t *bytestream;
49 const uint8_t *bytestream_end;
53 extern uint8_t ff_h264_mlps_state[4*64];
54 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
55 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
56 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
57 extern const uint8_t ff_h264_norm_shift[512];
60 # include "x86/cabac.h"
63 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
64 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
65 void ff_init_cabac_states(CABACContext *c);
68 static inline void put_cabac_bit(CABACContext *c, int b){
69 put_bits(&c->pb, 1, b);
70 for(;c->outstanding_count; c->outstanding_count--){
71 put_bits(&c->pb, 1, 1-b);
75 static inline void renorm_cabac_encoder(CABACContext *c){
76 while(c->range < 0x100){
80 }else if(c->low<0x200){
81 c->outstanding_count++;
94 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
95 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
97 if(bit == ((*state)&1)){
99 *state= ff_h264_mps_state[*state];
101 c->low += c->range - RangeLPS;
103 *state= ff_h264_lps_state[*state];
106 renorm_cabac_encoder(c);
113 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
114 assert(c->range > RangeLPS);
117 c->range -= RangeLPS;
119 c->low += c->range - RangeLPS;
123 renorm_cabac_encoder(c);
131 * @param bit 0 -> write zero bit, !=0 write one bit
133 static void put_cabac_bypass(CABACContext *c, int bit){
142 }else if(c->low<0x400){
143 c->outstanding_count++;
157 * @return the number of bytes written
159 static int put_cabac_terminate(CABACContext *c, int bit){
163 renorm_cabac_encoder(c);
168 renorm_cabac_encoder(c);
170 assert(c->low <= 0x1FF);
171 put_cabac_bit(c, c->low>>9);
172 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
174 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
181 return (put_bits_count(&c->pb)+7)>>3;
185 * put (truncated) unary binarization.
187 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
194 put_cabac(c, state, 1);
195 if(i < max_index) state++;
197 if(truncated==0 || v<max)
198 put_cabac(c, state, 0);
202 put_cabac(c, state+i, 1);
204 if(truncated==0 || v<max)
205 put_cabac(c, state+i, 0);
207 for(i=0; i<=max_index; i++){
208 put_cabac(c, state+i, 1);
211 put_cabac(c, state+max_index, 1);
213 if(truncated==0 || v<max)
214 put_cabac(c, state+max_index, 0);
220 * put unary exp golomb k-th order binarization.
222 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
226 put_cabac(c, state, 0);
228 const int sign= v < 0;
230 if(is_signed) v= FFABS(v);
234 put_cabac(c, state, 1);
235 if(i < max_index) state++;
238 put_cabac(c, state, 0);
242 for(i=0; i<max; i++){
243 put_cabac(c, state, 1);
244 if(i < max_index) state++;
248 while(v >= m){ //FIXME optimize
249 put_cabac_bypass(c, 1);
253 put_cabac_bypass(c, 0);
255 put_cabac_bypass(c, v&m);
260 put_cabac_bypass(c, sign);
265 static void refill(CABACContext *c){
267 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
269 c->low+= c->bytestream[0]<<1;
271 c->low -= CABAC_MASK;
272 c->bytestream+= CABAC_BITS/8;
275 static inline void renorm_cabac_decoder(CABACContext *c){
276 while(c->range < 0x100){
279 if(!(c->low & CABAC_MASK))
284 static inline void renorm_cabac_decoder_once(CABACContext *c){
285 int shift= (uint32_t)(c->range - 0x100)>>31;
288 if(!(c->low & CABAC_MASK))
292 #ifndef get_cabac_inline
293 static void refill2(CABACContext *c){
296 x= c->low ^ (c->low-1);
297 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
302 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
304 x+= c->bytestream[0]<<1;
308 c->bytestream+= CABAC_BITS/8;
311 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
313 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
316 c->range -= RangeLPS;
317 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
319 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
320 c->range += (RangeLPS - c->range) & lps_mask;
323 *state= (ff_h264_mlps_state+128)[s];
326 lps_mask= ff_h264_norm_shift[c->range];
327 c->range<<= lps_mask;
329 if(!(c->low & CABAC_MASK))
335 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
336 return get_cabac_inline(c,state);
339 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
340 return get_cabac_inline(c,state);
343 static int av_unused get_cabac_bypass(CABACContext *c){
347 if(!(c->low & CABAC_MASK))
350 range= c->range<<(CABAC_BITS+1);
360 #ifndef get_cabac_bypass_sign
361 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
365 if(!(c->low & CABAC_MASK))
368 range= c->range<<(CABAC_BITS+1);
373 return (val^mask)-mask;
379 * @return the number of bytes read or 0 if no end
381 static int av_unused get_cabac_terminate(CABACContext *c){
383 if(c->low < c->range<<(CABAC_BITS+1)){
384 renorm_cabac_decoder_once(c);
387 return c->bytestream - c->bytestream_start;
393 * Get (truncated) unary binarization.
395 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
398 for(i=0; i<max; i++){
399 if(get_cabac(c, state)==0)
402 if(i< max_index) state++;
405 return truncated ? max : -1;
409 * get unary exp golomb k-th order binarization.
411 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
415 if(get_cabac(c, state)==0)
418 if(0 < max_index) state++;
420 for(i=1; i<max; i++){
421 if(get_cabac(c, state)==0){
422 if(is_signed && get_cabac_bypass(c)){
428 if(i < max_index) state++;
431 while(get_cabac_bypass(c)){
438 v+= v + get_cabac_bypass(c);
442 if(is_signed && get_cabac_bypass(c)){
449 #endif /* AVCODEC_CABAC_H */