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