• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/iirfilter.c

Go to the documentation of this file.
00001 /*
00002  * IIR filter
00003  * Copyright (c) 2008 Konstantin Shishkov
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "iirfilter.h"
00028 #include <complex.h>
00029 #include <math.h>
00030 
00034 typedef struct FFIIRFilterCoeffs{
00035     int   order;
00036     float gain;
00037     int   *cx;
00038     float *cy;
00039 }FFIIRFilterCoeffs;
00040 
00044 typedef struct FFIIRFilterState{
00045     float x[1];
00046 }FFIIRFilterState;
00047 
00049 #define MAXORDER 30
00050 
00051 struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(enum IIRFilterType filt_type,
00052                                                     enum IIRFilterMode filt_mode,
00053                                                     int order, float cutoff_ratio,
00054                                                     float stopband, float ripple)
00055 {
00056     int i, j, size;
00057     FFIIRFilterCoeffs *c;
00058     double wa;
00059     complex p[MAXORDER + 1];
00060 
00061     if(filt_type != FF_FILTER_TYPE_BUTTERWORTH || filt_mode != FF_FILTER_MODE_LOWPASS)
00062         return NULL;
00063     if(order <= 1 || (order & 1) || order > MAXORDER || cutoff_ratio >= 1.0)
00064         return NULL;
00065 
00066     c = av_malloc(sizeof(FFIIRFilterCoeffs));
00067     c->cx = av_malloc(sizeof(c->cx[0]) * ((order >> 1) + 1));
00068     c->cy = av_malloc(sizeof(c->cy[0]) * order);
00069     c->order = order;
00070 
00071     wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
00072 
00073     c->cx[0] = 1;
00074     for(i = 1; i < (order >> 1) + 1; i++)
00075         c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
00076 
00077     p[0] = 1.0;
00078     for(i = 1; i <= order; i++)
00079         p[i] = 0.0;
00080     for(i = 0; i < order; i++){
00081         complex zp;
00082         double th = (i + (order >> 1) + 0.5) * M_PI / order;
00083         zp = cexp(I*th) * wa;
00084         zp = (zp + 2.0) / (zp - 2.0);
00085 
00086         for(j = order; j >= 1; j--)
00087             p[j] = zp*p[j] + p[j - 1];
00088         p[0] *= zp;
00089     }
00090     c->gain = creal(p[order]);
00091     for(i = 0; i < order; i++){
00092         c->gain += creal(p[i]);
00093         c->cy[i] = creal(-p[i] / p[order]);
00094     }
00095     c->gain /= 1 << order;
00096 
00097     return c;
00098 }
00099 
00100 struct FFIIRFilterState* ff_iir_filter_init_state(int order)
00101 {
00102     FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
00103     return s;
00104 }
00105 
00106 #define FILTER(i0, i1, i2, i3)                    \
00107     in =   *src * c->gain                         \
00108          + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1]  \
00109          + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
00110     res =  (s->x[i0] + in      )*1                \
00111          + (s->x[i1] + s->x[i3])*4                \
00112          +  s->x[i2]            *6;               \
00113     *dst = av_clip_int16(lrintf(res));            \
00114     s->x[i0] = in;                                \
00115     src += sstep;                                 \
00116     dst += dstep;                                 \
00117 
00118 void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
00119 {
00120     int i;
00121 
00122     if(c->order == 4){
00123         for(i = 0; i < size; i += 4){
00124             float in, res;
00125 
00126             FILTER(0, 1, 2, 3);
00127             FILTER(1, 2, 3, 0);
00128             FILTER(2, 3, 0, 1);
00129             FILTER(3, 0, 1, 2);
00130         }
00131     }else{
00132         for(i = 0; i < size; i++){
00133             int j;
00134             float in, res;
00135             in = *src * c->gain;
00136             for(j = 0; j < c->order; j++)
00137                 in += c->cy[j] * s->x[j];
00138             res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];
00139             for(j = 1; j < c->order >> 1; j++)
00140                 res += (s->x[j] + s->x[c->order - j]) * c->cx[j];
00141             for(j = 0; j < c->order - 1; j++)
00142                 s->x[j] = s->x[j + 1];
00143             *dst = av_clip_int16(lrintf(res));
00144             s->x[c->order - 1] = in;
00145             src += sstep;
00146             dst += sstep;
00147         }
00148     }
00149 }
00150 
00151 void ff_iir_filter_free_state(struct FFIIRFilterState *state)
00152 {
00153     av_free(state);
00154 }
00155 
00156 void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
00157 {
00158     if(coeffs){
00159         av_free(coeffs->cx);
00160         av_free(coeffs->cy);
00161     }
00162     av_free(coeffs);
00163 }
00164 

Generated on Tue Nov 4 2014 12:59:22 for ffmpeg by  doxygen 1.7.1