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.36 2003/08/19 21:20:00 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() */
36 #include "audio_output.h"
37 #include "aout_internal.h"
39 typedef struct user_filter_t
42 aout_filter_t * p_filter;
43 audio_sample_format_t filter_inter_format;
44 struct user_filter_t *p_next;
48 /*****************************************************************************
49 * aout_InputNew : allocate a new input and rework the filter pipeline
50 *****************************************************************************/
51 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
53 audio_sample_format_t intermediate_format;
55 headphone_intermediate_format;
56 aout_filter_t * p_headphone_filter;
57 vlc_bool_t b_use_headphone_filter = VLC_FALSE;
60 vlc_bool_t b_end=VLC_FALSE;
62 user_filter_t* p_first_filter=NULL;
63 user_filter_t* p_current_filter=NULL;
65 char * psz_filters, *psz_eof;
67 aout_FormatPrint( p_aout, "input", &p_input->input );
70 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
71 p_input->p_first_byte_to_mix = NULL;
73 /* Prepare format structure */
74 memcpy( &intermediate_format, &p_aout->mixer.mixer,
75 sizeof(audio_sample_format_t) );
76 intermediate_format.i_rate = p_input->input.i_rate;
79 /* Build the list of user filters */
80 psz_filters = config_GetPsz( p_aout , "audio-filter" );
82 p_first_filter = (user_filter_t *)malloc( sizeof( user_filter_t ) );
85 msg_Err( p_aout, "Out of memory" );
88 p_current_filter = p_first_filter;
89 p_current_filter->p_next = NULL;
90 memcpy( &p_current_filter->filter_inter_format,
91 &p_aout->mixer.mixer,sizeof(audio_sample_format_t));
92 p_current_filter->filter_inter_format.i_rate= p_input->input.i_rate;
94 if(psz_filters != NULL)
96 msg_Dbg(p_aout,"Building list of user filters");
99 psz_eof = strchr( psz_filters , ',' );
103 psz_eof = strchr( psz_filters,'\0');
110 msg_Dbg(p_aout,"Adding user filter: %s",psz_filters);
112 /* Append the new filter to the list */
113 p_current_filter->p_next =
114 (user_filter_t *)malloc(sizeof(user_filter_t));
115 if( !p_current_filter->p_next )
117 msg_Err( p_aout, "Out of memory" );
121 memcpy( &p_current_filter->p_next->filter_inter_format,
122 &p_current_filter->filter_inter_format,
123 sizeof(audio_sample_format_t) );
125 p_current_filter->p_next->filter_inter_format.i_rate =
126 p_current_filter->filter_inter_format.i_rate;
128 p_current_filter->p_next->
129 filter_inter_format.i_physical_channels =
130 p_current_filter->filter_inter_format.i_physical_channels;
132 p_current_filter->p_next->
133 filter_inter_format.i_original_channels =
134 p_current_filter->filter_inter_format.i_original_channels;
136 p_current_filter->p_next->filter_inter_format.i_bytes_per_frame =
137 p_current_filter->p_next->
138 filter_inter_format.i_bytes_per_frame *
139 aout_FormatNbChannels(&p_current_filter->p_next->
140 filter_inter_format) /
141 aout_FormatNbChannels( &intermediate_format);
143 /* Go to next filter */
144 p_current_filter = p_current_filter->p_next;
146 p_current_filter->p_next= NULL;
148 p_current_filter->psz_name = strdup(psz_filters);
150 psz_filters = psz_eof;
154 if(!*psz_filters || b_end == VLC_TRUE)
160 /* Headphone filter add-ons. */
162 memcpy( &headphone_intermediate_format, &p_aout->mixer.mixer,
163 sizeof(audio_sample_format_t) );
164 headphone_intermediate_format.i_rate =
165 p_input->input.i_rate;
166 if ( config_GetInt( p_aout , "headphone-opt" ) )
168 /* Do we use heaphone filter ? */
169 if ( intermediate_format.i_physical_channels
170 == ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
171 && ( intermediate_format.i_format != VLC_FOURCC('f','l','3','2')
172 || intermediate_format.i_format != VLC_FOURCC('f','i','3','2')
175 b_use_headphone_filter = VLC_TRUE;
178 if ( b_use_headphone_filter == VLC_TRUE )
180 /* Split the filter pipeline. */
181 headphone_intermediate_format.i_physical_channels =
182 p_input->input.i_physical_channels;
183 headphone_intermediate_format.i_original_channels =
184 p_input->input.i_original_channels;
185 headphone_intermediate_format.i_bytes_per_frame =
186 headphone_intermediate_format.i_bytes_per_frame
187 * aout_FormatNbChannels( &headphone_intermediate_format )
188 / aout_FormatNbChannels( &intermediate_format );
191 /* Create filters. */
192 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
193 &p_input->i_nb_filters, &p_input->input,
194 &intermediate_format ) < 0 )
196 msg_Err( p_aout, "couldn't set an input pipeline" );
198 aout_FifoDestroy( p_aout, &p_input->fifo );
199 p_input->b_error = 1;
204 /* Create filters. */
205 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
206 &p_input->i_nb_filters,
211 msg_Err( p_aout, "couldn't set an input pipeline" );
213 aout_FifoDestroy( p_aout, &p_input->fifo );
214 p_input->b_error = 1;
217 if( p_first_filter->p_next)
219 msg_Dbg(p_aout,"Searching user filters...");
220 p_current_filter = p_first_filter->p_next;
222 /* Ok, we now start to get the filters, from the first one */
223 while( p_current_filter )
225 /* Create a VLC object */
226 p_current_filter->p_filter = vlc_object_create( p_aout,
227 sizeof(aout_filter_t) );
228 if(p_current_filter->p_filter == NULL )
230 msg_Err( p_aout, "couldn't open the requested filter module" );
231 aout_FifoDestroy( p_aout, &p_input->fifo );
232 p_input->b_error = 1;
235 vlc_object_attach( p_current_filter->p_filter , p_aout );
236 memcpy(&p_current_filter->p_filter->input,
237 &p_current_filter->filter_inter_format,
238 sizeof(audio_sample_format_t) );
240 if( p_current_filter->p_next)
242 memcpy(&p_current_filter->p_filter->output,
243 &p_current_filter->p_next->filter_inter_format,
244 sizeof(audio_sample_format_t) );
248 memcpy(&p_current_filter->p_filter->output,
249 &intermediate_format,
250 sizeof(audio_sample_format_t) );
252 p_current_filter->p_filter->p_module =
253 module_Need(p_current_filter->p_filter,"audio filter",
254 p_current_filter->psz_name);
255 if(p_current_filter->p_filter->p_module== NULL )
257 vlc_object_detach( p_current_filter->p_filter);
258 vlc_object_destroy( p_current_filter->p_filter);
260 msg_Err( p_aout, "couldn't open the requested module" );
261 aout_FifoDestroy( p_aout, &p_input->fifo );
262 p_input->b_error = 1;
266 p_current_filter->p_filter->b_continuity = VLC_FALSE;
267 p_input->pp_filters[p_input->i_nb_filters++] =
268 p_current_filter->p_filter;
271 p_current_filter = p_current_filter->p_next;
276 p_current_filter = p_first_filter;
277 while (p_current_filter)
279 user_filter_t * p_old_filter = p_current_filter;
280 p_current_filter = p_current_filter->p_next;
286 /* Headphone filter add-ons. */
287 if ( b_use_headphone_filter == VLC_TRUE )
289 /* create a vlc object */
290 p_headphone_filter = vlc_object_create( p_aout
291 , sizeof(aout_filter_t) );
292 if ( p_headphone_filter == NULL )
294 msg_Err( p_aout, "couldn't open the headphone virtual spatialization module" );
295 aout_FifoDestroy( p_aout, &p_input->fifo );
296 p_input->b_error = 1;
299 vlc_object_attach( p_headphone_filter, p_aout );
301 /* find the headphone filter */
302 memcpy( &p_headphone_filter->input, &headphone_intermediate_format
303 , sizeof(audio_sample_format_t) );
304 memcpy( &p_headphone_filter->output, &intermediate_format
305 , sizeof(audio_sample_format_t) );
306 p_headphone_filter->p_module = module_Need( p_headphone_filter, "audio filter"
308 if ( p_headphone_filter->p_module == NULL )
310 vlc_object_detach( p_headphone_filter );
311 vlc_object_destroy( p_headphone_filter );
313 msg_Err( p_aout, "couldn't open the headphone virtual spatialization module" );
314 aout_FifoDestroy( p_aout, &p_input->fifo );
315 p_input->b_error = 1;
320 p_headphone_filter->b_continuity = VLC_FALSE;
321 p_input->pp_filters[p_input->i_nb_filters++] = p_headphone_filter;
325 /* Prepare hints for the buffer allocator. */
326 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
327 p_input->input_alloc.i_bytes_per_sec = -1;
329 if ( AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
331 p_input->i_nb_resamplers = 0;
335 /* Create resamplers. */
336 intermediate_format.i_rate = (__MAX(p_input->input.i_rate,
337 p_aout->mixer.mixer.i_rate)
338 * (100 + AOUT_MAX_RESAMPLING)) / 100;
339 if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
341 /* Just in case... */
342 intermediate_format.i_rate++;
344 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
345 &p_input->i_nb_resamplers,
346 &intermediate_format,
347 &p_aout->mixer.mixer ) < 0 )
349 msg_Err( p_aout, "couldn't set a resampler pipeline" );
351 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
352 p_input->i_nb_filters );
353 aout_FifoDestroy( p_aout, &p_input->fifo );
354 p_input->b_error = 1;
359 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
360 p_input->i_nb_resamplers,
361 &p_input->input_alloc );
363 /* Setup the initial rate of the resampler */
364 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
366 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
368 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
369 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
370 p_input->i_nb_filters,
371 &p_input->input_alloc );
373 /* i_bytes_per_sec is still == -1 if no filters */
374 p_input->input_alloc.i_bytes_per_sec = __MAX(
375 p_input->input_alloc.i_bytes_per_sec,
376 (int)(p_input->input.i_bytes_per_frame
377 * p_input->input.i_rate
378 / p_input->input.i_frame_length) );
379 /* Allocate in the heap, it is more convenient for the decoder. */
380 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
382 p_input->b_error = 0;
387 /*****************************************************************************
388 * aout_InputDelete : delete an input
389 *****************************************************************************
390 * This function must be entered with the mixer lock.
391 *****************************************************************************/
392 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
394 if ( p_input->b_error ) return 0;
396 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
397 p_input->i_nb_filters );
398 aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
399 p_input->i_nb_resamplers );
400 aout_FifoDestroy( p_aout, &p_input->fifo );
405 /*****************************************************************************
406 * aout_InputPlay : play a buffer
407 *****************************************************************************
408 * This function must be entered with the input lock.
409 *****************************************************************************/
410 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
411 aout_buffer_t * p_buffer )
415 /* We don't care if someone changes the start date behind our back after
416 * this. We'll deal with that when pushing the buffer, and compensate
417 * with the next incoming buffer. */
418 vlc_mutex_lock( &p_aout->input_fifos_lock );
419 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
420 vlc_mutex_unlock( &p_aout->input_fifos_lock );
422 if ( start_date != 0 && start_date < mdate() )
424 /* The decoder is _very_ late. This can only happen if the user
425 * pauses the stream (or if the decoder is buggy, which cannot
427 msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
428 "clearing out", mdate() - start_date );
429 vlc_mutex_lock( &p_aout->input_fifos_lock );
430 aout_FifoSet( p_aout, &p_input->fifo, 0 );
431 p_input->p_first_byte_to_mix = NULL;
432 vlc_mutex_unlock( &p_aout->input_fifos_lock );
433 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
434 msg_Warn( p_aout, "timing screwed, stopping resampling" );
435 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
436 if ( p_input->i_nb_resamplers != 0 )
438 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
439 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
444 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
446 /* The decoder gives us f*cked up PTS. It's its business, but we
447 * can't present it anyway, so drop the buffer. */
448 msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
449 mdate() - p_buffer->start_date );
450 aout_BufferFree( p_buffer );
451 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
452 if ( p_input->i_nb_resamplers != 0 )
454 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
455 p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
460 if ( start_date == 0 ) start_date = p_buffer->start_date;
462 /* Run pre-filters. */
464 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
467 /* Run the resampler if needed.
468 * We first need to calculate the output rate of this resampler. */
469 if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
470 ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
471 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
472 p_input->i_nb_resamplers > 0 )
474 /* Can happen in several circumstances :
475 * 1. A problem at the input (clock drift)
476 * 2. A small pause triggered by the user
477 * 3. Some delay in the output stage, causing a loss of lip
479 * Solution : resample the buffer to avoid a scratch.
481 mtime_t drift = p_buffer->start_date - start_date;
483 p_input->i_resamp_start_date = mdate();
484 p_input->i_resamp_start_drift = (int)drift;
487 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
489 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
491 msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
492 drift > 0 ? drift : -drift,
493 drift > 0 ? "in advance" : "late",
494 drift > 0 ? "down" : "up");
497 if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
499 /* Resampling has been triggered previously (because of dates
500 * mismatch). We want the resampling to happen progressively so
501 * it isn't too audible to the listener. */
503 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
505 p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
509 p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
512 /* Check if everything is back to normal, in which case we can stop the
514 if( p_input->pp_resamplers[0]->input.i_rate ==
515 p_input->input.i_rate )
517 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
518 msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
519 mdate() - p_input->i_resamp_start_date );
521 else if( abs( (int)(p_buffer->start_date - start_date) ) <
522 abs( p_input->i_resamp_start_drift ) / 2 )
524 /* if we reduced the drift from half, then it is time to switch
525 * back the resampling direction. */
526 if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
527 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
529 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
530 p_input->i_resamp_start_drift = 0;
532 else if( p_input->i_resamp_start_drift &&
533 ( abs( (int)(p_buffer->start_date - start_date) ) >
534 abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
536 /* If the drift is increasing and not decreasing, than something
537 * is bad. We'd better stop the resampling right now. */
538 msg_Warn( p_aout, "timing screwed, stopping resampling" );
539 p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
540 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
544 /* Adding the start date will be managed by aout_FifoPush(). */
545 p_buffer->end_date = start_date +
546 (p_buffer->end_date - p_buffer->start_date);
547 p_buffer->start_date = start_date;
549 /* Actually run the resampler now. */
550 if ( p_input->i_nb_resamplers > 0 )
552 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
553 p_input->i_nb_resamplers,
557 vlc_mutex_lock( &p_aout->input_fifos_lock );
558 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
559 vlc_mutex_unlock( &p_aout->input_fifos_lock );