Avoid duplicating compute_lpc_coefs() function in both the RA288 and AAC decoders.

Originally committed as revision 15193 to svn://svn.ffmpeg.org/ffmpeg/trunk
pull/126/head
Vitor Sessak 17 years ago
parent 287ba997b5
commit 1be0fc2909
  1. 19
      libavcodec/aac.c
  2. 48
      libavcodec/lpc.c
  3. 54
      libavcodec/lpc.h
  4. 43
      libavcodec/ra288.c

@ -79,6 +79,7 @@
#include "avcodec.h" #include "avcodec.h"
#include "bitstream.h" #include "bitstream.h"
#include "dsputil.h" #include "dsputil.h"
#include "lpc.h"
#include "aac.h" #include "aac.h"
#include "aactab.h" #include "aactab.h"
@ -634,7 +635,7 @@ static int decode_tns(AACContext * ac, TemporalNoiseShaping * tns,
tmp2_idx = 2*coef_compress + coef_res; tmp2_idx = 2*coef_compress + coef_res;
for (i = 0; i < tns->order[w][filt]; i++) for (i = 0; i < tns->order[w][filt]; i++)
tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)]; tns->coef[w][filt][i] = -tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
} }
} }
} }
@ -1124,20 +1125,8 @@ static void apply_tns(float coef[1024], TemporalNoiseShaping * tns, IndividualCh
if (order == 0) if (order == 0)
continue; continue;
/* tns_decode_coef // tns_decode_coef
* FIXME: This duplicates the functionality of some double code in lpc.c. compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
*/
for (m = 0; m < order; m++) {
float tmp;
lpc[m] = tns->coef[w][filt][m];
for (i = 0; i < m/2; i++) {
tmp = lpc[i];
lpc[i] += lpc[m] * lpc[m-1-i];
lpc[m-1-i] += lpc[m] * tmp;
}
if(m & 1)
lpc[i] += lpc[m] * lpc[i];
}
start = ics->swb_offset[FFMIN(bottom, mmm)]; start = ics->swb_offset[FFMIN(bottom, mmm)];
end = ics->swb_offset[FFMIN( top, mmm)]; end = ics->swb_offset[FFMIN( top, mmm)];

@ -21,45 +21,10 @@
#include "libavutil/lls.h" #include "libavutil/lls.h"
#include "dsputil.h" #include "dsputil.h"
#include "lpc.h"
/**
* Levinson-Durbin recursion.
* Produces LPC coefficients from autocorrelation data.
*/
static void compute_lpc_coefs(const double *autoc, int max_order,
double lpc[][MAX_LPC_ORDER], double *ref)
{
int i, j;
double err = autoc[0];
double lpc_tmp[MAX_LPC_ORDER];
for(i=0; i<max_order; i++) {
double r = -autoc[i+1];
for(j=0; j<i; j++)
r -= lpc_tmp[j] * autoc[i-j];
r /= err; #define LPC_USE_DOUBLE
ref[i] = fabs(r); #include "lpc.h"
err *= 1.0 - (r * r);
lpc_tmp[i] = r;
for(j=0; j < i>>1; j++) {
double tmp = lpc_tmp[j];
lpc_tmp[j] += r * lpc_tmp[i-1-j];
lpc_tmp[i-1-j] += r * tmp;
}
if(i & 1)
lpc_tmp[j] += lpc_tmp[j] * r;
for(j=0; j<=i; j++)
lpc[i][j] = -lpc_tmp[j];
}
}
/** /**
* Quantize LPC coefficients * Quantize LPC coefficients
@ -106,7 +71,7 @@ static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
/* output quantized coefficients and level shift */ /* output quantized coefficients and level shift */
error=0; error=0;
for(i=0; i<order; i++) { for(i=0; i<order; i++) {
error += lpc_in[i] * (1 << sh); error -= lpc_in[i] * (1 << sh);
lpc_out[i] = av_clip(lrintf(error), -qmax, qmax); lpc_out[i] = av_clip(lrintf(error), -qmax, qmax);
error -= lpc_out[i]; error -= lpc_out[i];
} }
@ -147,7 +112,10 @@ int ff_lpc_calc_coefs(DSPContext *s,
if(use_lpc == 1){ if(use_lpc == 1){
s->flac_compute_autocorr(samples, blocksize, max_order, autoc); s->flac_compute_autocorr(samples, blocksize, max_order, autoc);
compute_lpc_coefs(autoc, max_order, lpc, ref); compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1);
for(i=0; i<max_order; i++)
ref[i] = fabs(lpc[i][i]);
}else{ }else{
LLSModel m[2]; LLSModel m[2];
double var[MAX_LPC_ORDER+1], weight; double var[MAX_LPC_ORDER+1], weight;
@ -179,7 +147,7 @@ int ff_lpc_calc_coefs(DSPContext *s,
for(i=0; i<max_order; i++){ for(i=0; i<max_order; i++){
for(j=0; j<max_order; j++) for(j=0; j<max_order; j++)
lpc[i][j]= m[(pass-1)&1].coeff[i][j]; lpc[i][j]=-m[(pass-1)&1].coeff[i][j];
ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000; ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
} }
for(i=max_order-1; i>0; i--) for(i=max_order-1; i>0; i--)

@ -45,4 +45,58 @@ int ff_lpc_calc_coefs(DSPContext *s,
int32_t coefs[][MAX_LPC_ORDER], int *shift, int use_lpc, int32_t coefs[][MAX_LPC_ORDER], int *shift, int use_lpc,
int omethod, int max_shift, int zero_shift); int omethod, int max_shift, int zero_shift);
#ifdef LPC_USE_DOUBLE
#define LPC_type double
#else
#define LPC_type float
#endif
/**
* Levinson-Durbin recursion.
* Produces LPC coefficients from autocorrelation data.
*/
static inline int compute_lpc_coefs(const LPC_type *autoc, int max_order,
LPC_type *lpc, int lpc_stride, int fail,
int normalize)
{
int i, j;
LPC_type err;
LPC_type *lpc_last = lpc;
if (normalize)
err = *autoc++;
if (fail && (autoc[max_order - 1] == 0 || err <= 0))
return -1;
for(i=0; i<max_order; i++) {
LPC_type r = -autoc[i];
if (normalize) {
for(j=0; j<i; j++)
r -= lpc_last[j] * autoc[i-j-1];
r /= err;
err *= 1.0 - (r * r);
}
lpc[i] = r;
for(j=0; j < (i+1)>>1; j++) {
LPC_type f = lpc_last[ j];
LPC_type b = lpc_last[i-1-j];
lpc[ j] = f + r * b;
lpc[i-1-j] = b + r * f;
}
if (fail && err < 0)
return -1;
lpc_last = lpc;
lpc += lpc_stride;
}
return 0;
}
#endif /* AVCODEC_LPC_H */ #endif /* AVCODEC_LPC_H */

@ -23,6 +23,7 @@
#define ALT_BITSTREAM_READER_LE #define ALT_BITSTREAM_READER_LE
#include "bitstream.h" #include "bitstream.h"
#include "ra288.h" #include "ra288.h"
#include "lpc.h"
typedef struct { typedef struct {
float sp_lpc[36]; ///< LPC coefficients for speech data (spec: A) float sp_lpc[36]; ///< LPC coefficients for speech data (spec: A)
@ -113,44 +114,6 @@ static void decode(RA288Context *ractx, float gain, int cb_coef)
block[i] = av_clipf(block[i] + buffer[i], -4095, 4095); block[i] = av_clipf(block[i] + buffer[i], -4095, 4095);
} }
/**
* Converts autocorrelation coefficients to LPC coefficients using the
* Levinson-Durbin algorithm. See blocks 37 and 50 of the G.728 specification.
*
* @return 0 if success, -1 if fail
*/
static int eval_lpc_coeffs(const float *in, float *tgt, int n)
{
int i, j;
double f0, f1, f2;
if (in[n] == 0)
return -1;
if ((f0 = *in) <= 0)
return -1;
in--; // To avoid a -1 subtraction in the inner loop
for (i=1; i <= n; i++) {
f1 = in[i+1];
for (j=0; j < i - 1; j++)
f1 += in[i-j]*tgt[j];
tgt[i-1] = f2 = -f1/f0;
for (j=0; j < i >> 1; j++) {
float temp = tgt[j] + tgt[i-j-2]*f2;
tgt[i-j-2] += tgt[j]*f2;
tgt[j] = temp;
}
if ((f0 += f1*f2) < 0)
return -1;
}
return 0;
}
static void convolve(float *tgt, const float *src, int len, int n) static void convolve(float *tgt, const float *src, int len, int n)
{ {
for (; n >= 0; n--) for (; n >= 0; n--)
@ -210,13 +173,13 @@ static void backward_filter(RA288Context *ractx)
do_hybrid_window(36, 40, 35, ractx->sp_block+1, temp1, ractx->sp_hist, do_hybrid_window(36, 40, 35, ractx->sp_block+1, temp1, ractx->sp_hist,
ractx->sp_rec, syn_window); ractx->sp_rec, syn_window);
if (!eval_lpc_coeffs(temp1, ractx->sp_lpc, 36)) if (!compute_lpc_coefs(temp1, 36, ractx->sp_lpc, 0, 1, 1))
colmult(ractx->sp_lpc, ractx->sp_lpc, syn_bw_tab, 36); colmult(ractx->sp_lpc, ractx->sp_lpc, syn_bw_tab, 36);
do_hybrid_window(10, 8, 20, ractx->gain_block+2, temp2, ractx->gain_hist, do_hybrid_window(10, 8, 20, ractx->gain_block+2, temp2, ractx->gain_hist,
ractx->gain_rec, gain_window); ractx->gain_rec, gain_window);
if (!eval_lpc_coeffs(temp2, ractx->gain_lpc, 10)) if (!compute_lpc_coefs(temp2, 10, ractx->gain_lpc, 0, 1, 1))
colmult(ractx->gain_lpc, ractx->gain_lpc, gain_bw_tab, 10); colmult(ractx->gain_lpc, ractx->gain_lpc, gain_bw_tab, 10);
} }

Loading…
Cancel
Save