1 /*****************************************************************************
2 * filters.c : audio output filters management
3 *****************************************************************************
4 * Copyright (C) 2002-2004 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
36 #include "audio_output.h"
37 #include "aout_internal.h"
39 /*****************************************************************************
40 * FindFilter: find an audio filter for a specific transformation
41 *****************************************************************************/
42 static aout_filter_t * FindFilter( aout_instance_t * p_aout,
43 const audio_sample_format_t * p_input_format,
44 const audio_sample_format_t * p_output_format )
46 aout_filter_t * p_filter = vlc_object_create( p_aout,
47 sizeof(aout_filter_t) );
49 if ( p_filter == NULL ) return NULL;
50 vlc_object_attach( p_filter, p_aout );
52 memcpy( &p_filter->input, p_input_format, sizeof(audio_sample_format_t) );
53 memcpy( &p_filter->output, p_output_format,
54 sizeof(audio_sample_format_t) );
55 p_filter->p_module = module_Need( p_filter, "audio filter", NULL, 0 );
56 if ( p_filter->p_module == NULL )
58 vlc_object_detach( p_filter );
59 vlc_object_destroy( p_filter );
63 p_filter->b_continuity = VLC_FALSE;
68 /*****************************************************************************
69 * SplitConversion: split a conversion in two parts
70 *****************************************************************************
71 * Returns the number of conversions required by the first part - 0 if only
72 * one conversion was asked.
73 * Beware : p_output_format can be modified during this function if the
74 * developer passed SplitConversion( toto, titi, titi, ... ). That is legal.
75 * SplitConversion( toto, titi, toto, ... ) isn't.
76 *****************************************************************************/
77 static int SplitConversion( const audio_sample_format_t * p_input_format,
78 const audio_sample_format_t * p_output_format,
79 audio_sample_format_t * p_middle_format )
82 (p_input_format->i_format != p_output_format->i_format);
83 vlc_bool_t b_rate = (p_input_format->i_rate != p_output_format->i_rate);
84 vlc_bool_t b_channels =
85 (p_input_format->i_physical_channels
86 != p_output_format->i_physical_channels)
87 || (p_input_format->i_original_channels
88 != p_output_format->i_original_channels);
89 int i_nb_conversions = b_format + b_rate + b_channels;
91 if ( i_nb_conversions <= 1 ) return 0;
93 memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
95 if ( i_nb_conversions == 2 )
97 if ( !b_format || !b_channels )
99 p_middle_format->i_rate = p_input_format->i_rate;
100 aout_FormatPrepare( p_middle_format );
105 p_middle_format->i_physical_channels
106 = p_input_format->i_physical_channels;
107 p_middle_format->i_original_channels
108 = p_input_format->i_original_channels;
109 aout_FormatPrepare( p_middle_format );
113 /* i_nb_conversion == 3 */
114 p_middle_format->i_rate = p_input_format->i_rate;
115 aout_FormatPrepare( p_middle_format );
119 static void ReleaseFilter( aout_filter_t * p_filter )
121 module_Unneed( p_filter, p_filter->p_module );
122 vlc_object_detach( p_filter );
123 vlc_object_destroy( p_filter );
126 /*****************************************************************************
127 * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
129 *****************************************************************************
130 * pi_nb_filters must be initialized before calling this function
131 *****************************************************************************/
132 int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
133 aout_filter_t ** pp_filters_start,
135 const audio_sample_format_t * p_input_format,
136 const audio_sample_format_t * p_output_format )
138 aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
139 audio_sample_format_t temp_format;
140 int i_nb_conversions;
142 if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
144 msg_Dbg( p_aout, "no need for any filter" );
148 aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
150 if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
152 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
156 /* Try to find a filter to do the whole conversion. */
157 pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
158 if ( pp_filters[0] != NULL )
160 msg_Dbg( p_aout, "found a filter for the whole conversion" );
165 /* We'll have to split the conversion. We always do the downmixing
166 * before the resampling, because the audio decoder can probably do it
168 i_nb_conversions = SplitConversion( p_input_format,
169 p_output_format, &temp_format );
170 if ( !i_nb_conversions )
172 /* There was only one conversion to do, and we already failed. */
173 msg_Err( p_aout, "couldn't find a filter for the conversion" );
177 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
178 if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
180 /* Try with only one conversion. */
181 SplitConversion( p_input_format, &temp_format, &temp_format );
182 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
184 if ( pp_filters[0] == NULL )
187 "couldn't find a filter for the first part of the conversion" );
191 /* We have the first stage of the conversion. Find a filter for
193 if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
195 ReleaseFilter( pp_filters[0] );
196 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
199 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
201 if ( pp_filters[1] == NULL )
203 /* Try to split the conversion. */
204 i_nb_conversions = SplitConversion( &pp_filters[0]->output,
205 p_output_format, &temp_format );
206 if ( !i_nb_conversions )
208 ReleaseFilter( pp_filters[0] );
210 "couldn't find a filter for the second part of the conversion" );
213 if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
215 ReleaseFilter( pp_filters[0] );
216 msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
219 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
221 pp_filters[2] = FindFilter( p_aout, &temp_format,
224 if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
226 ReleaseFilter( pp_filters[0] );
227 if ( pp_filters[1] != NULL )
229 ReleaseFilter( pp_filters[1] );
231 if ( pp_filters[2] != NULL )
233 ReleaseFilter( pp_filters[2] );
236 "couldn't find filters for the second part of the conversion" );
240 msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
245 msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
251 /*****************************************************************************
252 * aout_FiltersDestroyPipeline: deallocate a filters pipeline
253 *****************************************************************************/
254 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
255 aout_filter_t ** pp_filters,
260 for ( i = 0; i < i_nb_filters; i++ )
262 module_Unneed( pp_filters[i], pp_filters[i]->p_module );
263 vlc_object_detach( pp_filters[i] );
264 vlc_object_destroy( pp_filters[i] );
268 /*****************************************************************************
269 * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
271 *****************************************************************************/
272 void aout_FiltersHintBuffers( aout_instance_t * p_aout,
273 aout_filter_t ** pp_filters,
274 int i_nb_filters, aout_alloc_t * p_first_alloc )
278 (void)p_aout; /* unused */
280 for ( i = i_nb_filters - 1; i >= 0; i-- )
282 aout_filter_t * p_filter = pp_filters[i];
284 int i_output_size = p_filter->output.i_bytes_per_frame
285 * p_filter->output.i_rate
286 / p_filter->output.i_frame_length;
287 int i_input_size = p_filter->input.i_bytes_per_frame
288 * p_filter->input.i_rate
289 / p_filter->input.i_frame_length;
291 p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
294 if ( p_filter->b_in_place )
296 p_first_alloc->i_bytes_per_sec = __MAX(
297 p_first_alloc->i_bytes_per_sec,
299 p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
303 /* We're gonna need a buffer allocation. */
304 memcpy( &p_filter->output_alloc, p_first_alloc,
305 sizeof(aout_alloc_t) );
306 p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
307 p_first_alloc->i_bytes_per_sec = i_input_size;
312 /*****************************************************************************
313 * aout_FiltersPlay: play a buffer
314 *****************************************************************************/
315 void aout_FiltersPlay( aout_instance_t * p_aout,
316 aout_filter_t ** pp_filters,
317 int i_nb_filters, aout_buffer_t ** pp_input_buffer )
321 for ( i = 0; i < i_nb_filters; i++ )
323 aout_filter_t * p_filter = pp_filters[i];
324 aout_buffer_t * p_output_buffer;
326 /* Resamplers can produce slightly more samples than (i_in_nb *
327 * p_filter->output.i_rate / p_filter->input.i_rate) so we need
328 * slightly bigger buffers. */
329 aout_BufferAlloc( &p_filter->output_alloc,
330 ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
331 * 1000000 / p_filter->input.i_rate,
332 *pp_input_buffer, p_output_buffer );
333 if ( p_output_buffer == NULL )
335 msg_Err( p_aout, "out of memory" );
338 /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
339 * shall be set by the filter plug-in. */
341 p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
344 if ( !p_filter->b_in_place )
346 aout_BufferFree( *pp_input_buffer );
347 *pp_input_buffer = p_output_buffer;