diff --git a/libavcodec/cabac.c b/libavcodec/cabac.c index 3bbecf50dd..6649feebeb 100644 --- a/libavcodec/cabac.c +++ b/libavcodec/cabac.c @@ -158,18 +158,6 @@ DECLARE_ASM_ALIGNED(1, const uint8_t, ff_h264_cabac_tables)[512 + 4*2*64 + 4*64 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8 }; -/** - * @param buf_size size of buf in bits - */ -void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size){ - init_put_bits(&c->pb, buf, buf_size); - - c->low= 0; - c->range= 0x1FE; - c->outstanding_count= 0; - c->pb.bit_left++; //avoids firstBitFlag -} - /** * * @param buf_size size of buf in bits diff --git a/libavcodec/cabac.h b/libavcodec/cabac.h index 1bf1c620d6..38d06b2842 100644 --- a/libavcodec/cabac.h +++ b/libavcodec/cabac.h @@ -29,8 +29,6 @@ #include -#include "put_bits.h" - extern const uint8_t ff_h264_cabac_tables[512 + 4*2*64 + 4*64 + 63]; #define H264_NORM_SHIFT_OFFSET 0 #define H264_LPS_RANGE_OFFSET 512 @@ -43,14 +41,11 @@ extern const uint8_t ff_h264_cabac_tables[512 + 4*2*64 + 4*64 + 63]; typedef struct CABACContext{ int low; int range; - int outstanding_count; const uint8_t *bytestream_start; const uint8_t *bytestream; const uint8_t *bytestream_end; - PutBitContext pb; }CABACContext; -void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size); int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size); #endif /* AVCODEC_CABAC_H */ diff --git a/libavcodec/tests/cabac.c b/libavcodec/tests/cabac.c index affe4eb141..a8bd131c95 100644 --- a/libavcodec/tests/cabac.c +++ b/libavcodec/tests/cabac.c @@ -24,41 +24,51 @@ #include "libavutil/lfg.h" #include "libavcodec/avcodec.h" +#include "libavcodec/put_bits.h" -static inline void put_cabac_bit(CABACContext *c, int b){ +typedef struct CABACTestContext { + CABACContext dec; + int outstanding_count; + PutBitContext pb; +} CABACTestContext; + +static inline void put_cabac_bit(CABACTestContext *c, int b) +{ put_bits(&c->pb, 1, b); for(;c->outstanding_count; c->outstanding_count--){ put_bits(&c->pb, 1, 1-b); } } -static inline void renorm_cabac_encoder(CABACContext *c){ - while(c->range < 0x100){ +static inline void renorm_cabac_encoder(CABACTestContext *c) +{ + while (c->dec.range < 0x100) { //FIXME optimize - if(c->low<0x100){ + if (c->dec.low < 0x100) { put_cabac_bit(c, 0); - }else if(c->low<0x200){ + } else if (c->dec.low < 0x200) { c->outstanding_count++; - c->low -= 0x100; + c->dec.low -= 0x100; }else{ put_cabac_bit(c, 1); - c->low -= 0x200; + c->dec.low -= 0x200; } - c->range+= c->range; - c->low += c->low; + c->dec.range += c->dec.range; + c->dec.low += c->dec.low; } } -static void put_cabac(CABACContext *c, uint8_t * const state, int bit){ - int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state]; +static void put_cabac(CABACTestContext *c, uint8_t * const state, int bit) +{ + int RangeLPS = ff_h264_lps_range[2 * (c->dec.range & 0xC0) + *state]; if(bit == ((*state)&1)){ - c->range -= RangeLPS; + c->dec.range -= RangeLPS; *state = ff_h264_mlps_state[128 + *state]; }else{ - c->low += c->range - RangeLPS; - c->range = RangeLPS; + c->dec.low += c->dec.range - RangeLPS; + c->dec.range = RangeLPS; *state= ff_h264_mlps_state[127 - *state]; } @@ -68,21 +78,22 @@ static void put_cabac(CABACContext *c, uint8_t * const state, int bit){ /** * @param bit 0 -> write zero bit, !=0 write one bit */ -static void put_cabac_bypass(CABACContext *c, int bit){ - c->low += c->low; +static void put_cabac_bypass(CABACTestContext *c, int bit) +{ + c->dec.low += c->dec.low; if(bit){ - c->low += c->range; + c->dec.low += c->dec.range; } //FIXME optimize - if(c->low<0x200){ + if (c->dec.low < 0x200) { put_cabac_bit(c, 0); - }else if(c->low<0x400){ + } else if (c->dec.low < 0x400) { c->outstanding_count++; - c->low -= 0x200; + c->dec.low -= 0x200; }else{ put_cabac_bit(c, 1); - c->low -= 0x400; + c->dec.low -= 0x400; } } @@ -90,20 +101,21 @@ static void put_cabac_bypass(CABACContext *c, int bit){ * * @return the number of bytes written */ -static int put_cabac_terminate(CABACContext *c, int bit){ - c->range -= 2; +static int put_cabac_terminate(CABACTestContext *c, int bit) +{ + c->dec.range -= 2; if(!bit){ renorm_cabac_encoder(c); }else{ - c->low += c->range; - c->range= 2; + c->dec.low += c->dec.range; + c->dec.range = 2; renorm_cabac_encoder(c); - av_assert0(c->low <= 0x1FF); - put_cabac_bit(c, c->low>>9); - put_bits(&c->pb, 2, ((c->low>>7)&3)|1); + av_assert0(c->dec.low <= 0x1FF); + put_cabac_bit(c, c->dec.low >> 9); + put_bits(&c->pb, 2, ((c->dec.low >> 7) & 3) | 1); flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong } @@ -111,8 +123,21 @@ static int put_cabac_terminate(CABACContext *c, int bit){ return (put_bits_count(&c->pb)+7)>>3; } +/** + * @param buf_size size of buf in bits + */ +static void init_cabac_encoder(CABACTestContext *c, uint8_t *buf, int buf_size) +{ + init_put_bits(&c->pb, buf, buf_size); + + c->dec.low = 0; + c->dec.range = 0x1FE; + c->outstanding_count = 0; + c->pb.bit_left++; //avoids firstBitFlag +} + int main(void){ - CABACContext c; + CABACTestContext c; uint8_t b[9*SIZE]; uint8_t r[9*SIZE]; int i, ret = 0; @@ -120,7 +145,7 @@ int main(void){ AVLFG prng; av_lfg_init(&prng, 1); - ff_init_cabac_encoder(&c, b, SIZE); + init_cabac_encoder(&c, b, SIZE); for(i=0; i