X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Ftests%2Fcabac.c;h=b84a96ce81adec53d116e5a30a74566ffc56a3a2;hb=f4df5039a791a56de85c64e6b9e4448a221b5c40;hp=affe4eb141e9c80098916f010e38c4de01092f32;hpb=92219ef4ac01b00e630b39cb19e8fbd17fdb63d0;p=ffmpeg diff --git a/libavcodec/tests/cabac.c b/libavcodec/tests/cabac.c index affe4eb141e..b84a96ce81a 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,29 +101,43 @@ 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 } - return (put_bits_count(&c->pb)+7)>>3; + return put_bytes_count(&c->pb, 1); +} + +/** + * @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