1 /*****************************************************************************
2 * audio_output.c : audio output instance miscellaneous functions
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: audio_output.c,v 1.100 2002/08/30 22:22:24 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"
40 * Instances management (see also input.c:aout_InputNew())
43 /*****************************************************************************
44 * aout_NewInstance: initialize aout structure
45 *****************************************************************************/
46 aout_instance_t * __aout_NewInstance( vlc_object_t * p_parent )
48 aout_instance_t * p_aout;
50 /* Allocate descriptor. */
51 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
57 /* Initialize members. */
58 vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
59 vlc_mutex_init( p_parent, &p_aout->mixer_lock );
60 vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
61 p_aout->i_nb_inputs = 0;
63 vlc_object_attach( p_aout, p_parent->p_vlc );
68 /*****************************************************************************
69 * aout_DeleteInstance: destroy aout structure
70 *****************************************************************************/
71 void aout_DeleteInstance( aout_instance_t * p_aout )
73 vlc_mutex_destroy( &p_aout->input_fifos_lock );
74 vlc_mutex_destroy( &p_aout->mixer_lock );
75 vlc_mutex_destroy( &p_aout->output_fifo_lock );
78 vlc_object_destroy( p_aout );
83 * Buffer management (interface to the decoders)
86 /*****************************************************************************
87 * aout_BufferNew : ask for a new empty buffer
88 *****************************************************************************/
89 aout_buffer_t * aout_BufferNew( aout_instance_t * p_aout,
90 aout_input_t * p_input,
93 aout_buffer_t * p_buffer;
94 mtime_t duration = (1000000 * (mtime_t)i_nb_samples)
95 / p_input->input.i_rate;
97 /* This necessarily allocates in the heap. */
98 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
99 p_buffer->i_nb_samples = i_nb_samples;
100 p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
101 / p_input->input.i_frame_length;
103 if ( p_buffer == NULL )
105 msg_Err( p_aout, "NULL buffer !" );
109 p_buffer->start_date = p_buffer->end_date = 0;
115 /*****************************************************************************
116 * aout_BufferDelete : destroy an undecoded buffer
117 *****************************************************************************/
118 void aout_BufferDelete( aout_instance_t * p_aout, aout_input_t * p_input,
119 aout_buffer_t * p_buffer )
121 aout_BufferFree( p_buffer );
124 /*****************************************************************************
125 * aout_BufferPlay : filter & mix the decoded buffer
126 *****************************************************************************/
127 void aout_BufferPlay( aout_instance_t * p_aout, aout_input_t * p_input,
128 aout_buffer_t * p_buffer )
130 if ( p_buffer->start_date == 0 )
132 msg_Warn( p_aout, "non-dated buffer received" );
133 aout_BufferFree( p_buffer );
137 p_buffer->end_date = p_buffer->start_date
138 + (mtime_t)(p_buffer->i_nb_samples * 1000000)
139 / p_input->input.i_rate;
142 /* If the buffer is too early, wait a while. */
143 mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
145 aout_InputPlay( p_aout, p_input, p_buffer );
147 /* Run the mixer if it is able to run. */
148 aout_MixerRun( p_aout );
153 * Formats management (internal)
156 /*****************************************************************************
157 * aout_FormatPrepare : compute the number of bytes per frame & frame length
158 *****************************************************************************/
159 void aout_FormatPrepare( audio_sample_format_t * p_format )
163 switch ( p_format->i_format )
170 case AOUT_FMT_U16_LE:
171 case AOUT_FMT_U16_BE:
172 case AOUT_FMT_S16_LE:
173 case AOUT_FMT_S16_BE:
177 case AOUT_FMT_FLOAT32:
178 case AOUT_FMT_FIXED32:
185 /* For these formats the caller has to indicate the parameters
190 i_result = 0; /* will segfault much sooner... */
193 p_format->i_bytes_per_frame = i_result * p_format->i_channels;
194 p_format->i_frame_length = 1;
199 * FIFO management (internal) - please understand that solving race conditions
200 * is _your_ job, ie. in the audio output you should own the mixer lock
201 * before calling any of these functions.
204 /*****************************************************************************
205 * aout_FifoInit : initialize the members of a FIFO
206 *****************************************************************************/
207 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
210 p_fifo->p_first = NULL;
211 p_fifo->pp_last = &p_fifo->p_first;
212 aout_DateInit( &p_fifo->end_date, i_rate );
215 /*****************************************************************************
216 * aout_FifoPush : push a packet into the FIFO
217 *****************************************************************************/
218 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
219 aout_buffer_t * p_buffer )
221 *p_fifo->pp_last = p_buffer;
222 p_fifo->pp_last = &p_buffer->p_next;
223 *p_fifo->pp_last = NULL;
224 /* Enforce the continuity of the stream. */
225 if ( aout_DateGet( &p_fifo->end_date ) )
227 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
228 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
229 p_buffer->i_nb_samples );
233 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
237 /*****************************************************************************
238 * aout_FifoSet : set end_date and trash all buffers (because they aren't
240 *****************************************************************************/
241 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
244 aout_buffer_t * p_buffer;
246 aout_DateSet( &p_fifo->end_date, date );
247 p_buffer = p_fifo->p_first;
248 while ( p_buffer != NULL )
250 aout_buffer_t * p_next = p_buffer->p_next;
251 aout_BufferFree( p_buffer );
254 p_fifo->p_first = NULL;
255 p_fifo->pp_last = &p_fifo->p_first;
258 /*****************************************************************************
259 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
260 *****************************************************************************/
261 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
264 aout_buffer_t * p_buffer;
266 aout_DateMove( &p_fifo->end_date, difference );
267 p_buffer = p_fifo->p_first;
268 while ( p_buffer != NULL )
270 p_buffer->start_date += difference;
271 p_buffer->end_date += difference;
272 p_buffer = p_buffer->p_next;
276 /*****************************************************************************
277 * aout_FifoNextStart : return the current end_date
278 *****************************************************************************/
279 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
281 return aout_DateGet( &p_fifo->end_date );
284 /*****************************************************************************
285 * aout_FifoPop : get the next buffer out of the FIFO
286 *****************************************************************************/
287 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
289 aout_buffer_t * p_buffer;
290 p_buffer = p_fifo->p_first;
291 if ( p_buffer == NULL ) return NULL;
292 p_fifo->p_first = p_buffer->p_next;
293 if ( p_fifo->p_first == NULL )
295 p_fifo->pp_last = &p_fifo->p_first;
301 /*****************************************************************************
302 * aout_FifoDestroy : destroy a FIFO and its buffers
303 *****************************************************************************/
304 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
306 aout_buffer_t * p_buffer;
308 p_buffer = p_fifo->p_first;
309 while ( p_buffer != NULL )
311 aout_buffer_t * p_next = p_buffer->p_next;
312 aout_BufferFree( p_buffer );
319 * Date management (internal and external)
322 /*****************************************************************************
323 * aout_DateInit : set the divider of an audio_date_t
324 *****************************************************************************/
325 void aout_DateInit( audio_date_t * p_date, u32 i_divider )
328 p_date->i_divider = i_divider;
329 p_date->i_remainder = 0;
332 /*****************************************************************************
333 * aout_DateSet : set the date of an audio_date_t
334 *****************************************************************************/
335 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
337 p_date->date = new_date;
338 p_date->i_remainder = 0;
341 /*****************************************************************************
342 * aout_DateMove : move forwards or backwards the date of an audio_date_t
343 *****************************************************************************/
344 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
346 p_date->date += difference;
349 /*****************************************************************************
350 * aout_DateGet : get the date of an audio_date_t
351 *****************************************************************************/
352 mtime_t aout_DateGet( const audio_date_t * p_date )
357 /*****************************************************************************
358 * aout_DateIncrement : increment the date and return the result, taking
359 * into account rounding errors
360 *****************************************************************************/
361 mtime_t aout_DateIncrement( audio_date_t * p_date, u32 i_nb_samples )
363 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
364 p_date->date += i_dividend / p_date->i_divider;
365 p_date->i_remainder += i_dividend % p_date->i_divider;
366 if ( p_date->i_remainder >= p_date->i_divider )
368 /* This is Bresenham algorithm. */
370 p_date->i_remainder -= p_date->i_divider;