55 int order,
float cutoff_ratio,
64 "low-pass filter mode\n");
69 "even filter orders\n");
73 wa = 2 * tan(
M_PI * 0.5 * cutoff_ratio);
76 for(i = 1; i < (order >> 1) + 1; i++)
77 c->
cx[i] = c->
cx[i - 1] * (order - i + 1LL) / i;
81 for(i = 1; i <= order; i++)
82 p[i][0] = p[i][1] = 0.0;
83 for(i = 0; i < order; i++){
85 double th = (i + (order >> 1) + 0.5) *
M_PI / order;
86 double a_re, a_im, c_re, c_im;
93 zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
94 zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
96 for(j = order; j >= 1; j--)
100 p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
101 p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
103 a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
104 p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
107 c->
gain = p[order][0];
108 for(i = 0; i < order; i++){
110 c->
cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
111 (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
113 c->
gain /= 1 << order;
120 float cutoff_ratio,
float stopband)
122 double cos_w0, sin_w0;
128 "high-pass and low-pass filter modes\n");
136 cos_w0 = cos(
M_PI * cutoff_ratio);
137 sin_w0 = sin(
M_PI * cutoff_ratio);
139 a0 = 1.0 + (sin_w0 / 2.0);
142 c->
gain = ((1.0 + cos_w0) / 2.0) /
a0;
143 x0 = ((1.0 + cos_w0) / 2.0) /
a0;
144 x1 = (-(1.0 + cos_w0)) /
a0;
146 c->
gain = ((1.0 - cos_w0) / 2.0) /
a0;
147 x0 = ((1.0 - cos_w0) / 2.0) /
a0;
148 x1 = (1.0 - cos_w0) / a0;
150 c->
cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
151 c->
cy[1] = (2.0 * cos_w0) / a0;
164 int order,
float cutoff_ratio,
165 float stopband,
float ripple)
170 if (order <= 0 || order >
MAXORDER || cutoff_ratio >= 1.0)
209 #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
211 #define CONV_FLT(dest, source) dest = source;
213 #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
214 in = *src0 * c->gain \
215 + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
216 + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
217 res = (s->x[i0] + in )*1 \
218 + (s->x[i1] + s->x[i3])*4 \
220 CONV_##fmt(*dst0, res) \
225 #define FILTER_BW_O4(type, fmt) { \
227 const type *src0 = src; \
229 for (i = 0; i < size; i += 4) { \
231 FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
232 FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
233 FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
234 FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
238 #define FILTER_DIRECT_FORM_II(type, fmt) { \
240 const type *src0 = src; \
242 for (i = 0; i < size; i++) { \
245 in = *src0 * c->gain; \
246 for(j = 0; j < c->order; j++) \
247 in += c->cy[j] * s->x[j]; \
248 res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
249 for(j = 1; j < c->order >> 1; j++) \
250 res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
251 for(j = 0; j < c->order - 1; j++) \
252 s->x[j] = s->x[j + 1]; \
253 CONV_##fmt(*dst0, res) \
254 s->x[c->order - 1] = in; \
260 #define FILTER_O2(type, fmt) { \
262 const type *src0 = src; \
264 for (i = 0; i < size; i++) { \
265 float in = *src0 * c->gain + \
266 s->x[0] * c->cy[0] + \
267 s->x[1] * c->cy[1]; \
268 CONV_##fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
278 const int16_t *
src,
int sstep, int16_t *dst,
int dstep)
282 }
else if (c->
order == 4) {
291 const float *
src,
int sstep,
float *dst,
int dstep)
295 }
else if (c->
order == 4) {
333 float cutoff_coeff = 0.4;
339 cutoff_coeff, 0.0, 0.0);
342 for (i = 0; i <
SIZE; i++) {
343 x[i] =
lrint(0.75 * INT16_MAX * sin(0.5*
M_PI*i*i/SIZE));
348 for (i = 0; i <
SIZE; i++)
349 printf(
"%6d %6d\n", x[i], y[i]);
static av_cold int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, enum IIRFilterMode filt_mode, int order, float cutoff_ratio, float stopband)
void(* filter_flt)(const struct FFIIRFilterCoeffs *coeffs, struct FFIIRFilterState *state, int size, const float *src, int sstep, float *dst, int dstep)
Perform IIR filtering on floating-point input samples.
av_cold struct FFIIRFilterState * ff_iir_filter_init_state(int order)
Create new filter state.
Macro definitions for various function/variable attributes.
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)
Initialize filter coefficients.
#define FILTER_DIRECT_FORM_II(type, fmt)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
av_cold void ff_iir_filter_free_statep(struct FFIIRFilterState **state)
Free and zero filter state.
void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const float *src, int sstep, float *dst, int dstep)
Perform IIR filtering on floating-point input samples.
#define FILTER_O2(type, fmt)
#define MAXORDER
maximum supported filter order
#define FILTER_BW_O4(type, fmt)
static av_cold int butterworth_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, enum IIRFilterMode filt_mode, int order, float cutoff_ratio, float stopband)
void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
Perform IIR filtering on signed 16-bit input samples.
IIR filter global parameters.
void ff_iir_filter_init_mips(FFIIRFilterContext *f)
void ff_iir_filter_init(FFIIRFilterContext *f)
Initialize FFIIRFilterContext.
common internal and external API header
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
static const int16_t coeffs[]
av_cold void ff_iir_filter_free_coeffsp(struct FFIIRFilterCoeffs **coeffsp)
Free filter coefficients.
int main(int argc, char **argv)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)