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