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