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