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>
39 #include <vlc_input.h>
40 #include <vlc_vout.h> /* for vout_Request */
48 #include "aout_internal.h"
50 #define AOUT_ASSERT_MIXER_LOCKED vlc_assert_locked( &p_aout->mixer_lock )
51 #define AOUT_ASSERT_INPUT_LOCKED vlc_assert_locked( &p_input->lock )
53 static void inputFailure( aout_instance_t *, aout_input_t *, const char * );
54 static void inputDrop( aout_input_t *, aout_buffer_t * );
55 static void inputResamplingStop( aout_input_t *p_input );
57 static int VisualizationCallback( vlc_object_t *, char const *,
58 vlc_value_t, vlc_value_t, void * );
59 static int EqualizerCallback( vlc_object_t *, char const *,
60 vlc_value_t, vlc_value_t, void * );
61 static int ReplayGainCallback( vlc_object_t *, char const *,
62 vlc_value_t, vlc_value_t, void * );
63 static void ReplayGainSelect( aout_instance_t *, aout_input_t * );
65 static vout_thread_t *RequestVout( void *,
66 vout_thread_t *, video_format_t *, bool );
67 static vout_thread_t *RequestVoutFromFilter( void *,
68 vout_thread_t *, video_format_t *, bool );
70 /*****************************************************************************
71 * aout_InputNew : allocate a new input and rework the filter pipeline
72 *****************************************************************************/
73 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input, const aout_request_vout_t *p_request_vout )
75 audio_sample_format_t chain_input_format;
76 audio_sample_format_t chain_output_format;
77 vlc_value_t val, text;
78 char *psz_filters, *psz_visual, *psz_scaletempo;
81 aout_FormatPrint( p_aout, "input", &p_input->input );
83 p_input->i_nb_resamplers = p_input->i_nb_filters = 0;
86 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
87 p_input->p_first_byte_to_mix = NULL;
92 p_input->request_vout = *p_request_vout;
96 p_input->request_vout.pf_request_vout = RequestVout;
97 p_input->request_vout.p_private = p_aout;
100 /* Prepare format structure */
101 memcpy( &chain_input_format, &p_input->input,
102 sizeof(audio_sample_format_t) );
103 memcpy( &chain_output_format, &p_aout->mixer.mixer,
104 sizeof(audio_sample_format_t) );
105 chain_output_format.i_rate = p_input->input.i_rate;
106 aout_FormatPrepare( &chain_output_format );
108 /* Now add user filters */
109 if( var_Type( p_aout, "visual" ) == 0 )
111 var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
112 text.psz_string = _("Visualizations");
113 var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
114 val.psz_string = (char*)""; text.psz_string = _("Disable");
115 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
116 val.psz_string = (char*)"spectrometer"; text.psz_string = _("Spectrometer");
117 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
118 val.psz_string = (char*)"scope"; text.psz_string = _("Scope");
119 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
120 val.psz_string = (char*)"spectrum"; text.psz_string = _("Spectrum");
121 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
122 val.psz_string = (char*)"vuMeter"; text.psz_string = _("Vu meter");
123 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
125 /* Look for goom plugin */
126 if( module_exists( "goom" ) )
128 val.psz_string = (char*)"goom"; text.psz_string = (char*)"Goom";
129 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
132 /* Look for galaktos plugin */
133 if( module_exists( "galaktos" ) )
135 val.psz_string = (char*)"galaktos"; text.psz_string = (char*)"GaLaktos";
136 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
139 /* Look for libprojectM plugin */
140 if( module_exists( "projectm" ) )
142 val.psz_string = (char*)"projectm"; text.psz_string = (char*)"projectM";
143 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
146 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
148 var_SetString( p_aout, "visual", val.psz_string );
149 free( val.psz_string );
151 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
154 if( var_Type( p_aout, "equalizer" ) == 0 )
156 module_config_t *p_config;
159 p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
160 if( p_config && p_config->i_list )
162 var_Create( p_aout, "equalizer",
163 VLC_VAR_STRING | VLC_VAR_HASCHOICE );
164 text.psz_string = _("Equalizer");
165 var_Change( p_aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL );
167 val.psz_string = (char*)""; text.psz_string = _("Disable");
168 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text );
170 for( i = 0; i < p_config->i_list; i++ )
172 val.psz_string = (char *)p_config->ppsz_list[i];
173 text.psz_string = (char *)p_config->ppsz_list_text[i];
174 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE,
178 var_AddCallback( p_aout, "equalizer", EqualizerCallback, NULL );
182 if( var_Type( p_aout, "audio-filter" ) == 0 )
184 var_Create( p_aout, "audio-filter",
185 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
186 text.psz_string = _("Audio filters");
187 var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
189 if( var_Type( p_aout, "audio-visual" ) == 0 )
191 var_Create( p_aout, "audio-visual",
192 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
193 text.psz_string = _("Audio visualizations");
194 var_Change( p_aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL );
197 if( var_Type( p_aout, "audio-replay-gain-mode" ) == 0 )
199 module_config_t *p_config;
202 p_config = config_FindConfig( VLC_OBJECT(p_aout), "audio-replay-gain-mode" );
203 if( p_config && p_config->i_list )
205 var_Create( p_aout, "audio-replay-gain-mode",
206 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
208 text.psz_string = _("Replay gain");
209 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL );
211 for( i = 0; i < p_config->i_list; i++ )
213 val.psz_string = (char *)p_config->ppsz_list[i];
214 text.psz_string = (char *)p_config->ppsz_list_text[i];
215 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
219 var_AddCallback( p_aout, "audio-replay-gain-mode", ReplayGainCallback, NULL );
222 if( var_Type( p_aout, "audio-replay-gain-preamp" ) == 0 )
224 var_Create( p_aout, "audio-replay-gain-preamp",
225 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
227 if( var_Type( p_aout, "audio-replay-gain-default" ) == 0 )
229 var_Create( p_aout, "audio-replay-gain-default",
230 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
232 if( var_Type( p_aout, "audio-replay-gain-peak-protection" ) == 0 )
234 var_Create( p_aout, "audio-replay-gain-peak-protection",
235 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
237 if( var_Type( p_aout, "audio-time-stretch" ) == 0 )
239 var_Create( p_aout, "audio-time-stretch",
240 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
243 psz_filters = var_GetString( p_aout, "audio-filter" );
244 psz_visual = var_GetString( p_aout, "audio-visual");
245 psz_scaletempo = var_GetBool( p_aout, "audio-time-stretch" ) ? strdup( "scaletempo" ) : NULL;
247 p_input->b_recycle_vout = psz_visual && *psz_visual;
249 /* parse user filter lists */
250 for( i_visual = 0; i_visual < 3 && !AOUT_FMT_NON_LINEAR(&chain_output_format); i_visual++ )
252 char *ppsz_array[] = { psz_scaletempo, psz_filters, psz_visual };
253 char *psz_next = NULL;
254 char *psz_parser = ppsz_array[i_visual];
256 if( psz_parser == NULL || !*psz_parser )
259 while( psz_parser && *psz_parser )
261 aout_filter_t * p_filter = NULL;
263 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
265 msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
269 while( *psz_parser == ' ' && *psz_parser == ':' )
273 if( ( psz_next = strchr( psz_parser , ':' ) ) )
277 if( *psz_parser =='\0' )
282 /* Create a VLC object */
283 static const char typename[] = "audio filter";
284 p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
285 VLC_OBJECT_GENERIC, typename );
286 if( p_filter == NULL )
288 msg_Err( p_aout, "cannot add user filter %s (skipped)",
290 psz_parser = psz_next;
294 vlc_object_attach( p_filter , p_aout );
296 p_filter->request_vout.pf_request_vout = RequestVoutFromFilter;
297 p_filter->request_vout.p_private = p_input;
299 p_filter->p_owner = malloc( sizeof(*p_filter->p_owner) );
300 p_filter->p_owner->p_aout = p_aout;
301 p_filter->p_owner->p_input = p_input;
303 /* try to find the requested filter */
304 if( i_visual == 2 ) /* this can only be a visualization module */
307 memcpy( &p_filter->input, &chain_output_format,
308 sizeof(audio_sample_format_t) );
309 memcpy( &p_filter->output, &chain_output_format,
310 sizeof(audio_sample_format_t) );
312 p_filter->p_module = module_need( p_filter, "visualization",
315 else /* this can be a audio filter module as well as a visualization module */
318 memcpy( &p_filter->input, &chain_input_format,
319 sizeof(audio_sample_format_t) );
320 memcpy( &p_filter->output, &chain_output_format,
321 sizeof(audio_sample_format_t) );
323 p_filter->p_module = module_need( p_filter, "audio filter",
326 if ( p_filter->p_module == NULL )
328 /* if the filter requested a special format, retry */
329 if ( !( AOUT_FMTS_IDENTICAL( &p_filter->input,
330 &chain_input_format )
331 && AOUT_FMTS_IDENTICAL( &p_filter->output,
332 &chain_output_format ) ) )
334 aout_FormatPrepare( &p_filter->input );
335 aout_FormatPrepare( &p_filter->output );
336 p_filter->p_module = module_need( p_filter,
340 /* try visual filters */
343 memcpy( &p_filter->input, &chain_output_format,
344 sizeof(audio_sample_format_t) );
345 memcpy( &p_filter->output, &chain_output_format,
346 sizeof(audio_sample_format_t) );
347 p_filter->p_module = module_need( p_filter,
355 if ( p_filter->p_module == NULL )
357 msg_Err( p_aout, "cannot add user filter %s (skipped)",
360 free( p_filter->p_owner );
361 vlc_object_detach( p_filter );
362 vlc_object_release( p_filter );
364 psz_parser = psz_next;
368 /* complete the filter chain if necessary */
369 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &p_filter->input ) )
371 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
372 &p_input->i_nb_filters,
374 &p_filter->input ) < 0 )
376 msg_Err( p_aout, "cannot add user filter %s (skipped)",
379 module_unneed( p_filter, p_filter->p_module );
380 free( p_filter->p_owner );
381 vlc_object_detach( p_filter );
382 vlc_object_release( p_filter );
384 psz_parser = psz_next;
390 p_filter->b_continuity = false;
391 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
392 memcpy( &chain_input_format, &p_filter->output,
393 sizeof( audio_sample_format_t ) );
395 /* next filter if any */
396 psz_parser = psz_next;
401 free( psz_scaletempo );
403 /* complete the filter chain if necessary */
404 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &chain_output_format ) )
406 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
407 &p_input->i_nb_filters,
409 &chain_output_format ) < 0 )
411 inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
416 /* Prepare hints for the buffer allocator. */
417 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
418 p_input->input_alloc.i_bytes_per_sec = -1;
420 /* Create resamplers. */
421 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
423 chain_output_format.i_rate = (__MAX(p_input->input.i_rate,
424 p_aout->mixer.mixer.i_rate)
425 * (100 + AOUT_MAX_RESAMPLING)) / 100;
426 if ( chain_output_format.i_rate == p_aout->mixer.mixer.i_rate )
428 /* Just in case... */
429 chain_output_format.i_rate++;
431 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
432 &p_input->i_nb_resamplers,
433 &chain_output_format,
434 &p_aout->mixer.mixer ) < 0 )
436 inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
440 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
441 p_input->i_nb_resamplers,
442 &p_input->input_alloc );
443 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
445 /* Setup the initial rate of the resampler */
446 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
448 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
450 p_input->p_playback_rate_filter = NULL;
451 for( int i = 0; i < p_input->i_nb_filters; i++ )
453 aout_filter_t *p_filter = p_input->pp_filters[i];
454 if( strcmp( "scaletempo", p_filter->psz_object_name ) == 0 )
456 p_input->p_playback_rate_filter = p_filter;
460 if( ! p_input->p_playback_rate_filter && p_input->i_nb_resamplers > 0 )
462 p_input->p_playback_rate_filter = p_input->pp_resamplers[0];
465 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
466 p_input->i_nb_filters,
467 &p_input->input_alloc );
468 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
470 /* i_bytes_per_sec is still == -1 if no filters */
471 p_input->input_alloc.i_bytes_per_sec = __MAX(
472 p_input->input_alloc.i_bytes_per_sec,
473 (int)(p_input->input.i_bytes_per_frame
474 * p_input->input.i_rate
475 / p_input->input.i_frame_length) );
477 ReplayGainSelect( p_aout, p_input );
480 p_input->b_error = false;
481 p_input->b_restart = false;
482 p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
487 /*****************************************************************************
488 * aout_InputDelete : delete an input
489 *****************************************************************************
490 * This function must be entered with the mixer lock.
491 *****************************************************************************/
492 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
494 AOUT_ASSERT_MIXER_LOCKED;
495 if ( p_input->b_error )
498 /* XXX We need to update b_recycle_vout before calling aout_FiltersDestroyPipeline.
499 * FIXME They can be a race condition if audio-visual is updated between
500 * aout_InputDelete and aout_InputNew.
502 char *psz_visual = var_GetString( p_aout, "audio-visual");
503 p_input->b_recycle_vout = psz_visual && *psz_visual;
506 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
507 p_input->i_nb_filters );
508 p_input->i_nb_filters = 0;
509 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
510 p_input->i_nb_resamplers );
511 p_input->i_nb_resamplers = 0;
512 aout_FifoDestroy( p_aout, &p_input->fifo );
517 /*****************************************************************************
518 * aout_InputPlay : play a buffer
519 *****************************************************************************
520 * This function must be entered with the input lock.
521 *****************************************************************************/
522 /* XXX Do not activate it !! */
523 //#define AOUT_PROCESS_BEFORE_CHEKS
524 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
525 aout_buffer_t * p_buffer, int i_input_rate )
528 AOUT_ASSERT_INPUT_LOCKED;
530 if( p_input->b_restart )
533 uint8_t *p_first_byte_to_mix;
535 mtime_t i_pause_date;
537 aout_lock_mixer( p_aout );
538 aout_lock_input_fifos( p_aout );
540 /* A little trick to avoid loosing our input fifo and properties */
542 p_first_byte_to_mix = p_input->p_first_byte_to_mix;
543 fifo = p_input->fifo;
544 b_paused = p_input->b_paused;
545 i_pause_date = p_input->i_pause_date;
547 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
549 aout_InputDelete( p_aout, p_input );
551 aout_InputNew( p_aout, p_input, &p_input->request_vout );
552 p_input->p_first_byte_to_mix = p_first_byte_to_mix;
553 p_input->fifo = fifo;
554 p_input->b_paused = b_paused;
555 p_input->i_pause_date = i_pause_date;
557 aout_unlock_input_fifos( p_aout );
558 aout_unlock_mixer( p_aout );
561 if( i_input_rate != INPUT_RATE_DEFAULT && p_input->p_playback_rate_filter == NULL )
563 inputDrop( p_input, p_buffer );
567 #ifdef AOUT_PROCESS_BEFORE_CHEKS
568 /* Run pre-filters. */
569 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
572 /* Actually run the resampler now. */
573 if ( p_input->i_nb_resamplers > 0 )
575 const mtime_t i_date = p_buffer->start_date;
576 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
577 p_input->i_nb_resamplers,
581 if( p_buffer->i_nb_samples <= 0 )
583 aout_BufferFree( p_buffer );
588 /* Handle input rate change, but keep drift correction */
589 if( i_input_rate != p_input->i_last_input_rate )
591 unsigned int * const pi_rate = &p_input->p_playback_rate_filter->input.i_rate;
592 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
593 const int i_delta = *pi_rate - F(p_input->input.i_rate,p_input->i_last_input_rate);
594 *pi_rate = F(p_input->input.i_rate + i_delta, i_input_rate);
596 p_input->i_last_input_rate = i_input_rate;
599 /* We don't care if someone changes the start date behind our back after
600 * this. We'll deal with that when pushing the buffer, and compensate
601 * with the next incoming buffer. */
602 aout_lock_input_fifos( p_aout );
603 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
604 aout_unlock_input_fifos( p_aout );
606 if ( start_date != 0 && start_date < mdate() )
608 /* The decoder is _very_ late. This can only happen if the user
609 * pauses the stream (or if the decoder is buggy, which cannot
611 msg_Warn( p_aout, "computed PTS is out of range (%"PRId64"), "
612 "clearing out", mdate() - start_date );
613 aout_lock_input_fifos( p_aout );
614 aout_FifoSet( p_aout, &p_input->fifo, 0 );
615 p_input->p_first_byte_to_mix = NULL;
616 aout_unlock_input_fifos( p_aout );
617 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
618 msg_Warn( p_aout, "timing screwed, stopping resampling" );
619 inputResamplingStop( p_input );
623 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
625 /* The decoder gives us f*cked up PTS. It's its business, but we
626 * can't present it anyway, so drop the buffer. */
627 msg_Warn( p_aout, "PTS is out of range (%"PRId64"), dropping buffer",
628 mdate() - p_buffer->start_date );
630 inputDrop( p_input, p_buffer );
631 inputResamplingStop( p_input );
635 /* If the audio drift is too big then it's not worth trying to resample
637 mtime_t i_pts_tolerance = 3 * AOUT_PTS_TOLERANCE * i_input_rate / INPUT_RATE_DEFAULT;
638 if ( start_date != 0 &&
639 ( start_date < p_buffer->start_date - i_pts_tolerance ) )
641 msg_Warn( p_aout, "audio drift is too big (%"PRId64"), clearing out",
642 start_date - p_buffer->start_date );
643 aout_lock_input_fifos( p_aout );
644 aout_FifoSet( p_aout, &p_input->fifo, 0 );
645 p_input->p_first_byte_to_mix = NULL;
646 aout_unlock_input_fifos( p_aout );
647 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
648 msg_Warn( p_aout, "timing screwed, stopping resampling" );
649 inputResamplingStop( p_input );
652 else if ( start_date != 0 &&
653 ( start_date > p_buffer->start_date + i_pts_tolerance) )
655 msg_Warn( p_aout, "audio drift is too big (%"PRId64"), dropping buffer",
656 start_date - p_buffer->start_date );
657 inputDrop( p_input, p_buffer );
661 if ( start_date == 0 ) start_date = p_buffer->start_date;
663 #ifndef AOUT_PROCESS_BEFORE_CHEKS
664 /* Run pre-filters. */
665 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
669 /* Run the resampler if needed.
670 * We first need to calculate the output rate of this resampler. */
671 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
672 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
673 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
674 p_input->i_nb_resamplers > 0 )
676 /* Can happen in several circumstances :
677 * 1. A problem at the input (clock drift)
678 * 2. A small pause triggered by the user
679 * 3. Some delay in the output stage, causing a loss of lip
681 * Solution : resample the buffer to avoid a scratch.
683 mtime_t drift = p_buffer->start_date - start_date;
685 p_input->i_resamp_start_date = mdate();
686 p_input->i_resamp_start_drift = (int)drift;
689 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
691 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
693 msg_Warn( p_aout, "buffer is %"PRId64" %s, triggering %ssampling",
694 drift > 0 ? drift : -drift,
695 drift > 0 ? "in advance" : "late",
696 drift > 0 ? "down" : "up");
699 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
701 /* Resampling has been triggered previously (because of dates
702 * mismatch). We want the resampling to happen progressively so
703 * it isn't too audible to the listener. */
705 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
707 p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
711 p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
714 /* Check if everything is back to normal, in which case we can stop the
716 unsigned int i_nominal_rate =
717 (p_input->pp_resamplers[0] == p_input->p_playback_rate_filter)
718 ? INPUT_RATE_DEFAULT * p_input->input.i_rate / i_input_rate
719 : p_input->input.i_rate;
720 if( p_input->pp_resamplers[0]->input.i_rate == i_nominal_rate )
722 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
723 msg_Warn( p_aout, "resampling stopped after %"PRIi64" usec "
724 "(drift: %"PRIi64")",
725 mdate() - p_input->i_resamp_start_date,
726 p_buffer->start_date - start_date);
728 else if( abs( (int)(p_buffer->start_date - start_date) ) <
729 abs( p_input->i_resamp_start_drift ) / 2 )
731 /* if we reduced the drift from half, then it is time to switch
732 * back the resampling direction. */
733 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
734 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
736 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
737 p_input->i_resamp_start_drift = 0;
739 else if( p_input->i_resamp_start_drift &&
740 ( abs( (int)(p_buffer->start_date - start_date) ) >
741 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
743 /* If the drift is increasing and not decreasing, than something
744 * is bad. We'd better stop the resampling right now. */
745 msg_Warn( p_aout, "timing screwed, stopping resampling" );
746 inputResamplingStop( p_input );
750 #ifndef AOUT_PROCESS_BEFORE_CHEKS
751 /* Actually run the resampler now. */
752 if ( p_input->i_nb_resamplers > 0 )
754 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
755 p_input->i_nb_resamplers,
759 if( p_buffer->i_nb_samples <= 0 )
761 aout_BufferFree( p_buffer );
766 /* Adding the start date will be managed by aout_FifoPush(). */
767 p_buffer->end_date = start_date +
768 (p_buffer->end_date - p_buffer->start_date);
769 p_buffer->start_date = start_date;
771 aout_lock_input_fifos( p_aout );
772 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
773 aout_unlock_input_fifos( p_aout );
777 /*****************************************************************************
779 *****************************************************************************/
781 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
782 const char * psz_error_message )
785 msg_Err( p_aout, "%s", psz_error_message );
788 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
789 p_input->i_nb_filters );
790 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
791 p_input->i_nb_resamplers );
792 aout_FifoDestroy( p_aout, &p_input->fifo );
793 var_Destroy( p_aout, "visual" );
794 var_Destroy( p_aout, "equalizer" );
795 var_Destroy( p_aout, "audio-filter" );
796 var_Destroy( p_aout, "audio-visual" );
798 var_Destroy( p_aout, "audio-replay-gain-mode" );
799 var_Destroy( p_aout, "audio-replay-gain-default" );
800 var_Destroy( p_aout, "audio-replay-gain-preamp" );
801 var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
804 p_input->b_error = 1;
807 static void inputDrop( aout_input_t *p_input, aout_buffer_t *p_buffer )
809 aout_BufferFree( p_buffer );
811 p_input->i_buffer_lost++;
814 static void inputResamplingStop( aout_input_t *p_input )
816 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
817 if( p_input->i_nb_resamplers != 0 )
819 p_input->pp_resamplers[0]->input.i_rate =
820 ( p_input->pp_resamplers[0] == p_input->p_playback_rate_filter )
821 ? INPUT_RATE_DEFAULT * p_input->input.i_rate / p_input->i_last_input_rate
822 : p_input->input.i_rate;
823 p_input->pp_resamplers[0]->b_continuity = false;
827 static vout_thread_t *RequestVout( void *p_private,
828 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
830 aout_instance_t *p_aout = p_private;
831 VLC_UNUSED(b_recycle);
832 return vout_Request( p_aout, p_vout, p_fmt );
835 static vout_thread_t *RequestVoutFromFilter( void *p_private,
836 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
838 aout_input_t *p_input = p_private;
839 aout_request_vout_t *p_request = &p_input->request_vout;
841 return p_request->pf_request_vout( p_request->p_private,
842 p_vout, p_fmt, p_input->b_recycle_vout && b_recycle );
845 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
846 const char *psz_name, bool b_add )
848 return AoutChangeFilterString( VLC_OBJECT(p_aout), p_aout,
849 psz_variable, psz_name, b_add ) ? 1 : 0;
852 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
853 vlc_value_t oldval, vlc_value_t newval, void *p_data )
855 aout_instance_t *p_aout = (aout_instance_t *)p_this;
856 char *psz_mode = newval.psz_string;
857 (void)psz_cmd; (void)oldval; (void)p_data;
859 if( !psz_mode || !*psz_mode )
861 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
862 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
863 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false );
864 ChangeFiltersString( p_aout, "audio-visual", "projectm", false );
868 if( !strcmp( "goom", psz_mode ) )
870 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
871 ChangeFiltersString( p_aout, "audio-visual", "goom", true );
872 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false );
873 ChangeFiltersString( p_aout, "audio-visual", "projectm", false );
875 else if( !strcmp( "galaktos", psz_mode ) )
877 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
878 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
879 ChangeFiltersString( p_aout, "audio-visual", "galaktos", true );
881 else if( !strcmp( "projectm", psz_mode ) )
883 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
884 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
885 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false );
886 ChangeFiltersString( p_aout, "audio-visual", "projectm", true );
890 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
891 var_SetString( p_aout, "effect-list", psz_mode );
893 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
894 ChangeFiltersString( p_aout, "audio-visual", "visual", true );
895 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false );
896 ChangeFiltersString( p_aout, "audio-visual", "projectm", false );
901 AoutInputsMarkToRestart( p_aout );
906 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
907 vlc_value_t oldval, vlc_value_t newval, void *p_data )
909 aout_instance_t *p_aout = (aout_instance_t *)p_this;
910 char *psz_mode = newval.psz_string;
912 (void)psz_cmd; (void)oldval; (void)p_data;
914 if( !psz_mode || !*psz_mode )
916 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
921 var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
922 var_SetString( p_aout, "equalizer-preset", psz_mode );
923 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
929 AoutInputsMarkToRestart( p_aout );
933 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
934 vlc_value_t oldval, vlc_value_t newval, void *p_data )
936 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
937 VLC_UNUSED(newval); VLC_UNUSED(p_data);
938 aout_instance_t *p_aout = (aout_instance_t *)p_this;
941 aout_lock_mixer( p_aout );
942 for( i = 0; i < p_aout->i_nb_inputs; i++ )
943 ReplayGainSelect( p_aout, p_aout->pp_inputs[i] );
945 /* Restart the mixer (a trivial mixer may be in use) */
946 aout_MixerMultiplierSet( p_aout, p_aout->mixer.f_multiplier );
947 aout_unlock_mixer( p_aout );
952 static void ReplayGainSelect( aout_instance_t *p_aout, aout_input_t *p_input )
954 char *psz_replay_gain = var_GetNonEmptyString( p_aout,
955 "audio-replay-gain-mode" );
960 p_input->f_multiplier = 1.0;
962 if( !psz_replay_gain )
965 /* Find select mode */
966 if( !strcmp( psz_replay_gain, "track" ) )
967 i_mode = AUDIO_REPLAY_GAIN_TRACK;
968 else if( !strcmp( psz_replay_gain, "album" ) )
969 i_mode = AUDIO_REPLAY_GAIN_ALBUM;
971 i_mode = AUDIO_REPLAY_GAIN_MAX;
973 /* If the select mode is not available, prefer the other one */
975 if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
977 for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
979 if( p_input->replay_gain.pb_gain[i_use] )
985 if( i_use != AUDIO_REPLAY_GAIN_MAX )
986 f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
987 else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
988 f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
991 p_input->f_multiplier = pow( 10.0, f_gain / 20.0 );
994 if( p_input->replay_gain.pb_peak[i_use] &&
995 var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
996 p_input->replay_gain.pf_peak[i_use] * p_input->f_multiplier > 1.0 )
998 p_input->f_multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
1001 free( psz_replay_gain );