]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
aout: delete input callbacks when input is deleted
[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 #include <vlc_aout.h>
45 #include <vlc_filter.h>
46 #include <vlc_atomic.h>
47 #include <libvlc.h>
48
49 #include "aout_internal.h"
50
51 static void inputFailure( audio_output_t *, aout_input_t *, const char * );
52 static void inputDrop( aout_input_t *, aout_buffer_t * );
53 static void inputResamplingStop( aout_input_t *p_input );
54
55 static int VisualizationCallback( vlc_object_t *, char const *,
56                                   vlc_value_t, vlc_value_t, void * );
57 static int EqualizerCallback( vlc_object_t *, char const *,
58                               vlc_value_t, vlc_value_t, void * );
59 static int ReplayGainCallback( vlc_object_t *, char const *,
60                                vlc_value_t, vlc_value_t, void * );
61 static float ReplayGainSelect(vlc_object_t *, const char *,
62                               const audio_replay_gain_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( audio_output_t * p_aout,
71                    const audio_sample_format_t *restrict infmt,
72                    const audio_sample_format_t *restrict outfmt,
73                    aout_input_t * p_input,
74                    const aout_request_vout_t *p_request_vout )
75 {
76     audio_sample_format_t chain_input_format;
77     audio_sample_format_t chain_output_format;
78     vlc_value_t val, text;
79     char *psz_filters, *psz_visual, *psz_scaletempo;
80     int i_visual;
81
82     aout_FormatPrint( p_aout, "input", infmt );
83     p_input->samplerate = infmt->i_rate;
84
85     p_input->i_nb_resamplers = p_input->i_nb_filters = 0;
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  = *infmt;
100     chain_output_format = *outfmt;
101     chain_output_format.i_rate = infmt->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     }
141     var_AddCallback( p_aout, "visual", VisualizationCallback, p_input );
142
143     if( var_Type( p_aout, "equalizer" ) == 0 )
144     {
145         module_config_t *p_config;
146         int i;
147
148         var_Create( p_aout, "equalizer",
149                     VLC_VAR_STRING | VLC_VAR_HASCHOICE );
150         p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
151         if( p_config && p_config->i_list )
152         {
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         }
168     }
169     var_AddCallback( p_aout, "equalizer", EqualizerCallback, p_input );
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     }
209     var_AddCallback( p_aout, "audio-replay-gain-mode", ReplayGainCallback, p_input );
210
211     char *gain = var_InheritString (p_aout, "audio-replay-gain-mode");
212     vlc_atomic_setf (&p_input->multiplier,
213                      ReplayGainSelect (VLC_OBJECT(p_aout), gain,
214                                        &p_input->replay_gain));
215     free (gain);
216
217     if( var_Type( p_aout, "audio-replay-gain-preamp" ) == 0 )
218     {
219         var_Create( p_aout, "audio-replay-gain-preamp",
220                     VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
221     }
222     if( var_Type( p_aout, "audio-replay-gain-default" ) == 0 )
223     {
224         var_Create( p_aout, "audio-replay-gain-default",
225                     VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
226     }
227     if( var_Type( p_aout, "audio-replay-gain-peak-protection" ) == 0 )
228     {
229         var_Create( p_aout, "audio-replay-gain-peak-protection",
230                     VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
231     }
232
233     psz_filters = var_GetString( p_aout, "audio-filter" );
234     psz_visual = var_GetString( p_aout, "audio-visual");
235     psz_scaletempo = var_InheritBool( p_aout, "audio-time-stretch" ) ? strdup( "scaletempo" ) : NULL;
236
237     p_input->b_recycle_vout = psz_visual && *psz_visual;
238
239     /* parse user filter lists */
240     char *const ppsz_array[] = { psz_scaletempo, psz_filters, psz_visual };
241     p_input->p_playback_rate_filter = NULL;
242
243     for( i_visual = 0; i_visual < 3 && AOUT_FMT_LINEAR(&chain_output_format); i_visual++ )
244     {
245         char *psz_next = NULL;
246         char *psz_parser = ppsz_array[i_visual];
247
248         if( psz_parser == NULL || !*psz_parser )
249             continue;
250
251         while( psz_parser && *psz_parser )
252         {
253             filter_t * p_filter = NULL;
254
255             if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
256             {
257                 msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
258                 break;
259             }
260
261             while( *psz_parser == ' ' && *psz_parser == ':' )
262             {
263                 psz_parser++;
264             }
265             if( ( psz_next = strchr( psz_parser , ':'  ) ) )
266             {
267                 *psz_next++ = '\0';
268             }
269             if( *psz_parser =='\0' )
270             {
271                 break;
272             }
273
274             /* Create a VLC object */
275             p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
276                                           "audio filter" );
277             if( p_filter == NULL )
278             {
279                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
280                          psz_parser );
281                 psz_parser = psz_next;
282                 continue;
283             }
284
285             p_filter->p_owner = malloc( sizeof(*p_filter->p_owner) );
286             p_filter->p_owner->p_aout  = p_aout;
287             p_filter->p_owner->p_input = p_input;
288
289             /* request format */
290             memcpy( &p_filter->fmt_in.audio, &chain_output_format,
291                     sizeof(audio_sample_format_t) );
292             p_filter->fmt_in.i_codec = chain_output_format.i_format;
293             memcpy( &p_filter->fmt_out.audio, &chain_output_format,
294                     sizeof(audio_sample_format_t) );
295             p_filter->fmt_out.i_codec = chain_output_format.i_format;
296             p_filter->pf_audio_buffer_new = aout_FilterBufferNew;
297
298             /* try to find the requested filter */
299             if( i_visual == 2 ) /* this can only be a visualization module */
300             {
301                 p_filter->p_module = module_need( p_filter, "visualization2",
302                                                   psz_parser, true );
303             }
304             else /* this can be a audio filter module as well as a visualization module */
305             {
306                 p_filter->p_module = module_need( p_filter, "audio filter",
307                                               psz_parser, true );
308
309                 if ( p_filter->p_module == NULL )
310                 {
311                     /* if the filter requested a special format, retry */
312                     if ( !( AOUT_FMTS_IDENTICAL( &p_filter->fmt_in.audio,
313                                                  &chain_input_format )
314                             && AOUT_FMTS_IDENTICAL( &p_filter->fmt_out.audio,
315                                                     &chain_output_format ) ) )
316                     {
317                         aout_FormatPrepare( &p_filter->fmt_in.audio );
318                         aout_FormatPrepare( &p_filter->fmt_out.audio );
319                         p_filter->p_module = module_need( p_filter,
320                                                           "audio filter",
321                                                           psz_parser, true );
322                     }
323                     /* try visual filters */
324                     else
325                     {
326                         memcpy( &p_filter->fmt_in.audio, &chain_output_format,
327                                 sizeof(audio_sample_format_t) );
328                         memcpy( &p_filter->fmt_out.audio, &chain_output_format,
329                                 sizeof(audio_sample_format_t) );
330                         p_filter->p_module = module_need( p_filter,
331                                                           "visualization2",
332                                                           psz_parser, true );
333                     }
334                 }
335             }
336
337             /* failure */
338             if ( p_filter->p_module == NULL )
339             {
340                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
341                          psz_parser );
342
343                 free( p_filter->p_owner );
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_FiltersCreatePipeline( p_aout, p_input->pp_filters,
352                                              &p_input->i_nb_filters,
353                                              &chain_input_format,
354                                              &p_filter->fmt_in.audio ) < 0 )
355             {
356                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
357                          psz_parser );
358
359                 module_unneed( p_filter, p_filter->p_module );
360                 free( p_filter->p_owner );
361                 vlc_object_release( p_filter );
362
363                 psz_parser = psz_next;
364                 continue;
365             }
366
367             /* success */
368             p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
369             memcpy( &chain_input_format, &p_filter->fmt_out.audio,
370                     sizeof( audio_sample_format_t ) );
371
372             if( i_visual == 0 ) /* scaletempo */
373                 p_input->p_playback_rate_filter = p_filter;
374
375             /* next filter if any */
376             psz_parser = psz_next;
377         }
378     }
379     free( psz_visual );
380     free( psz_filters );
381     free( psz_scaletempo );
382
383     /* complete the filter chain if necessary */
384     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
385                                      &p_input->i_nb_filters,
386                                      &chain_input_format,
387                                      &chain_output_format ) < 0 )
388     {
389         inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
390         return -1;
391     }
392
393     /* Create resamplers. */
394     if (AOUT_FMT_LINEAR(outfmt))
395     {
396         chain_output_format.i_rate = (__MAX(p_input->samplerate,
397                                             outfmt->i_rate)
398                                  * (100 + AOUT_MAX_RESAMPLING)) / 100;
399         if ( chain_output_format.i_rate == outfmt->i_rate )
400         {
401             /* Just in case... */
402             chain_output_format.i_rate++;
403         }
404         if (aout_FiltersCreatePipeline (p_aout, p_input->pp_resamplers,
405                                         &p_input->i_nb_resamplers,
406                                         &chain_output_format, outfmt) < 0)
407         {
408             inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
409             return -1;
410         }
411
412         /* Setup the initial rate of the resampler */
413         p_input->pp_resamplers[0]->fmt_in.audio.i_rate = p_input->samplerate;
414     }
415     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
416
417     if( ! p_input->p_playback_rate_filter && p_input->i_nb_resamplers > 0 )
418     {
419         p_input->p_playback_rate_filter = p_input->pp_resamplers[0];
420     }
421
422     /* Success */
423     p_input->b_error = false;
424     p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
425
426     return 0;
427 }
428
429 /*****************************************************************************
430  * aout_InputDelete : delete an input
431  *****************************************************************************
432  * This function must be entered with the mixer lock.
433  *****************************************************************************/
434 int aout_InputDelete( audio_output_t * p_aout, aout_input_t * p_input )
435 {
436     aout_assert_locked( p_aout );
437     if ( p_input->b_error )
438         return 0;
439
440     var_DelCallback (p_aout, "audio-replay-gain-mode", ReplayGainCallback,
441                      p_input);
442     var_DelCallback (p_aout, "equalizer", EqualizerCallback, p_input);
443     var_DelCallback (p_aout, "visual", VisualizationCallback, p_input);
444
445     /* XXX We need to update b_recycle_vout before calling aout_FiltersDestroyPipeline.
446      * FIXME They can be a race condition if audio-visual is updated between
447      * aout_InputDelete and aout_InputNew.
448      */
449     char *psz_visual = var_GetString( p_aout, "audio-visual");
450     p_input->b_recycle_vout = psz_visual && *psz_visual;
451     free( psz_visual );
452
453     aout_FiltersDestroyPipeline( p_input->pp_filters, p_input->i_nb_filters );
454     p_input->i_nb_filters = 0;
455     aout_FiltersDestroyPipeline( p_input->pp_resamplers,
456                                  p_input->i_nb_resamplers );
457     p_input->i_nb_resamplers = 0;
458
459     return 0;
460 }
461
462 /*****************************************************************************
463  * aout_InputPlay : play a buffer
464  *****************************************************************************
465  * This function must be entered with the input lock.
466  *****************************************************************************/
467 /* XXX Do not activate it !! */
468 //#define AOUT_PROCESS_BEFORE_CHEKS
469 block_t *aout_InputPlay(audio_output_t *p_aout, aout_input_t *p_input,
470                         block_t *p_buffer, int i_input_rate, date_t *date )
471 {
472     mtime_t start_date;
473
474     aout_assert_locked( p_aout );
475
476     if( i_input_rate != INPUT_RATE_DEFAULT && p_input->p_playback_rate_filter == NULL )
477     {
478         inputDrop( p_input, p_buffer );
479         return NULL;
480     }
481
482 #ifdef AOUT_PROCESS_BEFORE_CHEKS
483     /* Run pre-filters. */
484     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
485                       &p_buffer );
486     if( !p_buffer )
487         return NULL;
488
489     /* Actually run the resampler now. */
490     if ( p_input->i_nb_resamplers > 0 )
491     {
492         const mtime_t i_date = p_buffer->i_pts;
493         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
494                           p_input->i_nb_resamplers,
495                           &p_buffer );
496     }
497
498     if( !p_buffer )
499         return NULL;
500     if( p_buffer->i_nb_samples <= 0 )
501     {
502         block_Release( p_buffer );
503         return NULL;
504     }
505 #endif
506
507     /* Handle input rate change, but keep drift correction */
508     if( i_input_rate != p_input->i_last_input_rate )
509     {
510         unsigned int * const pi_rate = &p_input->p_playback_rate_filter->fmt_in.audio.i_rate;
511 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
512         const int i_delta = *pi_rate - F(p_input->samplerate,p_input->i_last_input_rate);
513         *pi_rate = F(p_input->samplerate + i_delta, i_input_rate);
514 #undef F
515         p_input->i_last_input_rate = i_input_rate;
516     }
517
518     mtime_t now = mdate();
519
520     /* We don't care if someone changes the start date behind our back after
521      * this. We'll deal with that when pushing the buffer, and compensate
522      * with the next incoming buffer. */
523     start_date = date_Get (date);
524
525     if ( start_date != VLC_TS_INVALID && start_date < now )
526     {
527         /* The decoder is _very_ late. This can only happen if the user
528          * pauses the stream (or if the decoder is buggy, which cannot
529          * happen :). */
530         msg_Warn( p_aout, "computed PTS is out of range (%"PRId64"), "
531                   "clearing out", now - start_date );
532         aout_OutputFlush( p_aout, false );
533         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
534             msg_Warn( p_aout, "timing screwed, stopping resampling" );
535         inputResamplingStop( p_input );
536         p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
537         start_date = VLC_TS_INVALID;
538     }
539
540     if ( p_buffer->i_pts < now + AOUT_MIN_PREPARE_TIME )
541     {
542         /* The decoder gives us f*cked up PTS. It's its business, but we
543          * can't present it anyway, so drop the buffer. */
544         msg_Warn( p_aout, "PTS is out of range (%"PRId64"), dropping buffer",
545                   now - p_buffer->i_pts );
546         inputDrop( p_input, p_buffer );
547         inputResamplingStop( p_input );
548         return NULL;
549     }
550
551     /* If the audio drift is too big then it's not worth trying to resample
552      * the audio. */
553     if( start_date == VLC_TS_INVALID )
554     {
555         start_date = p_buffer->i_pts;
556         date_Set (date, start_date);
557     }
558
559     mtime_t drift = start_date - p_buffer->i_pts;
560
561     if( drift < -i_input_rate * 3 * AOUT_MAX_PTS_ADVANCE / INPUT_RATE_DEFAULT )
562     {
563         msg_Warn( p_aout, "buffer way too early (%"PRId64"), clearing queue",
564                   drift );
565         aout_OutputFlush( p_aout, false );
566         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
567             msg_Warn( p_aout, "timing screwed, stopping resampling" );
568         inputResamplingStop( p_input );
569         p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
570         start_date = p_buffer->i_pts;
571         date_Set (date, start_date);
572         drift = 0;
573     }
574     else
575     if( drift > +i_input_rate * 3 * AOUT_MAX_PTS_DELAY / INPUT_RATE_DEFAULT )
576     {
577         msg_Warn( p_aout, "buffer way too late (%"PRId64"), dropping buffer",
578                   drift );
579         inputDrop( p_input, p_buffer );
580         return NULL;
581     }
582
583 #ifndef AOUT_PROCESS_BEFORE_CHEKS
584     /* Run pre-filters. */
585     aout_FiltersPlay( p_input->pp_filters, p_input->i_nb_filters, &p_buffer );
586     if( !p_buffer )
587         return NULL;
588 #endif
589
590     /* Run the resampler if needed.
591      * We first need to calculate the output rate of this resampler. */
592     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
593          ( drift < -AOUT_MAX_PTS_ADVANCE || drift > +AOUT_MAX_PTS_DELAY ) &&
594          p_input->i_nb_resamplers > 0 )
595     {
596         /* Can happen in several circumstances :
597          * 1. A problem at the input (clock drift)
598          * 2. A small pause triggered by the user
599          * 3. Some delay in the output stage, causing a loss of lip
600          *    synchronization
601          * Solution : resample the buffer to avoid a scratch.
602          */
603         p_input->i_resamp_start_date = now;
604         p_input->i_resamp_start_drift = (int)-drift;
605         p_input->i_resampling_type = (drift < 0) ? AOUT_RESAMPLING_DOWN
606                                                  : AOUT_RESAMPLING_UP;
607         msg_Warn( p_aout, (drift < 0)
608                   ? "buffer too early (%"PRId64"), down-sampling"
609                   : "buffer too late  (%"PRId64"), up-sampling", drift );
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             p_input->pp_resamplers[0]->fmt_in.audio.i_rate += 2; /* Hz */
620         else
621             p_input->pp_resamplers[0]->fmt_in.audio.i_rate -= 2; /* Hz */
622
623         /* Check if everything is back to normal, in which case we can stop the
624          * resampling */
625         unsigned int i_nominal_rate =
626           (p_input->pp_resamplers[0] == p_input->p_playback_rate_filter)
627           ? INPUT_RATE_DEFAULT * p_input->samplerate / i_input_rate
628           : p_input->samplerate;
629         if( p_input->pp_resamplers[0]->fmt_in.audio.i_rate == i_nominal_rate )
630         {
631             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
632             msg_Warn( p_aout, "resampling stopped after %"PRIi64" usec "
633                       "(drift: %"PRIi64")",
634                       now - p_input->i_resamp_start_date,
635                       p_buffer->i_pts - start_date);
636         }
637         else if( abs( (int)(p_buffer->i_pts - 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->i_pts - 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             p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
657         }
658     }
659
660 #ifndef AOUT_PROCESS_BEFORE_CHEKS
661     /* Actually run the resampler now. */
662     if ( p_input->i_nb_resamplers > 0 )
663     {
664         aout_FiltersPlay( p_input->pp_resamplers, p_input->i_nb_resamplers,
665                           &p_buffer );
666     }
667
668     if( !p_buffer )
669         return NULL;
670     if( p_buffer->i_nb_samples <= 0 )
671     {
672         block_Release( p_buffer );
673         return NULL;
674     }
675 #endif
676
677     p_buffer->i_pts = start_date;
678     return p_buffer;
679 }
680
681 /*****************************************************************************
682  * static functions
683  *****************************************************************************/
684
685 static void inputFailure( audio_output_t * p_aout, aout_input_t * p_input,
686                           const char * psz_error_message )
687 {
688     /* error message */
689     msg_Err( p_aout, "%s", psz_error_message );
690
691     /* clean up */
692     aout_FiltersDestroyPipeline( p_input->pp_filters, p_input->i_nb_filters );
693     aout_FiltersDestroyPipeline( p_input->pp_resamplers,
694                                  p_input->i_nb_resamplers );
695     var_Destroy( p_aout, "visual" );
696     var_Destroy( p_aout, "equalizer" );
697     var_Destroy( p_aout, "audio-filter" );
698     var_Destroy( p_aout, "audio-visual" );
699
700     var_Destroy( p_aout, "audio-replay-gain-mode" );
701     var_Destroy( p_aout, "audio-replay-gain-default" );
702     var_Destroy( p_aout, "audio-replay-gain-preamp" );
703     var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
704
705     /* error flag */
706     p_input->b_error = 1;
707 }
708
709 static void inputDrop( aout_input_t *p_input, aout_buffer_t *p_buffer )
710 {
711     aout_BufferFree( p_buffer );
712
713     p_input->i_buffer_lost++;
714 }
715
716 static void inputResamplingStop( aout_input_t *p_input )
717 {
718     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
719     if( p_input->i_nb_resamplers != 0 )
720     {
721         p_input->pp_resamplers[0]->fmt_in.audio.i_rate =
722             ( p_input->pp_resamplers[0] == p_input->p_playback_rate_filter )
723             ? INPUT_RATE_DEFAULT * p_input->samplerate / p_input->i_last_input_rate
724             : p_input->samplerate;
725     }
726 }
727
728 static vout_thread_t *RequestVout( void *p_private,
729                                    vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
730 {
731     audio_output_t *p_aout = p_private;
732     VLC_UNUSED(b_recycle);
733     vout_configuration_t cfg = {
734         .vout       = p_vout,
735         .input      = NULL,
736         .change_fmt = true,
737         .fmt        = p_fmt,
738         .dpb_size   = 1,
739     };
740     return vout_Request( p_aout, &cfg );
741 }
742
743 vout_thread_t *aout_filter_RequestVout( filter_t *p_filter,
744                                         vout_thread_t *p_vout, video_format_t *p_fmt )
745 {
746     aout_input_t *p_input = p_filter->p_owner->p_input;
747     aout_request_vout_t *p_request = &p_input->request_vout;
748
749     /* XXX: this only works from audio input */
750     /* If you want to use visualization filters from another place, you will
751      * need to add a new pf_aout_request_vout callback or store a pointer
752      * to aout_request_vout_t inside filter_t (i.e. a level of indirection). */
753
754     return p_request->pf_request_vout( p_request->p_private,
755                                        p_vout, p_fmt, p_input->b_recycle_vout );
756 }
757
758 static inline bool ChangeFiltersString (vlc_object_t *aout, const char *var,
759                                         const char *filter, bool add)
760 {
761     return aout_ChangeFilterString (aout, aout, var, filter, add);
762 }
763
764 static int VisualizationCallback (vlc_object_t *obj, char const *var,
765                                   vlc_value_t oldval, vlc_value_t newval,
766                                   void *data)
767 {
768     const char *mode = newval.psz_string;
769     //aout_input_t *input = data;
770     (void) data;
771
772     if (!*mode)
773     {
774         ChangeFiltersString (obj, "audio-visual", "goom", false);
775         ChangeFiltersString (obj, "audio-visual", "visual", false);
776         ChangeFiltersString (obj, "audio-visual", "projectm", false);
777     }
778     else if (!strcmp ("goom", mode))
779     {
780         ChangeFiltersString (obj, "audio-visual", "visual", false );
781         ChangeFiltersString (obj, "audio-visual", "goom", true );
782         ChangeFiltersString (obj, "audio-visual", "projectm", false );
783     }
784     else if (!strcmp ("projectm", mode))
785     {
786         ChangeFiltersString (obj, "audio-visual", "visual", false);
787         ChangeFiltersString (obj, "audio-visual", "goom", false);
788         ChangeFiltersString (obj, "audio-visual", "projectm", true);
789     }
790     else
791     {
792         var_Create (obj, "effect-list", VLC_VAR_STRING);
793         var_SetString (obj, "effect-list", mode);
794
795         ChangeFiltersString (obj, "audio-visual", "goom", false);
796         ChangeFiltersString (obj, "audio-visual", "visual", true);
797         ChangeFiltersString (obj, "audio-visual", "projectm", false);
798     }
799
800     /* That sucks FIXME: use "input" instead of cast */
801     aout_InputRequestRestart ((audio_output_t *)obj);
802
803     (void) var; (void) oldval;
804     return VLC_SUCCESS;
805 }
806
807 static int EqualizerCallback (vlc_object_t *obj, char const *cmd,
808                               vlc_value_t oldval, vlc_value_t newval,
809                               void *data)
810 {
811     char *mode = newval.psz_string;
812     //aout_input_t *input = data;
813     bool ret;
814
815     (void) data;
816     (void) cmd; (void) oldval;
817     if (!*mode)
818         ret = ChangeFiltersString (obj, "audio-filter", "equalizer", false);
819     else
820     {
821         var_Create (obj, "equalizer-preset", VLC_VAR_STRING);
822         var_SetString (obj, "equalizer-preset", mode);
823         ret = ChangeFiltersString (obj, "audio-filter", "equalizer", true);
824     }
825
826     /* That sucks */
827     if (ret)
828         aout_InputRequestRestart ((audio_output_t *)obj);
829     return VLC_SUCCESS;
830 }
831
832 float aout_InputGetMultiplier (const aout_input_t *input)
833 {
834     return vlc_atomic_getf (&input->multiplier);
835 }
836
837 static int ReplayGainCallback (vlc_object_t *obj, char const *var,
838                                vlc_value_t oldval, vlc_value_t val, void *data)
839 {
840     aout_input_t *input = data;
841     float multiplier = ReplayGainSelect (obj, val.psz_string,
842                                          &input->replay_gain);
843
844     vlc_atomic_setf (&input->multiplier, multiplier);
845
846     VLC_UNUSED(var); VLC_UNUSED(oldval);
847     return VLC_SUCCESS;
848 }
849
850 static float ReplayGainSelect (vlc_object_t *obj, const char *str,
851                                const audio_replay_gain_t *replay_gain)
852 {
853     float gain = 0.;
854     unsigned mode = AUDIO_REPLAY_GAIN_MAX;
855
856     if (likely(str != NULL))
857     {   /* Find selectrf mode */
858         if (!strcmp (str, "track"))
859             mode = AUDIO_REPLAY_GAIN_TRACK;
860         else
861         if (!strcmp (str, "album"))
862             mode = AUDIO_REPLAY_GAIN_ALBUM;
863
864         /* If the selectrf mode is not available, prefer the other one */
865         if (mode != AUDIO_REPLAY_GAIN_MAX && !replay_gain->pb_gain[mode])
866         {
867             if (replay_gain->pb_gain[!mode])
868                 mode = !mode;
869         }
870     }
871
872     /* */
873     if (mode == AUDIO_REPLAY_GAIN_MAX)
874         return 1.;
875
876     if (replay_gain->pb_gain[mode])
877         gain = replay_gain->pf_gain[mode]
878              + var_InheritFloat (obj, "audio-replay-gain-preamp");
879     else
880         gain = var_InheritFloat (obj, "audio-replay-gain-default");
881
882     float multiplier = pow (10., gain / 20.);
883
884     if (replay_gain->pb_peak[mode]
885      && var_InheritBool (obj, "audio-replay-gain-peak-protection")
886      && replay_gain->pf_peak[mode] * multiplier > 1.0)
887         multiplier = 1.0f / replay_gain->pf_peak[mode];
888
889     return multiplier;
890 }