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 libavcodec/iirfilter.c
24 * different IIR filters implementation
25 */
26
27#include "iirfilter.h"
28#include <complex.h>
29#include <math.h>
30
31/**
32 * IIR filter global parameters
33 */
34typedef struct FFIIRFilterCoeffs{
35    int   order;
36    float gain;
37    int   *cx;
38    float *cy;
39}FFIIRFilterCoeffs;
40
41/**
42 * IIR filter state
43 */
44typedef struct FFIIRFilterState{
45    float x[1];
46}FFIIRFilterState;
47
48/// maximum supported filter order
49#define MAXORDER 30
50
51struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(enum IIRFilterType filt_type,
52                                                    enum IIRFilterMode filt_mode,
53                                                    int order, float cutoff_ratio,
54                                                    float stopband, float ripple)
55{
56    int i, j, size;
57    FFIIRFilterCoeffs *c;
58    double wa;
59    complex p[MAXORDER + 1];
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
66    c = av_malloc(sizeof(FFIIRFilterCoeffs));
67    c->cx = av_malloc(sizeof(c->cx[0]) * ((order >> 1) + 1));
68    c->cy = av_malloc(sizeof(c->cy[0]) * order);
69    c->order = order;
70
71    wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
72
73    c->cx[0] = 1;
74    for(i = 1; i < (order >> 1) + 1; i++)
75        c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
76
77    p[0] = 1.0;
78    for(i = 1; i <= order; i++)
79        p[i] = 0.0;
80    for(i = 0; i < order; i++){
81        complex zp;
82        double th = (i + (order >> 1) + 0.5) * M_PI / order;
83        zp = cexp(I*th) * wa;
84        zp = (zp + 2.0) / (zp - 2.0);
85
86        for(j = order; j >= 1; j--)
87            p[j] = zp*p[j] + p[j - 1];
88        p[0] *= zp;
89    }
90    c->gain = creal(p[order]);
91    for(i = 0; i < order; i++){
92        c->gain += creal(p[i]);
93        c->cy[i] = creal(-p[i] / p[order]);
94    }
95    c->gain /= 1 << order;
96
97    return c;
98}
99
100struct FFIIRFilterState* ff_iir_filter_init_state(int order)
101{
102    FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
103    return s;
104}
105
106#define FILTER(i0, i1, i2, i3)                    \
107    in =   *src * c->gain                         \
108         + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1]  \
109         + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
110    res =  (s->x[i0] + in      )*1                \
111         + (s->x[i1] + s->x[i3])*4                \
112         +  s->x[i2]            *6;               \
113    *dst = av_clip_int16(lrintf(res));            \
114    s->x[i0] = in;                                \
115    src += sstep;                                 \
116    dst += dstep;                                 \
117
118void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
119{
120    int i;
121
122    if(c->order == 4){
123        for(i = 0; i < size; i += 4){
124            float in, res;
125
126            FILTER(0, 1, 2, 3);
127            FILTER(1, 2, 3, 0);
128            FILTER(2, 3, 0, 1);
129            FILTER(3, 0, 1, 2);
130        }
131    }else{
132        for(i = 0; i < size; i++){
133            int j;
134            float in, res;
135            in = *src * c->gain;
136            for(j = 0; j < c->order; j++)
137                in += c->cy[j] * s->x[j];
138            res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];
139            for(j = 1; j < c->order >> 1; j++)
140                res += (s->x[j] + s->x[c->order - j]) * c->cx[j];
141            for(j = 0; j < c->order - 1; j++)
142                s->x[j] = s->x[j + 1];
143            *dst = av_clip_int16(lrintf(res));
144            s->x[c->order - 1] = in;
145            src += sstep;
146            dst += sstep;
147        }
148    }
149}
150
151void ff_iir_filter_free_state(struct FFIIRFilterState *state)
152{
153    av_free(state);
154}
155
156void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
157{
158    if(coeffs){
159        av_free(coeffs->cx);
160        av_free(coeffs->cy);
161    }
162    av_free(coeffs);
163}
164
165