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 p_input->p_playback_rate_filter = NULL;
405 for( int i = 0; i < p_input->i_nb_filters; i++ )
407 aout_filter_t *p_filter = p_input->pp_filters[i];
408 if( strcmp( "scaletempo", p_filter->psz_object_name ) == 0 )
410 p_input->p_playback_rate_filter = p_filter;
414 if( ! p_input->p_playback_rate_filter && p_input->i_nb_resamplers > 0 )
416 p_input->p_playback_rate_filter = p_input->pp_resamplers[0];
419 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
420 p_input->i_nb_filters,
421 &p_input->input_alloc );
422 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
424 /* i_bytes_per_sec is still == -1 if no filters */
425 p_input->input_alloc.i_bytes_per_sec = __MAX(
426 p_input->input_alloc.i_bytes_per_sec,
427 (int)(p_input->input.i_bytes_per_frame
428 * p_input->input.i_rate
429 / p_input->input.i_frame_length) );
431 ReplayGainSelect( p_aout, p_input );
434 p_input->b_error = false;
435 p_input->b_restart = false;
436 p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
441 /*****************************************************************************
442 * aout_InputDelete : delete an input
443 *****************************************************************************
444 * This function must be entered with the mixer lock.
445 *****************************************************************************/
446 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
448 if ( p_input->b_error ) return 0;
450 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
451 p_input->i_nb_filters );
452 p_input->i_nb_filters = 0;
453 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
454 p_input->i_nb_resamplers );
455 p_input->i_nb_resamplers = 0;
456 aout_FifoDestroy( p_aout, &p_input->fifo );
461 /*****************************************************************************
462 * aout_InputPlay : play a buffer
463 *****************************************************************************
464 * This function must be entered with the input lock.
465 *****************************************************************************/
466 /* XXX Do not activate it !! */
467 //#define AOUT_PROCESS_BEFORE_CHEKS
468 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
469 aout_buffer_t * p_buffer, int i_input_rate )
473 if( p_input->b_restart )
475 aout_fifo_t fifo, dummy_fifo;
476 uint8_t *p_first_byte_to_mix;
478 vlc_mutex_lock( &p_aout->mixer_lock );
480 /* A little trick to avoid loosing our input fifo */
481 aout_FifoInit( p_aout, &dummy_fifo, p_aout->mixer.mixer.i_rate );
482 p_first_byte_to_mix = p_input->p_first_byte_to_mix;
483 fifo = p_input->fifo;
484 p_input->fifo = dummy_fifo;
485 aout_InputDelete( p_aout, p_input );
486 aout_InputNew( p_aout, p_input );
487 p_input->p_first_byte_to_mix = p_first_byte_to_mix;
488 p_input->fifo = fifo;
490 vlc_mutex_unlock( &p_aout->mixer_lock );
493 if( i_input_rate != INPUT_RATE_DEFAULT && p_input->p_playback_rate_filter == NULL )
495 inputDrop( p_aout, p_input, p_buffer );
499 #ifdef AOUT_PROCESS_BEFORE_CHEKS
500 /* Run pre-filters. */
501 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
504 /* Actually run the resampler now. */
505 if ( p_input->i_nb_resamplers > 0 )
507 const mtime_t i_date = p_buffer->start_date;
508 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
509 p_input->i_nb_resamplers,
513 if( p_buffer->i_nb_samples <= 0 )
515 aout_BufferFree( p_buffer );
520 /* Handle input rate change, but keep drift correction */
521 if( i_input_rate != p_input->i_last_input_rate )
523 unsigned int * const pi_rate = &p_input->p_playback_rate_filter->input.i_rate;
524 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
525 const int i_delta = *pi_rate - F(p_input->input.i_rate,p_input->i_last_input_rate);
526 *pi_rate = F(p_input->input.i_rate + i_delta, i_input_rate);
528 p_input->i_last_input_rate = i_input_rate;
531 /* We don't care if someone changes the start date behind our back after
532 * this. We'll deal with that when pushing the buffer, and compensate
533 * with the next incoming buffer. */
534 vlc_mutex_lock( &p_aout->input_fifos_lock );
535 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
536 vlc_mutex_unlock( &p_aout->input_fifos_lock );
538 if ( start_date != 0 && start_date < mdate() )
540 /* The decoder is _very_ late. This can only happen if the user
541 * pauses the stream (or if the decoder is buggy, which cannot
543 msg_Warn( p_aout, "computed PTS is out of range (%"PRId64"), "
544 "clearing out", mdate() - start_date );
545 vlc_mutex_lock( &p_aout->input_fifos_lock );
546 aout_FifoSet( p_aout, &p_input->fifo, 0 );
547 p_input->p_first_byte_to_mix = NULL;
548 vlc_mutex_unlock( &p_aout->input_fifos_lock );
549 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
550 msg_Warn( p_aout, "timing screwed, stopping resampling" );
551 inputResamplingStop( p_input );
555 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
557 /* The decoder gives us f*cked up PTS. It's its business, but we
558 * can't present it anyway, so drop the buffer. */
559 msg_Warn( p_aout, "PTS is out of range (%"PRId64"), dropping buffer",
560 mdate() - p_buffer->start_date );
562 inputDrop( p_aout, p_input, p_buffer );
563 inputResamplingStop( p_input );
567 /* If the audio drift is too big then it's not worth trying to resample
569 mtime_t i_pts_tolerance = 3 * AOUT_PTS_TOLERANCE * i_input_rate / INPUT_RATE_DEFAULT;
570 if ( start_date != 0 &&
571 ( start_date < p_buffer->start_date - i_pts_tolerance ) )
573 msg_Warn( p_aout, "audio drift is too big (%"PRId64"), clearing out",
574 start_date - p_buffer->start_date );
575 vlc_mutex_lock( &p_aout->input_fifos_lock );
576 aout_FifoSet( p_aout, &p_input->fifo, 0 );
577 p_input->p_first_byte_to_mix = NULL;
578 vlc_mutex_unlock( &p_aout->input_fifos_lock );
579 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
580 msg_Warn( p_aout, "timing screwed, stopping resampling" );
581 inputResamplingStop( p_input );
584 else if ( start_date != 0 &&
585 ( start_date > p_buffer->start_date + i_pts_tolerance) )
587 msg_Warn( p_aout, "audio drift is too big (%"PRId64"), dropping buffer",
588 start_date - p_buffer->start_date );
589 inputDrop( p_aout, p_input, p_buffer );
593 if ( start_date == 0 ) start_date = p_buffer->start_date;
595 #ifndef AOUT_PROCESS_BEFORE_CHEKS
596 /* Run pre-filters. */
597 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
601 /* Run the resampler if needed.
602 * We first need to calculate the output rate of this resampler. */
603 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
604 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
605 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
606 p_input->i_nb_resamplers > 0 )
608 /* Can happen in several circumstances :
609 * 1. A problem at the input (clock drift)
610 * 2. A small pause triggered by the user
611 * 3. Some delay in the output stage, causing a loss of lip
613 * Solution : resample the buffer to avoid a scratch.
615 mtime_t drift = p_buffer->start_date - start_date;
617 p_input->i_resamp_start_date = mdate();
618 p_input->i_resamp_start_drift = (int)drift;
621 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
623 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
625 msg_Warn( p_aout, "buffer is %"PRId64" %s, triggering %ssampling",
626 drift > 0 ? drift : -drift,
627 drift > 0 ? "in advance" : "late",
628 drift > 0 ? "down" : "up");
631 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
633 /* Resampling has been triggered previously (because of dates
634 * mismatch). We want the resampling to happen progressively so
635 * it isn't too audible to the listener. */
637 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
639 p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
643 p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
646 /* Check if everything is back to normal, in which case we can stop the
648 unsigned int i_nominal_rate =
649 (p_input->pp_resamplers[0] == p_input->p_playback_rate_filter)
650 ? INPUT_RATE_DEFAULT * p_input->input.i_rate / i_input_rate
651 : p_input->input.i_rate;
652 if( p_input->pp_resamplers[0]->input.i_rate == i_nominal_rate )
654 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
655 msg_Warn( p_aout, "resampling stopped after %"PRIi64" usec "
656 "(drift: %"PRIi64")",
657 mdate() - p_input->i_resamp_start_date,
658 p_buffer->start_date - start_date);
660 else if( abs( (int)(p_buffer->start_date - start_date) ) <
661 abs( p_input->i_resamp_start_drift ) / 2 )
663 /* if we reduced the drift from half, then it is time to switch
664 * back the resampling direction. */
665 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
666 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
668 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
669 p_input->i_resamp_start_drift = 0;
671 else if( p_input->i_resamp_start_drift &&
672 ( abs( (int)(p_buffer->start_date - start_date) ) >
673 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
675 /* If the drift is increasing and not decreasing, than something
676 * is bad. We'd better stop the resampling right now. */
677 msg_Warn( p_aout, "timing screwed, stopping resampling" );
678 inputResamplingStop( p_input );
682 #ifndef AOUT_PROCESS_BEFORE_CHEKS
683 /* Actually run the resampler now. */
684 if ( p_input->i_nb_resamplers > 0 )
686 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
687 p_input->i_nb_resamplers,
691 if( p_buffer->i_nb_samples <= 0 )
693 aout_BufferFree( p_buffer );
698 /* Adding the start date will be managed by aout_FifoPush(). */
699 p_buffer->end_date = start_date +
700 (p_buffer->end_date - p_buffer->start_date);
701 p_buffer->start_date = start_date;
703 vlc_mutex_lock( &p_aout->input_fifos_lock );
704 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
705 vlc_mutex_unlock( &p_aout->input_fifos_lock );
709 /*****************************************************************************
711 *****************************************************************************/
713 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
714 const char * psz_error_message )
717 msg_Err( p_aout, "%s", psz_error_message );
720 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
721 p_input->i_nb_filters );
722 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
723 p_input->i_nb_resamplers );
724 aout_FifoDestroy( p_aout, &p_input->fifo );
725 var_Destroy( p_aout, "visual" );
726 var_Destroy( p_aout, "equalizer" );
727 var_Destroy( p_aout, "audio-filter" );
728 var_Destroy( p_aout, "audio-visual" );
730 var_Destroy( p_aout, "audio-replay-gain-mode" );
731 var_Destroy( p_aout, "audio-replay-gain-default" );
732 var_Destroy( p_aout, "audio-replay-gain-preamp" );
733 var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
736 p_input->b_error = 1;
739 static void inputDrop( aout_instance_t *p_aout, aout_input_t *p_input, aout_buffer_t *p_buffer )
741 aout_BufferFree( p_buffer );
743 if( !p_input->p_input_thread )
746 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
747 stats_UpdateInteger( p_aout, p_input->p_input_thread->p->counters.p_lost_abuffers, 1, NULL );
748 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
751 static void inputResamplingStop( aout_input_t *p_input )
753 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
754 if( p_input->i_nb_resamplers != 0 )
756 p_input->pp_resamplers[0]->input.i_rate =
757 ( p_input->pp_resamplers[0] == p_input->p_playback_rate_filter )
758 ? INPUT_RATE_DEFAULT * p_input->input.i_rate / p_input->i_last_input_rate
759 : p_input->input.i_rate;
760 p_input->pp_resamplers[0]->b_continuity = false;
764 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
765 const char *psz_name, bool b_add )
770 var_Get( p_aout, psz_variable, &val );
772 if( !val.psz_string ) val.psz_string = strdup("");
774 psz_parser = strstr( val.psz_string, psz_name );
780 psz_parser = val.psz_string;
781 asprintf( &val.psz_string, (*val.psz_string) ? "%s:%s" : "%s%s",
782 val.psz_string, psz_name );
794 memmove( psz_parser, psz_parser + strlen(psz_name) +
795 (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
796 strlen(psz_parser + strlen(psz_name)) + 1 );
800 free( val.psz_string );
805 var_Set( p_aout, psz_variable, val );
806 free( val.psz_string );
810 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
811 vlc_value_t oldval, vlc_value_t newval, void *p_data )
813 aout_instance_t *p_aout = (aout_instance_t *)p_this;
814 char *psz_mode = newval.psz_string;
817 (void)psz_cmd; (void)oldval; (void)p_data;
819 if( !psz_mode || !*psz_mode )
821 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
822 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
823 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false );
827 if( !strcmp( "goom", psz_mode ) )
829 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
830 ChangeFiltersString( p_aout, "audio-visual", "goom", true );
831 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
833 else if( !strcmp( "galaktos", psz_mode ) )
835 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
836 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
837 ChangeFiltersString( p_aout, "audio-visual", "galaktos", true );
841 val.psz_string = psz_mode;
842 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
843 var_Set( p_aout, "effect-list", val );
845 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
846 ChangeFiltersString( p_aout, "audio-visual", "visual", true );
847 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
852 for( i = 0; i < p_aout->i_nb_inputs; i++ )
854 p_aout->pp_inputs[i]->b_restart = true;
860 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
861 vlc_value_t oldval, vlc_value_t newval, void *p_data )
863 aout_instance_t *p_aout = (aout_instance_t *)p_this;
864 char *psz_mode = newval.psz_string;
868 (void)psz_cmd; (void)oldval; (void)p_data;
870 if( !psz_mode || !*psz_mode )
872 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
877 val.psz_string = psz_mode;
878 var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
879 var_Set( p_aout, "equalizer-preset", val );
880 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
888 for( i = 0; i < p_aout->i_nb_inputs; i++ )
890 p_aout->pp_inputs[i]->b_restart = true;
897 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
898 vlc_value_t oldval, vlc_value_t newval, void *p_data )
900 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
901 VLC_UNUSED(newval); VLC_UNUSED(p_data);
902 aout_instance_t *p_aout = (aout_instance_t *)p_this;
905 vlc_mutex_lock( &p_aout->mixer_lock );
906 for( i = 0; i < p_aout->i_nb_inputs; i++ )
907 ReplayGainSelect( p_aout, p_aout->pp_inputs[i] );
909 /* Restart the mixer (a trivial mixer may be in use) */
910 aout_MixerMultiplierSet( p_aout, p_aout->mixer.f_multiplier );
911 vlc_mutex_unlock( &p_aout->mixer_lock );
916 static void ReplayGainSelect( aout_instance_t *p_aout, aout_input_t *p_input )
918 char *psz_replay_gain = var_GetNonEmptyString( p_aout,
919 "audio-replay-gain-mode" );
924 p_input->f_multiplier = 1.0;
926 if( !psz_replay_gain )
929 /* Find select mode */
930 if( !strcmp( psz_replay_gain, "track" ) )
931 i_mode = AUDIO_REPLAY_GAIN_TRACK;
932 else if( !strcmp( psz_replay_gain, "album" ) )
933 i_mode = AUDIO_REPLAY_GAIN_ALBUM;
935 i_mode = AUDIO_REPLAY_GAIN_MAX;
937 /* If the select mode is not available, prefer the other one */
939 if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
941 for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
943 if( p_input->replay_gain.pb_gain[i_use] )
949 if( i_use != AUDIO_REPLAY_GAIN_MAX )
950 f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
951 else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
952 f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
955 p_input->f_multiplier = pow( 10.0, f_gain / 20.0 );
958 if( p_input->replay_gain.pb_peak[i_use] &&
959 var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
960 p_input->replay_gain.pf_peak[i_use] * p_input->f_multiplier > 1.0 )
962 p_input->f_multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
965 free( psz_replay_gain );