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.7 2002/08/21 22:41:59 massiot 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() */
32 #include "audio_output.h"
33 #include "aout_internal.h"
35 /*****************************************************************************
36 * aout_InputNew : allocate a new input and rework the filter pipeline
37 *****************************************************************************/
38 static aout_input_t * InputNew( aout_instance_t * p_aout,
39 audio_sample_format_t * p_format )
41 aout_input_t * p_input = malloc(sizeof(aout_input_t));
43 if ( p_input == NULL ) return NULL;
45 vlc_mutex_lock( &p_aout->mixer_lock );
47 if ( p_aout->i_nb_inputs == 0 )
49 /* Recreate the output using the new format. */
50 if ( aout_OutputNew( p_aout, p_format ) < 0 )
58 aout_MixerDelete( p_aout );
61 memcpy( &p_input->input, p_format,
62 sizeof(audio_sample_format_t) );
63 aout_FormatPrepare( &p_input->input );
66 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
67 p_input->p_first_byte_to_mix = NULL;
70 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
71 &p_input->i_nb_filters, &p_input->input,
72 &p_aout->mixer.mixer ) < 0 )
74 msg_Err( p_aout, "couldn't set an input pipeline" );
76 aout_FifoDestroy( p_aout, &p_input->fifo );
80 if ( !p_aout->i_nb_inputs )
82 aout_OutputDelete( p_aout );
87 p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
88 p_aout->i_nb_inputs++;
90 if ( aout_MixerNew( p_aout ) < 0 )
92 p_aout->i_nb_inputs--;
93 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
94 p_input->i_nb_filters );
95 aout_FifoDestroy( p_aout, &p_input->fifo );
99 if ( !p_aout->i_nb_inputs )
101 aout_OutputDelete( p_aout );
105 aout_MixerNew( p_aout );
107 vlc_mutex_unlock( &p_aout->mixer_lock );
112 vlc_mutex_unlock( &p_aout->mixer_lock );
114 /* Prepare hints for the buffer allocator. */
115 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
116 p_input->input_alloc.i_bytes_per_sec = -1;
118 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
119 p_input->i_nb_filters,
120 &p_input->input_alloc );
122 /* i_bytes_per_sec is still == -1 if no filters */
123 p_input->input_alloc.i_bytes_per_sec = __MAX(
124 p_input->input_alloc.i_bytes_per_sec,
125 p_input->input.i_bytes_per_frame
126 * p_input->input.i_rate
127 / p_input->input.i_frame_length );
128 /* Allocate in the heap, it is more convenient for the decoder. */
129 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
131 msg_Dbg( p_aout, "input 0x%x created", p_input );
135 aout_input_t * __aout_InputNew( vlc_object_t * p_this,
136 aout_instance_t ** pp_aout,
137 audio_sample_format_t * p_format )
139 /* Create an audio output if there is none. */
140 *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
142 if( *pp_aout == NULL )
144 msg_Dbg( p_this, "no aout present, spawning one" );
146 *pp_aout = aout_NewInstance( p_this );
147 /* Everything failed, I'm a loser, I just wanna die */
148 if( *pp_aout == NULL )
155 vlc_object_release( *pp_aout );
158 return InputNew( *pp_aout, p_format );
161 /*****************************************************************************
162 * aout_InputDelete : delete an input
163 *****************************************************************************/
164 void aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
168 vlc_mutex_lock( &p_aout->mixer_lock );
170 for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
172 if ( p_aout->pp_inputs[i_input] == p_input )
178 if ( i_input == p_aout->i_nb_inputs )
180 msg_Err( p_aout, "cannot find an input to delete" );
184 /* Remove the input from the list. */
185 memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
186 (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
187 p_aout->i_nb_inputs--;
189 vlc_mutex_unlock( &p_aout->mixer_lock );
191 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
192 p_input->i_nb_filters );
193 aout_FifoDestroy( p_aout, &p_input->fifo );
197 if ( !p_aout->i_nb_inputs )
199 aout_OutputDelete( p_aout );
200 aout_MixerDelete( p_aout );
203 msg_Dbg( p_aout, "input 0x%x destroyed", p_input );
206 /*****************************************************************************
207 * aout_InputPlay : play a buffer
208 *****************************************************************************/
209 void aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
210 aout_buffer_t * p_buffer )
212 mtime_t start_date, duration;
214 vlc_mutex_lock( &p_aout->input_lock );
215 while( p_aout->b_change_requested )
217 vlc_cond_wait( &p_aout->input_signal, &p_aout->input_lock );
219 p_aout->i_inputs_active++;
220 vlc_mutex_unlock( &p_aout->input_lock );
222 /* We don't care if someone changes the start date behind our back after
223 * this. We'll deal with that when pushing the buffer, and compensate
224 * with the next incoming buffer. */
225 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
227 if ( start_date != 0 && start_date < mdate() )
229 /* The decoder is _very_ late. This can only happen if the user
230 * pauses the stream (or if the decoder is buggy, which cannot
232 msg_Warn( p_aout, "Computed PTS is out of range (%lld), clearing out",
234 vlc_mutex_lock( &p_aout->mixer_lock );
235 aout_FifoSet( p_aout, &p_input->fifo, 0 );
236 vlc_mutex_unlock( &p_aout->mixer_lock );
240 if ( p_buffer->start_date < mdate() - AOUT_MIN_PREPARE_TIME )
242 /* The decoder gives us f*cked up PTS. It's its business, but we
243 * can't present it anyway, so drop the buffer. */
244 msg_Warn( p_aout, "PTS is out of range (%lld), dropping buffer",
245 mdate() - p_buffer->start_date );
246 aout_BufferFree( p_buffer );
248 vlc_mutex_lock( &p_aout->input_lock );
249 p_aout->i_inputs_active--;
250 vlc_cond_broadcast( &p_aout->input_signal );
251 vlc_mutex_unlock( &p_aout->input_lock );
255 if ( start_date == 0 ) start_date = p_buffer->start_date;
257 if ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
258 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE )
260 /* Can happen in several circumstances :
261 * 1. A problem at the input (clock drift)
262 * 2. A small pause triggered by the user
263 * 3. Some delay in the output stage, causing a loss of lip
265 * Solution : resample the buffer to avoid a scratch.
267 audio_sample_format_t new_output;
268 int i_ratio, i_nb_filters;
269 mtime_t old_duration;
270 aout_filter_t * pp_filters[AOUT_MAX_FILTERS];
271 aout_buffer_t * p_new_buffer;
272 aout_alloc_t dummy_alloc;
273 mtime_t drift = p_buffer->start_date - start_date;
275 msg_Warn( p_aout, "buffer is %lld %s, resampling",
276 drift > 0 ? drift : -drift,
277 drift > 0 ? "in advance" : "late" );
278 memcpy( &new_output, &p_aout->mixer.mixer,
279 sizeof(audio_sample_format_t) );
280 old_duration = p_buffer->end_date - p_buffer->start_date;
281 duration = p_buffer->end_date - start_date;
282 i_ratio = duration * 100 / old_duration;
283 /* If the ratio is too != 100, the sound quality will be awful. */
284 if ( i_ratio < 66 /* % */ )
286 duration = old_duration * 66 / 100;
288 if ( i_ratio > 150 /* % */ )
290 duration = old_duration * 150 / 100;
292 new_output.i_rate = new_output.i_rate * duration / old_duration;
294 if ( aout_FiltersCreatePipeline( p_aout, pp_filters,
295 &i_nb_filters, &p_input->input,
298 msg_Err( p_aout, "couldn't set an input pipeline for resampling" );
299 vlc_mutex_lock( &p_aout->mixer_lock );
300 aout_FifoSet( p_aout, &p_input->fifo, 0 );
301 vlc_mutex_unlock( &p_aout->mixer_lock );
302 aout_BufferFree( p_buffer );
304 vlc_mutex_lock( &p_aout->input_lock );
305 p_aout->i_inputs_active--;
306 vlc_cond_broadcast( &p_aout->input_signal );
307 vlc_mutex_unlock( &p_aout->input_lock );
311 dummy_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
312 dummy_alloc.i_bytes_per_sec = -1;
313 aout_FiltersHintBuffers( p_aout, pp_filters, i_nb_filters,
315 dummy_alloc.i_bytes_per_sec = __MAX(
316 dummy_alloc.i_bytes_per_sec,
317 p_input->input.i_bytes_per_frame
318 * p_input->input.i_rate
319 / p_input->input.i_frame_length );
320 dummy_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
322 aout_BufferAlloc( &dummy_alloc, old_duration, NULL, p_new_buffer );
323 memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
324 p_buffer->i_nb_bytes );
325 p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
326 p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
327 aout_BufferFree( p_buffer );
328 p_buffer = p_new_buffer;
330 aout_FiltersPlay( p_aout, pp_filters, i_nb_filters,
333 aout_FiltersDestroyPipeline( p_aout, pp_filters,
338 /* No resampling needed (except maybe the one imposed by the
340 duration = p_buffer->end_date - p_buffer->start_date;
341 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
345 vlc_mutex_lock( &p_aout->mixer_lock );
346 /* Adding the start date will be managed by aout_FifoPush(). */
347 p_buffer->start_date = start_date;
348 p_buffer->end_date = start_date + duration;
349 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
350 vlc_mutex_unlock( &p_aout->mixer_lock );
352 vlc_mutex_lock( &p_aout->input_lock );
353 p_aout->i_inputs_active--;
354 vlc_cond_broadcast( &p_aout->input_signal );
355 vlc_mutex_unlock( &p_aout->input_lock );