notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- - Neither the name of the Xiph.org Foundation nor the names of its
- contributors may be used to endorse or promote products derived from
- this software without specific prior written permission.
-
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
#include "vq.h"
#include "arch.h"
#include "os_support.h"
+#include "bands.h"
#include "rate.h"
#ifndef M_PI
#define M_PI 3.141592653
#endif
-static celt_uint32 lcg_rand(celt_uint32 seed)
-{
- return 1664525 * seed + 1013904223;
-}
-
-static void exp_rotation1(celt_norm *X, int len, int dir, int stride, celt_word16 c, celt_word16 s)
+static void exp_rotation1(celt_norm *X, int len, int stride, opus_val16 c, opus_val16 s)
{
int i;
celt_norm *Xptr;
- if (dir>0)
- s = -s;
Xptr = X;
for (i=0;i<len-stride;i++)
{
}
}
-static void exp_rotation(celt_norm *X, int len, int dir, int stride, int K)
+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;
- celt_word16 c, s;
- celt_word16 gain, theta;
+ opus_val16 c, s;
+ opus_val16 gain, theta;
int stride2=0;
+ int factor;
/*int i;
if (len>=30)
{
X[14] = 1;
K=5;
}*/
- if (2*K>=len)
+ if (2*K>=len || spread==SPREAD_NONE)
return;
- gain = celt_div((celt_word32)MULT16_16(Q15_ONE,len),(celt_word32)(len+10*K));
- /* FIXME: Make that HALF16 instead of HALF32 */
- theta = HALF32(MULT16_16_Q15(gain,gain));
+ 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) */
while ((stride2*stride2+stride2)*stride + (stride>>2) < len)
stride2++;
}
+ /*TODO: We should be passing around log2(B), not B, for both this and for
+ extract_collapse_mask().*/
len /= stride;
for (i=0;i<stride;i++)
{
if (dir < 0)
{
if (stride2)
- exp_rotation1(X+i*len, len, dir, stride2, s, c);
- exp_rotation1(X+i*len, len, dir, 1, c, s);
+ exp_rotation1(X+i*len, len, stride2, s, c);
+ exp_rotation1(X+i*len, len, 1, c, s);
} else {
- exp_rotation1(X+i*len, len, dir, 1, c, s);
+ exp_rotation1(X+i*len, len, 1, c, -s);
if (stride2)
- exp_rotation1(X+i*len, len, dir, stride2, s, c);
+ exp_rotation1(X+i*len, len, stride2, s, -c);
}
}
/*if (len>=30)
/** Takes the pitch vector and the decoded residual vector, computes the gain
that will give ||p+g*y||=1 and mixes the residual with the pitch. */
-static void normalise_residual(int * restrict iy, celt_norm * restrict X, int N, int K, celt_word32 Ryy)
+static void normalise_residual(int * restrict iy, celt_norm * restrict X,
+ int N, opus_val32 Ryy, opus_val16 gain)
{
int i;
#ifdef FIXED_POINT
int k;
#endif
- celt_word32 t;
- celt_word16 g;
+ opus_val32 t;
+ opus_val16 g;
#ifdef FIXED_POINT
k = celt_ilog2(Ryy)>>1;
#endif
t = VSHR32(Ryy, (k-7)<<1);
- g = celt_rsqrt_norm(t);
+ g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
i=0;
do
while (++i < N);
}
-void alg_quant(celt_norm *X, int N, int K, int spread, int B, celt_norm *lowband, int resynth, ec_enc *enc, celt_int32 *seed)
+static unsigned extract_collapse_mask(int *iy, int N, int B)
+{
+ unsigned collapse_mask;
+ int N0;
+ int i;
+ if (B<=1)
+ return 1;
+ /*TODO: We should be passing around log2(B), not B, for both this and for
+ exp_rotation().*/
+ N0 = N/B;
+ collapse_mask = 0;
+ i=0; do {
+ int j;
+ j=0; do {
+ collapse_mask |= (iy[i*N0+j]!=0)<<i;
+ } while (++j<N0);
+ } while (++i<B);
+ return collapse_mask;
+}
+
+unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B,
+ int resynth, ec_enc *enc, opus_val16 gain)
{
VARDECL(celt_norm, y);
VARDECL(int, iy);
- VARDECL(celt_word16, signx);
- int j, is;
- celt_word16 s;
+ VARDECL(opus_val16, signx);
+ int i, j;
+ opus_val16 s;
int pulsesLeft;
- celt_word32 sum;
- celt_word32 xy, yy;
- int N_1; /* Inverse of N, in Q14 format (even for float) */
-#ifdef FIXED_POINT
- int yshift;
-#endif
+ opus_val32 sum;
+ opus_val32 xy;
+ opus_val16 yy;
+ unsigned collapse_mask;
SAVE_STACK;
- if (K==0)
- {
- if (lowband != NULL && resynth)
- {
- for (j=0;j<N;j++)
- X[j] = lowband[j];
- } else {
- /* This is important for encoding the side in stereo mode */
- for (j=0;j<N;j++)
- {
- *seed = lcg_rand(*seed);
- X[j] = (int)(*seed)>>20;
- }
- }
- renormalise_vector(X, Q15ONE, N, 1);
- return;
- }
- K = get_pulses(K);
-#ifdef FIXED_POINT
- yshift = 13-celt_ilog2(K);
-#endif
+ celt_assert2(K!=0, "alg_quant() needs at least one pulse");
ALLOC(y, N, celt_norm);
ALLOC(iy, N, int);
- ALLOC(signx, N, celt_word16);
- N_1 = 512/N;
+ ALLOC(signx, N, opus_val16);
- if (spread)
- exp_rotation(X, N, 1, B, K);
+ exp_rotation(X, N, 1, B, K, spread);
+ /* Get rid of the sign */
sum = 0;
j=0; do {
if (X[j]>0)
/* Do a pre-search by projecting on the pyramid */
if (K > (N>>1))
{
- celt_word16 rcp;
+ opus_val16 rcp;
j=0; do {
sum += X[j];
} while (++j<N);
+ /* If X is too small, just replace it with a pulse at 0 */
#ifdef FIXED_POINT
if (sum <= K)
#else
- if (sum <= EPSILON)
+ /* Prevents infinities and NaNs from causing too many pulses
+ to be allocated. 64 is an approximation of infinity here. */
+ if (!(sum > EPSILON && sum < 64))
#endif
{
X[0] = QCONST16(1.f,14);
/* It's really important to round *towards zero* here */
iy[j] = MULT16_16_Q15(X[j],rcp);
#else
- iy[j] = floor(rcp*X[j]);
+ iy[j] = (int)floor(rcp*X[j]);
#endif
- y[j] = SHL16(iy[j],yshift);
+ y[j] = iy[j];
yy = MAC16_16(yy, y[j],y[j]);
xy = MAC16_16(xy, X[j],y[j]);
y[j] *= 2;
}
celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
- while (pulsesLeft > 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_assert2(pulsesLeft<=N+3, "Not enough pulses in the quick pass");
+#endif
+ if (pulsesLeft > N+3)
+ {
+ opus_val16 tmp = pulsesLeft;
+ yy = MAC16_16(yy, tmp, tmp);
+ yy = MAC16_16(yy, tmp, y[0]);
+ iy[0] += pulsesLeft;
+ pulsesLeft=0;
+ }
+
+ s = 1;
+ for (i=0;i<pulsesLeft;i++)
{
- int pulsesAtOnce=1;
int best_id;
- celt_word16 magnitude;
- celt_word32 best_num = -VERY_LARGE16;
- celt_word16 best_den = 0;
+ opus_val32 best_num = -VERY_LARGE16;
+ opus_val16 best_den = 0;
#ifdef FIXED_POINT
int rshift;
#endif
- /* Decide on how many pulses to find at once */
- pulsesAtOnce = (pulsesLeft*N_1)>>9; /* pulsesLeft/N */
- if (pulsesAtOnce<1)
- pulsesAtOnce = 1;
#ifdef FIXED_POINT
- rshift = yshift+1+celt_ilog2(K-pulsesLeft+pulsesAtOnce);
+ rshift = 1+celt_ilog2(K-pulsesLeft+i+1);
#endif
- magnitude = SHL16(pulsesAtOnce, yshift);
-
best_id = 0;
/* The squared magnitude term gets added anyway, so we might as well
add it outside the loop */
- yy = MAC16_16(yy, magnitude,magnitude);
- /* Choose between fast and accurate strategy depending on where we are in the search */
- /* This should ensure that anything we can process will have a better score */
+ yy = ADD32(yy, 1);
j=0;
do {
- celt_word16 Rxy, Ryy;
- /* Select sign based on X[j] alone */
- s = magnitude;
+ opus_val16 Rxy, Ryy;
/* Temporary sums of the new pulse(s) */
- Rxy = EXTRACT16(SHR32(MAC16_16(xy, s,X[j]),rshift));
+ Rxy = EXTRACT16(SHR32(ADD32(xy, EXTEND32(X[j])),rshift));
/* We're multiplying y[j] by two so we don't have to do it here */
- Ryy = EXTRACT16(SHR32(MAC16_16(yy, s,y[j]),rshift));
-
- /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
- Rxy is positive because the sign is pre-computed) */
+ Ryy = ADD16(yy, y[j]);
+
+ /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
+ Rxy is positive because the sign is pre-computed) */
Rxy = MULT16_16_Q15(Rxy,Rxy);
- /* The idea is to check for num/den >= best_num/best_den, but that way
- we can do it without any division */
+ /* The idea is to check for num/den >= best_num/best_den, but that way
+ we can do it without any division */
/* OPT: Make sure to use conditional moves here */
if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
{
}
} while (++j<N);
- j = best_id;
- is = pulsesAtOnce;
- s = SHL16(is, yshift);
-
/* Updating the sums of the new pulse(s) */
- xy = xy + MULT16_16(s,X[j]);
+ xy = ADD32(xy, EXTEND32(X[best_id]));
/* We're multiplying y[j] by two so we don't have to do it here */
- yy = yy + MULT16_16(s,y[j]);
+ yy = ADD16(yy, y[best_id]);
/* Only now that we've made the final choice, update y/iy */
/* Multiplying y[j] by 2 so we don't have to do it everywhere else */
- y[j] += 2*s;
- iy[j] += is;
- pulsesLeft -= pulsesAtOnce;
+ y[best_id] += 2*s;
+ iy[best_id]++;
}
+
+ /* Put the original sign back */
j=0;
do {
X[j] = MULT16_16(signx[j],X[j]);
} while (++j<N);
encode_pulses(iy, N, K, enc);
- /* Recompute the gain in one pass to reduce the encoder-decoder mismatch
- due to the recursive computation used in quantisation. */
if (resynth)
{
- normalise_residual(iy, X, N, K, EXTRACT16(SHR32(yy,2*yshift)));
- if (spread)
- exp_rotation(X, N, -1, B, K);
+ 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. */
-void alg_unquant(celt_norm *X, int N, int K, int spread, int B, celt_norm *lowband, ec_dec *dec, celt_int32 *seed)
+unsigned alg_unquant(celt_norm *X, int N, int K, int spread, int B,
+ ec_dec *dec, opus_val16 gain)
{
int i;
- celt_word32 Ryy;
+ opus_val32 Ryy;
+ unsigned collapse_mask;
VARDECL(int, iy);
SAVE_STACK;
- if (K==0)
- {
- if (lowband != NULL)
- {
- for (i=0;i<N;i++)
- X[i] = lowband[i];
- } else {
- /* This is important for encoding the side in stereo mode */
- for (i=0;i<N;i++)
- {
- *seed = lcg_rand(*seed);
- X[i] = (int)(*seed)>>20;
- }
- }
- renormalise_vector(X, Q15ONE, N, 1);
- return;
- }
- K = get_pulses(K);
+ celt_assert2(K!=0, "alg_unquant() needs at least one pulse");
ALLOC(iy, N, int);
decode_pulses(iy, N, K, dec);
Ryy = 0;
do {
Ryy = MAC16_16(Ryy, iy[i], iy[i]);
} while (++i < N);
- normalise_residual(iy, X, N, K, Ryy);
- if (spread)
- exp_rotation(X, N, -1, B, K);
+ 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;
}
-celt_word16 renormalise_vector(celt_norm *X, celt_word16 value, int N, int stride)
+void renormalise_vector(celt_norm *X, int N, opus_val16 gain)
{
int i;
#ifdef FIXED_POINT
int k;
#endif
- celt_word32 E = EPSILON;
- celt_word16 g;
- celt_word32 t;
+ opus_val32 E = EPSILON;
+ opus_val16 g;
+ opus_val32 t;
celt_norm *xptr = X;
for (i=0;i<N;i++)
{
E = MAC16_16(E, *xptr, *xptr);
- xptr += stride;
+ xptr++;
}
#ifdef FIXED_POINT
k = celt_ilog2(E)>>1;
#endif
t = VSHR32(E, (k-7)<<1);
- g = MULT16_16_Q15(value, celt_rsqrt_norm(t));
+ g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
xptr = X;
for (i=0;i<N;i++)
{
*xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1));
- xptr += stride;
+ xptr++;
}
- return celt_sqrt(E);
+ /*return celt_sqrt(E);*/
}
+int stereo_itheta(celt_norm *X, celt_norm *Y, int stereo, int N)
+{
+ int i;
+ int itheta;
+ opus_val16 mid, side;
+ opus_val32 Emid, Eside;
+
+ Emid = Eside = EPSILON;
+ if (stereo)
+ {
+ for (i=0;i<N;i++)
+ {
+ celt_norm m, s;
+ m = ADD16(SHR16(X[i],1),SHR16(Y[i],1));
+ s = SUB16(SHR16(X[i],1),SHR16(Y[i],1));
+ Emid = MAC16_16(Emid, m, m);
+ Eside = MAC16_16(Eside, s, s);
+ }
+ } else {
+ for (i=0;i<N;i++)
+ {
+ celt_norm m, s;
+ m = X[i];
+ s = Y[i];
+ Emid = MAC16_16(Emid, m, m);
+ Eside = MAC16_16(Eside, s, s);
+ }
+ }
+ mid = celt_sqrt(Emid);
+ side = celt_sqrt(Eside);
+#ifdef FIXED_POINT
+ /* 0.63662 = 2/pi */
+ itheta = MULT16_16_Q15(QCONST16(0.63662f,15),celt_atan2p(side, mid));
+#else
+ itheta = (int)floor(.5f+16384*0.63662f*atan2(side,mid));
+#endif
+
+ return itheta;
+}