]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
aout: move input format to aout_owner
[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 #include <vlc_modules.h>
44
45 #include <vlc_aout.h>
46 #include <vlc_filter.h>
47 #include <libvlc.h>
48
49 #include "aout_internal.h"
50
51 static void inputFailure( audio_output_t *, aout_input_t *, const char * );
52 static void inputDrop( aout_input_t *, aout_buffer_t * );
53 static void inputResamplingStop( aout_input_t *p_input );
54
55 static int VisualizationCallback( vlc_object_t *, char const *,
56                                   vlc_value_t, vlc_value_t, void * );
57 static int EqualizerCallback( vlc_object_t *, char const *,
58                               vlc_value_t, vlc_value_t, void * );
59 static int ReplayGainCallback( vlc_object_t *, char const *,
60                                vlc_value_t, vlc_value_t, void * );
61 static void ReplayGainSelect( audio_output_t *, aout_input_t * );
62
63 static vout_thread_t *RequestVout( void *,
64                                    vout_thread_t *, video_format_t *, bool );
65
66 /*****************************************************************************
67  * aout_InputNew : allocate a new input and rework the filter pipeline
68  *****************************************************************************/
69 int aout_InputNew( audio_output_t * p_aout,
70                    const audio_sample_format_t *restrict format,
71                    aout_input_t * p_input,
72                    const aout_request_vout_t *p_request_vout )
73 {
74     aout_owner_t *owner = aout_owner (p_aout);
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", format );
82     p_input->samplerate = format->i_rate;
83
84     p_input->i_nb_resamplers = p_input->i_nb_filters = 0;
85
86     /* */
87     if( p_request_vout )
88     {
89         p_input->request_vout = *p_request_vout;
90     }
91     else
92     {
93         p_input->request_vout.pf_request_vout = RequestVout;
94         p_input->request_vout.p_private = p_aout;
95     }
96
97     /* Prepare format structure */
98     chain_input_format  = *format;
99     chain_output_format = owner->mixer_format;
100     chain_output_format.i_rate = format->i_rate;
101     aout_FormatPrepare( &chain_output_format );
102
103     /* Now add user filters */
104     if( var_Type( p_aout, "visual" ) == 0 )
105     {
106         var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
107         text.psz_string = _("Visualizations");
108         var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
109         val.psz_string = (char*)""; text.psz_string = _("Disable");
110         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
111         val.psz_string = (char*)"spectrometer"; text.psz_string = _("Spectrometer");
112         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
113         val.psz_string = (char*)"scope"; text.psz_string = _("Scope");
114         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
115         val.psz_string = (char*)"spectrum"; text.psz_string = _("Spectrum");
116         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
117         val.psz_string = (char*)"vuMeter"; text.psz_string = _("Vu meter");
118         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
119
120         /* Look for goom plugin */
121         if( module_exists( "goom" ) )
122         {
123             val.psz_string = (char*)"goom"; text.psz_string = (char*)"Goom";
124             var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
125         }
126
127         /* Look for libprojectM plugin */
128         if( module_exists( "projectm" ) )
129         {
130             val.psz_string = (char*)"projectm"; text.psz_string = (char*)"projectM";
131             var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
132         }
133
134         if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
135         {
136             var_SetString( p_aout, "visual", val.psz_string );
137             free( val.psz_string );
138         }
139         var_AddCallback( p_aout, "visual", VisualizationCallback, p_input );
140     }
141
142     if( var_Type( p_aout, "equalizer" ) == 0 )
143     {
144         module_config_t *p_config;
145         int i;
146
147         p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
148         if( p_config && p_config->i_list )
149         {
150                var_Create( p_aout, "equalizer",
151                            VLC_VAR_STRING | VLC_VAR_HASCHOICE );
152             text.psz_string = _("Equalizer");
153             var_Change( p_aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL );
154
155             val.psz_string = (char*)""; text.psz_string = _("Disable");
156             var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text );
157
158             for( i = 0; i < p_config->i_list; i++ )
159             {
160                 val.psz_string = (char *)p_config->ppsz_list[i];
161                 text.psz_string = (char *)p_config->ppsz_list_text[i];
162                 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE,
163                             &val, &text );
164             }
165
166             var_AddCallback( p_aout, "equalizer", EqualizerCallback, NULL );
167         }
168     }
169
170     if( var_Type( p_aout, "audio-filter" ) == 0 )
171     {
172         var_Create( p_aout, "audio-filter",
173                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
174         text.psz_string = _("Audio filters");
175         var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
176     }
177     if( var_Type( p_aout, "audio-visual" ) == 0 )
178     {
179         var_Create( p_aout, "audio-visual",
180                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
181         text.psz_string = _("Audio visualizations");
182         var_Change( p_aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL );
183     }
184
185     if( var_Type( p_aout, "audio-replay-gain-mode" ) == 0 )
186     {
187         module_config_t *p_config;
188         int i;
189
190         p_config = config_FindConfig( VLC_OBJECT(p_aout), "audio-replay-gain-mode" );
191         if( p_config && p_config->i_list )
192         {
193             var_Create( p_aout, "audio-replay-gain-mode",
194                         VLC_VAR_STRING | VLC_VAR_DOINHERIT );
195
196             text.psz_string = _("Replay gain");
197             var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL );
198
199             for( i = 0; i < p_config->i_list; i++ )
200             {
201                 val.psz_string = (char *)p_config->ppsz_list[i];
202                 text.psz_string = (char *)p_config->ppsz_list_text[i];
203                 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
204                             &val, &text );
205             }
206
207             var_AddCallback( p_aout, "audio-replay-gain-mode", ReplayGainCallback, NULL );
208         }
209     }
210     if( var_Type( p_aout, "audio-replay-gain-preamp" ) == 0 )
211     {
212         var_Create( p_aout, "audio-replay-gain-preamp",
213                     VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
214     }
215     if( var_Type( p_aout, "audio-replay-gain-default" ) == 0 )
216     {
217         var_Create( p_aout, "audio-replay-gain-default",
218                     VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
219     }
220     if( var_Type( p_aout, "audio-replay-gain-peak-protection" ) == 0 )
221     {
222         var_Create( p_aout, "audio-replay-gain-peak-protection",
223                     VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
224     }
225
226     psz_filters = var_GetString( p_aout, "audio-filter" );
227     psz_visual = var_GetString( p_aout, "audio-visual");
228     psz_scaletempo = var_InheritBool( p_aout, "audio-time-stretch" ) ? strdup( "scaletempo" ) : NULL;
229
230     p_input->b_recycle_vout = psz_visual && *psz_visual;
231
232     /* parse user filter lists */
233     char *const ppsz_array[] = { psz_scaletempo, psz_filters, psz_visual };
234     p_input->p_playback_rate_filter = NULL;
235
236     for( i_visual = 0; i_visual < 3 && AOUT_FMT_LINEAR(&chain_output_format); i_visual++ )
237     {
238         char *psz_next = NULL;
239         char *psz_parser = ppsz_array[i_visual];
240
241         if( psz_parser == NULL || !*psz_parser )
242             continue;
243
244         while( psz_parser && *psz_parser )
245         {
246             filter_t * p_filter = NULL;
247
248             if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
249             {
250                 msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
251                 break;
252             }
253
254             while( *psz_parser == ' ' && *psz_parser == ':' )
255             {
256                 psz_parser++;
257             }
258             if( ( psz_next = strchr( psz_parser , ':'  ) ) )
259             {
260                 *psz_next++ = '\0';
261             }
262             if( *psz_parser =='\0' )
263             {
264                 break;
265             }
266
267             /* Create a VLC object */
268             p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
269                                           "audio filter" );
270             if( p_filter == NULL )
271             {
272                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
273                          psz_parser );
274                 psz_parser = psz_next;
275                 continue;
276             }
277
278             p_filter->p_owner = malloc( sizeof(*p_filter->p_owner) );
279             p_filter->p_owner->p_aout  = p_aout;
280             p_filter->p_owner->p_input = p_input;
281
282             /* request format */
283             memcpy( &p_filter->fmt_in.audio, &chain_output_format,
284                     sizeof(audio_sample_format_t) );
285             p_filter->fmt_in.i_codec = chain_output_format.i_format;
286             memcpy( &p_filter->fmt_out.audio, &chain_output_format,
287                     sizeof(audio_sample_format_t) );
288             p_filter->fmt_out.i_codec = chain_output_format.i_format;
289             p_filter->pf_audio_buffer_new = aout_FilterBufferNew;
290
291             /* try to find the requested filter */
292             if( i_visual == 2 ) /* this can only be a visualization module */
293             {
294                 p_filter->p_module = module_need( p_filter, "visualization2",
295                                                   psz_parser, true );
296             }
297             else /* this can be a audio filter module as well as a visualization module */
298             {
299                 p_filter->p_module = module_need( p_filter, "audio filter",
300                                               psz_parser, true );
301
302                 if ( p_filter->p_module == NULL )
303                 {
304                     /* if the filter requested a special format, retry */
305                     if ( !( AOUT_FMTS_IDENTICAL( &p_filter->fmt_in.audio,
306                                                  &chain_input_format )
307                             && AOUT_FMTS_IDENTICAL( &p_filter->fmt_out.audio,
308                                                     &chain_output_format ) ) )
309                     {
310                         aout_FormatPrepare( &p_filter->fmt_in.audio );
311                         aout_FormatPrepare( &p_filter->fmt_out.audio );
312                         p_filter->p_module = module_need( p_filter,
313                                                           "audio filter",
314                                                           psz_parser, true );
315                     }
316                     /* try visual filters */
317                     else
318                     {
319                         memcpy( &p_filter->fmt_in.audio, &chain_output_format,
320                                 sizeof(audio_sample_format_t) );
321                         memcpy( &p_filter->fmt_out.audio, &chain_output_format,
322                                 sizeof(audio_sample_format_t) );
323                         p_filter->p_module = module_need( p_filter,
324                                                           "visualization2",
325                                                           psz_parser, true );
326                     }
327                 }
328             }
329
330             /* failure */
331             if ( p_filter->p_module == NULL )
332             {
333                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
334                          psz_parser );
335
336                 free( p_filter->p_owner );
337                 vlc_object_release( p_filter );
338
339                 psz_parser = psz_next;
340                 continue;
341             }
342
343             /* complete the filter chain if necessary */
344             if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
345                                              &p_input->i_nb_filters,
346                                              &chain_input_format,
347                                              &p_filter->fmt_in.audio ) < 0 )
348             {
349                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
350                          psz_parser );
351
352                 module_unneed( p_filter, p_filter->p_module );
353                 free( p_filter->p_owner );
354                 vlc_object_release( p_filter );
355
356                 psz_parser = psz_next;
357                 continue;
358             }
359
360             /* success */
361             p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
362             memcpy( &chain_input_format, &p_filter->fmt_out.audio,
363                     sizeof( audio_sample_format_t ) );
364
365             if( i_visual == 0 ) /* scaletempo */
366                 p_input->p_playback_rate_filter = p_filter;
367
368             /* next filter if any */
369             psz_parser = psz_next;
370         }
371     }
372     free( psz_visual );
373     free( psz_filters );
374     free( psz_scaletempo );
375
376     /* complete the filter chain if necessary */
377     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
378                                      &p_input->i_nb_filters,
379                                      &chain_input_format,
380                                      &chain_output_format ) < 0 )
381     {
382         inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
383         return -1;
384     }
385
386     /* Create resamplers. */
387     if (AOUT_FMT_LINEAR(&owner->mixer_format))
388     {
389         chain_output_format.i_rate = (__MAX(p_input->samplerate,
390                                             owner->mixer_format.i_rate)
391                                  * (100 + AOUT_MAX_RESAMPLING)) / 100;
392         if ( chain_output_format.i_rate == owner->mixer_format.i_rate )
393         {
394             /* Just in case... */
395             chain_output_format.i_rate++;
396         }
397         if (aout_FiltersCreatePipeline (p_aout, p_input->pp_resamplers,
398                                         &p_input->i_nb_resamplers,
399                                         &chain_output_format,
400                                         &owner->mixer_format) < 0)
401         {
402             inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
403             return -1;
404         }
405
406         /* Setup the initial rate of the resampler */
407         p_input->pp_resamplers[0]->fmt_in.audio.i_rate = p_input->samplerate;
408     }
409     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
410
411     if( ! p_input->p_playback_rate_filter && p_input->i_nb_resamplers > 0 )
412     {
413         p_input->p_playback_rate_filter = p_input->pp_resamplers[0];
414     }
415
416     ReplayGainSelect( p_aout, p_input );
417
418     /* Success */
419     p_input->b_error = false;
420     p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
421
422     return 0;
423 }
424
425 /*****************************************************************************
426  * aout_InputDelete : delete an input
427  *****************************************************************************
428  * This function must be entered with the mixer lock.
429  *****************************************************************************/
430 int aout_InputDelete( audio_output_t * p_aout, aout_input_t * p_input )
431 {
432     aout_assert_locked( p_aout );
433     if ( p_input->b_error )
434         return 0;
435
436     /* XXX We need to update b_recycle_vout before calling aout_FiltersDestroyPipeline.
437      * FIXME They can be a race condition if audio-visual is updated between
438      * aout_InputDelete and aout_InputNew.
439      */
440     char *psz_visual = var_GetString( p_aout, "audio-visual");
441     p_input->b_recycle_vout = psz_visual && *psz_visual;
442     free( psz_visual );
443
444     aout_FiltersDestroyPipeline( p_input->pp_filters, p_input->i_nb_filters );
445     p_input->i_nb_filters = 0;
446     aout_FiltersDestroyPipeline( p_input->pp_resamplers,
447                                  p_input->i_nb_resamplers );
448     p_input->i_nb_resamplers = 0;
449
450     return 0;
451 }
452
453 /*****************************************************************************
454  * aout_InputCheckAndRestart : restart an input
455  *****************************************************************************
456  * This function must be entered with the input and mixer lock.
457  *****************************************************************************/
458 void aout_InputCheckAndRestart( audio_output_t * p_aout, aout_input_t * p_input )
459 {
460     aout_owner_t *owner = aout_owner(p_aout);
461     aout_assert_locked( p_aout );
462
463     if( !p_input->b_restart )
464         return;
465
466     aout_InputDelete( p_aout, p_input );
467     aout_InputNew( p_aout, &owner->input_format, p_input, &p_input->request_vout );
468
469     p_input->b_restart = false;
470 }
471 /*****************************************************************************
472  * aout_InputPlay : play a buffer
473  *****************************************************************************
474  * This function must be entered with the input lock.
475  *****************************************************************************/
476 /* XXX Do not activate it !! */
477 //#define AOUT_PROCESS_BEFORE_CHEKS
478 block_t *aout_InputPlay(audio_output_t *p_aout, aout_input_t *p_input,
479                         block_t *p_buffer, int i_input_rate, date_t *date )
480 {
481     mtime_t start_date;
482
483     aout_assert_locked( p_aout );
484
485     if( i_input_rate != INPUT_RATE_DEFAULT && p_input->p_playback_rate_filter == NULL )
486     {
487         inputDrop( p_input, p_buffer );
488         return NULL;
489     }
490
491 #ifdef AOUT_PROCESS_BEFORE_CHEKS
492     /* Run pre-filters. */
493     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
494                       &p_buffer );
495     if( !p_buffer )
496         return NULL;
497
498     /* Actually run the resampler now. */
499     if ( p_input->i_nb_resamplers > 0 )
500     {
501         const mtime_t i_date = p_buffer->i_pts;
502         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
503                           p_input->i_nb_resamplers,
504                           &p_buffer );
505     }
506
507     if( !p_buffer )
508         return NULL;
509     if( p_buffer->i_nb_samples <= 0 )
510     {
511         block_Release( p_buffer );
512         return NULL;
513     }
514 #endif
515
516     /* Handle input rate change, but keep drift correction */
517     if( i_input_rate != p_input->i_last_input_rate )
518     {
519         unsigned int * const pi_rate = &p_input->p_playback_rate_filter->fmt_in.audio.i_rate;
520 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
521         const int i_delta = *pi_rate - F(p_input->samplerate,p_input->i_last_input_rate);
522         *pi_rate = F(p_input->samplerate + i_delta, i_input_rate);
523 #undef F
524         p_input->i_last_input_rate = i_input_rate;
525     }
526
527     mtime_t now = mdate();
528
529     /* We don't care if someone changes the start date behind our back after
530      * this. We'll deal with that when pushing the buffer, and compensate
531      * with the next incoming buffer. */
532     start_date = date_Get (date);
533
534     if ( start_date != VLC_TS_INVALID && start_date < now )
535     {
536         /* The decoder is _very_ late. This can only happen if the user
537          * pauses the stream (or if the decoder is buggy, which cannot
538          * happen :). */
539         msg_Warn( p_aout, "computed PTS is out of range (%"PRId64"), "
540                   "clearing out", now - start_date );
541         aout_OutputFlush( p_aout, false );
542         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
543             msg_Warn( p_aout, "timing screwed, stopping resampling" );
544         inputResamplingStop( p_input );
545         p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
546         start_date = VLC_TS_INVALID;
547     }
548
549     if ( p_buffer->i_pts < now + AOUT_MIN_PREPARE_TIME )
550     {
551         /* The decoder gives us f*cked up PTS. It's its business, but we
552          * can't present it anyway, so drop the buffer. */
553         msg_Warn( p_aout, "PTS is out of range (%"PRId64"), dropping buffer",
554                   now - p_buffer->i_pts );
555         inputDrop( p_input, p_buffer );
556         inputResamplingStop( p_input );
557         return NULL;
558     }
559
560     /* If the audio drift is too big then it's not worth trying to resample
561      * the audio. */
562     if( start_date == VLC_TS_INVALID )
563     {
564         start_date = p_buffer->i_pts;
565         date_Set (date, start_date);
566     }
567
568     mtime_t drift = start_date - p_buffer->i_pts;
569
570     if( drift < -i_input_rate * 3 * AOUT_MAX_PTS_ADVANCE / INPUT_RATE_DEFAULT )
571     {
572         msg_Warn( p_aout, "buffer way too early (%"PRId64"), clearing queue",
573                   drift );
574         aout_OutputFlush( p_aout, false );
575         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
576             msg_Warn( p_aout, "timing screwed, stopping resampling" );
577         inputResamplingStop( p_input );
578         p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
579         start_date = p_buffer->i_pts;
580         date_Set (date, start_date);
581         drift = 0;
582     }
583     else
584     if( drift > +i_input_rate * 3 * AOUT_MAX_PTS_DELAY / INPUT_RATE_DEFAULT )
585     {
586         msg_Warn( p_aout, "buffer way too late (%"PRId64"), dropping buffer",
587                   drift );
588         inputDrop( p_input, p_buffer );
589         return NULL;
590     }
591
592 #ifndef AOUT_PROCESS_BEFORE_CHEKS
593     /* Run pre-filters. */
594     aout_FiltersPlay( p_input->pp_filters, p_input->i_nb_filters, &p_buffer );
595     if( !p_buffer )
596         return NULL;
597 #endif
598
599     /* Run the resampler if needed.
600      * We first need to calculate the output rate of this resampler. */
601     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
602          ( drift < -AOUT_MAX_PTS_ADVANCE || drift > +AOUT_MAX_PTS_DELAY ) &&
603          p_input->i_nb_resamplers > 0 )
604     {
605         /* Can happen in several circumstances :
606          * 1. A problem at the input (clock drift)
607          * 2. A small pause triggered by the user
608          * 3. Some delay in the output stage, causing a loss of lip
609          *    synchronization
610          * Solution : resample the buffer to avoid a scratch.
611          */
612         p_input->i_resamp_start_date = now;
613         p_input->i_resamp_start_drift = (int)-drift;
614         p_input->i_resampling_type = (drift < 0) ? AOUT_RESAMPLING_DOWN
615                                                  : AOUT_RESAMPLING_UP;
616         msg_Warn( p_aout, (drift < 0)
617                   ? "buffer too early (%"PRId64"), down-sampling"
618                   : "buffer too late  (%"PRId64"), up-sampling", drift );
619     }
620
621     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
622     {
623         /* Resampling has been triggered previously (because of dates
624          * mismatch). We want the resampling to happen progressively so
625          * it isn't too audible to the listener. */
626
627         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
628             p_input->pp_resamplers[0]->fmt_in.audio.i_rate += 2; /* Hz */
629         else
630             p_input->pp_resamplers[0]->fmt_in.audio.i_rate -= 2; /* Hz */
631
632         /* Check if everything is back to normal, in which case we can stop the
633          * resampling */
634         unsigned int i_nominal_rate =
635           (p_input->pp_resamplers[0] == p_input->p_playback_rate_filter)
636           ? INPUT_RATE_DEFAULT * p_input->samplerate / i_input_rate
637           : p_input->samplerate;
638         if( p_input->pp_resamplers[0]->fmt_in.audio.i_rate == i_nominal_rate )
639         {
640             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
641             msg_Warn( p_aout, "resampling stopped after %"PRIi64" usec "
642                       "(drift: %"PRIi64")",
643                       now - p_input->i_resamp_start_date,
644                       p_buffer->i_pts - start_date);
645         }
646         else if( abs( (int)(p_buffer->i_pts - start_date) ) <
647                  abs( p_input->i_resamp_start_drift ) / 2 )
648         {
649             /* if we reduced the drift from half, then it is time to switch
650              * back the resampling direction. */
651             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
652                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
653             else
654                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
655             p_input->i_resamp_start_drift = 0;
656         }
657         else if( p_input->i_resamp_start_drift &&
658                  ( abs( (int)(p_buffer->i_pts - start_date) ) >
659                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
660         {
661             /* If the drift is increasing and not decreasing, than something
662              * is bad. We'd better stop the resampling right now. */
663             msg_Warn( p_aout, "timing screwed, stopping resampling" );
664             inputResamplingStop( p_input );
665             p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
666         }
667     }
668
669 #ifndef AOUT_PROCESS_BEFORE_CHEKS
670     /* Actually run the resampler now. */
671     if ( p_input->i_nb_resamplers > 0 )
672     {
673         aout_FiltersPlay( p_input->pp_resamplers, p_input->i_nb_resamplers,
674                           &p_buffer );
675     }
676
677     if( !p_buffer )
678         return NULL;
679     if( p_buffer->i_nb_samples <= 0 )
680     {
681         block_Release( p_buffer );
682         return NULL;
683     }
684 #endif
685
686     p_buffer->i_pts = start_date;
687     return p_buffer;
688 }
689
690 /*****************************************************************************
691  * static functions
692  *****************************************************************************/
693
694 static void inputFailure( audio_output_t * p_aout, aout_input_t * p_input,
695                           const char * psz_error_message )
696 {
697     /* error message */
698     msg_Err( p_aout, "%s", psz_error_message );
699
700     /* clean up */
701     aout_FiltersDestroyPipeline( p_input->pp_filters, p_input->i_nb_filters );
702     aout_FiltersDestroyPipeline( p_input->pp_resamplers,
703                                  p_input->i_nb_resamplers );
704     var_Destroy( p_aout, "visual" );
705     var_Destroy( p_aout, "equalizer" );
706     var_Destroy( p_aout, "audio-filter" );
707     var_Destroy( p_aout, "audio-visual" );
708
709     var_Destroy( p_aout, "audio-replay-gain-mode" );
710     var_Destroy( p_aout, "audio-replay-gain-default" );
711     var_Destroy( p_aout, "audio-replay-gain-preamp" );
712     var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
713
714     /* error flag */
715     p_input->b_error = 1;
716 }
717
718 static void inputDrop( aout_input_t *p_input, aout_buffer_t *p_buffer )
719 {
720     aout_BufferFree( p_buffer );
721
722     p_input->i_buffer_lost++;
723 }
724
725 static void inputResamplingStop( aout_input_t *p_input )
726 {
727     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
728     if( p_input->i_nb_resamplers != 0 )
729     {
730         p_input->pp_resamplers[0]->fmt_in.audio.i_rate =
731             ( p_input->pp_resamplers[0] == p_input->p_playback_rate_filter )
732             ? INPUT_RATE_DEFAULT * p_input->samplerate / p_input->i_last_input_rate
733             : p_input->samplerate;
734     }
735 }
736
737 static vout_thread_t *RequestVout( void *p_private,
738                                    vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
739 {
740     audio_output_t *p_aout = p_private;
741     VLC_UNUSED(b_recycle);
742     vout_configuration_t cfg = {
743         .vout       = p_vout,
744         .input      = NULL,
745         .change_fmt = true,
746         .fmt        = p_fmt,
747         .dpb_size   = 1,
748     };
749     return vout_Request( p_aout, &cfg );
750 }
751
752 vout_thread_t *aout_filter_RequestVout( filter_t *p_filter,
753                                         vout_thread_t *p_vout, video_format_t *p_fmt )
754 {
755     aout_input_t *p_input = p_filter->p_owner->p_input;
756     aout_request_vout_t *p_request = &p_input->request_vout;
757
758     /* XXX: this only works from audio input */
759     /* If you want to use visualization filters from another place, you will
760      * need to add a new pf_aout_request_vout callback or store a pointer
761      * to aout_request_vout_t inside filter_t (i.e. a level of indirection). */
762
763     return p_request->pf_request_vout( p_request->p_private,
764                                        p_vout, p_fmt, p_input->b_recycle_vout );
765 }
766
767 static inline bool ChangeFiltersString (vlc_object_t *aout, const char *var,
768                                         const char *filter, bool add)
769 {
770     return aout_ChangeFilterString (aout, aout, var, filter, add);
771 }
772
773 static int VisualizationCallback (vlc_object_t *obj, char const *var,
774                                   vlc_value_t oldval, vlc_value_t newval,
775                                   void *data)
776 {
777     const char *mode = newval.psz_string;
778     aout_input_t *input = data;
779
780     if (!*mode)
781     {
782         ChangeFiltersString (obj, "audio-visual", "goom", false);
783         ChangeFiltersString (obj, "audio-visual", "visual", false);
784         ChangeFiltersString (obj, "audio-visual", "projectm", false);
785     }
786     else if (!strcmp ("goom", mode))
787     {
788         ChangeFiltersString (obj, "audio-visual", "visual", false );
789         ChangeFiltersString (obj, "audio-visual", "goom", true );
790         ChangeFiltersString (obj, "audio-visual", "projectm", false );
791     }
792     else if (!strcmp ("projectm", mode))
793     {
794         ChangeFiltersString (obj, "audio-visual", "visual", false);
795         ChangeFiltersString (obj, "audio-visual", "goom", false);
796         ChangeFiltersString (obj, "audio-visual", "projectm", true);
797     }
798     else
799     {
800         var_Create (obj, "effect-list", VLC_VAR_STRING);
801         var_SetString (obj, "effect-list", mode);
802
803         ChangeFiltersString (obj, "audio-visual", "goom", false);
804         ChangeFiltersString (obj, "audio-visual", "visual", true);
805         ChangeFiltersString (obj, "audio-visual", "projectm", false);
806     }
807
808     /* That sucks FIXME: use "input" instead of cast */
809     AoutInputsMarkToRestart ((audio_output_t *)obj);
810
811     (void) var; (void) oldval;
812     return VLC_SUCCESS;
813 }
814
815 static int EqualizerCallback (vlc_object_t *obj, char const *cmd,
816                               vlc_value_t oldval, vlc_value_t newval,
817                               void *data)
818 {
819     char *mode = newval.psz_string;
820     aout_input_t *input = data;
821     bool ret;
822
823     (void) cmd; (void) oldval;
824     if (!*mode)
825         ret = ChangeFiltersString (obj, "audio-filter", "equalizer", false);
826     else
827     {
828         var_Create (obj, "equalizer-preset", VLC_VAR_STRING);
829         var_SetString (obj, "equalizer-preset", mode);
830         ret = ChangeFiltersString (obj, "audio-filter", "equalizer", true);
831     }
832
833     /* That sucks */
834     if (ret)
835         AoutInputsMarkToRestart ((audio_output_t *)obj);
836     return VLC_SUCCESS;
837 }
838
839 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
840                                vlc_value_t oldval, vlc_value_t newval, void *p_data )
841 {
842     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
843     VLC_UNUSED(newval); VLC_UNUSED(p_data);
844     audio_output_t *aout = (audio_output_t *)p_this;
845     aout_owner_t *owner = aout_owner (aout);
846
847     aout_lock (aout);
848     if (owner->input != NULL)
849         ReplayGainSelect (aout, owner->input);
850     aout_unlock (aout);
851
852     return VLC_SUCCESS;
853 }
854
855 static void ReplayGainSelect( audio_output_t *p_aout, aout_input_t *p_input )
856 {
857     char *psz_replay_gain = var_GetNonEmptyString( p_aout,
858                                                    "audio-replay-gain-mode" );
859     int i_mode;
860     int i_use;
861     float f_gain;
862
863     p_input->multiplier = 1.0;
864
865     if( !psz_replay_gain )
866         return;
867
868     /* Find select mode */
869     if( !strcmp( psz_replay_gain, "track" ) )
870         i_mode = AUDIO_REPLAY_GAIN_TRACK;
871     else if( !strcmp( psz_replay_gain, "album" ) )
872         i_mode = AUDIO_REPLAY_GAIN_ALBUM;
873     else
874         i_mode = AUDIO_REPLAY_GAIN_MAX;
875
876     /* If the select mode is not available, prefer the other one */
877     i_use = i_mode;
878     if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
879     {
880         for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
881         {
882             if( p_input->replay_gain.pb_gain[i_use] )
883                 break;
884         }
885     }
886
887     /* */
888     if( i_use != AUDIO_REPLAY_GAIN_MAX )
889         f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
890     else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
891         f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
892     else
893         f_gain = 0.0;
894     p_input->multiplier = pow( 10.0, f_gain / 20.0 );
895
896     /* */
897     if( p_input->replay_gain.pb_peak[i_use] &&
898         var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
899         p_input->replay_gain.pf_peak[i_use] * p_input->multiplier > 1.0 )
900     {
901         p_input->multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
902     }
903
904     free( psz_replay_gain );
905 }