1 /*****************************************************************************
2 * mixer.c : audio output mixing operations
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: mixer.c,v 1.16 2002/09/26 22:40:25 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_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 );
47 if ( p_aout->mixer.p_module == NULL )
49 msg_Err( p_aout, "no suitable aout 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 int aout_MixerDelete( aout_instance_t * p_aout )
63 if ( p_aout->mixer.b_error ) return 0;
64 module_Unneed( p_aout, p_aout->mixer.p_module );
65 p_aout->mixer.b_error = 1;
70 /*****************************************************************************
71 * MixBuffer: try to prepare one output buffer
72 *****************************************************************************
73 * Please note that you must hold the mixer lock.
74 *****************************************************************************/
75 static int MixBuffer( aout_instance_t * p_aout )
77 int i, i_nb_real_inputs = 0;
78 aout_buffer_t * p_output_buffer;
79 mtime_t start_date, end_date;
80 audio_date_t exact_start_date;
82 if ( p_aout->mixer.b_error )
84 /* Free all incoming buffers. */
85 vlc_mutex_lock( &p_aout->input_fifos_lock );
86 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
88 aout_input_t * p_input = p_aout->pp_inputs[i];
89 aout_buffer_t * p_buffer = p_input->fifo.p_first;
90 if ( p_input->b_error ) continue;
91 while ( p_buffer != NULL )
93 aout_buffer_t * p_next = p_buffer->p_next;
94 aout_BufferFree( p_buffer );
98 vlc_mutex_unlock( &p_aout->input_fifos_lock );
103 vlc_mutex_lock( &p_aout->output_fifo_lock );
104 vlc_mutex_lock( &p_aout->input_fifos_lock );
106 /* Retrieve the date of the next buffer. */
107 memcpy( &exact_start_date, &p_aout->output.fifo.end_date,
108 sizeof(audio_date_t) );
109 start_date = aout_DateGet( &exact_start_date );
111 if ( start_date != 0 && start_date < mdate() )
113 /* The output is _very_ late. This can only happen if the user
114 * pauses the stream (or if the decoder is buggy, which cannot
116 msg_Warn( p_aout, "output PTS is out of range (%lld), clearing out",
117 mdate() - start_date );
118 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
119 aout_DateSet( &exact_start_date, 0 );
123 vlc_mutex_unlock( &p_aout->output_fifo_lock );
125 /* See if we have enough data to prepare a new buffer for the audio
126 * output. First : start date. */
129 /* Find the latest start date available. */
130 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
132 aout_input_t * p_input = p_aout->pp_inputs[i];
133 aout_fifo_t * p_fifo = &p_input->fifo;
134 aout_buffer_t * p_buffer;
136 if ( p_input->b_error ) continue;
138 p_buffer = p_fifo->p_first;
139 if ( p_buffer == NULL )
144 if ( !start_date || start_date < p_buffer->start_date )
146 aout_DateSet( &exact_start_date, p_buffer->start_date );
147 start_date = p_buffer->start_date;
151 if ( i < p_aout->i_nb_inputs )
153 /* Interrupted before the end... We can't run. */
154 vlc_mutex_unlock( &p_aout->input_fifos_lock );
158 aout_DateIncrement( &exact_start_date, p_aout->output.i_nb_samples );
159 end_date = aout_DateGet( &exact_start_date );
161 /* Check that start_date and end_date are available for all input
163 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
165 aout_input_t * p_input = p_aout->pp_inputs[i];
166 aout_fifo_t * p_fifo = &p_input->fifo;
167 aout_buffer_t * p_buffer;
169 vlc_bool_t b_drop_buffers;
171 if ( p_input->b_error ) continue;
174 p_buffer = p_fifo->p_first;
175 if ( p_buffer == NULL )
180 /* Check for the continuity of start_date */
181 while ( p_buffer != NULL && p_buffer->end_date < start_date )
183 aout_buffer_t * p_next = p_buffer->p_next;
184 msg_Err( p_aout, "the mixer got a packet in the past (%lld)",
185 start_date - p_buffer->end_date );
186 aout_BufferFree( p_buffer );
187 p_fifo->p_first = p_buffer = p_next;
188 p_input->p_first_byte_to_mix = NULL;
190 if ( p_buffer == NULL )
192 p_fifo->pp_last = &p_fifo->p_first;
196 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
198 /* Additionally check that p_first_byte_to_mix is well
200 unsigned long i_nb_bytes = (start_date - p_buffer->start_date)
201 * p_aout->mixer.mixer.i_bytes_per_frame
202 * p_aout->mixer.mixer.i_rate
203 / p_aout->mixer.mixer.i_frame_length
205 ptrdiff_t mixer_nb_bytes;
207 if ( p_input->p_first_byte_to_mix == NULL )
209 p_input->p_first_byte_to_mix = p_buffer->p_buffer;
211 mixer_nb_bytes = p_input->p_first_byte_to_mix
212 - p_buffer->p_buffer;
214 if ( !((i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame
216 (i_nb_bytes < p_aout->mixer.mixer.i_bytes_per_frame
220 "mixer start isn't output start (%d)",
221 i_nb_bytes - mixer_nb_bytes );
223 /* Round to the nearest multiple */
224 i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame;
225 i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame;
226 p_input->p_first_byte_to_mix = p_buffer->p_buffer
231 /* Check that we have enough samples. */
234 p_buffer = p_fifo->p_first;
235 if ( p_buffer == NULL ) break;
236 if ( p_buffer->end_date >= end_date ) break;
238 /* Check that all buffers are contiguous. */
239 prev_date = p_fifo->p_first->end_date;
240 p_buffer = p_buffer->p_next;
242 for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
244 if ( prev_date != p_buffer->start_date )
247 "buffer hole, dropping packets (%lld)",
248 p_buffer->start_date - prev_date );
252 if ( p_buffer->end_date >= end_date ) break;
253 prev_date = p_buffer->end_date;
255 if ( b_drop_buffers )
257 aout_buffer_t * p_deleted = p_fifo->p_first;
258 while ( p_deleted != NULL && p_deleted != p_buffer )
260 aout_buffer_t * p_next = p_deleted->p_next;
261 aout_BufferFree( p_deleted );
264 p_fifo->p_first = p_deleted; /* == p_buffer */
268 if ( p_buffer == NULL ) break;
271 if ( i < p_aout->i_nb_inputs || !i_nb_real_inputs )
273 /* Interrupted before the end... We can't run. */
274 vlc_mutex_unlock( &p_aout->input_fifos_lock );
279 aout_BufferAlloc( &p_aout->mixer.output_alloc,
280 ((u64)p_aout->output.i_nb_samples * 1000000)
281 / p_aout->output.output.i_rate,
282 /* This is a bit kludgy, but is actually only used
283 * for the S/PDIF dummy mixer : */
284 p_aout->pp_inputs[0]->fifo.p_first,
286 if ( p_output_buffer == NULL )
288 msg_Err( p_aout, "out of memory" );
289 vlc_mutex_unlock( &p_aout->input_fifos_lock );
292 /* This is again a bit kludgy - for the S/PDIF mixer. */
293 if ( p_aout->mixer.output_alloc.i_alloc_type != AOUT_ALLOC_NONE )
295 p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
296 p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
297 * p_aout->mixer.mixer.i_bytes_per_frame
298 / p_aout->mixer.mixer.i_frame_length;
300 p_output_buffer->start_date = start_date;
301 p_output_buffer->end_date = end_date;
303 p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
305 vlc_mutex_unlock( &p_aout->input_fifos_lock );
307 aout_OutputPlay( p_aout, p_output_buffer );
312 /*****************************************************************************
313 * aout_MixerRun: entry point for the mixer & post-filters processing
314 *****************************************************************************
315 * Please note that you must hold the mixer lock.
316 *****************************************************************************/
317 void aout_MixerRun( aout_instance_t * p_aout )
319 while( MixBuffer( p_aout ) != -1 );
322 /*****************************************************************************
323 * aout_MixerMultiplierSet: set p_aout->mixer.f_multiplier
324 *****************************************************************************
325 * Please note that we assume that you own the mixer lock when entering this
326 * function. This function returns -1 on error.
327 *****************************************************************************/
328 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier )
330 float f_old = p_aout->mixer.f_multiplier;
331 vlc_bool_t b_new_mixer = 0;
333 if ( !p_aout->mixer.b_error )
335 aout_MixerDelete( p_aout );
339 p_aout->mixer.f_multiplier = f_multiplier;
341 if ( b_new_mixer && aout_MixerNew( p_aout ) )
343 p_aout->mixer.f_multiplier = f_old;
344 aout_MixerNew( p_aout );
351 /*****************************************************************************
352 * aout_MixerMultiplierGet: get p_aout->mixer.f_multiplier
353 *****************************************************************************
354 * Please note that we assume that you own the mixer lock when entering this
355 * function. This function returns -1 on error.
356 *****************************************************************************/
357 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier )
359 *pf_multiplier = p_aout->mixer.f_multiplier;