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.39 2003/11/02 06:33:49 hartman 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( ( psz_filters = config_GetPsz( p_aout , "audio-filter" ) ) )
80 char *psz_parser = psz_filters;
82 audio_sample_format_t format_in, format_out;
84 memcpy( &format_in, &p_aout->mixer.mixer,
85 sizeof( audio_sample_format_t ) );
86 memcpy( &format_out,&p_aout->mixer.mixer,
87 sizeof( audio_sample_format_t ) );
89 format_in.i_rate = p_input->input.i_rate;
90 format_out.i_rate = p_input->input.i_rate;
92 while( psz_parser && *psz_parser )
94 aout_filter_t * p_filter;
96 if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
98 msg_Dbg( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
102 while( *psz_parser == ' ' && *psz_parser == ',' )
106 if( ( psz_next = strchr( psz_parser , ',' ) ) )
110 if( *psz_parser =='\0' )
115 msg_Dbg( p_aout, "user filter %s", psz_parser );
117 /* Create a VLC object */
118 p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
119 if( p_filter == NULL )
121 msg_Err( p_aout, "cannot add user filter %s (skipped)",
123 psz_parser = psz_next;
127 vlc_object_attach( p_filter , p_aout );
128 memcpy( &p_filter->input, &format_in,
129 sizeof(audio_sample_format_t) );
130 memcpy( &p_filter->output, &format_out,
131 sizeof(audio_sample_format_t) );
134 module_Need( p_filter,"audio filter", psz_parser );
136 if( p_filter->p_module== NULL )
138 msg_Err( p_aout, "cannot add user filter %s (skipped)",
141 vlc_object_detach( p_filter );
142 vlc_object_destroy( p_filter );
143 psz_parser = psz_next;
147 p_filter->b_continuity = VLC_FALSE;
149 p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
151 /* next filter if any */
152 psz_parser = psz_next;
157 var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
158 text.psz_string = _("Visualizations");
159 var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
160 val.psz_string = ""; text.psz_string = _("Disable");
161 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
162 val.psz_string = "random"; text.psz_string = _("Random");
163 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
164 val.psz_string = "scope"; text.psz_string = _("Scope");
165 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
166 val.psz_string = "spectrum"; text.psz_string = _("Spectrum");
167 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
168 val.psz_string = "goom"; text.psz_string = _("Goom");
169 var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
170 if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
172 var_Set( p_aout, "visual", val );
174 var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
177 var_Create( p_aout, "audio-filter", VLC_VAR_STRING );
178 text.psz_string = _("Audio filters");
179 var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
180 var_Change( p_aout, "audio-filter", VLC_VAR_INHERITVALUE, &val, NULL );
181 if( var_Get( p_aout, "audio-filter", &val ) == VLC_SUCCESS )
183 var_Set( p_aout, "audio-filter", val );
185 //var_AddCallback( p_aout, "audio-filter", AudioFilterCallback, NULL );
187 /* Prepare hints for the buffer allocator. */
188 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
189 p_input->input_alloc.i_bytes_per_sec = -1;
191 if ( AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
193 p_input->i_nb_resamplers = 0;
197 /* Create resamplers. */
198 intermediate_format.i_rate = (__MAX(p_input->input.i_rate,
199 p_aout->mixer.mixer.i_rate)
200 * (100 + AOUT_MAX_RESAMPLING)) / 100;
201 if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
203 /* Just in case... */
204 intermediate_format.i_rate++;
206 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
207 &p_input->i_nb_resamplers,
208 &intermediate_format,
209 &p_aout->mixer.mixer ) < 0 )
211 msg_Err( p_aout, "couldn't set a resampler pipeline" );
213 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
214 p_input->i_nb_filters );
215 aout_FifoDestroy( p_aout, &p_input->fifo );
216 p_input->b_error = 1;
221 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
222 p_input->i_nb_resamplers,
223 &p_input->input_alloc );
225 /* Setup the initial rate of the resampler */
226 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
228 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
230 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
231 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
232 p_input->i_nb_filters,
233 &p_input->input_alloc );
235 /* i_bytes_per_sec is still == -1 if no filters */
236 p_input->input_alloc.i_bytes_per_sec = __MAX(
237 p_input->input_alloc.i_bytes_per_sec,
238 (int)(p_input->input.i_bytes_per_frame
239 * p_input->input.i_rate
240 / p_input->input.i_frame_length) );
241 /* Allocate in the heap, it is more convenient for the decoder. */
242 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
244 p_input->b_error = 0;
249 /*****************************************************************************
250 * aout_InputDelete : delete an input
251 *****************************************************************************
252 * This function must be entered with the mixer lock.
253 *****************************************************************************/
254 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
256 if ( p_input->b_error ) return 0;
258 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
259 p_input->i_nb_filters );
260 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
261 p_input->i_nb_resamplers );
262 aout_FifoDestroy( p_aout, &p_input->fifo );
267 /*****************************************************************************
268 * aout_InputPlay : play a buffer
269 *****************************************************************************
270 * This function must be entered with the input lock.
271 *****************************************************************************/
272 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
273 aout_buffer_t * p_buffer )
277 /* We don't care if someone changes the start date behind our back after
278 * this. We'll deal with that when pushing the buffer, and compensate
279 * with the next incoming buffer. */
280 vlc_mutex_lock( &p_aout->input_fifos_lock );
281 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
282 vlc_mutex_unlock( &p_aout->input_fifos_lock );
284 if ( start_date != 0 && start_date < mdate() )
286 /* The decoder is _very_ late. This can only happen if the user
287 * pauses the stream (or if the decoder is buggy, which cannot
289 msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
290 "clearing out", mdate() - start_date );
291 vlc_mutex_lock( &p_aout->input_fifos_lock );
292 aout_FifoSet( p_aout, &p_input->fifo, 0 );
293 p_input->p_first_byte_to_mix = NULL;
294 vlc_mutex_unlock( &p_aout->input_fifos_lock );
295 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
296 msg_Warn( p_aout, "timing screwed, stopping resampling" );
297 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
298 if ( p_input->i_nb_resamplers != 0 )
300 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
301 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
306 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
308 /* The decoder gives us f*cked up PTS. It's its business, but we
309 * can't present it anyway, so drop the buffer. */
310 msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
311 mdate() - p_buffer->start_date );
312 aout_BufferFree( p_buffer );
313 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
314 if ( p_input->i_nb_resamplers != 0 )
316 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
317 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
322 if ( start_date == 0 ) start_date = p_buffer->start_date;
324 /* Run pre-filters. */
326 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
329 /* Run the resampler if needed.
330 * We first need to calculate the output rate of this resampler. */
331 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
332 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
333 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
334 p_input->i_nb_resamplers > 0 )
336 /* Can happen in several circumstances :
337 * 1. A problem at the input (clock drift)
338 * 2. A small pause triggered by the user
339 * 3. Some delay in the output stage, causing a loss of lip
341 * Solution : resample the buffer to avoid a scratch.
343 mtime_t drift = p_buffer->start_date - start_date;
345 p_input->i_resamp_start_date = mdate();
346 p_input->i_resamp_start_drift = (int)drift;
349 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
351 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
353 msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
354 drift > 0 ? drift : -drift,
355 drift > 0 ? "in advance" : "late",
356 drift > 0 ? "down" : "up");
359 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
361 /* Resampling has been triggered previously (because of dates
362 * mismatch). We want the resampling to happen progressively so
363 * it isn't too audible to the listener. */
365 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
367 p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
371 p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
374 /* Check if everything is back to normal, in which case we can stop the
376 if( p_input->pp_resamplers[0]->input.i_rate ==
377 p_input->input.i_rate )
379 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
380 msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
381 mdate() - p_input->i_resamp_start_date );
383 else if( abs( (int)(p_buffer->start_date - start_date) ) <
384 abs( p_input->i_resamp_start_drift ) / 2 )
386 /* if we reduced the drift from half, then it is time to switch
387 * back the resampling direction. */
388 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
389 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
391 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
392 p_input->i_resamp_start_drift = 0;
394 else if( p_input->i_resamp_start_drift &&
395 ( abs( (int)(p_buffer->start_date - start_date) ) >
396 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
398 /* If the drift is increasing and not decreasing, than something
399 * is bad. We'd better stop the resampling right now. */
400 msg_Warn( p_aout, "timing screwed, stopping resampling" );
401 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
402 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
406 /* Adding the start date will be managed by aout_FifoPush(). */
407 p_buffer->end_date = start_date +
408 (p_buffer->end_date - p_buffer->start_date);
409 p_buffer->start_date = start_date;
411 /* Actually run the resampler now. */
412 if ( p_input->i_nb_resamplers > 0 )
414 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
415 p_input->i_nb_resamplers,
419 vlc_mutex_lock( &p_aout->input_fifos_lock );
420 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
421 vlc_mutex_unlock( &p_aout->input_fifos_lock );
427 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
428 vlc_value_t oldval, vlc_value_t newval, void *p_data )
430 aout_instance_t *p_aout = (aout_instance_t *)p_this;
431 input_thread_t *p_input;
434 char *psz_mode = newval.psz_string;
438 psz_filter = config_GetPsz( p_aout, "audio-filter" );
440 if( !psz_mode || !*psz_mode )
442 config_PutPsz( p_aout, "audio-filter", "" );
446 if( !psz_filter || !*psz_filter )
448 config_PutPsz( p_aout, "audio-filter", "visual" );
452 if( strstr( psz_filter, "visual" ) == NULL )
454 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
455 strcat( psz_filter, ",visual" );
457 config_PutPsz( p_aout, "audio-filter", psz_filter );
461 if( psz_mode && *psz_mode )
463 config_PutPsz( p_aout, "effect-list", psz_mode );
466 if( psz_filter ) free( psz_filter );