]> git.sesse.net Git - vlc/blob - src/audio_output/filters.c
bb75a89ef67f45b3b0162a18bce93d5a853d375a
[vlc] / src / audio_output / filters.c
1 /*****************************************************************************
2  * filters.c : audio output filters management
3  *****************************************************************************
4  * Copyright (C) 2002-2007 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 <vlc_cpu.h>
40 #include "aout_internal.h"
41 #include <libvlc.h>
42
43 /*****************************************************************************
44  * FindFilter: find an audio filter for a specific transformation
45  *****************************************************************************/
46 static filter_t * FindFilter( vlc_object_t *obj,
47                               const audio_sample_format_t *infmt,
48                               const audio_sample_format_t *outfmt )
49 {
50     static const char typename[] = "audio converter";
51     const char *type = "audio converter", *name = NULL;
52     filter_t * p_filter;
53
54     p_filter = vlc_custom_create( obj, sizeof(*p_filter), typename );
55
56     if ( p_filter == NULL ) return NULL;
57
58     p_filter->fmt_in.audio = *infmt;
59     p_filter->fmt_in.i_codec = infmt->i_format;
60     p_filter->fmt_out.audio = *outfmt;
61     p_filter->fmt_out.i_codec = outfmt->i_format;
62
63     if( infmt->i_format == outfmt->i_format
64      && infmt->i_physical_channels == outfmt->i_physical_channels
65      && infmt->i_original_channels == outfmt->i_original_channels )
66     {
67         assert( infmt->i_rate != outfmt->i_rate );
68         type = "audio resampler";
69         name = "$audio-resampler";
70     }
71
72     p_filter->p_module = module_need( p_filter, type, name, 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  * Splits audio format conversion in two simpler conversions
85  * @return 0 on successful split, -1 if the input and output formats are too
86  * similar to split the conversion.
87  */
88 static int SplitConversion( const audio_sample_format_t *restrict infmt,
89                             const audio_sample_format_t *restrict outfmt,
90                             audio_sample_format_t *midfmt )
91 {
92     *midfmt = *outfmt;
93
94     /* Lastly: resample (after format conversion and remixing) */
95     if( infmt->i_rate != outfmt->i_rate )
96         midfmt->i_rate = infmt->i_rate;
97     else
98     /* Penultimately: remix channels (after format conversion) */
99     if( infmt->i_physical_channels != outfmt->i_physical_channels
100      || infmt->i_original_channels != outfmt->i_original_channels )
101     {
102         midfmt->i_physical_channels = infmt->i_physical_channels;
103         midfmt->i_original_channels = infmt->i_original_channels;
104     }
105     else
106     /* Second: convert linear to S16N as intermediate format */
107     if( AOUT_FMT_LINEAR( infmt ) )
108     {
109         /* All conversion from linear to S16N must be supported directly. */
110         if( outfmt->i_format == VLC_CODEC_S16N )
111             return -1;
112         midfmt->i_format = VLC_CODEC_S16N;
113     }
114     else
115     /* First: convert non-linear to FI32 as intermediate format */
116     {
117         if( outfmt->i_format == VLC_CODEC_FI32 )
118             return -1;
119         midfmt->i_format = VLC_CODEC_FI32;
120     }
121
122     assert( !AOUT_FMTS_IDENTICAL( infmt, midfmt ) );
123     aout_FormatPrepare( midfmt );
124     return 0;
125 }
126
127 #undef aout_FiltersCreatePipeline
128 /**
129  * Allocates audio format conversion filters
130  * @param obj parent VLC object for new filters
131  * @param filters table of filters [IN/OUT]
132  * @param nb_filters pointer to the number of filters in the table [IN/OUT]
133  * @param max_filters size of filters table [IN]
134  * @param infmt input audio format
135  * @param outfmt output audio format
136  * @return 0 on success, -1 on failure
137  */
138 int aout_FiltersCreatePipeline( vlc_object_t *obj, filter_t **filters,
139                                 unsigned *nb_filters, unsigned max_filters,
140                                 const audio_sample_format_t *restrict infmt,
141                                 const audio_sample_format_t *restrict outfmt )
142 {
143     audio_sample_format_t curfmt = *outfmt;
144     unsigned i = 0;
145
146     max_filters -= *nb_filters;
147     filters += *nb_filters;
148     aout_FormatsPrint( obj, "filter(s)", infmt, outfmt );
149
150     while( !AOUT_FMTS_IDENTICAL( infmt, &curfmt ) )
151     {
152         if( i >= max_filters )
153         {
154             msg_Err( obj, "maximum of %u filters reached", max_filters );
155             dialog_Fatal( obj, _("Audio filtering failed"),
156                           _("The maximum number of filters (%u) was reached."),
157                           max_filters );
158             goto rollback;
159         }
160
161         /* Make room and prepend a filter */
162         memmove( filters + 1, filters, i * sizeof( *filters ) );
163
164         *filters = FindFilter( obj, infmt, &curfmt );
165         if( *filters != NULL )
166         {
167             i++;
168             break; /* done! */
169         }
170
171         audio_sample_format_t midfmt;
172         /* Split the conversion */
173         if( SplitConversion( infmt, &curfmt, &midfmt ) )
174         {
175             msg_Err( obj, "conversion pipeline failed: %4.4s -> %4.4s",
176                      (const char *)&infmt->i_format,
177                      (const char *)&outfmt->i_format );
178             goto rollback;
179         }
180
181         *filters = FindFilter( obj, &midfmt, &curfmt );
182         if( *filters == NULL )
183         {
184             msg_Err( obj, "cannot find filter for simple conversion" );
185             goto rollback;
186         }
187         curfmt = midfmt;
188         i++;
189     }
190
191     msg_Dbg( obj, "conversion pipeline completed" );
192     *nb_filters += i;
193     return 0;
194
195 rollback:
196     aout_FiltersDestroyPipeline( filters, i );
197     return -1;
198 }
199
200 /**
201  * Destroys a chain of audio filters.
202  */
203 void aout_FiltersDestroyPipeline( filter_t *const *filters, unsigned n )
204 {
205     for( unsigned i = 0; i < n; i++ )
206     {
207         filter_t *p_filter = filters[i];
208
209         module_unneed( p_filter, p_filter->p_module );
210         vlc_object_release( p_filter );
211     }
212 }
213
214 /**
215  * Filters an audio buffer through a chain of filters.
216  */
217 void aout_FiltersPlay( filter_t *const *pp_filters,
218                        unsigned i_nb_filters, block_t ** pp_block )
219 {
220     block_t *p_block = *pp_block;
221
222     /* TODO: use filter chain */
223     for( unsigned i = 0; (i < i_nb_filters) && (p_block != NULL); i++ )
224     {
225         filter_t * p_filter = pp_filters[i];
226
227         /* Please note that p_block->i_nb_samples & i_buffer
228          * shall be set by the filter plug-in. */
229         p_block = p_filter->pf_audio_filter( p_filter, p_block );
230     }
231     *pp_block = p_block;
232 }