comparison lpc.c @ 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 5e65094bbc23
children 9f929379bbca
comparison
equal deleted inserted replaced
7787:681a05d9b04f 7788:ffd4b1364b62
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22 #include "libavutil/lls.h" 22 #include "libavutil/lls.h"
23 #include "dsputil.h" 23 #include "dsputil.h"
24
25 #define LPC_USE_DOUBLE
24 #include "lpc.h" 26 #include "lpc.h"
25 27
26
27 /**
28 * Levinson-Durbin recursion.
29 * Produces LPC coefficients from autocorrelation data.
30 */
31 static void compute_lpc_coefs(const double *autoc, int max_order,
32 double lpc[][MAX_LPC_ORDER], double *ref)
33 {
34 int i, j;
35 double err = autoc[0];
36 double lpc_tmp[MAX_LPC_ORDER];
37
38 for(i=0; i<max_order; i++) {
39 double r = -autoc[i+1];
40
41 for(j=0; j<i; j++)
42 r -= lpc_tmp[j] * autoc[i-j];
43
44 r /= err;
45 ref[i] = fabs(r);
46
47 err *= 1.0 - (r * r);
48
49 lpc_tmp[i] = r;
50 for(j=0; j < i>>1; j++) {
51 double tmp = lpc_tmp[j];
52 lpc_tmp[j] += r * lpc_tmp[i-1-j];
53 lpc_tmp[i-1-j] += r * tmp;
54 }
55
56 if(i & 1)
57 lpc_tmp[j] += lpc_tmp[j] * r;
58
59 for(j=0; j<=i; j++)
60 lpc[i][j] = -lpc_tmp[j];
61 }
62 }
63 28
64 /** 29 /**
65 * Quantize LPC coefficients 30 * Quantize LPC coefficients
66 */ 31 */
67 static void quantize_lpc_coefs(double *lpc_in, int order, int precision, 32 static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
104 } 69 }
105 70
106 /* output quantized coefficients and level shift */ 71 /* output quantized coefficients and level shift */
107 error=0; 72 error=0;
108 for(i=0; i<order; i++) { 73 for(i=0; i<order; i++) {
109 error += lpc_in[i] * (1 << sh); 74 error -= lpc_in[i] * (1 << sh);
110 lpc_out[i] = av_clip(lrintf(error), -qmax, qmax); 75 lpc_out[i] = av_clip(lrintf(error), -qmax, qmax);
111 error -= lpc_out[i]; 76 error -= lpc_out[i];
112 } 77 }
113 *shift = sh; 78 *shift = sh;
114 } 79 }
145 assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER); 110 assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER);
146 111
147 if(use_lpc == 1){ 112 if(use_lpc == 1){
148 s->flac_compute_autocorr(samples, blocksize, max_order, autoc); 113 s->flac_compute_autocorr(samples, blocksize, max_order, autoc);
149 114
150 compute_lpc_coefs(autoc, max_order, lpc, ref); 115 compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1);
116
117 for(i=0; i<max_order; i++)
118 ref[i] = fabs(lpc[i][i]);
151 }else{ 119 }else{
152 LLSModel m[2]; 120 LLSModel m[2];
153 double var[MAX_LPC_ORDER+1], weight; 121 double var[MAX_LPC_ORDER+1], weight;
154 122
155 for(pass=0; pass<use_lpc-1; pass++){ 123 for(pass=0; pass<use_lpc-1; pass++){
177 av_solve_lls(&m[pass&1], 0.001, 0); 145 av_solve_lls(&m[pass&1], 0.001, 0);
178 } 146 }
179 147
180 for(i=0; i<max_order; i++){ 148 for(i=0; i<max_order; i++){
181 for(j=0; j<max_order; j++) 149 for(j=0; j<max_order; j++)
182 lpc[i][j]= m[(pass-1)&1].coeff[i][j]; 150 lpc[i][j]=-m[(pass-1)&1].coeff[i][j];
183 ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000; 151 ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
184 } 152 }
185 for(i=max_order-1; i>0; i--) 153 for(i=max_order-1; i>0; i--)
186 ref[i] = ref[i-1] - ref[i]; 154 ref[i] = ref[i-1] - ref[i];
187 } 155 }