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 *****************************************************************************/
28 #include <vlc_interface.h>
35 #include "aout_internal.h"
37 /*****************************************************************************
38 * FindFilter: find an audio filter for a specific transformation
39 *****************************************************************************/
40 static aout_filter_t * FindFilter( aout_instance_t * p_aout,
41 const audio_sample_format_t * p_input_format,
42 const audio_sample_format_t * p_output_format )
44 aout_filter_t * p_filter = vlc_object_create( p_aout,
45 sizeof(aout_filter_t) );
47 if ( p_filter == NULL ) return NULL;
48 vlc_object_attach( p_filter, p_aout );
50 memcpy( &p_filter->input, p_input_format, sizeof(audio_sample_format_t) );
51 memcpy( &p_filter->output, p_output_format,
52 sizeof(audio_sample_format_t) );
53 p_filter->p_module = module_Need( p_filter, "audio filter", NULL, 0 );
54 if ( p_filter->p_module == NULL )
56 vlc_object_detach( p_filter );
57 vlc_object_destroy( p_filter );
61 p_filter->b_continuity = VLC_FALSE;
66 /*****************************************************************************
67 * SplitConversion: split a conversion in two parts
68 *****************************************************************************
69 * Returns the number of conversions required by the first part - 0 if only
70 * one conversion was asked.
71 * Beware : p_output_format can be modified during this function if the
72 * developer passed SplitConversion( toto, titi, titi, ... ). That is legal.
73 * SplitConversion( toto, titi, toto, ... ) isn't.
74 *****************************************************************************/
75 static int SplitConversion( const audio_sample_format_t * p_input_format,
76 const audio_sample_format_t * p_output_format,
77 audio_sample_format_t * p_middle_format )
80 (p_input_format->i_format != p_output_format->i_format);
81 vlc_bool_t b_rate = (p_input_format->i_rate != p_output_format->i_rate);
82 vlc_bool_t b_channels =
83 (p_input_format->i_physical_channels
84 != p_output_format->i_physical_channels)
85 || (p_input_format->i_original_channels
86 != p_output_format->i_original_channels);
87 int i_nb_conversions = b_format + b_rate + b_channels;
89 if ( i_nb_conversions <= 1 ) return 0;
91 memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
93 if ( i_nb_conversions == 2 )
95 if ( !b_format || !b_channels )
97 p_middle_format->i_rate = p_input_format->i_rate;
98 aout_FormatPrepare( p_middle_format );
103 p_middle_format->i_physical_channels
104 = p_input_format->i_physical_channels;
105 p_middle_format->i_original_channels
106 = p_input_format->i_original_channels;
107 aout_FormatPrepare( p_middle_format );
111 /* i_nb_conversion == 3 */
112 p_middle_format->i_rate = p_input_format->i_rate;
113 aout_FormatPrepare( p_middle_format );
117 static void ReleaseFilter( aout_filter_t * p_filter )
119 module_Unneed( p_filter, p_filter->p_module );
120 vlc_object_detach( p_filter );
121 vlc_object_destroy( p_filter );
124 /*****************************************************************************
125 * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
127 *****************************************************************************
128 * pi_nb_filters must be initialized before calling this function
129 *****************************************************************************/
130 int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
131 aout_filter_t ** pp_filters_start,
133 const audio_sample_format_t * p_input_format,
134 const audio_sample_format_t * p_output_format )
136 aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
137 audio_sample_format_t temp_format;
138 int i_nb_conversions;
140 if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
142 msg_Dbg( p_aout, "no need for any filter" );
146 aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
148 if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
150 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
151 intf_UserFatal( p_aout, VLC_FALSE, _("Audio filtering failed"),
152 _("The maximum number of filters (%d) was reached."),
157 /* Try to find a filter to do the whole conversion. */
158 pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
159 if ( pp_filters[0] != NULL )
161 msg_Dbg( p_aout, "found a filter for the whole conversion" );
166 /* We'll have to split the conversion. We always do the downmixing
167 * before the resampling, because the audio decoder can probably do it
169 i_nb_conversions = SplitConversion( p_input_format,
170 p_output_format, &temp_format );
171 if ( !i_nb_conversions )
173 /* There was only one conversion to do, and we already failed. */
174 msg_Err( p_aout, "couldn't find a filter for the conversion" );
178 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
179 if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
181 /* Try with only one conversion. */
182 SplitConversion( p_input_format, &temp_format, &temp_format );
183 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
185 if ( pp_filters[0] == NULL )
188 "couldn't find a filter for the first part of the conversion" );
192 /* We have the first stage of the conversion. Find a filter for
194 if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
196 ReleaseFilter( pp_filters[0] );
197 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
198 intf_UserFatal( p_aout, VLC_FALSE, _("Audio filtering failed"),
199 _("The maximum number of filters (%d) was reached."),
203 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
205 if ( pp_filters[1] == NULL )
207 /* Try to split the conversion. */
208 i_nb_conversions = SplitConversion( &pp_filters[0]->output,
209 p_output_format, &temp_format );
210 if ( !i_nb_conversions )
212 ReleaseFilter( pp_filters[0] );
214 "couldn't find a filter for the second part of the conversion" );
217 if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
219 ReleaseFilter( pp_filters[0] );
220 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
221 intf_UserFatal( p_aout, VLC_FALSE, _("Audio filtering failed"),
222 _("The maximum number of filters (%d) was reached."),
226 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
228 pp_filters[2] = FindFilter( p_aout, &temp_format,
231 if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
233 ReleaseFilter( pp_filters[0] );
234 if ( pp_filters[1] != NULL )
236 ReleaseFilter( pp_filters[1] );
238 if ( pp_filters[2] != NULL )
240 ReleaseFilter( pp_filters[2] );
243 "couldn't find filters for the second part of the conversion" );
247 msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
252 msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
258 /*****************************************************************************
259 * aout_FiltersDestroyPipeline: deallocate a filters pipeline
260 *****************************************************************************/
261 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
262 aout_filter_t ** pp_filters,
268 for ( i = 0; i < i_nb_filters; i++ )
270 module_Unneed( pp_filters[i], pp_filters[i]->p_module );
271 vlc_object_detach( pp_filters[i] );
272 vlc_object_destroy( pp_filters[i] );
276 /*****************************************************************************
277 * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
279 *****************************************************************************/
280 void aout_FiltersHintBuffers( aout_instance_t * p_aout,
281 aout_filter_t ** pp_filters,
282 int i_nb_filters, aout_alloc_t * p_first_alloc )
286 (void)p_aout; /* unused */
288 for ( i = i_nb_filters - 1; i >= 0; i-- )
290 aout_filter_t * p_filter = pp_filters[i];
292 int i_output_size = p_filter->output.i_bytes_per_frame
293 * p_filter->output.i_rate * AOUT_MAX_INPUT_RATE
294 / p_filter->output.i_frame_length;
295 int i_input_size = p_filter->input.i_bytes_per_frame
296 * p_filter->input.i_rate * AOUT_MAX_INPUT_RATE
297 / p_filter->input.i_frame_length;
299 p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
302 if ( p_filter->b_in_place )
304 p_first_alloc->i_bytes_per_sec = __MAX(
305 p_first_alloc->i_bytes_per_sec,
307 p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
311 /* We're gonna need a buffer allocation. */
312 memcpy( &p_filter->output_alloc, p_first_alloc,
313 sizeof(aout_alloc_t) );
314 p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
315 p_first_alloc->i_bytes_per_sec = i_input_size;
320 /*****************************************************************************
321 * aout_FiltersPlay: play a buffer
322 *****************************************************************************/
323 void aout_FiltersPlay( aout_instance_t * p_aout,
324 aout_filter_t ** pp_filters,
325 int i_nb_filters, aout_buffer_t ** pp_input_buffer )
329 for ( i = 0; i < i_nb_filters; i++ )
331 aout_filter_t * p_filter = pp_filters[i];
332 aout_buffer_t * p_output_buffer;
334 /* Resamplers can produce slightly more samples than (i_in_nb *
335 * p_filter->output.i_rate / p_filter->input.i_rate) so we need
336 * slightly bigger buffers. */
337 aout_BufferAlloc( &p_filter->output_alloc,
338 ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
339 * 1000000 / p_filter->input.i_rate,
340 *pp_input_buffer, p_output_buffer );
341 if ( p_output_buffer == NULL )
343 msg_Err( p_aout, "out of memory" );
346 /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
347 * shall be set by the filter plug-in. */
349 p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
352 if ( !p_filter->b_in_place )
354 aout_BufferFree( *pp_input_buffer );
355 *pp_input_buffer = p_output_buffer;
358 if( p_output_buffer->i_nb_samples <= 0 )