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