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