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