00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/lls.h"
00023
00024 #define LPC_USE_DOUBLE
00025 #include "lpc.h"
00026
00027
00031 static void lpc_apply_welch_window_c(const int32_t *data, int len,
00032 double *w_data)
00033 {
00034 int i, n2;
00035 double w;
00036 double c;
00037
00038
00039
00040 assert(!(len & 1));
00041
00042 n2 = (len >> 1);
00043 c = 2.0 / (len - 1.0);
00044
00045 w_data+=n2;
00046 data+=n2;
00047 for(i=0; i<n2; i++) {
00048 w = c - n2 + i;
00049 w = 1.0 - (w * w);
00050 w_data[-i-1] = data[-i-1] * w;
00051 w_data[+i ] = data[+i ] * w;
00052 }
00053 }
00054
00059 static void lpc_compute_autocorr_c(const double *data, int len, int lag,
00060 double *autoc)
00061 {
00062 int i, j;
00063
00064 for(j=0; j<lag; j+=2){
00065 double sum0 = 1.0, sum1 = 1.0;
00066 for(i=j; i<len; i++){
00067 sum0 += data[i] * data[i-j];
00068 sum1 += data[i] * data[i-j-1];
00069 }
00070 autoc[j ] = sum0;
00071 autoc[j+1] = sum1;
00072 }
00073
00074 if(j==lag){
00075 double sum = 1.0;
00076 for(i=j-1; i<len; i+=2){
00077 sum += data[i ] * data[i-j ]
00078 + data[i+1] * data[i-j+1];
00079 }
00080 autoc[j] = sum;
00081 }
00082 }
00083
00087 static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
00088 int32_t *lpc_out, int *shift, int max_shift, int zero_shift)
00089 {
00090 int i;
00091 double cmax, error;
00092 int32_t qmax;
00093 int sh;
00094
00095
00096 qmax = (1 << (precision - 1)) - 1;
00097
00098
00099 cmax = 0.0;
00100 for(i=0; i<order; i++) {
00101 cmax= FFMAX(cmax, fabs(lpc_in[i]));
00102 }
00103
00104
00105 if(cmax * (1 << max_shift) < 1.0) {
00106 *shift = zero_shift;
00107 memset(lpc_out, 0, sizeof(int32_t) * order);
00108 return;
00109 }
00110
00111
00112 sh = max_shift;
00113 while((cmax * (1 << sh) > qmax) && (sh > 0)) {
00114 sh--;
00115 }
00116
00117
00118
00119 if(sh == 0 && cmax > qmax) {
00120 double scale = ((double)qmax) / cmax;
00121 for(i=0; i<order; i++) {
00122 lpc_in[i] *= scale;
00123 }
00124 }
00125
00126
00127 error=0;
00128 for(i=0; i<order; i++) {
00129 error -= lpc_in[i] * (1 << sh);
00130 lpc_out[i] = av_clip(lrintf(error), -qmax, qmax);
00131 error -= lpc_out[i];
00132 }
00133 *shift = sh;
00134 }
00135
00136 static int estimate_best_order(double *ref, int min_order, int max_order)
00137 {
00138 int i, est;
00139
00140 est = min_order;
00141 for(i=max_order-1; i>=min_order-1; i--) {
00142 if(ref[i] > 0.10) {
00143 est = i+1;
00144 break;
00145 }
00146 }
00147 return est;
00148 }
00149
00156 int ff_lpc_calc_coefs(LPCContext *s,
00157 const int32_t *samples, int blocksize, int min_order,
00158 int max_order, int precision,
00159 int32_t coefs[][MAX_LPC_ORDER], int *shift,
00160 enum FFLPCType lpc_type, int lpc_passes,
00161 int omethod, int max_shift, int zero_shift)
00162 {
00163 double autoc[MAX_LPC_ORDER+1];
00164 double ref[MAX_LPC_ORDER];
00165 double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
00166 int i, j, pass;
00167 int opt_order;
00168
00169 assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER &&
00170 lpc_type > FF_LPC_TYPE_FIXED);
00171
00172
00173 if (blocksize != s->blocksize || max_order != s->max_order ||
00174 lpc_type != s->lpc_type) {
00175 ff_lpc_end(s);
00176 ff_lpc_init(s, blocksize, max_order, lpc_type);
00177 }
00178
00179 if (lpc_type == FF_LPC_TYPE_LEVINSON) {
00180 double *windowed_samples = s->windowed_samples + max_order;
00181
00182 s->lpc_apply_welch_window(samples, blocksize, windowed_samples);
00183
00184 s->lpc_compute_autocorr(windowed_samples, blocksize, max_order, autoc);
00185
00186 compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1);
00187
00188 for(i=0; i<max_order; i++)
00189 ref[i] = fabs(lpc[i][i]);
00190 } else if (lpc_type == FF_LPC_TYPE_CHOLESKY) {
00191 LLSModel m[2];
00192 double var[MAX_LPC_ORDER+1], av_uninit(weight);
00193
00194 for(pass=0; pass<lpc_passes; pass++){
00195 av_init_lls(&m[pass&1], max_order);
00196
00197 weight=0;
00198 for(i=max_order; i<blocksize; i++){
00199 for(j=0; j<=max_order; j++)
00200 var[j]= samples[i-j];
00201
00202 if(pass){
00203 double eval, inv, rinv;
00204 eval= av_evaluate_lls(&m[(pass-1)&1], var+1, max_order-1);
00205 eval= (512>>pass) + fabs(eval - var[0]);
00206 inv = 1/eval;
00207 rinv = sqrt(inv);
00208 for(j=0; j<=max_order; j++)
00209 var[j] *= rinv;
00210 weight += inv;
00211 }else
00212 weight++;
00213
00214 av_update_lls(&m[pass&1], var, 1.0);
00215 }
00216 av_solve_lls(&m[pass&1], 0.001, 0);
00217 }
00218
00219 for(i=0; i<max_order; i++){
00220 for(j=0; j<max_order; j++)
00221 lpc[i][j]=-m[(pass-1)&1].coeff[i][j];
00222 ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
00223 }
00224 for(i=max_order-1; i>0; i--)
00225 ref[i] = ref[i-1] - ref[i];
00226 }
00227 opt_order = max_order;
00228
00229 if(omethod == ORDER_METHOD_EST) {
00230 opt_order = estimate_best_order(ref, min_order, max_order);
00231 i = opt_order-1;
00232 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
00233 } else {
00234 for(i=min_order-1; i<max_order; i++) {
00235 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
00236 }
00237 }
00238
00239 return opt_order;
00240 }
00241
00242 av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order,
00243 enum FFLPCType lpc_type)
00244 {
00245 s->blocksize = blocksize;
00246 s->max_order = max_order;
00247 s->lpc_type = lpc_type;
00248
00249 if (lpc_type == FF_LPC_TYPE_LEVINSON) {
00250 s->windowed_samples = av_mallocz((blocksize + max_order + 2) *
00251 sizeof(*s->windowed_samples));
00252 if (!s->windowed_samples)
00253 return AVERROR(ENOMEM);
00254 } else {
00255 s->windowed_samples = NULL;
00256 }
00257
00258 s->lpc_apply_welch_window = lpc_apply_welch_window_c;
00259 s->lpc_compute_autocorr = lpc_compute_autocorr_c;
00260
00261 if (HAVE_MMX)
00262 ff_lpc_init_x86(s);
00263
00264 return 0;
00265 }
00266
00267 av_cold void ff_lpc_end(LPCContext *s)
00268 {
00269 av_freep(&s->windowed_samples);
00270 }