annotate iirfilter.c @ 10655:0a2d577dd2d8 libavcodec

Make sure that compilation fails if functions like ff_put_string are used in code that selected a bitstream writer for which they do not work.
author reimar
date Mon, 07 Dec 2009 21:06:51 +0000
parents 0448219eb490
children 7dd2a45249a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
1 /*
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
2 * IIR filter
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
3 * Copyright (c) 2008 Konstantin Shishkov
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
4 *
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
5 * This file is part of FFmpeg.
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
6 *
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
11 *
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
16 *
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
20 */
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
21
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
22 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 7713
diff changeset
23 * @file libavcodec/iirfilter.c
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
24 * different IIR filters implementation
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
25 */
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
26
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
27 #include "iirfilter.h"
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
28 #include <math.h>
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
29
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
30 /**
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
31 * IIR filter global parameters
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
32 */
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
33 typedef struct FFIIRFilterCoeffs{
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
34 int order;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
35 float gain;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
36 int *cx;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
37 float *cy;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
38 }FFIIRFilterCoeffs;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
39
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
40 /**
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
41 * IIR filter state
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
42 */
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
43 typedef struct FFIIRFilterState{
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
44 float x[1];
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
45 }FFIIRFilterState;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
46
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
47 /// maximum supported filter order
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
48 #define MAXORDER 30
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
49
9947
1a345787ba68 Mark iir filter init and free functions av_cold.
alexc
parents: 9946
diff changeset
50 av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(enum IIRFilterType filt_type,
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
51 enum IIRFilterMode filt_mode,
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
52 int order, float cutoff_ratio,
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
53 float stopband, float ripple)
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
54 {
9946
0de8fdd06303 Fix "iirfilter.c:55: warning: unused variable ¡Æsize¡Ç"
alexc
parents: 9945
diff changeset
55 int i, j;
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
56 FFIIRFilterCoeffs *c;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
57 double wa;
9945
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
58 double p[MAXORDER + 1][2];
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
59
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
60 if(filt_type != FF_FILTER_TYPE_BUTTERWORTH || filt_mode != FF_FILTER_MODE_LOWPASS)
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
61 return NULL;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
62 if(order <= 1 || (order & 1) || order > MAXORDER || cutoff_ratio >= 1.0)
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
63 return NULL;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
64
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
65 c = av_malloc(sizeof(FFIIRFilterCoeffs));
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
66 c->cx = av_malloc(sizeof(c->cx[0]) * ((order >> 1) + 1));
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
67 c->cy = av_malloc(sizeof(c->cy[0]) * order);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
68 c->order = order;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
69
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
70 wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
71
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
72 c->cx[0] = 1;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
73 for(i = 1; i < (order >> 1) + 1; i++)
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
74 c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
75
9945
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
76 p[0][0] = 1.0;
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
77 p[0][1] = 0.0;
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
78 for(i = 1; i <= order; i++)
9945
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
79 p[i][0] = p[i][1] = 0.0;
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
80 for(i = 0; i < order; i++){
9945
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
81 double zp[2];
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
82 double th = (i + (order >> 1) + 0.5) * M_PI / order;
9945
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
83 double a_re, a_im, c_re, c_im;
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
84 zp[0] = cos(th) * wa;
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
85 zp[1] = sin(th) * wa;
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
86 a_re = zp[0] + 2.0;
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
87 c_re = zp[0] - 2.0;
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
88 a_im =
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
89 c_im = zp[1];
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
90 zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
91 zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
92
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
93 for(j = order; j >= 1; j--)
9945
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
94 {
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
95 a_re = p[j][0];
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
96 a_im = p[j][1];
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
97 p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
98 p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
99 }
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
100 a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
101 p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
102 p[0][0] = a_re;
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
103 }
9945
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
104 c->gain = p[order][0];
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
105 for(i = 0; i < order; i++){
9945
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
106 c->gain += p[i][0];
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
107 c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
12cf93d4b282 Eliminate use of complex.h from iirfilter.c
alexc
parents: 9943
diff changeset
108 (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
109 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
110 c->gain /= 1 << order;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
111
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
112 return c;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
113 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
114
9947
1a345787ba68 Mark iir filter init and free functions av_cold.
alexc
parents: 9946
diff changeset
115 av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
116 {
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
117 FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
118 return s;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
119 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
120
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
121 #define FILTER(i0, i1, i2, i3) \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
122 in = *src * c->gain \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
123 + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
124 + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
125 res = (s->x[i0] + in )*1 \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
126 + (s->x[i1] + s->x[i3])*4 \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
127 + s->x[i2] *6; \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
128 *dst = av_clip_int16(lrintf(res)); \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
129 s->x[i0] = in; \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
130 src += sstep; \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
131 dst += dstep; \
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
132
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
133 void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
134 {
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
135 int i;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
136
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
137 if(c->order == 4){
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
138 for(i = 0; i < size; i += 4){
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
139 float in, res;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
140
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
141 FILTER(0, 1, 2, 3);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
142 FILTER(1, 2, 3, 0);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
143 FILTER(2, 3, 0, 1);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
144 FILTER(3, 0, 1, 2);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
145 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
146 }else{
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
147 for(i = 0; i < size; i++){
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
148 int j;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
149 float in, res;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
150 in = *src * c->gain;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
151 for(j = 0; j < c->order; j++)
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
152 in += c->cy[j] * s->x[j];
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
153 res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
154 for(j = 1; j < c->order >> 1; j++)
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
155 res += (s->x[j] + s->x[c->order - j]) * c->cx[j];
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
156 for(j = 0; j < c->order - 1; j++)
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
157 s->x[j] = s->x[j + 1];
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
158 *dst = av_clip_int16(lrintf(res));
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
159 s->x[c->order - 1] = in;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
160 src += sstep;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
161 dst += sstep;
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
162 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
163 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
164 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
165
9947
1a345787ba68 Mark iir filter init and free functions av_cold.
alexc
parents: 9946
diff changeset
166 av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
167 {
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
168 av_free(state);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
169 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
170
9947
1a345787ba68 Mark iir filter init and free functions av_cold.
alexc
parents: 9946
diff changeset
171 av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
7713
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
172 {
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
173 if(coeffs){
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
174 av_free(coeffs->cx);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
175 av_free(coeffs->cy);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
176 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
177 av_free(coeffs);
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
178 }
f1e68b2dc389 Add generic IIR filter interface with Butterworth lowpass filter implementation
kostya
parents:
diff changeset
179
9956
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
180 #ifdef TEST
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
181 #define FILT_ORDER 4
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
182 #define SIZE 1024
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
183 int main(void)
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
184 {
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
185 struct FFIIRFilterCoeffs *fcoeffs = NULL;
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
186 struct FFIIRFilterState *fstate = NULL;
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
187 float cutoff_coeff = 0.4;
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
188 int16_t x[SIZE], y[SIZE];
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
189 int i;
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
190 FILE* fd;
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
191
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
192 fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH,
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
193 FF_FILTER_MODE_LOWPASS, FILT_ORDER,
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
194 cutoff_coeff, 0.0, 0.0);
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
195 fstate = ff_iir_filter_init_state(FILT_ORDER);
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
196
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
197 for (i = 0; i < SIZE; i++) {
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
198 x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
199 }
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
200
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
201 ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
202
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
203 fd = fopen("in.bin", "w");
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
204 fwrite(x, sizeof(x[0]), SIZE, fd);
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
205 fclose(fd);
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
206
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
207 fd = fopen("out.bin", "w");
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
208 fwrite(y, sizeof(y[0]), SIZE, fd);
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
209 fclose(fd);
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
210
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
211 ff_iir_filter_free_coeffs(fcoeffs);
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
212 ff_iir_filter_free_state(fstate);
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
213 return 0;
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
214 }
0448219eb490 IIR filter test program.
alexc
parents: 9947
diff changeset
215 #endif /* TEST */