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