]> git.sesse.net Git - vlc/blobdiff - src/audio_output/filters.c
input: use unsigned atomic value
[vlc] / src / audio_output / filters.c
index 3340a4e6f2b99faa064ef227bef30dc30df02a61..fdaad8e979725e1a1bc3654ed78c0b024048ce58 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 <vlc_aout.h>
 #include <vlc_filter.h>
+#include <vlc_cpu.h>
 #include "aout_internal.h"
 #include <libvlc.h>
 
-block_t *aout_FilterBufferNew( filter_t *p_filter, int size )
-{
-    (void) p_filter;
-    return block_Alloc( size );
-}
-
 /*****************************************************************************
  * FindFilter: find an audio filter for a specific transformation
  *****************************************************************************/
-static 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 )
 {
     static const char typename[] = "audio filter";
+    const char *type = "audio filter", *name = NULL;
     filter_t * p_filter;
 
-    p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
-                                  VLC_OBJECT_GENERIC, typename );
+    p_filter = vlc_custom_create( obj, sizeof(*p_filter), typename );
 
     if ( p_filter == NULL ) return NULL;
 
-    memcpy( &p_filter->fmt_in.audio, p_input_format,
-            sizeof(audio_sample_format_t) );
-    p_filter->fmt_in.i_codec = p_input_format->i_format;
-    memcpy( &p_filter->fmt_out.audio, p_output_format,
-            sizeof(audio_sample_format_t) );
-    p_filter->fmt_out.i_codec = p_output_format->i_format;
-    p_filter->pf_audio_buffer_new = aout_FilterBufferNew;
+    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, "audio filter", NULL, false );
+    p_filter->p_module = module_need( p_filter, type, name, false );
     if ( p_filter->p_module == NULL )
     {
         vlc_object_release( p_filter );
@@ -79,198 +80,120 @@ static filter_t * FindFilter( aout_instance_t * p_aout,
     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;
-
-    if ( i_nb_conversions <= 1 ) return 0;
+    *midfmt = *outfmt;
 
-    memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
-
-    if ( i_nb_conversions == 2 )
+    /* 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 )
     {
-        if ( !b_format || !b_channels )
-        {
-            p_middle_format->i_rate = p_input_format->i_rate;
-            aout_FormatPrepare( p_middle_format );
-            return 1;
-        }
-
-        /* !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;
+        midfmt->i_physical_channels = infmt->i_physical_channels;
+        midfmt->i_original_channels = infmt->i_original_channels;
     }
-
-    /* i_nb_conversion == 3 */
-    p_middle_format->i_rate = p_input_format->i_rate;
-    aout_FormatPrepare( p_middle_format );
-    return 2;
-}
-
-static void ReleaseFilter( filter_t * p_filter )
-{
-    module_unneed( p_filter, p_filter->p_module );
-    vlc_object_release( p_filter );
-}
-
-/*****************************************************************************
- * 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,
-                                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 )
-{
-    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 ) )
+    else
+    /* Second: convert linear to S16N as intermediate format */
+    if( AOUT_FMT_LINEAR( infmt ) )
     {
-        msg_Dbg( p_aout, "no need for any filter" );
-        return 0;
+        /* 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;
     }
-
-    aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
-
-    if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
+    else
+    /* First: convert non-linear to FI32 as intermediate format */
     {
-        msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
-        dialog_Fatal( p_aout, _("Audio filtering failed"),
-                      _("The maximum number of filters (%d) was reached."),
-                      AOUT_MAX_FILTERS );
-        return -1;
+        if( outfmt->i_format == VLC_CODEC_FI32 )
+            return -1;
+        midfmt->i_format = VLC_CODEC_FI32;
     }
 
-    /* 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 )
-    {
-        msg_Dbg( p_aout, "found a filter for the whole conversion" );
-        ++*pi_nb_filters;
-        return 0;
-    }
+    assert( !AOUT_FMTS_IDENTICAL( infmt, midfmt ) );
+    aout_FormatPrepare( midfmt );
+    return 0;
+}
 
-    /* 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 "
-                "%4.4s -> %4.4s",
-                (const char *)&p_input_format->i_format,
-                (const char *)&p_output_format->i_format );
-        return -1;
-    }
+#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 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,
+                                int *nb_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 = *nb_filters - AOUT_MAX_FILTERS;
 
-    pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
-    if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
-    {
-        /* Try with only one conversion. */
-        SplitConversion( p_input_format, &temp_format, &temp_format );
-        pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
-    }
-    if ( pp_filters[0] == NULL )
-    {
-        msg_Err( p_aout,
-              "couldn't find a filter for the first part of the conversion" );
-        return -1;
-    }
+    filters += *nb_filters;
+    aout_FormatsPrint( obj, "filter(s)", infmt, outfmt );
 
-    /* We have the first stage of the conversion. Find a filter for
-     * the rest. */
-    if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
+    while( !AOUT_FMTS_IDENTICAL( infmt, &curfmt ) )
     {
-        ReleaseFilter( pp_filters[0] );
-        msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
-        dialog_Fatal( p_aout, _("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]->fmt_out.audio,
-                                p_output_format );
-    if ( pp_filters[1] == NULL )
-    {
-        /* Try to split the conversion. */
-        i_nb_conversions = SplitConversion( &pp_filters[0]->fmt_out.audio,
-                                           p_output_format, &temp_format );
-        if ( !i_nb_conversions )
+        if( i >= max )
         {
-            ReleaseFilter( pp_filters[0] );
-            msg_Err( p_aout,
-              "couldn't find a filter for the second part of the conversion" );
-            return -1;
+            msg_Err( obj, "max (%u) filters reached", AOUT_MAX_FILTERS );
+            dialog_Fatal( obj, _("Audio filtering failed"),
+                          _("The maximum number of filters (%u) was reached."),
+                          AOUT_MAX_FILTERS );
+            goto rollback;
         }
-        if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
+
+        /* Make room and prepend a filter */
+        memmove( filters + 1, filters, i * sizeof( *filters ) );
+
+        *filters = FindFilter( obj, infmt, &curfmt );
+        if( *filters != NULL )
         {
-            ReleaseFilter( pp_filters[0] );
-            msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
-            dialog_Fatal( p_aout, _("Audio filtering failed"),
-                          _("The maximum number of filters (%d) was reached."),
-                          AOUT_MAX_FILTERS );
-            return -1;
+            i++;
+            break; /* done! */
         }
-        pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->fmt_out.audio,
-                                    &temp_format );
-        pp_filters[2] = FindFilter( p_aout, &temp_format,
-                                    p_output_format );
 
-        if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
+        audio_sample_format_t midfmt;
+        /* Split the conversion */
+        if( SplitConversion( infmt, &curfmt, &midfmt ) )
         {
-            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;
+            msg_Err( obj, "conversion pipeline failed: %4.4s -> %4.4s",
+                     (const char *)&infmt->i_format,
+                     (const char *)&outfmt->i_format );
+            goto rollback;
         }
-        *pi_nb_filters += 3;
-        msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
-    }
-    else
-    {
-        *pi_nb_filters += 2;
-        msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
+
+        *filters = FindFilter( obj, &midfmt, &curfmt );
+        if( *filters == NULL )
+        {
+            msg_Err( obj, "cannot find filter for simple conversion" );
+            goto rollback;
+        }
+        curfmt = midfmt;
+        i++;
     }
 
+    msg_Dbg( obj, "conversion pipeline completed" );
+    *nb_filters += i;
     return 0;
+
+rollback:
+    aout_FiltersDestroyPipeline( filters, i );
+    return -1;
 }
 
 /**
@@ -283,7 +206,6 @@ void aout_FiltersDestroyPipeline( filter_t *const *filters, unsigned n )
         filter_t *p_filter = filters[i];
 
         module_unneed( p_filter, p_filter->p_module );
-        free( p_filter->p_owner );
         vlc_object_release( p_filter );
     }
 }
@@ -297,7 +219,7 @@ void aout_FiltersPlay( filter_t *const *pp_filters,
     block_t *p_block = *pp_block;
 
     /* TODO: use filter chain */
-    for( unsigned i = 0; i < i_nb_filters; i++ )
+    for( unsigned i = 0; (i < i_nb_filters) && (p_block != NULL); i++ )
     {
         filter_t * p_filter = pp_filters[i];