1 /*****************************************************************************
2 * input.c : internal management of input streams for the audio output
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: input.c,v 1.40 2003/11/02 12:22:45 gbazin 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 * );
43 /*****************************************************************************
44 * aout_InputNew : allocate a new input and rework the filter pipeline
45 *****************************************************************************/
46 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
48 audio_sample_format_t intermediate_format;
49 vlc_value_t val, text;
52 aout_FormatPrint( p_aout, "input", &p_input->input );
55 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
56 p_input->p_first_byte_to_mix = NULL;
58 /* Prepare format structure */
59 memcpy( &intermediate_format, &p_aout->mixer.mixer,
60 sizeof(audio_sample_format_t) );
61 intermediate_format.i_rate = p_input->input.i_rate;
64 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
65 &p_input->i_nb_filters,
70 msg_Err( p_aout, "couldn't set an input pipeline" );
72 aout_FifoDestroy( p_aout, &p_input->fifo );
77 /* Now add user filters */
78 if( var_Type( p_aout, "visual" ) == 0 )
80 var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
81 text.psz_string = _("Visualizations");
82 var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
83 val.psz_string = ""; text.psz_string = _("Disable");
84 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
85 val.psz_string = "random"; text.psz_string = _("Random");
86 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
87 val.psz_string = "scope"; text.psz_string = _("Scope");
88 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
89 val.psz_string = "spectrum"; text.psz_string = _("Spectrum");
90 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
91 val.psz_string = "goom"; text.psz_string = _("Goom");
92 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
93 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
95 var_Set( p_aout, "visual", val );
96 if( val.psz_string ) free( val.psz_string );
98 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
101 if( var_Type( p_aout, "audio-filter" ) == 0 )
103 var_Create( p_aout, "audio-filter",
104 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
105 text.psz_string = _("Audio filters");
106 var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
109 var_Get( p_aout, "audio-filter", &val );
110 psz_filters = val.psz_string;
111 if( psz_filters && *psz_filters )
113 char *psz_parser = psz_filters;
115 audio_sample_format_t format_in, format_out;
117 memcpy( &format_in, &p_aout->mixer.mixer,
118 sizeof( audio_sample_format_t ) );
119 memcpy( &format_out,&p_aout->mixer.mixer,
120 sizeof( audio_sample_format_t ) );
122 format_in.i_rate = p_input->input.i_rate;
123 format_out.i_rate = p_input->input.i_rate;
125 while( psz_parser && *psz_parser )
127 aout_filter_t * p_filter;
129 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
131 msg_Dbg( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
135 while( *psz_parser == ' ' && *psz_parser == ',' )
139 if( ( psz_next = strchr( psz_parser , ',' ) ) )
143 if( *psz_parser =='\0' )
148 msg_Dbg( p_aout, "user filter %s", psz_parser );
150 /* Create a VLC object */
151 p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
152 if( p_filter == NULL )
154 msg_Err( p_aout, "cannot add user filter %s (skipped)",
156 psz_parser = psz_next;
160 vlc_object_attach( p_filter , p_aout );
161 memcpy( &p_filter->input, &format_in,
162 sizeof(audio_sample_format_t) );
163 memcpy( &p_filter->output, &format_out,
164 sizeof(audio_sample_format_t) );
167 module_Need( p_filter,"audio filter", psz_parser );
169 if( p_filter->p_module== NULL )
171 msg_Err( p_aout, "cannot add user filter %s (skipped)",
174 vlc_object_detach( p_filter );
175 vlc_object_destroy( p_filter );
176 psz_parser = psz_next;
180 p_filter->b_continuity = VLC_FALSE;
182 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
184 /* next filter if any */
185 psz_parser = psz_next;
188 if( psz_filters ) free( psz_filters );
190 /* Prepare hints for the buffer allocator. */
191 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
192 p_input->input_alloc.i_bytes_per_sec = -1;
194 if ( AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
196 p_input->i_nb_resamplers = 0;
200 /* Create resamplers. */
201 intermediate_format.i_rate = (__MAX(p_input->input.i_rate,
202 p_aout->mixer.mixer.i_rate)
203 * (100 + AOUT_MAX_RESAMPLING)) / 100;
204 if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
206 /* Just in case... */
207 intermediate_format.i_rate++;
209 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
210 &p_input->i_nb_resamplers,
211 &intermediate_format,
212 &p_aout->mixer.mixer ) < 0 )
214 msg_Err( p_aout, "couldn't set a resampler pipeline" );
216 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
217 p_input->i_nb_filters );
218 aout_FifoDestroy( p_aout, &p_input->fifo );
219 var_Destroy( p_aout, "visual" );
220 p_input->b_error = 1;
225 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
226 p_input->i_nb_resamplers,
227 &p_input->input_alloc );
229 /* Setup the initial rate of the resampler */
230 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
232 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
234 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
235 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
236 p_input->i_nb_filters,
237 &p_input->input_alloc );
239 /* i_bytes_per_sec is still == -1 if no filters */
240 p_input->input_alloc.i_bytes_per_sec = __MAX(
241 p_input->input_alloc.i_bytes_per_sec,
242 (int)(p_input->input.i_bytes_per_frame
243 * p_input->input.i_rate
244 / p_input->input.i_frame_length) );
245 /* Allocate in the heap, it is more convenient for the decoder. */
246 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
248 p_input->b_error = 0;
253 /*****************************************************************************
254 * aout_InputDelete : delete an input
255 *****************************************************************************
256 * This function must be entered with the mixer lock.
257 *****************************************************************************/
258 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
260 if ( p_input->b_error ) return 0;
262 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
263 p_input->i_nb_filters );
264 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
265 p_input->i_nb_resamplers );
266 aout_FifoDestroy( p_aout, &p_input->fifo );
271 /*****************************************************************************
272 * aout_InputPlay : play a buffer
273 *****************************************************************************
274 * This function must be entered with the input lock.
275 *****************************************************************************/
276 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
277 aout_buffer_t * p_buffer )
281 /* We don't care if someone changes the start date behind our back after
282 * this. We'll deal with that when pushing the buffer, and compensate
283 * with the next incoming buffer. */
284 vlc_mutex_lock( &p_aout->input_fifos_lock );
285 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
286 vlc_mutex_unlock( &p_aout->input_fifos_lock );
288 if ( start_date != 0 && start_date < mdate() )
290 /* The decoder is _very_ late. This can only happen if the user
291 * pauses the stream (or if the decoder is buggy, which cannot
293 msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
294 "clearing out", mdate() - start_date );
295 vlc_mutex_lock( &p_aout->input_fifos_lock );
296 aout_FifoSet( p_aout, &p_input->fifo, 0 );
297 p_input->p_first_byte_to_mix = NULL;
298 vlc_mutex_unlock( &p_aout->input_fifos_lock );
299 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
300 msg_Warn( p_aout, "timing screwed, stopping resampling" );
301 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
302 if ( p_input->i_nb_resamplers != 0 )
304 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
305 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
310 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
312 /* The decoder gives us f*cked up PTS. It's its business, but we
313 * can't present it anyway, so drop the buffer. */
314 msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
315 mdate() - p_buffer->start_date );
316 aout_BufferFree( p_buffer );
317 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
318 if ( p_input->i_nb_resamplers != 0 )
320 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
321 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
326 if ( start_date == 0 ) start_date = p_buffer->start_date;
328 /* Run pre-filters. */
330 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
333 /* Run the resampler if needed.
334 * We first need to calculate the output rate of this resampler. */
335 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
336 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
337 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
338 p_input->i_nb_resamplers > 0 )
340 /* Can happen in several circumstances :
341 * 1. A problem at the input (clock drift)
342 * 2. A small pause triggered by the user
343 * 3. Some delay in the output stage, causing a loss of lip
345 * Solution : resample the buffer to avoid a scratch.
347 mtime_t drift = p_buffer->start_date - start_date;
349 p_input->i_resamp_start_date = mdate();
350 p_input->i_resamp_start_drift = (int)drift;
353 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
355 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
357 msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
358 drift > 0 ? drift : -drift,
359 drift > 0 ? "in advance" : "late",
360 drift > 0 ? "down" : "up");
363 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
365 /* Resampling has been triggered previously (because of dates
366 * mismatch). We want the resampling to happen progressively so
367 * it isn't too audible to the listener. */
369 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
371 p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
375 p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
378 /* Check if everything is back to normal, in which case we can stop the
380 if( p_input->pp_resamplers[0]->input.i_rate ==
381 p_input->input.i_rate )
383 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
384 msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
385 mdate() - p_input->i_resamp_start_date );
387 else if( abs( (int)(p_buffer->start_date - start_date) ) <
388 abs( p_input->i_resamp_start_drift ) / 2 )
390 /* if we reduced the drift from half, then it is time to switch
391 * back the resampling direction. */
392 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
393 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
395 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
396 p_input->i_resamp_start_drift = 0;
398 else if( p_input->i_resamp_start_drift &&
399 ( abs( (int)(p_buffer->start_date - start_date) ) >
400 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
402 /* If the drift is increasing and not decreasing, than something
403 * is bad. We'd better stop the resampling right now. */
404 msg_Warn( p_aout, "timing screwed, stopping resampling" );
405 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
406 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
410 /* Adding the start date will be managed by aout_FifoPush(). */
411 p_buffer->end_date = start_date +
412 (p_buffer->end_date - p_buffer->start_date);
413 p_buffer->start_date = start_date;
415 /* Actually run the resampler now. */
416 if ( p_input->i_nb_resamplers > 0 )
418 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
419 p_input->i_nb_resamplers,
423 vlc_mutex_lock( &p_aout->input_fifos_lock );
424 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
425 vlc_mutex_unlock( &p_aout->input_fifos_lock );
430 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
431 vlc_value_t oldval, vlc_value_t newval, void *p_data )
433 aout_instance_t *p_aout = (aout_instance_t *)p_this;
435 char *psz_mode = newval.psz_string;
438 var_Get( p_aout, "audio-filter", &val );
439 psz_filter = val.psz_string;
441 if( !psz_mode || !*psz_mode )
444 var_Set( p_aout, "audio-filter", val );
448 if( !psz_filter || !*psz_filter )
450 val.psz_string = "visual";
451 var_Set( p_aout, "audio-filter", val );
455 if( strstr( psz_filter, "visual" ) == NULL )
457 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
458 strcat( psz_filter, ",visual" );
460 val.psz_string = psz_filter;
461 var_Set( p_aout, "audio-filter", val );
465 if( psz_mode && *psz_mode )
468 val.psz_string = psz_mode;
469 var_Create( p_aout, "effect-list", VLC_VAR_STRING );
470 var_Set( p_aout, "effect-list", val);
473 if( psz_filter ) free( psz_filter );
475 aout_Restart( p_aout );