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
00152 int ff_lpc_calc_ref_coefs(LPCContext *s,
00153 const int32_t *samples, int order, double *ref)
00154 {
00155 double autoc[MAX_LPC_ORDER + 1];
00156
00157 s->lpc_apply_welch_window(samples, s->blocksize, s->windowed_samples);
00158 s->lpc_compute_autocorr(s->windowed_samples, s->blocksize, order, autoc);
00159 compute_ref_coefs(autoc, order, ref, NULL);
00160
00161 return order;
00162 }
00163
00170 int ff_lpc_calc_coefs(LPCContext *s,
00171 const int32_t *samples, int blocksize, int min_order,
00172 int max_order, int precision,
00173 int32_t coefs[][MAX_LPC_ORDER], int *shift,
00174 enum FFLPCType lpc_type, int lpc_passes,
00175 int omethod, int max_shift, int zero_shift)
00176 {
00177 double autoc[MAX_LPC_ORDER+1];
00178 double ref[MAX_LPC_ORDER];
00179 double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
00180 int i, j, pass;
00181 int opt_order;
00182
00183 av_assert2(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER &&
00184 lpc_type > FF_LPC_TYPE_FIXED);
00185
00186
00187 if (blocksize != s->blocksize || max_order != s->max_order ||
00188 lpc_type != s->lpc_type) {
00189 ff_lpc_end(s);
00190 ff_lpc_init(s, blocksize, max_order, lpc_type);
00191 }
00192
00193 if (lpc_type == FF_LPC_TYPE_LEVINSON) {
00194 s->lpc_apply_welch_window(samples, blocksize, s->windowed_samples);
00195
00196 s->lpc_compute_autocorr(s->windowed_samples, blocksize, max_order, autoc);
00197
00198 compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1);
00199
00200 for(i=0; i<max_order; i++)
00201 ref[i] = fabs(lpc[i][i]);
00202 } else if (lpc_type == FF_LPC_TYPE_CHOLESKY) {
00203 LLSModel m[2];
00204 double var[MAX_LPC_ORDER+1], av_uninit(weight);
00205
00206 if(lpc_passes <= 0)
00207 lpc_passes = 2;
00208
00209 for(pass=0; pass<lpc_passes; pass++){
00210 av_init_lls(&m[pass&1], max_order);
00211
00212 weight=0;
00213 for(i=max_order; i<blocksize; i++){
00214 for(j=0; j<=max_order; j++)
00215 var[j]= samples[i-j];
00216
00217 if(pass){
00218 double eval, inv, rinv;
00219 eval= av_evaluate_lls(&m[(pass-1)&1], var+1, max_order-1);
00220 eval= (512>>pass) + fabs(eval - var[0]);
00221 inv = 1/eval;
00222 rinv = sqrt(inv);
00223 for(j=0; j<=max_order; j++)
00224 var[j] *= rinv;
00225 weight += inv;
00226 }else
00227 weight++;
00228
00229 av_update_lls(&m[pass&1], var, 1.0);
00230 }
00231 av_solve_lls(&m[pass&1], 0.001, 0);
00232 }
00233
00234 for(i=0; i<max_order; i++){
00235 for(j=0; j<max_order; j++)
00236 lpc[i][j]=-m[(pass-1)&1].coeff[i][j];
00237 ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
00238 }
00239 for(i=max_order-1; i>0; i--)
00240 ref[i] = ref[i-1] - ref[i];
00241 } else
00242 av_assert0(0);
00243 opt_order = max_order;
00244
00245 if(omethod == ORDER_METHOD_EST) {
00246 opt_order = estimate_best_order(ref, min_order, max_order);
00247 i = opt_order-1;
00248 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
00249 } else {
00250 for(i=min_order-1; i<max_order; i++) {
00251 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
00252 }
00253 }
00254
00255 return opt_order;
00256 }
00257
00258 av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order,
00259 enum FFLPCType lpc_type)
00260 {
00261 s->blocksize = blocksize;
00262 s->max_order = max_order;
00263 s->lpc_type = lpc_type;
00264
00265 if (lpc_type == FF_LPC_TYPE_LEVINSON) {
00266 s->windowed_buffer = av_mallocz((blocksize + 2 + FFALIGN(max_order, 4)) *
00267 sizeof(*s->windowed_samples));
00268 if (!s->windowed_buffer)
00269 return AVERROR(ENOMEM);
00270 s->windowed_samples = s->windowed_buffer + FFALIGN(max_order, 4);
00271 } else {
00272 s->windowed_samples = NULL;
00273 }
00274
00275 s->lpc_apply_welch_window = lpc_apply_welch_window_c;
00276 s->lpc_compute_autocorr = lpc_compute_autocorr_c;
00277
00278 if (ARCH_X86)
00279 ff_lpc_init_x86(s);
00280
00281 return 0;
00282 }
00283
00284 av_cold void ff_lpc_end(LPCContext *s)
00285 {
00286 av_freep(&s->windowed_buffer);
00287 }