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 p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
250 if( p_filter == NULL )
252 msg_Err( p_aout, "cannot add user filter %s (skipped)",
254 psz_parser = psz_next;
258 vlc_object_attach( p_filter , p_aout );
260 /* try to find the requested filter */
261 if( i_visual == 1 ) /* this can only be a visualization module */
264 memcpy( &p_filter->input, &chain_output_format,
265 sizeof(audio_sample_format_t) );
266 memcpy( &p_filter->output, &chain_output_format,
267 sizeof(audio_sample_format_t) );
269 p_filter->p_module = module_Need( p_filter, "visualization",
272 else /* this can be a audio filter module as well as a visualization module */
275 memcpy( &p_filter->input, &chain_input_format,
276 sizeof(audio_sample_format_t) );
277 memcpy( &p_filter->output, &chain_output_format,
278 sizeof(audio_sample_format_t) );
280 p_filter->p_module = module_Need( p_filter, "audio filter",
283 if ( p_filter->p_module == NULL )
285 /* if the filter requested a special format, retry */
286 if ( !( AOUT_FMTS_IDENTICAL( &p_filter->input,
287 &chain_input_format )
288 && AOUT_FMTS_IDENTICAL( &p_filter->output,
289 &chain_output_format ) ) )
291 aout_FormatPrepare( &p_filter->input );
292 aout_FormatPrepare( &p_filter->output );
293 p_filter->p_module = module_Need( p_filter,
297 /* try visual filters */
300 memcpy( &p_filter->input, &chain_output_format,
301 sizeof(audio_sample_format_t) );
302 memcpy( &p_filter->output, &chain_output_format,
303 sizeof(audio_sample_format_t) );
304 p_filter->p_module = module_Need( p_filter,
312 if ( p_filter->p_module == NULL )
314 msg_Err( p_aout, "cannot add user filter %s (skipped)",
317 vlc_object_detach( p_filter );
318 vlc_object_release( p_filter );
320 psz_parser = psz_next;
324 /* complete the filter chain if necessary */
325 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &p_filter->input ) )
327 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
328 &p_input->i_nb_filters,
330 &p_filter->input ) < 0 )
332 msg_Err( p_aout, "cannot add user filter %s (skipped)",
335 module_Unneed( p_filter, p_filter->p_module );
336 vlc_object_detach( p_filter );
337 vlc_object_release( p_filter );
339 psz_parser = psz_next;
345 p_filter->b_continuity = false;
346 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
347 memcpy( &chain_input_format, &p_filter->output,
348 sizeof( audio_sample_format_t ) );
350 /* next filter if any */
351 psz_parser = psz_next;
357 /* complete the filter chain if necessary */
358 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &chain_output_format ) )
360 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
361 &p_input->i_nb_filters,
363 &chain_output_format ) < 0 )
365 inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
370 /* Prepare hints for the buffer allocator. */
371 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
372 p_input->input_alloc.i_bytes_per_sec = -1;
374 /* Create resamplers. */
375 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
377 chain_output_format.i_rate = (__MAX(p_input->input.i_rate,
378 p_aout->mixer.mixer.i_rate)
379 * (100 + AOUT_MAX_RESAMPLING)) / 100;
380 if ( chain_output_format.i_rate == p_aout->mixer.mixer.i_rate )
382 /* Just in case... */
383 chain_output_format.i_rate++;
385 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
386 &p_input->i_nb_resamplers,
387 &chain_output_format,
388 &p_aout->mixer.mixer ) < 0 )
390 inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
394 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
395 p_input->i_nb_resamplers,
396 &p_input->input_alloc );
397 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
399 /* Setup the initial rate of the resampler */
400 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
402 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
404 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
405 p_input->i_nb_filters,
406 &p_input->input_alloc );
407 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
409 /* i_bytes_per_sec is still == -1 if no filters */
410 p_input->input_alloc.i_bytes_per_sec = __MAX(
411 p_input->input_alloc.i_bytes_per_sec,
412 (int)(p_input->input.i_bytes_per_frame
413 * p_input->input.i_rate
414 / p_input->input.i_frame_length) );
416 ReplayGainSelect( p_aout, p_input );
419 p_input->b_error = false;
420 p_input->b_restart = false;
421 p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
426 /*****************************************************************************
427 * aout_InputDelete : delete an input
428 *****************************************************************************
429 * This function must be entered with the mixer lock.
430 *****************************************************************************/
431 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
433 if ( p_input->b_error ) return 0;
435 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
436 p_input->i_nb_filters );
437 p_input->i_nb_filters = 0;
438 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
439 p_input->i_nb_resamplers );
440 p_input->i_nb_resamplers = 0;
441 aout_FifoDestroy( p_aout, &p_input->fifo );
446 /*****************************************************************************
447 * aout_InputPlay : play a buffer
448 *****************************************************************************
449 * This function must be entered with the input lock.
450 *****************************************************************************/
451 /* XXX Do not activate it !! */
452 //#define AOUT_PROCESS_BEFORE_CHEKS
453 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
454 aout_buffer_t * p_buffer, int i_input_rate )
458 if( p_input->b_restart )
460 aout_fifo_t fifo, dummy_fifo;
461 uint8_t *p_first_byte_to_mix;
463 vlc_mutex_lock( &p_aout->mixer_lock );
465 /* A little trick to avoid loosing our input fifo */
466 aout_FifoInit( p_aout, &dummy_fifo, p_aout->mixer.mixer.i_rate );
467 p_first_byte_to_mix = p_input->p_first_byte_to_mix;
468 fifo = p_input->fifo;
469 p_input->fifo = dummy_fifo;
470 aout_InputDelete( p_aout, p_input );
471 aout_InputNew( p_aout, p_input );
472 p_input->p_first_byte_to_mix = p_first_byte_to_mix;
473 p_input->fifo = fifo;
475 vlc_mutex_unlock( &p_aout->mixer_lock );
478 if( i_input_rate != INPUT_RATE_DEFAULT && p_input->i_nb_resamplers <= 0 )
480 inputDrop( p_aout, p_input, p_buffer );
484 #ifdef AOUT_PROCESS_BEFORE_CHEKS
485 /* Run pre-filters. */
486 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
489 /* Actually run the resampler now. */
490 if ( p_input->i_nb_resamplers > 0 )
492 const mtime_t i_date = p_buffer->start_date;
493 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
494 p_input->i_nb_resamplers,
498 if( p_buffer->i_nb_samples <= 0 )
500 aout_BufferFree( p_buffer );
505 /* Handle input rate change by modifying resampler input rate */
506 if( i_input_rate != p_input->i_last_input_rate )
508 unsigned int * const pi_rate = &p_input->pp_resamplers[0]->input.i_rate;
509 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
510 const int i_delta = *pi_rate - F(p_input->input.i_rate,p_input->i_last_input_rate);
511 *pi_rate = F(p_input->input.i_rate + i_delta, i_input_rate);
513 p_input->i_last_input_rate = i_input_rate;
516 /* We don't care if someone changes the start date behind our back after
517 * this. We'll deal with that when pushing the buffer, and compensate
518 * with the next incoming buffer. */
519 vlc_mutex_lock( &p_aout->input_fifos_lock );
520 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
521 vlc_mutex_unlock( &p_aout->input_fifos_lock );
523 if ( start_date != 0 && start_date < mdate() )
525 /* The decoder is _very_ late. This can only happen if the user
526 * pauses the stream (or if the decoder is buggy, which cannot
528 msg_Warn( p_aout, "computed PTS is out of range (%"PRId64"), "
529 "clearing out", mdate() - start_date );
530 vlc_mutex_lock( &p_aout->input_fifos_lock );
531 aout_FifoSet( p_aout, &p_input->fifo, 0 );
532 p_input->p_first_byte_to_mix = NULL;
533 vlc_mutex_unlock( &p_aout->input_fifos_lock );
534 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
535 msg_Warn( p_aout, "timing screwed, stopping resampling" );
536 inputResamplingStop( p_input );
540 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
542 /* The decoder gives us f*cked up PTS. It's its business, but we
543 * can't present it anyway, so drop the buffer. */
544 msg_Warn( p_aout, "PTS is out of range (%"PRId64"), dropping buffer",
545 mdate() - p_buffer->start_date );
547 inputDrop( p_aout, p_input, p_buffer );
548 inputResamplingStop( p_input );
552 /* If the audio drift is too big then it's not worth trying to resample
554 if ( start_date != 0 &&
555 ( start_date < p_buffer->start_date - 3 * AOUT_PTS_TOLERANCE ) )
557 msg_Warn( p_aout, "audio drift is too big (%"PRId64"), clearing out",
558 start_date - p_buffer->start_date );
559 vlc_mutex_lock( &p_aout->input_fifos_lock );
560 aout_FifoSet( p_aout, &p_input->fifo, 0 );
561 p_input->p_first_byte_to_mix = NULL;
562 vlc_mutex_unlock( &p_aout->input_fifos_lock );
563 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
564 msg_Warn( p_aout, "timing screwed, stopping resampling" );
565 inputResamplingStop( p_input );
568 else if ( start_date != 0 &&
569 ( start_date > p_buffer->start_date + 3 * AOUT_PTS_TOLERANCE ) )
571 msg_Warn( p_aout, "audio drift is too big (%"PRId64"), dropping buffer",
572 start_date - p_buffer->start_date );
573 inputDrop( p_aout, p_input, p_buffer );
577 if ( start_date == 0 ) start_date = p_buffer->start_date;
579 #ifndef AOUT_PROCESS_BEFORE_CHEKS
580 /* Run pre-filters. */
581 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
585 /* Run the resampler if needed.
586 * We first need to calculate the output rate of this resampler. */
587 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
588 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
589 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
590 p_input->i_nb_resamplers > 0 )
592 /* Can happen in several circumstances :
593 * 1. A problem at the input (clock drift)
594 * 2. A small pause triggered by the user
595 * 3. Some delay in the output stage, causing a loss of lip
597 * Solution : resample the buffer to avoid a scratch.
599 mtime_t drift = p_buffer->start_date - start_date;
601 p_input->i_resamp_start_date = mdate();
602 p_input->i_resamp_start_drift = (int)drift;
605 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
607 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
609 msg_Warn( p_aout, "buffer is %"PRId64" %s, triggering %ssampling",
610 drift > 0 ? drift : -drift,
611 drift > 0 ? "in advance" : "late",
612 drift > 0 ? "down" : "up");
615 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
617 /* Resampling has been triggered previously (because of dates
618 * mismatch). We want the resampling to happen progressively so
619 * it isn't too audible to the listener. */
621 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
623 p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
627 p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
630 /* Check if everything is back to normal, in which case we can stop the
632 if( p_input->pp_resamplers[0]->input.i_rate == 1000 * p_input->input.i_rate / i_input_rate )
634 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
635 msg_Warn( p_aout, "resampling stopped after %"PRIi64" usec "
636 "(drift: %"PRIi64")",
637 mdate() - p_input->i_resamp_start_date,
638 p_buffer->start_date - start_date);
640 else if( abs( (int)(p_buffer->start_date - start_date) ) <
641 abs( p_input->i_resamp_start_drift ) / 2 )
643 /* if we reduced the drift from half, then it is time to switch
644 * back the resampling direction. */
645 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
646 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
648 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
649 p_input->i_resamp_start_drift = 0;
651 else if( p_input->i_resamp_start_drift &&
652 ( abs( (int)(p_buffer->start_date - start_date) ) >
653 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
655 /* If the drift is increasing and not decreasing, than something
656 * is bad. We'd better stop the resampling right now. */
657 msg_Warn( p_aout, "timing screwed, stopping resampling" );
658 inputResamplingStop( p_input );
662 #ifndef AOUT_PROCESS_BEFORE_CHEKS
663 /* Actually run the resampler now. */
664 if ( p_input->i_nb_resamplers > 0 )
666 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
667 p_input->i_nb_resamplers,
671 if( p_buffer->i_nb_samples <= 0 )
673 aout_BufferFree( p_buffer );
678 /* Adding the start date will be managed by aout_FifoPush(). */
679 p_buffer->end_date = start_date +
680 (p_buffer->end_date - p_buffer->start_date);
681 p_buffer->start_date = start_date;
683 vlc_mutex_lock( &p_aout->input_fifos_lock );
684 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
685 vlc_mutex_unlock( &p_aout->input_fifos_lock );
689 /*****************************************************************************
691 *****************************************************************************/
693 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
694 const char * psz_error_message )
697 msg_Err( p_aout, "%s", psz_error_message );
700 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
701 p_input->i_nb_filters );
702 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
703 p_input->i_nb_resamplers );
704 aout_FifoDestroy( p_aout, &p_input->fifo );
705 var_Destroy( p_aout, "visual" );
706 var_Destroy( p_aout, "equalizer" );
707 var_Destroy( p_aout, "audio-filter" );
708 var_Destroy( p_aout, "audio-visual" );
710 var_Destroy( p_aout, "audio-replay-gain-mode" );
711 var_Destroy( p_aout, "audio-replay-gain-default" );
712 var_Destroy( p_aout, "audio-replay-gain-preamp" );
713 var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
716 p_input->b_error = 1;
719 static void inputDrop( aout_instance_t *p_aout, aout_input_t *p_input, aout_buffer_t *p_buffer )
721 aout_BufferFree( p_buffer );
723 if( !p_input->p_input_thread )
726 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
727 stats_UpdateInteger( p_aout, p_input->p_input_thread->p->counters.p_lost_abuffers, 1, NULL );
728 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
731 static void inputResamplingStop( aout_input_t *p_input )
733 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
734 if( p_input->i_nb_resamplers != 0 )
736 p_input->pp_resamplers[0]->input.i_rate = INPUT_RATE_DEFAULT *
737 p_input->input.i_rate / p_input->i_last_input_rate;
738 p_input->pp_resamplers[0]->b_continuity = false;
742 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
743 const char *psz_name, bool b_add )
748 var_Get( p_aout, psz_variable, &val );
750 if( !val.psz_string ) val.psz_string = strdup("");
752 psz_parser = strstr( val.psz_string, psz_name );
758 psz_parser = val.psz_string;
759 asprintf( &val.psz_string, (*val.psz_string) ? "%s:%s" : "%s%s",
760 val.psz_string, psz_name );
772 memmove( psz_parser, psz_parser + strlen(psz_name) +
773 (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
774 strlen(psz_parser + strlen(psz_name)) + 1 );
778 free( val.psz_string );
783 var_Set( p_aout, psz_variable, val );
784 free( val.psz_string );
788 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
789 vlc_value_t oldval, vlc_value_t newval, void *p_data )
791 aout_instance_t *p_aout = (aout_instance_t *)p_this;
792 char *psz_mode = newval.psz_string;
795 (void)psz_cmd; (void)oldval; (void)p_data;
797 if( !psz_mode || !*psz_mode )
799 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
800 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
801 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false );
805 if( !strcmp( "goom", psz_mode ) )
807 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
808 ChangeFiltersString( p_aout, "audio-visual", "goom", true );
809 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
811 else if( !strcmp( "galaktos", psz_mode ) )
813 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
814 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
815 ChangeFiltersString( p_aout, "audio-visual", "galaktos", true );
819 val.psz_string = psz_mode;
820 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
821 var_Set( p_aout, "effect-list", val );
823 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
824 ChangeFiltersString( p_aout, "audio-visual", "visual", true );
825 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
830 for( i = 0; i < p_aout->i_nb_inputs; i++ )
832 p_aout->pp_inputs[i]->b_restart = true;
838 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
839 vlc_value_t oldval, vlc_value_t newval, void *p_data )
841 aout_instance_t *p_aout = (aout_instance_t *)p_this;
842 char *psz_mode = newval.psz_string;
846 (void)psz_cmd; (void)oldval; (void)p_data;
848 if( !psz_mode || !*psz_mode )
850 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
855 val.psz_string = psz_mode;
856 var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
857 var_Set( p_aout, "equalizer-preset", val );
858 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
866 for( i = 0; i < p_aout->i_nb_inputs; i++ )
868 p_aout->pp_inputs[i]->b_restart = true;
875 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
876 vlc_value_t oldval, vlc_value_t newval, void *p_data )
878 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
879 VLC_UNUSED(newval); VLC_UNUSED(p_data);
880 aout_instance_t *p_aout = (aout_instance_t *)p_this;
883 vlc_mutex_lock( &p_aout->mixer_lock );
884 for( i = 0; i < p_aout->i_nb_inputs; i++ )
885 ReplayGainSelect( p_aout, p_aout->pp_inputs[i] );
887 /* Restart the mixer (a trivial mixer may be in use) */
888 aout_MixerMultiplierSet( p_aout, p_aout->mixer.f_multiplier );
889 vlc_mutex_unlock( &p_aout->mixer_lock );
894 static void ReplayGainSelect( aout_instance_t *p_aout, aout_input_t *p_input )
896 char *psz_replay_gain = var_GetNonEmptyString( p_aout,
897 "audio-replay-gain-mode" );
902 p_input->f_multiplier = 1.0;
904 if( !psz_replay_gain )
907 /* Find select mode */
908 if( !strcmp( psz_replay_gain, "track" ) )
909 i_mode = AUDIO_REPLAY_GAIN_TRACK;
910 else if( !strcmp( psz_replay_gain, "album" ) )
911 i_mode = AUDIO_REPLAY_GAIN_ALBUM;
913 i_mode = AUDIO_REPLAY_GAIN_MAX;
915 /* If the select mode is not available, prefer the other one */
917 if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
919 for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
921 if( p_input->replay_gain.pb_gain[i_use] )
927 if( i_use != AUDIO_REPLAY_GAIN_MAX )
928 f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
929 else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
930 f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
933 p_input->f_multiplier = pow( 10.0, f_gain / 20.0 );
936 if( p_input->replay_gain.pb_peak[i_use] &&
937 var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
938 p_input->replay_gain.pf_peak[i_use] * p_input->f_multiplier > 1.0 )
940 p_input->f_multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
943 free( psz_replay_gain );