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