3 * Copyright (c) 2008 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
24 * different IIR filters implementation
29 #include "libavutil/attributes.h"
30 #include "libavutil/common.h"
32 #include "iirfilter.h"
35 * IIR filter global parameters
37 typedef struct FFIIRFilterCoeffs {
47 typedef struct FFIIRFilterState {
51 /// maximum supported filter order
54 static av_cold int butterworth_init_coeffs(void *avc,
55 struct FFIIRFilterCoeffs *c,
56 enum IIRFilterMode filt_mode,
57 int order, float cutoff_ratio,
62 double p[MAXORDER + 1][2];
64 if (filt_mode != FF_FILTER_MODE_LOWPASS) {
65 av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
66 "low-pass filter mode\n");
70 av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
71 "even filter orders\n");
75 wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
78 for (i = 1; i < (order >> 1) + 1; i++)
79 c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
83 for (i = 1; i <= order; i++)
84 p[i][0] = p[i][1] = 0.0;
85 for (i = 0; i < order; i++) {
87 double th = (i + (order >> 1) + 0.5) * M_PI / order;
88 double a_re, a_im, c_re, c_im;
95 zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
96 zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
98 for (j = order; j >= 1; j--) {
101 p[j][0] = a_re * zp[0] - a_im * zp[1] + p[j - 1][0];
102 p[j][1] = a_re * zp[1] + a_im * zp[0] + p[j - 1][1];
104 a_re = p[0][0] * zp[0] - p[0][1] * zp[1];
105 p[0][1] = p[0][0] * zp[1] + p[0][1] * zp[0];
108 c->gain = p[order][0];
109 for (i = 0; i < order; i++) {
111 c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
112 (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
114 c->gain /= 1 << order;
119 static av_cold int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
120 enum IIRFilterMode filt_mode, int order,
121 float cutoff_ratio, float stopband)
123 double cos_w0, sin_w0;
126 if (filt_mode != FF_FILTER_MODE_HIGHPASS &&
127 filt_mode != FF_FILTER_MODE_LOWPASS) {
128 av_log(avc, AV_LOG_ERROR, "Biquad filter currently only supports "
129 "high-pass and low-pass filter modes\n");
133 av_log(avc, AV_LOG_ERROR, "Biquad filter must have order of 2\n");
137 cos_w0 = cos(M_PI * cutoff_ratio);
138 sin_w0 = sin(M_PI * cutoff_ratio);
140 a0 = 1.0 + (sin_w0 / 2.0);
142 if (filt_mode == FF_FILTER_MODE_HIGHPASS) {
143 c->gain = ((1.0 + cos_w0) / 2.0) / a0;
144 x0 = ((1.0 + cos_w0) / 2.0) / a0;
145 x1 = (-(1.0 + cos_w0)) / a0;
146 } else { // FF_FILTER_MODE_LOWPASS
147 c->gain = ((1.0 - cos_w0) / 2.0) / a0;
148 x0 = ((1.0 - cos_w0) / 2.0) / a0;
149 x1 = (1.0 - cos_w0) / a0;
151 c->cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
152 c->cy[1] = (2.0 * cos_w0) / a0;
154 // divide by gain to make the x coeffs integers.
155 // during filtering, the delay state will include the gain multiplication
156 c->cx[0] = lrintf(x0 / c->gain);
157 c->cx[1] = lrintf(x1 / c->gain);
162 av_cold struct FFIIRFilterCoeffs *ff_iir_filter_init_coeffs(void *avc,
163 enum IIRFilterType filt_type,
164 enum IIRFilterMode filt_mode,
165 int order, float cutoff_ratio,
166 float stopband, float ripple)
168 FFIIRFilterCoeffs *c;
171 if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
174 if (!(c = av_mallocz(sizeof(*c))) ||
175 !(c->cx = av_malloc (sizeof(c->cx[0]) * ((order >> 1) + 1))) ||
176 !(c->cy = av_malloc (sizeof(c->cy[0]) * order)))
181 case FF_FILTER_TYPE_BUTTERWORTH:
182 ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
185 case FF_FILTER_TYPE_BIQUAD:
186 ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
190 av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
197 ff_iir_filter_free_coeffsp(&c);
201 av_cold struct FFIIRFilterState *ff_iir_filter_init_state(int order)
203 FFIIRFilterState *s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
207 #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
209 #define CONV_FLT(dest, source) dest = source;
211 #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
212 in = *src0 * c->gain + \
213 c->cy[0] * s->x[i0] + \
214 c->cy[1] * s->x[i1] + \
215 c->cy[2] * s->x[i2] + \
216 c->cy[3] * s->x[i3]; \
217 res = (s->x[i0] + in) * 1 + \
218 (s->x[i1] + s->x[i3]) * 4 + \
220 CONV_ ## fmt(*dst0, res) \
225 #define FILTER_BW_O4(type, fmt) { \
227 const type *src0 = src; \
229 for (i = 0; i < size; i += 4) { \
231 FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
232 FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
233 FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
234 FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
238 #define FILTER_DIRECT_FORM_II(type, fmt) { \
240 const type *src0 = src; \
242 for (i = 0; i < size; i++) { \
245 in = *src0 * c->gain; \
246 for (j = 0; j < c->order; j++) \
247 in += c->cy[j] * s->x[j]; \
248 res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
249 for (j = 1; j < c->order >> 1; j++) \
250 res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
251 for (j = 0; j < c->order - 1; j++) \
252 s->x[j] = s->x[j + 1]; \
253 CONV_ ## fmt(*dst0, res) \
254 s->x[c->order - 1] = in; \
260 #define FILTER_O2(type, fmt) { \
262 const type *src0 = src; \
264 for (i = 0; i < size; i++) { \
265 float in = *src0 * c->gain + \
266 s->x[0] * c->cy[0] + \
267 s->x[1] * c->cy[1]; \
268 CONV_ ## fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
276 void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
277 struct FFIIRFilterState *s, int size,
278 const int16_t *src, ptrdiff_t sstep,
279 int16_t *dst, ptrdiff_t dstep)
282 FILTER_O2(int16_t, S16)
283 } else if (c->order == 4) {
284 FILTER_BW_O4(int16_t, S16)
286 FILTER_DIRECT_FORM_II(int16_t, S16)
290 void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
291 struct FFIIRFilterState *s, int size,
292 const float *src, ptrdiff_t sstep,
293 float *dst, ptrdiff_t dstep)
296 FILTER_O2(float, FLT)
297 } else if (c->order == 4) {
298 FILTER_BW_O4(float, FLT)
300 FILTER_DIRECT_FORM_II(float, FLT)
304 av_cold void ff_iir_filter_free_statep(struct FFIIRFilterState **state)
309 av_cold void ff_iir_filter_free_coeffsp(struct FFIIRFilterCoeffs **coeffsp)
311 struct FFIIRFilterCoeffs *coeffs = *coeffsp;
313 av_freep(&coeffs->cx);
314 av_freep(&coeffs->cy);
319 void ff_iir_filter_init(FFIIRFilterContext *f) {
320 f->filter_flt = ff_iir_filter_flt;
323 ff_iir_filter_init_mips(f);