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