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