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