1 /*****************************************************************************
2 * filters.c : audio output filters management
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: filters.c,v 1.10 2002/09/22 14:53:52 massiot Exp $
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 );
56 if ( p_filter->p_module == NULL )
58 vlc_object_detach( p_filter );
59 vlc_object_destroy( p_filter );
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( aout_instance_t * p_aout,
76 const audio_sample_format_t * p_input_format,
77 const audio_sample_format_t * p_output_format,
78 audio_sample_format_t * p_middle_format )
81 (p_input_format->i_format != p_output_format->i_format);
82 vlc_bool_t b_rate = (p_input_format->i_rate != p_output_format->i_rate);
83 vlc_bool_t b_channels =
84 (p_input_format->i_channels != p_output_format->i_channels);
85 int i_nb_conversions = b_format + b_rate + b_channels;
87 if ( i_nb_conversions <= 1 ) return 0;
89 memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
91 if ( i_nb_conversions == 2 )
93 if ( !b_format || !b_channels )
95 p_middle_format->i_rate = p_input_format->i_rate;
100 p_middle_format->i_channels = p_input_format->i_channels;
104 /* i_nb_conversion == 3 */
105 p_middle_format->i_rate = p_input_format->i_rate;
109 /*****************************************************************************
110 * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
112 *****************************************************************************
113 * TODO : allow the user to add/remove specific filters
114 *****************************************************************************/
115 int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
116 aout_filter_t ** pp_filters,
118 const audio_sample_format_t * p_input_format,
119 const audio_sample_format_t * p_output_format )
121 audio_sample_format_t temp_format;
122 int i_nb_conversions;
124 if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
126 msg_Dbg( p_aout, "no need for any filter" );
131 msg_Dbg( p_aout, "filter(s) format=%d->%d rate=%d->%d channels=%d->%d",
132 p_input_format->i_format, p_output_format->i_format,
133 p_input_format->i_rate, p_output_format->i_rate,
134 p_input_format->i_channels, p_output_format->i_channels );
136 /* Try to find a filter to do the whole conversion. */
137 pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
138 if ( pp_filters[0] != NULL )
140 msg_Dbg( p_aout, "found a filter for the whole conversion" );
145 /* We'll have to split the conversion. We always do the downmixing
146 * before the resampling, because the audio decoder can probably do it
148 i_nb_conversions = SplitConversion( p_aout, p_input_format,
149 p_output_format, &temp_format );
150 if ( !i_nb_conversions )
152 /* There was only one conversion to do, and we already failed. */
153 msg_Err( p_aout, "couldn't find a filter for the conversion" );
157 pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
158 if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
160 /* Try with only one conversion. */
161 SplitConversion( p_aout, p_input_format, &temp_format,
163 pp_filters[0] = FindFilter( p_aout, p_input_format,
166 if ( pp_filters[0] == NULL )
169 "couldn't find a filter for the first part of the conversion" );
173 /* We have the first stage of the conversion. Find a filter for
175 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
177 if ( pp_filters[1] == NULL )
179 /* Try to split the conversion. */
180 i_nb_conversions = SplitConversion( p_aout,
181 &pp_filters[0]->output,
182 p_output_format, &temp_format );
183 if ( !i_nb_conversions )
185 vlc_object_detach( pp_filters[0] );
186 vlc_object_destroy( pp_filters[0] );
188 "couldn't find a filter for the second part of the conversion" );
190 pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
192 pp_filters[2] = FindFilter( p_aout, &temp_format,
195 if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
197 vlc_object_detach( pp_filters[0] );
198 vlc_object_destroy( pp_filters[0] );
199 if ( pp_filters[1] != NULL )
201 vlc_object_detach( pp_filters[1] );
202 vlc_object_destroy( pp_filters[1] );
204 if ( pp_filters[2] != NULL )
206 vlc_object_detach( pp_filters[2] );
207 vlc_object_destroy( pp_filters[2] );
210 "couldn't find filters for the second part of the conversion" );
219 /* We have enough filters. */
220 msg_Dbg( p_aout, "found %d filters for the whole conversion",
225 /*****************************************************************************
226 * aout_FiltersDestroyPipeline: deallocate a filters pipeline
227 *****************************************************************************/
228 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
229 aout_filter_t ** pp_filters,
234 for ( i = 0; i < i_nb_filters; i++ )
236 module_Unneed( pp_filters[i], pp_filters[i]->p_module );
237 vlc_object_detach( pp_filters[i] );
238 vlc_object_destroy( pp_filters[i] );
242 /*****************************************************************************
243 * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
245 *****************************************************************************/
246 void aout_FiltersHintBuffers( aout_instance_t * p_aout,
247 aout_filter_t ** pp_filters,
248 int i_nb_filters, aout_alloc_t * p_first_alloc )
252 for ( i = i_nb_filters - 1; i >= 0; i-- )
254 aout_filter_t * p_filter = pp_filters[i];
256 int i_output_size = p_filter->output.i_bytes_per_frame
257 * p_filter->output.i_rate
258 / p_filter->output.i_frame_length;
259 int i_input_size = p_filter->input.i_bytes_per_frame
260 * p_filter->input.i_rate
261 / p_filter->input.i_frame_length;
263 p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
266 if ( p_filter->b_in_place )
268 p_first_alloc->i_bytes_per_sec = __MAX(
269 p_first_alloc->i_bytes_per_sec,
271 p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
275 /* We're gonna need a buffer allocation. */
276 memcpy( &p_filter->output_alloc, p_first_alloc,
277 sizeof(aout_alloc_t) );
278 p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
279 p_first_alloc->i_bytes_per_sec = i_input_size;
284 /*****************************************************************************
285 * aout_FiltersPlay: play a buffer
286 *****************************************************************************/
287 void aout_FiltersPlay( aout_instance_t * p_aout,
288 aout_filter_t ** pp_filters,
289 int i_nb_filters, aout_buffer_t ** pp_input_buffer )
293 for ( i = 0; i < i_nb_filters; i++ )
295 aout_filter_t * p_filter = pp_filters[i];
296 aout_buffer_t * p_output_buffer;
298 aout_BufferAlloc( &p_filter->output_alloc,
299 (mtime_t)(*pp_input_buffer)->i_nb_samples * 1000000
300 / p_filter->input.i_rate, *pp_input_buffer,
302 if ( p_output_buffer == NULL )
304 msg_Err( p_aout, "out of memory" );
307 /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
308 * shall be set by the filter plug-in. */
310 p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
313 if ( !p_filter->b_in_place )
315 aout_BufferFree( *pp_input_buffer );
316 *pp_input_buffer = p_output_buffer;