1 /*****************************************************************************
2 * input.c : internal management of input streams for the audio output
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
31 #include <vlc/input.h> /* for input_thread_t and i_pts_delay */
37 #include "audio_output.h"
38 #include "aout_internal.h"
40 static int VisualizationCallback( vlc_object_t *, char const *,
41 vlc_value_t, vlc_value_t, void * );
42 static aout_filter_t * allocateUserChannelMixer( aout_instance_t *,
43 audio_sample_format_t *,
44 audio_sample_format_t * );
46 /*****************************************************************************
47 * aout_InputNew : allocate a new input and rework the filter pipeline
48 *****************************************************************************/
49 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
51 audio_sample_format_t user_filter_format;
52 audio_sample_format_t intermediate_format;/* input of resampler */
53 vlc_value_t val, text;
55 aout_filter_t * p_user_channel_mixer;
57 aout_FormatPrint( p_aout, "input", &p_input->input );
60 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
61 p_input->p_first_byte_to_mix = NULL;
63 /* Prepare format structure */
64 memcpy( &intermediate_format, &p_aout->mixer.mixer,
65 sizeof(audio_sample_format_t) );
66 intermediate_format.i_rate = p_input->input.i_rate;
68 /* Try to use the channel mixer chosen by the user */
69 memcpy ( &user_filter_format, &intermediate_format,
70 sizeof(audio_sample_format_t) );
71 user_filter_format.i_physical_channels = p_input->input.i_physical_channels;
72 user_filter_format.i_original_channels = p_input->input.i_original_channels;
73 user_filter_format.i_bytes_per_frame = user_filter_format.i_bytes_per_frame
74 * aout_FormatNbChannels( &user_filter_format )
75 / aout_FormatNbChannels( &intermediate_format );
76 p_user_channel_mixer = allocateUserChannelMixer( p_aout, &user_filter_format,
77 &intermediate_format );
78 /* If it failed, let the main pipeline do channel mixing */
79 if ( ! p_user_channel_mixer )
81 memcpy ( &user_filter_format, &intermediate_format,
82 sizeof(audio_sample_format_t) );
86 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
87 &p_input->i_nb_filters,
92 msg_Err( p_aout, "couldn't set an input pipeline" );
94 aout_FifoDestroy( p_aout, &p_input->fifo );
99 /* Now add user filters */
100 if( var_Type( p_aout, "visual" ) == 0 )
103 var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
104 text.psz_string = _("Visualizations");
105 var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
106 val.psz_string = ""; text.psz_string = _("Disable");
107 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
108 val.psz_string = "random"; text.psz_string = _("Random");
109 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
110 val.psz_string = "scope"; text.psz_string = _("Scope");
111 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
112 val.psz_string = "spectrum"; text.psz_string = _("Spectrum");
113 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
115 /* Look for goom plugin */
116 p_module = config_FindModule( VLC_OBJECT(p_aout), "goom" );
119 val.psz_string = "goom"; text.psz_string = _("Goom");
120 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
123 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
125 var_Set( p_aout, "visual", val );
126 if( val.psz_string ) free( val.psz_string );
128 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
131 if( var_Type( p_aout, "audio-filter" ) == 0 )
133 var_Create( p_aout, "audio-filter",
134 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
135 text.psz_string = _("Audio filters");
136 var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
139 var_Get( p_aout, "audio-filter", &val );
140 psz_filters = val.psz_string;
141 if( psz_filters && *psz_filters )
143 char *psz_parser = psz_filters;
146 while( psz_parser && *psz_parser )
148 aout_filter_t * p_filter;
150 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
152 msg_Dbg( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
156 while( *psz_parser == ' ' && *psz_parser == ',' )
160 if( ( psz_next = strchr( psz_parser , ',' ) ) )
164 if( *psz_parser =='\0' )
169 msg_Dbg( p_aout, "user filter \"%s\"", psz_parser );
171 /* Create a VLC object */
172 p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
173 if( p_filter == NULL )
175 msg_Err( p_aout, "cannot add user filter %s (skipped)",
177 psz_parser = psz_next;
181 vlc_object_attach( p_filter , p_aout );
182 memcpy( &p_filter->input, &user_filter_format,
183 sizeof(audio_sample_format_t) );
184 memcpy( &p_filter->output, &user_filter_format,
185 sizeof(audio_sample_format_t) );
188 module_Need( p_filter,"audio filter", psz_parser, VLC_TRUE );
190 if( p_filter->p_module== NULL )
192 msg_Err( p_aout, "cannot add user filter %s (skipped)",
195 vlc_object_detach( p_filter );
196 vlc_object_destroy( p_filter );
197 psz_parser = psz_next;
201 p_filter->b_continuity = VLC_FALSE;
203 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
205 /* next filter if any */
206 psz_parser = psz_next;
209 if( psz_filters ) free( psz_filters );
211 /* Attach the user channel mixer */
212 if ( p_user_channel_mixer )
214 p_input->pp_filters[p_input->i_nb_filters++] = p_user_channel_mixer;
217 /* Prepare hints for the buffer allocator. */
218 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
219 p_input->input_alloc.i_bytes_per_sec = -1;
221 if ( AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
223 p_input->i_nb_resamplers = 0;
227 /* Create resamplers. */
228 intermediate_format.i_rate = (__MAX(p_input->input.i_rate,
229 p_aout->mixer.mixer.i_rate)
230 * (100 + AOUT_MAX_RESAMPLING)) / 100;
231 if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
233 /* Just in case... */
234 intermediate_format.i_rate++;
236 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
237 &p_input->i_nb_resamplers,
238 &intermediate_format,
239 &p_aout->mixer.mixer ) < 0 )
241 msg_Err( p_aout, "couldn't set a resampler pipeline" );
243 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
244 p_input->i_nb_filters );
245 aout_FifoDestroy( p_aout, &p_input->fifo );
246 var_Destroy( p_aout, "visual" );
247 p_input->b_error = 1;
252 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
253 p_input->i_nb_resamplers,
254 &p_input->input_alloc );
256 /* Setup the initial rate of the resampler */
257 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
259 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
261 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
262 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
263 p_input->i_nb_filters,
264 &p_input->input_alloc );
266 /* i_bytes_per_sec is still == -1 if no filters */
267 p_input->input_alloc.i_bytes_per_sec = __MAX(
268 p_input->input_alloc.i_bytes_per_sec,
269 (int)(p_input->input.i_bytes_per_frame
270 * p_input->input.i_rate
271 / p_input->input.i_frame_length) );
272 /* Allocate in the heap, it is more convenient for the decoder. */
273 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
275 p_input->b_error = VLC_FALSE;
276 p_input->b_restart = VLC_FALSE;
281 /*****************************************************************************
282 * aout_InputDelete : delete an input
283 *****************************************************************************
284 * This function must be entered with the mixer lock.
285 *****************************************************************************/
286 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
288 if ( p_input->b_error ) return 0;
290 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
291 p_input->i_nb_filters );
292 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
293 p_input->i_nb_resamplers );
294 aout_FifoDestroy( p_aout, &p_input->fifo );
299 /*****************************************************************************
300 * aout_InputPlay : play a buffer
301 *****************************************************************************
302 * This function must be entered with the input lock.
303 *****************************************************************************/
304 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
305 aout_buffer_t * p_buffer )
309 if( p_input->b_restart )
311 aout_fifo_t fifo, dummy_fifo;
312 int p_first_byte_to_mix;
314 vlc_mutex_lock( &p_aout->mixer_lock );
316 /* A little trick to avoid loosing our input fifo */
317 aout_FifoInit( p_aout, &dummy_fifo, p_aout->mixer.mixer.i_rate );
318 p_first_byte_to_mix = p_input->p_first_byte_to_mix;
319 fifo = p_input->fifo;
320 p_input->fifo = dummy_fifo;
321 aout_InputDelete( p_aout, p_input );
322 aout_InputNew( p_aout, p_input );
323 p_input->p_first_byte_to_mix = p_first_byte_to_mix;
324 p_input->fifo = fifo;
326 vlc_mutex_unlock( &p_aout->mixer_lock );
329 /* We don't care if someone changes the start date behind our back after
330 * this. We'll deal with that when pushing the buffer, and compensate
331 * with the next incoming buffer. */
332 vlc_mutex_lock( &p_aout->input_fifos_lock );
333 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
334 vlc_mutex_unlock( &p_aout->input_fifos_lock );
336 if ( start_date != 0 && start_date < mdate() )
338 /* The decoder is _very_ late. This can only happen if the user
339 * pauses the stream (or if the decoder is buggy, which cannot
341 msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
342 "clearing out", mdate() - start_date );
343 vlc_mutex_lock( &p_aout->input_fifos_lock );
344 aout_FifoSet( p_aout, &p_input->fifo, 0 );
345 p_input->p_first_byte_to_mix = NULL;
346 vlc_mutex_unlock( &p_aout->input_fifos_lock );
347 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
348 msg_Warn( p_aout, "timing screwed, stopping resampling" );
349 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
350 if ( p_input->i_nb_resamplers != 0 )
352 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
353 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
358 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
360 /* The decoder gives us f*cked up PTS. It's its business, but we
361 * can't present it anyway, so drop the buffer. */
362 msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
363 mdate() - p_buffer->start_date );
364 aout_BufferFree( p_buffer );
365 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
366 if ( p_input->i_nb_resamplers != 0 )
368 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
369 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
374 if ( start_date == 0 ) start_date = p_buffer->start_date;
376 /* Run pre-filters. */
378 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
381 /* Run the resampler if needed.
382 * We first need to calculate the output rate of this resampler. */
383 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
384 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
385 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
386 p_input->i_nb_resamplers > 0 )
388 /* Can happen in several circumstances :
389 * 1. A problem at the input (clock drift)
390 * 2. A small pause triggered by the user
391 * 3. Some delay in the output stage, causing a loss of lip
393 * Solution : resample the buffer to avoid a scratch.
395 mtime_t drift = p_buffer->start_date - start_date;
397 p_input->i_resamp_start_date = mdate();
398 p_input->i_resamp_start_drift = (int)drift;
401 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
403 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
405 msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
406 drift > 0 ? drift : -drift,
407 drift > 0 ? "in advance" : "late",
408 drift > 0 ? "down" : "up");
411 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
413 /* Resampling has been triggered previously (because of dates
414 * mismatch). We want the resampling to happen progressively so
415 * it isn't too audible to the listener. */
417 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
419 p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
423 p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
426 /* Check if everything is back to normal, in which case we can stop the
428 if( p_input->pp_resamplers[0]->input.i_rate ==
429 p_input->input.i_rate )
431 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
432 msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
433 mdate() - p_input->i_resamp_start_date );
435 else if( abs( (int)(p_buffer->start_date - start_date) ) <
436 abs( p_input->i_resamp_start_drift ) / 2 )
438 /* if we reduced the drift from half, then it is time to switch
439 * back the resampling direction. */
440 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
441 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
443 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
444 p_input->i_resamp_start_drift = 0;
446 else if( p_input->i_resamp_start_drift &&
447 ( abs( (int)(p_buffer->start_date - start_date) ) >
448 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
450 /* If the drift is increasing and not decreasing, than something
451 * is bad. We'd better stop the resampling right now. */
452 msg_Warn( p_aout, "timing screwed, stopping resampling" );
453 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
454 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
458 /* Adding the start date will be managed by aout_FifoPush(). */
459 p_buffer->end_date = start_date +
460 (p_buffer->end_date - p_buffer->start_date);
461 p_buffer->start_date = start_date;
463 /* Actually run the resampler now. */
464 if ( p_input->i_nb_resamplers > 0 )
466 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
467 p_input->i_nb_resamplers,
471 vlc_mutex_lock( &p_aout->input_fifos_lock );
472 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
473 vlc_mutex_unlock( &p_aout->input_fifos_lock );
478 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
479 vlc_value_t oldval, vlc_value_t newval, void *p_data )
481 aout_instance_t *p_aout = (aout_instance_t *)p_this;
482 char *psz_mode = newval.psz_string;
486 if( !psz_mode || !*psz_mode )
489 var_Set( p_aout, "audio-filter", val );
493 if( !strcmp( "goom", psz_mode ) )
495 val.psz_string = "goom";
499 val.psz_string = psz_mode;
500 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
501 var_Set( p_aout, "effect-list", val );
503 val.psz_string = "visual";
506 var_Set( p_aout, "audio-filter", val );
510 for( i = 0; i < p_aout->i_nb_inputs; i++ )
512 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
518 static aout_filter_t * allocateUserChannelMixer( aout_instance_t * p_aout,
519 audio_sample_format_t * p_input_format,
520 audio_sample_format_t * p_output_format )
522 aout_filter_t * p_channel_mixer;
524 /* Retreive user preferred channel mixer */
525 char * psz_name = config_GetPsz( p_aout, "audio-channel-mixer" );
527 /* Not specified => let the main pipeline do the mixing */
528 if ( ! psz_name ) return NULL;
530 /* Debug information */
531 aout_FormatsPrint( p_aout, "channel mixer", p_input_format,
534 /* Create a VLC object */
535 p_channel_mixer = vlc_object_create( p_aout, sizeof(aout_filter_t) );
536 if( p_channel_mixer == NULL )
538 msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
541 vlc_object_attach( p_channel_mixer , p_aout );
543 /* Attach the suitable module */
544 memcpy( &p_channel_mixer->input, p_input_format,
545 sizeof(audio_sample_format_t) );
546 memcpy( &p_channel_mixer->output, p_output_format,
547 sizeof(audio_sample_format_t) );
548 p_channel_mixer->p_module =
549 module_Need( p_channel_mixer,"audio filter", psz_name, VLC_TRUE );
550 if( p_channel_mixer->p_module== NULL )
552 msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
553 vlc_object_detach( p_channel_mixer );
554 vlc_object_destroy( p_channel_mixer );
557 p_channel_mixer->b_continuity = VLC_FALSE;
560 return p_channel_mixer;