1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: common.c,v 1.8 2002/11/13 20:51:04 sam 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() */
32 #include "audio_output.h"
33 #include "aout_internal.h"
37 * Instances management (internal and external)
40 /*****************************************************************************
41 * aout_New: initialize aout structure
42 *****************************************************************************/
43 aout_instance_t * __aout_New( vlc_object_t * p_parent )
45 aout_instance_t * p_aout;
47 /* Allocate descriptor. */
48 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
54 /* Initialize members. */
55 vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
56 vlc_mutex_init( p_parent, &p_aout->mixer_lock );
57 vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
58 p_aout->i_nb_inputs = 0;
59 p_aout->mixer.f_multiplier = 1.0;
60 p_aout->mixer.b_error = 1;
61 p_aout->output.b_starving = 1;
63 vlc_object_attach( p_aout, p_parent->p_vlc );
68 /*****************************************************************************
69 * aout_Delete: destroy aout structure
70 *****************************************************************************/
71 void aout_Delete( 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 * Formats management (internal and external)
86 /*****************************************************************************
87 * aout_FormatNbChannels : return the number of channels
88 *****************************************************************************/
89 int aout_FormatNbChannels( audio_sample_format_t * p_format )
93 switch ( p_format->i_channels & AOUT_CHAN_MASK )
95 case AOUT_CHAN_CHANNEL1:
96 case AOUT_CHAN_CHANNEL2:
101 case AOUT_CHAN_CHANNEL:
102 case AOUT_CHAN_STEREO:
103 case AOUT_CHAN_DOLBY:
125 if ( p_format->i_channels & AOUT_CHAN_LFE )
131 /*****************************************************************************
132 * aout_FormatPrepare : compute the number of bytes per frame & frame length
133 *****************************************************************************/
134 void aout_FormatPrepare( audio_sample_format_t * p_format )
138 switch ( p_format->i_format )
140 case VLC_FOURCC('u','8',' ',' '):
141 case VLC_FOURCC('s','8',' ',' '):
145 case VLC_FOURCC('u','1','6','l'):
146 case VLC_FOURCC('s','1','6','l'):
147 case VLC_FOURCC('u','1','6','b'):
148 case VLC_FOURCC('s','1','6','b'):
152 case VLC_FOURCC('f','l','3','2'):
153 case VLC_FOURCC('f','i','3','2'):
157 case VLC_FOURCC('s','p','d','i'):
158 case VLC_FOURCC('a','5','2',' '):
159 case VLC_FOURCC('d','t','s',' '):
161 /* For these formats the caller has to indicate the parameters
166 p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
167 p_format->i_frame_length = 1;
170 /*****************************************************************************
171 * FormatPrintChannels : print a channel in a human-readable form
172 *****************************************************************************/
173 static const char * FormatPrintChannels( int i_channels )
175 switch ( i_channels )
177 case AOUT_CHAN_CHANNEL: return "CHANNEL";
178 case AOUT_CHAN_CHANNEL1: return "CHANNEL1";
179 case AOUT_CHAN_CHANNEL2: return "CHANNEL2";
180 case AOUT_CHAN_MONO: return "MONO";
181 case AOUT_CHAN_STEREO: return "STEREO";
182 case AOUT_CHAN_3F: return "3F";
183 case AOUT_CHAN_2F1R: return "2F1R";
184 case AOUT_CHAN_3F1R: return "3F1R";
185 case AOUT_CHAN_2F2R: return "2F2R";
186 case AOUT_CHAN_3F2R: return "3F2R";
187 case AOUT_CHAN_DOLBY: return "DOLBY";
188 case AOUT_CHAN_CHANNEL | AOUT_CHAN_LFE: return "CHANNEL|LFE";
189 case AOUT_CHAN_CHANNEL1 | AOUT_CHAN_LFE: return "CHANNEL1|LFE";
190 case AOUT_CHAN_CHANNEL2 | AOUT_CHAN_LFE: return "CHANNEL2|LFE";
191 case AOUT_CHAN_MONO | AOUT_CHAN_LFE: return "MONO|LFE";
192 case AOUT_CHAN_STEREO | AOUT_CHAN_LFE: return "STEREO|LFE";
193 case AOUT_CHAN_3F | AOUT_CHAN_LFE: return "3F|LFE";
194 case AOUT_CHAN_2F1R | AOUT_CHAN_LFE: return "2F1R|LFE";
195 case AOUT_CHAN_3F1R | AOUT_CHAN_LFE: return "3F1R|LFE";
196 case AOUT_CHAN_2F2R | AOUT_CHAN_LFE: return "2F2R|LFE";
197 case AOUT_CHAN_3F2R | AOUT_CHAN_LFE: return "3F2R|LFE";
198 case AOUT_CHAN_DOLBY | AOUT_CHAN_LFE: return "DOLBY|LFE";
204 /*****************************************************************************
205 * aout_FormatPrint : print a format in a human-readable form
206 *****************************************************************************/
207 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
208 const audio_sample_format_t * p_format )
210 msg_Dbg( p_aout, "%s format='%4.4s' rate=%d channels=%s", psz_text,
211 (char *)&p_format->i_format, p_format->i_rate,
212 FormatPrintChannels( p_format->i_channels ) );
215 /*****************************************************************************
216 * aout_FormatsPrint : print two formats in a human-readable form
217 *****************************************************************************/
218 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
219 const audio_sample_format_t * p_format1,
220 const audio_sample_format_t * p_format2 )
222 msg_Dbg( p_aout, "%s format='%4.4s'->'%4.4s' rate=%d->%d channels=%s->%s",
224 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
225 p_format1->i_rate, p_format2->i_rate,
226 FormatPrintChannels( p_format1->i_channels ),
227 FormatPrintChannels( p_format2->i_channels ) );
232 * FIFO management (internal) - please understand that solving race conditions
233 * is _your_ job, ie. in the audio output you should own the mixer lock
234 * before calling any of these functions.
237 /*****************************************************************************
238 * aout_FifoInit : initialize the members of a FIFO
239 *****************************************************************************/
240 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
243 p_fifo->p_first = NULL;
244 p_fifo->pp_last = &p_fifo->p_first;
245 aout_DateInit( &p_fifo->end_date, i_rate );
248 /*****************************************************************************
249 * aout_FifoPush : push a packet into the FIFO
250 *****************************************************************************/
251 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
252 aout_buffer_t * p_buffer )
254 *p_fifo->pp_last = p_buffer;
255 p_fifo->pp_last = &p_buffer->p_next;
256 *p_fifo->pp_last = NULL;
257 /* Enforce the continuity of the stream. */
258 if ( aout_DateGet( &p_fifo->end_date ) )
260 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
261 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
262 p_buffer->i_nb_samples );
266 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
270 /*****************************************************************************
271 * aout_FifoSet : set end_date and trash all buffers (because they aren't
273 *****************************************************************************/
274 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
277 aout_buffer_t * p_buffer;
279 aout_DateSet( &p_fifo->end_date, date );
280 p_buffer = p_fifo->p_first;
281 while ( p_buffer != NULL )
283 aout_buffer_t * p_next = p_buffer->p_next;
284 aout_BufferFree( p_buffer );
287 p_fifo->p_first = NULL;
288 p_fifo->pp_last = &p_fifo->p_first;
291 /*****************************************************************************
292 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
293 *****************************************************************************/
294 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
297 aout_buffer_t * p_buffer;
299 aout_DateMove( &p_fifo->end_date, difference );
300 p_buffer = p_fifo->p_first;
301 while ( p_buffer != NULL )
303 p_buffer->start_date += difference;
304 p_buffer->end_date += difference;
305 p_buffer = p_buffer->p_next;
309 /*****************************************************************************
310 * aout_FifoNextStart : return the current end_date
311 *****************************************************************************/
312 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
314 return aout_DateGet( &p_fifo->end_date );
317 /*****************************************************************************
318 * aout_FifoFirstDate : return the playing date of the first buffer in the
320 *****************************************************************************/
321 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
323 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
326 /*****************************************************************************
327 * aout_FifoPop : get the next buffer out of the FIFO
328 *****************************************************************************/
329 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
331 aout_buffer_t * p_buffer;
332 p_buffer = p_fifo->p_first;
333 if ( p_buffer == NULL ) return NULL;
334 p_fifo->p_first = p_buffer->p_next;
335 if ( p_fifo->p_first == NULL )
337 p_fifo->pp_last = &p_fifo->p_first;
343 /*****************************************************************************
344 * aout_FifoDestroy : destroy a FIFO and its buffers
345 *****************************************************************************/
346 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
348 aout_buffer_t * p_buffer;
350 p_buffer = p_fifo->p_first;
351 while ( p_buffer != NULL )
353 aout_buffer_t * p_next = p_buffer->p_next;
354 aout_BufferFree( p_buffer );
361 * Date management (internal and external)
364 /*****************************************************************************
365 * aout_DateInit : set the divider of an audio_date_t
366 *****************************************************************************/
367 void aout_DateInit( audio_date_t * p_date, u32 i_divider )
370 p_date->i_divider = i_divider;
371 p_date->i_remainder = 0;
374 /*****************************************************************************
375 * aout_DateSet : set the date of an audio_date_t
376 *****************************************************************************/
377 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
379 p_date->date = new_date;
380 p_date->i_remainder = 0;
383 /*****************************************************************************
384 * aout_DateMove : move forwards or backwards the date of an audio_date_t
385 *****************************************************************************/
386 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
388 p_date->date += difference;
391 /*****************************************************************************
392 * aout_DateGet : get the date of an audio_date_t
393 *****************************************************************************/
394 mtime_t aout_DateGet( const audio_date_t * p_date )
399 /*****************************************************************************
400 * aout_DateIncrement : increment the date and return the result, taking
401 * into account rounding errors
402 *****************************************************************************/
403 mtime_t aout_DateIncrement( audio_date_t * p_date, u32 i_nb_samples )
405 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
406 p_date->date += i_dividend / p_date->i_divider;
407 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
408 if ( p_date->i_remainder >= p_date->i_divider )
410 /* This is Bresenham algorithm. */
412 p_date->i_remainder -= p_date->i_divider;