00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef AVCODEC_LPC_H
00023 #define AVCODEC_LPC_H
00024
00025 #include <stdint.h>
00026 #include "libavutil/avassert.h"
00027 #include "dsputil.h"
00028
00029 #define ORDER_METHOD_EST 0
00030 #define ORDER_METHOD_2LEVEL 1
00031 #define ORDER_METHOD_4LEVEL 2
00032 #define ORDER_METHOD_8LEVEL 3
00033 #define ORDER_METHOD_SEARCH 4
00034 #define ORDER_METHOD_LOG 5
00035
00036 #define MIN_LPC_ORDER 1
00037 #define MAX_LPC_ORDER 32
00038
00042 enum FFLPCType {
00043 FF_LPC_TYPE_DEFAULT = -1,
00044 FF_LPC_TYPE_NONE = 0,
00045 FF_LPC_TYPE_FIXED = 1,
00046 FF_LPC_TYPE_LEVINSON = 2,
00047 FF_LPC_TYPE_CHOLESKY = 3,
00048 FF_LPC_TYPE_NB ,
00049 };
00050
00051 typedef struct LPCContext {
00052 int blocksize;
00053 int max_order;
00054 enum FFLPCType lpc_type;
00055 double *windowed_buffer;
00056 double *windowed_samples;
00057
00066 void (*lpc_apply_welch_window)(const int32_t *data, int len,
00067 double *w_data);
00081 void (*lpc_compute_autocorr)(const double *data, int len, int lag,
00082 double *autoc);
00083 } LPCContext;
00084
00085
00089 int ff_lpc_calc_coefs(LPCContext *s,
00090 const int32_t *samples, int blocksize, int min_order,
00091 int max_order, int precision,
00092 int32_t coefs[][MAX_LPC_ORDER], int *shift,
00093 enum FFLPCType lpc_type, int lpc_passes,
00094 int omethod, int max_shift, int zero_shift);
00095
00096 int ff_lpc_calc_ref_coefs(LPCContext *s,
00097 const int32_t *samples, int order, double *ref);
00098
00102 int ff_lpc_init(LPCContext *s, int blocksize, int max_order,
00103 enum FFLPCType lpc_type);
00104 void ff_lpc_init_x86(LPCContext *s);
00105
00109 void ff_lpc_end(LPCContext *s);
00110
00111 #ifdef LPC_USE_DOUBLE
00112 #define LPC_TYPE double
00113 #else
00114 #define LPC_TYPE float
00115 #endif
00116
00121 static inline void compute_ref_coefs(const LPC_TYPE *autoc, int max_order,
00122 LPC_TYPE *ref, LPC_TYPE *error)
00123 {
00124 int i, j;
00125 LPC_TYPE err;
00126 LPC_TYPE gen0[MAX_LPC_ORDER], gen1[MAX_LPC_ORDER];
00127
00128 for (i = 0; i < max_order; i++)
00129 gen0[i] = gen1[i] = autoc[i + 1];
00130
00131 err = autoc[0];
00132 ref[0] = -gen1[0] / err;
00133 err += gen1[0] * ref[0];
00134 if (error)
00135 error[0] = err;
00136 for (i = 1; i < max_order; i++) {
00137 for (j = 0; j < max_order - i; j++) {
00138 gen1[j] = gen1[j + 1] + ref[i - 1] * gen0[j];
00139 gen0[j] = gen1[j + 1] * ref[i - 1] + gen0[j];
00140 }
00141 ref[i] = -gen1[0] / err;
00142 err += gen1[0] * ref[i];
00143 if (error)
00144 error[i] = err;
00145 }
00146 }
00147
00152 static inline int compute_lpc_coefs(const LPC_TYPE *autoc, int max_order,
00153 LPC_TYPE *lpc, int lpc_stride, int fail,
00154 int normalize)
00155 {
00156 int i, j;
00157 LPC_TYPE err;
00158 LPC_TYPE *lpc_last = lpc;
00159
00160 av_assert2(normalize || !fail);
00161
00162 if (normalize)
00163 err = *autoc++;
00164
00165 if (fail && (autoc[max_order - 1] == 0 || err <= 0))
00166 return -1;
00167
00168 for(i=0; i<max_order; i++) {
00169 LPC_TYPE r = -autoc[i];
00170
00171 if (normalize) {
00172 for(j=0; j<i; j++)
00173 r -= lpc_last[j] * autoc[i-j-1];
00174
00175 r /= err;
00176 err *= 1.0 - (r * r);
00177 }
00178
00179 lpc[i] = r;
00180
00181 for(j=0; j < (i+1)>>1; j++) {
00182 LPC_TYPE f = lpc_last[ j];
00183 LPC_TYPE b = lpc_last[i-1-j];
00184 lpc[ j] = f + r * b;
00185 lpc[i-1-j] = b + r * f;
00186 }
00187
00188 if (fail && err < 0)
00189 return -1;
00190
00191 lpc_last = lpc;
00192 lpc += lpc_stride;
00193 }
00194
00195 return 0;
00196 }
00197
00198 #endif