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