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