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