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