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