]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
Fix use of unitialized variable reported by David Thiel, fix from Christophe Massiot
[vlc] / src / audio_output / input.c
1 /*****************************************************************************
2  * input.c : internal management of input streams for the audio output
3  *****************************************************************************
4  * Copyright (C) 2002-2007 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 *, const 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_resamplers = 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 = (char *)p_config->ppsz_list[i];
138                 text.psz_string = (char *)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         chain_output_format.i_rate = (__MAX(p_input->input.i_rate,
329                                             p_aout->mixer.mixer.i_rate)
330                                  * (100 + AOUT_MAX_RESAMPLING)) / 100;
331         if ( chain_output_format.i_rate == p_aout->mixer.mixer.i_rate )
332         {
333             /* Just in case... */
334             chain_output_format.i_rate++;
335         }
336         if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
337                                          &p_input->i_nb_resamplers,
338                                          &chain_output_format,
339                                          &p_aout->mixer.mixer ) < 0 )
340         {
341             inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
342             return -1;
343         }
344
345         aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
346                                  p_input->i_nb_resamplers,
347                                  &p_input->input_alloc );
348         p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
349
350         /* Setup the initial rate of the resampler */
351         p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
352     }
353     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
354
355     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
356                              p_input->i_nb_filters,
357                              &p_input->input_alloc );
358     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
359
360     /* i_bytes_per_sec is still == -1 if no filters */
361     p_input->input_alloc.i_bytes_per_sec = __MAX(
362                                     p_input->input_alloc.i_bytes_per_sec,
363                                     (int)(p_input->input.i_bytes_per_frame
364                                      * p_input->input.i_rate
365                                      / p_input->input.i_frame_length) );
366
367     /* Success */
368     p_input->b_error = VLC_FALSE;
369     p_input->b_restart = VLC_FALSE;
370
371     return 0;
372 }
373
374 /*****************************************************************************
375  * aout_InputDelete : delete an input
376  *****************************************************************************
377  * This function must be entered with the mixer lock.
378  *****************************************************************************/
379 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
380 {
381     if ( p_input->b_error ) return 0;
382
383     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
384                                  p_input->i_nb_filters );
385     p_input->i_nb_filters = 0;
386     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
387                                  p_input->i_nb_resamplers );
388     p_input->i_nb_resamplers = 0;
389     aout_FifoDestroy( p_aout, &p_input->fifo );
390
391     return 0;
392 }
393
394 /*****************************************************************************
395  * aout_InputPlay : play a buffer
396  *****************************************************************************
397  * This function must be entered with the input lock.
398  *****************************************************************************/
399 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
400                     aout_buffer_t * p_buffer )
401 {
402     mtime_t start_date;
403
404     if( p_input->b_restart )
405     {
406         aout_fifo_t fifo, dummy_fifo;
407         byte_t      *p_first_byte_to_mix;
408
409         vlc_mutex_lock( &p_aout->mixer_lock );
410
411         /* A little trick to avoid loosing our input fifo */
412         aout_FifoInit( p_aout, &dummy_fifo, p_aout->mixer.mixer.i_rate );
413         p_first_byte_to_mix = p_input->p_first_byte_to_mix;
414         fifo = p_input->fifo;
415         p_input->fifo = dummy_fifo;
416         aout_InputDelete( p_aout, p_input );
417         aout_InputNew( p_aout, p_input );
418         p_input->p_first_byte_to_mix = p_first_byte_to_mix;
419         p_input->fifo = fifo;
420
421         vlc_mutex_unlock( &p_aout->mixer_lock );
422     }
423
424     /* We don't care if someone changes the start date behind our back after
425      * this. We'll deal with that when pushing the buffer, and compensate
426      * with the next incoming buffer. */
427     vlc_mutex_lock( &p_aout->input_fifos_lock );
428     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
429     vlc_mutex_unlock( &p_aout->input_fifos_lock );
430
431     if ( start_date != 0 && start_date < mdate() )
432     {
433         /* The decoder is _very_ late. This can only happen if the user
434          * pauses the stream (or if the decoder is buggy, which cannot
435          * happen :). */
436         msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
437                   "clearing out", mdate() - start_date );
438         vlc_mutex_lock( &p_aout->input_fifos_lock );
439         aout_FifoSet( p_aout, &p_input->fifo, 0 );
440         p_input->p_first_byte_to_mix = NULL;
441         vlc_mutex_unlock( &p_aout->input_fifos_lock );
442         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
443             msg_Warn( p_aout, "timing screwed, stopping resampling" );
444         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
445         if ( p_input->i_nb_resamplers != 0 )
446         {
447             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
448             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
449         }
450         start_date = 0;
451         if( p_input->p_input_thread )
452         {
453             vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
454             stats_UpdateInteger( p_aout, p_input->p_input_thread->p->counters.p_lost_abuffers, 1, NULL );
455             vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
456         }
457     }
458
459     if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
460     {
461         /* The decoder gives us f*cked up PTS. It's its business, but we
462          * can't present it anyway, so drop the buffer. */
463         msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
464                   mdate() - p_buffer->start_date );
465         if( p_input->p_input_thread )
466         {
467             vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
468             stats_UpdateInteger( p_aout, p_input->p_input_thread->p->counters.p_lost_abuffers, 1, NULL );
469             vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
470         }
471         aout_BufferFree( p_buffer );
472         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
473         if ( p_input->i_nb_resamplers != 0 )
474         {
475             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
476             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
477         }
478         return 0;
479     }
480
481     /* If the audio drift is too big then it's not worth trying to resample
482      * the audio. */
483     if ( start_date != 0 &&
484          ( start_date < p_buffer->start_date - 3 * AOUT_PTS_TOLERANCE ) )
485     {
486         msg_Warn( p_aout, "audio drift is too big ("I64Fd"), clearing out",
487                   start_date - p_buffer->start_date );
488         vlc_mutex_lock( &p_aout->input_fifos_lock );
489         aout_FifoSet( p_aout, &p_input->fifo, 0 );
490         p_input->p_first_byte_to_mix = NULL;
491         vlc_mutex_unlock( &p_aout->input_fifos_lock );
492         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
493             msg_Warn( p_aout, "timing screwed, stopping resampling" );
494         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
495         if ( p_input->i_nb_resamplers != 0 )
496         {
497             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
498             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
499         }
500         start_date = 0;
501     }
502     else if ( start_date != 0 &&
503               ( start_date > p_buffer->start_date + 3 * AOUT_PTS_TOLERANCE ) )
504     {
505         msg_Warn( p_aout, "audio drift is too big ("I64Fd"), dropping buffer",
506                   start_date - p_buffer->start_date );
507         aout_BufferFree( p_buffer );
508         if( p_input->p_input_thread )
509         {
510             vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
511             stats_UpdateInteger( p_aout, p_input->p_input_thread->p->counters.p_lost_abuffers, 1, NULL );
512             vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
513         }
514         return 0;
515     }
516
517     if ( start_date == 0 ) start_date = p_buffer->start_date;
518
519     /* Run pre-filters. */
520
521     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
522                       &p_buffer );
523
524     /* Run the resampler if needed.
525      * We first need to calculate the output rate of this resampler. */
526     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
527          ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
528            || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
529          p_input->i_nb_resamplers > 0 )
530     {
531         /* Can happen in several circumstances :
532          * 1. A problem at the input (clock drift)
533          * 2. A small pause triggered by the user
534          * 3. Some delay in the output stage, causing a loss of lip
535          *    synchronization
536          * Solution : resample the buffer to avoid a scratch.
537          */
538         mtime_t drift = p_buffer->start_date - start_date;
539
540         p_input->i_resamp_start_date = mdate();
541         p_input->i_resamp_start_drift = (int)drift;
542
543         if ( drift > 0 )
544             p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
545         else
546             p_input->i_resampling_type = AOUT_RESAMPLING_UP;
547
548         msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
549                           drift > 0 ? drift : -drift,
550                           drift > 0 ? "in advance" : "late",
551                           drift > 0 ? "down" : "up");
552     }
553
554     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
555     {
556         /* Resampling has been triggered previously (because of dates
557          * mismatch). We want the resampling to happen progressively so
558          * it isn't too audible to the listener. */
559
560         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
561         {
562             p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
563         }
564         else
565         {
566             p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
567         }
568
569         /* Check if everything is back to normal, in which case we can stop the
570          * resampling */
571         if( p_input->pp_resamplers[0]->input.i_rate ==
572               p_input->input.i_rate )
573         {
574             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
575             msg_Warn( p_aout, "resampling stopped after "I64Fi" usec "
576                       "(drift: "I64Fi")",
577                       mdate() - p_input->i_resamp_start_date,
578                       p_buffer->start_date - start_date);
579         }
580         else if( abs( (int)(p_buffer->start_date - start_date) ) <
581                  abs( p_input->i_resamp_start_drift ) / 2 )
582         {
583             /* if we reduced the drift from half, then it is time to switch
584              * back the resampling direction. */
585             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
586                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
587             else
588                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
589             p_input->i_resamp_start_drift = 0;
590         }
591         else if( p_input->i_resamp_start_drift &&
592                  ( abs( (int)(p_buffer->start_date - start_date) ) >
593                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
594         {
595             /* If the drift is increasing and not decreasing, than something
596              * is bad. We'd better stop the resampling right now. */
597             msg_Warn( p_aout, "timing screwed, stopping resampling" );
598             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
599             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
600         }
601     }
602
603     /* Adding the start date will be managed by aout_FifoPush(). */
604     p_buffer->end_date = start_date +
605         (p_buffer->end_date - p_buffer->start_date);
606     p_buffer->start_date = start_date;
607
608     /* Actually run the resampler now. */
609     if ( p_input->i_nb_resamplers > 0 )
610     {
611         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
612                           p_input->i_nb_resamplers,
613                           &p_buffer );
614     }
615
616     vlc_mutex_lock( &p_aout->input_fifos_lock );
617     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
618     vlc_mutex_unlock( &p_aout->input_fifos_lock );
619
620     return 0;
621 }
622
623 /*****************************************************************************
624  * static functions
625  *****************************************************************************/
626
627 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
628                           const char * psz_error_message )
629 {
630     /* error message */
631     msg_Err( p_aout, "%s", psz_error_message );
632
633     /* clean up */
634     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
635                                  p_input->i_nb_filters );
636     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
637                                  p_input->i_nb_resamplers );
638     aout_FifoDestroy( p_aout, &p_input->fifo );
639     var_Destroy( p_aout, "visual" );
640     var_Destroy( p_aout, "equalizer" );
641     var_Destroy( p_aout, "audio-filter" );
642     var_Destroy( p_aout, "audio-visual" );
643
644     /* error flag */
645     p_input->b_error = 1;
646 }
647
648 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
649                                  const char *psz_name, vlc_bool_t b_add )
650 {
651     vlc_value_t val;
652     char *psz_parser;
653
654     var_Get( p_aout, psz_variable, &val );
655
656     if( !val.psz_string ) val.psz_string = strdup("");
657
658     psz_parser = strstr( val.psz_string, psz_name );
659
660     if( b_add )
661     {
662         if( !psz_parser )
663         {
664             psz_parser = val.psz_string;
665             asprintf( &val.psz_string, (*val.psz_string) ? "%s:%s" : "%s%s",
666                       val.psz_string, psz_name );
667             free( psz_parser );
668         }
669         else
670         {
671             return 0;
672         }
673     }
674     else
675     {
676         if( psz_parser )
677         {
678             memmove( psz_parser, psz_parser + strlen(psz_name) +
679                      (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
680                      strlen(psz_parser + strlen(psz_name)) + 1 );
681         }
682         else
683         {
684             free( val.psz_string );
685             return 0;
686         }
687     }
688
689     var_Set( p_aout, psz_variable, val );
690     free( val.psz_string );
691     return 1;
692 }
693
694 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
695                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
696 {
697     aout_instance_t *p_aout = (aout_instance_t *)p_this;
698     char *psz_mode = newval.psz_string;
699     vlc_value_t val;
700     int i;
701     (void)psz_cmd; (void)oldval; (void)p_data;
702
703     if( !psz_mode || !*psz_mode )
704     {
705         ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
706         ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
707         ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE );
708     }
709     else
710     {
711         if( !strcmp( "goom", psz_mode ) )
712         {
713             ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
714             ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_TRUE );
715             ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE);
716         }
717         else if( !strcmp( "galaktos", psz_mode ) )
718         {
719             ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
720             ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
721             ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_TRUE );
722         }
723         else
724         {
725             val.psz_string = psz_mode;
726             var_Create( p_aout, "effect-list", VLC_VAR_STRING );
727             var_Set( p_aout, "effect-list", val );
728
729             ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
730             ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_TRUE );
731             ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE);
732         }
733     }
734
735     /* That sucks */
736     for( i = 0; i < p_aout->i_nb_inputs; i++ )
737     {
738         p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
739     }
740
741     return VLC_SUCCESS;
742 }
743
744 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
745                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
746 {
747     aout_instance_t *p_aout = (aout_instance_t *)p_this;
748     char *psz_mode = newval.psz_string;
749     vlc_value_t val;
750     int i;
751     int i_ret;
752     (void)psz_cmd; (void)oldval; (void)p_data;
753
754     if( !psz_mode || !*psz_mode )
755     {
756         i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
757                                      VLC_FALSE );
758     }
759     else
760     {
761         val.psz_string = psz_mode;
762         var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
763         var_Set( p_aout, "equalizer-preset", val );
764         i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
765                                      VLC_TRUE );
766
767     }
768
769     /* That sucks */
770     if( i_ret == 1 )
771     {
772         for( i = 0; i < p_aout->i_nb_inputs; i++ )
773         {
774             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
775         }
776     }
777
778     return VLC_SUCCESS;
779 }