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