ref: d77f61ac22fa57d2f24039a959ea722b9d725629
parent: 761811d91bc6fc18b28c364252ea2b232dc4f75a
author: Timothy B.B Terriberry <[email protected]>
date: Sun Oct 19 10:24:53 EDT 2008
Fixes some issues in the MF range coder on systems were ints are 16 bits.
--- a/libcelt/mfrngdec.c
+++ b/libcelt/mfrngdec.c
@@ -141,8 +141,7 @@
ret=ec_byte_read1(_this->buf);
if(ret<0){
ret=0;
- /*Needed to make sure the above conditional only triggers once, and to keep
- oc_dec_tell() operating correctly.*/
+ /*Needed to keep oc_dec_tell() operating correctly.*/
ec_byte_adv1(_this->buf);
}
return ret;
@@ -179,16 +178,19 @@
}
unsigned ec_decode(ec_dec *_this,unsigned _ft){
- unsigned d;
+ ec_uint32 ft;
+ ec_uint32 d;
+ unsigned e;
/*Step 1: Compute the normalization factor for the frequency counts.*/
_this->nrm=EC_ILOG(_this->rng)-EC_ILOG(_ft);
- _ft<<=_this->nrm;
- d=_ft>_this->rng;
- _ft>>=d;
- _this->nrm-=d;
+ ft=(ec_uint32)_ft<<_this->nrm;
+ e=ft>_this->rng;
+ ft>>=e;
+ _this->nrm-=e;
/*Step 2: invert the partition function.*/
- d=_this->rng-_ft;
- return EC_MAXI((int)(_this->dif>>1),(int)(_this->dif-d))>>_this->nrm;
+ d=_this->rng-ft;
+ return EC_MAXI((ec_int32)(_this->dif>>1),(ec_int32)(_this->dif-d))>>
+ _this->nrm;
/*Step 3: The caller locates the range [fl,fh) containing the return value
and calls ec_dec_update().*/
}
@@ -198,16 +200,19 @@
}
void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){
- unsigned r;
- unsigned s;
- unsigned d;
+ ec_uint32 fl;
+ ec_uint32 fh;
+ ec_uint32 ft;
+ ec_uint32 r;
+ ec_uint32 s;
+ ec_uint32 d;
/*Step 4: Evaluate the two partition function values.*/
- _fl<<=_this->nrm;
- _fh<<=_this->nrm;
- _ft<<=_this->nrm;
- d=_this->rng-_ft;
- r=_fh+EC_MINI(_fh,d);
- s=_fl+EC_MINI(_fl,d);
+ fl=(ec_uint32)_fl<<_this->nrm;
+ fh=(ec_uint32)_fh<<_this->nrm;
+ ft=(ec_uint32)_ft<<_this->nrm;
+ d=_this->rng-ft;
+ r=fh+EC_MINI(fh,d);
+ s=fl+EC_MINI(fl,d);
/*Step 5: Update the interval.*/
_this->rng=r-s;
_this->dif-=s;
@@ -219,7 +224,8 @@
ec_uint32 r;
int l;
long nbits;
- nbits=ec_byte_bytes(_this->buf)-(EC_CODE_BITS+EC_SYM_BITS-1)/EC_SYM_BITS<<3;
+ nbits=(ec_byte_bytes(_this->buf)-(EC_CODE_BITS+EC_SYM_BITS-1)/EC_SYM_BITS)*
+ EC_SYM_BITS;
/*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.
--- a/libcelt/mfrngenc.c
+++ b/libcelt/mfrngenc.c
@@ -92,10 +92,14 @@
}
void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){
- unsigned r;
- unsigned s;
- unsigned d;
- int nrm;
+ ec_uint32 fl;
+ ec_uint32 fh;
+ ec_uint32 ft;
+ ec_uint32 r;
+ ec_uint32 s;
+ ec_uint32 d;
+ int e;
+ int nrm;
/*Step 1: we want ft in the range of [rng/2,rng).
The high-order bits of the rng and ft are computed via a logarithm.
This could also be done on some architectures with some custom assembly,
@@ -104,18 +108,18 @@
/*Having the same high order bit may be too much.
We may need to shift one less to ensure that ft is actually in the proper
range.*/
- _ft<<=nrm;
- d=_ft>_this->rng;
- _ft>>=d;
- nrm-=d;
+ ft=(ec_uint32)_ft<<nrm;
+ e=ft>_this->rng;
+ ft>>=e;
+ nrm-=e;
/*We then scale everything by the computed power of 2.*/
- _fl<<=nrm;
- _fh<<=nrm;
+ fl=(ec_uint32)_fl<<nrm;
+ fh=(ec_uint32)_fh<<nrm;
/*Step 2: compute the two values of the partition function.
d is the splitting point of the interval [0,ft).*/
- d=_this->rng-_ft;
- r=_fh+EC_MINI(_fh,d);
- s=_fl+EC_MINI(_fl,d);
+ d=_this->rng-ft;
+ r=fh+EC_MINI(fh,d);
+ s=fl+EC_MINI(fl,d);
/*Step 3: Update the end-point and range of the interval.*/
_this->low+=s;
_this->rng=r-s;
@@ -131,7 +135,7 @@
ec_uint32 r;
int l;
long nbits;
- nbits=ec_byte_bytes(_this->buf)+(_this->rem>=0)+_this->ext<<3;
+ nbits=(ec_byte_bytes(_this->buf)+(_this->rem>=0)+_this->ext)*EC_SYM_BITS;
/*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.
--- a/libcelt/rangedec.c
+++ b/libcelt/rangedec.c
@@ -124,8 +124,7 @@
ret=ec_byte_read1(_this->buf);
if(ret<0){
ret=0;
- /*Needed to make sure the above conditional only triggers once, and to keep
- oc_dec_tell() operating correctly.*/
+ /*Needed to keep oc_dec_tell() operating correctly.*/
ec_byte_adv1(_this->buf);
}
return ret;
@@ -190,7 +189,8 @@
ec_uint32 r;
int l;
long nbits;
- nbits=ec_byte_bytes(_this->buf)-(EC_CODE_BITS+EC_SYM_BITS-1)/EC_SYM_BITS<<3;
+ nbits=(ec_byte_bytes(_this->buf)-(EC_CODE_BITS+EC_SYM_BITS-1)/EC_SYM_BITS)*
+ EC_SYM_BITS;
/*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.
--- a/libcelt/rangeenc.c
+++ b/libcelt/rangeenc.c
@@ -109,7 +109,7 @@
ec_uint32 r;
int l;
long nbits;
- nbits=ec_byte_bytes(_this->buf)+(_this->rem>=0)+_this->ext<<3;
+ nbits=(ec_byte_bytes(_this->buf)+(_this->rem>=0)+_this->ext)*EC_SYM_BITS;
/*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.