1 /*****************************************************************************
2 * filters.c : audio output filters management
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_dialog.h>
36 #include <vlc_modules.h>
38 #include <vlc_filter.h>
39 #include <vlc_vout.h> /* for vout_Request */
40 #include <vlc_input.h>
43 #include "aout_internal.h"
45 static filter_t *FindFilter (vlc_object_t *obj, const char *type,
47 const audio_sample_format_t *infmt,
48 const audio_sample_format_t *outfmt)
50 filter_t *filter = vlc_custom_create (obj, sizeof (*filter), type);
51 if (unlikely(filter == NULL))
54 filter->fmt_in.audio = *infmt;
55 filter->fmt_in.i_codec = infmt->i_format;
56 filter->fmt_out.audio = *outfmt;
57 filter->fmt_out.i_codec = outfmt->i_format;
58 filter->p_module = module_need (filter, type, name, false);
59 if (filter->p_module == NULL)
61 vlc_object_release (filter);
65 assert (filter->pf_audio_filter != NULL);
69 static filter_t *FindConverter (vlc_object_t *obj,
70 const audio_sample_format_t *infmt,
71 const audio_sample_format_t *outfmt)
73 return FindFilter (obj, "audio converter", NULL, infmt, outfmt);
76 static filter_t *FindResampler (vlc_object_t *obj,
77 const audio_sample_format_t *infmt,
78 const audio_sample_format_t *outfmt)
80 return FindFilter (obj, "audio resampler", "$audio-resampler",
85 * Destroys a chain of audio filters.
87 static void aout_FiltersPipelineDestroy(filter_t *const *filters, unsigned n)
89 for( unsigned i = 0; i < n; i++ )
91 filter_t *p_filter = filters[i];
93 module_unneed( p_filter, p_filter->p_module );
94 vlc_object_release( p_filter );
98 static filter_t *TryFormat (vlc_object_t *obj, vlc_fourcc_t codec,
99 audio_sample_format_t *restrict fmt)
101 audio_sample_format_t output = *fmt;
103 assert (codec != fmt->i_format);
104 output.i_format = codec;
105 aout_FormatPrepare (&output);
107 filter_t *filter = FindConverter (obj, fmt, &output);
114 * Allocates audio format conversion filters
115 * @param obj parent VLC object for new filters
116 * @param filters table of filters [IN/OUT]
117 * @param count pointer to the number of filters in the table [IN/OUT]
118 * @param max size of filters table [IN]
119 * @param infmt input audio format
120 * @param outfmt output audio format
121 * @return 0 on success, -1 on failure
123 static int aout_FiltersPipelineCreate(vlc_object_t *obj, filter_t **filters,
124 unsigned *count, unsigned max,
125 const audio_sample_format_t *restrict infmt,
126 const audio_sample_format_t *restrict outfmt)
128 aout_FormatsPrint (obj, "conversion:", infmt, outfmt);
132 /* There is a lot of second guessing on what the conversion plugins can
133 * and cannot do. This seems hardly avoidable, the conversion problem need
134 * to be reduced somehow. */
135 audio_sample_format_t input = *infmt;
138 /* Encapsulate or decode non-linear formats */
139 if (!AOUT_FMT_LINEAR(infmt) && infmt->i_format != outfmt->i_format)
144 filter_t *f = TryFormat (obj, VLC_CODEC_S32N, &input);
146 f = TryFormat (obj, VLC_CODEC_FL32, &input);
149 msg_Err (obj, "cannot find %s for conversion pipeline",
156 assert (AOUT_FMT_LINEAR(&input));
159 if (infmt->i_physical_channels != outfmt->i_physical_channels
160 || infmt->i_original_channels != outfmt->i_original_channels)
161 { /* Remixing currently requires FL32... TODO: S16N */
162 if (input.i_format != VLC_CODEC_FL32)
167 filter_t *f = TryFormat (obj, VLC_CODEC_FL32, &input);
170 msg_Err (obj, "cannot find %s for conversion pipeline",
171 "pre-mix converter");
181 audio_sample_format_t output;
182 output.i_format = input.i_format;
183 output.i_rate = input.i_rate;
184 output.i_physical_channels = outfmt->i_physical_channels;
185 output.i_original_channels = outfmt->i_original_channels;
186 aout_FormatPrepare (&output);
188 filter_t *f = FindConverter (obj, &input, &output);
191 msg_Err (obj, "cannot find %s for conversion pipeline",
201 if (input.i_rate != outfmt->i_rate)
202 { /* Resampling works with any linear format, but may be ugly. */
206 audio_sample_format_t output = input;
207 output.i_rate = outfmt->i_rate;
209 filter_t *f = FindConverter (obj, &input, &output);
212 msg_Err (obj, "cannot find %s for conversion pipeline",
222 if (input.i_format != outfmt->i_format)
227 filter_t *f = TryFormat (obj, outfmt->i_format, &input);
230 msg_Err (obj, "cannot find %s for conversion pipeline",
231 "post-mix converter");
237 msg_Dbg (obj, "conversion pipeline complete");
242 msg_Err (obj, "maximum of %u conversion filters reached", max);
243 dialog_Fatal (obj, _("Audio filtering failed"),
244 _("The maximum number of filters (%u) was reached."), max);
246 aout_FiltersPipelineDestroy (filters, n);
250 #define aout_FiltersPipelineCreate(obj,f,n,m,i,o) \
251 aout_FiltersPipelineCreate(VLC_OBJECT(obj),f,n,m,i,o)
253 static inline bool ChangeFiltersString (vlc_object_t *aout, const char *var,
254 const char *filter, bool add)
256 return aout_ChangeFilterString (aout, aout, var, filter, add);
260 * Filters an audio buffer through a chain of filters.
262 static block_t *aout_FiltersPipelinePlay(filter_t *const *filters,
263 unsigned count, block_t *block)
265 /* TODO: use filter chain */
266 for (unsigned i = 0; (i < count) && (block != NULL); i++)
268 filter_t *filter = filters[i];
270 /* Please note that p_block->i_nb_samples & i_buffer
271 * shall be set by the filter plug-in. */
272 block = filter->pf_audio_filter (filter, block);
277 /** Callback for visualization selection */
278 static int VisualizationCallback (vlc_object_t *obj, const char *var,
279 vlc_value_t oldval, vlc_value_t newval,
282 const char *mode = newval.psz_string;
286 /* FIXME: This ugly hack enforced by visual effect-list, as is the need for
287 * separate "visual" (external) and "audio-visual" (internal) variables...
288 * The visual plugin should have one submodule per effect instead. */
289 if (strcasecmp (mode, "none") && strcasecmp (mode, "goom")
290 && strcasecmp (mode, "projectm") && strcasecmp (mode, "vsxu"))
292 var_Create (obj, "effect-list", VLC_VAR_STRING);
293 var_SetString (obj, "effect-list", mode);
297 var_SetString (obj, "audio-visual", mode);
298 aout_InputRequestRestart ((audio_output_t *)obj);
299 (void) var; (void) oldval; (void) data;
303 static int EqualizerCallback (vlc_object_t *obj, char const *var,
304 vlc_value_t oldval, vlc_value_t newval,
307 audio_output_t *aout = (audio_output_t *)obj;
308 char *mode = newval.psz_string;
312 ret = ChangeFiltersString (obj, "audio-filter", "equalizer", false);
315 var_Create (obj, "equalizer-preset", VLC_VAR_STRING);
316 var_SetString (obj, "equalizer-preset", mode);
317 ret = ChangeFiltersString (obj, "audio-filter", "equalizer", true);
322 aout_InputRequestRestart (aout);
323 (void) var; (void) oldval; (void) data;
327 static vout_thread_t *RequestVout (void *data, vout_thread_t *vout,
328 video_format_t *fmt, bool recycle)
330 audio_output_t *aout = data;
331 vout_configuration_t cfg = {
340 return vout_Request (aout, &cfg);
343 vout_thread_t *aout_filter_RequestVout (filter_t *filter, vout_thread_t *vout,
346 /* NOTE: This only works from audio output.
347 * If you want to use visualization filters from another place, you will
348 * need to add a new pf_aout_request_vout callback or store a pointer
349 * to aout_request_vout_t inside filter_t (i.e. a level of indirection). */
350 aout_owner_t *owner = aout_owner ((audio_output_t *)filter->p_parent);
351 aout_request_vout_t *req = &owner->request_vout;
353 return req->pf_request_vout (req->p_private, vout, fmt,
354 owner->recycle_vout);
357 static filter_t *CreateFilter (vlc_object_t *parent, const char *name,
358 const audio_sample_format_t *restrict infmt,
359 const audio_sample_format_t *restrict outfmt,
362 filter_t *filter = vlc_custom_create (parent, sizeof (*filter),
364 if (unlikely(filter == NULL))
367 /*filter->p_owner = NOT NEEDED;*/
368 filter->fmt_in.i_codec = infmt->i_format;
369 filter->fmt_in.audio = *infmt;
370 filter->fmt_out.i_codec = outfmt->i_format;
371 filter->fmt_out.audio = *outfmt;
375 filter->p_module = module_need (filter, "audio filter", name, true);
376 if (filter->p_module != NULL)
379 /* If probing failed, formats shall not have been modified. */
380 assert (AOUT_FMTS_IDENTICAL(&filter->fmt_in.audio, infmt));
381 assert (AOUT_FMTS_IDENTICAL(&filter->fmt_out.audio, outfmt));
384 filter->p_module = module_need (filter, "visualization2", name, true);
385 if (filter->p_module != NULL)
388 vlc_object_release (filter);
393 * Sets up the audio filters.
395 int aout_FiltersNew (audio_output_t *aout,
396 const audio_sample_format_t *restrict infmt,
397 const audio_sample_format_t *restrict outfmt,
398 const aout_request_vout_t *request_vout)
400 aout_owner_t *owner = aout_owner (aout);
402 /* Prepare format structure */
403 aout_FormatPrint (aout, "input", infmt);
404 audio_sample_format_t input_format = *infmt;
405 audio_sample_format_t output_format = *outfmt;
407 /* Now add user filters */
408 owner->nb_filters = 0;
409 owner->rate_filter = NULL;
410 owner->resampler = NULL;
412 var_AddCallback (aout, "visual", VisualizationCallback, NULL);
413 var_AddCallback (aout, "equalizer", EqualizerCallback, NULL);
415 if (!AOUT_FMT_LINEAR(outfmt))
416 { /* Non-linear output: just convert formats, no filters/visu */
417 if (!AOUT_FMTS_IDENTICAL(infmt, outfmt))
419 aout_FormatsPrint (aout, "pass-through:", infmt, outfmt);
420 owner->filters[0] = FindConverter(VLC_OBJECT(aout), infmt, outfmt);
421 if (owner->filters[0] == NULL)
423 msg_Err (aout, "cannot setup pass-through");
431 const char *scaletempo =
432 var_InheritBool (aout, "audio-time-stretch") ? "scaletempo" : NULL;
433 char *filters = var_InheritString (aout, "audio-filter");
434 char *visual = var_InheritString (aout, "audio-visual");
435 if (visual != NULL && !strcasecmp (visual, "none"))
441 if (request_vout != NULL)
442 owner->request_vout = *request_vout;
445 owner->request_vout.pf_request_vout = RequestVout;
446 owner->request_vout.p_private = aout;
448 owner->recycle_vout = visual != NULL;
450 /* parse user filter lists */
451 const char *list[AOUT_MAX_FILTERS];
454 if (scaletempo != NULL)
455 list[n++] = scaletempo;
458 char *p = filters, *name;
459 while ((name = strsep (&p, " :")) != NULL && n < AOUT_MAX_FILTERS)
462 if (visual != NULL && n < AOUT_MAX_FILTERS)
465 for (unsigned i = 0; i < n; i++)
467 const char *name = list[i];
469 if (owner->nb_filters >= AOUT_MAX_FILTERS)
471 msg_Err (aout, "maximum of %u filters reached", AOUT_MAX_FILTERS);
472 msg_Err (aout, "cannot add user filter %s (skipped)", name);
476 filter_t *filter = CreateFilter (VLC_OBJECT(aout), name,
477 &input_format, &output_format,
481 msg_Err (aout, "cannot add user filter %s (skipped)", name);
485 /* convert to the filter input format if necessary */
486 if (aout_FiltersPipelineCreate (aout, owner->filters,
488 AOUT_MAX_FILTERS - 1,
489 &input_format, &filter->fmt_in.audio))
491 msg_Err (aout, "cannot add user filter %s (skipped)", name);
492 module_unneed (filter, filter->p_module);
493 vlc_object_release (filter);
497 assert (owner->nb_filters < AOUT_MAX_FILTERS);
498 owner->filters[owner->nb_filters++] = filter;
499 input_format = filter->fmt_out.audio;
501 if (name == scaletempo)
502 owner->rate_filter = filter;
507 /* convert to the output format (minus resampling) if necessary */
508 output_format.i_rate = input_format.i_rate;
509 if (aout_FiltersPipelineCreate (aout, owner->filters, &owner->nb_filters,
511 &input_format, &output_format))
513 msg_Err (aout, "cannot setup filtering pipeline");
516 input_format = output_format;
518 /* insert the resampler */
519 output_format.i_rate = outfmt->i_rate;
520 assert (AOUT_FMTS_IDENTICAL(&output_format, outfmt));
521 owner->resampler = FindResampler (VLC_OBJECT(aout), &input_format,
523 if (owner->resampler == NULL && input_format.i_rate != outfmt->i_rate)
525 msg_Err (aout, "cannot setup a resampler");
528 if (owner->rate_filter == NULL)
529 owner->rate_filter = owner->resampler;
530 owner->resampling = 0;
535 aout_FiltersPipelineDestroy (owner->filters, owner->nb_filters);
536 var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
537 var_DelCallback (aout, "visual", VisualizationCallback, NULL);
542 * Destroys the audio filters.
544 void aout_FiltersDelete (audio_output_t *aout)
546 aout_owner_t *owner = aout_owner (aout);
548 if (owner->resampler != NULL)
549 aout_FiltersPipelineDestroy (&owner->resampler, 1);
550 aout_FiltersPipelineDestroy (owner->filters, owner->nb_filters);
551 var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
552 var_DelCallback (aout, "visual", VisualizationCallback, NULL);
554 /* XXX We need to update recycle_vout before calling
555 * aout_FiltersPipelineDestroy().
556 * FIXME There may be a race condition if audio-visual is updated between
557 * aout_FiltersDestroy() and the next aout_FiltersNew().
559 char *visual = var_InheritString (aout, "audio-visual");
560 owner->recycle_vout = (visual != NULL) && *visual;
564 bool aout_FiltersAdjustResampling (audio_output_t *aout, int adjust)
566 aout_owner_t *owner = aout_owner (aout);
568 if (owner->resampler == NULL)
572 owner->resampling += adjust;
574 owner->resampling = 0;
575 return owner->resampling != 0;
578 block_t *aout_FiltersPlay (audio_output_t *aout, block_t *block, int rate)
580 aout_owner_t *owner = aout_owner (aout);
581 int nominal_rate = 0;
583 if (rate != INPUT_RATE_DEFAULT)
585 filter_t *rate_filter = owner->rate_filter;
587 if (rate_filter == NULL)
588 goto drop; /* Without linear, non-nominal rate is impossible. */
590 /* Override input rate */
591 nominal_rate = rate_filter->fmt_in.audio.i_rate;
592 rate_filter->fmt_in.audio.i_rate =
593 (nominal_rate * INPUT_RATE_DEFAULT) / rate;
596 block = aout_FiltersPipelinePlay (owner->filters, owner->nb_filters,
598 if (owner->resampler != NULL)
599 { /* NOTE: the resampler needs to run even if resampling is 0.
600 * The decoder and output rates can still be different. */
601 owner->resampler->fmt_in.audio.i_rate += owner->resampling;
602 block = aout_FiltersPipelinePlay (&owner->resampler, 1, block);
603 owner->resampler->fmt_in.audio.i_rate -= owner->resampling;
606 if (nominal_rate != 0)
607 { /* Restore input rate */
608 assert (owner->rate_filter != NULL);
609 owner->rate_filter->fmt_in.audio.i_rate = nominal_rate;
614 block_Release (block);