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.10 2002/08/28 22:25:39 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() */
36 #include "audio_output.h"
37 #include "aout_internal.h"
39 /*****************************************************************************
40 * aout_InputNew : allocate a new input and rework the filter pipeline
41 *****************************************************************************/
42 static aout_input_t * InputNew( aout_instance_t * p_aout,
43 audio_sample_format_t * p_format )
45 aout_input_t * p_input = malloc(sizeof(aout_input_t));
47 if ( p_input == NULL ) return NULL;
49 vlc_mutex_lock( &p_aout->mixer_lock );
51 if ( p_aout->i_nb_inputs == 0 )
53 /* Recreate the output using the new format. */
54 if ( aout_OutputNew( p_aout, p_format ) < 0 )
62 aout_MixerDelete( p_aout );
65 memcpy( &p_input->input, p_format,
66 sizeof(audio_sample_format_t) );
67 aout_FormatPrepare( &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;
74 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
75 &p_input->i_nb_filters, &p_input->input,
76 &p_aout->mixer.mixer ) < 0 )
78 msg_Err( p_aout, "couldn't set an input pipeline" );
80 aout_FifoDestroy( p_aout, &p_input->fifo );
84 if ( !p_aout->i_nb_inputs )
86 aout_OutputDelete( p_aout );
91 p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
92 p_aout->i_nb_inputs++;
94 if ( aout_MixerNew( p_aout ) < 0 )
96 p_aout->i_nb_inputs--;
97 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
98 p_input->i_nb_filters );
99 aout_FifoDestroy( p_aout, &p_input->fifo );
103 if ( !p_aout->i_nb_inputs )
105 aout_OutputDelete( p_aout );
109 aout_MixerNew( p_aout );
111 vlc_mutex_unlock( &p_aout->mixer_lock );
116 vlc_mutex_unlock( &p_aout->mixer_lock );
118 /* Prepare hints for the buffer allocator. */
119 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
120 p_input->input_alloc.i_bytes_per_sec = -1;
122 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
123 p_input->i_nb_filters,
124 &p_input->input_alloc );
126 /* i_bytes_per_sec is still == -1 if no filters */
127 p_input->input_alloc.i_bytes_per_sec = __MAX(
128 p_input->input_alloc.i_bytes_per_sec,
129 p_input->input.i_bytes_per_frame
130 * p_input->input.i_rate
131 / p_input->input.i_frame_length );
132 /* Allocate in the heap, it is more convenient for the decoder. */
133 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
135 msg_Dbg( p_aout, "input 0x%x created", p_input );
139 aout_input_t * __aout_InputNew( vlc_object_t * p_this,
140 aout_instance_t ** pp_aout,
141 audio_sample_format_t * p_format )
143 /* Create an audio output if there is none. */
144 *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
146 if( *pp_aout == NULL )
148 msg_Dbg( p_this, "no aout present, spawning one" );
150 *pp_aout = aout_NewInstance( p_this );
151 /* Everything failed, I'm a loser, I just wanna die */
152 if( *pp_aout == NULL )
159 vlc_object_release( *pp_aout );
162 return InputNew( *pp_aout, p_format );
165 /*****************************************************************************
166 * aout_InputDelete : delete an input
167 *****************************************************************************/
168 void aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
172 vlc_mutex_lock( &p_aout->mixer_lock );
174 for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
176 if ( p_aout->pp_inputs[i_input] == p_input )
182 if ( i_input == p_aout->i_nb_inputs )
184 msg_Err( p_aout, "cannot find an input to delete" );
188 /* Remove the input from the list. */
189 memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
190 (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
191 p_aout->i_nb_inputs--;
193 vlc_mutex_unlock( &p_aout->mixer_lock );
195 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
196 p_input->i_nb_filters );
197 aout_FifoDestroy( p_aout, &p_input->fifo );
201 if ( !p_aout->i_nb_inputs )
203 aout_OutputDelete( p_aout );
204 aout_MixerDelete( p_aout );
207 msg_Dbg( p_aout, "input 0x%x destroyed", p_input );
210 /*****************************************************************************
211 * aout_InputPlay : play a buffer
212 *****************************************************************************/
213 void aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
214 aout_buffer_t * p_buffer )
216 mtime_t start_date, duration;
218 vlc_mutex_lock( &p_aout->input_lock );
219 while( p_aout->b_change_requested )
221 vlc_cond_wait( &p_aout->input_signal, &p_aout->input_lock );
223 p_aout->i_inputs_active++;
224 vlc_mutex_unlock( &p_aout->input_lock );
226 /* We don't care if someone changes the start date behind our back after
227 * this. We'll deal with that when pushing the buffer, and compensate
228 * with the next incoming buffer. */
229 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
231 if ( start_date != 0 && start_date < mdate() )
233 /* The decoder is _very_ late. This can only happen if the user
234 * pauses the stream (or if the decoder is buggy, which cannot
236 msg_Warn( p_aout, "computed PTS is out of range (%lld), clearing out",
238 vlc_mutex_lock( &p_aout->mixer_lock );
239 aout_FifoSet( p_aout, &p_input->fifo, 0 );
240 vlc_mutex_unlock( &p_aout->mixer_lock );
244 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
246 /* The decoder gives us f*cked up PTS. It's its business, but we
247 * can't present it anyway, so drop the buffer. */
248 msg_Warn( p_aout, "PTS is out of range (%lld), dropping buffer",
249 mdate() - p_buffer->start_date );
250 aout_BufferFree( p_buffer );
252 vlc_mutex_lock( &p_aout->input_lock );
253 p_aout->i_inputs_active--;
254 vlc_cond_broadcast( &p_aout->input_signal );
255 vlc_mutex_unlock( &p_aout->input_lock );
259 if ( start_date == 0 ) start_date = p_buffer->start_date;
261 if ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
262 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE )
264 /* Can happen in several circumstances :
265 * 1. A problem at the input (clock drift)
266 * 2. A small pause triggered by the user
267 * 3. Some delay in the output stage, causing a loss of lip
269 * Solution : resample the buffer to avoid a scratch.
271 audio_sample_format_t new_input;
272 int i_ratio, i_nb_filters;
273 mtime_t old_duration;
274 aout_filter_t * pp_filters[AOUT_MAX_FILTERS];
275 aout_buffer_t * p_new_buffer;
276 aout_alloc_t dummy_alloc;
277 mtime_t drift = p_buffer->start_date - start_date;
279 msg_Warn( p_aout, "buffer is %lld %s, resampling",
280 drift > 0 ? drift : -drift,
281 drift > 0 ? "in advance" : "late" );
282 memcpy( &new_input, &p_input->input,
283 sizeof(audio_sample_format_t) );
284 old_duration = p_buffer->end_date - p_buffer->start_date;
285 duration = p_buffer->end_date - start_date;
286 i_ratio = duration * 100 / old_duration;
287 /* If the ratio is too != 100, the sound quality will be awful. */
288 if ( i_ratio < 66 /* % */ )
290 duration = old_duration * 66 / 100;
292 if ( i_ratio > 150 /* % */ )
294 duration = old_duration * 150 / 100;
296 new_input.i_rate = new_input.i_rate * old_duration / duration;
297 aout_FormatPrepare( &new_input );
299 if ( aout_FiltersCreatePipeline( p_aout, pp_filters,
300 &i_nb_filters, &new_input,
301 &p_aout->mixer.mixer ) < 0 )
303 msg_Err( p_aout, "couldn't set an input pipeline for resampling" );
304 vlc_mutex_lock( &p_aout->mixer_lock );
305 aout_FifoSet( p_aout, &p_input->fifo, 0 );
306 vlc_mutex_unlock( &p_aout->mixer_lock );
307 aout_BufferFree( p_buffer );
309 vlc_mutex_lock( &p_aout->input_lock );
310 p_aout->i_inputs_active--;
311 vlc_cond_broadcast( &p_aout->input_signal );
312 vlc_mutex_unlock( &p_aout->input_lock );
316 dummy_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
317 dummy_alloc.i_bytes_per_sec = -1;
318 aout_FiltersHintBuffers( p_aout, pp_filters, i_nb_filters,
320 dummy_alloc.i_bytes_per_sec = __MAX(
321 dummy_alloc.i_bytes_per_sec,
322 new_input.i_bytes_per_frame
324 / new_input.i_frame_length );
325 dummy_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
327 aout_BufferAlloc( &dummy_alloc, duration, NULL, p_new_buffer );
328 memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
329 p_buffer->i_nb_bytes );
330 p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
331 p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
333 aout_BufferFree( p_buffer );
334 p_buffer = p_new_buffer;
336 aout_FiltersPlay( p_aout, pp_filters, i_nb_filters,
339 aout_FiltersDestroyPipeline( p_aout, pp_filters,
344 /* No resampling needed (except maybe the one imposed by the
346 duration = p_buffer->end_date - p_buffer->start_date;
347 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
351 vlc_mutex_lock( &p_aout->mixer_lock );
352 /* Adding the start date will be managed by aout_FifoPush(). */
353 p_buffer->start_date = start_date;
354 p_buffer->end_date = start_date + duration;
355 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
356 vlc_mutex_unlock( &p_aout->mixer_lock );
358 vlc_mutex_lock( &p_aout->input_lock );
359 p_aout->i_inputs_active--;
360 vlc_cond_broadcast( &p_aout->input_signal );
361 vlc_mutex_unlock( &p_aout->input_lock );