]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
New vu meter visualization.
[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/vlc.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     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
405                              p_input->i_nb_filters,
406                              &p_input->input_alloc );
407     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
408
409     /* i_bytes_per_sec is still == -1 if no filters */
410     p_input->input_alloc.i_bytes_per_sec = __MAX(
411                                     p_input->input_alloc.i_bytes_per_sec,
412                                     (int)(p_input->input.i_bytes_per_frame
413                                      * p_input->input.i_rate
414                                      / p_input->input.i_frame_length) );
415
416     ReplayGainSelect( p_aout, p_input );
417
418     /* Success */
419     p_input->b_error = false;
420     p_input->b_restart = false;
421     p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
422
423     return 0;
424 }
425
426 /*****************************************************************************
427  * aout_InputDelete : delete an input
428  *****************************************************************************
429  * This function must be entered with the mixer lock.
430  *****************************************************************************/
431 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
432 {
433     if ( p_input->b_error ) return 0;
434
435     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
436                                  p_input->i_nb_filters );
437     p_input->i_nb_filters = 0;
438     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
439                                  p_input->i_nb_resamplers );
440     p_input->i_nb_resamplers = 0;
441     aout_FifoDestroy( p_aout, &p_input->fifo );
442
443     return 0;
444 }
445
446 /*****************************************************************************
447  * aout_InputPlay : play a buffer
448  *****************************************************************************
449  * This function must be entered with the input lock.
450  *****************************************************************************/
451 /* XXX Do not activate it !! */
452 //#define AOUT_PROCESS_BEFORE_CHEKS
453 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
454                     aout_buffer_t * p_buffer, int i_input_rate )
455 {
456     mtime_t start_date;
457
458     if( p_input->b_restart )
459     {
460         aout_fifo_t fifo, dummy_fifo;
461         uint8_t     *p_first_byte_to_mix;
462
463         vlc_mutex_lock( &p_aout->mixer_lock );
464
465         /* A little trick to avoid loosing our input fifo */
466         aout_FifoInit( p_aout, &dummy_fifo, p_aout->mixer.mixer.i_rate );
467         p_first_byte_to_mix = p_input->p_first_byte_to_mix;
468         fifo = p_input->fifo;
469         p_input->fifo = dummy_fifo;
470         aout_InputDelete( p_aout, p_input );
471         aout_InputNew( p_aout, p_input );
472         p_input->p_first_byte_to_mix = p_first_byte_to_mix;
473         p_input->fifo = fifo;
474
475         vlc_mutex_unlock( &p_aout->mixer_lock );
476     }
477
478     if( i_input_rate != INPUT_RATE_DEFAULT && p_input->i_nb_resamplers <= 0 )
479     {
480         inputDrop( p_aout, p_input, p_buffer );
481         return 0;
482     }
483
484 #ifdef AOUT_PROCESS_BEFORE_CHEKS
485     /* Run pre-filters. */
486     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
487                       &p_buffer );
488
489     /* Actually run the resampler now. */
490     if ( p_input->i_nb_resamplers > 0 )
491     {
492         const mtime_t i_date = p_buffer->start_date;
493         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
494                           p_input->i_nb_resamplers,
495                           &p_buffer );
496     }
497
498     if( p_buffer->i_nb_samples <= 0 )
499     {
500         aout_BufferFree( p_buffer );
501         return 0;
502     }
503 #endif
504
505     /* Handle input rate change by modifying resampler input rate */
506     if( i_input_rate != p_input->i_last_input_rate )
507     {
508         unsigned int * const pi_rate = &p_input->pp_resamplers[0]->input.i_rate;
509 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
510         const int i_delta = *pi_rate - F(p_input->input.i_rate,p_input->i_last_input_rate);
511         *pi_rate = F(p_input->input.i_rate + i_delta, i_input_rate);
512 #undef F
513         p_input->i_last_input_rate = i_input_rate;
514     }
515
516     /* We don't care if someone changes the start date behind our back after
517      * this. We'll deal with that when pushing the buffer, and compensate
518      * with the next incoming buffer. */
519     vlc_mutex_lock( &p_aout->input_fifos_lock );
520     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
521     vlc_mutex_unlock( &p_aout->input_fifos_lock );
522
523     if ( start_date != 0 && start_date < mdate() )
524     {
525         /* The decoder is _very_ late. This can only happen if the user
526          * pauses the stream (or if the decoder is buggy, which cannot
527          * happen :). */
528         msg_Warn( p_aout, "computed PTS is out of range (%"PRId64"), "
529                   "clearing out", mdate() - start_date );
530         vlc_mutex_lock( &p_aout->input_fifos_lock );
531         aout_FifoSet( p_aout, &p_input->fifo, 0 );
532         p_input->p_first_byte_to_mix = NULL;
533         vlc_mutex_unlock( &p_aout->input_fifos_lock );
534         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
535             msg_Warn( p_aout, "timing screwed, stopping resampling" );
536         inputResamplingStop( p_input );
537         start_date = 0;
538     }
539
540     if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
541     {
542         /* The decoder gives us f*cked up PTS. It's its business, but we
543          * can't present it anyway, so drop the buffer. */
544         msg_Warn( p_aout, "PTS is out of range (%"PRId64"), dropping buffer",
545                   mdate() - p_buffer->start_date );
546
547         inputDrop( p_aout, p_input, p_buffer );
548         inputResamplingStop( p_input );
549         return 0;
550     }
551
552     /* If the audio drift is too big then it's not worth trying to resample
553      * the audio. */
554     if ( start_date != 0 &&
555          ( start_date < p_buffer->start_date - 3 * AOUT_PTS_TOLERANCE ) )
556     {
557         msg_Warn( p_aout, "audio drift is too big (%"PRId64"), clearing out",
558                   start_date - p_buffer->start_date );
559         vlc_mutex_lock( &p_aout->input_fifos_lock );
560         aout_FifoSet( p_aout, &p_input->fifo, 0 );
561         p_input->p_first_byte_to_mix = NULL;
562         vlc_mutex_unlock( &p_aout->input_fifos_lock );
563         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
564             msg_Warn( p_aout, "timing screwed, stopping resampling" );
565         inputResamplingStop( p_input );
566         start_date = 0;
567     }
568     else if ( start_date != 0 &&
569               ( start_date > p_buffer->start_date + 3 * AOUT_PTS_TOLERANCE ) )
570     {
571         msg_Warn( p_aout, "audio drift is too big (%"PRId64"), dropping buffer",
572                   start_date - p_buffer->start_date );
573         inputDrop( p_aout, p_input, p_buffer );
574         return 0;
575     }
576
577     if ( start_date == 0 ) start_date = p_buffer->start_date;
578
579 #ifndef AOUT_PROCESS_BEFORE_CHEKS
580     /* Run pre-filters. */
581     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
582                       &p_buffer );
583 #endif
584
585     /* Run the resampler if needed.
586      * We first need to calculate the output rate of this resampler. */
587     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
588          ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
589            || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
590          p_input->i_nb_resamplers > 0 )
591     {
592         /* Can happen in several circumstances :
593          * 1. A problem at the input (clock drift)
594          * 2. A small pause triggered by the user
595          * 3. Some delay in the output stage, causing a loss of lip
596          *    synchronization
597          * Solution : resample the buffer to avoid a scratch.
598          */
599         mtime_t drift = p_buffer->start_date - start_date;
600
601         p_input->i_resamp_start_date = mdate();
602         p_input->i_resamp_start_drift = (int)drift;
603
604         if ( drift > 0 )
605             p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
606         else
607             p_input->i_resampling_type = AOUT_RESAMPLING_UP;
608
609         msg_Warn( p_aout, "buffer is %"PRId64" %s, triggering %ssampling",
610                           drift > 0 ? drift : -drift,
611                           drift > 0 ? "in advance" : "late",
612                           drift > 0 ? "down" : "up");
613     }
614
615     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
616     {
617         /* Resampling has been triggered previously (because of dates
618          * mismatch). We want the resampling to happen progressively so
619          * it isn't too audible to the listener. */
620
621         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
622         {
623             p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
624         }
625         else
626         {
627             p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
628         }
629
630         /* Check if everything is back to normal, in which case we can stop the
631          * resampling */
632         if( p_input->pp_resamplers[0]->input.i_rate == 1000 * p_input->input.i_rate / i_input_rate )
633         {
634             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
635             msg_Warn( p_aout, "resampling stopped after %"PRIi64" usec "
636                       "(drift: %"PRIi64")",
637                       mdate() - p_input->i_resamp_start_date,
638                       p_buffer->start_date - start_date);
639         }
640         else if( abs( (int)(p_buffer->start_date - start_date) ) <
641                  abs( p_input->i_resamp_start_drift ) / 2 )
642         {
643             /* if we reduced the drift from half, then it is time to switch
644              * back the resampling direction. */
645             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
646                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
647             else
648                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
649             p_input->i_resamp_start_drift = 0;
650         }
651         else if( p_input->i_resamp_start_drift &&
652                  ( abs( (int)(p_buffer->start_date - start_date) ) >
653                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
654         {
655             /* If the drift is increasing and not decreasing, than something
656              * is bad. We'd better stop the resampling right now. */
657             msg_Warn( p_aout, "timing screwed, stopping resampling" );
658             inputResamplingStop( p_input );
659         }
660     }
661
662 #ifndef AOUT_PROCESS_BEFORE_CHEKS
663     /* Actually run the resampler now. */
664     if ( p_input->i_nb_resamplers > 0 )
665     {
666         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
667                           p_input->i_nb_resamplers,
668                           &p_buffer );
669     }
670
671     if( p_buffer->i_nb_samples <= 0 )
672     {
673         aout_BufferFree( p_buffer );
674         return 0;
675     }
676 #endif
677
678     /* Adding the start date will be managed by aout_FifoPush(). */
679     p_buffer->end_date = start_date +
680         (p_buffer->end_date - p_buffer->start_date);
681     p_buffer->start_date = start_date;
682
683     vlc_mutex_lock( &p_aout->input_fifos_lock );
684     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
685     vlc_mutex_unlock( &p_aout->input_fifos_lock );
686     return 0;
687 }
688
689 /*****************************************************************************
690  * static functions
691  *****************************************************************************/
692
693 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
694                           const char * psz_error_message )
695 {
696     /* error message */
697     msg_Err( p_aout, "%s", psz_error_message );
698
699     /* clean up */
700     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
701                                  p_input->i_nb_filters );
702     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
703                                  p_input->i_nb_resamplers );
704     aout_FifoDestroy( p_aout, &p_input->fifo );
705     var_Destroy( p_aout, "visual" );
706     var_Destroy( p_aout, "equalizer" );
707     var_Destroy( p_aout, "audio-filter" );
708     var_Destroy( p_aout, "audio-visual" );
709
710     var_Destroy( p_aout, "audio-replay-gain-mode" );
711     var_Destroy( p_aout, "audio-replay-gain-default" );
712     var_Destroy( p_aout, "audio-replay-gain-preamp" );
713     var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
714
715     /* error flag */
716     p_input->b_error = 1;
717 }
718
719 static void inputDrop( aout_instance_t *p_aout, aout_input_t *p_input, aout_buffer_t *p_buffer )
720 {
721     aout_BufferFree( p_buffer );
722
723     if( !p_input->p_input_thread )
724         return;
725
726     vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
727     stats_UpdateInteger( p_aout, p_input->p_input_thread->p->counters.p_lost_abuffers, 1, NULL );
728     vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
729 }
730
731 static void inputResamplingStop( aout_input_t *p_input )
732 {
733     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
734     if( p_input->i_nb_resamplers != 0 )
735     {
736         p_input->pp_resamplers[0]->input.i_rate = INPUT_RATE_DEFAULT *
737                             p_input->input.i_rate / p_input->i_last_input_rate;
738         p_input->pp_resamplers[0]->b_continuity = false;
739     }
740 }
741
742 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
743                                  const char *psz_name, bool b_add )
744 {
745     vlc_value_t val;
746     char *psz_parser;
747
748     var_Get( p_aout, psz_variable, &val );
749
750     if( !val.psz_string ) val.psz_string = strdup("");
751
752     psz_parser = strstr( val.psz_string, psz_name );
753
754     if( b_add )
755     {
756         if( !psz_parser )
757         {
758             psz_parser = val.psz_string;
759             asprintf( &val.psz_string, (*val.psz_string) ? "%s:%s" : "%s%s",
760                       val.psz_string, psz_name );
761             free( psz_parser );
762         }
763         else
764         {
765             return 0;
766         }
767     }
768     else
769     {
770         if( psz_parser )
771         {
772             memmove( psz_parser, psz_parser + strlen(psz_name) +
773                      (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
774                      strlen(psz_parser + strlen(psz_name)) + 1 );
775         }
776         else
777         {
778             free( val.psz_string );
779             return 0;
780         }
781     }
782
783     var_Set( p_aout, psz_variable, val );
784     free( val.psz_string );
785     return 1;
786 }
787
788 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
789                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
790 {
791     aout_instance_t *p_aout = (aout_instance_t *)p_this;
792     char *psz_mode = newval.psz_string;
793     vlc_value_t val;
794     int i;
795     (void)psz_cmd; (void)oldval; (void)p_data;
796
797     if( !psz_mode || !*psz_mode )
798     {
799         ChangeFiltersString( p_aout, "audio-visual", "goom", false );
800         ChangeFiltersString( p_aout, "audio-visual", "visual", false );
801         ChangeFiltersString( p_aout, "audio-visual", "galaktos", false );
802     }
803     else
804     {
805         if( !strcmp( "goom", psz_mode ) )
806         {
807             ChangeFiltersString( p_aout, "audio-visual", "visual", false );
808             ChangeFiltersString( p_aout, "audio-visual", "goom", true );
809             ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
810         }
811         else if( !strcmp( "galaktos", psz_mode ) )
812         {
813             ChangeFiltersString( p_aout, "audio-visual", "visual", false );
814             ChangeFiltersString( p_aout, "audio-visual", "goom", false );
815             ChangeFiltersString( p_aout, "audio-visual", "galaktos", true );
816         }
817         else
818         {
819             val.psz_string = psz_mode;
820             var_Create( p_aout, "effect-list", VLC_VAR_STRING );
821             var_Set( p_aout, "effect-list", val );
822
823             ChangeFiltersString( p_aout, "audio-visual", "goom", false );
824             ChangeFiltersString( p_aout, "audio-visual", "visual", true );
825             ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
826         }
827     }
828
829     /* That sucks */
830     for( i = 0; i < p_aout->i_nb_inputs; i++ )
831     {
832         p_aout->pp_inputs[i]->b_restart = true;
833     }
834
835     return VLC_SUCCESS;
836 }
837
838 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
839                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
840 {
841     aout_instance_t *p_aout = (aout_instance_t *)p_this;
842     char *psz_mode = newval.psz_string;
843     vlc_value_t val;
844     int i;
845     int i_ret;
846     (void)psz_cmd; (void)oldval; (void)p_data;
847
848     if( !psz_mode || !*psz_mode )
849     {
850         i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
851                                      false );
852     }
853     else
854     {
855         val.psz_string = psz_mode;
856         var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
857         var_Set( p_aout, "equalizer-preset", val );
858         i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
859                                      true );
860
861     }
862
863     /* That sucks */
864     if( i_ret == 1 )
865     {
866         for( i = 0; i < p_aout->i_nb_inputs; i++ )
867         {
868             p_aout->pp_inputs[i]->b_restart = true;
869         }
870     }
871
872     return VLC_SUCCESS;
873 }
874
875 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
876                                vlc_value_t oldval, vlc_value_t newval, void *p_data )
877 {
878     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
879     VLC_UNUSED(newval); VLC_UNUSED(p_data);
880     aout_instance_t *p_aout = (aout_instance_t *)p_this;
881     int i;
882
883     vlc_mutex_lock( &p_aout->mixer_lock );
884     for( i = 0; i < p_aout->i_nb_inputs; i++ )
885         ReplayGainSelect( p_aout, p_aout->pp_inputs[i] );
886
887     /* Restart the mixer (a trivial mixer may be in use) */
888     aout_MixerMultiplierSet( p_aout, p_aout->mixer.f_multiplier );
889     vlc_mutex_unlock( &p_aout->mixer_lock );
890
891     return VLC_SUCCESS;
892 }
893
894 static void ReplayGainSelect( aout_instance_t *p_aout, aout_input_t *p_input )
895 {
896     char *psz_replay_gain = var_GetNonEmptyString( p_aout,
897                                                    "audio-replay-gain-mode" );
898     int i_mode;
899     int i_use;
900     float f_gain;
901
902     p_input->f_multiplier = 1.0;
903
904     if( !psz_replay_gain )
905         return;
906
907     /* Find select mode */
908     if( !strcmp( psz_replay_gain, "track" ) )
909         i_mode = AUDIO_REPLAY_GAIN_TRACK;
910     else if( !strcmp( psz_replay_gain, "album" ) )
911         i_mode = AUDIO_REPLAY_GAIN_ALBUM;
912     else
913         i_mode = AUDIO_REPLAY_GAIN_MAX;
914
915     /* If the select mode is not available, prefer the other one */
916     i_use = i_mode;
917     if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
918     {
919         for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
920         {
921             if( p_input->replay_gain.pb_gain[i_use] )
922                 break;
923         }
924     }
925
926     /* */
927     if( i_use != AUDIO_REPLAY_GAIN_MAX )
928         f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
929     else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
930         f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
931     else
932         f_gain = 0.0;
933     p_input->f_multiplier = pow( 10.0, f_gain / 20.0 );
934
935     /* */
936     if( p_input->replay_gain.pb_peak[i_use] &&
937         var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
938         p_input->replay_gain.pf_peak[i_use] * p_input->f_multiplier > 1.0 )
939     {
940         p_input->f_multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
941     }
942
943     free( psz_replay_gain );
944 }
945