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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_dialog.h>
37 #include "aout_internal.h"
40 /*****************************************************************************
41 * FindFilter: find an audio filter for a specific transformation
42 *****************************************************************************/
43 static aout_filter_t * FindFilter( aout_instance_t * p_aout,
44 const audio_sample_format_t * p_input_format,
45 const audio_sample_format_t * p_output_format )
47 static const char typename[] = "audio output";
48 aout_filter_t * p_filter;
50 p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
51 VLC_OBJECT_GENERIC, typename );
53 if ( p_filter == NULL ) return NULL;
54 vlc_object_attach( p_filter, p_aout );
56 memcpy( &p_filter->input, p_input_format, sizeof(audio_sample_format_t) );
57 memcpy( &p_filter->output, p_output_format,
58 sizeof(audio_sample_format_t) );
59 p_filter->p_module = module_need( p_filter, "audio filter", NULL, false );
60 if ( p_filter->p_module == NULL )
62 vlc_object_detach( p_filter );
63 vlc_object_release( p_filter );
67 p_filter->b_continuity = false;
72 /*****************************************************************************
73 * SplitConversion: split a conversion in two parts
74 *****************************************************************************
75 * Returns the number of conversions required by the first part - 0 if only
76 * one conversion was asked.
77 * Beware : p_output_format can be modified during this function if the
78 * developer passed SplitConversion( toto, titi, titi, ... ). That is legal.
79 * SplitConversion( toto, titi, toto, ... ) isn't.
80 *****************************************************************************/
81 static int SplitConversion( const audio_sample_format_t * p_input_format,
82 const audio_sample_format_t * p_output_format,
83 audio_sample_format_t * p_middle_format )
86 (p_input_format->i_format != p_output_format->i_format);
87 bool b_rate = (p_input_format->i_rate != p_output_format->i_rate);
89 (p_input_format->i_physical_channels
90 != p_output_format->i_physical_channels)
91 || (p_input_format->i_original_channels
92 != p_output_format->i_original_channels);
93 int i_nb_conversions = b_format + b_rate + b_channels;
95 if ( i_nb_conversions <= 1 ) return 0;
97 memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
99 if ( i_nb_conversions == 2 )
101 if ( !b_format || !b_channels )
103 p_middle_format->i_rate = p_input_format->i_rate;
104 aout_FormatPrepare( p_middle_format );
109 p_middle_format->i_physical_channels
110 = p_input_format->i_physical_channels;
111 p_middle_format->i_original_channels
112 = p_input_format->i_original_channels;
113 aout_FormatPrepare( p_middle_format );
117 /* i_nb_conversion == 3 */
118 p_middle_format->i_rate = p_input_format->i_rate;
119 aout_FormatPrepare( p_middle_format );
123 static void ReleaseFilter( aout_filter_t * p_filter )
125 module_unneed( p_filter, p_filter->p_module );
126 vlc_object_detach( p_filter );
127 vlc_object_release( p_filter );
130 /*****************************************************************************
131 * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
133 *****************************************************************************
134 * pi_nb_filters must be initialized before calling this function
135 *****************************************************************************/
136 int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
137 aout_filter_t ** pp_filters_start,
139 const audio_sample_format_t * p_input_format,
140 const audio_sample_format_t * p_output_format )
142 aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
143 audio_sample_format_t temp_format;
144 int i_nb_conversions;
146 if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
148 msg_Dbg( p_aout, "no need for any filter" );
152 aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
154 if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
156 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
157 dialog_Fatal( p_aout, _("Audio filtering failed"),
158 _("The maximum number of filters (%d) was reached."),
163 /* Try to find a filter to do the whole conversion. */
164 pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
165 if ( pp_filters[0] != NULL )
167 msg_Dbg( p_aout, "found a filter for the whole conversion" );
172 /* We'll have to split the conversion. We always do the downmixing
173 * before the resampling, because the audio decoder can probably do it
175 i_nb_conversions = SplitConversion( p_input_format,
176 p_output_format, &temp_format );
177 if ( !i_nb_conversions )
179 /* There was only one conversion to do, and we already failed. */
180 msg_Err( p_aout, "couldn't find a filter for the conversion" );
184 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
185 if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
187 /* Try with only one conversion. */
188 SplitConversion( p_input_format, &temp_format, &temp_format );
189 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
191 if ( pp_filters[0] == NULL )
194 "couldn't find a filter for the first part of the conversion" );
198 /* We have the first stage of the conversion. Find a filter for
200 if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
202 ReleaseFilter( pp_filters[0] );
203 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
204 dialog_Fatal( p_aout, _("Audio filtering failed"),
205 _("The maximum number of filters (%d) was reached."),
209 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
211 if ( pp_filters[1] == NULL )
213 /* Try to split the conversion. */
214 i_nb_conversions = SplitConversion( &pp_filters[0]->output,
215 p_output_format, &temp_format );
216 if ( !i_nb_conversions )
218 ReleaseFilter( pp_filters[0] );
220 "couldn't find a filter for the second part of the conversion" );
223 if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
225 ReleaseFilter( pp_filters[0] );
226 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
227 dialog_Fatal( p_aout, _("Audio filtering failed"),
228 _("The maximum number of filters (%d) was reached."),
232 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
234 pp_filters[2] = FindFilter( p_aout, &temp_format,
237 if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
239 ReleaseFilter( pp_filters[0] );
240 if ( pp_filters[1] != NULL )
242 ReleaseFilter( pp_filters[1] );
244 if ( pp_filters[2] != NULL )
246 ReleaseFilter( pp_filters[2] );
249 "couldn't find filters for the second part of the conversion" );
253 msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
258 msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
264 /*****************************************************************************
265 * aout_FiltersDestroyPipeline: deallocate a filters pipeline
266 *****************************************************************************/
267 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
268 aout_filter_t ** pp_filters,
274 for ( i = 0; i < i_nb_filters; i++ )
276 aout_filter_t *p_filter = pp_filters[i];
278 module_unneed( p_filter, p_filter->p_module );
279 free( p_filter->p_owner );
280 vlc_object_detach( p_filter );
281 vlc_object_release( p_filter );
285 /*****************************************************************************
286 * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
288 *****************************************************************************/
289 void aout_FiltersHintBuffers( aout_instance_t * p_aout,
290 aout_filter_t ** pp_filters,
291 int i_nb_filters, aout_alloc_t * p_first_alloc )
295 (void)p_aout; /* unused */
297 for ( i = i_nb_filters - 1; i >= 0; i-- )
299 aout_filter_t * p_filter = pp_filters[i];
301 int i_output_size = p_filter->output.i_bytes_per_frame
302 * p_filter->output.i_rate * AOUT_MAX_INPUT_RATE
303 / p_filter->output.i_frame_length;
304 int i_input_size = p_filter->input.i_bytes_per_frame
305 * p_filter->input.i_rate * AOUT_MAX_INPUT_RATE
306 / p_filter->input.i_frame_length;
308 p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
311 if ( p_filter->b_in_place )
313 p_first_alloc->i_bytes_per_sec = __MAX(
314 p_first_alloc->i_bytes_per_sec,
316 p_filter->output_alloc.b_alloc = false;
320 /* We're gonna need a buffer allocation. */
321 memcpy( &p_filter->output_alloc, p_first_alloc,
322 sizeof(aout_alloc_t) );
323 p_first_alloc->b_alloc = true;
324 p_first_alloc->i_bytes_per_sec = i_input_size;
329 /*****************************************************************************
330 * aout_FiltersPlay: play a buffer
331 *****************************************************************************/
332 void aout_FiltersPlay( aout_instance_t * p_aout,
333 aout_filter_t ** pp_filters,
334 int i_nb_filters, aout_buffer_t ** pp_input_buffer )
338 for( i = 0; i < i_nb_filters; i++ )
340 aout_filter_t * p_filter = pp_filters[i];
341 aout_buffer_t * p_output_buffer;
343 /* Resamplers can produce slightly more samples than (i_in_nb *
344 * p_filter->output.i_rate / p_filter->input.i_rate) so we need
345 * slightly bigger buffers. */
346 p_output_buffer = aout_BufferAlloc( &p_filter->output_alloc,
347 ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
348 * 1000000 / p_filter->input.i_rate,
350 if( p_output_buffer == NULL )
352 p_output_buffer->i_pts = (*pp_input_buffer)->i_pts;
353 p_output_buffer->i_length = (*pp_input_buffer)->i_length;
355 /* Please note that p_output_buffer->i_nb_samples & i_buffer
356 * shall be set by the filter plug-in. */
357 if( (*pp_input_buffer)->i_nb_samples > 0 )
359 p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
364 p_output_buffer->i_buffer = 0;
365 p_output_buffer->i_nb_samples = 0;
368 if( !p_filter->b_in_place )
370 aout_BufferFree( *pp_input_buffer );
371 *pp_input_buffer = p_output_buffer;
376 /*****************************************************************************
377 * aout_filter_RequestVout
378 *****************************************************************************/
379 vout_thread_t *aout_filter_RequestVout( aout_filter_t *p_filter,
380 vout_thread_t *p_vout, video_format_t *p_fmt )
382 if( !p_filter->request_vout.pf_request_vout )
384 return p_filter->request_vout.pf_request_vout( p_filter->request_vout.p_private,
385 p_vout, p_fmt, true );