1 /*****************************************************************************
2 * mixer.c : audio output mixing operations
3 *****************************************************************************
4 * Copyright (C) 2002-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
38 #include "aout_internal.h"
39 /*****************************************************************************
40 * aout_MixerNew: prepare a mixer plug-in
41 *****************************************************************************
42 * Please note that you must hold the mixer lock.
43 *****************************************************************************/
44 int aout_MixerNew( aout_instance_t * p_aout )
46 p_aout->mixer.p_module = module_need( p_aout, "audio mixer", NULL, 0 );
47 if ( p_aout->mixer.p_module == NULL )
49 msg_Err( p_aout, "no suitable audio mixer" );
52 p_aout->mixer.b_error = 0;
56 /*****************************************************************************
57 * aout_MixerDelete: delete the mixer
58 *****************************************************************************
59 * Please note that you must hold the mixer lock.
60 *****************************************************************************/
61 void aout_MixerDelete( aout_instance_t * p_aout )
63 if ( p_aout->mixer.b_error ) return;
64 module_unneed( p_aout, p_aout->mixer.p_module );
65 p_aout->mixer.b_error = 1;
68 /*****************************************************************************
69 * MixBuffer: try to prepare one output buffer
70 *****************************************************************************
71 * Please note that you must hold the mixer lock.
72 *****************************************************************************/
73 static int MixBuffer( aout_instance_t * p_aout )
75 int i, i_first_input = 0;
76 aout_buffer_t * p_output_buffer;
77 mtime_t start_date, end_date;
78 audio_date_t exact_start_date;
80 if ( p_aout->mixer.b_error )
82 /* Free all incoming buffers. */
83 aout_lock_input_fifos( p_aout );
84 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
86 aout_input_t * p_input = p_aout->pp_inputs[i];
87 aout_buffer_t * p_buffer = p_input->fifo.p_first;
88 if ( p_input->b_error ) continue;
89 while ( p_buffer != NULL )
91 aout_buffer_t * p_next = p_buffer->p_next;
92 aout_BufferFree( p_buffer );
96 aout_unlock_input_fifos( p_aout );
101 aout_lock_output_fifo( p_aout );
102 aout_lock_input_fifos( p_aout );
104 /* Retrieve the date of the next buffer. */
105 memcpy( &exact_start_date, &p_aout->output.fifo.end_date,
106 sizeof(audio_date_t) );
107 start_date = aout_DateGet( &exact_start_date );
109 if ( start_date != 0 && start_date < mdate() )
111 /* The output is _very_ late. This can only happen if the user
112 * pauses the stream (or if the decoder is buggy, which cannot
114 msg_Warn( p_aout, "output PTS is out of range (%"PRId64"), clearing out",
115 mdate() - start_date );
116 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
117 aout_DateSet( &exact_start_date, 0 );
121 aout_unlock_output_fifo( p_aout );
123 /* See if we have enough data to prepare a new buffer for the audio
124 * output. First : start date. */
127 /* Find the latest start date available. */
128 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
130 aout_input_t * p_input = p_aout->pp_inputs[i];
131 aout_fifo_t * p_fifo = &p_input->fifo;
132 aout_buffer_t * p_buffer;
134 if ( p_input->b_error || p_input->b_paused )
137 p_buffer = p_fifo->p_first;
138 while ( p_buffer != NULL && p_buffer->start_date < mdate() )
140 msg_Warn( p_aout, "input PTS is out of range (%"PRId64"), "
141 "trashing", mdate() - p_buffer->start_date );
142 p_buffer = aout_FifoPop( p_aout, p_fifo );
143 aout_BufferFree( p_buffer );
144 p_buffer = p_fifo->p_first;
145 p_input->p_first_byte_to_mix = NULL;
148 if ( p_buffer == NULL )
153 if ( !start_date || start_date < p_buffer->start_date )
155 aout_DateSet( &exact_start_date, p_buffer->start_date );
156 start_date = p_buffer->start_date;
160 if ( i < p_aout->i_nb_inputs )
162 /* Interrupted before the end... We can't run. */
163 aout_unlock_input_fifos( p_aout );
167 aout_DateIncrement( &exact_start_date, p_aout->output.i_nb_samples );
168 end_date = aout_DateGet( &exact_start_date );
170 /* Check that start_date and end_date are available for all input
172 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
174 aout_input_t * p_input = p_aout->pp_inputs[i];
175 aout_fifo_t * p_fifo = &p_input->fifo;
176 aout_buffer_t * p_buffer;
180 if ( p_input->b_error || p_input->b_paused )
182 if ( i_first_input == i ) i_first_input++;
186 p_buffer = p_fifo->p_first;
187 if ( p_buffer == NULL )
192 /* Check for the continuity of start_date */
193 while ( p_buffer != NULL && p_buffer->end_date < start_date - 1 )
195 /* We authorize a +-1 because rounding errors get compensated
197 aout_buffer_t * p_next = p_buffer->p_next;
198 msg_Warn( p_aout, "the mixer got a packet in the past (%"PRId64")",
199 start_date - p_buffer->end_date );
200 aout_BufferFree( p_buffer );
201 p_fifo->p_first = p_buffer = p_next;
202 p_input->p_first_byte_to_mix = NULL;
204 if ( p_buffer == NULL )
206 p_fifo->pp_last = &p_fifo->p_first;
210 /* Check that we have enough samples. */
213 p_buffer = p_fifo->p_first;
214 if ( p_buffer == NULL ) break;
215 if ( p_buffer->end_date >= end_date ) break;
217 /* Check that all buffers are contiguous. */
218 prev_date = p_fifo->p_first->end_date;
219 p_buffer = p_buffer->p_next;
221 for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
223 if ( prev_date != p_buffer->start_date )
226 "buffer hole, dropping packets (%"PRId64")",
227 p_buffer->start_date - prev_date );
231 if ( p_buffer->end_date >= end_date ) break;
232 prev_date = p_buffer->end_date;
234 if ( b_drop_buffers )
236 aout_buffer_t * p_deleted = p_fifo->p_first;
237 while ( p_deleted != NULL && p_deleted != p_buffer )
239 aout_buffer_t * p_next = p_deleted->p_next;
240 aout_BufferFree( p_deleted );
243 p_fifo->p_first = p_deleted; /* == p_buffer */
247 if ( p_buffer == NULL ) break;
249 p_buffer = p_fifo->p_first;
250 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
252 /* Additionally check that p_first_byte_to_mix is well
254 mtime_t i_nb_bytes = (start_date - p_buffer->start_date)
255 * p_aout->mixer.mixer.i_bytes_per_frame
256 * p_aout->mixer.mixer.i_rate
257 / p_aout->mixer.mixer.i_frame_length
259 ptrdiff_t mixer_nb_bytes;
261 if ( p_input->p_first_byte_to_mix == NULL )
263 p_input->p_first_byte_to_mix = p_buffer->p_buffer;
265 mixer_nb_bytes = p_input->p_first_byte_to_mix - p_buffer->p_buffer;
267 if ( !((i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame
269 (i_nb_bytes < p_aout->mixer.mixer.i_bytes_per_frame
272 msg_Warn( p_aout, "mixer start isn't output start (%"PRId64")",
273 i_nb_bytes - mixer_nb_bytes );
275 /* Round to the nearest multiple */
276 i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame;
277 i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame;
280 /* Is it really the best way to do it ? */
281 aout_lock_output_fifo( p_aout );
282 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
283 aout_DateSet( &exact_start_date, 0 );
284 aout_unlock_output_fifo( p_aout );
288 p_input->p_first_byte_to_mix = p_buffer->p_buffer + i_nb_bytes;
293 if ( i < p_aout->i_nb_inputs || i_first_input == p_aout->i_nb_inputs )
295 /* Interrupted before the end... We can't run. */
296 aout_unlock_input_fifos( p_aout );
301 aout_BufferAlloc( &p_aout->mixer.output_alloc,
302 ((uint64_t)p_aout->output.i_nb_samples * 1000000)
303 / p_aout->output.output.i_rate,
304 /* This is a bit kludgy, but is actually only used
305 * for the S/PDIF dummy mixer : */
306 p_aout->pp_inputs[i_first_input]->fifo.p_first,
308 if ( p_output_buffer == NULL )
310 aout_unlock_input_fifos( p_aout );
313 /* This is again a bit kludgy - for the S/PDIF mixer. */
314 if ( p_aout->mixer.output_alloc.i_alloc_type != AOUT_ALLOC_NONE )
316 p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
317 p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
318 * p_aout->mixer.mixer.i_bytes_per_frame
319 / p_aout->mixer.mixer.i_frame_length;
321 p_output_buffer->start_date = start_date;
322 p_output_buffer->end_date = end_date;
324 p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
326 aout_unlock_input_fifos( p_aout );
328 aout_OutputPlay( p_aout, p_output_buffer );
333 /*****************************************************************************
334 * aout_MixerRun: entry point for the mixer & post-filters processing
335 *****************************************************************************
336 * Please note that you must hold the mixer lock.
337 *****************************************************************************/
338 void aout_MixerRun( aout_instance_t * p_aout )
340 while( MixBuffer( p_aout ) != -1 );
343 /*****************************************************************************
344 * aout_MixerMultiplierSet: set p_aout->mixer.f_multiplier
345 *****************************************************************************
346 * Please note that we assume that you own the mixer lock when entering this
347 * function. This function returns -1 on error.
348 *****************************************************************************/
349 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier )
351 float f_old = p_aout->mixer.f_multiplier;
352 bool b_new_mixer = 0;
354 if ( !p_aout->mixer.b_error )
356 aout_MixerDelete( p_aout );
360 p_aout->mixer.f_multiplier = f_multiplier;
362 if ( b_new_mixer && aout_MixerNew( p_aout ) )
364 p_aout->mixer.f_multiplier = f_old;
365 aout_MixerNew( p_aout );
372 /*****************************************************************************
373 * aout_MixerMultiplierGet: get p_aout->mixer.f_multiplier
374 *****************************************************************************
375 * Please note that we assume that you own the mixer lock when entering this
376 * function. This function returns -1 on error.
377 *****************************************************************************/
378 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier )
380 *pf_multiplier = p_aout->mixer.f_multiplier;