00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "iirfilter.h"
00028 #include <math.h>
00029 #include "libavutil/common.h"
00030
00034 typedef struct FFIIRFilterCoeffs{
00035 int order;
00036 float gain;
00037 int *cx;
00038 float *cy;
00039 }FFIIRFilterCoeffs;
00040
00044 typedef struct FFIIRFilterState{
00045 float x[1];
00046 }FFIIRFilterState;
00047
00049 #define MAXORDER 30
00050
00051 static int butterworth_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
00052 enum IIRFilterMode filt_mode,
00053 int order, float cutoff_ratio,
00054 float stopband)
00055 {
00056 int i, j;
00057 double wa;
00058 double p[MAXORDER + 1][2];
00059
00060 if (filt_mode != FF_FILTER_MODE_LOWPASS) {
00061 av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
00062 "low-pass filter mode\n");
00063 return -1;
00064 }
00065 if (order & 1) {
00066 av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
00067 "even filter orders\n");
00068 return -1;
00069 }
00070
00071 wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
00072
00073 c->cx[0] = 1;
00074 for(i = 1; i < (order >> 1) + 1; i++)
00075 c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
00076
00077 p[0][0] = 1.0;
00078 p[0][1] = 0.0;
00079 for(i = 1; i <= order; i++)
00080 p[i][0] = p[i][1] = 0.0;
00081 for(i = 0; i < order; i++){
00082 double zp[2];
00083 double th = (i + (order >> 1) + 0.5) * M_PI / order;
00084 double a_re, a_im, c_re, c_im;
00085 zp[0] = cos(th) * wa;
00086 zp[1] = sin(th) * wa;
00087 a_re = zp[0] + 2.0;
00088 c_re = zp[0] - 2.0;
00089 a_im =
00090 c_im = zp[1];
00091 zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
00092 zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
00093
00094 for(j = order; j >= 1; j--)
00095 {
00096 a_re = p[j][0];
00097 a_im = p[j][1];
00098 p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
00099 p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
00100 }
00101 a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
00102 p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
00103 p[0][0] = a_re;
00104 }
00105 c->gain = p[order][0];
00106 for(i = 0; i < order; i++){
00107 c->gain += p[i][0];
00108 c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
00109 (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
00110 }
00111 c->gain /= 1 << order;
00112
00113 return 0;
00114 }
00115
00116 static int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
00117 enum IIRFilterMode filt_mode, int order,
00118 float cutoff_ratio, float stopband)
00119 {
00120 double cos_w0, sin_w0;
00121 double a0, x0, x1;
00122
00123 if (filt_mode != FF_FILTER_MODE_HIGHPASS &&
00124 filt_mode != FF_FILTER_MODE_LOWPASS) {
00125 av_log(avc, AV_LOG_ERROR, "Biquad filter currently only supports "
00126 "high-pass and low-pass filter modes\n");
00127 return -1;
00128 }
00129 if (order != 2) {
00130 av_log(avc, AV_LOG_ERROR, "Biquad filter must have order of 2\n");
00131 return -1;
00132 }
00133
00134 cos_w0 = cos(M_PI * cutoff_ratio);
00135 sin_w0 = sin(M_PI * cutoff_ratio);
00136
00137 a0 = 1.0 + (sin_w0 / 2.0);
00138
00139 if (filt_mode == FF_FILTER_MODE_HIGHPASS) {
00140 c->gain = ((1.0 + cos_w0) / 2.0) / a0;
00141 x0 = ((1.0 + cos_w0) / 2.0) / a0;
00142 x1 = (-(1.0 + cos_w0)) / a0;
00143 } else {
00144 c->gain = ((1.0 - cos_w0) / 2.0) / a0;
00145 x0 = ((1.0 - cos_w0) / 2.0) / a0;
00146 x1 = (1.0 - cos_w0) / a0;
00147 }
00148 c->cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
00149 c->cy[1] = (2.0 * cos_w0) / a0;
00150
00151
00152
00153 c->cx[0] = lrintf(x0 / c->gain);
00154 c->cx[1] = lrintf(x1 / c->gain);
00155
00156 return 0;
00157 }
00158
00159 av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(void *avc,
00160 enum IIRFilterType filt_type,
00161 enum IIRFilterMode filt_mode,
00162 int order, float cutoff_ratio,
00163 float stopband, float ripple)
00164 {
00165 FFIIRFilterCoeffs *c;
00166 int ret = 0;
00167
00168 if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
00169 return NULL;
00170
00171 FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
00172 init_fail);
00173 FF_ALLOC_OR_GOTO (avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
00174 init_fail);
00175 FF_ALLOC_OR_GOTO (avc, c->cy, sizeof(c->cy[0]) * order,
00176 init_fail);
00177 c->order = order;
00178
00179 switch (filt_type) {
00180 case FF_FILTER_TYPE_BUTTERWORTH:
00181 ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
00182 stopband);
00183 break;
00184 case FF_FILTER_TYPE_BIQUAD:
00185 ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
00186 stopband);
00187 break;
00188 default:
00189 av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
00190 goto init_fail;
00191 }
00192
00193 if (!ret)
00194 return c;
00195
00196 init_fail:
00197 ff_iir_filter_free_coeffs(c);
00198 return NULL;
00199 }
00200
00201 av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
00202 {
00203 FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
00204 return s;
00205 }
00206
00207 #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
00208
00209 #define CONV_FLT(dest, source) dest = source;
00210
00211 #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
00212 in = *src0 * c->gain \
00213 + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
00214 + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
00215 res = (s->x[i0] + in )*1 \
00216 + (s->x[i1] + s->x[i3])*4 \
00217 + s->x[i2] *6; \
00218 CONV_##fmt(*dst0, res) \
00219 s->x[i0] = in; \
00220 src0 += sstep; \
00221 dst0 += dstep;
00222
00223 #define FILTER_BW_O4(type, fmt) { \
00224 int i; \
00225 const type *src0 = src; \
00226 type *dst0 = dst; \
00227 for (i = 0; i < size; i += 4) { \
00228 float in, res; \
00229 FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
00230 FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
00231 FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
00232 FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
00233 } \
00234 }
00235
00236 #define FILTER_DIRECT_FORM_II(type, fmt) { \
00237 int i; \
00238 const type *src0 = src; \
00239 type *dst0 = dst; \
00240 for (i = 0; i < size; i++) { \
00241 int j; \
00242 float in, res; \
00243 in = *src0 * c->gain; \
00244 for(j = 0; j < c->order; j++) \
00245 in += c->cy[j] * s->x[j]; \
00246 res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
00247 for(j = 1; j < c->order >> 1; j++) \
00248 res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
00249 for(j = 0; j < c->order - 1; j++) \
00250 s->x[j] = s->x[j + 1]; \
00251 CONV_##fmt(*dst0, res) \
00252 s->x[c->order - 1] = in; \
00253 src0 += sstep; \
00254 dst0 += dstep; \
00255 } \
00256 }
00257
00258 #define FILTER_O2(type, fmt) { \
00259 int i; \
00260 const type *src0 = src; \
00261 type *dst0 = dst; \
00262 for (i = 0; i < size; i++) { \
00263 float in = *src0 * c->gain + \
00264 s->x[0] * c->cy[0] + \
00265 s->x[1] * c->cy[1]; \
00266 CONV_##fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
00267 s->x[0] = s->x[1]; \
00268 s->x[1] = in; \
00269 src0 += sstep; \
00270 dst0 += dstep; \
00271 } \
00272 }
00273
00274 void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
00275 struct FFIIRFilterState *s, int size,
00276 const int16_t *src, int sstep, int16_t *dst, int dstep)
00277 {
00278 if (c->order == 2) {
00279 FILTER_O2(int16_t, S16)
00280 } else if (c->order == 4) {
00281 FILTER_BW_O4(int16_t, S16)
00282 } else {
00283 FILTER_DIRECT_FORM_II(int16_t, S16)
00284 }
00285 }
00286
00287 void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
00288 struct FFIIRFilterState *s, int size,
00289 const float *src, int sstep, float *dst, int dstep)
00290 {
00291 if (c->order == 2) {
00292 FILTER_O2(float, FLT)
00293 } else if (c->order == 4) {
00294 FILTER_BW_O4(float, FLT)
00295 } else {
00296 FILTER_DIRECT_FORM_II(float, FLT)
00297 }
00298 }
00299
00300 av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
00301 {
00302 av_free(state);
00303 }
00304
00305 av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
00306 {
00307 if(coeffs){
00308 av_free(coeffs->cx);
00309 av_free(coeffs->cy);
00310 }
00311 av_free(coeffs);
00312 }
00313
00314 #ifdef TEST
00315 #include <stdio.h>
00316
00317 #define FILT_ORDER 4
00318 #define SIZE 1024
00319 int main(void)
00320 {
00321 struct FFIIRFilterCoeffs *fcoeffs = NULL;
00322 struct FFIIRFilterState *fstate = NULL;
00323 float cutoff_coeff = 0.4;
00324 int16_t x[SIZE], y[SIZE];
00325 int i;
00326
00327 fcoeffs = ff_iir_filter_init_coeffs(NULL, FF_FILTER_TYPE_BUTTERWORTH,
00328 FF_FILTER_MODE_LOWPASS, FILT_ORDER,
00329 cutoff_coeff, 0.0, 0.0);
00330 fstate = ff_iir_filter_init_state(FILT_ORDER);
00331
00332 for (i = 0; i < SIZE; i++) {
00333 x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
00334 }
00335
00336 ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
00337
00338 for (i = 0; i < SIZE; i++)
00339 printf("%6d %6d\n", x[i], y[i]);
00340
00341 ff_iir_filter_free_coeffs(fcoeffs);
00342 ff_iir_filter_free_state(fstate);
00343 return 0;
00344 }
00345 #endif