X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fiirfilter.c;h=40a543da4c5b6b5e552357b3265eec68340f9bc3;hb=a0e1c3517a656dd32293f054a339e0ac73328138;hp=832559cfea3a36e276aed9a0ff18d035fe446022;hpb=69915b48d6a787e569826eecb6c3b34ec19aeccd;p=ffmpeg diff --git a/libavcodec/iirfilter.c b/libavcodec/iirfilter.c index 832559cfea3..40a543da4c5 100644 --- a/libavcodec/iirfilter.c +++ b/libavcodec/iirfilter.c @@ -2,20 +2,20 @@ * IIR filter * Copyright (c) 2008 Konstantin Shishkov * - * This file is part of FFmpeg. + * This file is part of Libav. * - * FFmpeg is free software; you can redistribute it and/or + * Libav is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * FFmpeg is distributed in the hope that it will be useful, + * Libav is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software + * License along with Libav; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -26,6 +26,8 @@ #include "iirfilter.h" #include +#include "libavutil/attributes.h" +#include "libavutil/common.h" /** * IIR filter global parameters @@ -47,10 +49,11 @@ typedef struct FFIIRFilterState{ /// maximum supported filter order #define MAXORDER 30 -static int butterworth_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, - enum IIRFilterMode filt_mode, - int order, float cutoff_ratio, - float stopband) +static av_cold int butterworth_init_coeffs(void *avc, + struct FFIIRFilterCoeffs *c, + enum IIRFilterMode filt_mode, + int order, float cutoff_ratio, + float stopband) { int i, j; double wa; @@ -112,9 +115,9 @@ static int butterworth_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, return 0; } -static int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, - enum IIRFilterMode filt_mode, int order, - float cutoff_ratio, float stopband) +static av_cold int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, + enum IIRFilterMode filt_mode, int order, + float cutoff_ratio, float stopband) { double cos_w0, sin_w0; double a0, x0, x1; @@ -137,22 +140,20 @@ static int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, if (filt_mode == FF_FILTER_MODE_HIGHPASS) { c->gain = ((1.0 + cos_w0) / 2.0) / a0; - x0 = (-(1.0 + cos_w0)) / a0; - x1 = ((1.0 + cos_w0) / 2.0) / a0; + x0 = ((1.0 + cos_w0) / 2.0) / a0; + x1 = (-(1.0 + cos_w0)) / a0; } else { // FF_FILTER_MODE_LOWPASS c->gain = ((1.0 - cos_w0) / 2.0) / a0; - x0 = (1.0 - cos_w0) / a0; - x1 = ((1.0 - cos_w0) / 2.0) / a0; + x0 = ((1.0 - cos_w0) / 2.0) / a0; + x1 = (1.0 - cos_w0) / a0; } - c->cy[0] = (2.0 * cos_w0) / a0; - c->cy[1] = (-1.0 + (sin_w0 / 2.0)) / a0; + c->cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0; + c->cy[1] = (2.0 * cos_w0) / a0; // divide by gain to make the x coeffs integers. // during filtering, the delay state will include the gain multiplication c->cx[0] = lrintf(x0 / c->gain); c->cx[1] = lrintf(x1 / c->gain); - c->cy[0] /= c->gain; - c->cy[1] /= c->gain; return 0; } @@ -256,11 +257,29 @@ av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order) } \ } +#define FILTER_O2(type, fmt) { \ + int i; \ + const type *src0 = src; \ + type *dst0 = dst; \ + for (i = 0; i < size; i++) { \ + float in = *src0 * c->gain + \ + s->x[0] * c->cy[0] + \ + s->x[1] * c->cy[1]; \ + CONV_##fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \ + s->x[0] = s->x[1]; \ + s->x[1] = in; \ + src0 += sstep; \ + dst0 += dstep; \ + } \ +} + void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep) { - if (c->order == 4) { + if (c->order == 2) { + FILTER_O2(int16_t, S16) + } else if (c->order == 4) { FILTER_BW_O4(int16_t, S16) } else { FILTER_DIRECT_FORM_II(int16_t, S16) @@ -271,7 +290,9 @@ void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const float *src, int sstep, float *dst, int dstep) { - if (c->order == 4) { + if (c->order == 2) { + FILTER_O2(float, FLT) + } else if (c->order == 4) { FILTER_BW_O4(float, FLT) } else { FILTER_DIRECT_FORM_II(float, FLT) @@ -293,6 +314,8 @@ av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs) } #ifdef TEST +#include + #define FILT_ORDER 4 #define SIZE 1024 int main(void) @@ -302,9 +325,8 @@ int main(void) float cutoff_coeff = 0.4; int16_t x[SIZE], y[SIZE]; int i; - FILE* fd; - fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH, + fcoeffs = ff_iir_filter_init_coeffs(NULL, FF_FILTER_TYPE_BUTTERWORTH, FF_FILTER_MODE_LOWPASS, FILT_ORDER, cutoff_coeff, 0.0, 0.0); fstate = ff_iir_filter_init_state(FILT_ORDER); @@ -315,13 +337,8 @@ int main(void) ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1); - fd = fopen("in.bin", "w"); - fwrite(x, sizeof(x[0]), SIZE, fd); - fclose(fd); - - fd = fopen("out.bin", "w"); - fwrite(y, sizeof(y[0]), SIZE, fd); - fclose(fd); + for (i = 0; i < SIZE; i++) + printf("%6d %6d\n", x[i], y[i]); ff_iir_filter_free_coeffs(fcoeffs); ff_iir_filter_free_state(fstate);