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 *****************************************************************************/
33 #include <vlc_common.h>
37 #include "aout_internal.h"
38 /*****************************************************************************
39 * aout_MixerNew: prepare a mixer plug-in
40 *****************************************************************************
41 * Please note that you must hold the mixer lock.
42 *****************************************************************************/
43 int aout_MixerNew( aout_instance_t * p_aout )
45 assert( !p_aout->p_mixer );
46 vlc_assert_locked( &p_aout->input_fifos_lock );
48 aout_mixer_t *p_mixer = vlc_object_create( p_aout, sizeof(*p_mixer) );
52 p_mixer->fmt = p_aout->mixer_format;
53 p_mixer->allocation = p_aout->mixer_allocation;
54 p_mixer->multiplier = p_aout->mixer_multiplier;
55 p_mixer->input_count = p_aout->i_nb_inputs;
56 p_mixer->input = calloc( p_mixer->input_count, sizeof(*p_mixer->input) );
57 for( int i = 0; i < p_aout->i_nb_inputs; i++ )
58 p_mixer->input[i] = &p_aout->pp_inputs[i]->mixer;
62 vlc_object_attach( p_mixer, p_aout );
64 p_mixer->module = module_need( p_mixer, "audio mixer", NULL, false );
65 if( !p_mixer->module )
67 msg_Err( p_aout, "no suitable audio mixer" );
68 free( p_mixer->input );
69 vlc_object_release( p_mixer );
74 p_aout->p_mixer = p_mixer;
78 /*****************************************************************************
79 * aout_MixerDelete: delete the mixer
80 *****************************************************************************
81 * Please note that you must hold the mixer lock.
82 *****************************************************************************/
83 void aout_MixerDelete( aout_instance_t * p_aout )
85 if( !p_aout->p_mixer )
88 module_unneed( p_aout->p_mixer, p_aout->p_mixer->module );
90 free( p_aout->p_mixer->input );
91 vlc_object_release( p_aout->p_mixer );
94 p_aout->p_mixer = NULL;
97 /*****************************************************************************
98 * MixBuffer: try to prepare one output buffer
99 *****************************************************************************
100 * Please note that you must hold the mixer lock.
101 *****************************************************************************/
102 static int MixBuffer( aout_instance_t * p_aout )
104 int i, i_first_input = 0;
105 aout_buffer_t * p_output_buffer;
106 mtime_t start_date, end_date;
107 date_t exact_start_date;
109 if( !p_aout->p_mixer )
111 /* Free all incoming buffers. */
112 aout_lock_input_fifos( p_aout );
113 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
115 aout_input_t * p_input = p_aout->pp_inputs[i];
116 aout_buffer_t * p_buffer = p_input->mixer.fifo.p_first;
117 if ( p_input->b_error ) continue;
118 while ( p_buffer != NULL )
120 aout_buffer_t * p_next = p_buffer->p_next;
121 aout_BufferFree( p_buffer );
125 aout_unlock_input_fifos( p_aout );
130 aout_lock_input_fifos( p_aout );
131 aout_lock_output_fifo( p_aout );
133 /* Retrieve the date of the next buffer. */
134 exact_start_date = p_aout->output.fifo.end_date;
135 start_date = date_Get( &exact_start_date );
137 if ( start_date != 0 && start_date < mdate() )
139 /* The output is _very_ late. This can only happen if the user
140 * pauses the stream (or if the decoder is buggy, which cannot
142 msg_Warn( p_aout, "output PTS is out of range (%"PRId64"), clearing out",
143 mdate() - start_date );
144 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
145 date_Set( &exact_start_date, 0 );
149 aout_unlock_output_fifo( p_aout );
151 /* See if we have enough data to prepare a new buffer for the audio
152 * output. First : start date. */
155 /* Find the latest start date available. */
156 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
158 aout_input_t * p_input = p_aout->pp_inputs[i];
159 aout_fifo_t * p_fifo = &p_input->mixer.fifo;
160 aout_buffer_t * p_buffer;
162 if ( p_input->b_error || p_input->b_paused )
165 p_buffer = p_fifo->p_first;
166 while ( p_buffer != NULL && p_buffer->i_pts < mdate() )
168 msg_Warn( p_aout, "input PTS is out of range (%"PRId64"), "
169 "trashing", mdate() - p_buffer->i_pts );
170 p_buffer = aout_FifoPop( p_aout, p_fifo );
171 aout_BufferFree( p_buffer );
172 p_buffer = p_fifo->p_first;
173 p_input->mixer.begin = NULL;
176 if ( p_buffer == NULL )
181 if ( !start_date || start_date < p_buffer->i_pts )
183 date_Set( &exact_start_date, p_buffer->i_pts );
184 start_date = p_buffer->i_pts;
188 if ( i < p_aout->i_nb_inputs )
190 /* Interrupted before the end... We can't run. */
191 aout_unlock_input_fifos( p_aout );
195 date_Increment( &exact_start_date, p_aout->output.i_nb_samples );
196 end_date = date_Get( &exact_start_date );
198 /* Check that start_date and end_date are available for all input
200 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
202 aout_input_t * p_input = p_aout->pp_inputs[i];
203 aout_fifo_t * p_fifo = &p_input->mixer.fifo;
204 aout_buffer_t * p_buffer;
208 p_input->mixer.is_invalid = p_input->b_error || p_input->b_paused;
209 if ( p_input->mixer.is_invalid )
211 if ( i_first_input == i ) i_first_input++;
215 p_buffer = p_fifo->p_first;
216 if ( p_buffer == NULL )
221 /* Check for the continuity of start_date */
222 while ( p_buffer != NULL
223 && p_buffer->i_pts + p_buffer->i_length < start_date - 1 )
225 /* We authorize a +-1 because rounding errors get compensated
227 aout_buffer_t * p_next = p_buffer->p_next;
228 msg_Warn( p_aout, "the mixer got a packet in the past (%"PRId64")",
229 start_date - (p_buffer->i_pts + p_buffer->i_length) );
230 aout_BufferFree( p_buffer );
231 p_fifo->p_first = p_buffer = p_next;
232 p_input->mixer.begin = NULL;
234 if ( p_buffer == NULL )
236 p_fifo->pp_last = &p_fifo->p_first;
240 /* Check that we have enough samples. */
243 p_buffer = p_fifo->p_first;
244 if ( p_buffer == NULL ) break;
245 if ( p_buffer->i_pts + p_buffer->i_length >= end_date ) break;
247 /* Check that all buffers are contiguous. */
248 prev_date = p_fifo->p_first->i_pts + p_fifo->p_first->i_length;
249 p_buffer = p_buffer->p_next;
251 for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
253 if ( prev_date != p_buffer->i_pts )
256 "buffer hole, dropping packets (%"PRId64")",
257 p_buffer->i_pts - prev_date );
261 if ( p_buffer->i_pts + p_buffer->i_length >= end_date ) break;
262 prev_date = p_buffer->i_pts + p_buffer->i_length;
264 if ( b_drop_buffers )
266 aout_buffer_t * p_deleted = p_fifo->p_first;
267 while ( p_deleted != NULL && p_deleted != p_buffer )
269 aout_buffer_t * p_next = p_deleted->p_next;
270 aout_BufferFree( p_deleted );
273 p_fifo->p_first = p_deleted; /* == p_buffer */
277 if ( p_buffer == NULL ) break;
279 p_buffer = p_fifo->p_first;
280 if ( !AOUT_FMT_NON_LINEAR( &p_aout->p_mixer->fmt ) )
282 /* Additionally check that p_first_byte_to_mix is well
284 mtime_t i_buffer = (start_date - p_buffer->i_pts)
285 * p_aout->p_mixer->fmt.i_bytes_per_frame
286 * p_aout->p_mixer->fmt.i_rate
287 / p_aout->p_mixer->fmt.i_frame_length
289 ptrdiff_t mixer_nb_bytes;
291 if ( p_input->mixer.begin == NULL )
293 p_input->mixer.begin = p_buffer->p_buffer;
295 mixer_nb_bytes = p_input->mixer.begin - p_buffer->p_buffer;
297 if ( !((i_buffer + p_aout->p_mixer->fmt.i_bytes_per_frame
299 (i_buffer < p_aout->p_mixer->fmt.i_bytes_per_frame
302 msg_Warn( p_aout, "mixer start isn't output start (%"PRId64")",
303 i_buffer - mixer_nb_bytes );
305 /* Round to the nearest multiple */
306 i_buffer /= p_aout->p_mixer->fmt.i_bytes_per_frame;
307 i_buffer *= p_aout->p_mixer->fmt.i_bytes_per_frame;
310 /* Is it really the best way to do it ? */
311 aout_lock_output_fifo( p_aout );
312 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
313 date_Set( &exact_start_date, 0 );
314 aout_unlock_output_fifo( p_aout );
318 p_input->mixer.begin = p_buffer->p_buffer + i_buffer;
323 if ( i < p_aout->i_nb_inputs || i_first_input == p_aout->i_nb_inputs )
325 /* Interrupted before the end... We can't run. */
326 aout_unlock_input_fifos( p_aout );
331 p_output_buffer = aout_BufferAlloc( &p_aout->p_mixer->allocation,
332 ((uint64_t)p_aout->output.i_nb_samples * 1000000)
333 / p_aout->output.output.i_rate,
334 /* This is a bit kludgy, but is actually only used
335 * for the S/PDIF dummy mixer : */
336 p_aout->pp_inputs[i_first_input]->mixer.fifo.p_first);
337 if ( p_output_buffer == NULL )
339 aout_unlock_input_fifos( p_aout );
342 /* This is again a bit kludgy - for the S/PDIF mixer. */
343 if ( p_aout->p_mixer->allocation.b_alloc )
345 p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
346 p_output_buffer->i_buffer = p_aout->output.i_nb_samples
347 * p_aout->p_mixer->fmt.i_bytes_per_frame
348 / p_aout->p_mixer->fmt.i_frame_length;
350 p_output_buffer->i_pts = start_date;
351 p_output_buffer->i_length = end_date - start_date;
353 p_aout->p_mixer->mix( p_aout->p_mixer, p_output_buffer );
355 aout_unlock_input_fifos( p_aout );
357 aout_OutputPlay( p_aout, p_output_buffer );
362 /*****************************************************************************
363 * aout_MixerRun: entry point for the mixer & post-filters processing
364 *****************************************************************************
365 * Please note that you must hold the mixer lock.
366 *****************************************************************************/
367 void aout_MixerRun( aout_instance_t * p_aout )
369 while( MixBuffer( p_aout ) != -1 );
372 /*****************************************************************************
373 * aout_MixerMultiplierSet: set 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_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier )
380 float f_old = p_aout->mixer_multiplier;
381 bool b_new_mixer = false;
383 if ( p_aout->p_mixer )
385 aout_MixerDelete( p_aout );
389 p_aout->mixer_multiplier = f_multiplier;
391 if ( b_new_mixer && aout_MixerNew( p_aout ) )
393 p_aout->mixer_multiplier = f_old;
394 aout_MixerNew( p_aout );
401 /*****************************************************************************
402 * aout_MixerMultiplierGet: get p_aout->mixer.f_multiplier
403 *****************************************************************************
404 * Please note that we assume that you own the mixer lock when entering this
405 * function. This function returns -1 on error.
406 *****************************************************************************/
407 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier )
409 *pf_multiplier = p_aout->mixer_multiplier;