static int bitexact_log2tan(int isin,int icos) { int lc; int ls; lc=EC_ILOG(icos); ls=EC_ILOG(isin); icos<<=15-lc; isin<<=15-ls; return (ls-lc)*(1<<11) +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932) -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932); }
opus_uint32 ec_tell_frac(ec_ctx *_this) { opus_uint32 nbits; opus_uint32 r; int l; int i; /*To handle the non-integral number of bits still left in the encoder/decoder state, we compute the worst-case number of bits of val that must be encoded to ensure that the value is inside the range for any possible subsequent bits. The computation here is independent of val itself (the decoder does not even track that value), even though the real number of bits used after ec_enc_done() may be 1 smaller if rng is a power of two and the corresponding trailing bits of val are all zeros. If we did try to track that special case, then coding a value with a probability of 1/(1<<n) might sometimes appear to use more than n bits. This may help explain the surprising result that a newly initialized encoder or decoder claims to have used 1 bit.*/ nbits=_this->nbits_total<<BITRES; l=EC_ILOG(_this->rng); r=_this->rng>>(l-16); for(i=BITRES; i-->0;) { int b; r=r*r>>15; b=(int)(r>>16); l=l<<1|b; r>>=b; } return nbits-l; }
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)* 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. 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; }
unsigned ec_decode(ec_dec *_this,unsigned _ft){ 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=(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((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().*/ }
void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){ unsigned ft; unsigned fl; int ftb; _ft--; ftb=EC_ILOG(_ft)&-!!_ft; if(ftb>EC_UNIT_BITS){ ftb-=EC_UNIT_BITS; ft=(_ft>>ftb)+1; fl=(unsigned)(_fl>>ftb); ec_encode(_this,fl,fl+1,ft); ec_enc_bits(_this,_fl,ftb); } else {
opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft){ unsigned ft; unsigned s; int ftb; /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/ celt_assert(_ft>1); _ft--; ftb=EC_ILOG(_ft); if(ftb>EC_UINT_BITS){ opus_uint32 t; ftb-=EC_UINT_BITS; ft=(unsigned)(_ft>>ftb)+1; s=ec_decode(_this,ft); ec_dec_update(_this,s,s+1,ft); t=(opus_uint32)s<<ftb|ec_dec_bits(_this,ftb); if(t<=_ft)return t; _this->error=1; return _ft; }
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)* EC_SYM_BITS; /*To handle the non-integral number of bits still left in the decoder 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.*/ nbits+=EC_CODE_BITS+1+_this->nb_end_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; }
/*Guaranteed to return a conservatively large estimate of the binary logarithm with frac bits of fractional precision. Tested for all possible 32-bit inputs with frac=4, where the maximum overestimation is 0.06254243 bits.*/ int log2_frac(opus_uint32 val, int frac) { int l; l=EC_ILOG(val); if(val&(val-1)) { /*This is (val>>l-16), but guaranteed to round up, even if adding a bias before the shift would cause overflow (e.g., for 0xFFFFxxxx).*/ if(l>16)val=(val>>(l-16))+(((val&((1<<(l-16))-1))+(1<<(l-16))-1)>>(l-16)); else val<<=16-l; l=(l-1)<<frac; /*Note that we always need one iteration, since the rounding up above means that we might need to adjust the integer part of the logarithm.*/ do { int b; b=(int)(val>>16); l+=b<<frac; val=(val+b)>>b; val=(val*val+0x7FFF)>>15; } while(frac-->0); /*If val is not exactly 0x8000, then we have to round up the remainder.*/ return l+(val>0x8000); }
ec_uint32 ec_dec_uint(ec_dec *_this,ec_uint32 _ft){ ec_uint32 t; unsigned ft; unsigned s; int ftb; t=0; /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/ celt_assert(_ft>1); _ft--; ftb=EC_ILOG(_ft); if(ftb>EC_UNIT_BITS){ ftb-=EC_UNIT_BITS; ft=(unsigned)(_ft>>ftb)+1; s=ec_decode(_this,ft); ec_dec_update(_this,s,s+1,ft); t=t<<EC_UNIT_BITS|s; t = t<<ftb|ec_dec_bits(_this,ftb); if (t>_ft) { celt_notify("uint decode error"); t = _ft; } return t; } else {
int _celt_autocorr( const opus_val16 *x, /* in: [0...n-1] samples x */ opus_val32 *ac, /* out: [0...lag-1] ac values */ const opus_val16 *window, int overlap, int lag, int n, int arch ) { opus_val32 d; int i, k; int fastN=n-lag; int shift; const opus_val16 *xptr; VARDECL(opus_val16, xx); SAVE_STACK; ALLOC(xx, n, opus_val16); celt_assert(n>0); celt_assert(overlap>=0); if (overlap == 0) { xptr = x; } else { for (i=0;i<n;i++) xx[i] = x[i]; for (i=0;i<overlap;i++) { xx[i] = MULT16_16_Q15(x[i],window[i]); xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]); } xptr = xx; } shift=0; #ifdef OPUS_FIXED_POINT { opus_val32 ac0; ac0 = 1+(n<<7); if (n&1) ac0 += SHR32(MULT16_16(xptr[0],xptr[0]),9); for(i=(n&1);i<n;i+=2) { ac0 += SHR32(MULT16_16(xptr[i],xptr[i]),9); ac0 += SHR32(MULT16_16(xptr[i+1],xptr[i+1]),9); } shift = celt_ilog2(ac0)-30+10; shift = (shift)/2; if (shift>0) { for(i=0;i<n;i++) xx[i] = PSHR32(xptr[i], shift); xptr = xx; } else shift = 0; } #endif celt_pitch_xcorr(xptr, xptr, ac, fastN, lag+1, arch); for (k=0;k<=lag;k++) { for (i = k+fastN, d = 0; i < n; i++) d = MAC16_16(d, xptr[i], xptr[i-k]); ac[k] += d; } #ifdef OPUS_FIXED_POINT shift = 2*shift; if (shift<=0) ac[0] += SHL32((opus_int32)1, -shift); if (ac[0] < 268435456) { int shift2 = 29 - EC_ILOG(ac[0]); for (i=0;i<=lag;i++) ac[i] = SHL32(ac[i], shift2); shift -= shift2; } else if (ac[0] >= 536870912) { int shift2=1; if (ac[0] >= 1073741824) shift2++; for (i=0;i<=lag;i++) ac[i] = SHR32(ac[i], shift2); shift += shift2; } #endif RESTORE_STACK; return shift; }