comparison lpc.c @ 10424:94595d0e617c libavcodec

Move autocorrelation function from flacenc.c to lpc.c. Also rename the corresponding dsputil functions and remove their dependency on the FLAC encoder. Fixes Issue1486.
author jbr
date Sat, 17 Oct 2009 21:00:39 +0000
parents bffe8e6cb10e
children fdafbcef52f5
comparison
equal deleted inserted replaced
10423:2e4967487e59 10424:94595d0e617c
23 #include "dsputil.h" 23 #include "dsputil.h"
24 24
25 #define LPC_USE_DOUBLE 25 #define LPC_USE_DOUBLE
26 #include "lpc.h" 26 #include "lpc.h"
27 27
28
29 /**
30 * Apply Welch window function to audio block
31 */
32 static void apply_welch_window(const int32_t *data, int len, double *w_data)
33 {
34 int i, n2;
35 double w;
36 double c;
37
38 assert(!(len&1)); //the optimization in r11881 does not support odd len
39 //if someone wants odd len extend the change in r11881
40
41 n2 = (len >> 1);
42 c = 2.0 / (len - 1.0);
43
44 w_data+=n2;
45 data+=n2;
46 for(i=0; i<n2; i++) {
47 w = c - n2 + i;
48 w = 1.0 - (w * w);
49 w_data[-i-1] = data[-i-1] * w;
50 w_data[+i ] = data[+i ] * w;
51 }
52 }
53
54 /**
55 * Calculates autocorrelation data from audio samples
56 * A Welch window function is applied before calculation.
57 */
58 void ff_lpc_compute_autocorr(const int32_t *data, int len, int lag,
59 double *autoc)
60 {
61 int i, j;
62 double tmp[len + lag + 1];
63 double *data1= tmp + lag;
64
65 apply_welch_window(data, len, data1);
66
67 for(j=0; j<lag; j++)
68 data1[j-lag]= 0.0;
69 data1[len] = 0.0;
70
71 for(j=0; j<lag; j+=2){
72 double sum0 = 1.0, sum1 = 1.0;
73 for(i=j; i<len; i++){
74 sum0 += data1[i] * data1[i-j];
75 sum1 += data1[i] * data1[i-j-1];
76 }
77 autoc[j ] = sum0;
78 autoc[j+1] = sum1;
79 }
80
81 if(j==lag){
82 double sum = 1.0;
83 for(i=j-1; i<len; i+=2){
84 sum += data1[i ] * data1[i-j ]
85 + data1[i+1] * data1[i-j+1];
86 }
87 autoc[j] = sum;
88 }
89 }
28 90
29 /** 91 /**
30 * Quantize LPC coefficients 92 * Quantize LPC coefficients
31 */ 93 */
32 static void quantize_lpc_coefs(double *lpc_in, int order, int precision, 94 static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
113 int opt_order; 175 int opt_order;
114 176
115 assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER && use_lpc > 0); 177 assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER && use_lpc > 0);
116 178
117 if(use_lpc == 1){ 179 if(use_lpc == 1){
118 s->flac_compute_autocorr(samples, blocksize, max_order, autoc); 180 s->lpc_compute_autocorr(samples, blocksize, max_order, autoc);
119 181
120 compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1); 182 compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1);
121 183
122 for(i=0; i<max_order; i++) 184 for(i=0; i<max_order; i++)
123 ref[i] = fabs(lpc[i][i]); 185 ref[i] = fabs(lpc[i][i]);