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