]> git.sesse.net Git - vlc/blobdiff - src/audio_output/filters.c
aout: handle filters creation/deletion from decoder, fix memory leak
[vlc] / src / audio_output / filters.c
index 6bbaae12462f450771976471125a617774d2d72b..ffaf575e1547742509c89f178a080f48e0f16def 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
  * filters.c : audio output filters management
  *****************************************************************************
- * Copyright (C) 2002-2007 the VideoLAN team
+ * Copyright (C) 2002-2007 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * 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 General Public License for more details.
+ * 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 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.
+ * 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.
  *****************************************************************************/
 
 /*****************************************************************************
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
-#include <vlc_interface.h>
-
-#ifdef HAVE_ALLOCA_H
-#   include <alloca.h>
-#endif
+#include <string.h>
+#include <assert.h>
 
+#include <vlc_common.h>
+#include <vlc_dialog.h>
+#include <vlc_modules.h>
 #include <vlc_aout.h>
+#include <vlc_filter.h>
+#include <vlc_vout.h>                  /* for vout_Request */
+
+#include <libvlc.h>
 #include "aout_internal.h"
 
 /*****************************************************************************
  * FindFilter: find an audio filter for a specific transformation
  *****************************************************************************/
-static aout_filter_t * FindFilter( aout_instance_t * p_aout,
-                             const audio_sample_format_t * p_input_format,
-                             const audio_sample_format_t * p_output_format )
+static filter_t * FindFilter( vlc_object_t *obj,
+                              const audio_sample_format_t *infmt,
+                              const audio_sample_format_t *outfmt )
 {
-    aout_filter_t * p_filter = vlc_object_create( p_aout,
-                                                  sizeof(aout_filter_t) );
+    static const char typename[] = "audio converter";
+    const char *type = "audio converter", *name = NULL;
+    filter_t * p_filter;
+
+    p_filter = vlc_custom_create( obj, sizeof(*p_filter), typename );
 
     if ( p_filter == NULL ) return NULL;
-    vlc_object_attach( p_filter, p_aout );
 
-    memcpy( &p_filter->input, p_input_format, sizeof(audio_sample_format_t) );
-    memcpy( &p_filter->output, p_output_format,
-            sizeof(audio_sample_format_t) );
-    p_filter->p_module = module_Need( p_filter, "audio filter", NULL, 0 );
+    p_filter->fmt_in.audio = *infmt;
+    p_filter->fmt_in.i_codec = infmt->i_format;
+    p_filter->fmt_out.audio = *outfmt;
+    p_filter->fmt_out.i_codec = outfmt->i_format;
+
+    if( infmt->i_format == outfmt->i_format
+     && infmt->i_physical_channels == outfmt->i_physical_channels
+     && infmt->i_original_channels == outfmt->i_original_channels )
+    {
+        assert( infmt->i_rate != outfmt->i_rate );
+        type = "audio resampler";
+        name = "$audio-resampler";
+    }
+
+    p_filter->p_module = module_need( p_filter, type, name, false );
     if ( p_filter->p_module == NULL )
     {
-        vlc_object_detach( p_filter );
         vlc_object_release( p_filter );
         return NULL;
     }
 
-    p_filter->b_continuity = false;
-
+    assert( p_filter->pf_audio_filter );
     return p_filter;
 }
 
-/*****************************************************************************
- * SplitConversion: split a conversion in two parts
- *****************************************************************************
- * Returns the number of conversions required by the first part - 0 if only
- * one conversion was asked.
- * Beware : p_output_format can be modified during this function if the
- * developer passed SplitConversion( toto, titi, titi, ... ). That is legal.
- * SplitConversion( toto, titi, toto, ... ) isn't.
- *****************************************************************************/
-static int SplitConversion( const audio_sample_format_t * p_input_format,
-                            const audio_sample_format_t * p_output_format,
-                            audio_sample_format_t * p_middle_format )
+/**
+ * Splits audio format conversion in two simpler conversions
+ * @return 0 on successful split, -1 if the input and output formats are too
+ * similar to split the conversion.
+ */
+static int SplitConversion( const audio_sample_format_t *restrict infmt,
+                            const audio_sample_format_t *restrict outfmt,
+                            audio_sample_format_t *midfmt )
 {
-    bool b_format =
-             (p_input_format->i_format != p_output_format->i_format);
-    bool b_rate = (p_input_format->i_rate != p_output_format->i_rate);
-    bool b_channels =
-        (p_input_format->i_physical_channels
-          != p_output_format->i_physical_channels)
-     || (p_input_format->i_original_channels
-          != p_output_format->i_original_channels);
-    int i_nb_conversions = b_format + b_rate + b_channels;
+    *midfmt = *outfmt;
 
-    if ( i_nb_conversions <= 1 ) return 0;
+    /* Lastly: resample (after format conversion and remixing) */
+    if( infmt->i_rate != outfmt->i_rate )
+        midfmt->i_rate = infmt->i_rate;
+    else
+    /* Penultimately: remix channels (after format conversion) */
+    if( infmt->i_physical_channels != outfmt->i_physical_channels
+     || infmt->i_original_channels != outfmt->i_original_channels )
+    {
+        midfmt->i_physical_channels = infmt->i_physical_channels;
+        midfmt->i_original_channels = infmt->i_original_channels;
+    }
+    else
+    /* Second: convert linear to S16N as intermediate format */
+    if( AOUT_FMT_LINEAR( infmt ) )
+    {
+        /* All conversion from linear to S16N must be supported directly. */
+        if( outfmt->i_format == VLC_CODEC_S16N )
+            return -1;
+        midfmt->i_format = VLC_CODEC_S16N;
+    }
+    else
+    /* First: convert non-linear to FI32 as intermediate format */
+    {
+        if( outfmt->i_format == VLC_CODEC_FI32 )
+            return -1;
+        midfmt->i_format = VLC_CODEC_FI32;
+    }
 
-    memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
+    assert( !AOUT_FMTS_IDENTICAL( infmt, midfmt ) );
+    aout_FormatPrepare( midfmt );
+    return 0;
+}
+
+#undef aout_FiltersCreatePipeline
+/**
+ * Allocates audio format conversion filters
+ * @param obj parent VLC object for new filters
+ * @param filters table of filters [IN/OUT]
+ * @param nb_filters pointer to the number of filters in the table [IN/OUT]
+ * @param max_filters size of filters table [IN]
+ * @param infmt input audio format
+ * @param outfmt output audio format
+ * @return 0 on success, -1 on failure
+ */
+int aout_FiltersCreatePipeline( vlc_object_t *obj, filter_t **filters,
+                                unsigned *nb_filters, unsigned max_filters,
+                                const audio_sample_format_t *restrict infmt,
+                                const audio_sample_format_t *restrict outfmt )
+{
+    audio_sample_format_t curfmt = *outfmt;
+    unsigned i = 0;
+
+    max_filters -= *nb_filters;
+    filters += *nb_filters;
+    aout_FormatsPrint( obj, "filter(s)", infmt, outfmt );
 
-    if ( i_nb_conversions == 2 )
+    while( !AOUT_FMTS_IDENTICAL( infmt, &curfmt ) )
     {
-        if ( !b_format || !b_channels )
+        if( i >= max_filters )
+        {
+            msg_Err( obj, "maximum of %u filters reached", max_filters );
+            dialog_Fatal( obj, _("Audio filtering failed"),
+                          _("The maximum number of filters (%u) was reached."),
+                          max_filters );
+            goto rollback;
+        }
+
+        /* Make room and prepend a filter */
+        memmove( filters + 1, filters, i * sizeof( *filters ) );
+
+        *filters = FindFilter( obj, infmt, &curfmt );
+        if( *filters != NULL )
+        {
+            i++;
+            break; /* done! */
+        }
+
+        audio_sample_format_t midfmt;
+        /* Split the conversion */
+        if( SplitConversion( infmt, &curfmt, &midfmt ) )
         {
-            p_middle_format->i_rate = p_input_format->i_rate;
-            aout_FormatPrepare( p_middle_format );
-            return 1;
+            msg_Err( obj, "conversion pipeline failed: %4.4s -> %4.4s",
+                     (const char *)&infmt->i_format,
+                     (const char *)&outfmt->i_format );
+            goto rollback;
         }
 
-        /* !b_rate */
-        p_middle_format->i_physical_channels
-             = p_input_format->i_physical_channels;
-        p_middle_format->i_original_channels
-             = p_input_format->i_original_channels;
-        aout_FormatPrepare( p_middle_format );
-        return 1;
+        *filters = FindFilter( obj, &midfmt, &curfmt );
+        if( *filters == NULL )
+        {
+            msg_Err( obj, "cannot find filter for simple conversion" );
+            goto rollback;
+        }
+        curfmt = midfmt;
+        i++;
     }
 
-    /* i_nb_conversion == 3 */
-    p_middle_format->i_rate = p_input_format->i_rate;
-    aout_FormatPrepare( p_middle_format );
-    return 2;
-}
+    msg_Dbg( obj, "conversion pipeline completed" );
+    *nb_filters += i;
+    return 0;
 
-static void ReleaseFilter( aout_filter_t * p_filter )
-{
-    module_Unneed( p_filter, p_filter->p_module );
-    vlc_object_detach( p_filter );
-    vlc_object_release( p_filter );
+rollback:
+    aout_FiltersDestroyPipeline( filters, i );
+    return -1;
 }
 
-/*****************************************************************************
- * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
- *                             format to another
- *****************************************************************************
- * pi_nb_filters must be initialized before calling this function
- *****************************************************************************/
-int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
-                                aout_filter_t ** pp_filters_start,
-                                int * pi_nb_filters,
-                                const audio_sample_format_t * p_input_format,
-                                const audio_sample_format_t * p_output_format )
+/**
+ * Destroys a chain of audio filters.
+ */
+void aout_FiltersDestroyPipeline( filter_t *const *filters, unsigned n )
 {
-    aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
-    audio_sample_format_t temp_format;
-    int i_nb_conversions;
-
-    if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
+    for( unsigned i = 0; i < n; i++ )
     {
-        msg_Dbg( p_aout, "no need for any filter" );
-        return 0;
+        filter_t *p_filter = filters[i];
+
+        module_unneed( p_filter, p_filter->p_module );
+        vlc_object_release( p_filter );
     }
+}
 
-    aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
+static inline bool ChangeFiltersString (vlc_object_t *aout, const char *var,
+                                        const char *filter, bool add)
+{
+    return aout_ChangeFilterString (aout, aout, var, filter, add);
+}
 
-    if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
-    {
-        msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
-        intf_UserFatal( p_aout, false, _("Audio filtering failed"),
-                        _("The maximum number of filters (%d) was reached."),
-                        AOUT_MAX_FILTERS );
-        return -1;
-    }
+/**
+ * Filters an audio buffer through a chain of filters.
+ */
+void aout_FiltersPlay( filter_t *const *pp_filters,
+                       unsigned i_nb_filters, block_t ** pp_block )
+{
+    block_t *p_block = *pp_block;
 
-    /* Try to find a filter to do the whole conversion. */
-    pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
-    if ( pp_filters[0] != NULL )
+    /* TODO: use filter chain */
+    for( unsigned i = 0; (i < i_nb_filters) && (p_block != NULL); i++ )
     {
-        msg_Dbg( p_aout, "found a filter for the whole conversion" );
-        ++*pi_nb_filters;
-        return 0;
-    }
+        filter_t * p_filter = pp_filters[i];
 
-    /* We'll have to split the conversion. We always do the downmixing
-     * before the resampling, because the audio decoder can probably do it
-     * for us. */
-    i_nb_conversions = SplitConversion( p_input_format,
-                                        p_output_format, &temp_format );
-    if ( !i_nb_conversions )
-    {
-        /* There was only one conversion to do, and we already failed. */
-        msg_Err( p_aout, "couldn't find a filter for the conversion" );
-        return -1;
+        /* Please note that p_block->i_nb_samples & i_buffer
+         * shall be set by the filter plug-in. */
+        p_block = p_filter->pf_audio_filter( p_filter, p_block );
     }
+    *pp_block = p_block;
+}
 
-    pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
-    if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
+/** Callback for visualization selection */
+static int VisualizationCallback (vlc_object_t *obj, char const *var,
+                                  vlc_value_t oldval, vlc_value_t newval,
+                                  void *data)
+{
+    audio_output_t *aout = (audio_output_t *)obj;
+    const char *mode = newval.psz_string;
+
+    if (!*mode)
     {
-        /* Try with only one conversion. */
-        SplitConversion( p_input_format, &temp_format, &temp_format );
-        pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
+        ChangeFiltersString (obj, "audio-visual", "goom", false);
+        ChangeFiltersString (obj, "audio-visual", "visual", false);
+        ChangeFiltersString (obj, "audio-visual", "projectm", false);
+        ChangeFiltersString (obj, "audio-visual", "vsxu", false);
     }
-    if ( pp_filters[0] == NULL )
+    else if (!strcmp ("goom", mode))
     {
-        msg_Err( p_aout,
-              "couldn't find a filter for the first part of the conversion" );
-        return -1;
+        ChangeFiltersString (obj, "audio-visual", "visual", false );
+        ChangeFiltersString (obj, "audio-visual", "goom", true );
+        ChangeFiltersString (obj, "audio-visual", "projectm", false );
+        ChangeFiltersString (obj, "audio-visual", "vsxu", false);
     }
-
-    /* We have the first stage of the conversion. Find a filter for
-     * the rest. */
-    if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
+    else if (!strcmp ("projectm", mode))
     {
-        ReleaseFilter( pp_filters[0] );
-        msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
-        intf_UserFatal( p_aout, false, _("Audio filtering failed"),
-                        _("The maximum number of filters (%d) was reached."),
-                        AOUT_MAX_FILTERS );
-        return -1;
+        ChangeFiltersString (obj, "audio-visual", "visual", false);
+        ChangeFiltersString (obj, "audio-visual", "goom", false);
+        ChangeFiltersString (obj, "audio-visual", "projectm", true);
+        ChangeFiltersString (obj, "audio-visual", "vsxu", false);
     }
-    pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
-                                p_output_format );
-    if ( pp_filters[1] == NULL )
+    else if (!strcmp ("vsxu", mode))
     {
-        /* Try to split the conversion. */
-        i_nb_conversions = SplitConversion( &pp_filters[0]->output,
-                                           p_output_format, &temp_format );
-        if ( !i_nb_conversions )
-        {
-            ReleaseFilter( pp_filters[0] );
-            msg_Err( p_aout,
-              "couldn't find a filter for the second part of the conversion" );
-            return -1;
-        }
-        if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
-        {
-            ReleaseFilter( pp_filters[0] );
-            msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
-            intf_UserFatal( p_aout, false, _("Audio filtering failed"),
-                            _("The maximum number of filters (%d) was reached."),
-                            AOUT_MAX_FILTERS );
-            return -1;
-        }
-        pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
-                                    &temp_format );
-        pp_filters[2] = FindFilter( p_aout, &temp_format,
-                                    p_output_format );
-
-        if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
-        {
-            ReleaseFilter( pp_filters[0] );
-            if ( pp_filters[1] != NULL )
-            {
-                ReleaseFilter( pp_filters[1] );
-            }
-            if ( pp_filters[2] != NULL )
-            {
-                ReleaseFilter( pp_filters[2] );
-            }
-            msg_Err( p_aout,
-               "couldn't find filters for the second part of the conversion" );
-            return -1;
-        }
-        *pi_nb_filters += 3;
-        msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
+        ChangeFiltersString (obj, "audio-visual", "visual", false);
+        ChangeFiltersString (obj, "audio-visual", "goom", false);
+        ChangeFiltersString (obj, "audio-visual", "projectm", false);
+        ChangeFiltersString (obj, "audio-visual", "vsxu", true);
     }
     else
     {
-        *pi_nb_filters += 2;
-        msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
+        var_Create (obj, "effect-list", VLC_VAR_STRING);
+        var_SetString (obj, "effect-list", mode);
+
+        ChangeFiltersString (obj, "audio-visual", "goom", false);
+        ChangeFiltersString (obj, "audio-visual", "visual", true);
+        ChangeFiltersString (obj, "audio-visual", "projectm", false);
     }
 
-    return 0;
+    aout_InputRequestRestart (aout);
+    (void) var; (void) oldval; (void) data;
+    return VLC_SUCCESS;
 }
 
-/*****************************************************************************
- * aout_FiltersDestroyPipeline: deallocate a filters pipeline
- *****************************************************************************/
-void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
-                                  aout_filter_t ** pp_filters,
-                                  int i_nb_filters )
+static int EqualizerCallback (vlc_object_t *obj, char const *var,
+                              vlc_value_t oldval, vlc_value_t newval,
+                              void *data)
 {
-    int i;
-    (void)p_aout;
+    audio_output_t *aout = (audio_output_t *)obj;
+    char *mode = newval.psz_string;
+    bool ret;
 
-    for ( i = 0; i < i_nb_filters; i++ )
+    if (!*mode)
+        ret = ChangeFiltersString (obj, "audio-filter", "equalizer", false);
+    else
     {
-        module_Unneed( pp_filters[i], pp_filters[i]->p_module );
-        vlc_object_detach( pp_filters[i] );
-        vlc_object_release( pp_filters[i] );
+        var_Create (obj, "equalizer-preset", VLC_VAR_STRING);
+        var_SetString (obj, "equalizer-preset", mode);
+        ret = ChangeFiltersString (obj, "audio-filter", "equalizer", true);
     }
+
+    /* That sucks */
+    if (ret)
+        aout_InputRequestRestart (aout);
+    (void) var; (void) oldval; (void) data;
+    return VLC_SUCCESS;
 }
 
-/*****************************************************************************
- * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
- *                          buffer allocations
- *****************************************************************************/
-void aout_FiltersHintBuffers( aout_instance_t * p_aout,
-                              aout_filter_t ** pp_filters,
-                              int i_nb_filters, aout_alloc_t * p_first_alloc )
+static vout_thread_t *RequestVout (void *data, vout_thread_t *vout,
+                                   video_format_t *fmt, bool recycle)
 {
-    int i;
+    audio_output_t *aout = data;
+    vout_configuration_t cfg = {
+        .vout       = vout,
+        .input      = NULL,
+        .change_fmt = true,
+        .fmt        = fmt,
+        .dpb_size   = 1,
+    };
+
+    (void) recycle;
+    return vout_Request (aout, &cfg);
+}
 
-    (void)p_aout; /* unused */
+vout_thread_t *aout_filter_RequestVout (filter_t *filter, vout_thread_t *vout,
+                                        video_format_t *fmt)
+{
+    /* NOTE: This only works from audio output.
+     * If you want to use visualization filters from another place, you will
+     * need to add a new pf_aout_request_vout callback or store a pointer
+     * to aout_request_vout_t inside filter_t (i.e. a level of indirection). */
+    aout_owner_t *owner = aout_owner ((audio_output_t *)filter->p_parent);
+    aout_request_vout_t *req = &owner->request_vout;
+
+    return req->pf_request_vout (req->p_private, vout, fmt,
+                                 owner->recycle_vout);
+}
 
-    for ( i = i_nb_filters - 1; i >= 0; i-- )
-    {
-        aout_filter_t * p_filter = pp_filters[i];
+static filter_t *CreateFilter (vlc_object_t *parent, const char *name,
+                               const audio_sample_format_t *restrict infmt,
+                               const audio_sample_format_t *restrict outfmt,
+                               bool visu)
+{
+    filter_t *filter = vlc_custom_create (parent, sizeof (*filter),
+                                          "audio filter");
+    if (unlikely(filter == NULL))
+        return NULL;
 
-        int i_output_size = p_filter->output.i_bytes_per_frame
-                             * p_filter->output.i_rate * AOUT_MAX_INPUT_RATE
-                             / p_filter->output.i_frame_length;
-        int i_input_size = p_filter->input.i_bytes_per_frame
-                             * p_filter->input.i_rate * AOUT_MAX_INPUT_RATE
-                             / p_filter->input.i_frame_length;
+    /*filter->p_owner = NOT NEEDED;*/
+    filter->fmt_in.i_codec = infmt->i_format;
+    filter->fmt_in.audio = *infmt;
+    filter->fmt_out.i_codec = outfmt->i_format;
+    filter->fmt_out.audio = *outfmt;
 
-        p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
-                                                i_output_size );
+    if (!visu)
+    {
+        filter->p_module = module_need (filter, "audio filter", name, true);
+        if (filter->p_module != NULL)
+            return filter;
 
-        if ( p_filter->b_in_place )
-        {
-            p_first_alloc->i_bytes_per_sec = __MAX(
-                                         p_first_alloc->i_bytes_per_sec,
-                                         i_input_size );
-            p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
-        }
-        else
-        {
-            /* We're gonna need a buffer allocation. */
-            memcpy( &p_filter->output_alloc, p_first_alloc,
-                    sizeof(aout_alloc_t) );
-            p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
-            p_first_alloc->i_bytes_per_sec = i_input_size;
-        }
+        /* If probing failed, formats shall not have been modified. */
+        assert (AOUT_FMTS_IDENTICAL(&filter->fmt_in.audio, infmt));
+        assert (AOUT_FMTS_IDENTICAL(&filter->fmt_out.audio, outfmt));
     }
+
+    filter->p_module = module_need (filter, "visualization2", name, true);
+    if (filter->p_module != NULL)
+        return filter;
+
+    vlc_object_release (filter);
+    return NULL;
 }
 
-/*****************************************************************************
- * aout_FiltersPlay: play a buffer
- *****************************************************************************/
-void aout_FiltersPlay( aout_instance_t * p_aout,
-                       aout_filter_t ** pp_filters,
-                       int i_nb_filters, aout_buffer_t ** pp_input_buffer )
+/**
+ * Sets up the audio filters.
+ */
+int aout_FiltersNew (audio_output_t *aout,
+                     const audio_sample_format_t *restrict infmt,
+                     const audio_sample_format_t *restrict outfmt,
+                     const aout_request_vout_t *request_vout)
 {
-    int i;
+    aout_owner_t *owner = aout_owner (aout);
+
+    /* Prepare format structure */
+    aout_FormatPrint (aout, "input", infmt);
+    audio_sample_format_t input_format = *infmt;
+    audio_sample_format_t output_format = *outfmt;
+
+    /* Now add user filters */
+    owner->nb_filters = 0;
+    owner->rate_filter = NULL;
+    owner->resampler = NULL;
+
+    if (!AOUT_FMT_LINEAR(outfmt))
+        return 0;
+
+    var_AddCallback (aout, "visual", VisualizationCallback, NULL);
+    var_AddCallback (aout, "equalizer", EqualizerCallback, NULL);
+
+    const char *scaletempo =
+        var_InheritBool (aout, "audio-time-stretch") ? "scaletempo" : NULL;
+    char *filters = var_InheritString (aout, "audio-filter");
+    char *visual = var_InheritString (aout, "audio-visual");
+
+    if (request_vout != NULL)
+        owner->request_vout = *request_vout;
+    else
+    {
+        owner->request_vout.pf_request_vout = RequestVout;
+        owner->request_vout.p_private = aout;
+    }
+    owner->recycle_vout = (visual != NULL) && *visual;
+
+    /* parse user filter lists */
+    const char *list[AOUT_MAX_FILTERS];
+    unsigned n = 0;
 
-    for ( i = 0; i < i_nb_filters; i++ )
+    if (scaletempo != NULL)
+        list[n++] = scaletempo;
+    if (filters != NULL)
     {
-        aout_filter_t * p_filter = pp_filters[i];
-        aout_buffer_t * p_output_buffer;
-
-        /* Resamplers can produce slightly more samples than (i_in_nb *
-         * p_filter->output.i_rate / p_filter->input.i_rate) so we need
-         * slightly bigger buffers. */
-        aout_BufferAlloc( &p_filter->output_alloc,
-            ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
-            * 1000000 / p_filter->input.i_rate,
-            *pp_input_buffer, p_output_buffer );
-        if ( p_output_buffer == NULL )
+        char *p = filters, *name;
+        while ((name = strsep (&p, " :")) != NULL && n < AOUT_MAX_FILTERS)
+            list[n++] = name;
+    }
+    if (visual != NULL && n < AOUT_MAX_FILTERS)
+        list[n++] = visual;
+
+    for (unsigned i = 0; i < n; i++)
+    {
+        const char *name = list[i];
+
+        if (owner->nb_filters >= AOUT_MAX_FILTERS)
         {
-            msg_Err( p_aout, "out of memory" );
-            return;
+            msg_Err (aout, "maximum of %u filters reached", AOUT_MAX_FILTERS);
+            msg_Err (aout, "cannot add user filter %s (skipped)", name);
+            break;
         }
-        /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
-         * shall be set by the filter plug-in. */
 
-        p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
-                              p_output_buffer );
+        filter_t *filter = CreateFilter (VLC_OBJECT(aout), name,
+                                         &input_format, &output_format,
+                                         name == visual);
+        if (filter == NULL)
+        {
+            msg_Err (aout, "cannot add user filter %s (skipped)", name);
+            continue;
+        }
 
-        if ( !p_filter->b_in_place )
+        /* convert to the filter input format if necessary */
+        if (aout_FiltersCreatePipeline (VLC_OBJECT(aout), owner->filters,
+                                        &owner->nb_filters,
+                                        AOUT_MAX_FILTERS - 1,
+                                        &input_format, &filter->fmt_in.audio))
         {
-            aout_BufferFree( *pp_input_buffer );
-            *pp_input_buffer = p_output_buffer;
+            msg_Err (aout, "cannot add user filter %s (skipped)", name);
+            module_unneed (filter, filter->p_module);
+            vlc_object_release (filter);
+            continue;
         }
 
-        if( p_output_buffer->i_nb_samples <= 0 )
-            break;
+        assert (owner->nb_filters < AOUT_MAX_FILTERS);
+        owner->filters[owner->nb_filters++] = filter;
+        input_format = filter->fmt_out.audio;
+
+        if (name == scaletempo)
+            owner->rate_filter = filter;
+    }
+    free (visual);
+    free (filters);
+
+    /* convert to the output format (minus resampling) if necessary */
+    output_format.i_rate = input_format.i_rate;
+    if (aout_FiltersCreatePipeline (VLC_OBJECT(aout), owner->filters,
+                                    &owner->nb_filters, AOUT_MAX_FILTERS,
+                                    &input_format, &output_format))
+    {
+        msg_Err (aout, "cannot setup filtering pipeline");
+        goto error;
     }
+    input_format = output_format;
+
+    /* insert the resampler */
+    output_format.i_rate = outfmt->i_rate;
+    assert (AOUT_FMTS_IDENTICAL(&output_format, outfmt));
+
+    unsigned rate_bak = input_format.i_rate;
+    if (output_format.i_rate == input_format.i_rate)
+        /* For historical reasons, a different rate is required to probe
+         * resampling filters. */
+        input_format.i_rate++;
+    owner->resampler = FindFilter (VLC_OBJECT(aout), &input_format,
+                                   &output_format);
+    if (owner->resampler == NULL)
+    {
+        msg_Err (aout, "cannot setup a resampler");
+        goto error;
+    }
+    owner->resampler->fmt_in.audio.i_rate = rate_bak;
+    if (owner->rate_filter == NULL)
+        owner->rate_filter = owner->resampler;
+
+    return 0;
+
+error:
+    aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
+    var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
+    var_DelCallback (aout, "visual", VisualizationCallback, NULL);
+    return -1;
 }
 
+/**
+ * Destroys the audio filters.
+ */
+void aout_FiltersDelete (audio_output_t *aout)
+{
+    aout_owner_t *owner = aout_owner (aout);
+
+    if (owner->resampler != NULL)
+        aout_FiltersDestroyPipeline (&owner->resampler, 1);
+    aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
+    var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
+    var_DelCallback (aout, "visual", VisualizationCallback, NULL);
+
+    /* XXX We need to update recycle_vout before calling
+     * aout_FiltersDestroyPipeline().
+     * FIXME There may be a race condition if audio-visual is updated between
+     * aout_FiltersDestroy() and the next aout_FiltersNew().
+     */
+    char *visual = var_InheritString (aout, "audio-visual");
+    owner->recycle_vout = (visual != NULL) && *visual;
+    free (visual);
+}