1 /*****************************************************************************
2 * input.c : internal management of input streams for the audio output
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
38 #include <vlc_input.h> /* for input_thread_t and i_pts_delay */
45 #include "aout_internal.h"
47 /** FIXME: Ugly but needed to access the counters */
48 #include "input/input_internal.h"
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 );
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 )
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;
72 aout_FormatPrint( p_aout, "input", &p_input->input );
74 p_input->i_nb_resamplers = p_input->i_nb_filters = 0;
77 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
78 p_input->p_first_byte_to_mix = NULL;
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 );
88 /* Now add user filters */
89 if( var_Type( p_aout, "visual" ) == 0 )
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 );
105 /* Look for goom plugin */
106 if( module_Exists( VLC_OBJECT(p_aout), "goom" ) )
108 val.psz_string = (char*)"goom"; text.psz_string = (char*)"Goom";
109 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
112 /* Look for galaktos plugin */
113 if( module_Exists( VLC_OBJECT(p_aout), "galaktos" ) )
115 val.psz_string = (char*)"galaktos"; text.psz_string = (char*)"GaLaktos";
116 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
119 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
121 var_Set( p_aout, "visual", val );
122 free( val.psz_string );
124 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
127 if( var_Type( p_aout, "equalizer" ) == 0 )
129 module_config_t *p_config;
132 p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
133 if( p_config && p_config->i_list )
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 );
140 val.psz_string = (char*)""; text.psz_string = _("Disable");
141 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text );
143 for( i = 0; i < p_config->i_list; i++ )
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,
151 var_AddCallback( p_aout, "equalizer", EqualizerCallback, NULL );
155 if( var_Type( p_aout, "audio-filter" ) == 0 )
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 );
162 if( var_Type( p_aout, "audio-visual" ) == 0 )
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 );
170 if( var_Type( p_aout, "audio-replay-gain-mode" ) == 0 )
172 module_config_t *p_config;
175 p_config = config_FindConfig( VLC_OBJECT(p_aout), "audio-replay-gain-mode" );
176 if( p_config && p_config->i_list )
178 var_Create( p_aout, "audio-replay-gain-mode",
179 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
181 text.psz_string = _("Replay gain");
182 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL );
184 for( i = 0; i < p_config->i_list; i++ )
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,
192 var_AddCallback( p_aout, "audio-replay-gain-mode", ReplayGainCallback, NULL );
195 if( var_Type( p_aout, "audio-replay-gain-preamp" ) == 0 )
197 var_Create( p_aout, "audio-replay-gain-preamp",
198 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
200 if( var_Type( p_aout, "audio-replay-gain-default" ) == 0 )
202 var_Create( p_aout, "audio-replay-gain-default",
203 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
205 if( var_Type( p_aout, "audio-replay-gain-peak-protection" ) == 0 )
207 var_Create( p_aout, "audio-replay-gain-peak-protection",
208 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
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;
216 /* parse user filter lists */
217 for( i_visual = 0; i_visual < 2; i_visual++ )
219 char *psz_next = NULL;
220 char *psz_parser = i_visual ? psz_visual : psz_filters;
222 if( psz_parser == NULL || !*psz_parser )
225 while( psz_parser && *psz_parser )
227 aout_filter_t * p_filter = NULL;
229 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
231 msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
235 while( *psz_parser == ' ' && *psz_parser == ':' )
239 if( ( psz_next = strchr( psz_parser , ':' ) ) )
243 if( *psz_parser =='\0' )
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 )
254 msg_Err( p_aout, "cannot add user filter %s (skipped)",
256 psz_parser = psz_next;
260 vlc_object_attach( p_filter , p_aout );
262 /* try to find the requested filter */
263 if( i_visual == 1 ) /* this can only be a visualization module */
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) );
271 p_filter->p_module = module_Need( p_filter, "visualization",
274 else /* this can be a audio filter module as well as a visualization module */
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) );
282 p_filter->p_module = module_Need( p_filter, "audio filter",
285 if ( p_filter->p_module == NULL )
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 ) ) )
293 aout_FormatPrepare( &p_filter->input );
294 aout_FormatPrepare( &p_filter->output );
295 p_filter->p_module = module_Need( p_filter,
299 /* try visual filters */
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,
314 if ( p_filter->p_module == NULL )
316 msg_Err( p_aout, "cannot add user filter %s (skipped)",
319 vlc_object_detach( p_filter );
320 vlc_object_release( p_filter );
322 psz_parser = psz_next;
326 /* complete the filter chain if necessary */
327 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &p_filter->input ) )
329 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
330 &p_input->i_nb_filters,
332 &p_filter->input ) < 0 )
334 msg_Err( p_aout, "cannot add user filter %s (skipped)",
337 module_Unneed( p_filter, p_filter->p_module );
338 vlc_object_detach( p_filter );
339 vlc_object_release( p_filter );
341 psz_parser = psz_next;
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 ) );
352 /* next filter if any */
353 psz_parser = psz_next;
359 /* complete the filter chain if necessary */
360 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &chain_output_format ) )
362 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
363 &p_input->i_nb_filters,
365 &chain_output_format ) < 0 )
367 inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
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;
376 /* Create resamplers. */
377 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
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 )
384 /* Just in case... */
385 chain_output_format.i_rate++;
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 )
392 inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
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;
401 /* Setup the initial rate of the resampler */
402 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
404 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
406 p_input->p_playback_rate_filter = NULL;
407 for( int i = 0; i < p_input->i_nb_filters; i++ )
409 aout_filter_t *p_filter = p_input->pp_filters[i];
410 if( strcmp( "scaletempo", p_filter->psz_object_name ) == 0 )
412 p_input->p_playback_rate_filter = p_filter;
416 if( ! p_input->p_playback_rate_filter && p_input->i_nb_resamplers > 0 )
418 p_input->p_playback_rate_filter = p_input->pp_resamplers[0];
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;
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) );
433 ReplayGainSelect( p_aout, p_input );
436 p_input->b_error = false;
437 p_input->b_restart = false;
438 p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
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 )
450 if ( p_input->b_error ) return 0;
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 );
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 )
475 if( p_input->b_restart )
477 aout_fifo_t fifo, dummy_fifo;
478 uint8_t *p_first_byte_to_mix;
480 vlc_mutex_lock( &p_aout->mixer_lock );
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;
492 vlc_mutex_unlock( &p_aout->mixer_lock );
495 if( i_input_rate != INPUT_RATE_DEFAULT && p_input->p_playback_rate_filter == NULL )
497 inputDrop( p_aout, p_input, p_buffer );
501 #ifdef AOUT_PROCESS_BEFORE_CHEKS
502 /* Run pre-filters. */
503 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
506 /* Actually run the resampler now. */
507 if ( p_input->i_nb_resamplers > 0 )
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,
515 if( p_buffer->i_nb_samples <= 0 )
517 aout_BufferFree( p_buffer );
522 /* Handle input rate change, but keep drift correction */
523 if( i_input_rate != p_input->i_last_input_rate )
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);
530 p_input->i_last_input_rate = i_input_rate;
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 );
540 if ( start_date != 0 && start_date < mdate() )
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
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 );
557 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
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 );
564 inputDrop( p_aout, p_input, p_buffer );
565 inputResamplingStop( p_input );
569 /* If the audio drift is too big then it's not worth trying to resample
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 ) )
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 );
586 else if ( start_date != 0 &&
587 ( start_date > p_buffer->start_date + i_pts_tolerance) )
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 );
595 if ( start_date == 0 ) start_date = p_buffer->start_date;
597 #ifndef AOUT_PROCESS_BEFORE_CHEKS
598 /* Run pre-filters. */
599 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
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 )
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
615 * Solution : resample the buffer to avoid a scratch.
617 mtime_t drift = p_buffer->start_date - start_date;
619 p_input->i_resamp_start_date = mdate();
620 p_input->i_resamp_start_drift = (int)drift;
623 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
625 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
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");
633 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
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. */
639 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
641 p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
645 p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
648 /* Check if everything is back to normal, in which case we can stop the
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 )
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);
662 else if( abs( (int)(p_buffer->start_date - start_date) ) <
663 abs( p_input->i_resamp_start_drift ) / 2 )
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;
670 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
671 p_input->i_resamp_start_drift = 0;
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 ) )
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 );
684 #ifndef AOUT_PROCESS_BEFORE_CHEKS
685 /* Actually run the resampler now. */
686 if ( p_input->i_nb_resamplers > 0 )
688 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
689 p_input->i_nb_resamplers,
693 if( p_buffer->i_nb_samples <= 0 )
695 aout_BufferFree( p_buffer );
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;
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 );
711 /*****************************************************************************
713 *****************************************************************************/
715 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
716 const char * psz_error_message )
719 msg_Err( p_aout, "%s", psz_error_message );
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" );
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" );
738 p_input->b_error = 1;
741 static void inputDrop( aout_instance_t *p_aout, aout_input_t *p_input, aout_buffer_t *p_buffer )
743 aout_BufferFree( p_buffer );
745 if( !p_input->p_input_thread )
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);
753 static void inputResamplingStop( aout_input_t *p_input )
755 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
756 if( p_input->i_nb_resamplers != 0 )
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;
766 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
767 const char *psz_name, bool b_add )
769 return AoutChangeFilterString( VLC_OBJECT(p_aout), p_aout,
770 psz_variable, psz_name, b_add ) ? 1 : 0;
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 )
776 aout_instance_t *p_aout = (aout_instance_t *)p_this;
777 char *psz_mode = newval.psz_string;
779 (void)psz_cmd; (void)oldval; (void)p_data;
781 if( !psz_mode || !*psz_mode )
783 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
784 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
785 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false );
789 if( !strcmp( "goom", psz_mode ) )
791 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
792 ChangeFiltersString( p_aout, "audio-visual", "goom", true );
793 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
795 else if( !strcmp( "galaktos", psz_mode ) )
797 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
798 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
799 ChangeFiltersString( p_aout, "audio-visual", "galaktos", true );
803 val.psz_string = psz_mode;
804 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
805 var_Set( p_aout, "effect-list", val );
807 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
808 ChangeFiltersString( p_aout, "audio-visual", "visual", true );
809 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
814 AoutInputsMarkToRestart( p_aout );
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 )
822 aout_instance_t *p_aout = (aout_instance_t *)p_this;
823 char *psz_mode = newval.psz_string;
826 (void)psz_cmd; (void)oldval; (void)p_data;
828 if( !psz_mode || !*psz_mode )
830 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
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",
845 AoutInputsMarkToRestart( p_aout );
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 )
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;
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] );
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 );
868 static void ReplayGainSelect( aout_instance_t *p_aout, aout_input_t *p_input )
870 char *psz_replay_gain = var_GetNonEmptyString( p_aout,
871 "audio-replay-gain-mode" );
876 p_input->f_multiplier = 1.0;
878 if( !psz_replay_gain )
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;
887 i_mode = AUDIO_REPLAY_GAIN_MAX;
889 /* If the select mode is not available, prefer the other one */
891 if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
893 for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
895 if( p_input->replay_gain.pb_gain[i_use] )
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" );
907 p_input->f_multiplier = pow( 10.0, f_gain / 20.0 );
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 )
914 p_input->f_multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
917 free( psz_replay_gain );