]> git.sesse.net Git - ffmpeg/blob - libavcodec/iirfilter.c
lavc: Drop deprecated VDPAU buffer fields
[ffmpeg] / libavcodec / iirfilter.c
1 /*
2  * IIR filter
3  * Copyright (c) 2008 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 <math.h>
28
29 #include "libavutil/attributes.h"
30 #include "libavutil/common.h"
31 #include "libavutil/log.h"
32
33 #include "iirfilter.h"
34
35 /**
36  * IIR filter global parameters
37  */
38 typedef struct FFIIRFilterCoeffs {
39     int   order;
40     float gain;
41     int   *cx;
42     float *cy;
43 } FFIIRFilterCoeffs;
44
45 /**
46  * IIR filter state
47  */
48 typedef struct FFIIRFilterState {
49     float x[1];
50 } FFIIRFilterState;
51
52 /// maximum supported filter order
53 #define MAXORDER 30
54
55 static av_cold int butterworth_init_coeffs(void *avc,
56                                            struct FFIIRFilterCoeffs *c,
57                                            enum IIRFilterMode filt_mode,
58                                            int order, float cutoff_ratio,
59                                            float stopband)
60 {
61     int i, j;
62     double wa;
63     double p[MAXORDER + 1][2];
64
65     if (filt_mode != FF_FILTER_MODE_LOWPASS) {
66         av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
67                                   "low-pass filter mode\n");
68         return -1;
69     }
70     if (order & 1) {
71         av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
72                                   "even filter orders\n");
73         return -1;
74     }
75
76     wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
77
78     c->cx[0] = 1;
79     for (i = 1; i < (order >> 1) + 1; i++)
80         c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
81
82     p[0][0] = 1.0;
83     p[0][1] = 0.0;
84     for (i = 1; i <= order; i++)
85         p[i][0] = p[i][1] = 0.0;
86     for (i = 0; i < order; i++) {
87         double zp[2];
88         double th = (i + (order >> 1) + 0.5) * M_PI / order;
89         double a_re, a_im, c_re, c_im;
90         zp[0] = cos(th) * wa;
91         zp[1] = sin(th) * wa;
92         a_re  = zp[0] + 2.0;
93         c_re  = zp[0] - 2.0;
94         a_im  =
95         c_im  = zp[1];
96         zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
97         zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
98
99         for (j = order; j >= 1; j--) {
100             a_re    = p[j][0];
101             a_im    = p[j][1];
102             p[j][0] = a_re * zp[0] - a_im * zp[1] + p[j - 1][0];
103             p[j][1] = a_re * zp[1] + a_im * zp[0] + p[j - 1][1];
104         }
105         a_re    = p[0][0] * zp[0] - p[0][1] * zp[1];
106         p[0][1] = p[0][0] * zp[1] + p[0][1] * zp[0];
107         p[0][0] = a_re;
108     }
109     c->gain = p[order][0];
110     for (i = 0; i < order; i++) {
111         c->gain += p[i][0];
112         c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
113                    (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
114     }
115     c->gain /= 1 << order;
116
117     return 0;
118 }
119
120 static av_cold int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
121                                       enum IIRFilterMode filt_mode, int order,
122                                       float cutoff_ratio, float stopband)
123 {
124     double cos_w0, sin_w0;
125     double a0, x0, x1;
126
127     if (filt_mode != FF_FILTER_MODE_HIGHPASS &&
128         filt_mode != FF_FILTER_MODE_LOWPASS) {
129         av_log(avc, AV_LOG_ERROR, "Biquad filter currently only supports "
130                                   "high-pass and low-pass filter modes\n");
131         return -1;
132     }
133     if (order != 2) {
134         av_log(avc, AV_LOG_ERROR, "Biquad filter must have order of 2\n");
135         return -1;
136     }
137
138     cos_w0 = cos(M_PI * cutoff_ratio);
139     sin_w0 = sin(M_PI * cutoff_ratio);
140
141     a0 = 1.0 + (sin_w0 / 2.0);
142
143     if (filt_mode == FF_FILTER_MODE_HIGHPASS) {
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     } else { // FF_FILTER_MODE_LOWPASS
148         c->gain  =  ((1.0 - cos_w0) / 2.0)  / a0;
149         x0       =  ((1.0 - cos_w0) / 2.0)  / a0;
150         x1       =   (1.0 - cos_w0)         / a0;
151     }
152     c->cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
153     c->cy[1] =  (2.0 *  cos_w0)        / a0;
154
155     // divide by gain to make the x coeffs integers.
156     // during filtering, the delay state will include the gain multiplication
157     c->cx[0] = lrintf(x0 / c->gain);
158     c->cx[1] = lrintf(x1 / c->gain);
159
160     return 0;
161 }
162
163 av_cold struct FFIIRFilterCoeffs *ff_iir_filter_init_coeffs(void *avc,
164                                                             enum IIRFilterType filt_type,
165                                                             enum IIRFilterMode filt_mode,
166                                                             int order, float cutoff_ratio,
167                                                             float stopband, float ripple)
168 {
169     FFIIRFilterCoeffs *c;
170     int ret = 0;
171
172     if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
173         return NULL;
174
175     FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
176                       init_fail);
177     FF_ALLOC_OR_GOTO(avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
178                      init_fail);
179     FF_ALLOC_OR_GOTO(avc, c->cy, sizeof(c->cy[0]) * order,
180                      init_fail);
181     c->order = order;
182
183     switch (filt_type) {
184     case FF_FILTER_TYPE_BUTTERWORTH:
185         ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
186                                       stopband);
187         break;
188     case FF_FILTER_TYPE_BIQUAD:
189         ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
190                                  stopband);
191         break;
192     default:
193         av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
194         goto init_fail;
195     }
196
197     if (!ret)
198         return c;
199
200 init_fail:
201     ff_iir_filter_free_coeffs(c);
202     return NULL;
203 }
204
205 av_cold struct FFIIRFilterState *ff_iir_filter_init_state(int order)
206 {
207     FFIIRFilterState *s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
208     return s;
209 }
210
211 #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
212
213 #define CONV_FLT(dest, source) dest = source;
214
215 #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt)             \
216     in = *src0    * c->gain  +                          \
217          c->cy[0] * s->x[i0] +                          \
218          c->cy[1] * s->x[i1] +                          \
219          c->cy[2] * s->x[i2] +                          \
220          c->cy[3] * s->x[i3];                           \
221     res = (s->x[i0] + in)       * 1 +                   \
222           (s->x[i1] + s->x[i3]) * 4 +                   \
223            s->x[i2]             * 6;                    \
224     CONV_ ## fmt(*dst0, res)                            \
225     s->x[i0] = in;                                      \
226     src0    += sstep;                                   \
227     dst0    += dstep;
228
229 #define FILTER_BW_O4(type, fmt) {           \
230     int i;                                  \
231     const type *src0 = src;                 \
232     type       *dst0 = dst;                 \
233     for (i = 0; i < size; i += 4) {         \
234         float in, res;                      \
235         FILTER_BW_O4_1(0, 1, 2, 3, fmt);    \
236         FILTER_BW_O4_1(1, 2, 3, 0, fmt);    \
237         FILTER_BW_O4_1(2, 3, 0, 1, fmt);    \
238         FILTER_BW_O4_1(3, 0, 1, 2, fmt);    \
239     }                                       \
240 }
241
242 #define FILTER_DIRECT_FORM_II(type, fmt) {                                  \
243     int i;                                                                  \
244     const type *src0 = src;                                                 \
245     type       *dst0 = dst;                                                 \
246     for (i = 0; i < size; i++) {                                            \
247         int j;                                                              \
248         float in, res;                                                      \
249         in = *src0 * c->gain;                                               \
250         for (j = 0; j < c->order; j++)                                      \
251             in += c->cy[j] * s->x[j];                                       \
252         res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];    \
253         for (j = 1; j < c->order >> 1; j++)                                 \
254             res += (s->x[j] + s->x[c->order - j]) * c->cx[j];               \
255         for (j = 0; j < c->order - 1; j++)                                  \
256             s->x[j] = s->x[j + 1];                                          \
257         CONV_ ## fmt(*dst0, res)                                            \
258         s->x[c->order - 1] = in;                                            \
259         src0              += sstep;                                         \
260         dst0              += dstep;                                         \
261     }                                                                       \
262 }
263
264 #define FILTER_O2(type, fmt) {                                              \
265     int i;                                                                  \
266     const type *src0 = src;                                                 \
267     type       *dst0 = dst;                                                 \
268     for (i = 0; i < size; i++) {                                            \
269         float in = *src0   * c->gain  +                                     \
270                    s->x[0] * c->cy[0] +                                     \
271                    s->x[1] * c->cy[1];                                      \
272         CONV_ ## fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1])              \
273         s->x[0] = s->x[1];                                                  \
274         s->x[1] = in;                                                       \
275         src0   += sstep;                                                    \
276         dst0   += dstep;                                                    \
277     }                                                                       \
278 }
279
280 void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
281                    struct FFIIRFilterState *s, int size,
282                    const int16_t *src, ptrdiff_t sstep,
283                    int16_t *dst, ptrdiff_t dstep)
284 {
285     if (c->order == 2) {
286         FILTER_O2(int16_t, S16)
287     } else if (c->order == 4) {
288         FILTER_BW_O4(int16_t, S16)
289     } else {
290         FILTER_DIRECT_FORM_II(int16_t, S16)
291     }
292 }
293
294 void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
295                        struct FFIIRFilterState *s, int size,
296                        const float *src, ptrdiff_t sstep,
297                        float *dst, ptrdiff_t dstep)
298 {
299     if (c->order == 2) {
300         FILTER_O2(float, FLT)
301     } else if (c->order == 4) {
302         FILTER_BW_O4(float, FLT)
303     } else {
304         FILTER_DIRECT_FORM_II(float, FLT)
305     }
306 }
307
308 av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
309 {
310     av_free(state);
311 }
312
313 av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
314 {
315     if (coeffs) {
316         av_free(coeffs->cx);
317         av_free(coeffs->cy);
318     }
319     av_free(coeffs);
320 }