]> git.sesse.net Git - vlc/blob - src/audio_output/filters.c
A bit of headers cleanup
[vlc] / src / audio_output / filters.c
1 /*****************************************************************************
2  * filters.c : audio output filters management
3  *****************************************************************************
4  * Copyright (C) 2002-2006 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 <stdlib.h>                            /* calloc(), malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_interface.h>
32
33 #ifdef HAVE_ALLOCA_H
34 #   include <alloca.h>
35 #endif
36
37 #include <vlc_aout.h>
38 #include "aout_internal.h"
39
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 )
46 {
47     aout_filter_t * p_filter = vlc_object_create( p_aout,
48                                                   sizeof(aout_filter_t) );
49
50     if ( p_filter == NULL ) return NULL;
51     vlc_object_attach( p_filter, p_aout );
52
53     memcpy( &p_filter->input, p_input_format, sizeof(audio_sample_format_t) );
54     memcpy( &p_filter->output, p_output_format,
55             sizeof(audio_sample_format_t) );
56     p_filter->p_module = module_Need( p_filter, "audio filter", NULL, 0 );
57     if ( p_filter->p_module == NULL )
58     {
59         vlc_object_detach( p_filter );
60         vlc_object_destroy( p_filter );
61         return NULL;
62     }
63
64     p_filter->b_continuity = VLC_FALSE;
65
66     return p_filter;
67 }
68
69 /*****************************************************************************
70  * SplitConversion: split a conversion in two parts
71  *****************************************************************************
72  * Returns the number of conversions required by the first part - 0 if only
73  * one conversion was asked.
74  * Beware : p_output_format can be modified during this function if the
75  * developer passed SplitConversion( toto, titi, titi, ... ). That is legal.
76  * SplitConversion( toto, titi, toto, ... ) isn't.
77  *****************************************************************************/
78 static int SplitConversion( const audio_sample_format_t * p_input_format,
79                             const audio_sample_format_t * p_output_format,
80                             audio_sample_format_t * p_middle_format )
81 {
82     vlc_bool_t b_format =
83              (p_input_format->i_format != p_output_format->i_format);
84     vlc_bool_t b_rate = (p_input_format->i_rate != p_output_format->i_rate);
85     vlc_bool_t b_channels =
86         (p_input_format->i_physical_channels
87           != p_output_format->i_physical_channels)
88      || (p_input_format->i_original_channels
89           != p_output_format->i_original_channels);
90     int i_nb_conversions = b_format + b_rate + b_channels;
91
92     if ( i_nb_conversions <= 1 ) return 0;
93
94     memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );
95
96     if ( i_nb_conversions == 2 )
97     {
98         if ( !b_format || !b_channels )
99         {
100             p_middle_format->i_rate = p_input_format->i_rate;
101             aout_FormatPrepare( p_middle_format );
102             return 1;
103         }
104
105         /* !b_rate */
106         p_middle_format->i_physical_channels
107              = p_input_format->i_physical_channels;
108         p_middle_format->i_original_channels
109              = p_input_format->i_original_channels;
110         aout_FormatPrepare( p_middle_format );
111         return 1;
112     }
113
114     /* i_nb_conversion == 3 */
115     p_middle_format->i_rate = p_input_format->i_rate;
116     aout_FormatPrepare( p_middle_format );
117     return 2;
118 }
119
120 static void ReleaseFilter( aout_filter_t * p_filter )
121 {
122     module_Unneed( p_filter, p_filter->p_module );
123     vlc_object_detach( p_filter );
124     vlc_object_destroy( p_filter );
125 }
126
127 /*****************************************************************************
128  * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
129  *                             format to another
130  *****************************************************************************
131  * pi_nb_filters must be initialized before calling this function
132  *****************************************************************************/
133 int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
134                                 aout_filter_t ** pp_filters_start,
135                                 int * pi_nb_filters,
136                                 const audio_sample_format_t * p_input_format,
137                                 const audio_sample_format_t * p_output_format )
138 {
139     aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
140     audio_sample_format_t temp_format;
141     int i_nb_conversions;
142
143     if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
144     {
145         msg_Dbg( p_aout, "no need for any filter" );
146         return 0;
147     }
148
149     aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
150
151     if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
152     {
153         msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
154         intf_UserFatal( p_aout, VLC_FALSE, _("Audio filtering failed"), 
155                         _("The maximum number of filters (%d) was reached."),
156                         AOUT_MAX_FILTERS );
157         return -1;
158     }
159
160     /* Try to find a filter to do the whole conversion. */
161     pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
162     if ( pp_filters[0] != NULL )
163     {
164         msg_Dbg( p_aout, "found a filter for the whole conversion" );
165         ++*pi_nb_filters;
166         return 0;
167     }
168
169     /* We'll have to split the conversion. We always do the downmixing
170      * before the resampling, because the audio decoder can probably do it
171      * for us. */
172     i_nb_conversions = SplitConversion( p_input_format,
173                                         p_output_format, &temp_format );
174     if ( !i_nb_conversions )
175     {
176         /* There was only one conversion to do, and we already failed. */
177         msg_Err( p_aout, "couldn't find a filter for the conversion" );
178         return -1;
179     }
180
181     pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
182     if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
183     {
184         /* Try with only one conversion. */
185         SplitConversion( p_input_format, &temp_format, &temp_format );
186         pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
187     }
188     if ( pp_filters[0] == NULL )
189     {
190         msg_Err( p_aout,
191               "couldn't find a filter for the first part of the conversion" );
192         return -1;
193     }
194
195     /* We have the first stage of the conversion. Find a filter for
196      * the rest. */
197     if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
198     {
199         ReleaseFilter( pp_filters[0] );
200         msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
201         intf_UserFatal( p_aout, VLC_FALSE, _("Audio filtering failed"), 
202                         _("The maximum number of filters (%d) was reached."),
203                         AOUT_MAX_FILTERS );
204         return -1;
205     }
206     pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
207                                 p_output_format );
208     if ( pp_filters[1] == NULL )
209     {
210         /* Try to split the conversion. */
211         i_nb_conversions = SplitConversion( &pp_filters[0]->output,
212                                            p_output_format, &temp_format );
213         if ( !i_nb_conversions )
214         {
215             ReleaseFilter( pp_filters[0] );
216             msg_Err( p_aout,
217               "couldn't find a filter for the second part of the conversion" );
218             return -1;
219         }
220         if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
221         {
222             ReleaseFilter( pp_filters[0] );
223             msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
224             intf_UserFatal( p_aout, VLC_FALSE, _("Audio filtering failed"), 
225                             _("The maximum number of filters (%d) was reached."),
226                             AOUT_MAX_FILTERS );
227             return -1;
228         }
229         pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
230                                     &temp_format );
231         pp_filters[2] = FindFilter( p_aout, &temp_format,
232                                     p_output_format );
233
234         if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
235         {
236             ReleaseFilter( pp_filters[0] );
237             if ( pp_filters[1] != NULL )
238             {
239                 ReleaseFilter( pp_filters[1] );
240             }
241             if ( pp_filters[2] != NULL )
242             {
243                 ReleaseFilter( pp_filters[2] );
244             }
245             msg_Err( p_aout,
246                "couldn't find filters for the second part of the conversion" );
247             return -1;
248         }
249         *pi_nb_filters += 3;
250         msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
251     }
252     else
253     {
254         *pi_nb_filters += 2;
255         msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
256     }
257
258     return 0;
259 }
260
261 /*****************************************************************************
262  * aout_FiltersDestroyPipeline: deallocate a filters pipeline
263  *****************************************************************************/
264 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
265                                   aout_filter_t ** pp_filters,
266                                   int i_nb_filters )
267 {
268     int i;
269
270     for ( i = 0; i < i_nb_filters; i++ )
271     {
272         module_Unneed( pp_filters[i], pp_filters[i]->p_module );
273         vlc_object_detach( pp_filters[i] );
274         vlc_object_destroy( pp_filters[i] );
275     }
276 }
277
278 /*****************************************************************************
279  * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
280  *                          buffer allocations
281  *****************************************************************************/
282 void aout_FiltersHintBuffers( aout_instance_t * p_aout,
283                               aout_filter_t ** pp_filters,
284                               int i_nb_filters, aout_alloc_t * p_first_alloc )
285 {
286     int i;
287
288     (void)p_aout; /* unused */
289
290     for ( i = i_nb_filters - 1; i >= 0; i-- )
291     {
292         aout_filter_t * p_filter = pp_filters[i];
293
294         int i_output_size = p_filter->output.i_bytes_per_frame
295                              * p_filter->output.i_rate
296                              / p_filter->output.i_frame_length;
297         int i_input_size = p_filter->input.i_bytes_per_frame
298                              * p_filter->input.i_rate
299                              / p_filter->input.i_frame_length;
300
301         p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
302                                                 i_output_size );
303
304         if ( p_filter->b_in_place )
305         {
306             p_first_alloc->i_bytes_per_sec = __MAX(
307                                          p_first_alloc->i_bytes_per_sec,
308                                          i_input_size );
309             p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
310         }
311         else
312         {
313             /* We're gonna need a buffer allocation. */
314             memcpy( &p_filter->output_alloc, p_first_alloc,
315                     sizeof(aout_alloc_t) );
316             p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
317             p_first_alloc->i_bytes_per_sec = i_input_size;
318         }
319     }
320 }
321
322 /*****************************************************************************
323  * aout_FiltersPlay: play a buffer
324  *****************************************************************************/
325 void aout_FiltersPlay( aout_instance_t * p_aout,
326                        aout_filter_t ** pp_filters,
327                        int i_nb_filters, aout_buffer_t ** pp_input_buffer )
328 {
329     int i;
330
331     for ( i = 0; i < i_nb_filters; i++ )
332     {
333         aout_filter_t * p_filter = pp_filters[i];
334         aout_buffer_t * p_output_buffer;
335
336         /* Resamplers can produce slightly more samples than (i_in_nb *
337          * p_filter->output.i_rate / p_filter->input.i_rate) so we need
338          * slightly bigger buffers. */
339         aout_BufferAlloc( &p_filter->output_alloc,
340             ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
341             * 1000000 / p_filter->input.i_rate,
342             *pp_input_buffer, p_output_buffer );
343         if ( p_output_buffer == NULL )
344         {
345             msg_Err( p_aout, "out of memory" );
346             return;
347         }
348         /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
349          * shall be set by the filter plug-in. */
350
351         p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
352                               p_output_buffer );
353
354         if ( !p_filter->b_in_place )
355         {
356             aout_BufferFree( *pp_input_buffer );
357             *pp_input_buffer = p_output_buffer;
358         }
359     }
360 }
361