1 /*****************************************************************************
2 * mixer.c : audio output mixing operations
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: mixer.c,v 1.14 2002/09/20 23:27:04 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" );
55 /*****************************************************************************
56 * aout_MixerDelete: delete the mixer
57 *****************************************************************************
58 * Please note that you must hold the mixer lock.
59 *****************************************************************************/
60 void aout_MixerDelete( aout_instance_t * p_aout )
62 module_Unneed( p_aout, p_aout->mixer.p_module );
65 /*****************************************************************************
66 * MixBuffer: try to prepare one output buffer
67 *****************************************************************************/
68 static int MixBuffer( aout_instance_t * p_aout )
71 aout_buffer_t * p_output_buffer;
72 mtime_t start_date, end_date;
73 audio_date_t exact_start_date;
75 vlc_mutex_lock( &p_aout->mixer_lock );
76 vlc_mutex_lock( &p_aout->output_fifo_lock );
77 vlc_mutex_lock( &p_aout->input_fifos_lock );
79 /* Retrieve the date of the next buffer. */
80 memcpy( &exact_start_date, &p_aout->output.fifo.end_date,
81 sizeof(audio_date_t) );
82 start_date = aout_DateGet( &exact_start_date );
84 if ( start_date != 0 && start_date < mdate() )
86 /* The output is _very_ late. This can only happen if the user
87 * pauses the stream (or if the decoder is buggy, which cannot
89 msg_Warn( p_aout, "output PTS is out of range (%lld), clearing out",
90 mdate() - start_date );
91 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
92 aout_DateSet( &exact_start_date, 0 );
96 vlc_mutex_unlock( &p_aout->output_fifo_lock );
98 /* See if we have enough data to prepare a new buffer for the audio
99 * output. First : start date. */
102 /* Find the latest start date available. */
103 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
105 aout_input_t * p_input = p_aout->pp_inputs[i];
106 aout_fifo_t * p_fifo = &p_input->fifo;
107 aout_buffer_t * p_buffer;
109 if ( p_input->b_error ) continue;
111 p_buffer = p_fifo->p_first;
112 if ( p_buffer == NULL )
117 if ( !start_date || start_date < p_buffer->start_date )
119 aout_DateSet( &exact_start_date, p_buffer->start_date );
120 start_date = p_buffer->start_date;
124 if ( i < p_aout->i_nb_inputs )
126 /* Interrupted before the end... We can't run. */
127 vlc_mutex_unlock( &p_aout->input_fifos_lock );
128 vlc_mutex_unlock( &p_aout->mixer_lock );
132 aout_DateIncrement( &exact_start_date, p_aout->output.i_nb_samples );
133 end_date = aout_DateGet( &exact_start_date );
135 /* Check that start_date and end_date are available for all input
137 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
139 aout_input_t * p_input = p_aout->pp_inputs[i];
140 aout_fifo_t * p_fifo = &p_input->fifo;
141 aout_buffer_t * p_buffer;
143 vlc_bool_t b_drop_buffers;
145 if ( p_input->b_error ) continue;
147 p_buffer = p_fifo->p_first;
148 if ( p_buffer == NULL )
153 /* Check for the continuity of start_date */
154 while ( p_buffer != NULL && p_buffer->end_date < start_date )
156 aout_buffer_t * p_next = p_buffer->p_next;
157 msg_Err( p_aout, "the mixer got a packet in the past (%lld)",
158 start_date - p_buffer->end_date );
159 aout_BufferFree( p_buffer );
160 p_fifo->p_first = p_buffer = p_next;
161 p_input->p_first_byte_to_mix = NULL;
163 if ( p_buffer == NULL )
165 p_fifo->pp_last = &p_fifo->p_first;
169 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
171 /* Additionally check that p_first_byte_to_mix is well
173 unsigned long i_nb_bytes = (start_date - p_buffer->start_date)
174 * p_aout->mixer.mixer.i_bytes_per_frame
175 * p_aout->mixer.mixer.i_rate
176 / p_aout->mixer.mixer.i_frame_length
178 ptrdiff_t mixer_nb_bytes;
180 if ( p_input->p_first_byte_to_mix == NULL )
182 p_input->p_first_byte_to_mix = p_buffer->p_buffer;
184 mixer_nb_bytes = p_input->p_first_byte_to_mix
185 - p_buffer->p_buffer;
187 if ( !((i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame
189 (i_nb_bytes < p_aout->mixer.mixer.i_bytes_per_frame
193 "mixer start isn't output start (%d)",
194 i_nb_bytes - mixer_nb_bytes );
196 /* Round to the nearest multiple */
197 i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame;
198 i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame;
199 p_input->p_first_byte_to_mix = p_buffer->p_buffer
204 /* Check that we have enough samples. */
207 p_buffer = p_fifo->p_first;
208 if ( p_buffer == NULL ) break;
209 if ( p_buffer->end_date >= end_date ) break;
211 /* Check that all buffers are contiguous. */
212 prev_date = p_fifo->p_first->end_date;
213 p_buffer = p_buffer->p_next;
215 for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
217 if ( prev_date != p_buffer->start_date )
220 "buffer hole, dropping packets (%lld)",
221 p_buffer->start_date - prev_date );
225 if ( p_buffer->end_date >= end_date ) break;
226 prev_date = p_buffer->end_date;
228 if ( b_drop_buffers )
230 aout_buffer_t * p_deleted = p_fifo->p_first;
231 while ( p_deleted != NULL && p_deleted != p_buffer )
233 aout_buffer_t * p_next = p_deleted->p_next;
234 aout_BufferFree( p_deleted );
237 p_fifo->p_first = p_deleted; /* == p_buffer */
241 if ( p_buffer == NULL ) break;
244 if ( i < p_aout->i_nb_inputs )
246 /* Interrupted before the end... We can't run. */
247 vlc_mutex_unlock( &p_aout->input_fifos_lock );
248 vlc_mutex_unlock( &p_aout->mixer_lock );
253 aout_BufferAlloc( &p_aout->mixer.output_alloc,
254 ((u64)p_aout->output.i_nb_samples * 1000000)
255 / p_aout->output.output.i_rate,
256 /* This is a bit kludgy, but is actually only used
257 * for the S/PDIF dummy mixer : */
258 p_aout->pp_inputs[0]->fifo.p_first,
260 if ( p_output_buffer == NULL )
262 msg_Err( p_aout, "out of memory" );
263 vlc_mutex_unlock( &p_aout->input_fifos_lock );
264 vlc_mutex_unlock( &p_aout->mixer_lock );
267 /* This is again a bit kludgy - for the S/PDIF mixer. */
268 if ( p_aout->mixer.output_alloc.i_alloc_type != AOUT_ALLOC_NONE )
270 p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
271 p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
272 * p_aout->mixer.mixer.i_bytes_per_frame
273 / p_aout->mixer.mixer.i_frame_length;
275 p_output_buffer->start_date = start_date;
276 p_output_buffer->end_date = end_date;
278 p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
280 vlc_mutex_unlock( &p_aout->input_fifos_lock );
282 aout_OutputPlay( p_aout, p_output_buffer );
284 vlc_mutex_unlock( &p_aout->mixer_lock );
289 /*****************************************************************************
290 * aout_MixerRun: entry point for the mixer & post-filters processing
291 *****************************************************************************/
292 void aout_MixerRun( aout_instance_t * p_aout )
294 while( MixBuffer( p_aout ) != -1 );
297 /*****************************************************************************
298 * aout_MixerMultiplierSet: set p_aout->mixer.f_multiplier
299 *****************************************************************************
300 * Please note that we assume that you own the mixer lock when entering this
301 * function. This function returns -1 on error.
302 *****************************************************************************/
303 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier )
305 float f_old = p_aout->mixer.f_multiplier;
307 if ( p_aout->mixer.mixer.i_format != AOUT_FMT_FLOAT32 )
309 msg_Warn( p_aout, "MixerMultiplierSet called for non-float32 mixer" );
313 aout_MixerDelete( p_aout );
315 p_aout->mixer.f_multiplier = f_multiplier;
317 if ( aout_MixerNew( p_aout ) )
319 p_aout->mixer.f_multiplier = f_old;
320 aout_MixerNew( p_aout );
327 /*****************************************************************************
328 * aout_MixerMultiplierGet: get p_aout->mixer.f_multiplier
329 *****************************************************************************
330 * Please note that we assume that you own the mixer lock when entering this
331 * function. This function returns -1 on error.
332 *****************************************************************************/
333 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier )
335 if ( p_aout->mixer.mixer.i_format != AOUT_FMT_FLOAT32 )
337 msg_Warn( p_aout, "MixerMultiplierGet called for non-float32 mixer" );
341 *pf_multiplier = p_aout->mixer.f_multiplier;