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 *****************************************************************************/
34 #include <vlc_input.h> /* for input_thread_t and i_pts_delay */
41 #include "aout_internal.h"
43 /** FIXME: Ugly but needed to access the counters */
44 #include "input/input_internal.h"
46 static void inputFailure( aout_instance_t *, aout_input_t *, const char * );
47 static void inputDrop( aout_instance_t *, aout_input_t *, aout_buffer_t * );
48 static void inputResamplingStop( aout_input_t *p_input );
50 static int VisualizationCallback( vlc_object_t *, char const *,
51 vlc_value_t, vlc_value_t, void * );
52 static int EqualizerCallback( vlc_object_t *, char const *,
53 vlc_value_t, vlc_value_t, void * );
54 static int ReplayGainCallback( vlc_object_t *, char const *,
55 vlc_value_t, vlc_value_t, void * );
56 static void ReplayGainSelect( aout_instance_t *, aout_input_t * );
57 /*****************************************************************************
58 * aout_InputNew : allocate a new input and rework the filter pipeline
59 *****************************************************************************/
60 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
62 audio_sample_format_t chain_input_format;
63 audio_sample_format_t chain_output_format;
64 vlc_value_t val, text;
65 char * psz_filters, *psz_visual;
68 aout_FormatPrint( p_aout, "input", &p_input->input );
70 p_input->i_nb_resamplers = p_input->i_nb_filters = 0;
73 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
74 p_input->p_first_byte_to_mix = NULL;
76 /* Prepare format structure */
77 memcpy( &chain_input_format, &p_input->input,
78 sizeof(audio_sample_format_t) );
79 memcpy( &chain_output_format, &p_aout->mixer.mixer,
80 sizeof(audio_sample_format_t) );
81 chain_output_format.i_rate = p_input->input.i_rate;
82 aout_FormatPrepare( &chain_output_format );
84 /* Now add user filters */
85 if( var_Type( p_aout, "visual" ) == 0 )
88 var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
89 text.psz_string = _("Visualizations");
90 var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
91 val.psz_string = ""; text.psz_string = _("Disable");
92 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
93 val.psz_string = "spectrometer"; text.psz_string = _("Spectrometer");
94 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
95 val.psz_string = "scope"; text.psz_string = _("Scope");
96 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
97 val.psz_string = "spectrum"; text.psz_string = _("Spectrum");
98 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
100 /* Look for goom plugin */
101 p_module = config_FindModule( VLC_OBJECT(p_aout), "goom" );
104 val.psz_string = "goom"; text.psz_string = "Goom";
105 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
108 /* Look for galaktos plugin */
109 p_module = config_FindModule( VLC_OBJECT(p_aout), "galaktos" );
112 val.psz_string = "galaktos"; text.psz_string = "GaLaktos";
113 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
116 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
118 var_Set( p_aout, "visual", val );
119 if( val.psz_string ) free( val.psz_string );
121 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
124 if( var_Type( p_aout, "equalizer" ) == 0 )
126 module_config_t *p_config;
129 p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
130 if( p_config && p_config->i_list )
132 var_Create( p_aout, "equalizer",
133 VLC_VAR_STRING | VLC_VAR_HASCHOICE );
134 text.psz_string = _("Equalizer");
135 var_Change( p_aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL );
137 val.psz_string = ""; text.psz_string = _("Disable");
138 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text );
140 for( i = 0; i < p_config->i_list; i++ )
142 val.psz_string = (char *)p_config->ppsz_list[i];
143 text.psz_string = (char *)p_config->ppsz_list_text[i];
144 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE,
148 var_AddCallback( p_aout, "equalizer", EqualizerCallback, NULL );
152 if( var_Type( p_aout, "audio-filter" ) == 0 )
154 var_Create( p_aout, "audio-filter",
155 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
156 text.psz_string = _("Audio filters");
157 var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
159 if( var_Type( p_aout, "audio-visual" ) == 0 )
161 var_Create( p_aout, "audio-visual",
162 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
163 text.psz_string = _("Audio visualizations");
164 var_Change( p_aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL );
167 if( var_Type( p_aout, "audio-replay-gain-mode" ) == 0 )
169 module_config_t *p_config;
172 p_config = config_FindConfig( VLC_OBJECT(p_aout), "audio-replay-gain-mode" );
173 if( p_config && p_config->i_list )
175 var_Create( p_aout, "audio-replay-gain-mode",
176 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
178 text.psz_string = _("Replay gain");
179 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL );
181 for( i = 0; i < p_config->i_list; i++ )
183 val.psz_string = (char *)p_config->ppsz_list[i];
184 text.psz_string = (char *)p_config->ppsz_list_text[i];
185 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
189 var_AddCallback( p_aout, "audio-replay-gain-mode", ReplayGainCallback, NULL );
192 if( var_Type( p_aout, "audio-replay-gain-preamp" ) == 0 )
194 var_Create( p_aout, "audio-replay-gain-preamp",
195 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
197 if( var_Type( p_aout, "audio-replay-gain-default" ) == 0 )
199 var_Create( p_aout, "audio-replay-gain-default",
200 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
202 if( var_Type( p_aout, "audio-replay-gain-peak-protection" ) == 0 )
204 var_Create( p_aout, "audio-replay-gain-peak-protection",
205 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
208 var_Get( p_aout, "audio-filter", &val );
209 psz_filters = val.psz_string;
210 var_Get( p_aout, "audio-visual", &val );
211 psz_visual = val.psz_string;
213 /* parse user filter lists */
214 for( i_visual = 0; i_visual < 2; i_visual++ )
216 char *psz_next = NULL;
217 char *psz_parser = i_visual ? psz_visual : psz_filters;
219 if( psz_parser == NULL || !*psz_parser )
222 while( psz_parser && *psz_parser )
224 aout_filter_t * p_filter = NULL;
226 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
228 msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
232 while( *psz_parser == ' ' && *psz_parser == ':' )
236 if( ( psz_next = strchr( psz_parser , ':' ) ) )
240 if( *psz_parser =='\0' )
245 /* Create a VLC object */
246 p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
247 if( p_filter == NULL )
249 msg_Err( p_aout, "cannot add user filter %s (skipped)",
251 psz_parser = psz_next;
255 vlc_object_attach( p_filter , p_aout );
257 /* try to find the requested filter */
258 if( i_visual == 1 ) /* this can only be a visualization module */
261 memcpy( &p_filter->input, &chain_output_format,
262 sizeof(audio_sample_format_t) );
263 memcpy( &p_filter->output, &chain_output_format,
264 sizeof(audio_sample_format_t) );
266 p_filter->p_module = module_Need( p_filter, "visualization",
267 psz_parser, VLC_TRUE );
269 else /* this can be a audio filter module as well as a visualization module */
272 memcpy( &p_filter->input, &chain_input_format,
273 sizeof(audio_sample_format_t) );
274 memcpy( &p_filter->output, &chain_output_format,
275 sizeof(audio_sample_format_t) );
277 p_filter->p_module = module_Need( p_filter, "audio filter",
278 psz_parser, VLC_TRUE );
280 if ( p_filter->p_module == NULL )
282 /* if the filter requested a special format, retry */
283 if ( !( AOUT_FMTS_IDENTICAL( &p_filter->input,
284 &chain_input_format )
285 && AOUT_FMTS_IDENTICAL( &p_filter->output,
286 &chain_output_format ) ) )
288 aout_FormatPrepare( &p_filter->input );
289 aout_FormatPrepare( &p_filter->output );
290 p_filter->p_module = module_Need( p_filter,
292 psz_parser, VLC_TRUE );
294 /* try visual filters */
297 memcpy( &p_filter->input, &chain_output_format,
298 sizeof(audio_sample_format_t) );
299 memcpy( &p_filter->output, &chain_output_format,
300 sizeof(audio_sample_format_t) );
301 p_filter->p_module = module_Need( p_filter,
303 psz_parser, VLC_TRUE );
309 if ( p_filter->p_module == NULL )
311 msg_Err( p_aout, "cannot add user filter %s (skipped)",
314 vlc_object_detach( p_filter );
315 vlc_object_destroy( p_filter );
317 psz_parser = psz_next;
321 /* complete the filter chain if necessary */
322 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &p_filter->input ) )
324 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
325 &p_input->i_nb_filters,
327 &p_filter->input ) < 0 )
329 msg_Err( p_aout, "cannot add user filter %s (skipped)",
332 module_Unneed( p_filter, p_filter->p_module );
333 vlc_object_detach( p_filter );
334 vlc_object_destroy( p_filter );
336 psz_parser = psz_next;
342 p_filter->b_continuity = VLC_FALSE;
343 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
344 memcpy( &chain_input_format, &p_filter->output,
345 sizeof( audio_sample_format_t ) );
347 /* next filter if any */
348 psz_parser = psz_next;
351 if( psz_filters ) free( psz_filters );
352 if( psz_visual ) free( psz_visual );
354 /* complete the filter chain if necessary */
355 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &chain_output_format ) )
357 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
358 &p_input->i_nb_filters,
360 &chain_output_format ) < 0 )
362 inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
367 /* Prepare hints for the buffer allocator. */
368 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
369 p_input->input_alloc.i_bytes_per_sec = -1;
371 /* Create resamplers. */
372 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
374 chain_output_format.i_rate = (__MAX(p_input->input.i_rate,
375 p_aout->mixer.mixer.i_rate)
376 * (100 + AOUT_MAX_RESAMPLING)) / 100;
377 if ( chain_output_format.i_rate == p_aout->mixer.mixer.i_rate )
379 /* Just in case... */
380 chain_output_format.i_rate++;
382 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
383 &p_input->i_nb_resamplers,
384 &chain_output_format,
385 &p_aout->mixer.mixer ) < 0 )
387 inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
391 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
392 p_input->i_nb_resamplers,
393 &p_input->input_alloc );
394 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
396 /* Setup the initial rate of the resampler */
397 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
399 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
401 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
402 p_input->i_nb_filters,
403 &p_input->input_alloc );
404 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
406 /* i_bytes_per_sec is still == -1 if no filters */
407 p_input->input_alloc.i_bytes_per_sec = __MAX(
408 p_input->input_alloc.i_bytes_per_sec,
409 (int)(p_input->input.i_bytes_per_frame
410 * p_input->input.i_rate
411 / p_input->input.i_frame_length) );
413 ReplayGainSelect( p_aout, p_input );
416 p_input->b_error = VLC_FALSE;
417 p_input->b_restart = VLC_FALSE;
418 p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
423 /*****************************************************************************
424 * aout_InputDelete : delete an input
425 *****************************************************************************
426 * This function must be entered with the mixer lock.
427 *****************************************************************************/
428 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
430 if ( p_input->b_error ) return 0;
432 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
433 p_input->i_nb_filters );
434 p_input->i_nb_filters = 0;
435 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
436 p_input->i_nb_resamplers );
437 p_input->i_nb_resamplers = 0;
438 aout_FifoDestroy( p_aout, &p_input->fifo );
443 /*****************************************************************************
444 * aout_InputPlay : play a buffer
445 *****************************************************************************
446 * This function must be entered with the input lock.
447 *****************************************************************************/
448 /* XXX Do not activate it !! */
449 //#define AOUT_PROCESS_BEFORE_CHEKS
450 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
451 aout_buffer_t * p_buffer, int i_input_rate )
455 if( p_input->b_restart )
457 aout_fifo_t fifo, dummy_fifo;
458 byte_t *p_first_byte_to_mix;
460 vlc_mutex_lock( &p_aout->mixer_lock );
462 /* A little trick to avoid loosing our input fifo */
463 aout_FifoInit( p_aout, &dummy_fifo, p_aout->mixer.mixer.i_rate );
464 p_first_byte_to_mix = p_input->p_first_byte_to_mix;
465 fifo = p_input->fifo;
466 p_input->fifo = dummy_fifo;
467 aout_InputDelete( p_aout, p_input );
468 aout_InputNew( p_aout, p_input );
469 p_input->p_first_byte_to_mix = p_first_byte_to_mix;
470 p_input->fifo = fifo;
472 vlc_mutex_unlock( &p_aout->mixer_lock );
475 if( i_input_rate != INPUT_RATE_DEFAULT && p_input->i_nb_resamplers <= 0 )
477 inputDrop( p_aout, p_input, p_buffer );
481 #ifdef AOUT_PROCESS_BEFORE_CHEKS
482 /* Run pre-filters. */
483 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
486 /* Actually run the resampler now. */
487 if ( p_input->i_nb_resamplers > 0 )
489 const mtime_t i_date = p_buffer->start_date;
490 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
491 p_input->i_nb_resamplers,
495 if( p_buffer->i_nb_samples <= 0 )
497 aout_BufferFree( p_buffer );
502 /* Handle input rate change by modifying resampler input rate */
503 if( i_input_rate != p_input->i_last_input_rate )
505 unsigned int * const pi_rate = &p_input->pp_resamplers[0]->input.i_rate;
506 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
507 const int i_delta = *pi_rate - F(p_input->input.i_rate,p_input->i_last_input_rate);
508 *pi_rate = F(p_input->input.i_rate + i_delta, i_input_rate);
510 p_input->i_last_input_rate = i_input_rate;
513 /* We don't care if someone changes the start date behind our back after
514 * this. We'll deal with that when pushing the buffer, and compensate
515 * with the next incoming buffer. */
516 vlc_mutex_lock( &p_aout->input_fifos_lock );
517 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
518 vlc_mutex_unlock( &p_aout->input_fifos_lock );
520 if ( start_date != 0 && start_date < mdate() )
522 /* The decoder is _very_ late. This can only happen if the user
523 * pauses the stream (or if the decoder is buggy, which cannot
525 msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
526 "clearing out", mdate() - start_date );
527 vlc_mutex_lock( &p_aout->input_fifos_lock );
528 aout_FifoSet( p_aout, &p_input->fifo, 0 );
529 p_input->p_first_byte_to_mix = NULL;
530 vlc_mutex_unlock( &p_aout->input_fifos_lock );
531 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
532 msg_Warn( p_aout, "timing screwed, stopping resampling" );
533 inputResamplingStop( p_input );
537 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
539 /* The decoder gives us f*cked up PTS. It's its business, but we
540 * can't present it anyway, so drop the buffer. */
541 msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
542 mdate() - p_buffer->start_date );
544 inputDrop( p_aout, p_input, p_buffer );
545 inputResamplingStop( p_input );
549 /* If the audio drift is too big then it's not worth trying to resample
551 if ( start_date != 0 &&
552 ( start_date < p_buffer->start_date - 3 * AOUT_PTS_TOLERANCE ) )
554 msg_Warn( p_aout, "audio drift is too big ("I64Fd"), clearing out",
555 start_date - p_buffer->start_date );
556 vlc_mutex_lock( &p_aout->input_fifos_lock );
557 aout_FifoSet( p_aout, &p_input->fifo, 0 );
558 p_input->p_first_byte_to_mix = NULL;
559 vlc_mutex_unlock( &p_aout->input_fifos_lock );
560 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
561 msg_Warn( p_aout, "timing screwed, stopping resampling" );
562 inputResamplingStop( p_input );
565 else if ( start_date != 0 &&
566 ( start_date > p_buffer->start_date + 3 * AOUT_PTS_TOLERANCE ) )
568 msg_Warn( p_aout, "audio drift is too big ("I64Fd"), dropping buffer",
569 start_date - p_buffer->start_date );
570 inputDrop( p_aout, p_input, p_buffer );
574 if ( start_date == 0 ) start_date = p_buffer->start_date;
576 #ifndef AOUT_PROCESS_BEFORE_CHEKS
577 /* Run pre-filters. */
578 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
582 /* Run the resampler if needed.
583 * We first need to calculate the output rate of this resampler. */
584 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
585 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
586 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
587 p_input->i_nb_resamplers > 0 )
589 /* Can happen in several circumstances :
590 * 1. A problem at the input (clock drift)
591 * 2. A small pause triggered by the user
592 * 3. Some delay in the output stage, causing a loss of lip
594 * Solution : resample the buffer to avoid a scratch.
596 mtime_t drift = p_buffer->start_date - start_date;
598 p_input->i_resamp_start_date = mdate();
599 p_input->i_resamp_start_drift = (int)drift;
602 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
604 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
606 msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
607 drift > 0 ? drift : -drift,
608 drift > 0 ? "in advance" : "late",
609 drift > 0 ? "down" : "up");
612 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
614 /* Resampling has been triggered previously (because of dates
615 * mismatch). We want the resampling to happen progressively so
616 * it isn't too audible to the listener. */
618 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
620 p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
624 p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
627 /* Check if everything is back to normal, in which case we can stop the
629 if( p_input->pp_resamplers[0]->input.i_rate == 1000 * p_input->input.i_rate / i_input_rate )
631 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
632 msg_Warn( p_aout, "resampling stopped after "I64Fi" usec "
634 mdate() - p_input->i_resamp_start_date,
635 p_buffer->start_date - start_date);
637 else if( abs( (int)(p_buffer->start_date - start_date) ) <
638 abs( p_input->i_resamp_start_drift ) / 2 )
640 /* if we reduced the drift from half, then it is time to switch
641 * back the resampling direction. */
642 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
643 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
645 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
646 p_input->i_resamp_start_drift = 0;
648 else if( p_input->i_resamp_start_drift &&
649 ( abs( (int)(p_buffer->start_date - start_date) ) >
650 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
652 /* If the drift is increasing and not decreasing, than something
653 * is bad. We'd better stop the resampling right now. */
654 msg_Warn( p_aout, "timing screwed, stopping resampling" );
655 inputResamplingStop( p_input );
659 #ifndef AOUT_PROCESS_BEFORE_CHEKS
660 /* Actually run the resampler now. */
661 if ( p_input->i_nb_resamplers > 0 )
663 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
664 p_input->i_nb_resamplers,
668 if( p_buffer->i_nb_samples <= 0 )
670 aout_BufferFree( p_buffer );
675 /* Adding the start date will be managed by aout_FifoPush(). */
676 p_buffer->end_date = start_date +
677 (p_buffer->end_date - p_buffer->start_date);
678 p_buffer->start_date = start_date;
680 vlc_mutex_lock( &p_aout->input_fifos_lock );
681 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
682 vlc_mutex_unlock( &p_aout->input_fifos_lock );
686 /*****************************************************************************
688 *****************************************************************************/
690 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
691 const char * psz_error_message )
694 msg_Err( p_aout, "%s", psz_error_message );
697 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
698 p_input->i_nb_filters );
699 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
700 p_input->i_nb_resamplers );
701 aout_FifoDestroy( p_aout, &p_input->fifo );
702 var_Destroy( p_aout, "visual" );
703 var_Destroy( p_aout, "equalizer" );
704 var_Destroy( p_aout, "audio-filter" );
705 var_Destroy( p_aout, "audio-visual" );
707 var_Destroy( p_aout, "audio-replay-gain-mode" );
708 var_Destroy( p_aout, "audio-replay-gain-default" );
709 var_Destroy( p_aout, "audio-replay-gain-preamp" );
710 var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
713 p_input->b_error = 1;
716 static void inputDrop( aout_instance_t *p_aout, aout_input_t *p_input, aout_buffer_t *p_buffer )
718 aout_BufferFree( p_buffer );
720 if( !p_input->p_input_thread )
723 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
724 stats_UpdateInteger( p_aout, p_input->p_input_thread->p->counters.p_lost_abuffers, 1, NULL );
725 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
728 static void inputResamplingStop( aout_input_t *p_input )
730 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
731 if( p_input->i_nb_resamplers != 0 )
733 p_input->pp_resamplers[0]->input.i_rate = INPUT_RATE_DEFAULT *
734 p_input->input.i_rate / p_input->i_last_input_rate;
735 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
739 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
740 const char *psz_name, vlc_bool_t b_add )
745 var_Get( p_aout, psz_variable, &val );
747 if( !val.psz_string ) val.psz_string = strdup("");
749 psz_parser = strstr( val.psz_string, psz_name );
755 psz_parser = val.psz_string;
756 asprintf( &val.psz_string, (*val.psz_string) ? "%s:%s" : "%s%s",
757 val.psz_string, psz_name );
769 memmove( psz_parser, psz_parser + strlen(psz_name) +
770 (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
771 strlen(psz_parser + strlen(psz_name)) + 1 );
775 free( val.psz_string );
780 var_Set( p_aout, psz_variable, val );
781 free( val.psz_string );
785 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
786 vlc_value_t oldval, vlc_value_t newval, void *p_data )
788 aout_instance_t *p_aout = (aout_instance_t *)p_this;
789 char *psz_mode = newval.psz_string;
792 (void)psz_cmd; (void)oldval; (void)p_data;
794 if( !psz_mode || !*psz_mode )
796 ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
797 ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
798 ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE );
802 if( !strcmp( "goom", psz_mode ) )
804 ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
805 ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_TRUE );
806 ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE);
808 else if( !strcmp( "galaktos", psz_mode ) )
810 ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
811 ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
812 ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_TRUE );
816 val.psz_string = psz_mode;
817 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
818 var_Set( p_aout, "effect-list", val );
820 ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
821 ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_TRUE );
822 ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE);
827 for( i = 0; i < p_aout->i_nb_inputs; i++ )
829 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
835 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
836 vlc_value_t oldval, vlc_value_t newval, void *p_data )
838 aout_instance_t *p_aout = (aout_instance_t *)p_this;
839 char *psz_mode = newval.psz_string;
843 (void)psz_cmd; (void)oldval; (void)p_data;
845 if( !psz_mode || !*psz_mode )
847 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
852 val.psz_string = psz_mode;
853 var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
854 var_Set( p_aout, "equalizer-preset", val );
855 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
863 for( i = 0; i < p_aout->i_nb_inputs; i++ )
865 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
872 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
873 vlc_value_t oldval, vlc_value_t newval, void *p_data )
875 aout_instance_t *p_aout = (aout_instance_t *)p_this;
878 vlc_mutex_lock( &p_aout->mixer_lock );
879 for( i = 0; i < p_aout->i_nb_inputs; i++ )
880 ReplayGainSelect( p_aout, p_aout->pp_inputs[i] );
882 /* Restart the mixer (a trivial mixer may be in use) */
883 aout_MixerMultiplierSet( p_aout, p_aout->mixer.f_multiplier );
884 vlc_mutex_unlock( &p_aout->mixer_lock );
889 static void ReplayGainSelect( aout_instance_t *p_aout, aout_input_t *p_input )
891 char *psz_replay_gain = var_GetString( p_aout, "audio-replay-gain-mode" );
896 p_input->f_multiplier = 1.0;
898 if( !psz_replay_gain )
901 /* Find select mode */
902 if( !strcmp( psz_replay_gain, "track" ) )
903 i_mode = AUDIO_REPLAY_GAIN_TRACK;
904 else if( !strcmp( psz_replay_gain, "album" ) )
905 i_mode = AUDIO_REPLAY_GAIN_ALBUM;
907 i_mode = AUDIO_REPLAY_GAIN_MAX;
909 /* If the select mode is not available, prefer the other one */
911 if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
913 for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
915 if( p_input->replay_gain.pb_gain[i_use] )
921 if( i_use != AUDIO_REPLAY_GAIN_MAX )
922 f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
923 else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
924 f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
927 p_input->f_multiplier = pow( 10.0, f_gain / 20.0 );
930 if( p_input->replay_gain.pb_peak[i_use] &&
931 var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
932 p_input->replay_gain.pf_peak[i_use] * p_input->f_multiplier > 1.0 )
934 p_input->f_multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
937 free( psz_replay_gain );