60313f1abebc2537c2d4bd5fc345e90e8fece692
[ffmpeg.git] / libavcodec / iirfilter.c
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
30 /**
31  * IIR filter global parameters
32  */
33 typedef struct FFIIRFilterCoeffs{
34     int   order;
35     float gain;
36     int   *cx;
37     float *cy;
38 }FFIIRFilterCoeffs;
39
40 /**
41  * IIR filter state
42  */
43 typedef struct FFIIRFilterState{
44     float x[1];
45 }FFIIRFilterState;
46
47 /// maximum supported filter order
48 #define MAXORDER 30
49
50 av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(enum IIRFilterType filt_type,
51                                                     enum IIRFilterMode filt_mode,
52                                                     int order, float cutoff_ratio,
53                                                     float stopband, float ripple)
54 {
55     int i, j;
56     FFIIRFilterCoeffs *c;
57     double wa;
58     double p[MAXORDER + 1][2];
59
60     if(filt_type != FF_FILTER_TYPE_BUTTERWORTH || filt_mode != FF_FILTER_MODE_LOWPASS)
61         return NULL;
62     if(order <= 1 || (order & 1) || order > MAXORDER || cutoff_ratio >= 1.0)
63         return NULL;
64
65     c = av_malloc(sizeof(FFIIRFilterCoeffs));
66     c->cx = av_malloc(sizeof(c->cx[0]) * ((order >> 1) + 1));
67     c->cy = av_malloc(sizeof(c->cy[0]) * order);
68     c->order = order;
69
70     wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
71
72     c->cx[0] = 1;
73     for(i = 1; i < (order >> 1) + 1; i++)
74         c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
75
76     p[0][0] = 1.0;
77     p[0][1] = 0.0;
78     for(i = 1; i <= order; i++)
79         p[i][0] = p[i][1] = 0.0;
80     for(i = 0; i < order; i++){
81         double zp[2];
82         double th = (i + (order >> 1) + 0.5) * M_PI / order;
83         double a_re, a_im, c_re, c_im;
84         zp[0] = cos(th) * wa;
85         zp[1] = sin(th) * wa;
86         a_re = zp[0] + 2.0;
87         c_re = zp[0] - 2.0;
88         a_im =
89         c_im = zp[1];
90         zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
91         zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
92
93         for(j = order; j >= 1; j--)
94         {
95             a_re = p[j][0];
96             a_im = p[j][1];
97             p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
98             p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
99         }
100         a_re    = p[0][0]*zp[0] - p[0][1]*zp[1];
101         p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
102         p[0][0] = a_re;
103     }
104     c->gain = p[order][0];
105     for(i = 0; i < order; i++){
106         c->gain += p[i][0];
107         c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
108                    (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
109     }
110     c->gain /= 1 << order;
111
112     return c;
113 }
114
115 av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
116 {
117     FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
118     return s;
119 }
120
121 #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
122
123 #define CONV_FLT(dest, source) dest = source;
124
125 #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt)         \
126     in = *src0 * c->gain                            \
127          + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1]    \
128          + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3];   \
129     res =  (s->x[i0] + in      )*1                  \
130          + (s->x[i1] + s->x[i3])*4                  \
131          +  s->x[i2]            *6;                 \
132     CONV_##fmt(*dst0, res)                          \
133     s->x[i0] = in;                                  \
134     src0 += sstep;                                  \
135     dst0 += dstep;
136
137 #define FILTER_BW_O4(type, fmt) {           \
138     int i;                                  \
139     const type *src0 = src;                 \
140     type       *dst0 = dst;                 \
141     for (i = 0; i < size; i += 4) {         \
142         float in, res;                      \
143         FILTER_BW_O4_1(0, 1, 2, 3, fmt);    \
144         FILTER_BW_O4_1(1, 2, 3, 0, fmt);    \
145         FILTER_BW_O4_1(2, 3, 0, 1, fmt);    \
146         FILTER_BW_O4_1(3, 0, 1, 2, fmt);    \
147     }                                       \
148 }
149
150 #define FILTER_DIRECT_FORM_II(type, fmt) {                                  \
151     int i;                                                                  \
152     const type *src0 = src;                                                 \
153     type       *dst0 = dst;                                                 \
154     for (i = 0; i < size; i++) {                                            \
155         int j;                                                              \
156         float in, res;                                                      \
157         in = *src0 * c->gain;                                               \
158         for(j = 0; j < c->order; j++)                                       \
159             in += c->cy[j] * s->x[j];                                       \
160         res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];    \
161         for(j = 1; j < c->order >> 1; j++)                                  \
162             res += (s->x[j] + s->x[c->order - j]) * c->cx[j];               \
163         for(j = 0; j < c->order - 1; j++)                                   \
164             s->x[j] = s->x[j + 1];                                          \
165         CONV_##fmt(*dst0, res)                                              \
166         s->x[c->order - 1] = in;                                            \
167         src0 += sstep;                                                      \
168         dst0 += dstep;                                                      \
169     }                                                                       \
170 }
171
172 void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
173                    struct FFIIRFilterState *s, int size,
174                    const int16_t *src, int sstep, int16_t *dst, int dstep)
175 {
176     if (c->order == 4) {
177         FILTER_BW_O4(int16_t, S16)
178     } else {
179         FILTER_DIRECT_FORM_II(int16_t, S16)
180     }
181 }
182
183 void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
184                        struct FFIIRFilterState *s, int size,
185                        const float *src, int sstep, void *dst, int dstep)
186 {
187     if (c->order == 4) {
188         FILTER_BW_O4(float, FLT)
189     } else {
190         FILTER_DIRECT_FORM_II(float, FLT)
191     }
192 }
193
194 av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
195 {
196     av_free(state);
197 }
198
199 av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
200 {
201     if(coeffs){
202         av_free(coeffs->cx);
203         av_free(coeffs->cy);
204     }
205     av_free(coeffs);
206 }
207
208 #ifdef TEST
209 #define FILT_ORDER 4
210 #define SIZE 1024
211 int main(void)
212 {
213     struct FFIIRFilterCoeffs *fcoeffs = NULL;
214     struct FFIIRFilterState  *fstate  = NULL;
215     float cutoff_coeff = 0.4;
216     int16_t x[SIZE], y[SIZE];
217     int i;
218     FILE* fd;
219
220     fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH,
221                                         FF_FILTER_MODE_LOWPASS, FILT_ORDER,
222                                         cutoff_coeff, 0.0, 0.0);
223     fstate  = ff_iir_filter_init_state(FILT_ORDER);
224
225     for (i = 0; i < SIZE; i++) {
226         x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
227     }
228
229     ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
230
231     fd = fopen("in.bin", "w");
232     fwrite(x, sizeof(x[0]), SIZE, fd);
233     fclose(fd);
234
235     fd = fopen("out.bin", "w");
236     fwrite(y, sizeof(y[0]), SIZE, fd);
237     fclose(fd);
238
239     ff_iir_filter_free_coeffs(fcoeffs);
240     ff_iir_filter_free_state(fstate);
241     return 0;
242 }
243 #endif /* TEST */