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 vlc_mutex_lock( &p_input->lock );
313 aout_InputDelete( p_aout, p_input );
314 aout_InputNew( p_aout, p_input );
315 vlc_mutex_unlock( &p_input->lock );
316 vlc_mutex_unlock( &p_aout->mixer_lock );
319 /* We don't care if someone changes the start date behind our back after
320 * this. We'll deal with that when pushing the buffer, and compensate
321 * with the next incoming buffer. */
322 vlc_mutex_lock( &p_aout->input_fifos_lock );
323 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
324 vlc_mutex_unlock( &p_aout->input_fifos_lock );
326 if ( start_date != 0 && start_date < mdate() )
328 /* The decoder is _very_ late. This can only happen if the user
329 * pauses the stream (or if the decoder is buggy, which cannot
331 msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
332 "clearing out", mdate() - start_date );
333 vlc_mutex_lock( &p_aout->input_fifos_lock );
334 aout_FifoSet( p_aout, &p_input->fifo, 0 );
335 p_input->p_first_byte_to_mix = NULL;
336 vlc_mutex_unlock( &p_aout->input_fifos_lock );
337 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
338 msg_Warn( p_aout, "timing screwed, stopping resampling" );
339 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
340 if ( p_input->i_nb_resamplers != 0 )
342 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
343 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
348 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
350 /* The decoder gives us f*cked up PTS. It's its business, but we
351 * can't present it anyway, so drop the buffer. */
352 msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
353 mdate() - p_buffer->start_date );
354 aout_BufferFree( p_buffer );
355 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
356 if ( p_input->i_nb_resamplers != 0 )
358 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
359 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
364 if ( start_date == 0 ) start_date = p_buffer->start_date;
366 /* Run pre-filters. */
368 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
371 /* Run the resampler if needed.
372 * We first need to calculate the output rate of this resampler. */
373 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
374 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
375 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
376 p_input->i_nb_resamplers > 0 )
378 /* Can happen in several circumstances :
379 * 1. A problem at the input (clock drift)
380 * 2. A small pause triggered by the user
381 * 3. Some delay in the output stage, causing a loss of lip
383 * Solution : resample the buffer to avoid a scratch.
385 mtime_t drift = p_buffer->start_date - start_date;
387 p_input->i_resamp_start_date = mdate();
388 p_input->i_resamp_start_drift = (int)drift;
391 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
393 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
395 msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
396 drift > 0 ? drift : -drift,
397 drift > 0 ? "in advance" : "late",
398 drift > 0 ? "down" : "up");
401 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
403 /* Resampling has been triggered previously (because of dates
404 * mismatch). We want the resampling to happen progressively so
405 * it isn't too audible to the listener. */
407 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
409 p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
413 p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
416 /* Check if everything is back to normal, in which case we can stop the
418 if( p_input->pp_resamplers[0]->input.i_rate ==
419 p_input->input.i_rate )
421 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
422 msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
423 mdate() - p_input->i_resamp_start_date );
425 else if( abs( (int)(p_buffer->start_date - start_date) ) <
426 abs( p_input->i_resamp_start_drift ) / 2 )
428 /* if we reduced the drift from half, then it is time to switch
429 * back the resampling direction. */
430 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
431 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
433 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
434 p_input->i_resamp_start_drift = 0;
436 else if( p_input->i_resamp_start_drift &&
437 ( abs( (int)(p_buffer->start_date - start_date) ) >
438 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
440 /* If the drift is increasing and not decreasing, than something
441 * is bad. We'd better stop the resampling right now. */
442 msg_Warn( p_aout, "timing screwed, stopping resampling" );
443 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
444 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
448 /* Adding the start date will be managed by aout_FifoPush(). */
449 p_buffer->end_date = start_date +
450 (p_buffer->end_date - p_buffer->start_date);
451 p_buffer->start_date = start_date;
453 /* Actually run the resampler now. */
454 if ( p_input->i_nb_resamplers > 0 )
456 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
457 p_input->i_nb_resamplers,
461 vlc_mutex_lock( &p_aout->input_fifos_lock );
462 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
463 vlc_mutex_unlock( &p_aout->input_fifos_lock );
468 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
469 vlc_value_t oldval, vlc_value_t newval, void *p_data )
471 aout_instance_t *p_aout = (aout_instance_t *)p_this;
472 char *psz_mode = newval.psz_string;
476 if( !psz_mode || !*psz_mode )
479 var_Set( p_aout, "audio-filter", val );
483 if( !strcmp( "goom", psz_mode ) )
485 val.psz_string = "goom";
489 val.psz_string = psz_mode;
490 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
491 var_Set( p_aout, "effect-list", val );
493 val.psz_string = "visual";
496 var_Set( p_aout, "audio-filter", val );
500 for( i = 0; i < p_aout->i_nb_inputs; i++ )
502 p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
508 static aout_filter_t * allocateUserChannelMixer( aout_instance_t * p_aout,
509 audio_sample_format_t * p_input_format,
510 audio_sample_format_t * p_output_format )
512 aout_filter_t * p_channel_mixer;
514 /* Retreive user preferred channel mixer */
515 char * psz_name = config_GetPsz( p_aout, "audio-channel-mixer" );
517 /* Not specified => let the main pipeline do the mixing */
518 if ( ! psz_name ) return NULL;
520 /* Debug information */
521 aout_FormatsPrint( p_aout, "channel mixer", p_input_format,
524 /* Create a VLC object */
525 p_channel_mixer = vlc_object_create( p_aout, sizeof(aout_filter_t) );
526 if( p_channel_mixer == NULL )
528 msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
531 vlc_object_attach( p_channel_mixer , p_aout );
533 /* Attach the suitable module */
534 memcpy( &p_channel_mixer->input, p_input_format,
535 sizeof(audio_sample_format_t) );
536 memcpy( &p_channel_mixer->output, p_output_format,
537 sizeof(audio_sample_format_t) );
538 p_channel_mixer->p_module =
539 module_Need( p_channel_mixer,"audio filter", psz_name, VLC_TRUE );
540 if( p_channel_mixer->p_module== NULL )
542 msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
543 vlc_object_detach( p_channel_mixer );
544 vlc_object_destroy( p_channel_mixer );
547 p_channel_mixer->b_continuity = VLC_FALSE;
550 return p_channel_mixer;