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 vlc_mutex_lock( &p_aout->mixer_lock );
312 aout_InputDelete( p_aout, p_input );
313 aout_InputNew( p_aout, p_input );
314 vlc_mutex_unlock( &p_aout->mixer_lock );
317 /* We don't care if someone changes the start date behind our back after
318 * this. We'll deal with that when pushing the buffer, and compensate
319 * with the next incoming buffer. */
320 vlc_mutex_lock( &p_aout->input_fifos_lock );
321 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
322 vlc_mutex_unlock( &p_aout->input_fifos_lock );
324 if ( start_date != 0 && start_date < mdate() )
326 /* The decoder is _very_ late. This can only happen if the user
327 * pauses the stream (or if the decoder is buggy, which cannot
329 msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
330 "clearing out", mdate() - start_date );
331 vlc_mutex_lock( &p_aout->input_fifos_lock );
332 aout_FifoSet( p_aout, &p_input->fifo, 0 );
333 p_input->p_first_byte_to_mix = NULL;
334 vlc_mutex_unlock( &p_aout->input_fifos_lock );
335 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
336 msg_Warn( p_aout, "timing screwed, stopping resampling" );
337 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
338 if ( p_input->i_nb_resamplers != 0 )
340 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
341 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
346 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
348 /* The decoder gives us f*cked up PTS. It's its business, but we
349 * can't present it anyway, so drop the buffer. */
350 msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
351 mdate() - p_buffer->start_date );
352 aout_BufferFree( p_buffer );
353 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
354 if ( p_input->i_nb_resamplers != 0 )
356 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
357 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
362 if ( start_date == 0 ) start_date = p_buffer->start_date;
364 /* Run pre-filters. */
366 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
369 /* Run the resampler if needed.
370 * We first need to calculate the output rate of this resampler. */
371 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
372 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
373 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
374 p_input->i_nb_resamplers > 0 )
376 /* Can happen in several circumstances :
377 * 1. A problem at the input (clock drift)
378 * 2. A small pause triggered by the user
379 * 3. Some delay in the output stage, causing a loss of lip
381 * Solution : resample the buffer to avoid a scratch.
383 mtime_t drift = p_buffer->start_date - start_date;
385 p_input->i_resamp_start_date = mdate();
386 p_input->i_resamp_start_drift = (int)drift;
389 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
391 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
393 msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
394 drift > 0 ? drift : -drift,
395 drift > 0 ? "in advance" : "late",
396 drift > 0 ? "down" : "up");
399 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
401 /* Resampling has been triggered previously (because of dates
402 * mismatch). We want the resampling to happen progressively so
403 * it isn't too audible to the listener. */
405 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
407 p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
411 p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
414 /* Check if everything is back to normal, in which case we can stop the
416 if( p_input->pp_resamplers[0]->input.i_rate ==
417 p_input->input.i_rate )
419 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
420 msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
421 mdate() - p_input->i_resamp_start_date );
423 else if( abs( (int)(p_buffer->start_date - start_date) ) <
424 abs( p_input->i_resamp_start_drift ) / 2 )
426 /* if we reduced the drift from half, then it is time to switch
427 * back the resampling direction. */
428 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
429 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
431 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
432 p_input->i_resamp_start_drift = 0;
434 else if( p_input->i_resamp_start_drift &&
435 ( abs( (int)(p_buffer->start_date - start_date) ) >
436 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
438 /* If the drift is increasing and not decreasing, than something
439 * is bad. We'd better stop the resampling right now. */
440 msg_Warn( p_aout, "timing screwed, stopping resampling" );
441 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
442 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
446 /* Adding the start date will be managed by aout_FifoPush(). */
447 p_buffer->end_date = start_date +
448 (p_buffer->end_date - p_buffer->start_date);
449 p_buffer->start_date = start_date;
451 /* Actually run the resampler now. */
452 if ( p_input->i_nb_resamplers > 0 )
454 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
455 p_input->i_nb_resamplers,
459 vlc_mutex_lock( &p_aout->input_fifos_lock );
460 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
461 vlc_mutex_unlock( &p_aout->input_fifos_lock );
466 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
467 vlc_value_t oldval, vlc_value_t newval, void *p_data )
469 aout_instance_t *p_aout = (aout_instance_t *)p_this;
470 char *psz_mode = newval.psz_string;
474 if( !psz_mode || !*psz_mode )
477 var_Set( p_aout, "audio-filter", val );
481 if( !strcmp( "goom", psz_mode ) )
483 val.psz_string = "goom";
487 val.psz_string = psz_mode;
488 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
489 var_Set( p_aout, "effect-list", val );
491 val.psz_string = "visual";
494 var_Set( p_aout, "audio-filter", val );
498 for( i = 0; i < p_aout->i_nb_inputs; i++ )
500 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
506 static aout_filter_t * allocateUserChannelMixer( aout_instance_t * p_aout,
507 audio_sample_format_t * p_input_format,
508 audio_sample_format_t * p_output_format )
510 aout_filter_t * p_channel_mixer;
512 /* Retreive user preferred channel mixer */
513 char * psz_name = config_GetPsz( p_aout, "audio-channel-mixer" );
515 /* Not specified => let the main pipeline do the mixing */
516 if ( ! psz_name ) return NULL;
518 /* Debug information */
519 aout_FormatsPrint( p_aout, "channel mixer", p_input_format,
522 /* Create a VLC object */
523 p_channel_mixer = vlc_object_create( p_aout, sizeof(aout_filter_t) );
524 if( p_channel_mixer == NULL )
526 msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
529 vlc_object_attach( p_channel_mixer , p_aout );
531 /* Attach the suitable module */
532 memcpy( &p_channel_mixer->input, p_input_format,
533 sizeof(audio_sample_format_t) );
534 memcpy( &p_channel_mixer->output, p_output_format,
535 sizeof(audio_sample_format_t) );
536 p_channel_mixer->p_module =
537 module_Need( p_channel_mixer,"audio filter", psz_name, VLC_TRUE );
538 if( p_channel_mixer->p_module== NULL )
540 msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
541 vlc_object_detach( p_channel_mixer );
542 vlc_object_destroy( p_channel_mixer );
545 p_channel_mixer->b_continuity = VLC_FALSE;
548 return p_channel_mixer;