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 *CreateFilter (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->p_owner not set here */
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)
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);
69 assert (filter->pf_audio_filter != NULL);
73 static filter_t *FindConverter (vlc_object_t *obj,
74 const audio_sample_format_t *infmt,
75 const audio_sample_format_t *outfmt)
77 return CreateFilter (obj, "audio converter", NULL, infmt, outfmt);
80 static filter_t *FindResampler (vlc_object_t *obj,
81 const audio_sample_format_t *infmt,
82 const audio_sample_format_t *outfmt)
84 return CreateFilter (obj, "audio resampler", "$audio-resampler",
89 * Destroys a chain of audio filters.
91 static void aout_FiltersPipelineDestroy(filter_t *const *filters, unsigned n)
93 for( unsigned i = 0; i < n; i++ )
95 filter_t *p_filter = filters[i];
97 module_unneed( p_filter, p_filter->p_module );
98 vlc_object_release( p_filter );
102 static filter_t *TryFormat (vlc_object_t *obj, vlc_fourcc_t codec,
103 audio_sample_format_t *restrict fmt)
105 audio_sample_format_t output = *fmt;
107 assert (codec != fmt->i_format);
108 output.i_format = codec;
109 aout_FormatPrepare (&output);
111 filter_t *filter = FindConverter (obj, fmt, &output);
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
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)
132 aout_FormatsPrint (obj, "conversion:", infmt, outfmt);
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;
142 /* Encapsulate or decode non-linear formats */
143 if (!AOUT_FMT_LINEAR(infmt) && infmt->i_format != outfmt->i_format)
148 filter_t *f = TryFormat (obj, VLC_CODEC_S32N, &input);
150 f = TryFormat (obj, VLC_CODEC_FL32, &input);
153 msg_Err (obj, "cannot find %s for conversion pipeline",
160 assert (AOUT_FMT_LINEAR(&input));
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)
171 filter_t *f = TryFormat (obj, VLC_CODEC_FL32, &input);
174 msg_Err (obj, "cannot find %s for conversion pipeline",
175 "pre-mix converter");
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);
192 filter_t *f = FindConverter (obj, &input, &output);
195 msg_Err (obj, "cannot find %s for conversion pipeline",
205 if (input.i_rate != outfmt->i_rate)
206 { /* Resampling works with any linear format, but may be ugly. */
210 audio_sample_format_t output = input;
211 output.i_rate = outfmt->i_rate;
213 filter_t *f = FindConverter (obj, &input, &output);
216 msg_Err (obj, "cannot find %s for conversion pipeline",
226 if (input.i_format != outfmt->i_format)
231 filter_t *f = TryFormat (obj, outfmt->i_format, &input);
234 msg_Err (obj, "cannot find %s for conversion pipeline",
235 "post-mix converter");
241 msg_Dbg (obj, "conversion pipeline complete");
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);
250 aout_FiltersPipelineDestroy (filters, n);
254 #define aout_FiltersPipelineCreate(obj,f,n,m,i,o) \
255 aout_FiltersPipelineCreate(VLC_OBJECT(obj),f,n,m,i,o)
258 * Filters an audio buffer through a chain of filters.
260 static block_t *aout_FiltersPipelinePlay(filter_t *const *filters,
261 unsigned count, block_t *block)
263 /* TODO: use filter chain */
264 for (unsigned i = 0; (i < count) && (block != NULL); i++)
266 filter_t *filter = filters[i];
268 /* Please note that p_block->i_nb_samples & i_buffer
269 * shall be set by the filter plug-in. */
270 block = filter->pf_audio_filter (filter, block);
275 /** Callback for visualization selection */
276 static int VisualizationCallback (vlc_object_t *obj, const char *var,
277 vlc_value_t oldval, vlc_value_t newval,
280 const char *mode = newval.psz_string;
284 /* FIXME: This ugly hack enforced by visual effect-list, as is the need for
285 * separate "visual" (external) and "audio-visual" (internal) variables...
286 * The visual plugin should have one submodule per effect instead. */
287 if (strcasecmp (mode, "none") && strcasecmp (mode, "goom")
288 && strcasecmp (mode, "projectm") && strcasecmp (mode, "vsxu"))
290 var_Create (obj, "effect-list", VLC_VAR_STRING);
291 var_SetString (obj, "effect-list", mode);
295 var_SetString (obj, "audio-visual", mode);
296 aout_InputRequestRestart ((audio_output_t *)obj);
297 (void) var; (void) oldval; (void) data;
301 static int EqualizerCallback (vlc_object_t *obj, const char *var,
302 vlc_value_t oldval, vlc_value_t newval,
305 const char *val = newval.psz_string;
309 var_Create (obj, "equalizer-preset", VLC_VAR_STRING);
310 var_SetString (obj, "equalizer-preset", val);
313 if (aout_ChangeFilterString (obj, obj, "audio-filter", "equalizer", *val))
314 aout_InputRequestRestart ((audio_output_t *)obj); /* <- That sucks! */
316 (void) var; (void) oldval; (void) data;
320 vout_thread_t *aout_filter_RequestVout (filter_t *filter, vout_thread_t *vout,
323 /* NOTE: This only works from audio output.
324 * If you want to use visualization filters from another place, you will
325 * need to add a new pf_aout_request_vout callback or store a pointer
326 * to aout_request_vout_t inside filter_t (i.e. a level of indirection). */
327 aout_owner_t *owner = aout_owner ((audio_output_t *)filter->p_parent);
328 aout_request_vout_t *req = &owner->request_vout;
330 return req->pf_request_vout (req->p_private, vout, fmt,
331 owner->recycle_vout);
334 static int AppendFilter(vlc_object_t *obj, const char *type, const char *name,
335 filter_t **filters, unsigned *count,
336 audio_sample_format_t *restrict infmt,
337 const audio_sample_format_t *restrict outfmt)
339 const unsigned max = AOUT_MAX_FILTERS;
342 msg_Err (obj, "maximum of %u filters reached", max);
346 filter_t *filter = CreateFilter (obj, type, name, infmt, outfmt);
349 msg_Err (obj, "cannot add user %s \"%s\" (skipped)", type, name);
353 /* convert to the filter input format if necessary */
354 if (aout_FiltersPipelineCreate (filter, filters, count, max - 1, infmt,
355 &filter->fmt_in.audio))
357 msg_Err (filter, "cannot add user %s \"%s\" (skipped)", type, name);
358 module_unneed (filter, filter->p_module);
359 vlc_object_release (filter);
363 assert (*count < max);
364 filters[(*count)++] = filter;
365 *infmt = filter->fmt_out.audio;
371 * Sets up the audio filters.
373 int aout_FiltersNew (audio_output_t *aout,
374 const audio_sample_format_t *restrict infmt,
375 const audio_sample_format_t *restrict outfmt,
376 const aout_request_vout_t *request_vout)
378 aout_owner_t *owner = aout_owner (aout);
380 /* Prepare format structure */
381 aout_FormatPrint (aout, "input", infmt);
382 audio_sample_format_t input_format = *infmt;
383 audio_sample_format_t output_format = *outfmt;
385 /* Now add user filters */
386 owner->nb_filters = 0;
387 owner->rate_filter = NULL;
388 owner->resampler = NULL;
390 var_AddCallback (aout, "visual", VisualizationCallback, NULL);
391 var_AddCallback (aout, "equalizer", EqualizerCallback, NULL);
393 if (!AOUT_FMT_LINEAR(outfmt))
394 { /* Non-linear output: just convert formats, no filters/visu */
395 if (!AOUT_FMTS_IDENTICAL(infmt, outfmt))
397 aout_FormatsPrint (aout, "pass-through:", infmt, outfmt);
398 owner->filters[0] = FindConverter(VLC_OBJECT(aout), infmt, outfmt);
399 if (owner->filters[0] == NULL)
401 msg_Err (aout, "cannot setup pass-through");
409 /* parse user filter lists */
410 if (var_InheritBool (aout, "audio-time-stretch"))
412 if (AppendFilter(VLC_OBJECT(aout), "audio filter", "scaletempo",
413 owner->filters, &owner->nb_filters,
414 &input_format, &output_format) == 0)
415 owner->rate_filter = owner->filters[owner->nb_filters - 1];
418 char *filters = var_InheritString (aout, "audio-filter");
421 char *p = filters, *name;
422 while ((name = strsep (&p, " :")) != NULL)
424 AppendFilter(VLC_OBJECT(aout), "audio filter", name,
425 owner->filters, &owner->nb_filters,
426 &input_format, &output_format);
431 char *visual = var_InheritString (aout, "audio-visual");
432 owner->request_vout = *request_vout;
433 owner->recycle_vout = visual != NULL;
434 if (visual != NULL && strcasecmp (visual, "none"))
436 AppendFilter(VLC_OBJECT(aout), "visualization", visual,
437 owner->filters, &owner->nb_filters,
438 &input_format, &output_format);
442 /* convert to the output format (minus resampling) if necessary */
443 output_format.i_rate = input_format.i_rate;
444 if (aout_FiltersPipelineCreate (aout, owner->filters, &owner->nb_filters,
446 &input_format, &output_format))
448 msg_Err (aout, "cannot setup filtering pipeline");
451 input_format = output_format;
453 /* insert the resampler */
454 output_format.i_rate = outfmt->i_rate;
455 assert (AOUT_FMTS_IDENTICAL(&output_format, outfmt));
456 owner->resampler = FindResampler (VLC_OBJECT(aout), &input_format,
458 if (owner->resampler == NULL && input_format.i_rate != outfmt->i_rate)
460 msg_Err (aout, "cannot setup a resampler");
463 if (owner->rate_filter == NULL)
464 owner->rate_filter = owner->resampler;
465 owner->resampling = 0;
470 aout_FiltersPipelineDestroy (owner->filters, owner->nb_filters);
471 var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
472 var_DelCallback (aout, "visual", VisualizationCallback, NULL);
477 * Destroys the audio filters.
479 void aout_FiltersDelete (audio_output_t *aout)
481 aout_owner_t *owner = aout_owner (aout);
483 if (owner->resampler != NULL)
484 aout_FiltersPipelineDestroy (&owner->resampler, 1);
485 aout_FiltersPipelineDestroy (owner->filters, owner->nb_filters);
486 var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
487 var_DelCallback (aout, "visual", VisualizationCallback, NULL);
489 /* XXX We need to update recycle_vout before calling
490 * aout_FiltersPipelineDestroy().
491 * FIXME There may be a race condition if audio-visual is updated between
492 * aout_FiltersDestroy() and the next aout_FiltersNew().
494 char *visual = var_InheritString (aout, "audio-visual");
495 owner->recycle_vout = (visual != NULL) && *visual;
499 bool aout_FiltersAdjustResampling (audio_output_t *aout, int adjust)
501 aout_owner_t *owner = aout_owner (aout);
503 if (owner->resampler == NULL)
507 owner->resampling += adjust;
509 owner->resampling = 0;
510 return owner->resampling != 0;
513 block_t *aout_FiltersPlay (audio_output_t *aout, block_t *block, int rate)
515 aout_owner_t *owner = aout_owner (aout);
516 int nominal_rate = 0;
518 if (rate != INPUT_RATE_DEFAULT)
520 filter_t *rate_filter = owner->rate_filter;
522 if (rate_filter == NULL)
523 goto drop; /* Without linear, non-nominal rate is impossible. */
525 /* Override input rate */
526 nominal_rate = rate_filter->fmt_in.audio.i_rate;
527 rate_filter->fmt_in.audio.i_rate =
528 (nominal_rate * INPUT_RATE_DEFAULT) / rate;
531 block = aout_FiltersPipelinePlay (owner->filters, owner->nb_filters,
533 if (owner->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 owner->resampler->fmt_in.audio.i_rate += owner->resampling;
537 block = aout_FiltersPipelinePlay (&owner->resampler, 1, block);
538 owner->resampler->fmt_in.audio.i_rate -= owner->resampling;
541 if (nominal_rate != 0)
542 { /* Restore input rate */
543 assert (owner->rate_filter != NULL);
544 owner->rate_filter->fmt_in.audio.i_rate = nominal_rate;
549 block_Release (block);