]> git.sesse.net Git - vlc/blob - src/audio_output/filters.c
aout: rationalize filters pipelines function names (cosmetic)
[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 <string.h>
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_dialog.h>
36 #include <vlc_modules.h>
37 #include <vlc_aout.h>
38 #include <vlc_filter.h>
39 #include <vlc_vout.h>                  /* for vout_Request */
40
41 #include <libvlc.h>
42 #include "aout_internal.h"
43
44 /*****************************************************************************
45  * FindFilter: find an audio filter for a specific transformation
46  *****************************************************************************/
47 static filter_t * FindFilter( vlc_object_t *obj,
48                               const audio_sample_format_t *infmt,
49                               const audio_sample_format_t *outfmt )
50 {
51     static const char typename[] = "audio converter";
52     const char *type = "audio converter", *name = NULL;
53     filter_t * p_filter;
54
55     p_filter = vlc_custom_create( obj, sizeof(*p_filter), typename );
56
57     if ( p_filter == NULL ) return NULL;
58
59     p_filter->fmt_in.audio = *infmt;
60     p_filter->fmt_in.i_codec = infmt->i_format;
61     p_filter->fmt_out.audio = *outfmt;
62     p_filter->fmt_out.i_codec = outfmt->i_format;
63
64     if( infmt->i_format == outfmt->i_format
65      && infmt->i_physical_channels == outfmt->i_physical_channels
66      && infmt->i_original_channels == outfmt->i_original_channels )
67     {
68         assert( infmt->i_rate != outfmt->i_rate );
69         type = "audio resampler";
70         name = "$audio-resampler";
71     }
72
73     p_filter->p_module = module_need( p_filter, type, name, false );
74     if ( p_filter->p_module == NULL )
75     {
76         vlc_object_release( p_filter );
77         return NULL;
78     }
79
80     assert( p_filter->pf_audio_filter );
81     return p_filter;
82 }
83
84 /**
85  * Splits audio format conversion in two simpler conversions
86  * @return 0 on successful split, -1 if the input and output formats are too
87  * similar to split the conversion.
88  */
89 static int SplitConversion( const audio_sample_format_t *restrict infmt,
90                             const audio_sample_format_t *restrict outfmt,
91                             audio_sample_format_t *midfmt )
92 {
93     *midfmt = *outfmt;
94
95     /* Lastly: resample (after format conversion and remixing) */
96     if( infmt->i_rate != outfmt->i_rate )
97         midfmt->i_rate = infmt->i_rate;
98     else
99     /* Penultimately: remix channels (after format conversion) */
100     if( infmt->i_physical_channels != outfmt->i_physical_channels
101      || infmt->i_original_channels != outfmt->i_original_channels )
102     {
103         midfmt->i_physical_channels = infmt->i_physical_channels;
104         midfmt->i_original_channels = infmt->i_original_channels;
105     }
106     else
107     /* Second: convert linear to S16N as intermediate format */
108     if( AOUT_FMT_LINEAR( infmt ) )
109     {
110         /* All conversion from linear to S16N must be supported directly. */
111         if( outfmt->i_format == VLC_CODEC_S16N )
112             return -1;
113         midfmt->i_format = VLC_CODEC_S16N;
114     }
115     else
116     /* First: convert non-linear to FI32 as intermediate format */
117     {
118         if( outfmt->i_format == VLC_CODEC_FI32 )
119             return -1;
120         midfmt->i_format = VLC_CODEC_FI32;
121     }
122
123     assert( !AOUT_FMTS_IDENTICAL( infmt, midfmt ) );
124     aout_FormatPrepare( midfmt );
125     return 0;
126 }
127
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_FiltersPipelineCreate)(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_FiltersPipelineDestroy (filters, i);
197     return -1;
198 }
199
200 /**
201  * Destroys a chain of audio filters.
202  */
203 void aout_FiltersPipelineDestroy(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 static inline bool ChangeFiltersString (vlc_object_t *aout, const char *var,
215                                         const char *filter, bool add)
216 {
217     return aout_ChangeFilterString (aout, aout, var, filter, add);
218 }
219
220 /**
221  * Filters an audio buffer through a chain of filters.
222  */
223 block_t *aout_FiltersPipelinePlay(filter_t *const *filters, unsigned count,
224                                   block_t *block)
225 {
226     /* TODO: use filter chain */
227     for (unsigned i = 0; (i < count) && (block != NULL); i++)
228     {
229         filter_t *filter = filters[i];
230
231         /* Please note that p_block->i_nb_samples & i_buffer
232          * shall be set by the filter plug-in. */
233         block = filter->pf_audio_filter (filter, block);
234     }
235     return block;
236 }
237
238 /** Callback for visualization selection */
239 static int VisualizationCallback (vlc_object_t *obj, char const *var,
240                                   vlc_value_t oldval, vlc_value_t newval,
241                                   void *data)
242 {
243     audio_output_t *aout = (audio_output_t *)obj;
244     const char *mode = newval.psz_string;
245
246     if (!*mode)
247     {
248         ChangeFiltersString (obj, "audio-visual", "goom", false);
249         ChangeFiltersString (obj, "audio-visual", "visual", false);
250         ChangeFiltersString (obj, "audio-visual", "projectm", false);
251         ChangeFiltersString (obj, "audio-visual", "vsxu", false);
252     }
253     else if (!strcmp ("goom", mode))
254     {
255         ChangeFiltersString (obj, "audio-visual", "visual", false );
256         ChangeFiltersString (obj, "audio-visual", "goom", true );
257         ChangeFiltersString (obj, "audio-visual", "projectm", false );
258         ChangeFiltersString (obj, "audio-visual", "vsxu", false);
259     }
260     else if (!strcmp ("projectm", mode))
261     {
262         ChangeFiltersString (obj, "audio-visual", "visual", false);
263         ChangeFiltersString (obj, "audio-visual", "goom", false);
264         ChangeFiltersString (obj, "audio-visual", "projectm", true);
265         ChangeFiltersString (obj, "audio-visual", "vsxu", false);
266     }
267     else if (!strcmp ("vsxu", mode))
268     {
269         ChangeFiltersString (obj, "audio-visual", "visual", false);
270         ChangeFiltersString (obj, "audio-visual", "goom", false);
271         ChangeFiltersString (obj, "audio-visual", "projectm", false);
272         ChangeFiltersString (obj, "audio-visual", "vsxu", true);
273     }
274     else
275     {
276         var_Create (obj, "effect-list", VLC_VAR_STRING);
277         var_SetString (obj, "effect-list", mode);
278
279         ChangeFiltersString (obj, "audio-visual", "goom", false);
280         ChangeFiltersString (obj, "audio-visual", "visual", true);
281         ChangeFiltersString (obj, "audio-visual", "projectm", false);
282     }
283
284     aout_InputRequestRestart (aout);
285     (void) var; (void) oldval; (void) data;
286     return VLC_SUCCESS;
287 }
288
289 static int EqualizerCallback (vlc_object_t *obj, char const *var,
290                               vlc_value_t oldval, vlc_value_t newval,
291                               void *data)
292 {
293     audio_output_t *aout = (audio_output_t *)obj;
294     char *mode = newval.psz_string;
295     bool ret;
296
297     if (!*mode)
298         ret = ChangeFiltersString (obj, "audio-filter", "equalizer", false);
299     else
300     {
301         var_Create (obj, "equalizer-preset", VLC_VAR_STRING);
302         var_SetString (obj, "equalizer-preset", mode);
303         ret = ChangeFiltersString (obj, "audio-filter", "equalizer", true);
304     }
305
306     /* That sucks */
307     if (ret)
308         aout_InputRequestRestart (aout);
309     (void) var; (void) oldval; (void) data;
310     return VLC_SUCCESS;
311 }
312
313 static vout_thread_t *RequestVout (void *data, vout_thread_t *vout,
314                                    video_format_t *fmt, bool recycle)
315 {
316     audio_output_t *aout = data;
317     vout_configuration_t cfg = {
318         .vout       = vout,
319         .input      = NULL,
320         .change_fmt = true,
321         .fmt        = fmt,
322         .dpb_size   = 1,
323     };
324
325     (void) recycle;
326     return vout_Request (aout, &cfg);
327 }
328
329 vout_thread_t *aout_filter_RequestVout (filter_t *filter, vout_thread_t *vout,
330                                         video_format_t *fmt)
331 {
332     /* NOTE: This only works from audio output.
333      * If you want to use visualization filters from another place, you will
334      * need to add a new pf_aout_request_vout callback or store a pointer
335      * to aout_request_vout_t inside filter_t (i.e. a level of indirection). */
336     aout_owner_t *owner = aout_owner ((audio_output_t *)filter->p_parent);
337     aout_request_vout_t *req = &owner->request_vout;
338
339     return req->pf_request_vout (req->p_private, vout, fmt,
340                                  owner->recycle_vout);
341 }
342
343 static filter_t *CreateFilter (vlc_object_t *parent, const char *name,
344                                const audio_sample_format_t *restrict infmt,
345                                const audio_sample_format_t *restrict outfmt,
346                                bool visu)
347 {
348     filter_t *filter = vlc_custom_create (parent, sizeof (*filter),
349                                           "audio filter");
350     if (unlikely(filter == NULL))
351         return NULL;
352
353     /*filter->p_owner = NOT NEEDED;*/
354     filter->fmt_in.i_codec = infmt->i_format;
355     filter->fmt_in.audio = *infmt;
356     filter->fmt_out.i_codec = outfmt->i_format;
357     filter->fmt_out.audio = *outfmt;
358
359     if (!visu)
360     {
361         filter->p_module = module_need (filter, "audio filter", name, true);
362         if (filter->p_module != NULL)
363             return filter;
364
365         /* If probing failed, formats shall not have been modified. */
366         assert (AOUT_FMTS_IDENTICAL(&filter->fmt_in.audio, infmt));
367         assert (AOUT_FMTS_IDENTICAL(&filter->fmt_out.audio, outfmt));
368     }
369
370     filter->p_module = module_need (filter, "visualization2", name, true);
371     if (filter->p_module != NULL)
372         return filter;
373
374     vlc_object_release (filter);
375     return NULL;
376 }
377
378 /**
379  * Sets up the audio filters.
380  */
381 int aout_FiltersNew (audio_output_t *aout,
382                      const audio_sample_format_t *restrict infmt,
383                      const audio_sample_format_t *restrict outfmt,
384                      const aout_request_vout_t *request_vout)
385 {
386     aout_owner_t *owner = aout_owner (aout);
387
388     /* Prepare format structure */
389     aout_FormatPrint (aout, "input", infmt);
390     audio_sample_format_t input_format = *infmt;
391     audio_sample_format_t output_format = *outfmt;
392
393     /* Now add user filters */
394     owner->nb_filters = 0;
395     owner->rate_filter = NULL;
396     owner->resampler = NULL;
397
398     var_AddCallback (aout, "visual", VisualizationCallback, NULL);
399     var_AddCallback (aout, "equalizer", EqualizerCallback, NULL);
400
401     if (!AOUT_FMT_LINEAR(outfmt))
402         return 0;
403
404     const char *scaletempo =
405         var_InheritBool (aout, "audio-time-stretch") ? "scaletempo" : NULL;
406     char *filters = var_InheritString (aout, "audio-filter");
407     char *visual = var_InheritString (aout, "audio-visual");
408
409     if (request_vout != NULL)
410         owner->request_vout = *request_vout;
411     else
412     {
413         owner->request_vout.pf_request_vout = RequestVout;
414         owner->request_vout.p_private = aout;
415     }
416     owner->recycle_vout = (visual != NULL) && *visual;
417
418     /* parse user filter lists */
419     const char *list[AOUT_MAX_FILTERS];
420     unsigned n = 0;
421
422     if (scaletempo != NULL)
423         list[n++] = scaletempo;
424     if (filters != NULL)
425     {
426         char *p = filters, *name;
427         while ((name = strsep (&p, " :")) != NULL && n < AOUT_MAX_FILTERS)
428             list[n++] = name;
429     }
430     if (visual != NULL && n < AOUT_MAX_FILTERS)
431         list[n++] = visual;
432
433     for (unsigned i = 0; i < n; i++)
434     {
435         const char *name = list[i];
436
437         if (owner->nb_filters >= AOUT_MAX_FILTERS)
438         {
439             msg_Err (aout, "maximum of %u filters reached", AOUT_MAX_FILTERS);
440             msg_Err (aout, "cannot add user filter %s (skipped)", name);
441             break;
442         }
443
444         filter_t *filter = CreateFilter (VLC_OBJECT(aout), name,
445                                          &input_format, &output_format,
446                                          name == visual);
447         if (filter == NULL)
448         {
449             msg_Err (aout, "cannot add user filter %s (skipped)", name);
450             continue;
451         }
452
453         /* convert to the filter input format if necessary */
454         if (aout_FiltersPipelineCreate (aout, owner->filters,
455                                         &owner->nb_filters,
456                                         AOUT_MAX_FILTERS - 1,
457                                         &input_format, &filter->fmt_in.audio))
458         {
459             msg_Err (aout, "cannot add user filter %s (skipped)", name);
460             module_unneed (filter, filter->p_module);
461             vlc_object_release (filter);
462             continue;
463         }
464
465         assert (owner->nb_filters < AOUT_MAX_FILTERS);
466         owner->filters[owner->nb_filters++] = filter;
467         input_format = filter->fmt_out.audio;
468
469         if (name == scaletempo)
470             owner->rate_filter = filter;
471     }
472     free (visual);
473     free (filters);
474
475     /* convert to the output format (minus resampling) if necessary */
476     output_format.i_rate = input_format.i_rate;
477     if (aout_FiltersPipelineCreate (aout, owner->filters, &owner->nb_filters,
478                                     AOUT_MAX_FILTERS,
479                                     &input_format, &output_format))
480     {
481         msg_Err (aout, "cannot setup filtering pipeline");
482         goto error;
483     }
484     input_format = output_format;
485
486     /* insert the resampler */
487     output_format.i_rate = outfmt->i_rate;
488     assert (AOUT_FMTS_IDENTICAL(&output_format, outfmt));
489
490     unsigned rate_bak = input_format.i_rate;
491     if (output_format.i_rate == input_format.i_rate)
492         /* For historical reasons, a different rate is required to probe
493          * resampling filters. */
494         input_format.i_rate++;
495     owner->resampler = FindFilter (VLC_OBJECT(aout), &input_format,
496                                    &output_format);
497     if (owner->resampler == NULL)
498     {
499         msg_Err (aout, "cannot setup a resampler");
500         goto error;
501     }
502     owner->resampler->fmt_in.audio.i_rate = rate_bak;
503     if (owner->rate_filter == NULL)
504         owner->rate_filter = owner->resampler;
505
506     return 0;
507
508 error:
509     aout_FiltersPipelineDestroy (owner->filters, owner->nb_filters);
510     var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
511     var_DelCallback (aout, "visual", VisualizationCallback, NULL);
512     return -1;
513 }
514
515 /**
516  * Destroys the audio filters.
517  */
518 void aout_FiltersDelete (audio_output_t *aout)
519 {
520     aout_owner_t *owner = aout_owner (aout);
521
522     if (owner->resampler != NULL)
523         aout_FiltersPipelineDestroy (&owner->resampler, 1);
524     aout_FiltersPipelineDestroy (owner->filters, owner->nb_filters);
525     var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
526     var_DelCallback (aout, "visual", VisualizationCallback, NULL);
527
528     /* XXX We need to update recycle_vout before calling
529      * aout_FiltersPipelineDestroy().
530      * FIXME There may be a race condition if audio-visual is updated between
531      * aout_FiltersDestroy() and the next aout_FiltersNew().
532      */
533     char *visual = var_InheritString (aout, "audio-visual");
534     owner->recycle_vout = (visual != NULL) && *visual;
535     free (visual);
536 }