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