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