[FFmpeg-devel] [PATCH] Lowpass functionality for lavc

Michael Niedermayer michaelni
Thu Aug 14 16:40:27 CEST 2008


On Thu, Aug 14, 2008 at 04:29:15PM +0300, Kostya wrote:
> On Tue, Aug 12, 2008 at 07:33:43PM +0200, Michael Niedermayer wrote:
> > On Tue, Aug 12, 2008 at 08:11:49PM +0300, Kostya wrote:
> > > On Tue, Aug 12, 2008 at 07:58:19PM +0300, Kostya wrote:
> > > [...]
> > > 
> > > oops, attached an old version of files, here're new ones
> > 
> > [...]
> > > /** filter order */
> > > #define LOWPASS_FILTER_ORDER 4
> > > 
> > > /**
> > >  * IIR filter global parameters
> > >  */
> > > typedef struct LPFilterCoeffs{
> > >     float gain;
> > >     float c[LOWPASS_FILTER_ORDER];
> > > }LPFilterCoeffs;
> > > 
> > > /**
> > >  * IIR filter state
> > >  */
> > > typedef struct LPFilterState{
> > >     int   pos;
> > >     float x[LOWPASS_FILTER_ORDER];
> > >     float y[LOWPASS_FILTER_ORDER];
> > > }LPFilterState;
> > 
> > I think these things could be moved to the c file
> > 
> > 
> > > 
> > > /**
> > >  * Initialize filter coefficients.
> > >  *
> > >  * @param order  filter order
> > >  * @param coeffs filter coefficients
> > >  * @param freq   input frequency (sample rate/2)
> > >  * @param cutoff cutoff frequency
> > >  *
> > >  * @return zero if filter creation succeeded, a negative value if filter could not be created
> > >  */
> > > int ff_lowpass_filter_init_coeffs(int order, LPFilterCoeffs *coeffs, int freq, int cutoff);
> > > 
> > 
> > > /**
> > >  * Filter input value.
> > >  *
> > >  * @param coeffs filter coefficients
> > >  * @param s      filter state
> > >  * @param in     input value
> > >  *
> > >  * @return filtered value
> > >  */
> > > static av_always_inline float ff_lowpass_filter(LPFilterCoeffs *coeffs, LPFilterState *s, float in)
> > > {
> > >     int i;
> > >     float res;
> > >     
> > >     in *= coeffs->gain;
> > >     //FIXME: made only for 4th order filter
> > >     res = (s->x[s->pos&3] + in)*1 + (s->x[(s->pos+1)&3] + s->x[(s->pos+3)&3])*4 +  s->x[(s->pos+2)&3]*6;
> > >     for(i = 0; i < 4; i++)
> > >         res += coeffs->c[i] * s->y[(s->pos + i)&3];
> > >     s->x[s->pos&3] = in;
> > >     s->y[s->pos&3] = res;
> > >     s->pos++;
> > >     return res;
> > > }
> > 
> > i suspect this would be even slower than the moving of samples.
> > does gcc optimize the &3 stuff away? if not you will have to unroll it
> > by hand like i suggested, if OTOH gcc does optimize it away then its fine.
> > and with unroll i mean a function that works with 4 samples at a time.
> > 
> > 
> > [...]
> > > int ff_lowpass_filter_init_coeffs(int order, LPFilterCoeffs *coeffs, int freq, int cutoff)
> > > {
> > >     int i, j, size;
> > >     float cutoff_ratio;
> > > 
> > >     //since I'm too lazy to calculate coefficients, I take more or less matching ones from the table
> > >     //TODO: generic version
> > >     size = sizeof(lp_filter_data) / sizeof(lp_filter_data[0]);
> > >     cutoff_ratio = (float)cutoff / freq;
> > >     if(cutoff_ratio > lp_filter_data[0][0])
> > >         return -1;
> > >     for(i = 0; i < size; i++){
> > >         if(cutoff_ratio >= lp_filter_data[i][0])
> > >             break;
> > >     }
> > >     if(i == size)
> > >         i = size - 1;
> > >     coeffs->gain     = lp_filter_data[i][1];
> > 
> > >     for(j = 0; j < 4; j++)
> > >         coeffs->c[j] = lp_filter_data[i][j+2];
> > 
> > memcpy
> 
> Here's a new version that will require changing only implementation,
> interface will remain the same.

[...]

> /*
>  * Lowpass IIR filter
>  * Copyright (c) 2008 Konstantin Shishkov
>  *
>  * This file is part of FFmpeg.
>  *
>  * FFmpeg 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,
>  * 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
>  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>  */
> 
> /**
>  * @file lowpass.h
>  * lowpass filter interface
>  */
> 
> #ifndef FFMPEG_LOWPASS_H
> #define FFMPEG_LOWPASS_H
> 
> #include "avcodec.h"
> 

ok


> /**
>  * Initialize filter coefficients.
>  *
>  * @param coeffs filter coefficients
>  * @param freq   input frequency (sample rate/2)
>  * @param cutoff cutoff frequency
>  *
>  * @return pointer to filter coefficients structure or NULL if filter cannot be created
>  */
> void* ff_lowpass_filter_init_coeffs(int order, int freq, int cutoff);

it should have some type not void* (and yes it can be done without putting the
whole struct in the header see libavutil/*.h)
you also forget to docuent order not that this is imporatnt i just noticed


> 
> /**
>  * Create new filter state.
>  *
>  * @param order filter order
>  *
>  * @return pointer to new filter state or NULL if state creation fails
>  */
> void* ff_lowpass_filter_init_state(int order);

state and filter coefficients structure? why 2?
especially with void this is a recipe for confusion


[...]

> /*
>  * Lowpass IIR filter
>  * Copyright (c) 2008 Konstantin Shishkov
>  *
>  * This file is part of FFmpeg.
>  *
>  * FFmpeg 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,
>  * 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
>  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>  */
> 
> /**
>  * @file lowpass.c
>  * lowpass filter implementation
>  */
> 
> #include "lowpass.h"
> 
> /**********************
>  *         TODO:
>  * support filters with order != 4
>  * calculate coefficients for filter instead of taking approximate ones from the table
>  *********************/
> 
> /** filter order */
> #define LOWPASS_FILTER_ORDER 4
> 
> /**
>  * filter data for 4th order IIR lowpass Butterworth filter
>  *
>  * data format:
>  * normalized cutoff frequency | inverse filter gain | coefficients
>  */
> static const float lp_filter_data[][LOWPASS_FILTER_ORDER+2] = {
>     { 0.5000000000, 9.398085e-01, -0.0176648009,  0.0000000000, -0.4860288221,  0.0000000000 },
>     { 0.4535147392, 6.816645e-01, -0.4646665999, -2.2127207402, -3.9912017501, -3.2380429984 },
>     { 0.4166666667, 4.998150e-01, -0.2498216698, -1.3392807613, -2.7693097862, -2.6386277439 },
>     { 0.3628117914, 3.103469e-01, -0.0965076902, -0.5977763360, -1.4972580903, -1.7740085241 },
>     { 0.3333333333, 2.346995e-01, -0.0557639007, -0.3623690447, -1.0304538354, -1.3066051440 },
>     { 0.2916666667, 1.528432e-01, -0.0261686639, -0.1473794606, -0.6204721225, -0.6514716536 },
>     { 0.2267573696, 6.917529e-02, -0.0202414073,  0.0780167640, -0.5277442247,  0.3631641670 },
>     { 0.2187500000, 6.178391e-02, -0.0223681543,  0.1069446609, -0.5615167033,  0.4883976841 },
>     { 0.2083333333, 5.298685e-02, -0.0261686639,  0.1473794606, -0.6204721225,  0.6514716536 },
>     { 0.1587301587, 2.229030e-02, -0.0647354087,  0.4172275190, -1.1412129810,  1.4320761385 },
>     { 0.1458333333, 1.693903e-02, -0.0823177861,  0.5192354923, -1.3444768251,  1.6365345642 },
>     { 0.1133786848, 7.374053e-03, -0.1481421788,  0.8650973862, -1.9894244796,  2.1544844308 },
>     { 0.1041666667, 5.541768e-03, -0.1742301048,  0.9921936565, -2.2090801108,  2.3024482658 },
> };
> 

ok


[...]
> void ff_lowpass_filter_free_coeffs(void *coeffs)
> {
>     if(coeffs)
>         av_free(coeffs);
> }
> 
> void ff_lowpass_filter_free_state(void *state)
> {
>     if(state)
>         av_free(state);
> }

the if are useless


> 
> void ff_lowpass_filter(void *coeffs, void *state, int size, int16_t *src, int sstep, int16_t *dst, int dstep)
> {
>     int i, j;
>     float in, res;
>     LPFilterCoeffs *c = (LPFilterCoeffs*)coeffs;
>     LPFilterState  *s = (LPFilterState*) state;
> 
>     for(i = 0; i < size; i++, src += sstep, dst += dstep){ 
>         for(j = 0; j < LOWPASS_FILTER_ORDER - 1; j++){
>             s->x[j] = s->x[j+1];
>             s->y[j] = s->y[j+1];
>         }

luckily iam in good mood
please unroll the loop and remove this

its supposed to look something like

#define FILTER(a,b,c,d, src)
    s->x[a]=  *src++ * c->gain;
    s->y[a]=  s->x[a] + 123*s->y[a] + 321*s->y[b] + ...
    *dst++= av_clip_int16(s->y[a]);

FILTER(0,1,2,3)
FILTER(1,2,3,0)
FILTER(2,3,0,1)
FILTER(3,0,1,2)

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The misfortune of the wise is better than the prosperity of the fool.
-- Epicurus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20080814/3c9d4ed7/attachment.pgp>



More information about the ffmpeg-devel mailing list