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 *****************************************************************************/
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 );
103 /* Look for goom plugin */
104 if( module_Exists( VLC_OBJECT(p_aout), "goom" ) )
106 val.psz_string = (char*)"goom"; text.psz_string = (char*)"Goom";
107 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
110 /* Look for galaktos plugin */
111 if( module_Exists( VLC_OBJECT(p_aout), "galaktos" ) )
113 val.psz_string = (char*)"galaktos"; text.psz_string = (char*)"GaLaktos";
114 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
117 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
119 var_Set( p_aout, "visual", val );
120 free( val.psz_string );
122 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
125 if( var_Type( p_aout, "equalizer" ) == 0 )
127 module_config_t *p_config;
130 p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
131 if( p_config && p_config->i_list )
133 var_Create( p_aout, "equalizer",
134 VLC_VAR_STRING | VLC_VAR_HASCHOICE );
135 text.psz_string = _("Equalizer");
136 var_Change( p_aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL );
138 val.psz_string = (char*)""; text.psz_string = _("Disable");
139 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text );
141 for( i = 0; i < p_config->i_list; i++ )
143 val.psz_string = (char *)p_config->ppsz_list[i];
144 text.psz_string = (char *)p_config->ppsz_list_text[i];
145 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE,
149 var_AddCallback( p_aout, "equalizer", EqualizerCallback, NULL );
153 if( var_Type( p_aout, "audio-filter" ) == 0 )
155 var_Create( p_aout, "audio-filter",
156 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
157 text.psz_string = _("Audio filters");
158 var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
160 if( var_Type( p_aout, "audio-visual" ) == 0 )
162 var_Create( p_aout, "audio-visual",
163 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
164 text.psz_string = _("Audio visualizations");
165 var_Change( p_aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL );
168 if( var_Type( p_aout, "audio-replay-gain-mode" ) == 0 )
170 module_config_t *p_config;
173 p_config = config_FindConfig( VLC_OBJECT(p_aout), "audio-replay-gain-mode" );
174 if( p_config && p_config->i_list )
176 var_Create( p_aout, "audio-replay-gain-mode",
177 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
179 text.psz_string = _("Replay gain");
180 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL );
182 for( i = 0; i < p_config->i_list; i++ )
184 val.psz_string = (char *)p_config->ppsz_list[i];
185 text.psz_string = (char *)p_config->ppsz_list_text[i];
186 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
190 var_AddCallback( p_aout, "audio-replay-gain-mode", ReplayGainCallback, NULL );
193 if( var_Type( p_aout, "audio-replay-gain-preamp" ) == 0 )
195 var_Create( p_aout, "audio-replay-gain-preamp",
196 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
198 if( var_Type( p_aout, "audio-replay-gain-default" ) == 0 )
200 var_Create( p_aout, "audio-replay-gain-default",
201 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
203 if( var_Type( p_aout, "audio-replay-gain-peak-protection" ) == 0 )
205 var_Create( p_aout, "audio-replay-gain-peak-protection",
206 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
209 var_Get( p_aout, "audio-filter", &val );
210 psz_filters = val.psz_string;
211 var_Get( p_aout, "audio-visual", &val );
212 psz_visual = val.psz_string;
214 /* parse user filter lists */
215 for( i_visual = 0; i_visual < 2; i_visual++ )
217 char *psz_next = NULL;
218 char *psz_parser = i_visual ? psz_visual : psz_filters;
220 if( psz_parser == NULL || !*psz_parser )
223 while( psz_parser && *psz_parser )
225 aout_filter_t * p_filter = NULL;
227 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
229 msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
233 while( *psz_parser == ' ' && *psz_parser == ':' )
237 if( ( psz_next = strchr( psz_parser , ':' ) ) )
241 if( *psz_parser =='\0' )
246 /* Create a VLC object */
247 p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
248 if( p_filter == NULL )
250 msg_Err( p_aout, "cannot add user filter %s (skipped)",
252 psz_parser = psz_next;
256 vlc_object_attach( p_filter , p_aout );
258 /* try to find the requested filter */
259 if( i_visual == 1 ) /* this can only be a visualization module */
262 memcpy( &p_filter->input, &chain_output_format,
263 sizeof(audio_sample_format_t) );
264 memcpy( &p_filter->output, &chain_output_format,
265 sizeof(audio_sample_format_t) );
267 p_filter->p_module = module_Need( p_filter, "visualization",
268 psz_parser, VLC_TRUE );
270 else /* this can be a audio filter module as well as a visualization module */
273 memcpy( &p_filter->input, &chain_input_format,
274 sizeof(audio_sample_format_t) );
275 memcpy( &p_filter->output, &chain_output_format,
276 sizeof(audio_sample_format_t) );
278 p_filter->p_module = module_Need( p_filter, "audio filter",
279 psz_parser, VLC_TRUE );
281 if ( p_filter->p_module == NULL )
283 /* if the filter requested a special format, retry */
284 if ( !( AOUT_FMTS_IDENTICAL( &p_filter->input,
285 &chain_input_format )
286 && AOUT_FMTS_IDENTICAL( &p_filter->output,
287 &chain_output_format ) ) )
289 aout_FormatPrepare( &p_filter->input );
290 aout_FormatPrepare( &p_filter->output );
291 p_filter->p_module = module_Need( p_filter,
293 psz_parser, VLC_TRUE );
295 /* try visual filters */
298 memcpy( &p_filter->input, &chain_output_format,
299 sizeof(audio_sample_format_t) );
300 memcpy( &p_filter->output, &chain_output_format,
301 sizeof(audio_sample_format_t) );
302 p_filter->p_module = module_Need( p_filter,
304 psz_parser, VLC_TRUE );
310 if ( p_filter->p_module == NULL )
312 msg_Err( p_aout, "cannot add user filter %s (skipped)",
315 vlc_object_detach( p_filter );
316 vlc_object_release( p_filter );
318 psz_parser = psz_next;
322 /* complete the filter chain if necessary */
323 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &p_filter->input ) )
325 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
326 &p_input->i_nb_filters,
328 &p_filter->input ) < 0 )
330 msg_Err( p_aout, "cannot add user filter %s (skipped)",
333 module_Unneed( p_filter, p_filter->p_module );
334 vlc_object_detach( p_filter );
335 vlc_object_release( p_filter );
337 psz_parser = psz_next;
343 p_filter->b_continuity = VLC_FALSE;
344 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
345 memcpy( &chain_input_format, &p_filter->output,
346 sizeof( audio_sample_format_t ) );
348 /* next filter if any */
349 psz_parser = psz_next;
355 /* complete the filter chain if necessary */
356 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &chain_output_format ) )
358 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
359 &p_input->i_nb_filters,
361 &chain_output_format ) < 0 )
363 inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
368 /* Prepare hints for the buffer allocator. */
369 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
370 p_input->input_alloc.i_bytes_per_sec = -1;
372 /* Create resamplers. */
373 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
375 chain_output_format.i_rate = (__MAX(p_input->input.i_rate,
376 p_aout->mixer.mixer.i_rate)
377 * (100 + AOUT_MAX_RESAMPLING)) / 100;
378 if ( chain_output_format.i_rate == p_aout->mixer.mixer.i_rate )
380 /* Just in case... */
381 chain_output_format.i_rate++;
383 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
384 &p_input->i_nb_resamplers,
385 &chain_output_format,
386 &p_aout->mixer.mixer ) < 0 )
388 inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
392 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
393 p_input->i_nb_resamplers,
394 &p_input->input_alloc );
395 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
397 /* Setup the initial rate of the resampler */
398 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
400 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
402 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
403 p_input->i_nb_filters,
404 &p_input->input_alloc );
405 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
407 /* i_bytes_per_sec is still == -1 if no filters */
408 p_input->input_alloc.i_bytes_per_sec = __MAX(
409 p_input->input_alloc.i_bytes_per_sec,
410 (int)(p_input->input.i_bytes_per_frame
411 * p_input->input.i_rate
412 / p_input->input.i_frame_length) );
414 ReplayGainSelect( p_aout, p_input );
417 p_input->b_error = VLC_FALSE;
418 p_input->b_restart = VLC_FALSE;
419 p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
424 /*****************************************************************************
425 * aout_InputDelete : delete an input
426 *****************************************************************************
427 * This function must be entered with the mixer lock.
428 *****************************************************************************/
429 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
431 if ( p_input->b_error ) return 0;
433 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
434 p_input->i_nb_filters );
435 p_input->i_nb_filters = 0;
436 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
437 p_input->i_nb_resamplers );
438 p_input->i_nb_resamplers = 0;
439 aout_FifoDestroy( p_aout, &p_input->fifo );
444 /*****************************************************************************
445 * aout_InputPlay : play a buffer
446 *****************************************************************************
447 * This function must be entered with the input lock.
448 *****************************************************************************/
449 /* XXX Do not activate it !! */
450 //#define AOUT_PROCESS_BEFORE_CHEKS
451 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
452 aout_buffer_t * p_buffer, int i_input_rate )
456 if( p_input->b_restart )
458 aout_fifo_t fifo, dummy_fifo;
459 byte_t *p_first_byte_to_mix;
461 vlc_mutex_lock( &p_aout->mixer_lock );
463 /* A little trick to avoid loosing our input fifo */
464 aout_FifoInit( p_aout, &dummy_fifo, p_aout->mixer.mixer.i_rate );
465 p_first_byte_to_mix = p_input->p_first_byte_to_mix;
466 fifo = p_input->fifo;
467 p_input->fifo = dummy_fifo;
468 aout_InputDelete( p_aout, p_input );
469 aout_InputNew( p_aout, p_input );
470 p_input->p_first_byte_to_mix = p_first_byte_to_mix;
471 p_input->fifo = fifo;
473 vlc_mutex_unlock( &p_aout->mixer_lock );
476 if( i_input_rate != INPUT_RATE_DEFAULT && p_input->i_nb_resamplers <= 0 )
478 inputDrop( p_aout, p_input, p_buffer );
482 #ifdef AOUT_PROCESS_BEFORE_CHEKS
483 /* Run pre-filters. */
484 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
487 /* Actually run the resampler now. */
488 if ( p_input->i_nb_resamplers > 0 )
490 const mtime_t i_date = p_buffer->start_date;
491 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
492 p_input->i_nb_resamplers,
496 if( p_buffer->i_nb_samples <= 0 )
498 aout_BufferFree( p_buffer );
503 /* Handle input rate change by modifying resampler input rate */
504 if( i_input_rate != p_input->i_last_input_rate )
506 unsigned int * const pi_rate = &p_input->pp_resamplers[0]->input.i_rate;
507 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
508 const int i_delta = *pi_rate - F(p_input->input.i_rate,p_input->i_last_input_rate);
509 *pi_rate = F(p_input->input.i_rate + i_delta, i_input_rate);
511 p_input->i_last_input_rate = i_input_rate;
514 /* We don't care if someone changes the start date behind our back after
515 * this. We'll deal with that when pushing the buffer, and compensate
516 * with the next incoming buffer. */
517 vlc_mutex_lock( &p_aout->input_fifos_lock );
518 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
519 vlc_mutex_unlock( &p_aout->input_fifos_lock );
521 if ( start_date != 0 && start_date < mdate() )
523 /* The decoder is _very_ late. This can only happen if the user
524 * pauses the stream (or if the decoder is buggy, which cannot
526 msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
527 "clearing out", mdate() - start_date );
528 vlc_mutex_lock( &p_aout->input_fifos_lock );
529 aout_FifoSet( p_aout, &p_input->fifo, 0 );
530 p_input->p_first_byte_to_mix = NULL;
531 vlc_mutex_unlock( &p_aout->input_fifos_lock );
532 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
533 msg_Warn( p_aout, "timing screwed, stopping resampling" );
534 inputResamplingStop( p_input );
538 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
540 /* The decoder gives us f*cked up PTS. It's its business, but we
541 * can't present it anyway, so drop the buffer. */
542 msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
543 mdate() - p_buffer->start_date );
545 inputDrop( p_aout, p_input, p_buffer );
546 inputResamplingStop( p_input );
550 /* If the audio drift is too big then it's not worth trying to resample
552 if ( start_date != 0 &&
553 ( start_date < p_buffer->start_date - 3 * AOUT_PTS_TOLERANCE ) )
555 msg_Warn( p_aout, "audio drift is too big ("I64Fd"), clearing out",
556 start_date - p_buffer->start_date );
557 vlc_mutex_lock( &p_aout->input_fifos_lock );
558 aout_FifoSet( p_aout, &p_input->fifo, 0 );
559 p_input->p_first_byte_to_mix = NULL;
560 vlc_mutex_unlock( &p_aout->input_fifos_lock );
561 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
562 msg_Warn( p_aout, "timing screwed, stopping resampling" );
563 inputResamplingStop( p_input );
566 else if ( start_date != 0 &&
567 ( start_date > p_buffer->start_date + 3 * AOUT_PTS_TOLERANCE ) )
569 msg_Warn( p_aout, "audio drift is too big ("I64Fd"), dropping buffer",
570 start_date - p_buffer->start_date );
571 inputDrop( p_aout, p_input, p_buffer );
575 if ( start_date == 0 ) start_date = p_buffer->start_date;
577 #ifndef AOUT_PROCESS_BEFORE_CHEKS
578 /* Run pre-filters. */
579 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
583 /* Run the resampler if needed.
584 * We first need to calculate the output rate of this resampler. */
585 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
586 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
587 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
588 p_input->i_nb_resamplers > 0 )
590 /* Can happen in several circumstances :
591 * 1. A problem at the input (clock drift)
592 * 2. A small pause triggered by the user
593 * 3. Some delay in the output stage, causing a loss of lip
595 * Solution : resample the buffer to avoid a scratch.
597 mtime_t drift = p_buffer->start_date - start_date;
599 p_input->i_resamp_start_date = mdate();
600 p_input->i_resamp_start_drift = (int)drift;
603 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
605 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
607 msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
608 drift > 0 ? drift : -drift,
609 drift > 0 ? "in advance" : "late",
610 drift > 0 ? "down" : "up");
613 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
615 /* Resampling has been triggered previously (because of dates
616 * mismatch). We want the resampling to happen progressively so
617 * it isn't too audible to the listener. */
619 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
621 p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
625 p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
628 /* Check if everything is back to normal, in which case we can stop the
630 if( p_input->pp_resamplers[0]->input.i_rate == 1000 * p_input->input.i_rate / i_input_rate )
632 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
633 msg_Warn( p_aout, "resampling stopped after "I64Fi" usec "
635 mdate() - p_input->i_resamp_start_date,
636 p_buffer->start_date - start_date);
638 else if( abs( (int)(p_buffer->start_date - start_date) ) <
639 abs( p_input->i_resamp_start_drift ) / 2 )
641 /* if we reduced the drift from half, then it is time to switch
642 * back the resampling direction. */
643 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
644 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
646 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
647 p_input->i_resamp_start_drift = 0;
649 else if( p_input->i_resamp_start_drift &&
650 ( abs( (int)(p_buffer->start_date - start_date) ) >
651 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
653 /* If the drift is increasing and not decreasing, than something
654 * is bad. We'd better stop the resampling right now. */
655 msg_Warn( p_aout, "timing screwed, stopping resampling" );
656 inputResamplingStop( p_input );
660 #ifndef AOUT_PROCESS_BEFORE_CHEKS
661 /* Actually run the resampler now. */
662 if ( p_input->i_nb_resamplers > 0 )
664 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
665 p_input->i_nb_resamplers,
669 if( p_buffer->i_nb_samples <= 0 )
671 aout_BufferFree( p_buffer );
676 /* Adding the start date will be managed by aout_FifoPush(). */
677 p_buffer->end_date = start_date +
678 (p_buffer->end_date - p_buffer->start_date);
679 p_buffer->start_date = start_date;
681 vlc_mutex_lock( &p_aout->input_fifos_lock );
682 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
683 vlc_mutex_unlock( &p_aout->input_fifos_lock );
687 /*****************************************************************************
689 *****************************************************************************/
691 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
692 const char * psz_error_message )
695 msg_Err( p_aout, "%s", psz_error_message );
698 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
699 p_input->i_nb_filters );
700 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
701 p_input->i_nb_resamplers );
702 aout_FifoDestroy( p_aout, &p_input->fifo );
703 var_Destroy( p_aout, "visual" );
704 var_Destroy( p_aout, "equalizer" );
705 var_Destroy( p_aout, "audio-filter" );
706 var_Destroy( p_aout, "audio-visual" );
708 var_Destroy( p_aout, "audio-replay-gain-mode" );
709 var_Destroy( p_aout, "audio-replay-gain-default" );
710 var_Destroy( p_aout, "audio-replay-gain-preamp" );
711 var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
714 p_input->b_error = 1;
717 static void inputDrop( aout_instance_t *p_aout, aout_input_t *p_input, aout_buffer_t *p_buffer )
719 aout_BufferFree( p_buffer );
721 if( !p_input->p_input_thread )
724 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
725 stats_UpdateInteger( p_aout, p_input->p_input_thread->p->counters.p_lost_abuffers, 1, NULL );
726 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
729 static void inputResamplingStop( aout_input_t *p_input )
731 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
732 if( p_input->i_nb_resamplers != 0 )
734 p_input->pp_resamplers[0]->input.i_rate = INPUT_RATE_DEFAULT *
735 p_input->input.i_rate / p_input->i_last_input_rate;
736 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
740 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
741 const char *psz_name, vlc_bool_t b_add )
746 var_Get( p_aout, psz_variable, &val );
748 if( !val.psz_string ) val.psz_string = strdup("");
750 psz_parser = strstr( val.psz_string, psz_name );
756 psz_parser = val.psz_string;
757 asprintf( &val.psz_string, (*val.psz_string) ? "%s:%s" : "%s%s",
758 val.psz_string, psz_name );
770 memmove( psz_parser, psz_parser + strlen(psz_name) +
771 (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
772 strlen(psz_parser + strlen(psz_name)) + 1 );
776 free( val.psz_string );
781 var_Set( p_aout, psz_variable, val );
782 free( val.psz_string );
786 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
787 vlc_value_t oldval, vlc_value_t newval, void *p_data )
789 aout_instance_t *p_aout = (aout_instance_t *)p_this;
790 char *psz_mode = newval.psz_string;
793 (void)psz_cmd; (void)oldval; (void)p_data;
795 if( !psz_mode || !*psz_mode )
797 ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
798 ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
799 ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE );
803 if( !strcmp( "goom", psz_mode ) )
805 ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
806 ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_TRUE );
807 ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE);
809 else if( !strcmp( "galaktos", psz_mode ) )
811 ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_FALSE );
812 ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
813 ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_TRUE );
817 val.psz_string = psz_mode;
818 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
819 var_Set( p_aout, "effect-list", val );
821 ChangeFiltersString( p_aout, "audio-visual", "goom", VLC_FALSE );
822 ChangeFiltersString( p_aout, "audio-visual", "visual", VLC_TRUE );
823 ChangeFiltersString( p_aout, "audio-visual", "galaktos", VLC_FALSE);
828 for( i = 0; i < p_aout->i_nb_inputs; i++ )
830 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
836 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
837 vlc_value_t oldval, vlc_value_t newval, void *p_data )
839 aout_instance_t *p_aout = (aout_instance_t *)p_this;
840 char *psz_mode = newval.psz_string;
844 (void)psz_cmd; (void)oldval; (void)p_data;
846 if( !psz_mode || !*psz_mode )
848 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
853 val.psz_string = psz_mode;
854 var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
855 var_Set( p_aout, "equalizer-preset", val );
856 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
864 for( i = 0; i < p_aout->i_nb_inputs; i++ )
866 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
873 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
874 vlc_value_t oldval, vlc_value_t newval, void *p_data )
876 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
877 VLC_UNUSED(newval); VLC_UNUSED(p_data);
878 aout_instance_t *p_aout = (aout_instance_t *)p_this;
881 vlc_mutex_lock( &p_aout->mixer_lock );
882 for( i = 0; i < p_aout->i_nb_inputs; i++ )
883 ReplayGainSelect( p_aout, p_aout->pp_inputs[i] );
885 /* Restart the mixer (a trivial mixer may be in use) */
886 aout_MixerMultiplierSet( p_aout, p_aout->mixer.f_multiplier );
887 vlc_mutex_unlock( &p_aout->mixer_lock );
892 static void ReplayGainSelect( aout_instance_t *p_aout, aout_input_t *p_input )
894 char *psz_replay_gain = var_GetNonEmptyString( p_aout,
895 "audio-replay-gain-mode" );
900 p_input->f_multiplier = 1.0;
902 if( !psz_replay_gain )
905 /* Find select mode */
906 if( !strcmp( psz_replay_gain, "track" ) )
907 i_mode = AUDIO_REPLAY_GAIN_TRACK;
908 else if( !strcmp( psz_replay_gain, "album" ) )
909 i_mode = AUDIO_REPLAY_GAIN_ALBUM;
911 i_mode = AUDIO_REPLAY_GAIN_MAX;
913 /* If the select mode is not available, prefer the other one */
915 if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
917 for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
919 if( p_input->replay_gain.pb_gain[i_use] )
925 if( i_use != AUDIO_REPLAY_GAIN_MAX )
926 f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
927 else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
928 f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
931 p_input->f_multiplier = pow( 10.0, f_gain / 20.0 );
934 if( p_input->replay_gain.pb_peak[i_use] &&
935 var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
936 p_input->replay_gain.pf_peak[i_use] * p_input->f_multiplier > 1.0 )
938 p_input->f_multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
941 free( psz_replay_gain );