/* Copyright (c) 2007-2008 CSIRO Copyright (c) 2007-2009 Xiph.Org Foundation Written by Jean-Marc Valin */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "mathops.h" #include "cwrs.h" #include "vq.h" #include "arch.h" #include "os_support.h" #include "bands.h" #include "rate.h" #include "pitch.h" #include "SigProc_FIX.h" #if defined(FIXED_POINT) void norm_scaleup(celt_norm *X, int N, int shift) { int i; celt_assert(shift >= 0); if (shift <= 0) return; for (i=0;i= 0); if (shift <= 0) return; for (i=0;i>2*(NORM_SHIFT-14); } #endif #ifndef OVERRIDE_vq_exp_rotation1 static void exp_rotation1(celt_norm *X, int len, int stride, opus_val16 c, opus_val16 s) { int i; opus_val16 ms; celt_norm *Xptr; Xptr = X; ms = NEG16(s); norm_scaledown(X, len, NORM_SHIFT-14); for (i=0;i=0;i--) { celt_norm x1, x2; x1 = Xptr[0]; x2 = Xptr[stride]; Xptr[stride] = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x2), s, x1), 15)); *Xptr-- = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x1), ms, x2), 15)); } norm_scaleup(X, len, NORM_SHIFT-14); } #endif /* OVERRIDE_vq_exp_rotation1 */ void exp_rotation(celt_norm *X, int len, int dir, int stride, int K, int spread) { static const int SPREAD_FACTOR[3]={15,10,5}; int i; opus_val16 c, s; opus_val16 gain, theta; int stride2=0; int factor; if (2*K>=len || spread==SPREAD_NONE) return; factor = SPREAD_FACTOR[spread-1]; gain = celt_div((opus_val32)MULT16_16(Q15_ONE,len),(opus_val32)(len+factor*K)); theta = HALF16(MULT16_16_Q15(gain,gain)); c = celt_cos_norm(EXTEND32(theta)); s = celt_cos_norm(EXTEND32(SUB16(Q15ONE,theta))); /* sin(theta) */ if (len>=8*stride) { stride2 = 1; /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding. It's basically incrementing long as (stride2+0.5)^2 < len/stride. */ while ((stride2*stride2+stride2)*stride + (stride>>2) < len) stride2++; } /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for extract_collapse_mask().*/ len = celt_udiv(len, stride); for (i=0;i>1; #endif t = VSHR32(Ryy, 2*(k-7)-15); g = MULT32_32_Q31(celt_rsqrt_norm32(t),gain); i=0; (void)shift; #if defined(FIXED_POINT) && defined(ENABLE_QEXT) if (shift>0) { int tot_shift = NORM_SHIFT+1-k-shift; if (tot_shift >= 0) { do X[i] = MULT32_32_Q31(g, SHL32(iy[i], tot_shift)); while (++i < N); } else { do X[i] = MULT32_32_Q31(g, PSHR32(iy[i], -tot_shift)); while (++i < N); } } else #endif do X[i] = VSHR32(MULT16_32_Q15(iy[i], g), k+15-NORM_SHIFT); while (++i < N); } static unsigned extract_collapse_mask(int *iy, int N, int B) { unsigned collapse_mask; int N0; int i; if (B<=1) return 1; /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for exp_rotation().*/ N0 = celt_udiv(N, B); collapse_mask = 0; i=0; do { int j; unsigned tmp=0; j=0; do { tmp |= iy[i*N0+j]; } while (++j (N>>1)) { opus_val16 rcp; j=0; do { sum += X[j]; } while (++j EPSILON && sum < 64)) #endif { X[0] = QCONST16(1.f,14); j=1; do X[j]=0; while (++j=0); /* This should never happen, but just in case it does (e.g. on silence) we fill the first bin with pulses. */ #ifdef FIXED_POINT_DEBUG celt_sig_assert(pulsesLeft<=N+3); #endif if (pulsesLeft > N+3) { opus_val16 tmp = (opus_val16)pulsesLeft; yy = MAC16_16(yy, tmp, tmp); yy = MAC16_16(yy, tmp, y[0]); iy[0] += pulsesLeft; pulsesLeft=0; } for (i=0;i= best_num/best_den, but that way we can do it without any division */ /* OPT: It's not clear whether a cmov is faster than a branch here since the condition is more often false than true and using a cmov introduces data dependencies across iterations. The optimal choice may be architecture-dependent. */ if (opus_unlikely(MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))) { best_den = Ryy; best_num = Rxy; best_id = j; } } while (++j>2*shift; #else (void)shift; return K*(float)K*up*up; #endif } #ifdef FIXED_POINT int sum_shift; opus_val32 X0; sum_shift = 30-celt_ilog2(sum); rcp_sum = celt_rcp_norm32(SHL32(sum, sum_shift)); X0 = MULT32_32_Q31(SHL32(X[0], sum_shift), rcp_sum); iy[0] = PSHR32(MULT32_32_Q31(SHL32(K, 8), X0), 7); up_iy[0] = PSHR32(MULT32_32_Q31(SHL32(up*K, 8), X0), 7); #else rcp_sum = 1.f/sum; iy[0] = (int)floor(.5f+K*X[0]*rcp_sum); up_iy[0] = (int)floor(.5f+up*K*X[0]*rcp_sum); #endif up_iy[0] = IMAX(up*iy[0] - (up-1)/2, IMIN(up*iy[0] + (up-1)/2, up_iy[0])); offset = up_iy[0] - up*iy[0]; iy[1] = K-abs(iy[0]); up_iy[1] = up*K-abs(up_iy[0]); if (X[1] < 0) { iy[1] = -iy[1]; up_iy[1] = -up_iy[1]; offset = -offset; } *refine = offset; #ifdef FIXED_POINT return (up_iy[0]*(opus_val64)up_iy[0] + up_iy[1]*(opus_val64)up_iy[1] + (1<<2*shift>>1))>>2*shift; #else return up_iy[0]*(opus_val64)up_iy[0] + up_iy[1]*(opus_val64)up_iy[1]; #endif } static int op_pvq_refine(const opus_val32 *Xn, int *iy, int *iy0, int K, int up, int margin, int N) { int i; int dir; VARDECL(opus_val32, rounding); int iysum = 0; SAVE_STACK; ALLOC(rounding, N, opus_val32); for (i=0;i> 7; #else iy[i] = (int)floor(.5+tmp); #endif rounding[i] = tmp - SHL32(iy[i], 7); } if (iy != iy0) { for (i=0;i 32) { RESTORE_STACK; return 1; } dir = iysum < K ? 1 : -1; while (iysum != K) { opus_val32 roundval=-1000000*dir; int roundpos=0; for (i=0;i 0 && abs(iy[i]-up*iy0[i]) < (margin-1) && !(dir==-1 && iy[i] == 0)) { roundval = rounding[i]; roundpos = i; } } iy[roundpos] += dir; rounding[roundpos] -= SHL32(dir, 15); iysum+=dir; } RESTORE_STACK; return 0; } static opus_val32 op_pvq_search_extra(const celt_norm *X, int *iy, int *up_iy, int K, int up, int *refine, int N, int shift) { opus_val32 rcp_sum; opus_val32 sum=0; int i; int failed=0; opus_val64 yy=0; VARDECL(opus_val32, Xn); SAVE_STACK; for (i=0;i>1))>>2*shift; #else (void)shift; return yy; #endif } #endif #ifdef ENABLE_QEXT /* Take advantage of the fact that "large" refine values are much less likely than smaller ones. */ static void ec_enc_refine(ec_enc *enc, opus_int32 refine, opus_int32 up, int extra_bits, int use_entropy) { int large; large = abs(refine)>up/2; ec_enc_bit_logp(enc, large, use_entropy ? 3 : 1); if (large) { ec_enc_bits(enc, refine < 0, 1); ec_enc_bits(enc, abs(refine)-up/2-1, extra_bits-1); } else { ec_enc_bits(enc, refine+up/2, extra_bits); } } static int ec_dec_refine(ec_enc *dec, opus_int32 up, int extra_bits, int use_entropy) { int large, refine; large = ec_dec_bit_logp(dec, use_entropy ? 3 : 1); if (large) { int sign = ec_dec_bits(dec, 1); refine = ec_dec_bits(dec, extra_bits-1) + up/2+1; if (sign) refine = -refine; } else { refine = (opus_int32)ec_dec_bits(dec, extra_bits)-up/2; } return refine; } #endif unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B, ec_enc *enc, opus_val32 gain, int resynth ARG_QEXT(ec_enc *ext_enc) ARG_QEXT(int extra_bits), int arch) { VARDECL(int, iy); opus_val32 yy; unsigned collapse_mask; #ifdef ENABLE_QEXT int yy_shift = 0; #endif SAVE_STACK; celt_assert2(K>0, "alg_quant() needs at least one pulse"); celt_assert2(N>1, "alg_quant() needs at least two dimensions"); /* Covers vectorization by up to 4. */ ALLOC(iy, N+3, int); exp_rotation(X, N, 1, B, K, spread); #ifdef ENABLE_QEXT if (N==2 && extra_bits >= 2) { int refine; int up_iy[2]; int up; yy_shift = IMAX(0, extra_bits-7); up = (1<= 2) { int i; VARDECL(int, up_iy); VARDECL(int, refine); int up, use_entropy; ALLOC(up_iy, N, int); ALLOC(refine, N, int); yy_shift = IMAX(0, extra_bits-7); up = (1<storage*8 - ec_tell(ext_enc)) > (unsigned)(N-1)*(extra_bits+3)+1; for (i=0;i0, "alg_unquant() needs at least one pulse"); celt_assert2(N>1, "alg_unquant() needs at least two dimensions"); ALLOC(iy, N, int); Ryy = decode_pulses(iy, N, K, dec); #ifdef ENABLE_QEXT if (N==2 && extra_bits >= 2) { int up; int refine; yy_shift = IMAX(0, extra_bits-7); up = (1< 0) ? -refine : refine; iy[0] += (refine*(opus_int64)iy[0] > 0) ? -refine : refine; } else if (iy[1] > 0) { iy[0] += refine; iy[1] -= refine*(iy[0]>0?1:-1); } else { iy[0] -= refine; iy[1] -= refine*(iy[0]>0?1:-1); } #ifdef FIXED_POINT Ryy = (iy[0]*(opus_val64)iy[0] + iy[1]*(opus_val64)iy[1] + (1<<2*yy_shift>>1)) >> 2*yy_shift; #else Ryy = iy[0]*(opus_val64)iy[0] + iy[1]*(opus_val64)iy[1]; #endif } else if (extra_bits >= 2) { int i; opus_val64 yy64; VARDECL(int, refine); int up, use_entropy; int sign=0; ALLOC(refine, N, int); yy_shift = IMAX(0, extra_bits-7); up = (1<storage*8 - ec_tell(ext_dec)) > (unsigned)(N-1)*(extra_bits+3)+1; for (i=0;i>1)) >> 2*yy_shift; #else Ryy = yy64; #endif } #endif normalise_residual(iy, X, N, Ryy, gain, yy_shift); exp_rotation(X, N, -1, B, K, spread); collapse_mask = extract_collapse_mask(iy, N, B); RESTORE_STACK; return collapse_mask; } #ifndef OVERRIDE_renormalise_vector void renormalise_vector(celt_norm *X, int N, opus_val32 gain, int arch) { int i; #ifdef FIXED_POINT int k; #endif opus_val32 E; opus_val16 g; opus_val32 t; celt_norm *xptr; norm_scaledown(X, N, NORM_SHIFT-14); E = EPSILON + celt_inner_prod_norm(X, X, N, arch); #ifdef FIXED_POINT k = celt_ilog2(E)>>1; #endif t = VSHR32(E, 2*(k-7)); g = MULT32_32_Q31(celt_rsqrt_norm(t),gain); xptr = X; for (i=0;i>1; mag = celt_rsqrt_norm32(SHL32(sum, 2*sum_shift+1)); for (i=0;i faceval) { faceval = ABS32(X[i]); face = i; } } sign = X[face]<0; ec_enc_uint(enc, face, N); ec_enc_bits(enc, sign, 1); #ifdef FIXED_POINT if (faceval != 0) { int face_shift = 30-celt_ilog2(faceval); norm = celt_rcp_norm32(SHL32(faceval, face_shift)); norm = MULT16_32_Q15(K, norm); for (i=0;i> 15); } } else { OPUS_CLEAR(iy, N); } #else norm = .5f*K/(faceval+EPSILON); for (i=0;i