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