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