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 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
36 #include "aout_internal.h"
37 /*****************************************************************************
38 * aout_MixerNew: prepare a mixer plug-in
39 *****************************************************************************
40 * Please note that you must hold the mixer lock.
41 *****************************************************************************/
42 int aout_MixerNew( aout_instance_t * p_aout )
44 p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL, 0 );
45 if ( p_aout->mixer.p_module == NULL )
47 msg_Err( p_aout, "no suitable audio mixer" );
50 p_aout->mixer.b_error = 0;
54 /*****************************************************************************
55 * aout_MixerDelete: delete the mixer
56 *****************************************************************************
57 * Please note that you must hold the mixer lock.
58 *****************************************************************************/
59 void aout_MixerDelete( aout_instance_t * p_aout )
61 if ( p_aout->mixer.b_error ) return;
62 module_Unneed( p_aout, p_aout->mixer.p_module );
63 p_aout->mixer.b_error = 1;
66 /*****************************************************************************
67 * MixBuffer: try to prepare one output buffer
68 *****************************************************************************
69 * Please note that you must hold the mixer lock.
70 *****************************************************************************/
71 static int MixBuffer( aout_instance_t * p_aout )
73 int i, i_first_input = 0;
74 aout_buffer_t * p_output_buffer;
75 mtime_t start_date, end_date;
76 audio_date_t exact_start_date;
78 if ( p_aout->mixer.b_error )
80 /* Free all incoming buffers. */
81 vlc_mutex_lock( &p_aout->input_fifos_lock );
82 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
84 aout_input_t * p_input = p_aout->pp_inputs[i];
85 aout_buffer_t * p_buffer = p_input->fifo.p_first;
86 if ( p_input->b_error ) continue;
87 while ( p_buffer != NULL )
89 aout_buffer_t * p_next = p_buffer->p_next;
90 aout_BufferFree( p_buffer );
94 vlc_mutex_unlock( &p_aout->input_fifos_lock );
99 vlc_mutex_lock( &p_aout->output_fifo_lock );
100 vlc_mutex_lock( &p_aout->input_fifos_lock );
102 /* Retrieve the date of the next buffer. */
103 memcpy( &exact_start_date, &p_aout->output.fifo.end_date,
104 sizeof(audio_date_t) );
105 start_date = aout_DateGet( &exact_start_date );
107 if ( start_date != 0 && start_date < mdate() )
109 /* The output is _very_ late. This can only happen if the user
110 * pauses the stream (or if the decoder is buggy, which cannot
112 msg_Warn( p_aout, "output PTS is out of range ("I64Fd"), clearing out",
113 mdate() - start_date );
114 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
115 aout_DateSet( &exact_start_date, 0 );
119 vlc_mutex_unlock( &p_aout->output_fifo_lock );
121 /* See if we have enough data to prepare a new buffer for the audio
122 * output. First : start date. */
125 /* Find the latest start date available. */
126 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
128 aout_input_t * p_input = p_aout->pp_inputs[i];
129 aout_fifo_t * p_fifo = &p_input->fifo;
130 aout_buffer_t * p_buffer;
132 if ( p_input->b_error ) continue;
134 p_buffer = p_fifo->p_first;
135 while ( p_buffer != NULL && p_buffer->start_date < mdate() )
137 msg_Warn( p_aout, "input PTS is out of range ("I64Fd"), "
138 "trashing", mdate() - p_buffer->start_date );
139 p_buffer = aout_FifoPop( p_aout, p_fifo );
140 aout_BufferFree( p_buffer );
141 if( p_input->p_input_thread )
143 // stats_UpdateInteger( p_input->p_input_thread,
144 // "lost_abuffers", 1 );
146 p_buffer = p_fifo->p_first;
147 p_input->p_first_byte_to_mix = NULL;
150 if ( p_buffer == NULL )
155 if ( !start_date || start_date < p_buffer->start_date )
157 aout_DateSet( &exact_start_date, p_buffer->start_date );
158 start_date = p_buffer->start_date;
162 if ( i < p_aout->i_nb_inputs )
164 /* Interrupted before the end... We can't run. */
165 vlc_mutex_unlock( &p_aout->input_fifos_lock );
169 aout_DateIncrement( &exact_start_date, p_aout->output.i_nb_samples );
170 end_date = aout_DateGet( &exact_start_date );
172 /* Check that start_date and end_date are available for all input
174 for ( i = 0; i < p_aout->i_nb_inputs; i++ )
176 aout_input_t * p_input = p_aout->pp_inputs[i];
177 aout_fifo_t * p_fifo = &p_input->fifo;
178 aout_buffer_t * p_buffer;
180 vlc_bool_t b_drop_buffers;
182 if ( p_input->b_error )
184 if ( i_first_input == i ) i_first_input++;
188 p_buffer = p_fifo->p_first;
189 if ( p_buffer == NULL )
194 /* Check for the continuity of start_date */
195 while ( p_buffer != NULL && p_buffer->end_date < start_date - 1 )
197 /* We authorize a +-1 because rounding errors get compensated
199 aout_buffer_t * p_next = p_buffer->p_next;
200 msg_Warn( p_aout, "the mixer got a packet in the past ("I64Fd")",
201 start_date - p_buffer->end_date );
202 aout_BufferFree( p_buffer );
203 if( p_input->p_input_thread )
205 // stats_UpdateInteger( p_input->p_input_thread,
206 // "lost_abuffers", 1 );
208 p_fifo->p_first = p_buffer = p_next;
209 p_input->p_first_byte_to_mix = NULL;
211 if ( p_buffer == NULL )
213 p_fifo->pp_last = &p_fifo->p_first;
217 /* Check that we have enough samples. */
220 p_buffer = p_fifo->p_first;
221 if ( p_buffer == NULL ) break;
222 if ( p_buffer->end_date >= end_date ) break;
224 /* Check that all buffers are contiguous. */
225 prev_date = p_fifo->p_first->end_date;
226 p_buffer = p_buffer->p_next;
228 for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
230 if ( prev_date != p_buffer->start_date )
233 "buffer hole, dropping packets ("I64Fd")",
234 p_buffer->start_date - prev_date );
238 if ( p_buffer->end_date >= end_date ) break;
239 prev_date = p_buffer->end_date;
241 if ( b_drop_buffers )
243 aout_buffer_t * p_deleted = p_fifo->p_first;
244 while ( p_deleted != NULL && p_deleted != p_buffer )
246 aout_buffer_t * p_next = p_deleted->p_next;
247 aout_BufferFree( p_deleted );
250 p_fifo->p_first = p_deleted; /* == p_buffer */
254 if ( p_buffer == NULL ) break;
256 p_buffer = p_fifo->p_first;
257 if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
259 /* Additionally check that p_first_byte_to_mix is well
261 mtime_t i_nb_bytes = (start_date - p_buffer->start_date)
262 * p_aout->mixer.mixer.i_bytes_per_frame
263 * p_aout->mixer.mixer.i_rate
264 / p_aout->mixer.mixer.i_frame_length
266 ptrdiff_t mixer_nb_bytes;
268 if ( p_input->p_first_byte_to_mix == NULL )
270 p_input->p_first_byte_to_mix = p_buffer->p_buffer;
272 mixer_nb_bytes = p_input->p_first_byte_to_mix - p_buffer->p_buffer;
274 if ( !((i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame
276 (i_nb_bytes < p_aout->mixer.mixer.i_bytes_per_frame
279 msg_Warn( p_aout, "mixer start isn't output start ("I64Fd")",
280 i_nb_bytes - mixer_nb_bytes );
282 /* Round to the nearest multiple */
283 i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame;
284 i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame;
287 /* Is it really the best way to do it ? */
288 aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
289 aout_DateSet( &exact_start_date, 0 );
293 p_input->p_first_byte_to_mix = p_buffer->p_buffer + i_nb_bytes;
298 if ( i < p_aout->i_nb_inputs || i_first_input == p_aout->i_nb_inputs )
300 /* Interrupted before the end... We can't run. */
301 vlc_mutex_unlock( &p_aout->input_fifos_lock );
306 aout_BufferAlloc( &p_aout->mixer.output_alloc,
307 ((uint64_t)p_aout->output.i_nb_samples * 1000000)
308 / p_aout->output.output.i_rate,
309 /* This is a bit kludgy, but is actually only used
310 * for the S/PDIF dummy mixer : */
311 p_aout->pp_inputs[i_first_input]->fifo.p_first,
313 if ( p_output_buffer == NULL )
315 msg_Err( p_aout, "out of memory" );
316 vlc_mutex_unlock( &p_aout->input_fifos_lock );
319 /* This is again a bit kludgy - for the S/PDIF mixer. */
320 if ( p_aout->mixer.output_alloc.i_alloc_type != AOUT_ALLOC_NONE )
322 p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
323 p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
324 * p_aout->mixer.mixer.i_bytes_per_frame
325 / p_aout->mixer.mixer.i_frame_length;
327 p_output_buffer->start_date = start_date;
328 p_output_buffer->end_date = end_date;
330 p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
332 vlc_mutex_unlock( &p_aout->input_fifos_lock );
334 aout_OutputPlay( p_aout, p_output_buffer );
339 /*****************************************************************************
340 * aout_MixerRun: entry point for the mixer & post-filters processing
341 *****************************************************************************
342 * Please note that you must hold the mixer lock.
343 *****************************************************************************/
344 void aout_MixerRun( aout_instance_t * p_aout )
346 while( MixBuffer( p_aout ) != -1 );
349 /*****************************************************************************
350 * aout_MixerMultiplierSet: set p_aout->mixer.f_multiplier
351 *****************************************************************************
352 * Please note that we assume that you own the mixer lock when entering this
353 * function. This function returns -1 on error.
354 *****************************************************************************/
355 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier )
357 float f_old = p_aout->mixer.f_multiplier;
358 vlc_bool_t b_new_mixer = 0;
360 if ( !p_aout->mixer.b_error )
362 aout_MixerDelete( p_aout );
366 p_aout->mixer.f_multiplier = f_multiplier;
368 if ( b_new_mixer && aout_MixerNew( p_aout ) )
370 p_aout->mixer.f_multiplier = f_old;
371 aout_MixerNew( p_aout );
378 /*****************************************************************************
379 * aout_MixerMultiplierGet: get p_aout->mixer.f_multiplier
380 *****************************************************************************
381 * Please note that we assume that you own the mixer lock when entering this
382 * function. This function returns -1 on error.
383 *****************************************************************************/
384 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier )
386 *pf_multiplier = p_aout->mixer.f_multiplier;