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