1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002-2005 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., 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_error = 1;
63 p_aout->output.b_starving = 1;
65 var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
66 val.b_bool = VLC_TRUE;
67 var_Set( p_aout, "intf-change", val );
72 /*****************************************************************************
73 * aout_Delete: destroy aout structure
74 *****************************************************************************/
75 void aout_Delete( aout_instance_t * p_aout )
77 var_Destroy( p_aout, "intf-change" );
79 vlc_mutex_destroy( &p_aout->input_fifos_lock );
80 vlc_mutex_destroy( &p_aout->mixer_lock );
81 vlc_mutex_destroy( &p_aout->output_fifo_lock );
84 vlc_object_destroy( p_aout );
89 * Formats management (internal and external)
92 /*****************************************************************************
93 * aout_FormatNbChannels : return the number of channels
94 *****************************************************************************/
95 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
97 static const uint32_t pi_channels[] =
98 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
99 AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
100 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
101 unsigned int i_nb = 0, i;
103 for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
105 if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
111 /*****************************************************************************
112 * aout_FormatPrepare : compute the number of bytes per frame & frame length
113 *****************************************************************************/
114 void aout_FormatPrepare( audio_sample_format_t * p_format )
118 switch ( p_format->i_format )
120 case VLC_FOURCC('u','8',' ',' '):
121 case VLC_FOURCC('s','8',' ',' '):
125 case VLC_FOURCC('u','1','6','l'):
126 case VLC_FOURCC('s','1','6','l'):
127 case VLC_FOURCC('u','1','6','b'):
128 case VLC_FOURCC('s','1','6','b'):
132 case VLC_FOURCC('u','2','4','l'):
133 case VLC_FOURCC('s','2','4','l'):
134 case VLC_FOURCC('u','2','4','b'):
135 case VLC_FOURCC('s','2','4','b'):
139 case VLC_FOURCC('f','l','3','2'):
140 case VLC_FOURCC('f','i','3','2'):
144 case VLC_FOURCC('s','p','d','i'):
145 case VLC_FOURCC('a','5','2',' '):
146 case VLC_FOURCC('d','t','s',' '):
147 case VLC_FOURCC('m','p','g','a'):
148 case VLC_FOURCC('m','p','g','3'):
150 /* For these formats the caller has to indicate the parameters
155 p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
156 p_format->i_frame_length = 1;
159 /*****************************************************************************
160 * aout_FormatPrintChannels : print a channel in a human-readable form
161 *****************************************************************************/
162 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
164 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
166 case AOUT_CHAN_CENTER:
167 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
168 || (p_format->i_original_channels
169 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
171 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
174 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
175 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
177 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
178 return "Dolby/Reverse";
179 return "Stereo/Reverse";
183 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
185 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
187 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
188 return "Stereo/Mono";
189 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
190 return "Stereo/Left";
191 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
192 return "Stereo/Right";
195 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
197 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
199 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
200 | AOUT_CHAN_REARCENTER:
202 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
203 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
205 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
206 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
209 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
210 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
211 || (p_format->i_original_channels
212 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
214 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
217 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
218 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
220 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
221 return "Dual-mono/LFE";
222 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
224 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
225 return "Stereo/Left/LFE";
226 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
227 return "Stereo/Right/LFE";
229 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
231 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
234 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
235 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
237 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
238 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
240 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
241 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
243 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
244 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
245 | AOUT_CHAN_MIDDLERIGHT:
247 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
248 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
249 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
256 /*****************************************************************************
257 * aout_FormatPrint : print a format in a human-readable form
258 *****************************************************************************/
259 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
260 const audio_sample_format_t * p_format )
262 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
263 (char *)&p_format->i_format, p_format->i_rate,
264 aout_FormatPrintChannels( p_format ),
265 p_format->i_frame_length, p_format->i_bytes_per_frame );
268 /*****************************************************************************
269 * aout_FormatsPrint : print two formats in a human-readable form
270 *****************************************************************************/
271 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
272 const audio_sample_format_t * p_format1,
273 const audio_sample_format_t * p_format2 )
275 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
277 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
278 p_format1->i_rate, p_format2->i_rate,
279 aout_FormatPrintChannels( p_format1 ),
280 aout_FormatPrintChannels( p_format2 ) );
285 * FIFO management (internal) - please understand that solving race conditions
286 * is _your_ job, ie. in the audio output you should own the mixer lock
287 * before calling any of these functions.
290 /*****************************************************************************
291 * aout_FifoInit : initialize the members of a FIFO
292 *****************************************************************************/
293 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
296 p_fifo->p_first = NULL;
297 p_fifo->pp_last = &p_fifo->p_first;
298 aout_DateInit( &p_fifo->end_date, i_rate );
301 /*****************************************************************************
302 * aout_FifoPush : push a packet into the FIFO
303 *****************************************************************************/
304 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
305 aout_buffer_t * p_buffer )
307 *p_fifo->pp_last = p_buffer;
308 p_fifo->pp_last = &p_buffer->p_next;
309 *p_fifo->pp_last = NULL;
310 /* Enforce the continuity of the stream. */
311 if ( aout_DateGet( &p_fifo->end_date ) )
313 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
314 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
315 p_buffer->i_nb_samples );
319 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
323 /*****************************************************************************
324 * aout_FifoSet : set end_date and trash all buffers (because they aren't
326 *****************************************************************************/
327 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
330 aout_buffer_t * p_buffer;
332 aout_DateSet( &p_fifo->end_date, date );
333 p_buffer = p_fifo->p_first;
334 while ( p_buffer != NULL )
336 aout_buffer_t * p_next = p_buffer->p_next;
337 aout_BufferFree( p_buffer );
340 p_fifo->p_first = NULL;
341 p_fifo->pp_last = &p_fifo->p_first;
344 /*****************************************************************************
345 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
346 *****************************************************************************/
347 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
350 aout_buffer_t * p_buffer;
352 aout_DateMove( &p_fifo->end_date, difference );
353 p_buffer = p_fifo->p_first;
354 while ( p_buffer != NULL )
356 p_buffer->start_date += difference;
357 p_buffer->end_date += difference;
358 p_buffer = p_buffer->p_next;
362 /*****************************************************************************
363 * aout_FifoNextStart : return the current end_date
364 *****************************************************************************/
365 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
367 return aout_DateGet( &p_fifo->end_date );
370 /*****************************************************************************
371 * aout_FifoFirstDate : return the playing date of the first buffer in the
373 *****************************************************************************/
374 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
376 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
379 /*****************************************************************************
380 * aout_FifoPop : get the next buffer out of the FIFO
381 *****************************************************************************/
382 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
384 aout_buffer_t * p_buffer;
386 p_buffer = p_fifo->p_first;
387 if ( p_buffer == NULL ) return NULL;
388 p_fifo->p_first = p_buffer->p_next;
389 if ( p_fifo->p_first == NULL )
391 p_fifo->pp_last = &p_fifo->p_first;
397 /*****************************************************************************
398 * aout_FifoDestroy : destroy a FIFO and its buffers
399 *****************************************************************************/
400 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
402 aout_buffer_t * p_buffer;
404 p_buffer = p_fifo->p_first;
405 while ( p_buffer != NULL )
407 aout_buffer_t * p_next = p_buffer->p_next;
408 aout_BufferFree( p_buffer );
412 p_fifo->p_first = NULL;
413 p_fifo->pp_last = &p_fifo->p_first;
418 * Date management (internal and external)
421 /*****************************************************************************
422 * aout_DateInit : set the divider of an audio_date_t
423 *****************************************************************************/
424 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
427 p_date->i_divider = i_divider;
428 p_date->i_remainder = 0;
431 /*****************************************************************************
432 * aout_DateSet : set the date of an audio_date_t
433 *****************************************************************************/
434 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
436 p_date->date = new_date;
437 p_date->i_remainder = 0;
440 /*****************************************************************************
441 * aout_DateMove : move forwards or backwards the date of an audio_date_t
442 *****************************************************************************/
443 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
445 p_date->date += difference;
448 /*****************************************************************************
449 * aout_DateGet : get the date of an audio_date_t
450 *****************************************************************************/
451 mtime_t aout_DateGet( const audio_date_t * p_date )
456 /*****************************************************************************
457 * aout_DateIncrement : increment the date and return the result, taking
458 * into account rounding errors
459 *****************************************************************************/
460 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
462 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
463 p_date->date += i_dividend / p_date->i_divider;
464 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
465 if ( p_date->i_remainder >= p_date->i_divider )
467 /* This is Bresenham algorithm. */
469 p_date->i_remainder -= p_date->i_divider;
474 /*****************************************************************************
475 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
476 *****************************************************************************/
477 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
478 const uint32_t *pi_chan_order_out,
479 uint32_t i_channel_mask,
480 int i_channels, int *pi_chan_table )
482 vlc_bool_t b_chan_reorder = VLC_FALSE;
485 if( i_channels > AOUT_CHAN_MAX ) return VLC_FALSE;
487 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
489 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
491 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
493 if( i_channel_mask & pi_chan_order_out[k] ) l++;
496 pi_chan_table[j++] = l;
499 for( i = 0; i < i_channels; i++ )
501 if( pi_chan_table[i] != i ) b_chan_reorder = VLC_TRUE;
504 return b_chan_reorder;
507 /*****************************************************************************
508 * aout_ChannelReorder :
509 *****************************************************************************/
510 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
511 int i_channels, const int *pi_chan_table,
512 int i_bits_per_sample )
514 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
517 if( i_bits_per_sample == 8 )
519 for( i = 0; i < i_buffer / i_channels; i++ )
521 for( j = 0; j < i_channels; j++ )
523 p_tmp[pi_chan_table[j]] = p_buf[j];
526 memcpy( p_buf, p_tmp, i_channels );
530 else if( i_bits_per_sample == 16 )
532 for( i = 0; i < i_buffer / i_channels / 2; i++ )
534 for( j = 0; j < i_channels; j++ )
536 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
537 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
540 memcpy( p_buf, p_tmp, 2 * i_channels );
541 p_buf += 2 * i_channels;
544 else if( i_bits_per_sample == 24 )
546 for( i = 0; i < i_buffer / i_channels / 3; i++ )
548 for( j = 0; j < i_channels; j++ )
550 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
551 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
552 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
555 memcpy( p_buf, p_tmp, 3 * i_channels );
556 p_buf += 3 * i_channels;
559 else if( i_bits_per_sample == 32 )
561 for( i = 0; i < i_buffer / i_channels / 4; i++ )
563 for( j = 0; j < i_channels; j++ )
565 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
566 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
567 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
568 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
571 memcpy( p_buf, p_tmp, 4 * i_channels );
572 p_buf += 4 * i_channels;