ref: 095c1782c5925b5b37c433b18e438b833fe3f9e7
parent: 92ae37027ffacac07e61d8cafda449b5d835b731
author: Jean-Marc Valin <[email protected]>
date: Thu Sep 17 18:38:34 EDT 2009
Removed code that is no longer necessary with denorm pitch and spreading
--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -260,7 +260,7 @@
g = DIV32(SHL32(SHR32(num,shift),14),SHR32(den,shift));
if (Sxy < MULT16_32_Q15(fact, MULT16_16(celt_sqrt(EPSILON+Sxx),celt_sqrt(EPSILON+Syy))))
g = 0;
- /* This MUST round down */
+ /* This MUST round down so that we don't over-estimate the gain */
*gain_id = EXTRACT16(SHR32(MULT16_16(20,(g-QCONST16(.5,14))),14));
}
#else
@@ -271,6 +271,7 @@
g = Sxy/(.1+Sxx+.03*Syy);
if (Sxy < .5*fact*celt_sqrt(1+Sxx*Syy))
g = 0;
+ /* This MUST round down so that we don't over-estimate the gain */
*gain_id = floor(20*(g-.5));
}
#endif
@@ -301,43 +302,6 @@
}
}
-/* Compute the best gain for each "pitch band" */
-int compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm_t *P, celt_pgain_t *gains)
-{
- int i;
- int gain_sum = 0;
- const celt_int16_t *pBands = m->pBands;
- const int C = CHANNELS(m);
-
- for (i=0;i<m->nbPBands;i++)
- {
- celt_word32_t Sxy=0, Sxx=0;
- int j;
- /* We know we're not going to overflow because Sxx can't be more than 1 (Q28) */
- for (j=C*pBands[i];j<C*pBands[i+1];j++)
- {
- Sxy = MAC16_16(Sxy, X[j], P[j]);
- Sxx = MAC16_16(Sxx, X[j], X[j]);
- }
- Sxy = SHR32(Sxy,2);
- Sxx = SHR32(Sxx,2);
- /* No negative gain allowed */
- if (Sxy < 0)
- Sxy = 0;
- /* Not sure how that would happen, just making sure */
- if (Sxy > Sxx)
- Sxy = Sxx;
- /* We need to be a bit conservative (multiply gain by 0.9), otherwise the
- residual doesn't quantise well */
- Sxy = MULT16_32_Q15(QCONST16(.99f, 15), Sxy);
- /* gain = Sxy/Sxx */
- gains[i] = EXTRACT16(celt_div(Sxy,ADD32(SHR32(Sxx, PGAIN_SHIFT),EPSILON)));
- if (gains[i]>QCONST16(.5,15))
- gain_sum++;
- }
- return gain_sum > 5;
-}
-
#ifndef DISABLE_STEREO
static void stereo_band_mix(const CELTMode *m, celt_norm_t *X, const celt_ener_t *bank, int stereo_mode, int bandID, int dir)
@@ -480,8 +444,7 @@
int i, j, remaining_bits, balance;
const celt_int16_t * restrict eBands = m->eBands;
celt_norm_t * restrict norm;
- VARDECL(celt_norm_t, _norm); const celt_int16_t *pBands = m->pBands;
- int pband=-1;
+ VARDECL(celt_norm_t, _norm);
int B;
SAVE_STACK;
@@ -525,41 +488,12 @@
n = SHL16(celt_sqrt(eBands[i+1]-eBands[i]),11);
- /* If pitch is in use and this eBand begins a pitch band, encode the pitch gain flag */
- if (pitch_used && eBands[i]< m->pitchEnd && eBands[i] == pBands[pband+1])
- {
- int enabled = 1;
- pband++;
- if (remaining_bits >= 1<<BITRES) {
- enabled = pgains[pband] > QCONST16(.5,15);
- ec_enc_bits(enc, enabled, 1);
- balance += 1<<BITRES;
- }
- if (enabled)
- pgains[pband] = QCONST16(.9,15);
- else
- pgains[pband] = 0;
- }
-
- /* If pitch isn't available, use intra-frame prediction */
- if (q==0)
- {
- intra_fold(m, X+eBands[i], eBands[i+1]-eBands[i], norm, P+eBands[i], eBands[i], B);
- } else if (pitch_used && eBands[i] < m->pitchEnd) {
- for (j=eBands[i];j<eBands[i+1];j++)
- P[j] = MULT16_16_Q15(pgains[pband], P[j]);
- } else {
- for (j=eBands[i];j<eBands[i+1];j++)
- P[j] = 0;
- }
-
if (q > 0)
{
int spread = (eBands[i] >= m->pitchEnd && fold) ? B : 0;
- alg_quant(X+eBands[i], W+eBands[i], eBands[i+1]-eBands[i], q, spread, P+eBands[i], enc);
+ alg_quant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, enc);
} else {
- for (j=eBands[i];j<eBands[i+1];j++)
- X[j] = P[j];
+ intra_fold(m, X+eBands[i], eBands[i+1]-eBands[i], norm, X+eBands[i], eBands[i], B);
}
for (j=eBands[i];j<eBands[i+1];j++)
norm[j] = MULT16_16_Q15(n,X[j]);
@@ -576,7 +510,6 @@
celt_norm_t * restrict norm;
VARDECL(celt_norm_t, _norm);
const int C = CHANNELS(m);
- const celt_int16_t *pBands = m->pBands;
int pband=-1;
int B;
celt_word16_t mid, side;
@@ -661,23 +594,6 @@
}
n = SHL16(celt_sqrt((eBands[i+1]-eBands[i])),11);
- /* If pitch is in use and this eBand begins a pitch band, encode the pitch gain flag */
- if (pitch_used && eBands[i]< m->pitchEnd && eBands[i] == pBands[pband+1])
- {
- int enabled = 1;
- pband++;
- if (remaining_bits >= 1<<BITRES) {
- enabled = pgains[pband] > QCONST16(.5,15);
- ec_enc_bits(enc, enabled, 1);
- balance += 1<<BITRES;
- remaining_bits -= 1<<BITRES;
- }
- if (enabled)
- pgains[pband] = QCONST16(.9,15);
- else
- pgains[pband] = 0;
- }
-
if (N==2)
{
int c2;
@@ -692,19 +608,6 @@
c = itheta > 8192 ? 1 : 0;
c2 = 1-c;
- if (eBands[i] >= m->pitchEnd && fold)
- {
- } else if (pitch_used && eBands[i] < m->pitchEnd) {
- stereo_band_mix(m, P, bandE, qb==0, i, 1);
- renormalise_vector(P+C*eBands[i], Q15ONE, N, C);
- renormalise_vector(P+C*eBands[i]+1, Q15ONE, N, C);
- deinterleave(P+C*eBands[i], C*N);
- for (j=C*eBands[i];j<C*eBands[i+1];j++)
- P[j] = MULT16_16_Q15(pgains[pband], P[j]);
- } else {
- for (j=C*eBands[i];j<C*eBands[i+1];j++)
- P[j] = 0;
- }
v[0] = x2[c];
v[1] = x2[c+C];
w[0] = x2[c2];
@@ -723,7 +626,7 @@
if (q1 > 0)
{
int spread = (eBands[i] >= m->pitchEnd && fold) ? B : 0;
- alg_quant(v, W+C*eBands[i], N, q1, spread, P+C*eBands[i]+c*N, enc);
+ alg_quant(v, N, q1, spread, enc);
} else {
v[0] = QCONST16(1.f, 14);
v[1] = 0;
@@ -783,27 +686,17 @@
{
intra_fold(m, X+C*eBands[i], eBands[i+1]-eBands[i], norm, P+C*eBands[i], eBands[i], B);
deinterleave(P+C*eBands[i], C*N);
- } else if (pitch_used && eBands[i] < m->pitchEnd) {
- stereo_band_mix(m, P, bandE, qb==0, i, 1);
- renormalise_vector(P+C*eBands[i], Q15ONE, N, C);
- renormalise_vector(P+C*eBands[i]+1, Q15ONE, N, C);
- deinterleave(P+C*eBands[i], C*N);
- for (j=C*eBands[i];j<C*eBands[i+1];j++)
- P[j] = MULT16_16_Q15(pgains[pband], P[j]);
- } else {
- for (j=C*eBands[i];j<C*eBands[i+1];j++)
- P[j] = 0;
}
deinterleave(X+C*eBands[i], C*N);
if (q1 > 0) {
int spread = (eBands[i] >= m->pitchEnd && fold) ? B : 0;
- alg_quant(X+C*eBands[i], W+C*eBands[i], N, q1, spread, P+C*eBands[i], enc);
+ alg_quant(X+C*eBands[i], N, q1, spread, enc);
} else
for (j=C*eBands[i];j<C*eBands[i]+N;j++)
X[j] = P[j];
if (q2 > 0) {
int spread = (eBands[i] >= m->pitchEnd && fold) ? B : 0;
- alg_quant(X+C*eBands[i]+N, W+C*eBands[i], N, q2, spread, P+C*eBands[i]+N, enc);
+ alg_quant(X+C*eBands[i]+N, N, q2, spread, enc);
} else
for (j=C*eBands[i]+N;j<C*eBands[i+1];j++)
X[j] = 0;
@@ -845,8 +738,6 @@
const celt_int16_t * restrict eBands = m->eBands;
celt_norm_t * restrict norm;
VARDECL(celt_norm_t, _norm);
- const celt_int16_t *pBands = m->pBands;
- int pband=-1;
int B;
SAVE_STACK;
@@ -890,40 +781,12 @@
n = SHL16(celt_sqrt(eBands[i+1]-eBands[i]),11);
- /* If pitch is in use and this eBand begins a pitch band, encode the pitch gain flag */
- if (pitch_used && eBands[i] < m->pitchEnd && eBands[i] == pBands[pband+1])
- {
- int enabled = 1;
- pband++;
- if (remaining_bits >= 1<<BITRES) {
- enabled = ec_dec_bits(dec, 1);
- balance += 1<<BITRES;
- }
- if (enabled)
- pgains[pband] = QCONST16(.9,15);
- else
- pgains[pband] = 0;
- }
-
- /* If pitch isn't available, use intra-frame prediction */
- if (q==0)
- {
- intra_fold(m, X+eBands[i], eBands[i+1]-eBands[i], norm, P+eBands[i], eBands[i], B);
- } else if (pitch_used && eBands[i] < m->pitchEnd) {
- for (j=eBands[i];j<eBands[i+1];j++)
- P[j] = MULT16_16_Q15(pgains[pband], P[j]);
- } else {
- for (j=eBands[i];j<eBands[i+1];j++)
- P[j] = 0;
- }
-
if (q > 0)
{
int spread = (eBands[i] >= m->pitchEnd && fold) ? B : 0;
- alg_unquant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, P+eBands[i], dec);
+ alg_unquant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, dec);
} else {
- for (j=eBands[i];j<eBands[i+1];j++)
- X[j] = P[j];
+ intra_fold(m, X+eBands[i], eBands[i+1]-eBands[i], norm, X+eBands[i], eBands[i], B);
}
for (j=eBands[i];j<eBands[i+1];j++)
norm[j] = MULT16_16_Q15(n,X[j]);
@@ -940,7 +803,6 @@
celt_norm_t * restrict norm;
VARDECL(celt_norm_t, _norm);
const int C = CHANNELS(m);
- const celt_int16_t *pBands = m->pBands;
int pband=-1;
int B;
celt_word16_t mid, side;
@@ -1014,22 +876,6 @@
}
n = SHL16(celt_sqrt((eBands[i+1]-eBands[i])),11);
- /* If pitch is in use and this eBand begins a pitch band, encode the pitch gain flag */
- if (pitch_used && eBands[i]< m->pitchEnd && eBands[i] == pBands[pband+1])
- {
- int enabled = 1;
- pband++;
- if (remaining_bits >= 1<<BITRES) {
- enabled = ec_dec_bits(dec, 1);
- balance += 1<<BITRES;
- remaining_bits -= 1<<BITRES;
- }
- if (enabled)
- pgains[pband] = QCONST16(.9,15);
- else
- pgains[pband] = 0;
- }
-
if (N==2)
{
int c2;
@@ -1044,19 +890,6 @@
c = itheta > 8192 ? 1 : 0;
c2 = 1-c;
- if (eBands[i] >= m->pitchEnd && fold)
- {
- } else if (pitch_used && eBands[i] < m->pitchEnd) {
- stereo_band_mix(m, P, bandE, qb==0, i, 1);
- renormalise_vector(P+C*eBands[i], Q15ONE, N, C);
- renormalise_vector(P+C*eBands[i]+1, Q15ONE, N, C);
- deinterleave(P+C*eBands[i], C*N);
- for (j=C*eBands[i];j<C*eBands[i+1];j++)
- P[j] = MULT16_16_Q15(pgains[pband], P[j]);
- } else {
- for (j=C*eBands[i];j<C*eBands[i+1];j++)
- P[j] = 0;
- }
v[0] = x2[c];
v[1] = x2[c+C];
w[0] = x2[c2];
@@ -1075,7 +908,7 @@
if (q1 > 0)
{
int spread = (eBands[i] >= m->pitchEnd && fold) ? B : 0;
- alg_unquant(v, N, q1, spread, P+C*eBands[i]+c*N, dec);
+ alg_unquant(v, N, q1, spread, dec);
} else {
v[0] = QCONST16(1.f, 14);
v[1] = 0;
@@ -1130,22 +963,12 @@
{
intra_fold(m, X+C*eBands[i], eBands[i+1]-eBands[i], norm, P+C*eBands[i], eBands[i], B);
deinterleave(P+C*eBands[i], C*N);
- } else if (pitch_used && eBands[i] < m->pitchEnd) {
- stereo_band_mix(m, P, bandE, qb==0, i, 1);
- renormalise_vector(P+C*eBands[i], Q15ONE, N, C);
- renormalise_vector(P+C*eBands[i]+1, Q15ONE, N, C);
- deinterleave(P+C*eBands[i], C*N);
- for (j=C*eBands[i];j<C*eBands[i+1];j++)
- P[j] = MULT16_16_Q15(pgains[pband], P[j]);
- } else {
- for (j=C*eBands[i];j<C*eBands[i+1];j++)
- P[j] = 0;
}
deinterleave(X+C*eBands[i], C*N);
if (q1 > 0)
{
int spread = (eBands[i] >= m->pitchEnd && fold) ? B : 0;
- alg_unquant(X+C*eBands[i], N, q1, spread, P+C*eBands[i], dec);
+ alg_unquant(X+C*eBands[i], N, q1, spread, dec);
} else
for (j=C*eBands[i];j<C*eBands[i]+N;j++)
X[j] = P[j];
@@ -1152,7 +975,7 @@
if (q2 > 0)
{
int spread = (eBands[i] >= m->pitchEnd && fold) ? B : 0;
- alg_unquant(X+C*eBands[i]+N, N, q2, spread, P+C*eBands[i]+N, dec);
+ alg_unquant(X+C*eBands[i]+N, N, q2, spread, dec);
} else
for (j=C*eBands[i]+N;j<C*eBands[i+1];j++)
X[j] = 0;
--- a/libcelt/vq.c
+++ b/libcelt/vq.c
@@ -95,11 +95,10 @@
/** 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 mix_pitch_and_residual(int * restrict iy, celt_norm_t * restrict X, int N, int K, const celt_norm_t * restrict P)
+static void mix_pitch_and_residual(int * restrict iy, celt_norm_t * restrict X, int N, int K)
{
int i;
- celt_word32_t Ryp, Ryy, Rpp;
- celt_word16_t ryp, ryy, rpp;
+ celt_word32_t Ryy;
celt_word32_t g;
VARDECL(celt_norm_t, y);
#ifdef FIXED_POINT
@@ -111,32 +110,18 @@
#endif
ALLOC(y, N, celt_norm_t);
- Rpp = 0;
i=0;
- do {
- Rpp = MAC16_16(Rpp,P[i],P[i]);
- y[i] = SHL16(iy[i],yshift);
- } while (++i < N);
-
- Ryp = 0;
Ryy = 0;
- /* If this doesn't generate a dual MAC (on supported archs), fire the compiler guy */
- i=0;
do {
- Ryp = MAC16_16(Ryp, y[i], P[i]);
+ y[i] = SHL16(iy[i],yshift);
Ryy = MAC16_16(Ryy, y[i], y[i]);
} while (++i < N);
- ryp = ROUND16(Ryp,14);
- ryy = ROUND16(Ryy,14);
- rpp = ROUND16(Rpp,14);
- /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */
- g = MULT16_32_Q15(celt_sqrt(MAC16_16(Ryy, ryp,ryp) - MULT16_16(ryy,rpp)) - ryp,
- celt_rcp(SHR32(Ryy,9)));
+ g = MULT16_32_Q15(celt_sqrt(Ryy), celt_rcp(SHR32(Ryy,9)));
i=0;
do
- X[i] = ADD16(P[i], ROUND16(MULT16_16(y[i], g),11));
+ X[i] = ROUND16(MULT16_16(y[i], g),11);
while (++i < N);
RESTORE_STACK;
@@ -143,7 +128,7 @@
}
-void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, int spread, celt_norm_t *P, ec_enc *enc)
+void alg_quant(celt_norm_t *X, int N, int K, int spread, ec_enc *enc)
{
VARDECL(celt_norm_t, y);
VARDECL(int, iy);
@@ -152,8 +137,7 @@
celt_word16_t s;
int pulsesLeft;
celt_word32_t sum;
- celt_word32_t xy, yy, yp;
- celt_word16_t Rpp;
+ celt_word32_t xy, yy;
int N_1; /* Inverse of N, in Q14 format (even for float) */
#ifdef FIXED_POINT
int yshift;
@@ -175,23 +159,19 @@
sum = 0;
j=0; do {
- X[j] -= P[j];
if (X[j]>0)
signx[j]=1;
else {
signx[j]=-1;
X[j]=-X[j];
- P[j]=-P[j];
}
iy[j] = 0;
y[j] = 0;
- sum = MAC16_16(sum, P[j],P[j]);
} while (++j<N);
- Rpp = ROUND16(sum, NORM_SHIFT);
celt_assert2(Rpp<=NORM_SCALING, "Rpp should never have a norm greater than unity");
- xy = yy = yp = 0;
+ xy = yy = 0;
pulsesLeft = K;
@@ -228,7 +208,6 @@
y[j] = SHL16(iy[j],yshift);
yy = MAC16_16(yy, y[j],y[j]);
xy = MAC16_16(xy, X[j],y[j]);
- yp += P[j]*y[j];
y[j] *= 2;
pulsesLeft -= iy[j];
} while (++j<N);
@@ -235,7 +214,7 @@
}
celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
- while (pulsesLeft > 1)
+ while (pulsesLeft > 0)
{
int pulsesAtOnce=1;
int best_id;
@@ -292,7 +271,6 @@
xy = xy + MULT16_16(s,X[j]);
/* We're multiplying y[j] by two so we don't have to do it here */
yy = yy + MULT16_16(s,y[j]);
- yp = yp + MULT16_16(s, P[j]);
/* 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 */
@@ -300,54 +278,8 @@
iy[j] += is;
pulsesLeft -= pulsesAtOnce;
}
-
- if (pulsesLeft > 0)
- {
- celt_word16_t g;
- celt_word16_t best_num = -VERY_LARGE16;
- celt_word16_t best_den = 0;
- int best_id = 0;
- celt_word16_t magnitude = SHL16(1, yshift);
-
- /* The squared magnitude term gets added anyway, so we might as well
- add it outside the loop */
- yy = MAC16_16(yy, magnitude,magnitude);
- j=0;
- do {
- celt_word16_t Rxy, Ryy, Ryp;
- celt_word16_t num;
- /* Select sign based on X[j] alone */
- s = magnitude;
- /* Temporary sums of the new pulse(s) */
- Rxy = ROUND16(MAC16_16(xy, s,X[j]), 14);
- /* We're multiplying y[j] by two so we don't have to do it here */
- Ryy = ROUND16(MAC16_16(yy, s,y[j]), 14);
- Ryp = ROUND16(MAC16_16(yp, s,P[j]), 14);
-
- /* Compute the gain such that ||p + g*y|| = 1
- ...but instead, we compute g*Ryy to avoid dividing */
- g = celt_psqrt(MULT16_16(Ryp,Ryp) + MULT16_16(Ryy,QCONST16(1.f,14)-Rpp)) - Ryp;
- /* Knowing that gain, what's the error: (x-g*y)^2
- (result is negated and we discard x^2 because it's constant) */
- /* score = 2*g*Rxy - g*g*Ryy;*/
-#ifdef FIXED_POINT
- /* No need to multiply Rxy by 2 because we did it earlier */
- num = MULT16_16_Q15(ADD16(SUB16(Rxy,g),Rxy),g);
-#else
- num = g*(2*Rxy-g);
-#endif
- if (MULT16_16(best_den, num) > MULT16_16(Ryy, best_num))
- {
- best_den = Ryy;
- best_num = num;
- best_id = j;
- }
- } while (++j<N);
- iy[best_id] += 1;
- }
j=0;
do {
- P[j] = MULT16_16(signx[j],P[j]);
X[j] = MULT16_16(signx[j],X[j]);
if (signx[j] < 0)
iy[j] = -iy[j];
@@ -356,7 +288,7 @@
/* Recompute the gain in one pass to reduce the encoder-decoder mismatch
due to the recursive computation used in quantisation. */
- mix_pitch_and_residual(iy, X, N, K, P);
+ mix_pitch_and_residual(iy, X, N, K);
if (spread)
exp_rotation(X, N, -1, spread, K);
RESTORE_STACK;
@@ -365,7 +297,7 @@
/** 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_t *X, int N, int K, int spread, celt_norm_t *P, ec_dec *dec)
+void alg_unquant(celt_norm_t *X, int N, int K, int spread, ec_dec *dec)
{
VARDECL(int, iy);
SAVE_STACK;
@@ -372,7 +304,7 @@
K = get_pulses(K);
ALLOC(iy, N, int);
decode_pulses(iy, N, K, dec);
- mix_pitch_and_residual(iy, X, N, K, P);
+ mix_pitch_and_residual(iy, X, N, K);
if (spread)
exp_rotation(X, N, -1, spread, K);
RESTORE_STACK;
--- a/libcelt/vq.h
+++ b/libcelt/vq.h
@@ -50,7 +50,7 @@
* @param p Pitch vector (it is assumed that p+x is a unit vector)
* @param enc Entropy encoder state
*/
-void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, int spread, celt_norm_t *P, ec_enc *enc);
+void alg_quant(celt_norm_t *X, int N, int K, int spread, ec_enc *enc);
/** Algebraic pulse decoder
* @param x Decoded normalised spectrum (returned)
@@ -59,7 +59,7 @@
* @param p Pitch vector (automatically added to x)
* @param dec Entropy decoder state
*/
-void alg_unquant(celt_norm_t *X, int N, int K, int spread, celt_norm_t *P, ec_dec *dec);
+void alg_unquant(celt_norm_t *X, int N, int K, int spread, ec_dec *dec);
celt_word16_t renormalise_vector(celt_norm_t *X, celt_word16_t value, int N, int stride);