/* 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" #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); 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)); } } #endif /* OVERRIDE_vq_exp_rotation1 */ static 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)); g = MULT16_16_P15(celt_rsqrt_norm(t),gain); i=0; do X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1)); 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, "Allocated too many pulses in the quick pass"); /* 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_assert2(pulsesLeft<=N+3, "Not enough pulses in the quick pass"); #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 (++j0, "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); yy = op_pvq_search(X, iy, K, N, arch); encode_pulses(iy, N, K, enc); if (resynth) { normalise_residual(iy, X, N, yy, gain); exp_rotation(X, N, -1, B, K, spread); } collapse_mask = extract_collapse_mask(iy, N, B); RESTORE_STACK; return collapse_mask; } /** Decode pulse vector and combine the result with the pitch vector to produce the final normalised signal in the current band. */ unsigned alg_unquant(celt_norm *X, int N, int K, int spread, int B, ec_dec *dec, opus_val16 gain) { opus_val32 Ryy; unsigned collapse_mask; VARDECL(int, iy); SAVE_STACK; celt_assert2(K>0, "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); normalise_residual(iy, X, N, Ryy, gain); 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_val16 gain, int arch) { int i; #ifdef FIXED_POINT int k; #endif opus_val32 E; opus_val16 g; opus_val32 t; celt_norm *xptr; E = EPSILON + celt_inner_prod(X, X, N, arch); #ifdef FIXED_POINT k = celt_ilog2(E)>>1; #endif t = VSHR32(E, 2*(k-7)); g = MULT16_16_P15(celt_rsqrt_norm(t),gain); xptr = X; for (i=0;i