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 * );
67 /*****************************************************************************
68 * aout_InputNew : allocate a new input and rework the filter pipeline
69 *****************************************************************************/
70 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input, const aout_request_vout_t *p_request_vout )
72 audio_sample_format_t chain_input_format;
73 audio_sample_format_t chain_output_format;
74 vlc_value_t val, text;
75 char *psz_filters, *psz_visual, *psz_scaletempo;
78 aout_FormatPrint( p_aout, "input", &p_input->input );
80 p_input->i_nb_resamplers = p_input->i_nb_filters = 0;
83 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
84 p_input->p_first_byte_to_mix = NULL;
89 p_input->request_vout = *p_request_vout;
93 p_input->request_vout.pf_request_vout = RequestVout;
94 p_input->request_vout.p_private = p_aout;
97 /* Prepare format structure */
98 memcpy( &chain_input_format, &p_input->input,
99 sizeof(audio_sample_format_t) );
100 memcpy( &chain_output_format, &p_aout->mixer.mixer,
101 sizeof(audio_sample_format_t) );
102 chain_output_format.i_rate = p_input->input.i_rate;
103 aout_FormatPrepare( &chain_output_format );
105 /* Now add user filters */
106 if( var_Type( p_aout, "visual" ) == 0 )
108 var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
109 text.psz_string = _("Visualizations");
110 var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
111 val.psz_string = (char*)""; text.psz_string = _("Disable");
112 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
113 val.psz_string = (char*)"spectrometer"; text.psz_string = _("Spectrometer");
114 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
115 val.psz_string = (char*)"scope"; text.psz_string = _("Scope");
116 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
117 val.psz_string = (char*)"spectrum"; text.psz_string = _("Spectrum");
118 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
119 val.psz_string = (char*)"vuMeter"; text.psz_string = _("Vu meter");
120 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
122 /* Look for goom plugin */
123 if( module_exists( "goom" ) )
125 val.psz_string = (char*)"goom"; text.psz_string = (char*)"Goom";
126 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
129 /* Look for galaktos plugin */
130 if( module_exists( "galaktos" ) )
132 val.psz_string = (char*)"galaktos"; text.psz_string = (char*)"GaLaktos";
133 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
136 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
138 var_SetString( p_aout, "visual", val.psz_string );
139 free( val.psz_string );
141 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
144 if( var_Type( p_aout, "equalizer" ) == 0 )
146 module_config_t *p_config;
149 p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
150 if( p_config && p_config->i_list )
152 var_Create( p_aout, "equalizer",
153 VLC_VAR_STRING | VLC_VAR_HASCHOICE );
154 text.psz_string = _("Equalizer");
155 var_Change( p_aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL );
157 val.psz_string = (char*)""; text.psz_string = _("Disable");
158 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text );
160 for( i = 0; i < p_config->i_list; i++ )
162 val.psz_string = (char *)p_config->ppsz_list[i];
163 text.psz_string = (char *)p_config->ppsz_list_text[i];
164 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE,
168 var_AddCallback( p_aout, "equalizer", EqualizerCallback, NULL );
172 if( var_Type( p_aout, "audio-filter" ) == 0 )
174 var_Create( p_aout, "audio-filter",
175 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
176 text.psz_string = _("Audio filters");
177 var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
179 if( var_Type( p_aout, "audio-visual" ) == 0 )
181 var_Create( p_aout, "audio-visual",
182 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
183 text.psz_string = _("Audio visualizations");
184 var_Change( p_aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL );
187 if( var_Type( p_aout, "audio-replay-gain-mode" ) == 0 )
189 module_config_t *p_config;
192 p_config = config_FindConfig( VLC_OBJECT(p_aout), "audio-replay-gain-mode" );
193 if( p_config && p_config->i_list )
195 var_Create( p_aout, "audio-replay-gain-mode",
196 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
198 text.psz_string = _("Replay gain");
199 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL );
201 for( i = 0; i < p_config->i_list; i++ )
203 val.psz_string = (char *)p_config->ppsz_list[i];
204 text.psz_string = (char *)p_config->ppsz_list_text[i];
205 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
209 var_AddCallback( p_aout, "audio-replay-gain-mode", ReplayGainCallback, NULL );
212 if( var_Type( p_aout, "audio-replay-gain-preamp" ) == 0 )
214 var_Create( p_aout, "audio-replay-gain-preamp",
215 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
217 if( var_Type( p_aout, "audio-replay-gain-default" ) == 0 )
219 var_Create( p_aout, "audio-replay-gain-default",
220 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
222 if( var_Type( p_aout, "audio-replay-gain-peak-protection" ) == 0 )
224 var_Create( p_aout, "audio-replay-gain-peak-protection",
225 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
227 if( var_Type( p_aout, "audio-time-stretch" ) == 0 )
229 var_Create( p_aout, "audio-time-stretch",
230 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
233 psz_filters = var_GetString( p_aout, "audio-filter" );
234 psz_visual = var_GetString( p_aout, "audio-visual");
236 psz_scaletempo = var_GetBool( p_aout, "audio-time-stretch" ) ? strdup( "scaletempo" ) : NULL;
238 /* parse user filter lists */
239 for( i_visual = 0; i_visual < 3 && !AOUT_FMT_NON_LINEAR(&chain_output_format); i_visual++ )
241 char *ppsz_array[] = { psz_scaletempo, psz_filters, psz_visual };
242 char *psz_next = NULL;
243 char *psz_parser = ppsz_array[i_visual];
245 if( psz_parser == NULL || !*psz_parser )
248 while( psz_parser && *psz_parser )
250 aout_filter_t * p_filter = NULL;
252 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
254 msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
258 while( *psz_parser == ' ' && *psz_parser == ':' )
262 if( ( psz_next = strchr( psz_parser , ':' ) ) )
266 if( *psz_parser =='\0' )
271 /* Create a VLC object */
272 static const char typename[] = "audio filter";
273 p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
274 VLC_OBJECT_GENERIC, typename );
275 if( p_filter == NULL )
277 msg_Err( p_aout, "cannot add user filter %s (skipped)",
279 psz_parser = psz_next;
283 vlc_object_attach( p_filter , p_aout );
285 p_filter->request_vout = p_input->request_vout;
286 p_filter->p_owner = malloc( sizeof(*p_filter->p_owner) );
287 p_filter->p_owner->p_aout = p_aout;
288 p_filter->p_owner->p_input = p_input;
290 /* try to find the requested filter */
291 if( i_visual == 2 ) /* this can only be a visualization module */
294 memcpy( &p_filter->input, &chain_output_format,
295 sizeof(audio_sample_format_t) );
296 memcpy( &p_filter->output, &chain_output_format,
297 sizeof(audio_sample_format_t) );
299 p_filter->p_module = module_need( p_filter, "visualization",
302 else /* this can be a audio filter module as well as a visualization module */
305 memcpy( &p_filter->input, &chain_input_format,
306 sizeof(audio_sample_format_t) );
307 memcpy( &p_filter->output, &chain_output_format,
308 sizeof(audio_sample_format_t) );
310 p_filter->p_module = module_need( p_filter, "audio filter",
313 if ( p_filter->p_module == NULL )
315 /* if the filter requested a special format, retry */
316 if ( !( AOUT_FMTS_IDENTICAL( &p_filter->input,
317 &chain_input_format )
318 && AOUT_FMTS_IDENTICAL( &p_filter->output,
319 &chain_output_format ) ) )
321 aout_FormatPrepare( &p_filter->input );
322 aout_FormatPrepare( &p_filter->output );
323 p_filter->p_module = module_need( p_filter,
327 /* try visual filters */
330 memcpy( &p_filter->input, &chain_output_format,
331 sizeof(audio_sample_format_t) );
332 memcpy( &p_filter->output, &chain_output_format,
333 sizeof(audio_sample_format_t) );
334 p_filter->p_module = module_need( p_filter,
342 if ( p_filter->p_module == NULL )
344 msg_Err( p_aout, "cannot add user filter %s (skipped)",
347 free( p_filter->p_owner );
348 vlc_object_detach( p_filter );
349 vlc_object_release( p_filter );
351 psz_parser = psz_next;
355 /* complete the filter chain if necessary */
356 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &p_filter->input ) )
358 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
359 &p_input->i_nb_filters,
361 &p_filter->input ) < 0 )
363 msg_Err( p_aout, "cannot add user filter %s (skipped)",
366 module_unneed( p_filter, p_filter->p_module );
367 free( p_filter->p_owner );
368 vlc_object_detach( p_filter );
369 vlc_object_release( p_filter );
371 psz_parser = psz_next;
377 p_filter->b_continuity = false;
378 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
379 memcpy( &chain_input_format, &p_filter->output,
380 sizeof( audio_sample_format_t ) );
382 /* next filter if any */
383 psz_parser = psz_next;
388 free( psz_scaletempo );
390 /* complete the filter chain if necessary */
391 if ( !AOUT_FMTS_IDENTICAL( &chain_input_format, &chain_output_format ) )
393 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
394 &p_input->i_nb_filters,
396 &chain_output_format ) < 0 )
398 inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
403 /* Prepare hints for the buffer allocator. */
404 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
405 p_input->input_alloc.i_bytes_per_sec = -1;
407 /* Create resamplers. */
408 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
410 chain_output_format.i_rate = (__MAX(p_input->input.i_rate,
411 p_aout->mixer.mixer.i_rate)
412 * (100 + AOUT_MAX_RESAMPLING)) / 100;
413 if ( chain_output_format.i_rate == p_aout->mixer.mixer.i_rate )
415 /* Just in case... */
416 chain_output_format.i_rate++;
418 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
419 &p_input->i_nb_resamplers,
420 &chain_output_format,
421 &p_aout->mixer.mixer ) < 0 )
423 inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
427 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
428 p_input->i_nb_resamplers,
429 &p_input->input_alloc );
430 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
432 /* Setup the initial rate of the resampler */
433 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
435 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
437 p_input->p_playback_rate_filter = NULL;
438 for( int i = 0; i < p_input->i_nb_filters; i++ )
440 aout_filter_t *p_filter = p_input->pp_filters[i];
441 if( strcmp( "scaletempo", p_filter->psz_object_name ) == 0 )
443 p_input->p_playback_rate_filter = p_filter;
447 if( ! p_input->p_playback_rate_filter && p_input->i_nb_resamplers > 0 )
449 p_input->p_playback_rate_filter = p_input->pp_resamplers[0];
452 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
453 p_input->i_nb_filters,
454 &p_input->input_alloc );
455 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
457 /* i_bytes_per_sec is still == -1 if no filters */
458 p_input->input_alloc.i_bytes_per_sec = __MAX(
459 p_input->input_alloc.i_bytes_per_sec,
460 (int)(p_input->input.i_bytes_per_frame
461 * p_input->input.i_rate
462 / p_input->input.i_frame_length) );
464 ReplayGainSelect( p_aout, p_input );
467 p_input->b_error = false;
468 p_input->b_restart = false;
469 p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
474 /*****************************************************************************
475 * aout_InputDelete : delete an input
476 *****************************************************************************
477 * This function must be entered with the mixer lock.
478 *****************************************************************************/
479 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
481 AOUT_ASSERT_MIXER_LOCKED;
482 if ( p_input->b_error ) return 0;
484 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
485 p_input->i_nb_filters );
486 p_input->i_nb_filters = 0;
487 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
488 p_input->i_nb_resamplers );
489 p_input->i_nb_resamplers = 0;
490 aout_FifoDestroy( p_aout, &p_input->fifo );
495 /*****************************************************************************
496 * aout_InputPlay : play a buffer
497 *****************************************************************************
498 * This function must be entered with the input lock.
499 *****************************************************************************/
500 /* XXX Do not activate it !! */
501 //#define AOUT_PROCESS_BEFORE_CHEKS
502 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
503 aout_buffer_t * p_buffer, int i_input_rate )
506 AOUT_ASSERT_INPUT_LOCKED;
508 if( p_input->b_restart )
511 uint8_t *p_first_byte_to_mix;
513 mtime_t i_pause_date;
515 aout_lock_mixer( p_aout );
516 aout_lock_input_fifos( p_aout );
518 /* A little trick to avoid loosing our input fifo and properties */
520 p_first_byte_to_mix = p_input->p_first_byte_to_mix;
521 fifo = p_input->fifo;
522 b_paused = p_input->b_paused;
523 i_pause_date = p_input->i_pause_date;
525 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
527 aout_InputDelete( p_aout, p_input );
529 aout_InputNew( p_aout, p_input, &p_input->request_vout );
530 p_input->p_first_byte_to_mix = p_first_byte_to_mix;
531 p_input->fifo = fifo;
532 p_input->b_paused = b_paused;
533 p_input->i_pause_date = i_pause_date;
535 aout_unlock_input_fifos( p_aout );
536 aout_unlock_mixer( p_aout );
539 if( i_input_rate != INPUT_RATE_DEFAULT && p_input->p_playback_rate_filter == NULL )
541 inputDrop( p_input, p_buffer );
545 #ifdef AOUT_PROCESS_BEFORE_CHEKS
546 /* Run pre-filters. */
547 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
550 /* Actually run the resampler now. */
551 if ( p_input->i_nb_resamplers > 0 )
553 const mtime_t i_date = p_buffer->start_date;
554 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
555 p_input->i_nb_resamplers,
559 if( p_buffer->i_nb_samples <= 0 )
561 aout_BufferFree( p_buffer );
566 /* Handle input rate change, but keep drift correction */
567 if( i_input_rate != p_input->i_last_input_rate )
569 unsigned int * const pi_rate = &p_input->p_playback_rate_filter->input.i_rate;
570 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
571 const int i_delta = *pi_rate - F(p_input->input.i_rate,p_input->i_last_input_rate);
572 *pi_rate = F(p_input->input.i_rate + i_delta, i_input_rate);
574 p_input->i_last_input_rate = i_input_rate;
577 /* We don't care if someone changes the start date behind our back after
578 * this. We'll deal with that when pushing the buffer, and compensate
579 * with the next incoming buffer. */
580 aout_lock_input_fifos( p_aout );
581 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
582 aout_unlock_input_fifos( p_aout );
584 if ( start_date != 0 && start_date < mdate() )
586 /* The decoder is _very_ late. This can only happen if the user
587 * pauses the stream (or if the decoder is buggy, which cannot
589 msg_Warn( p_aout, "computed PTS is out of range (%"PRId64"), "
590 "clearing out", mdate() - start_date );
591 aout_lock_input_fifos( p_aout );
592 aout_FifoSet( p_aout, &p_input->fifo, 0 );
593 p_input->p_first_byte_to_mix = NULL;
594 aout_unlock_input_fifos( p_aout );
595 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
596 msg_Warn( p_aout, "timing screwed, stopping resampling" );
597 inputResamplingStop( p_input );
601 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
603 /* The decoder gives us f*cked up PTS. It's its business, but we
604 * can't present it anyway, so drop the buffer. */
605 msg_Warn( p_aout, "PTS is out of range (%"PRId64"), dropping buffer",
606 mdate() - p_buffer->start_date );
608 inputDrop( p_input, p_buffer );
609 inputResamplingStop( p_input );
613 /* If the audio drift is too big then it's not worth trying to resample
615 mtime_t i_pts_tolerance = 3 * AOUT_PTS_TOLERANCE * i_input_rate / INPUT_RATE_DEFAULT;
616 if ( start_date != 0 &&
617 ( start_date < p_buffer->start_date - i_pts_tolerance ) )
619 msg_Warn( p_aout, "audio drift is too big (%"PRId64"), clearing out",
620 start_date - p_buffer->start_date );
621 aout_lock_input_fifos( p_aout );
622 aout_FifoSet( p_aout, &p_input->fifo, 0 );
623 p_input->p_first_byte_to_mix = NULL;
624 aout_unlock_input_fifos( p_aout );
625 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
626 msg_Warn( p_aout, "timing screwed, stopping resampling" );
627 inputResamplingStop( p_input );
630 else if ( start_date != 0 &&
631 ( start_date > p_buffer->start_date + i_pts_tolerance) )
633 msg_Warn( p_aout, "audio drift is too big (%"PRId64"), dropping buffer",
634 start_date - p_buffer->start_date );
635 inputDrop( p_input, p_buffer );
639 if ( start_date == 0 ) start_date = p_buffer->start_date;
641 #ifndef AOUT_PROCESS_BEFORE_CHEKS
642 /* Run pre-filters. */
643 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
647 /* Run the resampler if needed.
648 * We first need to calculate the output rate of this resampler. */
649 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
650 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
651 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
652 p_input->i_nb_resamplers > 0 )
654 /* Can happen in several circumstances :
655 * 1. A problem at the input (clock drift)
656 * 2. A small pause triggered by the user
657 * 3. Some delay in the output stage, causing a loss of lip
659 * Solution : resample the buffer to avoid a scratch.
661 mtime_t drift = p_buffer->start_date - start_date;
663 p_input->i_resamp_start_date = mdate();
664 p_input->i_resamp_start_drift = (int)drift;
667 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
669 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
671 msg_Warn( p_aout, "buffer is %"PRId64" %s, triggering %ssampling",
672 drift > 0 ? drift : -drift,
673 drift > 0 ? "in advance" : "late",
674 drift > 0 ? "down" : "up");
677 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
679 /* Resampling has been triggered previously (because of dates
680 * mismatch). We want the resampling to happen progressively so
681 * it isn't too audible to the listener. */
683 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
685 p_input->pp_resamplers[0]->input.i_rate += 2; /* Hz */
689 p_input->pp_resamplers[0]->input.i_rate -= 2; /* Hz */
692 /* Check if everything is back to normal, in which case we can stop the
694 unsigned int i_nominal_rate =
695 (p_input->pp_resamplers[0] == p_input->p_playback_rate_filter)
696 ? INPUT_RATE_DEFAULT * p_input->input.i_rate / i_input_rate
697 : p_input->input.i_rate;
698 if( p_input->pp_resamplers[0]->input.i_rate == i_nominal_rate )
700 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
701 msg_Warn( p_aout, "resampling stopped after %"PRIi64" usec "
702 "(drift: %"PRIi64")",
703 mdate() - p_input->i_resamp_start_date,
704 p_buffer->start_date - start_date);
706 else if( abs( (int)(p_buffer->start_date - start_date) ) <
707 abs( p_input->i_resamp_start_drift ) / 2 )
709 /* if we reduced the drift from half, then it is time to switch
710 * back the resampling direction. */
711 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
712 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
714 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
715 p_input->i_resamp_start_drift = 0;
717 else if( p_input->i_resamp_start_drift &&
718 ( abs( (int)(p_buffer->start_date - start_date) ) >
719 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
721 /* If the drift is increasing and not decreasing, than something
722 * is bad. We'd better stop the resampling right now. */
723 msg_Warn( p_aout, "timing screwed, stopping resampling" );
724 inputResamplingStop( p_input );
728 #ifndef AOUT_PROCESS_BEFORE_CHEKS
729 /* Actually run the resampler now. */
730 if ( p_input->i_nb_resamplers > 0 )
732 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
733 p_input->i_nb_resamplers,
737 if( p_buffer->i_nb_samples <= 0 )
739 aout_BufferFree( p_buffer );
744 /* Adding the start date will be managed by aout_FifoPush(). */
745 p_buffer->end_date = start_date +
746 (p_buffer->end_date - p_buffer->start_date);
747 p_buffer->start_date = start_date;
749 aout_lock_input_fifos( p_aout );
750 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
751 aout_unlock_input_fifos( p_aout );
755 /*****************************************************************************
757 *****************************************************************************/
759 static void inputFailure( aout_instance_t * p_aout, aout_input_t * p_input,
760 const char * psz_error_message )
763 msg_Err( p_aout, "%s", psz_error_message );
766 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
767 p_input->i_nb_filters );
768 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
769 p_input->i_nb_resamplers );
770 aout_FifoDestroy( p_aout, &p_input->fifo );
771 var_Destroy( p_aout, "visual" );
772 var_Destroy( p_aout, "equalizer" );
773 var_Destroy( p_aout, "audio-filter" );
774 var_Destroy( p_aout, "audio-visual" );
776 var_Destroy( p_aout, "audio-replay-gain-mode" );
777 var_Destroy( p_aout, "audio-replay-gain-default" );
778 var_Destroy( p_aout, "audio-replay-gain-preamp" );
779 var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
782 p_input->b_error = 1;
785 static void inputDrop( aout_input_t *p_input, aout_buffer_t *p_buffer )
787 aout_BufferFree( p_buffer );
789 p_input->i_buffer_lost++;
792 static void inputResamplingStop( aout_input_t *p_input )
794 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
795 if( p_input->i_nb_resamplers != 0 )
797 p_input->pp_resamplers[0]->input.i_rate =
798 ( p_input->pp_resamplers[0] == p_input->p_playback_rate_filter )
799 ? INPUT_RATE_DEFAULT * p_input->input.i_rate / p_input->i_last_input_rate
800 : p_input->input.i_rate;
801 p_input->pp_resamplers[0]->b_continuity = false;
805 static vout_thread_t *RequestVout( void *p_private,
806 vout_thread_t *p_vout, video_format_t *p_fmt )
808 aout_instance_t *p_aout = p_private;
809 return vout_Request( p_aout, p_vout, p_fmt );
812 static int ChangeFiltersString( aout_instance_t * p_aout, const char* psz_variable,
813 const char *psz_name, bool b_add )
815 return AoutChangeFilterString( VLC_OBJECT(p_aout), p_aout,
816 psz_variable, psz_name, b_add ) ? 1 : 0;
819 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
820 vlc_value_t oldval, vlc_value_t newval, void *p_data )
822 aout_instance_t *p_aout = (aout_instance_t *)p_this;
823 char *psz_mode = newval.psz_string;
824 (void)psz_cmd; (void)oldval; (void)p_data;
826 if( !psz_mode || !*psz_mode )
828 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
829 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
830 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false );
834 if( !strcmp( "goom", psz_mode ) )
836 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
837 ChangeFiltersString( p_aout, "audio-visual", "goom", true );
838 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
840 else if( !strcmp( "galaktos", psz_mode ) )
842 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
843 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
844 ChangeFiltersString( p_aout, "audio-visual", "galaktos", true );
848 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
849 var_SetString( p_aout, "effect-list", psz_mode );
851 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
852 ChangeFiltersString( p_aout, "audio-visual", "visual", true );
853 ChangeFiltersString( p_aout, "audio-visual", "galaktos", false);
858 AoutInputsMarkToRestart( p_aout );
863 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
864 vlc_value_t oldval, vlc_value_t newval, void *p_data )
866 aout_instance_t *p_aout = (aout_instance_t *)p_this;
867 char *psz_mode = newval.psz_string;
869 (void)psz_cmd; (void)oldval; (void)p_data;
871 if( !psz_mode || !*psz_mode )
873 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
878 var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
879 var_SetString( p_aout, "equalizer-preset", psz_mode );
880 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
886 AoutInputsMarkToRestart( p_aout );
890 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
891 vlc_value_t oldval, vlc_value_t newval, void *p_data )
893 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
894 VLC_UNUSED(newval); VLC_UNUSED(p_data);
895 aout_instance_t *p_aout = (aout_instance_t *)p_this;
898 aout_lock_mixer( p_aout );
899 for( i = 0; i < p_aout->i_nb_inputs; i++ )
900 ReplayGainSelect( p_aout, p_aout->pp_inputs[i] );
902 /* Restart the mixer (a trivial mixer may be in use) */
903 aout_MixerMultiplierSet( p_aout, p_aout->mixer.f_multiplier );
904 aout_unlock_mixer( p_aout );
909 static void ReplayGainSelect( aout_instance_t *p_aout, aout_input_t *p_input )
911 char *psz_replay_gain = var_GetNonEmptyString( p_aout,
912 "audio-replay-gain-mode" );
917 p_input->f_multiplier = 1.0;
919 if( !psz_replay_gain )
922 /* Find select mode */
923 if( !strcmp( psz_replay_gain, "track" ) )
924 i_mode = AUDIO_REPLAY_GAIN_TRACK;
925 else if( !strcmp( psz_replay_gain, "album" ) )
926 i_mode = AUDIO_REPLAY_GAIN_ALBUM;
928 i_mode = AUDIO_REPLAY_GAIN_MAX;
930 /* If the select mode is not available, prefer the other one */
932 if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
934 for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
936 if( p_input->replay_gain.pb_gain[i_use] )
942 if( i_use != AUDIO_REPLAY_GAIN_MAX )
943 f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
944 else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
945 f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
948 p_input->f_multiplier = pow( 10.0, f_gain / 20.0 );
951 if( p_input->replay_gain.pb_peak[i_use] &&
952 var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
953 p_input->replay_gain.pf_peak[i_use] * p_input->f_multiplier > 1.0 )
955 p_input->f_multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
958 free( psz_replay_gain );