]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
Improvements to preferences
[vlc] / src / audio_output / input.c
1 /*****************************************************************************
2  * input.c : internal management of input streams for the audio output
3  *****************************************************************************
4  * Copyright (C) 2002-2004 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
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                            /* calloc(), malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>                 /* for input_thread_t and i_pts_delay */
32
33 #ifdef HAVE_ALLOCA_H
34 #   include <alloca.h>
35 #endif
36
37 #include "audio_output.h"
38 #include "aout_internal.h"
39
40 static int VisualizationCallback( vlc_object_t *, char const *,
41                                   vlc_value_t, vlc_value_t, void * );
42 static int EqualizerCallback( vlc_object_t *, char const *,
43                               vlc_value_t, vlc_value_t, void * );
44 static aout_filter_t * allocateUserChannelMixer( aout_instance_t *,
45                                                  audio_sample_format_t *,
46                                                  audio_sample_format_t * );
47
48 /*****************************************************************************
49  * aout_InputNew : allocate a new input and rework the filter pipeline
50  *****************************************************************************/
51 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
52 {
53     audio_sample_format_t user_filter_format;
54     audio_sample_format_t intermediate_format;/* input of resampler */
55     vlc_value_t val, text;
56     char * psz_filters, *psz_visual;
57     aout_filter_t * p_user_channel_mixer;
58
59     aout_FormatPrint( p_aout, "input", &p_input->input );
60
61     /* Prepare FIFO. */
62     aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
63     p_input->p_first_byte_to_mix = NULL;
64
65     /* Prepare format structure */
66     memcpy( &intermediate_format, &p_aout->mixer.mixer,
67             sizeof(audio_sample_format_t) );
68     intermediate_format.i_rate = p_input->input.i_rate;
69
70     /* Try to use the channel mixer chosen by the user */
71     memcpy ( &user_filter_format, &intermediate_format,
72              sizeof(audio_sample_format_t) );
73     user_filter_format.i_physical_channels = p_input->input.i_physical_channels;
74     user_filter_format.i_original_channels = p_input->input.i_original_channels;
75     user_filter_format.i_bytes_per_frame = user_filter_format.i_bytes_per_frame
76                               * aout_FormatNbChannels( &user_filter_format )
77                               / aout_FormatNbChannels( &intermediate_format );
78     p_user_channel_mixer = allocateUserChannelMixer( p_aout, &user_filter_format,
79                                                    &intermediate_format );
80     /* If it failed, let the main pipeline do channel mixing */
81     if ( ! p_user_channel_mixer )
82     {
83         memcpy ( &user_filter_format, &intermediate_format,
84                  sizeof(audio_sample_format_t) );
85     }
86
87     /* Create filters. */
88     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
89                                      &p_input->i_nb_filters,
90                                      &p_input->input,
91                                      &user_filter_format
92                                      ) < 0 )
93     {
94         msg_Err( p_aout, "couldn't set an input pipeline" );
95
96         aout_FifoDestroy( p_aout, &p_input->fifo );
97         p_input->b_error = 1;
98         return -1;
99     }
100
101     /* Now add user filters */
102     if( var_Type( p_aout, "visual" ) == 0 )
103     {
104         module_t *p_module;
105         var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
106         text.psz_string = _("Visualizations");
107         var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
108         val.psz_string = ""; text.psz_string = _("Disable");
109         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
110         val.psz_string = "random"; text.psz_string = _("Random");
111         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
112         val.psz_string = "scope"; text.psz_string = _("Scope");
113         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
114         val.psz_string = "spectrum"; text.psz_string = _("Spectrum");
115         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
116
117         /* Look for goom plugin */
118         p_module = config_FindModule( VLC_OBJECT(p_aout), "goom" );
119         if( p_module )
120         {
121             val.psz_string = "goom"; text.psz_string = "Goom";
122             var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
123         }
124
125         /* Look for galaktos plugin */
126         p_module = config_FindModule( VLC_OBJECT(p_aout), "galaktos" );
127         if( p_module )
128         {
129             val.psz_string = "galaktos"; text.psz_string = "GaLaktos";
130             var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
131         }
132
133         if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
134         {
135             var_Set( p_aout, "visual", val );
136             if( val.psz_string ) free( val.psz_string );
137         }
138         var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
139     }
140
141     if( var_Type( p_aout, "equalizer" ) == 0 )
142     {
143         module_config_t *p_config;
144         int i;
145
146         p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
147         if( p_config && p_config->i_list )
148         {
149                var_Create( p_aout, "equalizer",
150                            VLC_VAR_STRING | VLC_VAR_HASCHOICE );
151             text.psz_string = _("Equalizer");
152             var_Change( p_aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL );
153
154             val.psz_string = ""; text.psz_string = _("Disable");
155             var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text );
156
157             for( i = 0; i < p_config->i_list; i++ )
158             {
159                 val.psz_string = p_config->ppsz_list[i];
160                 text.psz_string = p_config->ppsz_list_text[i];
161                 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE,
162                             &val, &text );
163             }
164
165             var_AddCallback( p_aout, "equalizer", EqualizerCallback, NULL );
166         }
167     }
168
169     if( var_Type( p_aout, "audio-filter" ) == 0 )
170     {
171         var_Create( p_aout, "audio-filter",
172                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
173         text.psz_string = _("Audio filters");
174         var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
175     }
176     if( var_Type( p_aout, "audio-visual" ) == 0 )
177     {
178         var_Create( p_aout, "audio-visual",
179                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
180         text.psz_string = _("Audio visualizations");
181         var_Change( p_aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL );
182     }
183
184     var_Get( p_aout, "audio-filter", &val );
185     psz_filters = val.psz_string;
186     var_Get( p_aout, "audio-visual", &val );
187     psz_visual = val.psz_string;
188
189     if( psz_filters && *psz_filters && psz_visual && *psz_visual )
190     {
191         psz_filters = (char *)realloc( psz_filters, strlen( psz_filters ) +
192                                                     strlen( psz_visual )  + 1);
193         sprintf( psz_filters, "%s,%s", psz_filters, psz_visual );
194     }
195     else if(  psz_visual && *psz_visual )
196     {
197         if( psz_filters ) free( psz_filters );
198         psz_filters = strdup( psz_visual );
199     }
200
201     if( psz_filters && *psz_filters )
202     {
203         char *psz_parser = psz_filters;
204         char *psz_next;
205         while( psz_parser && *psz_parser )
206         {
207             aout_filter_t * p_filter;
208
209             if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
210             {
211                 msg_Dbg( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
212                 break;
213             }
214
215             while( *psz_parser == ' ' && *psz_parser == ',' )
216             {
217                 psz_parser++;
218             }
219             if( ( psz_next = strchr( psz_parser , ','  ) ) )
220             {
221                 *psz_next++ = '\0';
222             }
223             if( *psz_parser =='\0' )
224             {
225                 break;
226             }
227
228             msg_Dbg( p_aout, "user filter \"%s\"", psz_parser );
229
230             /* Create a VLC object */
231             p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
232             if( p_filter == NULL )
233             {
234                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
235                          psz_parser );
236                 psz_parser = psz_next;
237                 continue;
238             }
239
240             vlc_object_attach( p_filter , p_aout );
241             memcpy( &p_filter->input, &user_filter_format,
242                     sizeof(audio_sample_format_t) );
243             memcpy( &p_filter->output, &user_filter_format,
244                     sizeof(audio_sample_format_t) );
245
246             p_filter->p_module =
247                 module_Need( p_filter,"audio filter", psz_parser, VLC_TRUE );
248
249             if( p_filter->p_module== NULL )
250             {
251                 p_filter->p_module =
252                     module_Need( p_filter,"visualization", psz_parser,
253                                                            VLC_TRUE );
254                 if( p_filter->p_module == NULL )
255                 {
256                     msg_Err( p_aout, "cannot add user filter %s (skipped)",
257                              psz_parser );
258
259                     vlc_object_detach( p_filter );
260                     vlc_object_destroy( p_filter );
261                     psz_parser = psz_next;
262                     continue;
263                 }
264             }
265             p_filter->b_continuity = VLC_FALSE;
266
267             p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
268
269             /* next filter if any */
270             psz_parser = psz_next;
271         }
272     }
273     if( psz_filters ) free( psz_filters );
274     if( psz_visual ) free( psz_visual );
275
276     /* Attach the user channel mixer */
277     if ( p_user_channel_mixer )
278     {
279         p_input->pp_filters[p_input->i_nb_filters++] = p_user_channel_mixer;
280     }
281
282     /* Prepare hints for the buffer allocator. */
283     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
284     p_input->input_alloc.i_bytes_per_sec = -1;
285
286     if ( AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
287     {
288         p_input->i_nb_resamplers = 0;
289     }
290     else
291     {
292         /* Create resamplers. */
293         intermediate_format.i_rate = (__MAX(p_input->input.i_rate,
294                                             p_aout->mixer.mixer.i_rate)
295                                  * (100 + AOUT_MAX_RESAMPLING)) / 100;
296         if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
297         {
298             /* Just in case... */
299             intermediate_format.i_rate++;
300         }
301         if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
302                                          &p_input->i_nb_resamplers,
303                                          &intermediate_format,
304                                          &p_aout->mixer.mixer ) < 0 )
305         {
306             msg_Err( p_aout, "couldn't set a resampler pipeline" );
307
308             aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
309                                          p_input->i_nb_filters );
310             aout_FifoDestroy( p_aout, &p_input->fifo );
311             var_Destroy( p_aout, "visual" );
312             p_input->b_error = 1;
313
314             return -1;
315         }
316
317         aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
318                                  p_input->i_nb_resamplers,
319                                  &p_input->input_alloc );
320
321         /* Setup the initial rate of the resampler */
322         p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
323     }
324     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
325
326     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
327     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
328                              p_input->i_nb_filters,
329                              &p_input->input_alloc );
330
331     /* i_bytes_per_sec is still == -1 if no filters */
332     p_input->input_alloc.i_bytes_per_sec = __MAX(
333                                     p_input->input_alloc.i_bytes_per_sec,
334                                     (int)(p_input->input.i_bytes_per_frame
335                                      * p_input->input.i_rate
336                                      / p_input->input.i_frame_length) );
337     /* Allocate in the heap, it is more convenient for the decoder. */
338     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
339
340     p_input->b_error = VLC_FALSE;
341     p_input->b_restart = VLC_FALSE;
342
343     return 0;
344 }
345
346 /*****************************************************************************
347  * aout_InputDelete : delete an input
348  *****************************************************************************
349  * This function must be entered with the mixer lock.
350  *****************************************************************************/
351 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
352 {
353     if ( p_input->b_error ) return 0;
354
355     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
356                                  p_input->i_nb_filters );
357     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
358                                  p_input->i_nb_resamplers );
359     aout_FifoDestroy( p_aout, &p_input->fifo );
360
361     return 0;
362 }
363
364 /*****************************************************************************
365  * aout_InputPlay : play a buffer
366  *****************************************************************************
367  * This function must be entered with the input lock.
368  *****************************************************************************/
369 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
370                     aout_buffer_t * p_buffer )
371 {
372     mtime_t start_date;
373
374     if( p_input->b_restart )
375     {
376         aout_fifo_t fifo, dummy_fifo;
377         byte_t      *p_first_byte_to_mix;
378
379         vlc_mutex_lock( &p_aout->mixer_lock );
380
381         /* A little trick to avoid loosing our input fifo */
382         aout_FifoInit( p_aout, &dummy_fifo, p_aout->mixer.mixer.i_rate );
383         p_first_byte_to_mix = p_input->p_first_byte_to_mix;
384         fifo = p_input->fifo;
385         p_input->fifo = dummy_fifo;
386         aout_InputDelete( p_aout, p_input );
387         aout_InputNew( p_aout, p_input );
388         p_input->p_first_byte_to_mix = p_first_byte_to_mix;
389         p_input->fifo = fifo;
390
391         vlc_mutex_unlock( &p_aout->mixer_lock );
392     }
393
394     /* We don't care if someone changes the start date behind our back after
395      * this. We'll deal with that when pushing the buffer, and compensate
396      * with the next incoming buffer. */
397     vlc_mutex_lock( &p_aout->input_fifos_lock );
398     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
399     vlc_mutex_unlock( &p_aout->input_fifos_lock );
400
401     if ( start_date != 0 && start_date < mdate() )
402     {
403         /* The decoder is _very_ late. This can only happen if the user
404          * pauses the stream (or if the decoder is buggy, which cannot
405          * happen :). */
406         msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
407                   "clearing out", mdate() - start_date );
408         vlc_mutex_lock( &p_aout->input_fifos_lock );
409         aout_FifoSet( p_aout, &p_input->fifo, 0 );
410         p_input->p_first_byte_to_mix = NULL;
411         vlc_mutex_unlock( &p_aout->input_fifos_lock );
412         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
413             msg_Warn( p_aout, "timing screwed, stopping resampling" );
414         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
415         if ( p_input->i_nb_resamplers != 0 )
416         {
417             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
418             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
419         }
420         start_date = 0;
421     }
422
423     if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
424     {
425         /* The decoder gives us f*cked up PTS. It's its business, but we
426          * can't present it anyway, so drop the buffer. */
427         msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
428                   mdate() - p_buffer->start_date );
429         aout_BufferFree( p_buffer );
430         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
431         if ( p_input->i_nb_resamplers != 0 )
432         {
433             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
434             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
435         }
436         return 0;
437     }
438
439     /* If the audio drift is too big then it's not worth trying to resample
440      * the audio. */
441     if ( start_date != 0 &&
442          ( start_date < p_buffer->start_date - 3 * AOUT_PTS_TOLERANCE ) )
443     {
444         msg_Warn( p_aout, "audio drift is too big ("I64Fd"), clearing out",
445                   start_date - p_buffer->start_date );
446         vlc_mutex_lock( &p_aout->input_fifos_lock );
447         aout_FifoSet( p_aout, &p_input->fifo, 0 );
448         p_input->p_first_byte_to_mix = NULL;
449         vlc_mutex_unlock( &p_aout->input_fifos_lock );
450         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
451             msg_Warn( p_aout, "timing screwed, stopping resampling" );
452         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
453         if ( p_input->i_nb_resamplers != 0 )
454         {
455             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
456             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
457         }
458         start_date = 0;
459     }
460     else if ( start_date != 0 &&
461               ( start_date > p_buffer->start_date + 3 * AOUT_PTS_TOLERANCE ) )
462     {
463         msg_Warn( p_aout, "audio drift is too big ("I64Fd"), dropping buffer",
464                   start_date - p_buffer->start_date );
465         aout_BufferFree( p_buffer );
466         return 0;
467     }
468
469     if ( start_date == 0 ) start_date = p_buffer->start_date;
470
471     /* Run pre-filters. */
472
473     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
474                       &p_buffer );
475
476     /* Run the resampler if needed.
477      * We first need to calculate the output rate of this resampler. */
478     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
479          ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
480            || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
481          p_input->i_nb_resamplers > 0 )
482     {
483         /* Can happen in several circumstances :
484          * 1. A problem at the input (clock drift)
485          * 2. A small pause triggered by the user
486          * 3. Some delay in the output stage, causing a loss of lip
487          *    synchronization
488          * Solution : resample the buffer to avoid a scratch.
489          */
490         mtime_t drift = p_buffer->start_date - start_date;
491
492         p_input->i_resamp_start_date = mdate();
493         p_input->i_resamp_start_drift = (int)drift;
494
495         if ( drift > 0 )
496             p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
497         else
498             p_input->i_resampling_type = AOUT_RESAMPLING_UP;
499
500         msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
501                           drift > 0 ? drift : -drift,
502                           drift > 0 ? "in advance" : "late",
503                           drift > 0 ? "down" : "up");
504     }
505
506     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
507     {
508         /* Resampling has been triggered previously (because of dates
509          * mismatch). We want the resampling to happen progressively so
510          * it isn't too audible to the listener. */
511
512         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
513         {
514             p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
515         }
516         else
517         {
518             p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
519         }
520
521         /* Check if everything is back to normal, in which case we can stop the
522          * resampling */
523         if( p_input->pp_resamplers[0]->input.i_rate ==
524               p_input->input.i_rate )
525         {
526             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
527             msg_Warn( p_aout, "resampling stopped after "I64Fi" usec "
528                       "(drift: "I64Fi")",
529                       mdate() - p_input->i_resamp_start_date,
530                       p_buffer->start_date - start_date);
531         }
532         else if( abs( (int)(p_buffer->start_date - start_date) ) <
533                  abs( p_input->i_resamp_start_drift ) / 2 )
534         {
535             /* if we reduced the drift from half, then it is time to switch
536              * back the resampling direction. */
537             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
538                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
539             else
540                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
541             p_input->i_resamp_start_drift = 0;
542         }
543         else if( p_input->i_resamp_start_drift &&
544                  ( abs( (int)(p_buffer->start_date - start_date) ) >
545                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
546         {
547             /* If the drift is increasing and not decreasing, than something
548              * is bad. We'd better stop the resampling right now. */
549             msg_Warn( p_aout, "timing screwed, stopping resampling" );
550             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
551             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
552         }
553     }
554
555     /* Adding the start date will be managed by aout_FifoPush(). */
556     p_buffer->end_date = start_date +
557         (p_buffer->end_date - p_buffer->start_date);
558     p_buffer->start_date = start_date;
559
560     /* Actually run the resampler now. */
561     if ( p_input->i_nb_resamplers > 0 )
562     {
563         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
564                           p_input->i_nb_resamplers,
565                           &p_buffer );
566     }
567
568     vlc_mutex_lock( &p_aout->input_fifos_lock );
569     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
570     vlc_mutex_unlock( &p_aout->input_fifos_lock );
571
572     return 0;
573 }
574
575 static int ChangeFiltersString( aout_instance_t * p_aout,
576                                  char *psz_name, vlc_bool_t b_add )
577 {
578     vlc_value_t val;
579     char *psz_parser;
580
581     var_Get( p_aout, "audio-filter", &val );
582
583     if( !val.psz_string ) val.psz_string = strdup("");
584
585     psz_parser = strstr( val.psz_string, psz_name );
586
587     if( b_add )
588     {
589         if( !psz_parser )
590         {
591             psz_parser = val.psz_string;
592             asprintf( &val.psz_string, (*val.psz_string) ? "%s,%s" : "%s%s",
593                       val.psz_string, psz_name );
594             free( psz_parser );
595         }
596         else
597         {
598             return 0;
599         }
600     }
601     else
602     {
603         if( psz_parser )
604         {
605             memmove( psz_parser, psz_parser + strlen(psz_name) +
606                      (*(psz_parser + strlen(psz_name)) == ',' ? 1 : 0 ),
607                      strlen(psz_parser + strlen(psz_name)) + 1 );
608         }
609         else
610         {
611             free( val.psz_string );
612             return 0;
613         }
614     }
615
616     var_Set( p_aout, "audio-filter", val );
617     free( val.psz_string );
618     return 1;
619 }
620
621 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
622                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
623 {
624     aout_instance_t *p_aout = (aout_instance_t *)p_this;
625     char *psz_mode = newval.psz_string;
626     vlc_value_t val;
627     int i;
628
629     if( !psz_mode || !*psz_mode )
630     {
631         ChangeFiltersString( p_aout, "goom", VLC_FALSE );
632         ChangeFiltersString( p_aout, "visual", VLC_FALSE );
633         ChangeFiltersString( p_aout, "galaktos", VLC_FALSE );
634     }
635     else
636     {
637         if( !strcmp( "goom", psz_mode ) )
638         {
639             ChangeFiltersString( p_aout, "visual", VLC_FALSE );
640             ChangeFiltersString( p_aout, "goom", VLC_TRUE );
641             ChangeFiltersString( p_aout, "galaktos", VLC_FALSE );
642         }
643         else if( !strcmp( "galaktos", psz_mode ) )
644         {
645             ChangeFiltersString( p_aout, "visual", VLC_FALSE );
646             ChangeFiltersString( p_aout, "goom", VLC_FALSE );
647             ChangeFiltersString( p_aout, "galaktos", VLC_TRUE );
648         }
649         else
650         {
651             val.psz_string = psz_mode;
652             var_Create( p_aout, "effect-list", VLC_VAR_STRING );
653             var_Set( p_aout, "effect-list", val );
654
655             ChangeFiltersString( p_aout, "goom", VLC_FALSE );
656             ChangeFiltersString( p_aout, "visual", VLC_TRUE );
657             ChangeFiltersString( p_aout, "galaktos", VLC_FALSE );
658         }
659     }
660
661     /* That sucks */
662     for( i = 0; i < p_aout->i_nb_inputs; i++ )
663     {
664         p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
665     }
666
667     return VLC_SUCCESS;
668 }
669
670 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
671                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
672 {
673     aout_instance_t *p_aout = (aout_instance_t *)p_this;
674     char *psz_mode = newval.psz_string;
675     vlc_value_t val;
676     int i;
677     int i_ret;
678
679     if( !psz_mode || !*psz_mode )
680     {
681         i_ret = ChangeFiltersString( p_aout, "equalizer", VLC_FALSE );
682     }
683     else
684     {
685         val.psz_string = psz_mode;
686         var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
687         var_Set( p_aout, "equalizer-preset", val );
688         i_ret = ChangeFiltersString( p_aout, "equalizer", VLC_TRUE );
689
690     }
691
692     /* That sucks */
693     if( i_ret == 1 )
694     {
695         for( i = 0; i < p_aout->i_nb_inputs; i++ )
696         {
697             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
698         }
699     }
700
701     return VLC_SUCCESS;
702 }
703
704 static aout_filter_t * allocateUserChannelMixer( aout_instance_t * p_aout,
705                                      audio_sample_format_t * p_input_format,
706                                      audio_sample_format_t * p_output_format )
707 {
708     aout_filter_t * p_channel_mixer;
709
710     /* Retreive user preferred channel mixer */
711     char * psz_name = config_GetPsz( p_aout, "audio-channel-mixer" );
712
713     /* Not specified => let the main pipeline do the mixing */
714     if ( ! psz_name ) return NULL;
715
716     /* Debug information */
717     aout_FormatsPrint( p_aout, "channel mixer", p_input_format,
718                        p_output_format );
719
720     /* Create a VLC object */
721     p_channel_mixer = vlc_object_create( p_aout, sizeof(aout_filter_t) );
722     if( p_channel_mixer == NULL )
723     {
724         msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
725         return NULL;
726     }
727     vlc_object_attach( p_channel_mixer , p_aout );
728
729     /* Attach the suitable module */
730     memcpy( &p_channel_mixer->input, p_input_format,
731                     sizeof(audio_sample_format_t) );
732     memcpy( &p_channel_mixer->output, p_output_format,
733                     sizeof(audio_sample_format_t) );
734     p_channel_mixer->p_module =
735         module_Need( p_channel_mixer,"audio filter", psz_name, VLC_TRUE );
736     if( p_channel_mixer->p_module== NULL )
737     {
738         msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
739         vlc_object_detach( p_channel_mixer );
740         vlc_object_destroy( p_channel_mixer );
741         return NULL;
742     }
743     p_channel_mixer->b_continuity = VLC_FALSE;
744
745     /* Ok */
746     return p_channel_mixer;
747 }