1 /*****************************************************************************
2 * filters.c : audio output filters management
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_interface.h>
39 #include "aout_internal.h"
42 /*****************************************************************************
43 * FindFilter: find an audio filter for a specific transformation
44 *****************************************************************************/
45 static aout_filter_t * FindFilter( aout_instance_t * p_aout,
46 const audio_sample_format_t * p_input_format,
47 const audio_sample_format_t * p_output_format )
49 static const char typename[] = "audio output";
50 aout_filter_t * p_filter;
52 p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
53 VLC_OBJECT_GENERIC, typename );
55 if ( p_filter == NULL ) return NULL;
56 vlc_object_attach( p_filter, p_aout );
58 memcpy( &p_filter->input, p_input_format, sizeof(audio_sample_format_t) );
59 memcpy( &p_filter->output, p_output_format,
60 sizeof(audio_sample_format_t) );
61 p_filter->p_module = module_Need( p_filter, "audio filter", NULL, 0 );
62 if ( p_filter->p_module == NULL )
64 vlc_object_detach( p_filter );
65 vlc_object_release( p_filter );
69 p_filter->b_continuity = false;
74 /*****************************************************************************
75 * SplitConversion: split a conversion in two parts
76 *****************************************************************************
77 * Returns the number of conversions required by the first part - 0 if only
78 * one conversion was asked.
79 * Beware : p_output_format can be modified during this function if the
80 * developer passed SplitConversion( toto, titi, titi, ... ). That is legal.
81 * SplitConversion( toto, titi, toto, ... ) isn't.
82 *****************************************************************************/
83 static int SplitConversion( const audio_sample_format_t * p_input_format,
84 const audio_sample_format_t * p_output_format,
85 audio_sample_format_t * p_middle_format )
88 (p_input_format->i_format != p_output_format->i_format);
89 bool b_rate = (p_input_format->i_rate != p_output_format->i_rate);
91 (p_input_format->i_physical_channels
92 != p_output_format->i_physical_channels)
93 || (p_input_format->i_original_channels
94 != p_output_format->i_original_channels);
95 int i_nb_conversions = b_format + b_rate + b_channels;
97 if ( i_nb_conversions <= 1 ) return 0;
99 memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
101 if ( i_nb_conversions == 2 )
103 if ( !b_format || !b_channels )
105 p_middle_format->i_rate = p_input_format->i_rate;
106 aout_FormatPrepare( p_middle_format );
111 p_middle_format->i_physical_channels
112 = p_input_format->i_physical_channels;
113 p_middle_format->i_original_channels
114 = p_input_format->i_original_channels;
115 aout_FormatPrepare( p_middle_format );
119 /* i_nb_conversion == 3 */
120 p_middle_format->i_rate = p_input_format->i_rate;
121 aout_FormatPrepare( p_middle_format );
125 static void ReleaseFilter( aout_filter_t * p_filter )
127 module_Unneed( p_filter, p_filter->p_module );
128 vlc_object_detach( p_filter );
129 vlc_object_release( p_filter );
132 /*****************************************************************************
133 * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
135 *****************************************************************************
136 * pi_nb_filters must be initialized before calling this function
137 *****************************************************************************/
138 int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
139 aout_filter_t ** pp_filters_start,
141 const audio_sample_format_t * p_input_format,
142 const audio_sample_format_t * p_output_format )
144 aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
145 audio_sample_format_t temp_format;
146 int i_nb_conversions;
148 if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
150 msg_Dbg( p_aout, "no need for any filter" );
154 aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
156 if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
158 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
159 intf_UserFatal( p_aout, false, _("Audio filtering failed"),
160 _("The maximum number of filters (%d) was reached."),
165 /* Try to find a filter to do the whole conversion. */
166 pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
167 if ( pp_filters[0] != NULL )
169 msg_Dbg( p_aout, "found a filter for the whole conversion" );
174 /* We'll have to split the conversion. We always do the downmixing
175 * before the resampling, because the audio decoder can probably do it
177 i_nb_conversions = SplitConversion( p_input_format,
178 p_output_format, &temp_format );
179 if ( !i_nb_conversions )
181 /* There was only one conversion to do, and we already failed. */
182 msg_Err( p_aout, "couldn't find a filter for the conversion" );
186 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
187 if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
189 /* Try with only one conversion. */
190 SplitConversion( p_input_format, &temp_format, &temp_format );
191 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
193 if ( pp_filters[0] == NULL )
196 "couldn't find a filter for the first part of the conversion" );
200 /* We have the first stage of the conversion. Find a filter for
202 if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
204 ReleaseFilter( pp_filters[0] );
205 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
206 intf_UserFatal( p_aout, false, _("Audio filtering failed"),
207 _("The maximum number of filters (%d) was reached."),
211 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
213 if ( pp_filters[1] == NULL )
215 /* Try to split the conversion. */
216 i_nb_conversions = SplitConversion( &pp_filters[0]->output,
217 p_output_format, &temp_format );
218 if ( !i_nb_conversions )
220 ReleaseFilter( pp_filters[0] );
222 "couldn't find a filter for the second part of the conversion" );
225 if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
227 ReleaseFilter( pp_filters[0] );
228 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
229 intf_UserFatal( p_aout, false, _("Audio filtering failed"),
230 _("The maximum number of filters (%d) was reached."),
234 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
236 pp_filters[2] = FindFilter( p_aout, &temp_format,
239 if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
241 ReleaseFilter( pp_filters[0] );
242 if ( pp_filters[1] != NULL )
244 ReleaseFilter( pp_filters[1] );
246 if ( pp_filters[2] != NULL )
248 ReleaseFilter( pp_filters[2] );
251 "couldn't find filters for the second part of the conversion" );
255 msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
260 msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
266 /*****************************************************************************
267 * aout_FiltersDestroyPipeline: deallocate a filters pipeline
268 *****************************************************************************/
269 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
270 aout_filter_t ** pp_filters,
276 for ( i = 0; i < i_nb_filters; i++ )
278 module_Unneed( pp_filters[i], pp_filters[i]->p_module );
279 vlc_object_detach( pp_filters[i] );
280 vlc_object_release( pp_filters[i] );
284 /*****************************************************************************
285 * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
287 *****************************************************************************/
288 void aout_FiltersHintBuffers( aout_instance_t * p_aout,
289 aout_filter_t ** pp_filters,
290 int i_nb_filters, aout_alloc_t * p_first_alloc )
294 (void)p_aout; /* unused */
296 for ( i = i_nb_filters - 1; i >= 0; i-- )
298 aout_filter_t * p_filter = pp_filters[i];
300 int i_output_size = p_filter->output.i_bytes_per_frame
301 * p_filter->output.i_rate * AOUT_MAX_INPUT_RATE
302 / p_filter->output.i_frame_length;
303 int i_input_size = p_filter->input.i_bytes_per_frame
304 * p_filter->input.i_rate * AOUT_MAX_INPUT_RATE
305 / p_filter->input.i_frame_length;
307 p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
310 if ( p_filter->b_in_place )
312 p_first_alloc->i_bytes_per_sec = __MAX(
313 p_first_alloc->i_bytes_per_sec,
315 p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
319 /* We're gonna need a buffer allocation. */
320 memcpy( &p_filter->output_alloc, p_first_alloc,
321 sizeof(aout_alloc_t) );
322 p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
323 p_first_alloc->i_bytes_per_sec = i_input_size;
328 /*****************************************************************************
329 * aout_FiltersPlay: play a buffer
330 *****************************************************************************/
331 void aout_FiltersPlay( aout_instance_t * p_aout,
332 aout_filter_t ** pp_filters,
333 int i_nb_filters, aout_buffer_t ** pp_input_buffer )
337 for ( i = 0; i < i_nb_filters; i++ )
339 aout_filter_t * p_filter = pp_filters[i];
340 aout_buffer_t * p_output_buffer;
342 /* Resamplers can produce slightly more samples than (i_in_nb *
343 * p_filter->output.i_rate / p_filter->input.i_rate) so we need
344 * slightly bigger buffers. */
345 aout_BufferAlloc( &p_filter->output_alloc,
346 ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
347 * 1000000 / p_filter->input.i_rate,
348 *pp_input_buffer, p_output_buffer );
349 if ( p_output_buffer == NULL )
351 /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
352 * shall be set by the filter plug-in. */
354 p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
357 if ( !p_filter->b_in_place )
359 aout_BufferFree( *pp_input_buffer );
360 *pp_input_buffer = p_output_buffer;
363 if( p_output_buffer->i_nb_samples <= 0 )