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