]> git.sesse.net Git - vlc/blob - src/audio_output/filters.c
Major change of the channels management. p_format->i_channels disappeares
[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.14 2002/11/14 22:38:48 massiot 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( aout_instance_t * p_aout,
78                              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             return 1;
102         }
103
104         /* !b_rate */
105         p_middle_format->i_physical_channels
106              = p_input_format->i_physical_channels;
107         p_middle_format->i_original_channels
108              = p_input_format->i_original_channels;
109         return 1;
110     }
111
112     /* i_nb_conversion == 3 */
113     p_middle_format->i_rate = p_input_format->i_rate;
114     return 2;
115 }
116
117 /*****************************************************************************
118  * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
119  *                             format to another
120  *****************************************************************************
121  * TODO : allow the user to add/remove specific filters
122  *****************************************************************************/
123 int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
124                                 aout_filter_t ** pp_filters,
125                                 int * pi_nb_filters,
126                                 const audio_sample_format_t * p_input_format,
127                                 const audio_sample_format_t * p_output_format )
128 {
129     audio_sample_format_t temp_format;
130     int i_nb_conversions;
131
132     if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
133     {
134         msg_Dbg( p_aout, "no need for any filter" );
135         *pi_nb_filters = 0;
136         return 0;
137     }
138
139     aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
140
141     /* Try to find a filter to do the whole conversion. */
142     pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
143     if ( pp_filters[0] != NULL )
144     {
145         msg_Dbg( p_aout, "found a filter for the whole conversion" );
146         *pi_nb_filters = 1;
147         return 0;
148     }
149
150     /* We'll have to split the conversion. We always do the downmixing
151      * before the resampling, because the audio decoder can probably do it
152      * for us. */
153     i_nb_conversions = SplitConversion( p_aout, p_input_format,
154                                         p_output_format, &temp_format );
155     if ( !i_nb_conversions )
156     {
157         /* There was only one conversion to do, and we already failed. */
158         msg_Err( p_aout, "couldn't find a filter for the conversion" );
159         return -1;
160     }
161
162     pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
163     if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
164     {
165         /* Try with only one conversion. */
166         SplitConversion( p_aout, p_input_format, &temp_format,
167                          &temp_format );
168         pp_filters[0] = FindFilter( p_aout, p_input_format,
169                                     &temp_format );
170     }
171     if ( pp_filters[0] == NULL )
172     {
173         msg_Err( p_aout,
174               "couldn't find a filter for the first part of the conversion" );
175         return -1;
176     }
177
178     /* We have the first stage of the conversion. Find a filter for
179      * the rest. */
180     pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
181                                 p_output_format );
182     if ( pp_filters[1] == NULL )
183     {
184         /* Try to split the conversion. */
185         i_nb_conversions = SplitConversion( p_aout,
186                                     &pp_filters[0]->output,
187                                     p_output_format, &temp_format );
188         if ( !i_nb_conversions )
189         {
190             vlc_object_detach( pp_filters[0] );
191             vlc_object_destroy( pp_filters[0] );
192             msg_Err( p_aout,
193               "couldn't find a filter for the second part of the conversion" );
194         }
195         pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
196                                     &temp_format );
197         pp_filters[2] = FindFilter( p_aout, &temp_format,
198                                     p_output_format );
199
200         if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
201         {
202             vlc_object_detach( pp_filters[0] );
203             vlc_object_destroy( pp_filters[0] );
204             if ( pp_filters[1] != NULL )
205             {
206                 vlc_object_detach( pp_filters[1] );
207                 vlc_object_destroy( pp_filters[1] );
208             }
209             if ( pp_filters[2] != NULL )
210             {
211                 vlc_object_detach( pp_filters[2] );
212                 vlc_object_destroy( pp_filters[2] );
213             }
214             msg_Err( p_aout,
215                "couldn't find filters for the second part of the conversion" );
216         }
217         *pi_nb_filters = 3;
218     }
219     else
220     {
221         *pi_nb_filters = 2;
222     }
223
224     /* We have enough filters. */
225     msg_Dbg( p_aout, "found %d filters for the whole conversion",
226              *pi_nb_filters );
227     return 0;
228 }
229
230 /*****************************************************************************
231  * aout_FiltersDestroyPipeline: deallocate a filters pipeline
232  *****************************************************************************/
233 void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
234                                   aout_filter_t ** pp_filters,
235                                   int i_nb_filters )
236 {
237     int i;
238
239     for ( i = 0; i < i_nb_filters; i++ )
240     {
241         module_Unneed( pp_filters[i], pp_filters[i]->p_module );
242         vlc_object_detach( pp_filters[i] );
243         vlc_object_destroy( pp_filters[i] );
244     }
245 }
246
247 /*****************************************************************************
248  * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
249  *                          buffer allocations
250  *****************************************************************************/
251 void aout_FiltersHintBuffers( aout_instance_t * p_aout,
252                               aout_filter_t ** pp_filters,
253                               int i_nb_filters, aout_alloc_t * p_first_alloc )
254 {
255     int i;
256
257     for ( i = i_nb_filters - 1; i >= 0; i-- )
258     {
259         aout_filter_t * p_filter = pp_filters[i];
260
261         int i_output_size = p_filter->output.i_bytes_per_frame
262                              * p_filter->output.i_rate
263                              / p_filter->output.i_frame_length;
264         int i_input_size = p_filter->input.i_bytes_per_frame
265                              * p_filter->input.i_rate
266                              / p_filter->input.i_frame_length;
267
268         p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
269                                                 i_output_size );
270
271         if ( p_filter->b_in_place )
272         {
273             p_first_alloc->i_bytes_per_sec = __MAX(
274                                          p_first_alloc->i_bytes_per_sec,
275                                          i_input_size );
276             p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
277         }
278         else
279         {
280             /* We're gonna need a buffer allocation. */
281             memcpy( &p_filter->output_alloc, p_first_alloc,
282                     sizeof(aout_alloc_t) );
283             p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
284             p_first_alloc->i_bytes_per_sec = i_input_size;
285         }
286     }
287 }
288
289 /*****************************************************************************
290  * aout_FiltersPlay: play a buffer
291  *****************************************************************************/
292 void aout_FiltersPlay( aout_instance_t * p_aout,
293                        aout_filter_t ** pp_filters,
294                        int i_nb_filters, aout_buffer_t ** pp_input_buffer )
295 {
296     int i;
297
298     for ( i = 0; i < i_nb_filters; i++ )
299     {
300         aout_filter_t * p_filter = pp_filters[i];
301         aout_buffer_t * p_output_buffer;
302
303         /* We need this because resamplers can produce more samples than
304            (i_in_nb * p_filter->output.i_rate / p_filter->input.i_rate) */
305         int i_compensate_rounding = 2 * p_filter->input.i_rate
306             / p_filter->output.i_rate;
307
308         aout_BufferAlloc( &p_filter->output_alloc,
309             ((mtime_t)(*pp_input_buffer)->i_nb_samples + i_compensate_rounding)
310             * 1000000 / p_filter->input.i_rate,
311             *pp_input_buffer, p_output_buffer );
312         if ( p_output_buffer == NULL )
313         {
314             msg_Err( p_aout, "out of memory" );
315             return;
316         }
317         /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
318          * shall be set by the filter plug-in. */
319
320         p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
321                               p_output_buffer );
322
323         if ( !p_filter->b_in_place )
324         {
325             aout_BufferFree( *pp_input_buffer );
326             *pp_input_buffer = p_output_buffer;
327         }
328     }
329 }
330