eac3dec: get right of unnecessary left shifts in 16-bit * 24-bit
[FFMpeg-mirror/lagarith.git] / libavcodec / iirfilter.c
blob62c2a61cd57c4ac4e5604e0c0df01f9a48059c72
1 /*
2 * IIR filter
3 * Copyright (c) 2008 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file libavcodec/iirfilter.c
24 * different IIR filters implementation
27 #include "iirfilter.h"
28 #include <complex.h>
29 #include <math.h>
31 /**
32 * IIR filter global parameters
34 typedef struct FFIIRFilterCoeffs{
35 int order;
36 float gain;
37 int *cx;
38 float *cy;
39 }FFIIRFilterCoeffs;
41 /**
42 * IIR filter state
44 typedef struct FFIIRFilterState{
45 float x[1];
46 }FFIIRFilterState;
48 /// maximum supported filter order
49 #define MAXORDER 30
51 struct 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)
56 int i, j, size;
57 FFIIRFilterCoeffs *c;
58 double wa;
59 complex p[MAXORDER + 1];
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;
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;
71 wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
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;
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);
86 for(j = order; j >= 1; j--)
87 p[j] = zp*p[j] + p[j - 1];
88 p[0] *= zp;
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]);
95 c->gain /= 1 << order;
97 return c;
100 struct FFIIRFilterState* ff_iir_filter_init_state(int order)
102 FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
103 return s;
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; \
118 void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
120 int i;
122 if(c->order == 4){
123 for(i = 0; i < size; i += 4){
124 float in, res;
126 FILTER(0, 1, 2, 3);
127 FILTER(1, 2, 3, 0);
128 FILTER(2, 3, 0, 1);
129 FILTER(3, 0, 1, 2);
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;
151 void ff_iir_filter_free_state(struct FFIIRFilterState *state)
153 av_free(state);
156 void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
158 if(coeffs){
159 av_free(coeffs->cx);
160 av_free(coeffs->cy);
162 av_free(coeffs);