1 /*****************************************************************************
2 * input.c : internal management of input streams for the audio output
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
41 #include <vlc_input.h>
42 #include <vlc_vout.h> /* for vout_Request */
43 #include <vlc_modules.h>
46 #include <vlc_filter.h>
49 #include "aout_internal.h"
51 #define AOUT_ASSERT_LOCKED vlc_assert_locked( &p_aout->lock )
53 static void inputFailure( audio_output_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( audio_output_t *, aout_input_t * );
65 static vout_thread_t *RequestVout( void *,
66 vout_thread_t *, video_format_t *, bool );
68 /*****************************************************************************
69 * aout_InputNew : allocate a new input and rework the filter pipeline
70 *****************************************************************************/
71 int aout_InputNew( audio_output_t * p_aout, aout_input_t * p_input, const aout_request_vout_t *p_request_vout )
73 audio_sample_format_t chain_input_format;
74 audio_sample_format_t chain_output_format;
75 vlc_value_t val, text;
76 char *psz_filters, *psz_visual, *psz_scaletempo;
79 aout_FormatPrint( p_aout, "input", &p_input->input );
81 p_input->i_nb_resamplers = p_input->i_nb_filters = 0;
84 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer_format.i_rate );
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 chain_input_format = p_input->input;
99 chain_output_format = p_aout->mixer_format;
100 chain_output_format.i_rate = p_input->input.i_rate;
101 aout_FormatPrepare( &chain_output_format );
103 /* Now add user filters */
104 if( var_Type( p_aout, "visual" ) == 0 )
106 var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
107 text.psz_string = _("Visualizations");
108 var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
109 val.psz_string = (char*)""; text.psz_string = _("Disable");
110 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
111 val.psz_string = (char*)"spectrometer"; text.psz_string = _("Spectrometer");
112 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
113 val.psz_string = (char*)"scope"; text.psz_string = _("Scope");
114 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
115 val.psz_string = (char*)"spectrum"; text.psz_string = _("Spectrum");
116 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
117 val.psz_string = (char*)"vuMeter"; text.psz_string = _("Vu meter");
118 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
120 /* Look for goom plugin */
121 if( module_exists( "goom" ) )
123 val.psz_string = (char*)"goom"; text.psz_string = (char*)"Goom";
124 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
127 /* Look for libprojectM plugin */
128 if( module_exists( "projectm" ) )
130 val.psz_string = (char*)"projectm"; text.psz_string = (char*)"projectM";
131 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
134 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
136 var_SetString( p_aout, "visual", val.psz_string );
137 free( val.psz_string );
139 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
142 if( var_Type( p_aout, "equalizer" ) == 0 )
144 module_config_t *p_config;
147 p_config = config_FindConfig( VLC_OBJECT(p_aout), "equalizer-preset" );
148 if( p_config && p_config->i_list )
150 var_Create( p_aout, "equalizer",
151 VLC_VAR_STRING | VLC_VAR_HASCHOICE );
152 text.psz_string = _("Equalizer");
153 var_Change( p_aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL );
155 val.psz_string = (char*)""; text.psz_string = _("Disable");
156 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text );
158 for( i = 0; i < p_config->i_list; i++ )
160 val.psz_string = (char *)p_config->ppsz_list[i];
161 text.psz_string = (char *)p_config->ppsz_list_text[i];
162 var_Change( p_aout, "equalizer", VLC_VAR_ADDCHOICE,
166 var_AddCallback( p_aout, "equalizer", EqualizerCallback, NULL );
170 if( var_Type( p_aout, "audio-filter" ) == 0 )
172 var_Create( p_aout, "audio-filter",
173 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
174 text.psz_string = _("Audio filters");
175 var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
177 if( var_Type( p_aout, "audio-visual" ) == 0 )
179 var_Create( p_aout, "audio-visual",
180 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
181 text.psz_string = _("Audio visualizations");
182 var_Change( p_aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL );
185 if( var_Type( p_aout, "audio-replay-gain-mode" ) == 0 )
187 module_config_t *p_config;
190 p_config = config_FindConfig( VLC_OBJECT(p_aout), "audio-replay-gain-mode" );
191 if( p_config && p_config->i_list )
193 var_Create( p_aout, "audio-replay-gain-mode",
194 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
196 text.psz_string = _("Replay gain");
197 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL );
199 for( i = 0; i < p_config->i_list; i++ )
201 val.psz_string = (char *)p_config->ppsz_list[i];
202 text.psz_string = (char *)p_config->ppsz_list_text[i];
203 var_Change( p_aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
207 var_AddCallback( p_aout, "audio-replay-gain-mode", ReplayGainCallback, NULL );
210 if( var_Type( p_aout, "audio-replay-gain-preamp" ) == 0 )
212 var_Create( p_aout, "audio-replay-gain-preamp",
213 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
215 if( var_Type( p_aout, "audio-replay-gain-default" ) == 0 )
217 var_Create( p_aout, "audio-replay-gain-default",
218 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
220 if( var_Type( p_aout, "audio-replay-gain-peak-protection" ) == 0 )
222 var_Create( p_aout, "audio-replay-gain-peak-protection",
223 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
226 psz_filters = var_GetString( p_aout, "audio-filter" );
227 psz_visual = var_GetString( p_aout, "audio-visual");
228 psz_scaletempo = var_InheritBool( p_aout, "audio-time-stretch" ) ? strdup( "scaletempo" ) : NULL;
230 p_input->b_recycle_vout = psz_visual && *psz_visual;
232 /* parse user filter lists */
233 char *const ppsz_array[] = { psz_scaletempo, psz_filters, psz_visual };
234 p_input->p_playback_rate_filter = NULL;
236 for( i_visual = 0; i_visual < 3 && !AOUT_FMT_NON_LINEAR(&chain_output_format); i_visual++ )
238 char *psz_next = NULL;
239 char *psz_parser = ppsz_array[i_visual];
241 if( psz_parser == NULL || !*psz_parser )
244 while( psz_parser && *psz_parser )
246 filter_t * p_filter = NULL;
248 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
250 msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
254 while( *psz_parser == ' ' && *psz_parser == ':' )
258 if( ( psz_next = strchr( psz_parser , ':' ) ) )
262 if( *psz_parser =='\0' )
267 /* Create a VLC object */
268 p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
270 if( p_filter == NULL )
272 msg_Err( p_aout, "cannot add user filter %s (skipped)",
274 psz_parser = psz_next;
278 p_filter->p_owner = malloc( sizeof(*p_filter->p_owner) );
279 p_filter->p_owner->p_aout = p_aout;
280 p_filter->p_owner->p_input = p_input;
283 memcpy( &p_filter->fmt_in.audio, &chain_output_format,
284 sizeof(audio_sample_format_t) );
285 p_filter->fmt_in.i_codec = chain_output_format.i_format;
286 memcpy( &p_filter->fmt_out.audio, &chain_output_format,
287 sizeof(audio_sample_format_t) );
288 p_filter->fmt_out.i_codec = chain_output_format.i_format;
289 p_filter->pf_audio_buffer_new = aout_FilterBufferNew;
291 /* try to find the requested filter */
292 if( i_visual == 2 ) /* this can only be a visualization module */
294 p_filter->p_module = module_need( p_filter, "visualization2",
297 else /* this can be a audio filter module as well as a visualization module */
299 p_filter->p_module = module_need( p_filter, "audio filter",
302 if ( p_filter->p_module == NULL )
304 /* if the filter requested a special format, retry */
305 if ( !( AOUT_FMTS_IDENTICAL( &p_filter->fmt_in.audio,
306 &chain_input_format )
307 && AOUT_FMTS_IDENTICAL( &p_filter->fmt_out.audio,
308 &chain_output_format ) ) )
310 aout_FormatPrepare( &p_filter->fmt_in.audio );
311 aout_FormatPrepare( &p_filter->fmt_out.audio );
312 p_filter->p_module = module_need( p_filter,
316 /* try visual filters */
319 memcpy( &p_filter->fmt_in.audio, &chain_output_format,
320 sizeof(audio_sample_format_t) );
321 memcpy( &p_filter->fmt_out.audio, &chain_output_format,
322 sizeof(audio_sample_format_t) );
323 p_filter->p_module = module_need( p_filter,
331 if ( p_filter->p_module == NULL )
333 msg_Err( p_aout, "cannot add user filter %s (skipped)",
336 free( p_filter->p_owner );
337 vlc_object_release( p_filter );
339 psz_parser = psz_next;
343 /* complete the filter chain if necessary */
344 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
345 &p_input->i_nb_filters,
347 &p_filter->fmt_in.audio ) < 0 )
349 msg_Err( p_aout, "cannot add user filter %s (skipped)",
352 module_unneed( p_filter, p_filter->p_module );
353 free( p_filter->p_owner );
354 vlc_object_release( p_filter );
356 psz_parser = psz_next;
361 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
362 memcpy( &chain_input_format, &p_filter->fmt_out.audio,
363 sizeof( audio_sample_format_t ) );
365 if( i_visual == 0 ) /* scaletempo */
366 p_input->p_playback_rate_filter = p_filter;
368 /* next filter if any */
369 psz_parser = psz_next;
374 free( psz_scaletempo );
376 /* complete the filter chain if necessary */
377 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
378 &p_input->i_nb_filters,
380 &chain_output_format ) < 0 )
382 inputFailure( p_aout, p_input, "couldn't set an input pipeline" );
386 /* Create resamplers. */
387 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer_format ) )
389 chain_output_format.i_rate = (__MAX(p_input->input.i_rate,
390 p_aout->mixer_format.i_rate)
391 * (100 + AOUT_MAX_RESAMPLING)) / 100;
392 if ( chain_output_format.i_rate == p_aout->mixer_format.i_rate )
394 /* Just in case... */
395 chain_output_format.i_rate++;
397 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
398 &p_input->i_nb_resamplers,
399 &chain_output_format,
400 &p_aout->mixer_format ) < 0 )
402 inputFailure( p_aout, p_input, "couldn't set a resampler pipeline");
406 /* Setup the initial rate of the resampler */
407 p_input->pp_resamplers[0]->fmt_in.audio.i_rate = p_input->input.i_rate;
409 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
411 if( ! p_input->p_playback_rate_filter && p_input->i_nb_resamplers > 0 )
413 p_input->p_playback_rate_filter = p_input->pp_resamplers[0];
416 ReplayGainSelect( p_aout, p_input );
419 p_input->b_error = false;
420 p_input->i_last_input_rate = INPUT_RATE_DEFAULT;
425 /*****************************************************************************
426 * aout_InputDelete : delete an input
427 *****************************************************************************
428 * This function must be entered with the mixer lock.
429 *****************************************************************************/
430 int aout_InputDelete( audio_output_t * p_aout, aout_input_t * p_input )
433 if ( p_input->b_error )
436 /* XXX We need to update b_recycle_vout before calling aout_FiltersDestroyPipeline.
437 * FIXME They can be a race condition if audio-visual is updated between
438 * aout_InputDelete and aout_InputNew.
440 char *psz_visual = var_GetString( p_aout, "audio-visual");
441 p_input->b_recycle_vout = psz_visual && *psz_visual;
444 aout_FiltersDestroyPipeline( p_input->pp_filters, p_input->i_nb_filters );
445 p_input->i_nb_filters = 0;
446 aout_FiltersDestroyPipeline( p_input->pp_resamplers,
447 p_input->i_nb_resamplers );
448 p_input->i_nb_resamplers = 0;
449 aout_FifoDestroy( &p_input->fifo );
454 /*****************************************************************************
455 * aout_InputCheckAndRestart : restart an input
456 *****************************************************************************
457 * This function must be entered with the input and mixer lock.
458 *****************************************************************************/
459 void aout_InputCheckAndRestart( audio_output_t * p_aout, aout_input_t * p_input )
463 if( !p_input->b_restart )
466 /* A little trick to avoid loosing our input fifo and properties */
468 aout_fifo_t fifo = p_input->fifo;
469 bool b_paused = p_input->b_paused;
470 mtime_t i_pause_date = p_input->i_pause_date;
472 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer_format.i_rate );
474 aout_InputDelete( p_aout, p_input );
476 aout_InputNew( p_aout, p_input, &p_input->request_vout );
477 p_input->fifo = fifo;
478 p_input->b_paused = b_paused;
479 p_input->i_pause_date = i_pause_date;
481 p_input->b_restart = false;
483 /*****************************************************************************
484 * aout_InputPlay : play a buffer
485 *****************************************************************************
486 * This function must be entered with the input lock.
487 *****************************************************************************/
488 /* XXX Do not activate it !! */
489 //#define AOUT_PROCESS_BEFORE_CHEKS
490 void aout_InputPlay( audio_output_t * p_aout, aout_input_t * p_input,
491 aout_buffer_t * p_buffer, int i_input_rate )
496 if( i_input_rate != INPUT_RATE_DEFAULT && p_input->p_playback_rate_filter == NULL )
498 inputDrop( p_input, p_buffer );
502 #ifdef AOUT_PROCESS_BEFORE_CHEKS
503 /* Run pre-filters. */
504 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
509 /* Actually run the resampler now. */
510 if ( p_input->i_nb_resamplers > 0 )
512 const mtime_t i_date = p_buffer->i_pts;
513 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
514 p_input->i_nb_resamplers,
520 if( p_buffer->i_nb_samples <= 0 )
522 block_Release( p_buffer );
527 /* Handle input rate change, but keep drift correction */
528 if( i_input_rate != p_input->i_last_input_rate )
530 unsigned int * const pi_rate = &p_input->p_playback_rate_filter->fmt_in.audio.i_rate;
531 #define F(r,ir) ( INPUT_RATE_DEFAULT * (r) / (ir) )
532 const int i_delta = *pi_rate - F(p_input->input.i_rate,p_input->i_last_input_rate);
533 *pi_rate = F(p_input->input.i_rate + i_delta, i_input_rate);
535 p_input->i_last_input_rate = i_input_rate;
538 mtime_t now = mdate();
540 /* We don't care if someone changes the start date behind our back after
541 * this. We'll deal with that when pushing the buffer, and compensate
542 * with the next incoming buffer. */
543 start_date = aout_FifoNextStart( &p_input->fifo );
545 if ( start_date != VLC_TS_INVALID && start_date < now )
547 /* The decoder is _very_ late. This can only happen if the user
548 * pauses the stream (or if the decoder is buggy, which cannot
550 msg_Warn( p_aout, "computed PTS is out of range (%"PRId64"), "
551 "clearing out", now - start_date );
552 aout_FifoReset( &p_input->fifo );
553 aout_FifoReset( &p_aout->fifo );
554 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
555 msg_Warn( p_aout, "timing screwed, stopping resampling" );
556 inputResamplingStop( p_input );
557 p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
558 start_date = VLC_TS_INVALID;
561 if ( p_buffer->i_pts < now + AOUT_MIN_PREPARE_TIME )
563 /* The decoder gives us f*cked up PTS. It's its business, but we
564 * can't present it anyway, so drop the buffer. */
565 msg_Warn( p_aout, "PTS is out of range (%"PRId64"), dropping buffer",
566 now - p_buffer->i_pts );
567 inputDrop( p_input, p_buffer );
568 inputResamplingStop( p_input );
572 /* If the audio drift is too big then it's not worth trying to resample
574 if( start_date == VLC_TS_INVALID )
575 start_date = p_buffer->i_pts;
577 mtime_t drift = start_date - p_buffer->i_pts;
579 if( drift < -i_input_rate * 3 * AOUT_MAX_PTS_ADVANCE / INPUT_RATE_DEFAULT )
581 msg_Warn( p_aout, "buffer way too early (%"PRId64"), clearing queue",
583 aout_FifoReset( &p_input->fifo );
584 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
585 msg_Warn( p_aout, "timing screwed, stopping resampling" );
586 inputResamplingStop( p_input );
587 p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
588 start_date = p_buffer->i_pts;
592 if( drift > +i_input_rate * 3 * AOUT_MAX_PTS_DELAY / INPUT_RATE_DEFAULT )
594 msg_Warn( p_aout, "buffer way too late (%"PRId64"), dropping buffer",
596 inputDrop( p_input, p_buffer );
600 #ifndef AOUT_PROCESS_BEFORE_CHEKS
601 /* Run pre-filters. */
602 aout_FiltersPlay( p_input->pp_filters, p_input->i_nb_filters, &p_buffer );
607 /* Run the resampler if needed.
608 * We first need to calculate the output rate of this resampler. */
609 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
610 ( drift < -AOUT_MAX_PTS_ADVANCE || drift > +AOUT_MAX_PTS_DELAY ) &&
611 p_input->i_nb_resamplers > 0 )
613 /* Can happen in several circumstances :
614 * 1. A problem at the input (clock drift)
615 * 2. A small pause triggered by the user
616 * 3. Some delay in the output stage, causing a loss of lip
618 * Solution : resample the buffer to avoid a scratch.
620 p_input->i_resamp_start_date = now;
621 p_input->i_resamp_start_drift = (int)-drift;
622 p_input->i_resampling_type = (drift < 0) ? AOUT_RESAMPLING_DOWN
623 : AOUT_RESAMPLING_UP;
624 msg_Warn( p_aout, (drift < 0)
625 ? "buffer too early (%"PRId64"), down-sampling"
626 : "buffer too late (%"PRId64"), up-sampling", drift );
629 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
631 /* Resampling has been triggered previously (because of dates
632 * mismatch). We want the resampling to happen progressively so
633 * it isn't too audible to the listener. */
635 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
636 p_input->pp_resamplers[0]->fmt_in.audio.i_rate += 2; /* Hz */
638 p_input->pp_resamplers[0]->fmt_in.audio.i_rate -= 2; /* Hz */
640 /* Check if everything is back to normal, in which case we can stop the
642 unsigned int i_nominal_rate =
643 (p_input->pp_resamplers[0] == p_input->p_playback_rate_filter)
644 ? INPUT_RATE_DEFAULT * p_input->input.i_rate / i_input_rate
645 : p_input->input.i_rate;
646 if( p_input->pp_resamplers[0]->fmt_in.audio.i_rate == i_nominal_rate )
648 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
649 msg_Warn( p_aout, "resampling stopped after %"PRIi64" usec "
650 "(drift: %"PRIi64")",
651 now - p_input->i_resamp_start_date,
652 p_buffer->i_pts - start_date);
654 else if( abs( (int)(p_buffer->i_pts - start_date) ) <
655 abs( p_input->i_resamp_start_drift ) / 2 )
657 /* if we reduced the drift from half, then it is time to switch
658 * back the resampling direction. */
659 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
660 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
662 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
663 p_input->i_resamp_start_drift = 0;
665 else if( p_input->i_resamp_start_drift &&
666 ( abs( (int)(p_buffer->i_pts - start_date) ) >
667 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
669 /* If the drift is increasing and not decreasing, than something
670 * is bad. We'd better stop the resampling right now. */
671 msg_Warn( p_aout, "timing screwed, stopping resampling" );
672 inputResamplingStop( p_input );
673 p_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
677 #ifndef AOUT_PROCESS_BEFORE_CHEKS
678 /* Actually run the resampler now. */
679 if ( p_input->i_nb_resamplers > 0 )
681 aout_FiltersPlay( p_input->pp_resamplers, p_input->i_nb_resamplers,
687 if( p_buffer->i_nb_samples <= 0 )
689 block_Release( p_buffer );
694 /* Adding the start date will be managed by aout_FifoPush(). */
695 p_buffer->i_pts = start_date;
696 aout_FifoPush( &p_input->fifo, p_buffer );
699 /*****************************************************************************
701 *****************************************************************************/
703 static void inputFailure( audio_output_t * p_aout, aout_input_t * p_input,
704 const char * psz_error_message )
707 msg_Err( p_aout, "%s", psz_error_message );
710 aout_FiltersDestroyPipeline( p_input->pp_filters, p_input->i_nb_filters );
711 aout_FiltersDestroyPipeline( p_input->pp_resamplers,
712 p_input->i_nb_resamplers );
713 aout_FifoDestroy( &p_input->fifo );
714 var_Destroy( p_aout, "visual" );
715 var_Destroy( p_aout, "equalizer" );
716 var_Destroy( p_aout, "audio-filter" );
717 var_Destroy( p_aout, "audio-visual" );
719 var_Destroy( p_aout, "audio-replay-gain-mode" );
720 var_Destroy( p_aout, "audio-replay-gain-default" );
721 var_Destroy( p_aout, "audio-replay-gain-preamp" );
722 var_Destroy( p_aout, "audio-replay-gain-peak-protection" );
725 p_input->b_error = 1;
728 static void inputDrop( aout_input_t *p_input, aout_buffer_t *p_buffer )
730 aout_BufferFree( p_buffer );
732 p_input->i_buffer_lost++;
735 static void inputResamplingStop( aout_input_t *p_input )
737 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
738 if( p_input->i_nb_resamplers != 0 )
740 p_input->pp_resamplers[0]->fmt_in.audio.i_rate =
741 ( p_input->pp_resamplers[0] == p_input->p_playback_rate_filter )
742 ? INPUT_RATE_DEFAULT * p_input->input.i_rate / p_input->i_last_input_rate
743 : p_input->input.i_rate;
747 static vout_thread_t *RequestVout( void *p_private,
748 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recycle )
750 audio_output_t *p_aout = p_private;
751 VLC_UNUSED(b_recycle);
752 vout_configuration_t cfg = {
759 return vout_Request( p_aout, &cfg );
762 vout_thread_t *aout_filter_RequestVout( filter_t *p_filter,
763 vout_thread_t *p_vout, video_format_t *p_fmt )
765 aout_input_t *p_input = p_filter->p_owner->p_input;
766 aout_request_vout_t *p_request = &p_input->request_vout;
768 /* XXX: this only works from audio input */
769 /* If you want to use visualization filters from another place, you will
770 * need to add a new pf_aout_request_vout callback or store a pointer
771 * to aout_request_vout_t inside filter_t (i.e. a level of indirection). */
773 return p_request->pf_request_vout( p_request->p_private,
774 p_vout, p_fmt, p_input->b_recycle_vout );
777 static int ChangeFiltersString( audio_output_t * p_aout, const char* psz_variable,
778 const char *psz_name, bool b_add )
780 return aout_ChangeFilterString( VLC_OBJECT(p_aout), p_aout,
781 psz_variable, psz_name, b_add ) ? 1 : 0;
784 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
785 vlc_value_t oldval, vlc_value_t newval, void *p_data )
787 audio_output_t *p_aout = (audio_output_t *)p_this;
788 char *psz_mode = newval.psz_string;
789 (void)psz_cmd; (void)oldval; (void)p_data;
791 if( !psz_mode || !*psz_mode )
793 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
794 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
795 ChangeFiltersString( p_aout, "audio-visual", "projectm", false );
799 if( !strcmp( "goom", psz_mode ) )
801 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
802 ChangeFiltersString( p_aout, "audio-visual", "goom", true );
803 ChangeFiltersString( p_aout, "audio-visual", "projectm", false );
805 else if( !strcmp( "projectm", psz_mode ) )
807 ChangeFiltersString( p_aout, "audio-visual", "visual", false );
808 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
809 ChangeFiltersString( p_aout, "audio-visual", "projectm", true );
813 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
814 var_SetString( p_aout, "effect-list", psz_mode );
816 ChangeFiltersString( p_aout, "audio-visual", "goom", false );
817 ChangeFiltersString( p_aout, "audio-visual", "visual", true );
818 ChangeFiltersString( p_aout, "audio-visual", "projectm", false );
823 AoutInputsMarkToRestart( p_aout );
828 static int EqualizerCallback( vlc_object_t *p_this, char const *psz_cmd,
829 vlc_value_t oldval, vlc_value_t newval, void *p_data )
831 audio_output_t *p_aout = (audio_output_t *)p_this;
832 char *psz_mode = newval.psz_string;
834 (void)psz_cmd; (void)oldval; (void)p_data;
836 if( !psz_mode || !*psz_mode )
838 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
843 var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING );
844 var_SetString( p_aout, "equalizer-preset", psz_mode );
845 i_ret = ChangeFiltersString( p_aout, "audio-filter", "equalizer",
851 AoutInputsMarkToRestart( p_aout );
855 static int ReplayGainCallback( vlc_object_t *p_this, char const *psz_cmd,
856 vlc_value_t oldval, vlc_value_t newval, void *p_data )
858 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
859 VLC_UNUSED(newval); VLC_UNUSED(p_data);
860 audio_output_t *p_aout = (audio_output_t *)p_this;
863 if( p_aout->p_input != NULL )
864 ReplayGainSelect( p_aout, p_aout->p_input );
865 aout_unlock( p_aout );
870 static void ReplayGainSelect( audio_output_t *p_aout, aout_input_t *p_input )
872 char *psz_replay_gain = var_GetNonEmptyString( p_aout,
873 "audio-replay-gain-mode" );
878 p_input->multiplier = 1.0;
880 if( !psz_replay_gain )
883 /* Find select mode */
884 if( !strcmp( psz_replay_gain, "track" ) )
885 i_mode = AUDIO_REPLAY_GAIN_TRACK;
886 else if( !strcmp( psz_replay_gain, "album" ) )
887 i_mode = AUDIO_REPLAY_GAIN_ALBUM;
889 i_mode = AUDIO_REPLAY_GAIN_MAX;
891 /* If the select mode is not available, prefer the other one */
893 if( i_use != AUDIO_REPLAY_GAIN_MAX && !p_input->replay_gain.pb_gain[i_use] )
895 for( i_use = 0; i_use < AUDIO_REPLAY_GAIN_MAX; i_use++ )
897 if( p_input->replay_gain.pb_gain[i_use] )
903 if( i_use != AUDIO_REPLAY_GAIN_MAX )
904 f_gain = p_input->replay_gain.pf_gain[i_use] + var_GetFloat( p_aout, "audio-replay-gain-preamp" );
905 else if( i_mode != AUDIO_REPLAY_GAIN_MAX )
906 f_gain = var_GetFloat( p_aout, "audio-replay-gain-default" );
909 p_input->multiplier = pow( 10.0, f_gain / 20.0 );
912 if( p_input->replay_gain.pb_peak[i_use] &&
913 var_GetBool( p_aout, "audio-replay-gain-peak-protection" ) &&
914 p_input->replay_gain.pf_peak[i_use] * p_input->multiplier > 1.0 )
916 p_input->multiplier = 1.0f / p_input->replay_gain.pf_peak[i_use];
919 free( psz_replay_gain );