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