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