]> git.sesse.net Git - vlc/blob - src/audio_output/filters.c
decoder: inline DecoderSignalWait()
[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 #include <vlc_input.h>
41
42 #include <libvlc.h>
43 #include "aout_internal.h"
44
45 static filter_t *CreateFilter (vlc_object_t *obj, const char *type,
46                                const char *name, filter_owner_sys_t *owner,
47                                const audio_sample_format_t *infmt,
48                                const audio_sample_format_t *outfmt)
49 {
50     filter_t *filter = vlc_custom_create (obj, sizeof (*filter), type);
51     if (unlikely(filter == NULL))
52         return NULL;
53
54     filter->owner.sys = owner;
55     filter->fmt_in.audio = *infmt;
56     filter->fmt_in.i_codec = infmt->i_format;
57     filter->fmt_out.audio = *outfmt;
58     filter->fmt_out.i_codec = outfmt->i_format;
59     filter->p_module = module_need (filter, type, name, false);
60     if (filter->p_module == NULL)
61     {
62         /* If probing failed, formats shall not have been modified. */
63         assert (AOUT_FMTS_IDENTICAL(&filter->fmt_in.audio, infmt));
64         assert (AOUT_FMTS_IDENTICAL(&filter->fmt_out.audio, outfmt));
65         vlc_object_release (filter);
66         filter = NULL;
67     }
68     else
69         assert (filter->pf_audio_filter != NULL);
70     return filter;
71 }
72
73 static filter_t *FindConverter (vlc_object_t *obj,
74                                 const audio_sample_format_t *infmt,
75                                 const audio_sample_format_t *outfmt)
76 {
77     return CreateFilter (obj, "audio converter", NULL, NULL, infmt, outfmt);
78 }
79
80 static filter_t *FindResampler (vlc_object_t *obj,
81                                 const audio_sample_format_t *infmt,
82                                 const audio_sample_format_t *outfmt)
83 {
84     return CreateFilter (obj, "audio resampler", "$audio-resampler", NULL,
85                          infmt, outfmt);
86 }
87
88 /**
89  * Destroys a chain of audio filters.
90  */
91 static void aout_FiltersPipelineDestroy(filter_t *const *filters, unsigned n)
92 {
93     for( unsigned i = 0; i < n; i++ )
94     {
95         filter_t *p_filter = filters[i];
96
97         module_unneed( p_filter, p_filter->p_module );
98         vlc_object_release( p_filter );
99     }
100 }
101
102 static filter_t *TryFormat (vlc_object_t *obj, vlc_fourcc_t codec,
103                             audio_sample_format_t *restrict fmt)
104 {
105     audio_sample_format_t output = *fmt;
106
107     assert (codec != fmt->i_format);
108     output.i_format = codec;
109     aout_FormatPrepare (&output);
110
111     filter_t *filter = FindConverter (obj, fmt, &output);
112     if (filter != NULL)
113         *fmt = output;
114     return filter;
115 }
116
117 /**
118  * Allocates audio format conversion filters
119  * @param obj parent VLC object for new filters
120  * @param filters table of filters [IN/OUT]
121  * @param count pointer to the number of filters in the table [IN/OUT]
122  * @param max size of filters table [IN]
123  * @param infmt input audio format
124  * @param outfmt output audio format
125  * @return 0 on success, -1 on failure
126  */
127 static int aout_FiltersPipelineCreate(vlc_object_t *obj, filter_t **filters,
128                                       unsigned *count, unsigned max,
129                                  const audio_sample_format_t *restrict infmt,
130                                  const audio_sample_format_t *restrict outfmt)
131 {
132     aout_FormatsPrint (obj, "conversion:", infmt, outfmt);
133     max -= *count;
134     filters += *count;
135
136     /* There is a lot of second guessing on what the conversion plugins can
137      * and cannot do. This seems hardly avoidable, the conversion problem need
138      * to be reduced somehow. */
139     audio_sample_format_t input = *infmt;
140     unsigned n = 0;
141
142     /* Encapsulate or decode non-linear formats */
143     if (!AOUT_FMT_LINEAR(infmt) && infmt->i_format != outfmt->i_format)
144     {
145         if (n == max)
146             goto overflow;
147
148         filter_t *f = TryFormat (obj, VLC_CODEC_S32N, &input);
149         if (f == NULL)
150             f = TryFormat (obj, VLC_CODEC_FL32, &input);
151         if (f == NULL)
152         {
153             msg_Err (obj, "cannot find %s for conversion pipeline",
154                      "decoder");
155             goto error;
156         }
157
158         filters[n++] = f;
159     }
160     assert (AOUT_FMT_LINEAR(&input));
161
162     /* Remix channels */
163     if (infmt->i_physical_channels != outfmt->i_physical_channels
164      || infmt->i_original_channels != outfmt->i_original_channels)
165     {   /* Remixing currently requires FL32... TODO: S16N */
166         if (input.i_format != VLC_CODEC_FL32)
167         {
168             if (n == max)
169                 goto overflow;
170
171             filter_t *f = TryFormat (obj, VLC_CODEC_FL32, &input);
172             if (f == NULL)
173             {
174                 msg_Err (obj, "cannot find %s for conversion pipeline",
175                          "pre-mix converter");
176                 goto error;
177             }
178
179             filters[n++] = f;
180         }
181
182         if (n == max)
183             goto overflow;
184
185         audio_sample_format_t output;
186         output.i_format = input.i_format;
187         output.i_rate = input.i_rate;
188         output.i_physical_channels = outfmt->i_physical_channels;
189         output.i_original_channels = outfmt->i_original_channels;
190         aout_FormatPrepare (&output);
191
192         filter_t *f = FindConverter (obj, &input, &output);
193         if (f == NULL)
194         {
195             msg_Err (obj, "cannot find %s for conversion pipeline",
196                      "remixer");
197             goto error;
198         }
199
200         input = output;
201         filters[n++] = f;
202     }
203
204     /* Resample */
205     if (input.i_rate != outfmt->i_rate)
206     {   /* Resampling works with any linear format, but may be ugly. */
207         if (n == max)
208             goto overflow;
209
210         audio_sample_format_t output = input;
211         output.i_rate = outfmt->i_rate;
212
213         filter_t *f = FindConverter (obj, &input, &output);
214         if (f == NULL)
215         {
216             msg_Err (obj, "cannot find %s for conversion pipeline",
217                      "resampler");
218             goto error;
219         }
220
221         input = output;
222         filters[n++] = f;
223     }
224
225     /* Format */
226     if (input.i_format != outfmt->i_format)
227     {
228         if (max == 0)
229             goto overflow;
230
231         filter_t *f = TryFormat (obj, outfmt->i_format, &input);
232         if (f == NULL)
233         {
234             msg_Err (obj, "cannot find %s for conversion pipeline",
235                      "post-mix converter");
236             goto error;
237         }
238         filters[n++] = f;
239     }
240
241     msg_Dbg (obj, "conversion pipeline complete");
242     *count += n;
243     return 0;
244
245 overflow:
246     msg_Err (obj, "maximum of %u conversion filters reached", max);
247     dialog_Fatal (obj, _("Audio filtering failed"),
248                   _("The maximum number of filters (%u) was reached."), max);
249 error:
250     aout_FiltersPipelineDestroy (filters, n);
251     return -1;
252 }
253
254 /**
255  * Filters an audio buffer through a chain of filters.
256  */
257 static block_t *aout_FiltersPipelinePlay(filter_t *const *filters,
258                                          unsigned count, block_t *block)
259 {
260     /* TODO: use filter chain */
261     for (unsigned i = 0; (i < count) && (block != NULL); i++)
262     {
263         filter_t *filter = filters[i];
264
265         /* Please note that p_block->i_nb_samples & i_buffer
266          * shall be set by the filter plug-in. */
267         block = filter->pf_audio_filter (filter, block);
268     }
269     return block;
270 }
271
272 #define AOUT_MAX_FILTERS 10
273
274 struct aout_filters
275 {
276     filter_t *rate_filter; /**< The filter adjusting samples count
277         (either the scaletempo filter or a resampler) */
278     filter_t *resampler; /**< The resampler */
279     int resampling; /**< Current resampling (Hz) */
280
281     unsigned count; /**< Number of filters */
282     filter_t *tab[AOUT_MAX_FILTERS]; /**< Configured user filters
283         (e.g. equalization) and their conversions */
284 };
285
286 /** Callback for visualization selection */
287 static int VisualizationCallback (vlc_object_t *obj, const char *var,
288                                   vlc_value_t oldval, vlc_value_t newval,
289                                   void *data)
290 {
291     const char *mode = newval.psz_string;
292
293     if (!*mode)
294         mode = "none";
295     /* FIXME: This ugly hack enforced by visual effect-list, as is the need for
296      * separate "visual" (external) and "audio-visual" (internal) variables...
297      * The visual plugin should have one submodule per effect instead. */
298     if (strcasecmp (mode, "none") && strcasecmp (mode, "goom")
299      && strcasecmp (mode, "projectm") && strcasecmp (mode, "vsxu")
300      && strcasecmp (mode, "glspectrum"))
301     {
302         var_Create (obj, "effect-list", VLC_VAR_STRING);
303         var_SetString (obj, "effect-list", mode);
304         mode = "visual";
305     }
306
307     var_SetString (obj, "audio-visual", mode);
308     aout_InputRequestRestart ((audio_output_t *)obj);
309     (void) var; (void) oldval; (void) data;
310     return VLC_SUCCESS;
311 }
312
313 vout_thread_t *aout_filter_RequestVout (filter_t *filter, vout_thread_t *vout,
314                                         video_format_t *fmt)
315 {
316     /* NOTE: This only works from aout_filters_t.
317      * If you want to use visualization filters from another place, you will
318      * need to add a new pf_aout_request_vout callback or store a pointer
319      * to aout_request_vout_t inside filter_t (i.e. a level of indirection). */
320     const aout_request_vout_t *req = filter->owner.sys;
321     char *visual = var_InheritString (filter->p_parent, "audio-visual");
322     /* NOTE: Disable recycling to always close the filter vout because OpenGL
323      * visualizations do not use this function to ask for a context. */
324     bool recycle = false;
325     free (visual);
326
327     return req->pf_request_vout (req->p_private, vout, fmt, recycle);
328 }
329
330 static int AppendFilter(vlc_object_t *obj, const char *type, const char *name,
331                         aout_filters_t *restrict filters, const void *owner,
332                         audio_sample_format_t *restrict infmt,
333                         const audio_sample_format_t *restrict outfmt)
334 {
335     const unsigned max = sizeof (filters->tab) / sizeof (filters->tab[0]);
336     if (filters->count >= max)
337     {
338         msg_Err (obj, "maximum of %u filters reached", max);
339         return -1;
340     }
341
342     filter_t *filter = CreateFilter (obj, type, name,
343                                      (void *)owner, infmt, outfmt);
344     if (filter == NULL)
345     {
346         msg_Err (obj, "cannot add user %s \"%s\" (skipped)", type, name);
347         return -1;
348     }
349
350     /* convert to the filter input format if necessary */
351     if (aout_FiltersPipelineCreate (obj, filters->tab, &filters->count,
352                                     max - 1, infmt, &filter->fmt_in.audio))
353     {
354         msg_Err (filter, "cannot add user %s \"%s\" (skipped)", type, name);
355         module_unneed (filter, filter->p_module);
356         vlc_object_release (filter);
357         return -1;
358     }
359
360     assert (filters->count < max);
361     filters->tab[filters->count] = filter;
362     filters->count++;
363     *infmt = filter->fmt_out.audio;
364     return 0;
365 }
366
367 #undef aout_FiltersNew
368 /**
369  * Sets a chain of audio filters up.
370  * \param obj parent object for the filters
371  * \param infmt chain input format [IN]
372  * \param outfmt chain output format [IN]
373  * \param request_vout visualization video output request callback
374  * \return a filters chain or NULL on failure
375  *
376  * \note
377  * *request_vout (if not NULL) must remain valid until aout_FiltersDelete().
378  *
379  * \bug
380  * If request_vout is non NULL, obj is assumed to be an audio_output_t pointer.
381  */
382 aout_filters_t *aout_FiltersNew (vlc_object_t *obj,
383                                  const audio_sample_format_t *restrict infmt,
384                                  const audio_sample_format_t *restrict outfmt,
385                                  const aout_request_vout_t *request_vout)
386 {
387     aout_filters_t *filters = malloc (sizeof (*filters));
388     if (unlikely(filters == NULL))
389         return NULL;
390
391     filters->rate_filter = NULL;
392     filters->resampler = NULL;
393     filters->resampling = 0;
394     filters->count = 0;
395
396     /* Prepare format structure */
397     aout_FormatPrint (obj, "input", infmt);
398     audio_sample_format_t input_format = *infmt;
399     audio_sample_format_t output_format = *outfmt;
400
401     /* Callbacks (before reading values and also before return statement) */
402     if (request_vout != NULL)
403         var_AddCallback (obj, "visual", VisualizationCallback, NULL);
404
405     /* Now add user filters */
406     if (!AOUT_FMT_LINEAR(outfmt))
407     {   /* Non-linear output: just convert formats, no filters/visu */
408         if (!AOUT_FMTS_IDENTICAL(infmt, outfmt))
409         {
410             aout_FormatsPrint (obj, "pass-through:", infmt, outfmt);
411             filters->tab[0] = FindConverter(obj, infmt, outfmt);
412             if (filters->tab[0] == NULL)
413             {
414                 msg_Err (obj, "cannot setup pass-through");
415                 goto error;
416             }
417             filters->count++;
418         }
419         return filters;
420     }
421
422     /* parse user filter lists */
423     if (var_InheritBool (obj, "audio-time-stretch"))
424     {
425         if (AppendFilter(obj, "audio filter", "scaletempo",
426                          filters, NULL, &input_format, &output_format) == 0)
427             filters->rate_filter = filters->tab[filters->count - 1];
428     }
429
430     char *str = var_InheritString (obj, "audio-filter");
431     if (str != NULL)
432     {
433         char *p = str, *name;
434         while ((name = strsep (&p, " :")) != NULL)
435         {
436             AppendFilter(obj, "audio filter", name, filters,
437                          NULL, &input_format, &output_format);
438         }
439         free (str);
440     }
441
442     if (request_vout != NULL)
443     {
444         char *visual = var_InheritString (obj, "audio-visual");
445         if (visual != NULL && strcasecmp (visual, "none"))
446             AppendFilter(obj, "visualization", visual, filters,
447                          request_vout, &input_format, &output_format);
448         free (visual);
449     }
450
451     /* convert to the output format (minus resampling) if necessary */
452     output_format.i_rate = input_format.i_rate;
453     if (aout_FiltersPipelineCreate (obj, filters->tab, &filters->count,
454                               AOUT_MAX_FILTERS, &input_format, &output_format))
455     {
456         msg_Err (obj, "cannot setup filtering pipeline");
457         goto error;
458     }
459     input_format = output_format;
460
461     /* insert the resampler */
462     output_format.i_rate = outfmt->i_rate;
463     assert (AOUT_FMTS_IDENTICAL(&output_format, outfmt));
464     filters->resampler = FindResampler (obj, &input_format,
465                                         &output_format);
466     if (filters->resampler == NULL && input_format.i_rate != outfmt->i_rate)
467     {
468         msg_Err (obj, "cannot setup a resampler");
469         goto error;
470     }
471     if (filters->rate_filter == NULL)
472         filters->rate_filter = filters->resampler;
473
474     return filters;
475
476 error:
477     aout_FiltersPipelineDestroy (filters->tab, filters->count);
478     if (request_vout != NULL)
479         var_DelCallback (obj, "visual", VisualizationCallback, NULL);
480     free (filters);
481     return NULL;
482 }
483
484 #undef aout_FiltersDelete
485 /**
486  * Destroys a chain of audio filters.
487  * \param obj object used with aout_FiltersNew()
488  * \param filters chain to be destroyed
489  * \bug
490  * obj must be NULL iff request_vout was NULL in aout_FiltersNew()
491  * (this implies obj is an audio_output_t pointer if non NULL).
492  */
493 void aout_FiltersDelete (vlc_object_t *obj, aout_filters_t *filters)
494 {
495     if (filters->resampler != NULL)
496         aout_FiltersPipelineDestroy (&filters->resampler, 1);
497     aout_FiltersPipelineDestroy (filters->tab, filters->count);
498     if (obj != NULL)
499         var_DelCallback (obj, "visual", VisualizationCallback, NULL);
500     free (filters);
501 }
502
503 bool aout_FiltersAdjustResampling (aout_filters_t *filters, int adjust)
504 {
505     if (filters->resampler == NULL)
506         return false;
507
508     if (adjust)
509         filters->resampling += adjust;
510     else
511         filters->resampling = 0;
512     return filters->resampling != 0;
513 }
514
515 block_t *aout_FiltersPlay (aout_filters_t *filters, block_t *block, int rate)
516 {
517     int nominal_rate = 0;
518
519     if (rate != INPUT_RATE_DEFAULT)
520     {
521         filter_t *rate_filter = filters->rate_filter;
522
523         if (rate_filter == NULL)
524             goto drop; /* Without linear, non-nominal rate is impossible. */
525
526         /* Override input rate */
527         nominal_rate = rate_filter->fmt_in.audio.i_rate;
528         rate_filter->fmt_in.audio.i_rate =
529             (nominal_rate * INPUT_RATE_DEFAULT) / rate;
530     }
531
532     block = aout_FiltersPipelinePlay (filters->tab, filters->count, block);
533     if (filters->resampler != NULL)
534     {   /* NOTE: the resampler needs to run even if resampling is 0.
535          * The decoder and output rates can still be different. */
536         filters->resampler->fmt_in.audio.i_rate += filters->resampling;
537         block = aout_FiltersPipelinePlay (&filters->resampler, 1, block);
538         filters->resampler->fmt_in.audio.i_rate -= filters->resampling;
539     }
540
541     if (nominal_rate != 0)
542     {   /* Restore input rate */
543         assert (filters->rate_filter != NULL);
544         filters->rate_filter->fmt_in.audio.i_rate = nominal_rate;
545     }
546     return block;
547
548 drop:
549     block_Release (block);
550     return NULL;
551 }