1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002-2007 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 *****************************************************************************/
34 #include <vlc_common.h>
36 #include "aout_internal.h"
40 * Instances management (internal and external)
44 static void aout_Destructor( vlc_object_t * p_this );
46 /*****************************************************************************
47 * aout_New: initialize aout structure
48 *****************************************************************************/
49 aout_instance_t * __aout_New( vlc_object_t * p_parent )
51 aout_instance_t * p_aout;
53 /* Allocate descriptor. */
54 p_aout = vlc_custom_create( p_parent, sizeof( *p_aout ), VLC_OBJECT_AOUT,
61 /* Initialize members. */
62 vlc_mutex_init( &p_aout->volume_lock );
63 vlc_mutex_init( &p_aout->lock );
64 p_aout->p_input = NULL;
65 p_aout->mixer_multiplier = 1.0;
66 p_aout->p_mixer = NULL;
67 p_aout->output.b_starving = 1;
68 p_aout->output.p_module = NULL;
70 var_Create( p_aout, "intf-change", VLC_VAR_VOID );
72 vlc_object_set_destructor( p_aout, aout_Destructor );
77 /*****************************************************************************
78 * aout_Destructor: destroy aout structure
79 *****************************************************************************/
80 static void aout_Destructor( vlc_object_t * p_this )
82 aout_instance_t * p_aout = (aout_instance_t *)p_this;
83 vlc_mutex_destroy( &p_aout->volume_lock );
84 vlc_mutex_destroy( &p_aout->lock );
89 static __thread unsigned aout_locks = 0;
91 void aout_lock_check (unsigned i)
100 allowed = VOLUME_LOCK;
106 if (aout_locks & ~allowed)
108 fprintf (stderr, "Illegal audio lock transition (%x -> %x)\n",
109 aout_locks, aout_locks|i);
116 void aout_unlock_check (unsigned i)
118 assert (aout_locks & i);
124 * Formats management (internal and external)
127 /*****************************************************************************
128 * aout_BitsPerSample : get the number of bits per sample
129 *****************************************************************************/
130 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
132 switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
162 /* For these formats the caller has to indicate the parameters
168 /*****************************************************************************
169 * aout_FormatPrepare : compute the number of bytes per frame & frame length
170 *****************************************************************************/
171 void aout_FormatPrepare( audio_sample_format_t * p_format )
173 p_format->i_channels = aout_FormatNbChannels( p_format );
174 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
175 if( p_format->i_bitspersample > 0 )
177 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
178 * aout_FormatNbChannels( p_format );
179 p_format->i_frame_length = 1;
183 /*****************************************************************************
184 * aout_FormatPrintChannels : print a channel in a human-readable form
185 *****************************************************************************/
186 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
188 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
191 case AOUT_CHAN_RIGHT:
192 case AOUT_CHAN_CENTER:
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:
201 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
203 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
204 return "Dolby/Reverse";
205 return "Stereo/Reverse";
209 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
211 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
213 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
214 return "Stereo/Mono";
215 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
216 return "Stereo/Left";
217 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
218 return "Stereo/Right";
221 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
223 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
225 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
226 | AOUT_CHAN_REARCENTER:
228 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
229 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
231 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
232 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
234 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
235 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
237 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
238 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
241 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
242 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
243 || (p_format->i_original_channels
244 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
246 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
249 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
250 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
252 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
253 return "Dual-mono/LFE";
254 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
256 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
257 return "Stereo/Left/LFE";
258 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
259 return "Stereo/Right/LFE";
261 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
263 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
266 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
267 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
269 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
270 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
272 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
273 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
275 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
276 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
278 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
279 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
281 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
282 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
283 | AOUT_CHAN_MIDDLERIGHT:
285 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
286 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
287 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
294 /*****************************************************************************
295 * aout_FormatPrint : print a format in a human-readable form
296 *****************************************************************************/
297 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
298 const audio_sample_format_t * p_format )
300 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
301 (char *)&p_format->i_format, p_format->i_rate,
302 aout_FormatPrintChannels( p_format ),
303 p_format->i_frame_length, p_format->i_bytes_per_frame );
306 /*****************************************************************************
307 * aout_FormatsPrint : print two formats in a human-readable form
308 *****************************************************************************/
309 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
310 const audio_sample_format_t * p_format1,
311 const audio_sample_format_t * p_format2 )
313 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
315 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
316 p_format1->i_rate, p_format2->i_rate,
317 aout_FormatPrintChannels( p_format1 ),
318 aout_FormatPrintChannels( p_format2 ) );
323 * FIFO management (internal) - please understand that solving race conditions
324 * is _your_ job, ie. in the audio output you should own the mixer lock
325 * before calling any of these functions.
329 /*****************************************************************************
330 * aout_FifoInit : initialize the members of a FIFO
331 *****************************************************************************/
332 void aout_FifoInit( vlc_object_t *obj, aout_fifo_t * p_fifo, uint32_t i_rate )
334 if( unlikely(i_rate == 0) )
335 msg_Err( obj, "initialising fifo with zero divider" );
337 p_fifo->p_first = NULL;
338 p_fifo->pp_last = &p_fifo->p_first;
339 date_Init( &p_fifo->end_date, i_rate, 1 );
342 /*****************************************************************************
343 * aout_FifoPush : push a packet into the FIFO
344 *****************************************************************************/
345 void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
347 *p_fifo->pp_last = p_buffer;
348 p_fifo->pp_last = &p_buffer->p_next;
349 *p_fifo->pp_last = NULL;
350 /* Enforce the continuity of the stream. */
351 if ( date_Get( &p_fifo->end_date ) )
353 p_buffer->i_pts = date_Get( &p_fifo->end_date );
354 p_buffer->i_length = date_Increment( &p_fifo->end_date,
355 p_buffer->i_nb_samples );
356 p_buffer->i_length -= p_buffer->i_pts;
360 date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
364 /*****************************************************************************
365 * aout_FifoSet : set end_date and trash all buffers (because they aren't
367 *****************************************************************************/
368 void aout_FifoSet( aout_fifo_t * p_fifo, mtime_t date )
370 aout_buffer_t * p_buffer;
372 date_Set( &p_fifo->end_date, date );
373 p_buffer = p_fifo->p_first;
374 while ( p_buffer != NULL )
376 aout_buffer_t * p_next = p_buffer->p_next;
377 aout_BufferFree( p_buffer );
380 p_fifo->p_first = NULL;
381 p_fifo->pp_last = &p_fifo->p_first;
384 /*****************************************************************************
385 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
386 *****************************************************************************/
387 void aout_FifoMoveDates( aout_fifo_t * p_fifo, mtime_t difference )
389 aout_buffer_t * p_buffer;
391 date_Move( &p_fifo->end_date, difference );
392 p_buffer = p_fifo->p_first;
393 while ( p_buffer != NULL )
395 p_buffer->i_pts += difference;
396 p_buffer = p_buffer->p_next;
400 /*****************************************************************************
401 * aout_FifoNextStart : return the current end_date
402 *****************************************************************************/
403 mtime_t aout_FifoNextStart( const aout_fifo_t *p_fifo )
405 return date_Get( &p_fifo->end_date );
408 /*****************************************************************************
409 * aout_FifoFirstDate : return the playing date of the first buffer in the
411 *****************************************************************************/
412 mtime_t aout_FifoFirstDate( const aout_fifo_t *p_fifo )
414 return (p_fifo->p_first != NULL) ? p_fifo->p_first->i_pts : 0;
417 /*****************************************************************************
418 * aout_FifoPop : get the next buffer out of the FIFO
419 *****************************************************************************/
420 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
422 aout_buffer_t *p_buffer = p_fifo->p_first;
423 if( p_buffer != NULL )
425 p_fifo->p_first = p_buffer->p_next;
426 if( p_fifo->p_first == NULL )
427 p_fifo->pp_last = &p_fifo->p_first;
432 /*****************************************************************************
433 * aout_FifoDestroy : destroy a FIFO and its buffers
434 *****************************************************************************/
435 void aout_FifoDestroy( aout_fifo_t * p_fifo )
437 aout_buffer_t * p_buffer;
439 p_buffer = p_fifo->p_first;
440 while ( p_buffer != NULL )
442 aout_buffer_t * p_next = p_buffer->p_next;
443 aout_BufferFree( p_buffer );
447 p_fifo->p_first = NULL;
448 p_fifo->pp_last = &p_fifo->p_first;
451 /*****************************************************************************
452 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
453 *****************************************************************************/
454 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
455 const uint32_t *pi_chan_order_out,
456 uint32_t i_channel_mask,
457 int i_channels, int *pi_chan_table )
459 bool b_chan_reorder = false;
462 if( i_channels > AOUT_CHAN_MAX )
465 if( pi_chan_order_in == NULL )
466 pi_chan_order_in = pi_vlc_chan_order_wg4;
467 if( pi_chan_order_out == NULL )
468 pi_chan_order_out = pi_vlc_chan_order_wg4;
470 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
472 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
474 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
476 if( i_channel_mask & pi_chan_order_out[k] ) l++;
479 pi_chan_table[j++] = l;
482 for( i = 0; i < i_channels; i++ )
484 if( pi_chan_table[i] != i ) b_chan_reorder = true;
487 return b_chan_reorder;
490 /*****************************************************************************
491 * aout_ChannelReorder :
492 *****************************************************************************/
493 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
494 int i_channels, const int *pi_chan_table,
495 int i_bits_per_sample )
497 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
500 if( i_bits_per_sample == 8 )
502 for( i = 0; i < i_buffer / i_channels; i++ )
504 for( j = 0; j < i_channels; j++ )
506 p_tmp[pi_chan_table[j]] = p_buf[j];
509 memcpy( p_buf, p_tmp, i_channels );
513 else if( i_bits_per_sample == 16 )
515 for( i = 0; i < i_buffer / i_channels / 2; i++ )
517 for( j = 0; j < i_channels; j++ )
519 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
520 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
523 memcpy( p_buf, p_tmp, 2 * i_channels );
524 p_buf += 2 * i_channels;
527 else if( i_bits_per_sample == 24 )
529 for( i = 0; i < i_buffer / i_channels / 3; i++ )
531 for( j = 0; j < i_channels; j++ )
533 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
534 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
535 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
538 memcpy( p_buf, p_tmp, 3 * i_channels );
539 p_buf += 3 * i_channels;
542 else if( i_bits_per_sample == 32 )
544 for( i = 0; i < i_buffer / i_channels / 4; i++ )
546 for( j = 0; j < i_channels; j++ )
548 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
549 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
550 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
551 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
554 memcpy( p_buf, p_tmp, 4 * i_channels );
555 p_buf += 4 * i_channels;
560 /*****************************************************************************
561 * aout_ChannelExtract:
562 *****************************************************************************/
563 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
564 const uint8_t *pi_src, int i_src_channels,
566 const int *pi_selection, int i_bytes )
568 for( int i = 0; i < i_sample_count; i++ )
570 for( int j = 0; j < i_dst_channels; j++ )
571 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
572 pi_dst += i_dst_channels * i_bytes;
573 pi_src += i_src_channels * i_bytes;
577 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
578 const void *p_src, int i_src_channels,
579 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
581 /* It does not work in place */
582 assert( p_dst != p_src );
584 /* Force the compiler to inline for the specific cases so it can optimize */
585 if( i_bits_per_sample == 8 )
586 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
587 else if( i_bits_per_sample == 16 )
588 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
589 else if( i_bits_per_sample == 24 )
590 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
591 else if( i_bits_per_sample == 32 )
592 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
593 else if( i_bits_per_sample == 64 )
594 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
597 bool aout_CheckChannelExtraction( int *pi_selection,
598 uint32_t *pi_layout, int *pi_channels,
599 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
600 const uint32_t *pi_order_src, int i_channels )
602 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
603 uint32_t i_layout = 0;
605 int pi_index[AOUT_CHAN_MAX];
609 pi_order_dst = pi_vlc_chan_order_wg4;
611 /* Detect special dual mono case */
612 if( i_channels == 2 &&
613 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
615 i_layout |= AOUT_CHAN_DUALMONO;
616 pi_order_src = pi_order_dual_mono;
620 for( int i = 0; i < i_channels; i++ )
622 /* Ignore unknown or duplicated channels or not present in output */
623 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
626 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
628 if( pi_order_dst[j] == pi_order_src[i] )
630 assert( i_out < AOUT_CHAN_MAX );
631 pi_index[i_out++] = i;
632 i_layout |= pi_order_src[i];
639 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
641 for( int k = 0; k < i_out; k++ )
643 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
645 pi_selection[j++] = pi_index[k];
651 *pi_layout = i_layout;
652 *pi_channels = i_out;
654 for( int i = 0; i < i_out; i++ )
656 if( pi_selection[i] != i )
659 return i_out == i_channels;
662 /* Return the order in which filters should be inserted */
663 static int FilterOrder( const char *psz_name )
665 static const struct {
666 const char *psz_name;
672 for( int i = 0; filter[i].psz_name; i++ )
674 if( !strcmp( filter[i].psz_name, psz_name ) )
675 return filter[i].i_order;
680 /* This function will add or remove a a module from a string list (colon
681 * separated). It will return true if there is a modification
682 * In case p_aout is NULL, we will use configuration instead of variable */
683 bool aout_ChangeFilterString( vlc_object_t *p_obj, aout_instance_t *p_aout,
684 const char *psz_variable,
685 const char *psz_name, bool b_add )
687 if( *psz_name == '\0' )
693 psz_list = var_GetString( p_aout, psz_variable );
697 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
698 var_Destroy( p_obj->p_libvlc, psz_variable );
701 /* Split the string into an array of filters */
703 for( char *p = psz_list; p && *p; p++ )
704 i_count += *p == ':';
707 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
713 bool b_present = false;
715 for( char *p = psz_list; p && *p; )
717 char *psz_end = strchr(p, ':');
721 psz_end = p + strlen(p);
724 b_present |= !strcmp( p, psz_name );
725 ppsz_filter[i_count++] = p;
729 if( b_present == b_add )
738 int i_order = FilterOrder( psz_name );
740 for( i = 0; i < i_count; i++ )
742 if( FilterOrder( ppsz_filter[i] ) > i_order )
746 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
747 ppsz_filter[i] = psz_name;
752 for( int i = 0; i < i_count; i++ )
754 if( !strcmp( ppsz_filter[i], psz_name ) )
759 for( int i = 0; i < i_count; i++ )
760 i_length += 1 + strlen( ppsz_filter[i] );
762 char *psz_new = malloc( i_length + 1 );
764 for( int i = 0; i < i_count; i++ )
766 if( *ppsz_filter[i] == '\0' )
769 strcat( psz_new, ":" );
770 strcat( psz_new, ppsz_filter[i] );
776 var_SetString( p_aout, psz_variable, psz_new );
778 config_PutPsz( p_obj, psz_variable, psz_new );