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