[vlc-devel] [PATCH 1/1] audio_filter: add RNNoise denoiser filter

Thomas Guillem thomas at gllm.fr
Mon Jul 29 11:51:36 CEST 2019



On Fri, Jul 26, 2019, at 16:23, Tristan Matthews wrote:
> ---
>  NEWS                             |   3 +
>  configure.ac                     |   2 +
>  modules/audio_filter/Makefile.am |   8 +++
>  modules/audio_filter/rnnoise.c   | 108 +++++++++++++++++++++++++++++++
>  po/POTFILES.in                   |   1 +
>  5 files changed, 122 insertions(+)
>  create mode 100644 modules/audio_filter/rnnoise.c
> 
> diff --git a/NEWS b/NEWS
> index 0f4f6d74c0..e364df7abe 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -71,6 +71,9 @@ Video output:
>   * Remove RealRTSP plugin
>   * Remove Real demuxer plugin
>  
> +Audio filter:
> + * Add RNNoise recurrent neural network denoiser
> +
>  Video filter:
>   * Update yadif
>   * Remove remote OSD plugin
> diff --git a/configure.ac b/configure.ac
> index ac818d8b83..666d5bb10f 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -2901,6 +2901,8 @@ AS_IF([test "${enable_speex}" != "no"], [
>  ])
>  AM_CONDITIONAL([HAVE_SPEEXDSP], [test "$have_speexdsp" = "yes"])
>  
> +PKG_ENABLE_MODULES_VLC([RNNOISE], [], [rnnoise], [Rnnoise support], 
> [auto])
> +
>  dnl
>  dnl  Opus plugin
>  dnl
> diff --git a/modules/audio_filter/Makefile.am 
> b/modules/audio_filter/Makefile.am
> index 309074c75b..1c5fe20d25 100644
> --- a/modules/audio_filter/Makefile.am
> +++ b/modules/audio_filter/Makefile.am
> @@ -137,3 +137,11 @@ libspeex_resampler_plugin_la_LIBADD = 
> $(SPEEXDSP_LIBS)
>  if HAVE_SPEEXDSP
>  audio_filter_LTLIBRARIES += libspeex_resampler_plugin.la
>  endif
> +
> +librnnoise_plugin_la_SOURCES = audio_filter/rnnoise.c
> +librnnoise_plugin_la_CFLAGS = $(AM_CFLAGS) $(RNNOISE_CFLAGS)
> +librnnoise_plugin_la_LIBADD = $(RNNOISE_LIBS) $(LIBM)
> +librnnoise_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath 
> '$(audio_filterdir)'
> +
> +audio_filter_LTLIBRARIES += $(LTLIBrnnoise)
> +EXTRA_LTLIBRARIES += librnnoise_plugin.la
> diff --git a/modules/audio_filter/rnnoise.c 
> b/modules/audio_filter/rnnoise.c
> new file mode 100644
> index 0000000000..40956e9798
> --- /dev/null
> +++ b/modules/audio_filter/rnnoise.c
> @@ -0,0 +1,108 @@
> +/*****************************************************************************
> + * rnnoise.c : Recurrent neural network for audio noise reduction
> + 
> *****************************************************************************
> + * Copyright © 2019 Tristan Matthews
> + *
> + * This program 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.
> + *
> + * This program 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 this program; if not, write to the Free Software 
> Foundation,
> + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> + 
> *****************************************************************************/
> +
> +#ifdef HAVE_CONFIG_H
> +# include "config.h"
> +#endif
> +
> +#include <vlc_common.h>
> +#include <vlc_aout.h>
> +#include <vlc_filter.h>
> +#include <vlc_plugin.h>
> +
> +#include <rnnoise.h>
> +
> +static int Open (vlc_object_t *);
> +static void Close (vlc_object_t *);
> +
> +typedef struct
> +{
> +    DenoiseState *p_st;
> +    bool b_first;
> +} filter_sys_t;
> +
> +vlc_module_begin ()
> +    set_shortname (N_("RNNoise"))
> +    set_description (N_("RNNoise filter"))
> +    set_category (CAT_AUDIO)
> +    set_subcategory (SUBCAT_AUDIO_AFILTER)
> +    set_capability ("audio filter", 0)
> +    set_callbacks( Open, Close )
> +vlc_module_end ()
> +
> +static block_t *Process (filter_t *, block_t *);
> +
> +static int Open (vlc_object_t *obj)
> +{
> +    filter_t *p_filter = (filter_t *)obj;
> +
> +    filter_sys_t *p_sys = p_filter->p_sys = malloc( 
> sizeof(filter_sys_t) );
> +    if( unlikely(!p_sys) )
> +        return VLC_ENOMEM;
> +
> +    p_sys->b_first = true;
> +
> +    p_sys->p_st = rnnoise_create(NULL);
> +    if( unlikely(!p_sys->p_st) ) {
> +        free( p_sys );
> +        return VLC_ENOMEM;
> +    }
> +
> +    p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
> +    aout_FormatPrepare(&p_filter->fmt_in.audio);
> +    p_filter->fmt_out.audio = p_filter->fmt_in.audio;
> +    p_filter->pf_audio_filter = Process;
> +    return VLC_SUCCESS;
> +}
> +
> +static void Close( vlc_object_t *obj )
> +{
> +    filter_t *p_filter  = (filter_t *)obj;
> +    filter_sys_t *p_sys = p_filter->p_sys;
> +    rnnoise_destroy( p_sys->p_st );
> +    free( p_sys );
> +}
> +
> +#define FRAME_SIZE 480
> +
> +static block_t *Process (filter_t *p_filter, block_t *p_block)
> +{
> +    filter_sys_t *p_sys = p_filter->p_sys;
> +    float tmp[FRAME_SIZE];
> +    float *p_buffer = (float *)p_block->p_buffer;
> +    for ( int i_nb_samples = p_block->i_nb_samples; i_nb_samples > 0; 
> i_nb_samples -= FRAME_SIZE )
> +    {
> +        /* rnnoise processes blocks of 480 samples, and expects input 
> to be in the 32768 scale. */
> +        for (unsigned i = 0; i < FRAME_SIZE; i++) tmp[i] = p_buffer[i] 
> * 32768.f;


Possible heap-buffer-overflow here. You need to make sure p_buffer is > 480. If it's not the case, you need to keep the remaining data for the next Filter call.

> +
> +        rnnoise_process_frame(p_sys->p_st, tmp, tmp);
> +
> +        if( !p_sys->b_first )
> +        {
> +            for (unsigned i = 0; i < FRAME_SIZE; i++) p_buffer[i] = 
> tmp[i] / 32768.f;
> +        }
> +        else
> +        {
> +            p_sys->b_first = false;
> +        }
> +        p_buffer += FRAME_SIZE;
> +    }
> +    return p_block;
> +}
> diff --git a/po/POTFILES.in b/po/POTFILES.in
> index 1c2a5298a8..49b89067f4 100644
> --- a/po/POTFILES.in
> +++ b/po/POTFILES.in
> @@ -245,6 +245,7 @@ modules/audio_filter/resampler/soxr.c
>  modules/audio_filter/resampler/speex.c
>  modules/audio_filter/resampler/src.c
>  modules/audio_filter/resampler/ugly.c
> +modules/audio_filter/rnnoise.c
>  modules/audio_filter/scaletempo.c
>  modules/audio_filter/spatializer/spatializer.cpp
>  modules/audio_filter/stereo_widen.c
> -- 
> 2.20.1
> 
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel


More information about the vlc-devel mailing list