changeset 7788:ffd4b1364b62 libavcodec

Avoid duplicating compute_lpc_coefs() function in both the RA288 and AAC decoders.
author vitor
date Thu, 04 Sep 2008 11:03:14 +0000
parents 681a05d9b04f
children 5c532e05faea
files aac.c lpc.c lpc.h ra288.c
diffstat 4 files changed, 69 insertions(+), 95 deletions(-) [+]
line wrap: on
line diff
--- a/aac.c	Wed Sep 03 19:04:46 2008 +0000
+++ b/aac.c	Thu Sep 04 11:03:14 2008 +0000
@@ -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 @@
                 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 @@
             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)];
--- a/lpc.c	Wed Sep 03 19:04:46 2008 +0000
+++ b/lpc.c	Thu Sep 04 11:03:14 2008 +0000
@@ -21,47 +21,12 @@
 
 #include "libavutil/lls.h"
 #include "dsputil.h"
+
+#define LPC_USE_DOUBLE
 #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;
-
-        for(j=0; j<=i; j++)
-            lpc[i][j] = -lpc_tmp[j];
-    }
-}
-
-/**
  * Quantize LPC coefficients
  */
 static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
@@ -106,7 +71,7 @@
     /* 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 @@
     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 @@
 
         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--)
--- a/lpc.h	Wed Sep 03 19:04:46 2008 +0000
+++ b/lpc.h	Thu Sep 04 11:03:14 2008 +0000
@@ -45,4 +45,58 @@
                       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 */
--- a/ra288.c	Wed Sep 03 19:04:46 2008 +0000
+++ b/ra288.c	Thu Sep 04 11:03:14 2008 +0000
@@ -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 @@
         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 @@
     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);
 }