iirfilter.c
Go to the documentation of this file.
1 /*
2  * IIR filter
3  * Copyright (c) 2008 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * different IIR filters implementation
25  */
26 
27 #include "iirfilter.h"
28 #include <math.h>
29 #include "libavutil/common.h"
30 
31 /**
32  * IIR filter global parameters
33  */
34 typedef struct FFIIRFilterCoeffs{
35  int order;
36  float gain;
37  int *cx;
38  float *cy;
40 
41 /**
42  * IIR filter state
43  */
44 typedef struct FFIIRFilterState{
45  float x[1];
47 
48 /// maximum supported filter order
49 #define MAXORDER 30
50 
51 static int butterworth_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
52  enum IIRFilterMode filt_mode,
53  int order, float cutoff_ratio,
54  float stopband)
55 {
56  int i, j;
57  double wa;
58  double p[MAXORDER + 1][2];
59 
60  if (filt_mode != FF_FILTER_MODE_LOWPASS) {
61  av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
62  "low-pass filter mode\n");
63  return -1;
64  }
65  if (order & 1) {
66  av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
67  "even filter orders\n");
68  return -1;
69  }
70 
71  wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
72 
73  c->cx[0] = 1;
74  for(i = 1; i < (order >> 1) + 1; i++)
75  c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
76 
77  p[0][0] = 1.0;
78  p[0][1] = 0.0;
79  for(i = 1; i <= order; i++)
80  p[i][0] = p[i][1] = 0.0;
81  for(i = 0; i < order; i++){
82  double zp[2];
83  double th = (i + (order >> 1) + 0.5) * M_PI / order;
84  double a_re, a_im, c_re, c_im;
85  zp[0] = cos(th) * wa;
86  zp[1] = sin(th) * wa;
87  a_re = zp[0] + 2.0;
88  c_re = zp[0] - 2.0;
89  a_im =
90  c_im = zp[1];
91  zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
92  zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
93 
94  for(j = order; j >= 1; j--)
95  {
96  a_re = p[j][0];
97  a_im = p[j][1];
98  p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
99  p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
100  }
101  a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
102  p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
103  p[0][0] = a_re;
104  }
105  c->gain = p[order][0];
106  for(i = 0; i < order; i++){
107  c->gain += p[i][0];
108  c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
109  (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
110  }
111  c->gain /= 1 << order;
112 
113  return 0;
114 }
115 
116 static int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
117  enum IIRFilterMode filt_mode, int order,
118  float cutoff_ratio, float stopband)
119 {
120  double cos_w0, sin_w0;
121  double a0, x0, x1;
122 
123  if (filt_mode != FF_FILTER_MODE_HIGHPASS &&
124  filt_mode != FF_FILTER_MODE_LOWPASS) {
125  av_log(avc, AV_LOG_ERROR, "Biquad filter currently only supports "
126  "high-pass and low-pass filter modes\n");
127  return -1;
128  }
129  if (order != 2) {
130  av_log(avc, AV_LOG_ERROR, "Biquad filter must have order of 2\n");
131  return -1;
132  }
133 
134  cos_w0 = cos(M_PI * cutoff_ratio);
135  sin_w0 = sin(M_PI * cutoff_ratio);
136 
137  a0 = 1.0 + (sin_w0 / 2.0);
138 
139  if (filt_mode == FF_FILTER_MODE_HIGHPASS) {
140  c->gain = ((1.0 + cos_w0) / 2.0) / a0;
141  x0 = ((1.0 + cos_w0) / 2.0) / a0;
142  x1 = (-(1.0 + cos_w0)) / a0;
143  } else { // FF_FILTER_MODE_LOWPASS
144  c->gain = ((1.0 - cos_w0) / 2.0) / a0;
145  x0 = ((1.0 - cos_w0) / 2.0) / a0;
146  x1 = (1.0 - cos_w0) / a0;
147  }
148  c->cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
149  c->cy[1] = (2.0 * cos_w0) / a0;
150 
151  // divide by gain to make the x coeffs integers.
152  // during filtering, the delay state will include the gain multiplication
153  c->cx[0] = lrintf(x0 / c->gain);
154  c->cx[1] = lrintf(x1 / c->gain);
155 
156  return 0;
157 }
158 
160  enum IIRFilterType filt_type,
161  enum IIRFilterMode filt_mode,
162  int order, float cutoff_ratio,
163  float stopband, float ripple)
164 {
166  int ret = 0;
167 
168  if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
169  return NULL;
170 
171  FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
172  init_fail);
173  FF_ALLOC_OR_GOTO (avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
174  init_fail);
175  FF_ALLOC_OR_GOTO (avc, c->cy, sizeof(c->cy[0]) * order,
176  init_fail);
177  c->order = order;
178 
179  switch (filt_type) {
181  ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
182  stopband);
183  break;
185  ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
186  stopband);
187  break;
188  default:
189  av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
190  goto init_fail;
191  }
192 
193  if (!ret)
194  return c;
195 
196 init_fail:
198  return NULL;
199 }
200 
202 {
203  FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
204  return s;
205 }
206 
207 #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
208 
209 #define CONV_FLT(dest, source) dest = source;
210 
211 #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
212  in = *src0 * c->gain \
213  + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
214  + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
215  res = (s->x[i0] + in )*1 \
216  + (s->x[i1] + s->x[i3])*4 \
217  + s->x[i2] *6; \
218  CONV_##fmt(*dst0, res) \
219  s->x[i0] = in; \
220  src0 += sstep; \
221  dst0 += dstep;
222 
223 #define FILTER_BW_O4(type, fmt) { \
224  int i; \
225  const type *src0 = src; \
226  type *dst0 = dst; \
227  for (i = 0; i < size; i += 4) { \
228  float in, res; \
229  FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
230  FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
231  FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
232  FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
233  } \
234 }
235 
236 #define FILTER_DIRECT_FORM_II(type, fmt) { \
237  int i; \
238  const type *src0 = src; \
239  type *dst0 = dst; \
240  for (i = 0; i < size; i++) { \
241  int j; \
242  float in, res; \
243  in = *src0 * c->gain; \
244  for(j = 0; j < c->order; j++) \
245  in += c->cy[j] * s->x[j]; \
246  res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
247  for(j = 1; j < c->order >> 1; j++) \
248  res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
249  for(j = 0; j < c->order - 1; j++) \
250  s->x[j] = s->x[j + 1]; \
251  CONV_##fmt(*dst0, res) \
252  s->x[c->order - 1] = in; \
253  src0 += sstep; \
254  dst0 += dstep; \
255  } \
256 }
257 
258 #define FILTER_O2(type, fmt) { \
259  int i; \
260  const type *src0 = src; \
261  type *dst0 = dst; \
262  for (i = 0; i < size; i++) { \
263  float in = *src0 * c->gain + \
264  s->x[0] * c->cy[0] + \
265  s->x[1] * c->cy[1]; \
266  CONV_##fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
267  s->x[0] = s->x[1]; \
268  s->x[1] = in; \
269  src0 += sstep; \
270  dst0 += dstep; \
271  } \
272 }
273 
274 void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
275  struct FFIIRFilterState *s, int size,
276  const int16_t *src, int sstep, int16_t *dst, int dstep)
277 {
278  if (c->order == 2) {
279  FILTER_O2(int16_t, S16)
280  } else if (c->order == 4) {
281  FILTER_BW_O4(int16_t, S16)
282  } else {
283  FILTER_DIRECT_FORM_II(int16_t, S16)
284  }
285 }
286 
288  struct FFIIRFilterState *s, int size,
289  const float *src, int sstep, float *dst, int dstep)
290 {
291  if (c->order == 2) {
292  FILTER_O2(float, FLT)
293  } else if (c->order == 4) {
294  FILTER_BW_O4(float, FLT)
295  } else {
296  FILTER_DIRECT_FORM_II(float, FLT)
297  }
298 }
299 
301 {
302  av_free(state);
303 }
304 
306 {
307  if(coeffs){
308  av_free(coeffs->cx);
309  av_free(coeffs->cy);
310  }
311  av_free(coeffs);
312 }
313 
316 
317  if (HAVE_MIPSFPU)
319 }
320 
321 #ifdef TEST
322 #include <stdio.h>
323 
324 #define FILT_ORDER 4
325 #define SIZE 1024
326 int main(void)
327 {
328  struct FFIIRFilterCoeffs *fcoeffs = NULL;
329  struct FFIIRFilterState *fstate = NULL;
330  float cutoff_coeff = 0.4;
331  int16_t x[SIZE], y[SIZE];
332  int i;
333 
336  cutoff_coeff, 0.0, 0.0);
338 
339  for (i = 0; i < SIZE; i++) {
340  x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
341  }
342 
343  ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
344 
345  for (i = 0; i < SIZE; i++)
346  printf("%6d %6d\n", x[i], y[i]);
347 
348  ff_iir_filter_free_coeffs(fcoeffs);
349  ff_iir_filter_free_state(fstate);
350  return 0;
351 }
352 #endif /* TEST */
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const char * s
Definition: avisynth_c.h:668
#define a0
Definition: regdef.h:46
Sinusoidal phase f
IIRFilterMode
Definition: iirfilter.h:43
x1
Definition: genspecsines3.m:7
struct FFIIRFilterState FFIIRFilterState
IIR filter state.
static int butterworth_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, enum IIRFilterMode filt_mode, int order, float cutoff_ratio, float stopband)
Definition: iirfilter.c:51
av_cold struct FFIIRFilterState * ff_iir_filter_init_state(int order)
Create new filter state.
Definition: iirfilter.c:201
av_cold struct FFIIRFilterCoeffs * ff_iir_filter_init_coeffs(void *avc, enum IIRFilterType filt_type, enum IIRFilterMode filt_mode, int order, float cutoff_ratio, float stopband, float ripple)
Initialize filter coefficients.
Definition: iirfilter.c:159
#define av_cold
Definition: attributes.h:78
struct FFIIRFilterCoeffs FFIIRFilterCoeffs
IIR filter global parameters.
#define SIZE
Definition: golomb-test.c:31
#define lrintf(x)
Definition: libm_mips.h:70
#define FILTER_DIRECT_FORM_II(type, fmt)
Definition: iirfilter.c:236
Discrete Time axis x
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
static int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, enum IIRFilterMode filt_mode, int order, float cutoff_ratio, float stopband)
Definition: iirfilter.c:116
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
IIR filter state.
Definition: iirfilter.c:44
void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const float *src, int sstep, float *dst, int dstep)
Perform IIR filtering on floating-point input samples.
Definition: iirfilter.c:287
#define th
Definition: regdef.h:75
void(* filter_flt)(const struct FFIIRFilterCoeffs *coeffs, struct FFIIRFilterState *state, int size, const float *src, int sstep, float *dst, int dstep)
Perform IIR filtering on floating-point input samples.
Definition: iirfilter.h:62
#define FILTER_O2(type, fmt)
Definition: iirfilter.c:258
ret
Definition: avfilter.c:821
#define MAXORDER
maximum supported filter order
Definition: iirfilter.c:49
#define FILTER_BW_O4(type, fmt)
Definition: iirfilter.c:223
float x[1]
Definition: iirfilter.c:45
av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
Free filter coefficients.
Definition: iirfilter.c:305
NULL
Definition: eval.c:55
void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
Perform IIR filtering on signed 16-bit input samples.
Definition: iirfilter.c:274
AVS_Value src
Definition: avisynth_c.h:523
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
synthesis window for stochastic i
static av_always_inline av_const long int lrint(double x)
Definition: libm.h:148
IIRFilterType
Definition: iirfilter.h:35
static uint32_t state
Definition: trasher.c:27
IIR filter global parameters.
Definition: iirfilter.c:34
void ff_iir_filter_init_mips(FFIIRFilterContext *f)
void ff_iir_filter_init(FFIIRFilterContext *f)
Initialize FFIIRFilterContext.
Definition: iirfilter.c:314
common internal and external API header
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
static double c[64]
function y
Definition: D.m:1
#define FILT_ORDER
Definition: psymodel.c:94
IIR filter interface.
#define HAVE_MIPSFPU
Definition: config.h:59
printf("static const uint8_t my_array[100] = {\n")
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
#define M_PI
Definition: mathematics.h:46
int main(int argc, char **argv)
Definition: main.c:22
av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
Free filter state.
Definition: iirfilter.c:300
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)