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->fmt_in.audio, p_input_format, sizeof(audio_sample_format_t) );
57 memcpy( &p_filter->fmt_out.audio, 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 );
70 /*****************************************************************************
71 * SplitConversion: split a conversion in two parts
72 *****************************************************************************
73 * Returns the number of conversions required by the first part - 0 if only
74 * one conversion was asked.
75 * Beware : p_output_format can be modified during this function if the
76 * developer passed SplitConversion( toto, titi, titi, ... ). That is legal.
77 * SplitConversion( toto, titi, toto, ... ) isn't.
78 *****************************************************************************/
79 static int SplitConversion( const audio_sample_format_t * p_input_format,
80 const audio_sample_format_t * p_output_format,
81 audio_sample_format_t * p_middle_format )
84 (p_input_format->i_format != p_output_format->i_format);
85 bool b_rate = (p_input_format->i_rate != p_output_format->i_rate);
87 (p_input_format->i_physical_channels
88 != p_output_format->i_physical_channels)
89 || (p_input_format->i_original_channels
90 != p_output_format->i_original_channels);
91 int i_nb_conversions = b_format + b_rate + b_channels;
93 if ( i_nb_conversions <= 1 ) return 0;
95 memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
97 if ( i_nb_conversions == 2 )
99 if ( !b_format || !b_channels )
101 p_middle_format->i_rate = p_input_format->i_rate;
102 aout_FormatPrepare( p_middle_format );
107 p_middle_format->i_physical_channels
108 = p_input_format->i_physical_channels;
109 p_middle_format->i_original_channels
110 = p_input_format->i_original_channels;
111 aout_FormatPrepare( p_middle_format );
115 /* i_nb_conversion == 3 */
116 p_middle_format->i_rate = p_input_format->i_rate;
117 aout_FormatPrepare( p_middle_format );
121 static void ReleaseFilter( aout_filter_t * p_filter )
123 module_unneed( p_filter, p_filter->p_module );
124 vlc_object_detach( p_filter );
125 vlc_object_release( p_filter );
128 /*****************************************************************************
129 * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
131 *****************************************************************************
132 * pi_nb_filters must be initialized before calling this function
133 *****************************************************************************/
134 int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
135 aout_filter_t ** pp_filters_start,
137 const audio_sample_format_t * p_input_format,
138 const audio_sample_format_t * p_output_format )
140 aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
141 audio_sample_format_t temp_format;
142 int i_nb_conversions;
144 if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
146 msg_Dbg( p_aout, "no need for any filter" );
150 aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
152 if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
154 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
155 dialog_Fatal( p_aout, _("Audio filtering failed"),
156 _("The maximum number of filters (%d) was reached."),
161 /* Try to find a filter to do the whole conversion. */
162 pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
163 if ( pp_filters[0] != NULL )
165 msg_Dbg( p_aout, "found a filter for the whole conversion" );
170 /* We'll have to split the conversion. We always do the downmixing
171 * before the resampling, because the audio decoder can probably do it
173 i_nb_conversions = SplitConversion( p_input_format,
174 p_output_format, &temp_format );
175 if ( !i_nb_conversions )
177 /* There was only one conversion to do, and we already failed. */
178 msg_Err( p_aout, "couldn't find a filter for the conversion" );
182 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
183 if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
185 /* Try with only one conversion. */
186 SplitConversion( p_input_format, &temp_format, &temp_format );
187 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
189 if ( pp_filters[0] == NULL )
192 "couldn't find a filter for the first part of the conversion" );
196 /* We have the first stage of the conversion. Find a filter for
198 if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
200 ReleaseFilter( pp_filters[0] );
201 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
202 dialog_Fatal( p_aout, _("Audio filtering failed"),
203 _("The maximum number of filters (%d) was reached."),
207 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->fmt_out.audio,
209 if ( pp_filters[1] == NULL )
211 /* Try to split the conversion. */
212 i_nb_conversions = SplitConversion( &pp_filters[0]->fmt_out.audio,
213 p_output_format, &temp_format );
214 if ( !i_nb_conversions )
216 ReleaseFilter( pp_filters[0] );
218 "couldn't find a filter for the second part of the conversion" );
221 if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
223 ReleaseFilter( pp_filters[0] );
224 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
225 dialog_Fatal( p_aout, _("Audio filtering failed"),
226 _("The maximum number of filters (%d) was reached."),
230 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->fmt_out.audio,
232 pp_filters[2] = FindFilter( p_aout, &temp_format,
235 if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
237 ReleaseFilter( pp_filters[0] );
238 if ( pp_filters[1] != NULL )
240 ReleaseFilter( pp_filters[1] );
242 if ( pp_filters[2] != NULL )
244 ReleaseFilter( pp_filters[2] );
247 "couldn't find filters for the second part of the conversion" );
251 msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
256 msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
262 /*****************************************************************************
263 * aout_FiltersDestroyPipeline: deallocate a filters pipeline
264 *****************************************************************************/
265 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
266 aout_filter_t ** pp_filters,
272 for ( i = 0; i < i_nb_filters; i++ )
274 aout_filter_t *p_filter = pp_filters[i];
276 module_unneed( p_filter, p_filter->p_module );
277 free( p_filter->p_owner );
278 vlc_object_detach( p_filter );
279 vlc_object_release( p_filter );
283 /*****************************************************************************
284 * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
286 *****************************************************************************/
287 void aout_FiltersHintBuffers( aout_instance_t * p_aout,
288 aout_filter_t ** pp_filters,
289 int i_nb_filters, aout_alloc_t * p_first_alloc )
293 (void)p_aout; /* unused */
295 for ( i = i_nb_filters - 1; i >= 0; i-- )
297 aout_filter_t * p_filter = pp_filters[i];
299 int i_output_size = p_filter->fmt_out.audio.i_bytes_per_frame
300 * p_filter->fmt_out.audio.i_rate * AOUT_MAX_INPUT_RATE
301 / p_filter->fmt_out.audio.i_frame_length;
302 int i_input_size = p_filter->fmt_in.audio.i_bytes_per_frame
303 * p_filter->fmt_in.audio.i_rate * AOUT_MAX_INPUT_RATE
304 / p_filter->fmt_in.audio.i_frame_length;
306 p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
309 if ( p_filter->b_in_place )
311 p_first_alloc->i_bytes_per_sec = __MAX(
312 p_first_alloc->i_bytes_per_sec,
314 p_filter->output_alloc.b_alloc = false;
318 /* We're gonna need a buffer allocation. */
319 memcpy( &p_filter->output_alloc, p_first_alloc,
320 sizeof(aout_alloc_t) );
321 p_first_alloc->b_alloc = true;
322 p_first_alloc->i_bytes_per_sec = i_input_size;
327 /*****************************************************************************
328 * aout_FiltersPlay: play a buffer
329 *****************************************************************************/
330 void aout_FiltersPlay( aout_instance_t * p_aout,
331 aout_filter_t ** pp_filters,
332 int i_nb_filters, aout_buffer_t ** pp_input_buffer )
336 for( i = 0; i < i_nb_filters; i++ )
338 aout_filter_t * p_filter = pp_filters[i];
339 aout_buffer_t * p_output_buffer;
341 /* Resamplers can produce slightly more samples than (i_in_nb *
342 * p_filter->output.i_rate / p_filter->input.i_rate) so we need
343 * slightly bigger buffers. */
344 p_output_buffer = aout_BufferAlloc( &p_filter->output_alloc,
345 ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
346 * 1000000 / p_filter->fmt_in.audio.i_rate,
348 if( p_output_buffer == NULL )
350 p_output_buffer->i_pts = (*pp_input_buffer)->i_pts;
351 p_output_buffer->i_length = (*pp_input_buffer)->i_length;
353 /* Please note that p_output_buffer->i_nb_samples & i_buffer
354 * shall be set by the filter plug-in. */
355 if( (*pp_input_buffer)->i_nb_samples > 0 )
357 p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
362 p_output_buffer->i_buffer = 0;
363 p_output_buffer->i_nb_samples = 0;
366 if( !p_filter->b_in_place )
368 aout_BufferFree( *pp_input_buffer );
369 *pp_input_buffer = p_output_buffer;