]> git.sesse.net Git - vlc/blob - src/audio_output/filters.c
9878911075c2b9128f57b03baff680b89a7ebc0b
[vlc] / src / audio_output / filters.c
1 /*****************************************************************************
2  * filters.c : audio output filters management
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_interface.h>
29
30 #ifdef HAVE_ALLOCA_H
31 #   include <alloca.h>
32 #endif
33
34 #include <vlc_aout.h>
35 #include "aout_internal.h"
36
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 )
43 {
44     aout_filter_t * p_filter = vlc_object_create( p_aout,
45                                                   sizeof(aout_filter_t) );
46
47     if ( p_filter == NULL ) return NULL;
48     vlc_object_attach( p_filter, p_aout );
49
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 )
55     {
56         vlc_object_detach( p_filter );
57         vlc_object_destroy( p_filter );
58         return NULL;
59     }
60
61     p_filter->b_continuity = VLC_FALSE;
62
63     return p_filter;
64 }
65
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 )
78 {
79     vlc_bool_t b_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;
88
89     if ( i_nb_conversions <= 1 ) return 0;
90
91     memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
92
93     if ( i_nb_conversions == 2 )
94     {
95         if ( !b_format || !b_channels )
96         {
97             p_middle_format->i_rate = p_input_format->i_rate;
98             aout_FormatPrepare( p_middle_format );
99             return 1;
100         }
101
102         /* !b_rate */
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 );
108         return 1;
109     }
110
111     /* i_nb_conversion == 3 */
112     p_middle_format->i_rate = p_input_format->i_rate;
113     aout_FormatPrepare( p_middle_format );
114     return 2;
115 }
116
117 static void ReleaseFilter( aout_filter_t * p_filter )
118 {
119     module_Unneed( p_filter, p_filter->p_module );
120     vlc_object_detach( p_filter );
121     vlc_object_destroy( p_filter );
122 }
123
124 /*****************************************************************************
125  * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
126  *                             format to another
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,
132                                 int * pi_nb_filters,
133                                 const audio_sample_format_t * p_input_format,
134                                 const audio_sample_format_t * p_output_format )
135 {
136     aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
137     audio_sample_format_t temp_format;
138     int i_nb_conversions;
139
140     if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
141     {
142         msg_Dbg( p_aout, "no need for any filter" );
143         return 0;
144     }
145
146     aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
147
148     if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
149     {
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."),
153                         AOUT_MAX_FILTERS );
154         return -1;
155     }
156
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 )
160     {
161         msg_Dbg( p_aout, "found a filter for the whole conversion" );
162         ++*pi_nb_filters;
163         return 0;
164     }
165
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
168      * for us. */
169     i_nb_conversions = SplitConversion( p_input_format,
170                                         p_output_format, &temp_format );
171     if ( !i_nb_conversions )
172     {
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" );
175         return -1;
176     }
177
178     pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
179     if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
180     {
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 );
184     }
185     if ( pp_filters[0] == NULL )
186     {
187         msg_Err( p_aout,
188               "couldn't find a filter for the first part of the conversion" );
189         return -1;
190     }
191
192     /* We have the first stage of the conversion. Find a filter for
193      * the rest. */
194     if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
195     {
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."),
200                         AOUT_MAX_FILTERS );
201         return -1;
202     }
203     pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
204                                 p_output_format );
205     if ( pp_filters[1] == NULL )
206     {
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 )
211         {
212             ReleaseFilter( pp_filters[0] );
213             msg_Err( p_aout,
214               "couldn't find a filter for the second part of the conversion" );
215             return -1;
216         }
217         if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
218         {
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."),
223                             AOUT_MAX_FILTERS );
224             return -1;
225         }
226         pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
227                                     &temp_format );
228         pp_filters[2] = FindFilter( p_aout, &temp_format,
229                                     p_output_format );
230
231         if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
232         {
233             ReleaseFilter( pp_filters[0] );
234             if ( pp_filters[1] != NULL )
235             {
236                 ReleaseFilter( pp_filters[1] );
237             }
238             if ( pp_filters[2] != NULL )
239             {
240                 ReleaseFilter( pp_filters[2] );
241             }
242             msg_Err( p_aout,
243                "couldn't find filters for the second part of the conversion" );
244             return -1;
245         }
246         *pi_nb_filters += 3;
247         msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
248     }
249     else
250     {
251         *pi_nb_filters += 2;
252         msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
253     }
254
255     return 0;
256 }
257
258 /*****************************************************************************
259  * aout_FiltersDestroyPipeline: deallocate a filters pipeline
260  *****************************************************************************/
261 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
262                                   aout_filter_t ** pp_filters,
263                                   int i_nb_filters )
264 {
265     int i;
266     (void)p_aout;
267
268     for ( i = 0; i < i_nb_filters; i++ )
269     {
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] );
273     }
274 }
275
276 /*****************************************************************************
277  * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
278  *                          buffer allocations
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 )
283 {
284     int i;
285
286     (void)p_aout; /* unused */
287
288     for ( i = i_nb_filters - 1; i >= 0; i-- )
289     {
290         aout_filter_t * p_filter = pp_filters[i];
291
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;
298
299         p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
300                                                 i_output_size );
301
302         if ( p_filter->b_in_place )
303         {
304             p_first_alloc->i_bytes_per_sec = __MAX(
305                                          p_first_alloc->i_bytes_per_sec,
306                                          i_input_size );
307             p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
308         }
309         else
310         {
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;
316         }
317     }
318 }
319
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 )
326 {
327     int i;
328
329     for ( i = 0; i < i_nb_filters; i++ )
330     {
331         aout_filter_t * p_filter = pp_filters[i];
332         aout_buffer_t * p_output_buffer;
333
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 )
342         {
343             msg_Err( p_aout, "out of memory" );
344             return;
345         }
346         /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
347          * shall be set by the filter plug-in. */
348
349         p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
350                               p_output_buffer );
351
352         if ( !p_filter->b_in_place )
353         {
354             aout_BufferFree( *pp_input_buffer );
355             *pp_input_buffer = p_output_buffer;
356         }
357
358         if( p_output_buffer->i_nb_samples <= 0 )
359             break;
360     }
361 }
362