1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: common.c,v 1.12 2002/12/25 02:23:37 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() */
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;
48 /* Allocate descriptor. */
49 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
55 /* Initialize members. */
56 vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
57 vlc_mutex_init( p_parent, &p_aout->mixer_lock );
58 vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
59 p_aout->i_nb_inputs = 0;
60 p_aout->mixer.f_multiplier = 1.0;
61 p_aout->mixer.b_error = 1;
62 p_aout->output.b_starving = 1;
64 var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
65 val.b_bool = VLC_TRUE;
66 var_Set( p_aout, "intf-change", val );
68 vlc_object_attach( p_aout, p_parent->p_vlc );
73 /*****************************************************************************
74 * aout_Delete: destroy aout structure
75 *****************************************************************************/
76 void aout_Delete( aout_instance_t * p_aout )
78 var_Destroy( p_aout, "intf-change" );
80 vlc_mutex_destroy( &p_aout->input_fifos_lock );
81 vlc_mutex_destroy( &p_aout->mixer_lock );
82 vlc_mutex_destroy( &p_aout->output_fifo_lock );
85 vlc_object_destroy( p_aout );
90 * Formats management (internal and external)
93 /*****************************************************************************
94 * aout_FormatNbChannels : return the number of channels
95 *****************************************************************************/
96 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
98 static const uint32_t pi_channels[] =
99 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
100 AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
102 unsigned int i_nb = 0, i;
104 for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
106 if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
112 /*****************************************************************************
113 * aout_FormatPrepare : compute the number of bytes per frame & frame length
114 *****************************************************************************/
115 void aout_FormatPrepare( audio_sample_format_t * p_format )
119 switch ( p_format->i_format )
121 case VLC_FOURCC('u','8',' ',' '):
122 case VLC_FOURCC('s','8',' ',' '):
126 case VLC_FOURCC('u','1','6','l'):
127 case VLC_FOURCC('s','1','6','l'):
128 case VLC_FOURCC('u','1','6','b'):
129 case VLC_FOURCC('s','1','6','b'):
133 case VLC_FOURCC('f','l','3','2'):
134 case VLC_FOURCC('f','i','3','2'):
138 case VLC_FOURCC('s','p','d','i'):
139 case VLC_FOURCC('a','5','2',' '):
140 case VLC_FOURCC('d','t','s',' '):
142 /* For these formats the caller has to indicate the parameters
147 p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
148 p_format->i_frame_length = 1;
151 /*****************************************************************************
152 * aout_FormatPrintChannels : print a channel in a human-readable form
153 *****************************************************************************/
154 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
156 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
158 case AOUT_CHAN_CENTER:
159 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
160 || (p_format->i_original_channels
161 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
163 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
166 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
167 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
169 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
171 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
173 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
174 return "Stereo/Left";
175 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
176 return "Stereo/Right";
178 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
180 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
182 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
183 | AOUT_CHAN_REARCENTER:
185 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
186 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
188 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
189 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
192 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
193 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
194 || (p_format->i_original_channels
195 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
197 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
200 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
201 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
203 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
204 return "Dual-mono/LFE";
205 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
207 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
208 return "Stereo/Left/LFE";
209 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
210 return "Stereo/Right/LFE";
212 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
214 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
217 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
218 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
220 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
221 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
223 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
224 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
231 /*****************************************************************************
232 * aout_FormatPrint : print a format in a human-readable form
233 *****************************************************************************/
234 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
235 const audio_sample_format_t * p_format )
237 msg_Dbg( p_aout, "%s format='%4.4s' rate=%d channels=%s", psz_text,
238 (char *)&p_format->i_format, p_format->i_rate,
239 aout_FormatPrintChannels( p_format ) );
242 /*****************************************************************************
243 * aout_FormatsPrint : print two formats in a human-readable form
244 *****************************************************************************/
245 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
246 const audio_sample_format_t * p_format1,
247 const audio_sample_format_t * p_format2 )
249 msg_Dbg( p_aout, "%s format='%4.4s'->'%4.4s' rate=%d->%d channels=%s->%s",
251 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
252 p_format1->i_rate, p_format2->i_rate,
253 aout_FormatPrintChannels( p_format1 ),
254 aout_FormatPrintChannels( p_format2 ) );
259 * FIFO management (internal) - please understand that solving race conditions
260 * is _your_ job, ie. in the audio output you should own the mixer lock
261 * before calling any of these functions.
264 /*****************************************************************************
265 * aout_FifoInit : initialize the members of a FIFO
266 *****************************************************************************/
267 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
270 p_fifo->p_first = NULL;
271 p_fifo->pp_last = &p_fifo->p_first;
272 aout_DateInit( &p_fifo->end_date, i_rate );
275 /*****************************************************************************
276 * aout_FifoPush : push a packet into the FIFO
277 *****************************************************************************/
278 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
279 aout_buffer_t * p_buffer )
281 *p_fifo->pp_last = p_buffer;
282 p_fifo->pp_last = &p_buffer->p_next;
283 *p_fifo->pp_last = NULL;
284 /* Enforce the continuity of the stream. */
285 if ( aout_DateGet( &p_fifo->end_date ) )
287 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
288 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
289 p_buffer->i_nb_samples );
293 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
297 /*****************************************************************************
298 * aout_FifoSet : set end_date and trash all buffers (because they aren't
300 *****************************************************************************/
301 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
304 aout_buffer_t * p_buffer;
306 aout_DateSet( &p_fifo->end_date, date );
307 p_buffer = p_fifo->p_first;
308 while ( p_buffer != NULL )
310 aout_buffer_t * p_next = p_buffer->p_next;
311 aout_BufferFree( p_buffer );
314 p_fifo->p_first = NULL;
315 p_fifo->pp_last = &p_fifo->p_first;
318 /*****************************************************************************
319 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
320 *****************************************************************************/
321 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
324 aout_buffer_t * p_buffer;
326 aout_DateMove( &p_fifo->end_date, difference );
327 p_buffer = p_fifo->p_first;
328 while ( p_buffer != NULL )
330 p_buffer->start_date += difference;
331 p_buffer->end_date += difference;
332 p_buffer = p_buffer->p_next;
336 /*****************************************************************************
337 * aout_FifoNextStart : return the current end_date
338 *****************************************************************************/
339 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
341 return aout_DateGet( &p_fifo->end_date );
344 /*****************************************************************************
345 * aout_FifoFirstDate : return the playing date of the first buffer in the
347 *****************************************************************************/
348 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
350 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
353 /*****************************************************************************
354 * aout_FifoPop : get the next buffer out of the FIFO
355 *****************************************************************************/
356 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
358 aout_buffer_t * p_buffer;
360 p_buffer = p_fifo->p_first;
361 if ( p_buffer == NULL ) return NULL;
362 p_fifo->p_first = p_buffer->p_next;
363 if ( p_fifo->p_first == NULL )
365 p_fifo->pp_last = &p_fifo->p_first;
371 /*****************************************************************************
372 * aout_FifoDestroy : destroy a FIFO and its buffers
373 *****************************************************************************/
374 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
376 aout_buffer_t * p_buffer;
378 p_buffer = p_fifo->p_first;
379 while ( p_buffer != NULL )
381 aout_buffer_t * p_next = p_buffer->p_next;
382 aout_BufferFree( p_buffer );
389 * Date management (internal and external)
392 /*****************************************************************************
393 * aout_DateInit : set the divider of an audio_date_t
394 *****************************************************************************/
395 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
398 p_date->i_divider = i_divider;
399 p_date->i_remainder = 0;
402 /*****************************************************************************
403 * aout_DateSet : set the date of an audio_date_t
404 *****************************************************************************/
405 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
407 p_date->date = new_date;
408 p_date->i_remainder = 0;
411 /*****************************************************************************
412 * aout_DateMove : move forwards or backwards the date of an audio_date_t
413 *****************************************************************************/
414 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
416 p_date->date += difference;
419 /*****************************************************************************
420 * aout_DateGet : get the date of an audio_date_t
421 *****************************************************************************/
422 mtime_t aout_DateGet( const audio_date_t * p_date )
427 /*****************************************************************************
428 * aout_DateIncrement : increment the date and return the result, taking
429 * into account rounding errors
430 *****************************************************************************/
431 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
433 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
434 p_date->date += i_dividend / p_date->i_divider;
435 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
436 if ( p_date->i_remainder >= p_date->i_divider )
438 /* This is Bresenham algorithm. */
440 p_date->i_remainder -= p_date->i_divider;