/*****************************************************************************
* filters.c : audio output filters management
*****************************************************************************
- * Copyright (C) 2002 VideoLAN
- * $Id: filters.c,v 1.5 2002/08/19 21:31:11 massiot Exp $
+ * Copyright (C) 2002-2007 the VideoLAN team
+ * $Id$
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <stdlib.h> /* calloc(), malloc(), free() */
-#include <string.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
-#include <vlc/vlc.h>
+#include <assert.h>
-#ifdef HAVE_ALLOCA_H
-# include <alloca.h>
-#endif
+#include <vlc_common.h>
+#include <vlc_dialog.h>
+#include <vlc_modules.h>
-#include "audio_output.h"
+#include <vlc_aout.h>
+#include <vlc_filter.h>
+#include <vlc_cpu.h>
#include "aout_internal.h"
+#include <libvlc.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 * p_input_format,
+ const audio_sample_format_t * p_output_format )
{
- aout_filter_t * p_filter = vlc_object_create( p_aout,
- sizeof(aout_filter_t) );
+ static const char typename[] = "audio filter";
+ 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,
+ 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->p_module = module_Need( p_filter, "audio filter", NULL );
+ p_filter->fmt_out.i_codec = p_output_format->i_format;
+ p_filter->p_owner = NULL;
+
+ p_filter->p_module = module_need( p_filter, "audio filter", NULL, false );
if ( p_filter->p_module == NULL )
{
- vlc_object_detach( p_filter );
- vlc_object_destroy( p_filter );
+ vlc_object_release( p_filter );
return NULL;
}
+ 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( aout_instance_t * p_aout,
- const audio_sample_format_t * p_input_format,
- const audio_sample_format_t * p_output_format,
- audio_sample_format_t * p_middle_format,
- boolean_t b_format_first,
- boolean_t b_rate_first )
+/**
+ * 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 )
{
- boolean_t b_format =
- (p_input_format->i_format != p_output_format->i_format);
- boolean_t b_rate = (p_input_format->i_rate != p_output_format->i_rate);
- boolean_t b_channels =
- (p_input_format->i_channels != p_output_format->i_channels);
- int i_nb_conversions = b_format + b_rate + b_channels;
-
- if ( i_nb_conversions <= 1 ) return 0;
-
- memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
-
- if ( i_nb_conversions == 2 )
- {
- if ( !b_format )
- {
- if ( b_rate_first )
- {
- p_middle_format->i_channels = p_input_format->i_channels;
- }
- else
- {
- p_middle_format->i_rate = p_input_format->i_rate;
- }
- return 1;
- }
-
- if ( !b_rate )
- {
- if ( b_format_first )
- {
- p_middle_format->i_channels = p_input_format->i_channels;
- }
- else
- {
- p_middle_format->i_format = p_input_format->i_format;
- }
- return 1;
- }
-
- /* !b_channels */
- if ( b_format_first )
- {
- p_middle_format->i_rate = p_input_format->i_rate;
- }
- else
- {
- p_middle_format->i_format = p_input_format->i_format;
- }
- return 1;
- }
+ *midfmt = *outfmt;
- /* i_nb_conversion == 3 */
- if ( !b_format_first )
- {
- p_middle_format->i_format = p_input_format->i_format;
- }
- else if ( !b_rate_first )
+ if( infmt->i_rate != outfmt->i_rate )
+ midfmt->i_rate = infmt->i_rate;
+ else
+ if( infmt->i_physical_channels != outfmt->i_physical_channels
+ || infmt->i_original_channels != outfmt->i_original_channels )
{
- p_middle_format->i_channels = p_input_format->i_channels;
+ midfmt->i_physical_channels = infmt->i_physical_channels;
+ midfmt->i_original_channels = infmt->i_original_channels;
}
else
{
- p_middle_format->i_rate = p_input_format->i_rate;
+ assert( infmt->i_format != outfmt->i_format );
+ if( AOUT_FMT_LINEAR( infmt ) )
+ /* NOTE: Use S16N as intermediate. We have all conversions to S16N,
+ * and all useful conversions from S16N. TODO: FL32 if HAVE_FPU. */
+ midfmt->i_format = VLC_CODEC_S16N;
+ else
+ if( AOUT_FMT_LINEAR( outfmt ) )
+ /* NOTE: our non-linear -> linear filters always output 32-bits */
+ midfmt->i_format = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_FI32;
+ else
+ return -1; /* no indirect non-linear -> non-linear */
}
- return 2;
+ aout_FormatPrepare( midfmt );
+ return AOUT_FMTS_IDENTICAL( infmt, midfmt ) ? -1 : 0;
}
-/*****************************************************************************
- * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
- * format to another
- *****************************************************************************
- * TODO : allow the user to add/remove specific filters
- *****************************************************************************/
-int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
- aout_filter_t ** pp_filters,
- int * pi_nb_filters,
- const audio_sample_format_t * p_input_format,
- const audio_sample_format_t * p_output_format )
+#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 temp_format;
- boolean_t b_format_first, b_rate_first;
+ audio_sample_format_t curfmt = *outfmt;
+ unsigned i = 0, max = *nb_filters - AOUT_MAX_FILTERS;
- if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
- {
- msg_Dbg( p_aout, "no need for any filter" );
- *pi_nb_filters = 0;
- return 0;
- }
-
- msg_Dbg( p_aout, "filter(s) format=%d->%d rate=%d->%d channels=%d->%d",
- p_input_format->i_format, p_output_format->i_format,
- p_input_format->i_rate, p_output_format->i_rate,
- p_input_format->i_channels, p_output_format->i_channels );
+ filters += *nb_filters;
+ aout_FormatsPrint( obj, "filter(s)", infmt, outfmt );
- /* 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 )
+ while( !AOUT_FMTS_IDENTICAL( infmt, &curfmt ) )
{
- msg_Dbg( p_aout, "found a filter for the whole conversion" );
- *pi_nb_filters = 1;
- return 0;
- }
-
- /* We'll have to split the conversion. We always to the downmixing
- * before the resampling, and the upmixing after the resampling (to
- * maximize the resampling efficiency). */
- b_rate_first = (p_input_format->i_channels < p_output_format->i_channels);
-
- for ( b_format_first = 1; b_format_first >= 0; b_format_first-- )
- {
- int i_nb_conversions = SplitConversion( p_aout, p_input_format,
- p_output_format, &temp_format,
- b_format_first, b_rate_first );
- if ( !i_nb_conversions )
+ if( i >= max )
{
- /* 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;
+ 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;
}
- 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_aout, p_input_format, &temp_format,
- &temp_format, b_format_first, b_rate_first );
- pp_filters[0] = FindFilter( p_aout, p_input_format,
- &temp_format );
- }
- if ( pp_filters[0] == NULL )
+ /* Make room and prepend a filter */
+ memmove( filters + 1, filters, i * sizeof( *filters ) );
+
+ *filters = FindFilter( obj, infmt, &curfmt );
+ if( *filters != NULL )
{
- /* Retry with b_format_first = 0. */
- continue;
+ i++;
+ break; /* done! */
}
- /* We have the first stage of the conversion. Find a filter for
- * the rest. */
- pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
- p_output_format );
- if ( pp_filters[1] == NULL )
+ audio_sample_format_t midfmt;
+ /* Split the conversion */
+ if( SplitConversion( infmt, &curfmt, &midfmt ) )
{
- /* Try to split the conversion. */
- i_nb_conversions = SplitConversion( p_aout,
- &pp_filters[0]->output,
- p_output_format, &temp_format,
- b_format_first, b_rate_first );
- if ( !i_nb_conversions )
- {
- vlc_object_detach( pp_filters[0] );
- vlc_object_destroy( pp_filters[0] );
- continue;
- }
- 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 )
- {
- vlc_object_detach( pp_filters[0] );
- vlc_object_destroy( pp_filters[0] );
- if ( pp_filters[1] != NULL )
- {
- vlc_object_detach( pp_filters[1] );
- vlc_object_destroy( pp_filters[1] );
- }
- if ( pp_filters[2] != NULL )
- {
- vlc_object_detach( pp_filters[2] );
- vlc_object_destroy( pp_filters[2] );
- }
- continue;
- }
- *pi_nb_filters = 3;
+ msg_Err( obj, "conversion pipeline failed: %4.4s -> %4.4s",
+ (const char *)&infmt->i_format,
+ (const char *)&outfmt->i_format );
+ goto rollback;
}
- else
+
+ *filters = FindFilter( obj, &midfmt, &curfmt );
+ if( *filters == NULL )
{
- *pi_nb_filters = 2;
+ msg_Err( obj, "cannot find filter for simple conversion" );
+ goto rollback;
}
-
- /* We have enough filters. */
- msg_Dbg( p_aout, "found %d filters for the whole conversion",
- *pi_nb_filters );
- return 0;
+ curfmt = midfmt;
+ i++;
}
- msg_Err( p_aout, "couldn't find filters for the conversion" );
+ msg_Dbg( obj, "conversion pipeline completed" );
+ *nb_filters += i;
+ return 0;
+
+rollback:
+ aout_FiltersDestroyPipeline( filters, i );
return -1;
}
-/*****************************************************************************
- * aout_FiltersDestroyPipeline: deallocate a filters pipeline
- *****************************************************************************/
-void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
- aout_filter_t ** pp_filters,
- int i_nb_filters )
+/**
+ * Destroys a chain of audio filters.
+ */
+void aout_FiltersDestroyPipeline( filter_t *const *filters, unsigned n )
{
- int i;
-
- for ( i = 0; i < i_nb_filters; i++ )
+ for( unsigned i = 0; i < n; i++ )
{
- module_Unneed( pp_filters[i], pp_filters[i]->p_module );
- vlc_object_detach( pp_filters[i] );
- vlc_object_destroy( pp_filters[i] );
- }
-}
-
-/*****************************************************************************
- * 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 )
-{
- int i;
+ filter_t *p_filter = filters[i];
- for ( i = i_nb_filters - 1; i >= 0; i-- )
- {
- aout_filter_t * p_filter = pp_filters[i];
-
- int i_output_size = p_filter->output.i_bytes_per_frame
- * p_filter->output.i_rate
- / p_filter->output.i_frame_length;
- int i_input_size = p_filter->input.i_bytes_per_frame
- * p_filter->input.i_rate
- / p_filter->input.i_frame_length;
-
- p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
- i_output_size );
-
- if ( p_filter->b_in_place )
- {
- p_first_alloc->i_bytes_per_sec = __MAX(
- p_first_alloc->i_bytes_per_sec,
- i_input_size );
- }
- 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;
- }
+ module_unneed( p_filter, p_filter->p_module );
+ free( p_filter->p_owner );
+ vlc_object_release( p_filter );
}
}
-/*****************************************************************************
- * 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 )
+/**
+ * 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 )
{
- int i;
+ block_t *p_block = *pp_block;
- for ( i = 0; i < i_nb_filters; i++ )
+ /* TODO: use filter chain */
+ for( unsigned i = 0; i < i_nb_filters; i++ )
{
- aout_filter_t * p_filter = pp_filters[i];
- aout_buffer_t * p_output_buffer;
+ filter_t * p_filter = pp_filters[i];
- aout_BufferAlloc( &p_filter->output_alloc,
- (mtime_t)(*pp_input_buffer)->i_nb_samples * 1000000
- / p_filter->output.i_rate, *pp_input_buffer,
- p_output_buffer );
- if ( p_output_buffer == NULL )
- {
- msg_Err( p_aout, "out of memory" );
- return;
- }
- /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
+ /* Please note that p_block->i_nb_samples & i_buffer
* shall be set by the filter plug-in. */
-
- p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
- p_output_buffer );
-
- if ( !p_filter->b_in_place )
- {
- aout_BufferFree( *pp_input_buffer );
- *pp_input_buffer = p_output_buffer;
- }
+ p_block = p_filter->pf_audio_filter( p_filter, p_block );
}
+ *pp_block = p_block;
}
-