diff --git a/libentcode/ectest.c b/libentcode/ectest.c index 18d42bf1..85994dcf 100644 --- a/libentcode/ectest.c +++ b/libentcode/ectest.c @@ -11,6 +11,7 @@ int main(int _argc,char **_argv){ ec_probmod mod; ec_uint64 sym64; long nbits; + long nbits2; double entropy; int ft; int ftb; @@ -33,12 +34,10 @@ int main(int _argc,char **_argv){ /*Testing encoding of raw bit values.*/ for(ftb=0;ftb<16;ftb++){ for(i=0;i<(1<buf); if(ret<0){ - unsigned char *buf; - long bytes; + long bytes; bytes=ec_byte_bytes(_this->buf); - buf=ec_byte_get_buffer(_this->buf); /*Breaking abstraction: don't do this at home, kids.*/ - if(_this->buf->storage==bytes){ - ec_byte_adv1(_this->buf); - if(bytes>0){ - unsigned char *p; - p=buf+bytes; - /*If we end in a string of 0 or more EC_FOF_RSV1 bytes preceded by a - zero, return an extra EC_FOF_RSV1 byte.*/ - do p--; - while(p>buf&&p[0]==EC_FOF_RSV1); - if(!p[0])return EC_FOF_RSV1; - } + if(_this->buf->storage==bytes&&bytes>0){ + unsigned char *buf; + buf=ec_byte_get_buffer(_this->buf); + /*If we end in a string of 0 or more EC_FOF_RSV1 bytes preceded by a + zero, return an extra EC_FOF_RSV1 byte.*/ + do bytes--; + while(bytes>0&&buf[bytes]==EC_FOF_RSV1); + if(!buf[bytes])ret=EC_FOF_RSV1; } - return 0; + else ret=0; + /*Needed to make sure the above conditional only triggers once, and to keep + oc_dec_tell() operating correctly.*/ + ec_byte_adv1(_this->buf); } - else return ret; + return ret; } /*Normalizes the contents of dif and rng so that rng lies entirely in the @@ -222,6 +220,31 @@ void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){ ec_dec_normalize(_this); } +long ec_dec_tell(ec_dec *_this,int _b){ + ec_uint32 r; + int l; + long nbits; + nbits=ec_byte_bytes(_this->buf)-(EC_CODE_BITS+EC_SYM_BITS-1)/EC_SYM_BITS<<3; + /*To handle the non-integral number of bits still left in the encoder state, + we compute the number of bits of low that must be encoded to ensure that + the value is inside the range for any possible subsequent bits. + Note that this is subtly different than the actual value we would end the + stream with, which tries to make as many of the trailing bits zeros as + possible.*/ + nbits+=EC_CODE_BITS; + nbits<<=_b; + l=EC_ILOG(_this->rng); + r=_this->rng>>l-16; + while(_b-->0){ + int b; + r=r*r>>15; + b=(int)(r>>16); + l=l<<1|b; + r>>=b; + } + return nbits-l; +} + #if 0 int ec_dec_done(ec_dec *_this){ unsigned low; diff --git a/libentcode/mfrngenc.c b/libentcode/mfrngenc.c index 5debf8ad..2b2b15c6 100644 --- a/libentcode/mfrngenc.c +++ b/libentcode/mfrngenc.c @@ -119,20 +119,7 @@ void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){ ec_enc_normalize(_this); } -long ec_enc_tell(ec_enc *_this){ - long nbits; - nbits=ec_byte_bytes(_this->buf)+(_this->rem>=0)+_this->ext<<3; - /*To handle the non-integral number of bits still left in the encoder state, - we compute the number of bits of low that must be encoded to ensure that - the value is inside the range for any possible subsequent bits. - Note that this is subtly different than the actual value we would end the - stream with, which tries to make as many of the trailing bits zeros as - possible.*/ - nbits+=EC_CODE_BITS-EC_ILOG(_this->rng); - return nbits; -} - -long ec_enc_tellf(ec_enc *_this,int _b){ +long ec_enc_tell(ec_enc *_this,int _b){ ec_uint32 r; int l; long nbits; diff --git a/libentcode/rangedec.c b/libentcode/rangedec.c index 76817472..49d26fd7 100644 --- a/libentcode/rangedec.c +++ b/libentcode/rangedec.c @@ -120,26 +120,24 @@ static int ec_dec_in(ec_dec *_this){ int ret; ret=ec_byte_read1(_this->buf); if(ret<0){ - unsigned char *buf; - long bytes; + long bytes; bytes=ec_byte_bytes(_this->buf); - buf=ec_byte_get_buffer(_this->buf); /*Breaking abstraction: don't do this at home, kids.*/ - if(_this->buf->storage==bytes){ - ec_byte_adv1(_this->buf); - if(bytes>0){ - unsigned char *p; - p=buf+bytes; - /*If we end in a string of 0 or more EC_FOF_RSV1 bytes preceded by a - zero, return an extra EC_FOF_RSV1 byte.*/ - do p--; - while(p>buf&&p[0]==EC_FOF_RSV1); - if(!p[0])return EC_FOF_RSV1; - } + if(_this->buf->storage==bytes&&bytes>0){ + unsigned char *buf; + buf=ec_byte_get_buffer(_this->buf); + /*If we end in a string of 0 or more EC_FOF_RSV1 bytes preceded by a + zero, return an extra EC_FOF_RSV1 byte.*/ + do bytes--; + while(bytes>0&&buf[bytes]==EC_FOF_RSV1); + if(!buf[bytes])ret=EC_FOF_RSV1; } - return 0; + else ret=0; + /*Needed to make sure the above conditional only triggers once, and to keep + oc_dec_tell() operating correctly.*/ + ec_byte_adv1(_this->buf); } - else return ret; + return ret; } /*Normalizes the contents of dif and rng so that rng lies entirely in the @@ -188,6 +186,31 @@ void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){ ec_dec_normalize(_this); } +long ec_dec_tell(ec_dec *_this,int _b){ + ec_uint32 r; + int l; + long nbits; + nbits=ec_byte_bytes(_this->buf)-(EC_CODE_BITS+EC_SYM_BITS-1)/EC_SYM_BITS<<3; + /*To handle the non-integral number of bits still left in the encoder state, + we compute the number of bits of low that must be encoded to ensure that + the value is inside the range for any possible subsequent bits. + Note that this is subtly different than the actual value we would end the + stream with, which tries to make as many of the trailing bits zeros as + possible.*/ + nbits+=EC_CODE_BITS; + nbits<<=_b; + l=EC_ILOG(_this->rng); + r=_this->rng>>l-16; + while(_b-->0){ + int b; + r=r*r>>15; + b=(int)(r>>16); + l=l<<1|b; + r>>=b; + } + return nbits-l; +} + #if 0 int ec_dec_done(ec_dec *_this){ unsigned low; diff --git a/libentcode/rangeenc.c b/libentcode/rangeenc.c index 8d3eef3b..246a4555 100644 --- a/libentcode/rangeenc.c +++ b/libentcode/rangeenc.c @@ -91,20 +91,7 @@ void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){ ec_enc_normalize(_this); } -long ec_enc_tell(ec_enc *_this){ - long nbits; - nbits=ec_byte_bytes(_this->buf)+(_this->rem>=0)+_this->ext<<3; - /*To handle the non-integral number of bits still left in the encoder state, - we compute the number of bits of low that must be encoded to ensure that - the value is inside the range for any possible subsequent bits. - Note that this is subtly different than the actual value we would end the - stream with, which tries to make as many of the trailing bits zeros as - possible.*/ - nbits+=EC_CODE_BITS-EC_ILOG(_this->rng); - return nbits; -} - -long ec_enc_tellf(ec_enc *_this,int _b){ +long ec_enc_tell(ec_enc *_this,int _b){ ec_uint32 r; int l; long nbits;