1 /*****************************************************************************
2 * input.c : internal management of input streams for the audio output
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
5 * $Id: input.c,v 1.43 2004/01/06 12:02:05 zorglub Exp $
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 )
102 var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
103 text.psz_string = _("Visualizations");
104 var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
105 val.psz_string = ""; text.psz_string = _("Disable");
106 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
107 val.psz_string = "random"; text.psz_string = _("Random");
108 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
109 val.psz_string = "scope"; text.psz_string = _("Scope");
110 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
111 val.psz_string = "spectrum"; text.psz_string = _("Spectrum");
112 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
113 /* val.psz_string = "goom"; text.psz_string = _("Goom");
114 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );*/
115 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
117 var_Set( p_aout, "visual", val );
118 if( val.psz_string ) free( val.psz_string );
120 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
123 if( var_Type( p_aout, "audio-filter" ) == 0 )
125 var_Create( p_aout, "audio-filter",
126 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
127 text.psz_string = _("Audio filters");
128 var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
131 var_Get( p_aout, "audio-filter", &val );
132 psz_filters = val.psz_string;
133 if( psz_filters && *psz_filters )
135 char *psz_parser = psz_filters;
138 while( psz_parser && *psz_parser )
140 aout_filter_t * p_filter;
142 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
144 msg_Dbg( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
148 while( *psz_parser == ' ' && *psz_parser == ',' )
152 if( ( psz_next = strchr( psz_parser , ',' ) ) )
156 if( *psz_parser =='\0' )
161 msg_Dbg( p_aout, "user filter %s", psz_parser );
163 /* Create a VLC object */
164 p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
165 if( p_filter == NULL )
167 msg_Err( p_aout, "cannot add user filter %s (skipped)",
169 psz_parser = psz_next;
173 vlc_object_attach( p_filter , p_aout );
174 memcpy( &p_filter->input, &user_filter_format,
175 sizeof(audio_sample_format_t) );
176 memcpy( &p_filter->output, &user_filter_format,
177 sizeof(audio_sample_format_t) );
180 module_Need( p_filter,"audio filter", psz_parser );
182 if( p_filter->p_module== NULL )
184 msg_Err( p_aout, "cannot add user filter %s (skipped)",
187 vlc_object_detach( p_filter );
188 vlc_object_destroy( p_filter );
189 psz_parser = psz_next;
193 p_filter->b_continuity = VLC_FALSE;
195 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
197 /* next filter if any */
198 psz_parser = psz_next;
201 if( psz_filters ) free( psz_filters );
203 /* Attach the user channel mixer */
204 if ( p_user_channel_mixer )
206 p_input->pp_filters[p_input->i_nb_filters++] = p_user_channel_mixer;
209 /* Prepare hints for the buffer allocator. */
210 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
211 p_input->input_alloc.i_bytes_per_sec = -1;
213 if ( AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
215 p_input->i_nb_resamplers = 0;
219 /* Create resamplers. */
220 intermediate_format.i_rate = (__MAX(p_input->input.i_rate,
221 p_aout->mixer.mixer.i_rate)
222 * (100 + AOUT_MAX_RESAMPLING)) / 100;
223 if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
225 /* Just in case... */
226 intermediate_format.i_rate++;
228 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
229 &p_input->i_nb_resamplers,
230 &intermediate_format,
231 &p_aout->mixer.mixer ) < 0 )
233 msg_Err( p_aout, "couldn't set a resampler pipeline" );
235 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
236 p_input->i_nb_filters );
237 aout_FifoDestroy( p_aout, &p_input->fifo );
238 var_Destroy( p_aout, "visual" );
239 p_input->b_error = 1;
244 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
245 p_input->i_nb_resamplers,
246 &p_input->input_alloc );
248 /* Setup the initial rate of the resampler */
249 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
251 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
253 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
254 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
255 p_input->i_nb_filters,
256 &p_input->input_alloc );
258 /* i_bytes_per_sec is still == -1 if no filters */
259 p_input->input_alloc.i_bytes_per_sec = __MAX(
260 p_input->input_alloc.i_bytes_per_sec,
261 (int)(p_input->input.i_bytes_per_frame
262 * p_input->input.i_rate
263 / p_input->input.i_frame_length) );
264 /* Allocate in the heap, it is more convenient for the decoder. */
265 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
267 p_input->b_error = 0;
272 /*****************************************************************************
273 * aout_InputDelete : delete an input
274 *****************************************************************************
275 * This function must be entered with the mixer lock.
276 *****************************************************************************/
277 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
279 if ( p_input->b_error ) return 0;
281 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
282 p_input->i_nb_filters );
283 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
284 p_input->i_nb_resamplers );
285 aout_FifoDestroy( p_aout, &p_input->fifo );
290 /*****************************************************************************
291 * aout_InputPlay : play a buffer
292 *****************************************************************************
293 * This function must be entered with the input lock.
294 *****************************************************************************/
295 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
296 aout_buffer_t * p_buffer )
300 /* We don't care if someone changes the start date behind our back after
301 * this. We'll deal with that when pushing the buffer, and compensate
302 * with the next incoming buffer. */
303 vlc_mutex_lock( &p_aout->input_fifos_lock );
304 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
305 vlc_mutex_unlock( &p_aout->input_fifos_lock );
307 if ( start_date != 0 && start_date < mdate() )
309 /* The decoder is _very_ late. This can only happen if the user
310 * pauses the stream (or if the decoder is buggy, which cannot
312 msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
313 "clearing out", mdate() - start_date );
314 vlc_mutex_lock( &p_aout->input_fifos_lock );
315 aout_FifoSet( p_aout, &p_input->fifo, 0 );
316 p_input->p_first_byte_to_mix = NULL;
317 vlc_mutex_unlock( &p_aout->input_fifos_lock );
318 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
319 msg_Warn( p_aout, "timing screwed, stopping resampling" );
320 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
321 if ( p_input->i_nb_resamplers != 0 )
323 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
324 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
329 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
331 /* The decoder gives us f*cked up PTS. It's its business, but we
332 * can't present it anyway, so drop the buffer. */
333 msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
334 mdate() - p_buffer->start_date );
335 aout_BufferFree( p_buffer );
336 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
337 if ( p_input->i_nb_resamplers != 0 )
339 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
340 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
345 if ( start_date == 0 ) start_date = p_buffer->start_date;
347 /* Run pre-filters. */
349 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
352 /* Run the resampler if needed.
353 * We first need to calculate the output rate of this resampler. */
354 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
355 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
356 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
357 p_input->i_nb_resamplers > 0 )
359 /* Can happen in several circumstances :
360 * 1. A problem at the input (clock drift)
361 * 2. A small pause triggered by the user
362 * 3. Some delay in the output stage, causing a loss of lip
364 * Solution : resample the buffer to avoid a scratch.
366 mtime_t drift = p_buffer->start_date - start_date;
368 p_input->i_resamp_start_date = mdate();
369 p_input->i_resamp_start_drift = (int)drift;
372 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
374 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
376 msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
377 drift > 0 ? drift : -drift,
378 drift > 0 ? "in advance" : "late",
379 drift > 0 ? "down" : "up");
382 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
384 /* Resampling has been triggered previously (because of dates
385 * mismatch). We want the resampling to happen progressively so
386 * it isn't too audible to the listener. */
388 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
390 p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
394 p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
397 /* Check if everything is back to normal, in which case we can stop the
399 if( p_input->pp_resamplers[0]->input.i_rate ==
400 p_input->input.i_rate )
402 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
403 msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
404 mdate() - p_input->i_resamp_start_date );
406 else if( abs( (int)(p_buffer->start_date - start_date) ) <
407 abs( p_input->i_resamp_start_drift ) / 2 )
409 /* if we reduced the drift from half, then it is time to switch
410 * back the resampling direction. */
411 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
412 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
414 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
415 p_input->i_resamp_start_drift = 0;
417 else if( p_input->i_resamp_start_drift &&
418 ( abs( (int)(p_buffer->start_date - start_date) ) >
419 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
421 /* If the drift is increasing and not decreasing, than something
422 * is bad. We'd better stop the resampling right now. */
423 msg_Warn( p_aout, "timing screwed, stopping resampling" );
424 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
425 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
429 /* Adding the start date will be managed by aout_FifoPush(). */
430 p_buffer->end_date = start_date +
431 (p_buffer->end_date - p_buffer->start_date);
432 p_buffer->start_date = start_date;
434 /* Actually run the resampler now. */
435 if ( p_input->i_nb_resamplers > 0 )
437 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
438 p_input->i_nb_resamplers,
442 vlc_mutex_lock( &p_aout->input_fifos_lock );
443 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
444 vlc_mutex_unlock( &p_aout->input_fifos_lock );
449 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
450 vlc_value_t oldval, vlc_value_t newval, void *p_data )
452 aout_instance_t *p_aout = (aout_instance_t *)p_this;
454 char *psz_mode = newval.psz_string;
457 var_Get( p_aout, "audio-filter", &val );
458 psz_filter = val.psz_string;
460 if( !psz_mode || !*psz_mode )
463 var_Set( p_aout, "audio-filter", val );
467 if( !psz_filter || !*psz_filter )
469 val.psz_string = "visual";
470 var_Set( p_aout, "audio-filter", val );
474 if( strstr( psz_filter, "visual" ) == NULL )
476 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
477 strcat( psz_filter, ",visual" );
479 val.psz_string = psz_filter;
480 var_Set( p_aout, "audio-filter", val );
484 if( psz_mode && *psz_mode )
487 val.psz_string = psz_mode;
488 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
489 var_Set( p_aout, "effect-list", val);
492 if( psz_filter ) free( psz_filter );
494 aout_Restart( p_aout );
499 static aout_filter_t * allocateUserChannelMixer( aout_instance_t * p_aout,
500 audio_sample_format_t * p_input_format,
501 audio_sample_format_t * p_output_format )
503 aout_filter_t * p_channel_mixer;
505 /* Retreive user preferred channel mixer */
506 char * psz_name = config_GetPsz( p_aout, "audio-channel-mixer" );
508 /* Not specified => let the main pipeline do the mixing */
509 if ( ! psz_name ) return NULL;
511 /* Debug information */
512 aout_FormatsPrint( p_aout, "channel mixer", p_input_format,
515 /* Create a VLC object */
516 p_channel_mixer = vlc_object_create( p_aout, sizeof(aout_filter_t) );
517 if( p_channel_mixer == NULL )
519 msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
522 vlc_object_attach( p_channel_mixer , p_aout );
524 /* Attach the suitable module */
525 memcpy( &p_channel_mixer->input, p_input_format,
526 sizeof(audio_sample_format_t) );
527 memcpy( &p_channel_mixer->output, p_output_format,
528 sizeof(audio_sample_format_t) );
529 p_channel_mixer->p_module =
530 module_Need( p_channel_mixer,"audio filter", psz_name );
531 if( p_channel_mixer->p_module== NULL )
533 msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
534 vlc_object_detach( p_channel_mixer );
535 vlc_object_destroy( p_channel_mixer );
538 p_channel_mixer->b_continuity = VLC_FALSE;
541 return p_channel_mixer;