]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
Fixed invalid p_aout->p_mixer usage in aout input.
[vlc] / src / audio_output / input.c
1 /*****************************************************************************
2  * input.c : internal management of input streams for the audio output
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <assert.h>
33
34 #include <vlc_common.h>
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <math.h>
39 #include <assert.h>
40
41 #include <vlc_input.h>
42 #include <vlc_vout.h>                  /* for vout_Request */
43
44 #include <vlc_aout.h>
45 #include <libvlc.h>
46
47 #include "aout_internal.h"
48
49 #define AOUT_ASSERT_MIXER_LOCKED vlc_assert_locked( &p_aout->mixer_lock )
50 #define AOUT_ASSERT_INPUT_LOCKED vlc_assert_locked( &p_input->lock )
51
52 static void inputFailure( aout_instance_t *, aout_input_t *, const char * );
53 static void inputDrop( aout_input_t *, aout_buffer_t * );
54 static void inputResamplingStop( aout_input_t *p_input );
55
56 static int VisualizationCallback( vlc_object_t *, char const *,
57                                   vlc_value_t, vlc_value_t, void * );
58 static int EqualizerCallback( vlc_object_t *, char const *,
59                               vlc_value_t, vlc_value_t, void * );
60 static int ReplayGainCallback( vlc_object_t *, char const *,
61                                vlc_value_t, vlc_value_t, void * );
62 static void ReplayGainSelect( aout_instance_t *, aout_input_t * );
63
64 static vout_thread_t *RequestVout( void *,
65                                    vout_thread_t *, video_format_t *, bool );
66 static vout_thread_t *RequestVoutFromFilter( 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     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     psz_filters = var_GetString( p_aout, "audio-filter" );
234     psz_visual = var_GetString( p_aout, "audio-visual");
235     psz_scaletempo = var_GetBool( 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_NON_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             aout_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             static const char typename[] = "audio filter";
276             p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
277                                           VLC_OBJECT_GENERIC, typename );
278             if( p_filter == NULL )
279             {
280                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
281                          psz_parser );
282                 psz_parser = psz_next;
283                 continue;
284             }
285
286             vlc_object_set_name( p_filter, psz_parser );
287             vlc_object_attach( p_filter , p_aout );
288
289             p_filter->request_vout.pf_request_vout = RequestVoutFromFilter;
290             p_filter->request_vout.p_private = p_input;
291
292             p_filter->p_owner = malloc( sizeof(*p_filter->p_owner) );
293             p_filter->p_owner->p_aout  = p_aout;
294             p_filter->p_owner->p_input = p_input;
295
296             /* request format */
297             memcpy( &p_filter->input, &chain_output_format,
298                     sizeof(audio_sample_format_t) );
299             memcpy( &p_filter->output, &chain_output_format,
300                     sizeof(audio_sample_format_t) );
301
302
303             /* try to find the requested filter */
304             if( i_visual == 2 ) /* this can only be a visualization module */
305             {
306                 p_filter->p_module = module_need( p_filter, "visualization",
307                                                   psz_parser, true );
308             }
309             else /* this can be a audio filter module as well as a visualization module */
310             {
311                 p_filter->p_module = module_need( p_filter, "audio filter",
312                                               psz_parser, true );
313
314                 if ( p_filter->p_module == NULL )
315                 {
316                     /* if the filter requested a special format, retry */
317                     if ( !( AOUT_FMTS_IDENTICAL( &p_filter->input,
318                                                  &chain_input_format )
319                             && AOUT_FMTS_IDENTICAL( &p_filter->output,
320                                                     &chain_output_format ) ) )
321                     {
322                         aout_FormatPrepare( &p_filter->input );
323                         aout_FormatPrepare( &p_filter->output );
324                         p_filter->p_module = module_need( p_filter,
325                                                           "audio filter",
326                                                           psz_parser, true );
327                     }
328                     /* try visual filters */
329                     else
330                     {
331                         memcpy( &p_filter->input, &chain_output_format,
332                                 sizeof(audio_sample_format_t) );
333                         memcpy( &p_filter->output, &chain_output_format,
334                                 sizeof(audio_sample_format_t) );
335                         p_filter->p_module = module_need( p_filter,
336                                                           "visualization",
337                                                           psz_parser, true );
338                     }
339                 }
340             }
341
342             /* failure */
343             if ( p_filter->p_module == NULL )
344             {
345                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
346                          psz_parser );
347
348                 free( p_filter->p_owner );
349                 vlc_object_detach( p_filter );
350                 vlc_object_release( p_filter );
351
352                 psz_parser = psz_next;
353                 continue;
354             }
355
356             /* complete the filter chain if necessary */
357             if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &p_filter->input ) )
358             {
359                 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
360                                                  &p_input->i_nb_filters,
361                                                  &chain_input_format,
362                                                  &p_filter->input ) < 0 )
363                 {
364                     msg_Err( p_aout, "cannot add user filter %s (skipped)",
365                              psz_parser );
366
367                     module_unneed( p_filter, p_filter->p_module );
368                     free( p_filter->p_owner );
369                     vlc_object_detach( p_filter );
370                     vlc_object_release( p_filter );
371
372                     psz_parser = psz_next;
373                     continue;
374                 }
375             }
376
377             /* success */
378             p_filter->b_continuity = false;
379             p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
380             memcpy( &chain_input_format, &p_filter->output,
381                     sizeof( audio_sample_format_t ) );
382
383             if( i_visual == 0 ) /* scaletempo */
384                 p_input->p_playback_rate_filter = p_filter;
385
386             /* next filter if any */
387             psz_parser = psz_next;
388         }
389     }
390     free( psz_visual );
391     free( psz_filters );
392     free( psz_scaletempo );
393
394     /* complete the filter chain if necessary */
395     if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &chain_output_format ) )
396     {
397         if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
398                                          &p_input->i_nb_filters,
399                                          &chain_input_format,
400                                          &chain_output_format ) < 0 )
401         {
402             inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
403             return -1;
404         }
405     }
406
407     /* Prepare hints for the buffer allocator. */
408     p_input->input_alloc.b_alloc = true;
409     p_input->input_alloc.i_bytes_per_sec = -1;
410
411     /* Create resamplers. */
412     if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer_format ) )
413     {
414         chain_output_format.i_rate = (__MAX(p_input->input.i_rate,
415                                             p_aout->mixer_format.i_rate)
416                                  * (100 + AOUT_MAX_RESAMPLING)) / 100;
417         if ( chain_output_format.i_rate == p_aout->mixer_format.i_rate )
418         {
419             /* Just in case... */
420             chain_output_format.i_rate++;
421         }
422         if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
423                                          &p_input->i_nb_resamplers,
424                                          &chain_output_format,
425                                          &p_aout->mixer_format ) < 0 )
426         {
427             inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
428             return -1;
429         }
430
431         aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
432                                  p_input->i_nb_resamplers,
433                                  &p_input->input_alloc );
434         p_input->input_alloc.b_alloc = true;
435
436         /* Setup the initial rate of the resampler */
437         p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
438     }
439     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
440
441     if( ! p_input->p_playback_rate_filter && p_input->i_nb_resamplers > 0 )
442     {
443         p_input->p_playback_rate_filter = p_input->pp_resamplers[0];
444     }
445
446     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
447                              p_input->i_nb_filters,
448                              &p_input->input_alloc );
449     p_input->input_alloc.b_alloc = true;
450
451     /* i_bytes_per_sec is still == -1 if no filters */
452     p_input->input_alloc.i_bytes_per_sec = __MAX(
453                                     p_input->input_alloc.i_bytes_per_sec,
454                                     (int)(p_input->input.i_bytes_per_frame
455                                      * p_input->input.i_rate
456                                      / p_input->input.i_frame_length) );
457
458     ReplayGainSelect( p_aout, p_input );
459
460     /* Success */
461     p_input->b_error = false;
462     p_input->b_restart = false;
463     p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
464
465     return 0;
466 }
467
468 /*****************************************************************************
469  * aout_InputDelete : delete an input
470  *****************************************************************************
471  * This function must be entered with the mixer lock.
472  *****************************************************************************/
473 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
474 {
475     AOUT_ASSERT_MIXER_LOCKED;
476     if ( p_input->b_error )
477         return 0;
478
479     /* XXX We need to update b_recycle_vout before calling aout_FiltersDestroyPipeline.
480      * FIXME They can be a race condition if audio-visual is updated between
481      * aout_InputDelete and aout_InputNew.
482      */
483     char *psz_visual = var_GetString( p_aout, "audio-visual");
484     p_input->b_recycle_vout = psz_visual && *psz_visual;
485     free( psz_visual );
486
487     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
488                                  p_input->i_nb_filters );
489     p_input->i_nb_filters = 0;
490     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
491                                  p_input->i_nb_resamplers );
492     p_input->i_nb_resamplers = 0;
493     aout_FifoDestroy( p_aout, &p_input->mixer.fifo );
494
495     return 0;
496 }
497
498 /*****************************************************************************
499  * aout_InputPlay : play a buffer
500  *****************************************************************************
501  * This function must be entered with the input lock.
502  *****************************************************************************/
503 /* XXX Do not activate it !! */
504 //#define AOUT_PROCESS_BEFORE_CHEKS
505 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
506                     aout_buffer_t * p_buffer, int i_input_rate )
507 {
508     mtime_t start_date;
509     AOUT_ASSERT_INPUT_LOCKED;
510
511     if( p_input->b_restart )
512     {
513         aout_fifo_t fifo;
514         uint8_t     *p_first_byte_to_mix;
515         bool        b_paused;
516         mtime_t     i_pause_date;
517
518         aout_lock_mixer( p_aout );
519         aout_lock_input_fifos( p_aout );
520
521         /* A little trick to avoid loosing our input fifo and properties */
522
523         p_first_byte_to_mix = p_input->mixer.begin;
524         fifo = p_input->mixer.fifo;
525         b_paused = p_input->b_paused;
526         i_pause_date = p_input->i_pause_date;
527
528         aout_FifoInit( p_aout, &p_input->mixer.fifo, p_aout->mixer_format.i_rate );
529
530         aout_InputDelete( p_aout, p_input );
531
532         aout_InputNew( p_aout, p_input, &p_input->request_vout );
533         p_input->mixer.begin = p_first_byte_to_mix;
534         p_input->mixer.fifo = fifo;
535         p_input->b_paused = b_paused;
536         p_input->i_pause_date = i_pause_date;
537
538         aout_unlock_input_fifos( p_aout );
539         aout_unlock_mixer( p_aout );
540     }
541
542     if( i_input_rate != INPUT_RATE_DEFAULT && p_input->p_playback_rate_filter == NULL )
543     {
544         inputDrop( p_input, p_buffer );
545         return 0;
546     }
547
548 #ifdef AOUT_PROCESS_BEFORE_CHEKS
549     /* Run pre-filters. */
550     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
551                       &p_buffer );
552
553     /* Actually run the resampler now. */
554     if ( p_input->i_nb_resamplers > 0 )
555     {
556         const mtime_t i_date = p_buffer->start_date;
557         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
558                           p_input->i_nb_resamplers,
559                           &p_buffer );
560     }
561
562     if( p_buffer->i_nb_samples <= 0 )
563     {
564         aout_BufferFree( p_buffer );
565         return 0;
566     }
567 #endif
568
569     /* Handle input rate change, but keep drift correction */
570     if( i_input_rate != p_input->i_last_input_rate )
571     {
572         unsigned int * const pi_rate = &p_input->p_playback_rate_filter->input.i_rate;
573 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
574         const int i_delta = *pi_rate - F(p_input->input.i_rate,p_input->i_last_input_rate);
575         *pi_rate = F(p_input->input.i_rate + i_delta, i_input_rate);
576 #undef F
577         p_input->i_last_input_rate = i_input_rate;
578     }
579
580     /* We don't care if someone changes the start date behind our back after
581      * this. We'll deal with that when pushing the buffer, and compensate
582      * with the next incoming buffer. */
583     aout_lock_input_fifos( p_aout );
584     start_date = aout_FifoNextStart( p_aout, &p_input->mixer.fifo );
585     aout_unlock_input_fifos( p_aout );
586
587     if ( start_date != 0 && start_date < mdate() )
588     {
589         /* The decoder is _very_ late. This can only happen if the user
590          * pauses the stream (or if the decoder is buggy, which cannot
591          * happen :). */
592         msg_Warn( p_aout, "computed PTS is out of range (%"PRId64"), "
593                   "clearing out", mdate() - start_date );
594         aout_lock_input_fifos( p_aout );
595         aout_FifoSet( p_aout, &p_input->mixer.fifo, 0 );
596         p_input->mixer.begin = NULL;
597         aout_unlock_input_fifos( p_aout );
598         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
599             msg_Warn( p_aout, "timing screwed, stopping resampling" );
600         inputResamplingStop( p_input );
601         start_date = 0;
602     }
603
604     if ( p_buffer->start_date < 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->start_date );
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->start_date - i_pts_tolerance ) )
621     {
622         msg_Warn( p_aout, "audio drift is too big (%"PRId64"), clearing out",
623                   start_date - p_buffer->start_date );
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         start_date = 0;
632     }
633     else if ( start_date != 0 &&
634               ( start_date > p_buffer->start_date + i_pts_tolerance) )
635     {
636         msg_Warn( p_aout, "audio drift is too big (%"PRId64"), dropping buffer",
637                   start_date - p_buffer->start_date );
638         inputDrop( p_input, p_buffer );
639         return 0;
640     }
641
642     if ( start_date == 0 ) start_date = p_buffer->start_date;
643
644 #ifndef AOUT_PROCESS_BEFORE_CHEKS
645     /* Run pre-filters. */
646     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
647                       &p_buffer );
648 #endif
649
650     /* Run the resampler if needed.
651      * We first need to calculate the output rate of this resampler. */
652     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
653          ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
654            || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
655          p_input->i_nb_resamplers > 0 )
656     {
657         /* Can happen in several circumstances :
658          * 1. A problem at the input (clock drift)
659          * 2. A small pause triggered by the user
660          * 3. Some delay in the output stage, causing a loss of lip
661          *    synchronization
662          * Solution : resample the buffer to avoid a scratch.
663          */
664         mtime_t drift = p_buffer->start_date - start_date;
665
666         p_input->i_resamp_start_date = mdate();
667         p_input->i_resamp_start_drift = (int)drift;
668
669         if ( drift > 0 )
670             p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
671         else
672             p_input->i_resampling_type = AOUT_RESAMPLING_UP;
673
674         msg_Warn( p_aout, "buffer is %"PRId64" %s, triggering %ssampling",
675                           drift > 0 ? drift : -drift,
676                           drift > 0 ? "in advance" : "late",
677                           drift > 0 ? "down" : "up");
678     }
679
680     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
681     {
682         /* Resampling has been triggered previously (because of dates
683          * mismatch). We want the resampling to happen progressively so
684          * it isn't too audible to the listener. */
685
686         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
687         {
688             p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
689         }
690         else
691         {
692             p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
693         }
694
695         /* Check if everything is back to normal, in which case we can stop the
696          * resampling */
697         unsigned int i_nominal_rate =
698           (p_input->pp_resamplers[0] == p_input->p_playback_rate_filter)
699           ? INPUT_RATE_DEFAULT * p_input->input.i_rate / i_input_rate
700           : p_input->input.i_rate;
701         if( p_input->pp_resamplers[0]->input.i_rate == i_nominal_rate )
702         {
703             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
704             msg_Warn( p_aout, "resampling stopped after %"PRIi64" usec "
705                       "(drift: %"PRIi64")",
706                       mdate() - p_input->i_resamp_start_date,
707                       p_buffer->start_date - start_date);
708         }
709         else if( abs( (int)(p_buffer->start_date - start_date) ) <
710                  abs( p_input->i_resamp_start_drift ) / 2 )
711         {
712             /* if we reduced the drift from half, then it is time to switch
713              * back the resampling direction. */
714             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
715                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
716             else
717                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
718             p_input->i_resamp_start_drift = 0;
719         }
720         else if( p_input->i_resamp_start_drift &&
721                  ( abs( (int)(p_buffer->start_date - start_date) ) >
722                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
723         {
724             /* If the drift is increasing and not decreasing, than something
725              * is bad. We'd better stop the resampling right now. */
726             msg_Warn( p_aout, "timing screwed, stopping resampling" );
727             inputResamplingStop( p_input );
728         }
729     }
730
731 #ifndef AOUT_PROCESS_BEFORE_CHEKS
732     /* Actually run the resampler now. */
733     if ( p_input->i_nb_resamplers > 0 )
734     {
735         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
736                           p_input->i_nb_resamplers,
737                           &p_buffer );
738     }
739
740     if( p_buffer->i_nb_samples <= 0 )
741     {
742         aout_BufferFree( p_buffer );
743         return 0;
744     }
745 #endif
746
747     /* Adding the start date will be managed by aout_FifoPush(). */
748     p_buffer->end_date = start_date +
749         (p_buffer->end_date - p_buffer->start_date);
750     p_buffer->start_date = start_date;
751
752     aout_lock_input_fifos( p_aout );
753     aout_FifoPush( p_aout, &p_input->mixer.fifo, p_buffer );
754     aout_unlock_input_fifos( p_aout );
755     return 0;
756 }
757
758 /*****************************************************************************
759  * static functions
760  *****************************************************************************/
761
762 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
763                           const char * psz_error_message )
764 {
765     /* error message */
766     msg_Err( p_aout, "%s", psz_error_message );
767
768     /* clean up */
769     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
770                                  p_input->i_nb_filters );
771     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
772                                  p_input->i_nb_resamplers );
773     aout_FifoDestroy( p_aout, &p_input->mixer.fifo );
774     var_Destroy( p_aout, "visual" );
775     var_Destroy( p_aout, "equalizer" );
776     var_Destroy( p_aout, "audio-filter" );
777     var_Destroy( p_aout, "audio-visual" );
778
779     var_Destroy( p_aout, "audio-replay-gain-mode" );
780     var_Destroy( p_aout, "audio-replay-gain-default" );
781     var_Destroy( p_aout, "audio-replay-gain-preamp" );
782     var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
783
784     /* error flag */
785     p_input->b_error = 1;
786 }
787
788 static void inputDrop( aout_input_t *p_input, aout_buffer_t *p_buffer )
789 {
790     aout_BufferFree( p_buffer );
791
792     p_input->i_buffer_lost++;
793 }
794
795 static void inputResamplingStop( aout_input_t *p_input )
796 {
797     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
798     if( p_input->i_nb_resamplers != 0 )
799     {
800         p_input->pp_resamplers[0]->input.i_rate =
801             ( p_input->pp_resamplers[0] == p_input->p_playback_rate_filter )
802             ? INPUT_RATE_DEFAULT * p_input->input.i_rate / p_input->i_last_input_rate
803             : p_input->input.i_rate;
804         p_input->pp_resamplers[0]->b_continuity = false;
805     }
806 }
807
808 static vout_thread_t *RequestVout( void *p_private,
809                                    vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
810 {
811     aout_instance_t *p_aout = p_private;
812     VLC_UNUSED(b_recycle);
813     return vout_Request( p_aout, p_vout, p_fmt );
814 }
815
816 static vout_thread_t *RequestVoutFromFilter( void *p_private,
817                                             vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
818 {
819     aout_input_t *p_input = p_private;
820     aout_request_vout_t *p_request = &p_input->request_vout;
821
822     return p_request->pf_request_vout( p_request->p_private,
823                                        p_vout, p_fmt, p_input->b_recycle_vout && b_recycle );
824 }
825
826 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
827                                  const char *psz_name, bool b_add )
828 {
829     return AoutChangeFilterString( VLC_OBJECT(p_aout), p_aout,
830                                    psz_variable, psz_name, b_add ) ? 1 : 0;
831 }
832
833 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
834                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
835 {
836     aout_instance_t *p_aout = (aout_instance_t *)p_this;
837     char *psz_mode = newval.psz_string;
838     (void)psz_cmd; (void)oldval; (void)p_data;
839
840     if( !psz_mode || !*psz_mode )
841     {
842         ChangeFiltersString( p_aout, "audio-visual", "goom", false );
843         ChangeFiltersString( p_aout, "audio-visual", "visual", false );
844         ChangeFiltersString( p_aout, "audio-visual", "projectm", false );
845     }
846     else
847     {
848         if( !strcmp( "goom", psz_mode ) )
849         {
850             ChangeFiltersString( p_aout, "audio-visual", "visual", false );
851             ChangeFiltersString( p_aout, "audio-visual", "goom", true );
852             ChangeFiltersString( p_aout, "audio-visual", "projectm", false );
853         }
854         else if( !strcmp( "projectm", psz_mode ) )
855         {
856             ChangeFiltersString( p_aout, "audio-visual", "visual", false );
857             ChangeFiltersString( p_aout, "audio-visual", "goom", false );
858             ChangeFiltersString( p_aout, "audio-visual", "projectm", true );
859         }
860         else
861         {
862             var_Create( p_aout, "effect-list", VLC_VAR_STRING );
863             var_SetString( p_aout, "effect-list", psz_mode );
864
865             ChangeFiltersString( p_aout, "audio-visual", "goom", false );
866             ChangeFiltersString( p_aout, "audio-visual", "visual", true );
867             ChangeFiltersString( p_aout, "audio-visual", "projectm", false );
868         }
869     }
870
871     /* That sucks */
872     AoutInputsMarkToRestart( p_aout );
873
874     return VLC_SUCCESS;
875 }
876
877 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
878                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
879 {
880     aout_instance_t *p_aout = (aout_instance_t *)p_this;
881     char *psz_mode = newval.psz_string;
882     int i_ret;
883     (void)psz_cmd; (void)oldval; (void)p_data;
884
885     if( !psz_mode || !*psz_mode )
886     {
887         i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
888                                      false );
889     }
890     else
891     {
892         var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
893         var_SetString( p_aout, "equalizer-preset", psz_mode );
894         i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
895                                      true );
896     }
897
898     /* That sucks */
899     if( i_ret == 1 )
900         AoutInputsMarkToRestart( p_aout );
901     return VLC_SUCCESS;
902 }
903
904 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
905                                vlc_value_t oldval, vlc_value_t newval, void *p_data )
906 {
907     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
908     VLC_UNUSED(newval); VLC_UNUSED(p_data);
909     aout_instance_t *p_aout = (aout_instance_t *)p_this;
910     int i;
911
912     aout_lock_mixer( p_aout );
913     for( i = 0; i < p_aout->i_nb_inputs; i++ )
914         ReplayGainSelect( p_aout, p_aout->pp_inputs[i] );
915
916     /* Restart the mixer (a trivial mixer may be in use) */
917     if( p_aout->p_mixer )
918         aout_MixerMultiplierSet( p_aout, p_aout->mixer_multiplier );
919     aout_unlock_mixer( p_aout );
920
921     return VLC_SUCCESS;
922 }
923
924 static void ReplayGainSelect( aout_instance_t *p_aout, aout_input_t *p_input )
925 {
926     char *psz_replay_gain = var_GetNonEmptyString( p_aout,
927                                                    "audio-replay-gain-mode" );
928     int i_mode;
929     int i_use;
930     float f_gain;
931
932     p_input->mixer.multiplier = 1.0;
933
934     if( !psz_replay_gain )
935         return;
936
937     /* Find select mode */
938     if( !strcmp( psz_replay_gain, "track" ) )
939         i_mode = AUDIO_REPLAY_GAIN_TRACK;
940     else if( !strcmp( psz_replay_gain, "album" ) )
941         i_mode = AUDIO_REPLAY_GAIN_ALBUM;
942     else
943         i_mode = AUDIO_REPLAY_GAIN_MAX;
944
945     /* If the select mode is not available, prefer the other one */
946     i_use = i_mode;
947     if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
948     {
949         for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
950         {
951             if( p_input->replay_gain.pb_gain[i_use] )
952                 break;
953         }
954     }
955
956     /* */
957     if( i_use != AUDIO_REPLAY_GAIN_MAX )
958         f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
959     else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
960         f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
961     else
962         f_gain = 0.0;
963     p_input->mixer.multiplier = pow( 10.0, f_gain / 20.0 );
964
965     /* */
966     if( p_input->replay_gain.pb_peak[i_use] &&
967         var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
968         p_input->replay_gain.pf_peak[i_use] * p_input->mixer.multiplier > 1.0 )
969     {
970         p_input->mixer.multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
971     }
972
973     free( psz_replay_gain );
974 }
975