diff --git a/celt/entdec.c b/celt/entdec.c index ba224e24aecae5e0aff55a6bdcd6e5dfd267519a..8ba09864bce1c5ae4234a0ecf0aef4544b519197 100644 --- a/celt/entdec.c +++ b/celt/entdec.c @@ -126,6 +126,11 @@ void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage){ _this->end_offs=0; _this->end_window=0; _this->nend_bits=0; + /*This is the offset from which ec_tell() will subtract partial bits. + The final value after the ec_dec_normalize() call will be the same as in + the encoder, but we have to compensate for the bits that are added there.*/ + _this->nbits_total=EC_CODE_BITS+1 + -((EC_CODE_BITS-EC_CODE_EXTRA)/EC_SYM_BITS)*EC_SYM_BITS; _this->offs=0; _this->rng=1U<<EC_CODE_EXTRA; _this->rem=ec_read_byte(_this); @@ -133,10 +138,6 @@ void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage){ _this->error=0; /*Normalize the interval.*/ ec_dec_normalize(_this); - /*This is the offset from which ec_tell() will subtract partial bits. - This must be after the initial ec_dec_normalize(), or you will have to - compensate for the bits that are read there.*/ - _this->nbits_total=EC_CODE_BITS+1; } unsigned ec_decode(ec_dec *_this,unsigned _ft){ diff --git a/doc/draft-ietf-codec-opus.xml b/doc/draft-ietf-codec-opus.xml index d53c20e847f3f0e594798b7aba3653c576146602..4524c3e5e9ee5dcc7e0fbf489c5a59272e7df1db 100644 --- a/doc/draft-ietf-codec-opus.xml +++ b/doc/draft-ietf-codec-opus.xml @@ -1344,8 +1344,8 @@ The reference implementation keeps track of the total number of whole bits that have been processed by the decoder so far in the variable nbits_total, including the (possibly fractional) number of bits that are currently buffered, but not consumed, inside the range coder. -nbits_total is initialized to 33 just after the initial range renormalization - process completes (or equivalently, it can be initialized to 9 before the +nbits_total is initialized to 9 just before the initial range renormalization + process completes (or equivalently, it can be initialized to 33 after the first renormalization). The extra two bits over the actual amount buffered by the range coder guarantees that it is an upper bound and that there is enough room for the