1 /*****************************************************************************
2 * filters.c : audio output filters management
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_dialog.h>
35 #include <vlc_modules.h>
38 #include <vlc_filter.h>
40 #include "aout_internal.h"
43 /*****************************************************************************
44 * FindFilter: find an audio filter for a specific transformation
45 *****************************************************************************/
46 static filter_t * FindFilter( vlc_object_t *obj,
47 const audio_sample_format_t *infmt,
48 const audio_sample_format_t *outfmt )
50 static const char typename[] = "audio converter";
51 const char *type = "audio converter", *name = NULL;
54 p_filter = vlc_custom_create( obj, sizeof(*p_filter), typename );
56 if ( p_filter == NULL ) return NULL;
58 p_filter->fmt_in.audio = *infmt;
59 p_filter->fmt_in.i_codec = infmt->i_format;
60 p_filter->fmt_out.audio = *outfmt;
61 p_filter->fmt_out.i_codec = outfmt->i_format;
63 if( infmt->i_format == outfmt->i_format
64 && infmt->i_physical_channels == outfmt->i_physical_channels
65 && infmt->i_original_channels == outfmt->i_original_channels )
67 assert( infmt->i_rate != outfmt->i_rate );
68 type = "audio resampler";
69 name = "$audio-resampler";
72 p_filter->p_module = module_need( p_filter, type, name, false );
73 if ( p_filter->p_module == NULL )
75 vlc_object_release( p_filter );
79 assert( p_filter->pf_audio_filter );
84 * Splits audio format conversion in two simpler conversions
85 * @return 0 on successful split, -1 if the input and output formats are too
86 * similar to split the conversion.
88 static int SplitConversion( const audio_sample_format_t *restrict infmt,
89 const audio_sample_format_t *restrict outfmt,
90 audio_sample_format_t *midfmt )
94 /* Lastly: resample (after format conversion and remixing) */
95 if( infmt->i_rate != outfmt->i_rate )
96 midfmt->i_rate = infmt->i_rate;
98 /* Penultimately: remix channels (after format conversion) */
99 if( infmt->i_physical_channels != outfmt->i_physical_channels
100 || infmt->i_original_channels != outfmt->i_original_channels )
102 midfmt->i_physical_channels = infmt->i_physical_channels;
103 midfmt->i_original_channels = infmt->i_original_channels;
106 /* Second: convert linear to S16N as intermediate format */
107 if( AOUT_FMT_LINEAR( infmt ) )
109 /* All conversion from linear to S16N must be supported directly. */
110 if( outfmt->i_format == VLC_CODEC_S16N )
112 midfmt->i_format = VLC_CODEC_S16N;
115 /* First: convert non-linear to FI32 as intermediate format */
117 if( outfmt->i_format == VLC_CODEC_FI32 )
119 midfmt->i_format = VLC_CODEC_FI32;
122 assert( !AOUT_FMTS_IDENTICAL( infmt, midfmt ) );
123 aout_FormatPrepare( midfmt );
127 #undef aout_FiltersCreatePipeline
129 * Allocates audio format conversion filters
130 * @param obj parent VLC object for new filters
131 * @param filters table of filters [IN/OUT]
132 * @param nb_filters pointer to the number of filters in the table [IN/OUT]
133 * @param max_filters size of filters table [IN]
134 * @param infmt input audio format
135 * @param outfmt output audio format
136 * @return 0 on success, -1 on failure
138 int aout_FiltersCreatePipeline( vlc_object_t *obj, filter_t **filters,
139 unsigned *nb_filters, unsigned max_filters,
140 const audio_sample_format_t *restrict infmt,
141 const audio_sample_format_t *restrict outfmt )
143 audio_sample_format_t curfmt = *outfmt;
146 max_filters -= *nb_filters;
147 filters += *nb_filters;
148 aout_FormatsPrint( obj, "filter(s)", infmt, outfmt );
150 while( !AOUT_FMTS_IDENTICAL( infmt, &curfmt ) )
152 if( i >= max_filters )
154 msg_Err( obj, "maximum of %u filters reached", max_filters );
155 dialog_Fatal( obj, _("Audio filtering failed"),
156 _("The maximum number of filters (%u) was reached."),
161 /* Make room and prepend a filter */
162 memmove( filters + 1, filters, i * sizeof( *filters ) );
164 *filters = FindFilter( obj, infmt, &curfmt );
165 if( *filters != NULL )
171 audio_sample_format_t midfmt;
172 /* Split the conversion */
173 if( SplitConversion( infmt, &curfmt, &midfmt ) )
175 msg_Err( obj, "conversion pipeline failed: %4.4s -> %4.4s",
176 (const char *)&infmt->i_format,
177 (const char *)&outfmt->i_format );
181 *filters = FindFilter( obj, &midfmt, &curfmt );
182 if( *filters == NULL )
184 msg_Err( obj, "cannot find filter for simple conversion" );
191 msg_Dbg( obj, "conversion pipeline completed" );
196 aout_FiltersDestroyPipeline( filters, i );
201 * Destroys a chain of audio filters.
203 void aout_FiltersDestroyPipeline( filter_t *const *filters, unsigned n )
205 for( unsigned i = 0; i < n; i++ )
207 filter_t *p_filter = filters[i];
209 module_unneed( p_filter, p_filter->p_module );
210 vlc_object_release( p_filter );
215 * Filters an audio buffer through a chain of filters.
217 void aout_FiltersPlay( filter_t *const *pp_filters,
218 unsigned i_nb_filters, block_t ** pp_block )
220 block_t *p_block = *pp_block;
222 /* TODO: use filter chain */
223 for( unsigned i = 0; (i < i_nb_filters) && (p_block != NULL); i++ )
225 filter_t * p_filter = pp_filters[i];
227 /* Please note that p_block->i_nb_samples & i_buffer
228 * shall be set by the filter plug-in. */
229 p_block = p_filter->pf_audio_filter( p_filter, p_block );