diff options
author | Vitor Sessak <vitor1001@gmail.com> | 2008-09-04 11:03:14 +0000 |
---|---|---|
committer | Vitor Sessak <vitor1001@gmail.com> | 2008-09-04 11:03:14 +0000 |
commit | 1be0fc2909fbcefd800d20cfff1420234fd0715b (patch) | |
tree | 5e947eaeb7845f55f3c6f09b81e67393d8fb74be | |
parent | 287ba997b5a4df061a0f6efb1cc7e2ae67831f7e (diff) | |
download | ffmpeg-1be0fc2909fbcefd800d20cfff1420234fd0715b.tar.gz |
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
-rw-r--r-- | libavcodec/aac.c | 19 | ||||
-rw-r--r-- | libavcodec/lpc.c | 48 | ||||
-rw-r--r-- | libavcodec/lpc.h | 54 | ||||
-rw-r--r-- | libavcodec/ra288.c | 43 |
4 files changed, 69 insertions, 95 deletions
diff --git a/libavcodec/aac.c b/libavcodec/aac.c index a75a7cefdd..94f27acda1 100644 --- a/libavcodec/aac.c +++ b/libavcodec/aac.c @@ -79,6 +79,7 @@ #include "avcodec.h" #include "bitstream.h" #include "dsputil.h" +#include "lpc.h" #include "aac.h" #include "aactab.h" @@ -634,7 +635,7 @@ static int decode_tns(AACContext * ac, TemporalNoiseShaping * tns, tmp2_idx = 2*coef_compress + coef_res; 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) continue; - /* tns_decode_coef - * FIXME: This duplicates the functionality of some double code in lpc.c. - */ - 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]; - } + // tns_decode_coef + compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0); start = ics->swb_offset[FFMIN(bottom, mmm)]; end = ics->swb_offset[FFMIN( top, mmm)]; diff --git a/libavcodec/lpc.c b/libavcodec/lpc.c index 253267c5aa..dd145ca875 100644 --- a/libavcodec/lpc.c +++ b/libavcodec/lpc.c @@ -21,45 +21,10 @@ #include "libavutil/lls.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; - ref[i] = fabs(r); - - 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; +#define LPC_USE_DOUBLE +#include "lpc.h" - for(j=0; j<=i; j++) - lpc[i][j] = -lpc_tmp[j]; - } -} /** * 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 */ error=0; 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); error -= lpc_out[i]; } @@ -147,7 +112,10 @@ int ff_lpc_calc_coefs(DSPContext *s, if(use_lpc == 1){ 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{ LLSModel m[2]; 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(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; } for(i=max_order-1; i>0; i--) diff --git a/libavcodec/lpc.h b/libavcodec/lpc.h index 96f9f856f2..9b584c8ee2 100644 --- a/libavcodec/lpc.h +++ b/libavcodec/lpc.h @@ -45,4 +45,58 @@ int ff_lpc_calc_coefs(DSPContext *s, int32_t coefs[][MAX_LPC_ORDER], int *shift, int use_lpc, 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 */ diff --git a/libavcodec/ra288.c b/libavcodec/ra288.c index 353ae529a6..fc683663a7 100644 --- a/libavcodec/ra288.c +++ b/libavcodec/ra288.c @@ -23,6 +23,7 @@ #define ALT_BITSTREAM_READER_LE #include "bitstream.h" #include "ra288.h" +#include "lpc.h" typedef struct { 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); } -/** - * 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) { 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, 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); do_hybrid_window(10, 8, 20, ractx->gain_block+2, temp2, ractx->gain_hist, 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); } |