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 );
47 /*****************************************************************************
48 * aout_New: initialize aout structure
49 *****************************************************************************/
50 audio_output_t *aout_New( vlc_object_t * p_parent )
52 audio_output_t * p_aout;
54 /* Allocate descriptor. */
55 p_aout = vlc_custom_create( p_parent, sizeof( *p_aout ), "audio output" );
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;
67 p_aout->b_starving = true;
68 p_aout->module = NULL;
69 aout_VolumeNoneInit( p_aout );
71 var_Create( p_aout, "intf-change", VLC_VAR_VOID );
73 vlc_object_set_destructor( p_aout, aout_Destructor );
78 /*****************************************************************************
79 * aout_Destructor: destroy aout structure
80 *****************************************************************************/
81 static void aout_Destructor( vlc_object_t * p_this )
83 audio_output_t * p_aout = (audio_output_t *)p_this;
84 vlc_mutex_destroy( &p_aout->volume_lock );
85 vlc_mutex_destroy( &p_aout->lock );
90 static __thread unsigned aout_locks = 0;
92 void aout_lock_check (unsigned i)
101 allowed = VOLUME_LOCK;
107 if (aout_locks & ~allowed)
109 fprintf (stderr, "Illegal audio lock transition (%x -> %x)\n",
110 aout_locks, aout_locks|i);
117 void aout_unlock_check (unsigned i)
119 assert (aout_locks & i);
125 * Formats management (internal and external)
128 /*****************************************************************************
129 * aout_BitsPerSample : get the number of bits per sample
130 *****************************************************************************/
131 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
133 switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
163 /* For these formats the caller has to indicate the parameters
169 /*****************************************************************************
170 * aout_FormatPrepare : compute the number of bytes per frame & frame length
171 *****************************************************************************/
172 void aout_FormatPrepare( audio_sample_format_t * p_format )
174 p_format->i_channels = aout_FormatNbChannels( p_format );
175 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
176 if( p_format->i_bitspersample > 0 )
178 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
179 * aout_FormatNbChannels( p_format );
180 p_format->i_frame_length = 1;
184 /*****************************************************************************
185 * aout_FormatPrintChannels : print a channel in a human-readable form
186 *****************************************************************************/
187 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
189 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
192 case AOUT_CHAN_RIGHT:
193 case AOUT_CHAN_CENTER:
194 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
195 || (p_format->i_original_channels
196 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
198 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
201 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
202 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
204 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
205 return "Dolby/Reverse";
206 return "Stereo/Reverse";
210 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
212 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
214 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
215 return "Stereo/Mono";
216 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
217 return "Stereo/Left";
218 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
219 return "Stereo/Right";
222 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
224 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
226 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
227 | AOUT_CHAN_REARCENTER:
229 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
230 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
232 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
233 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
235 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
236 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
238 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
239 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
242 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
243 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
244 || (p_format->i_original_channels
245 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
247 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
250 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
251 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
253 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
254 return "Dual-mono/LFE";
255 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
257 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
258 return "Stereo/Left/LFE";
259 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
260 return "Stereo/Right/LFE";
262 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
264 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
267 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
268 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
270 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
271 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
273 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
274 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
276 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
277 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
279 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
280 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
282 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
283 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
284 | AOUT_CHAN_MIDDLERIGHT:
286 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
287 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
288 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
295 /*****************************************************************************
296 * aout_FormatPrint : print a format in a human-readable form
297 *****************************************************************************/
298 void aout_FormatPrint( audio_output_t * p_aout, const char * psz_text,
299 const audio_sample_format_t * p_format )
301 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
302 (char *)&p_format->i_format, p_format->i_rate,
303 aout_FormatPrintChannels( p_format ),
304 p_format->i_frame_length, p_format->i_bytes_per_frame );
307 /*****************************************************************************
308 * aout_FormatsPrint : print two formats in a human-readable form
309 *****************************************************************************/
310 void aout_FormatsPrint( audio_output_t * p_aout, const char * psz_text,
311 const audio_sample_format_t * p_format1,
312 const audio_sample_format_t * p_format2 )
314 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
316 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
317 p_format1->i_rate, p_format2->i_rate,
318 aout_FormatPrintChannels( p_format1 ),
319 aout_FormatPrintChannels( p_format2 ) );
324 * FIFO management (internal) - please understand that solving race conditions
325 * is _your_ job, ie. in the audio output you should own the mixer lock
326 * before calling any of these functions.
330 /*****************************************************************************
331 * aout_FifoInit : initialize the members of a FIFO
332 *****************************************************************************/
333 void aout_FifoInit( vlc_object_t *obj, aout_fifo_t * p_fifo, uint32_t i_rate )
335 if( unlikely(i_rate == 0) )
336 msg_Err( obj, "initialising fifo with zero divider" );
338 p_fifo->p_first = NULL;
339 p_fifo->pp_last = &p_fifo->p_first;
340 date_Init( &p_fifo->end_date, i_rate, 1 );
341 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
344 /*****************************************************************************
345 * aout_FifoPush : push a packet into the FIFO
346 *****************************************************************************/
347 void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
349 *p_fifo->pp_last = p_buffer;
350 p_fifo->pp_last = &p_buffer->p_next;
351 *p_fifo->pp_last = NULL;
352 /* Enforce the continuity of the stream. */
353 if( date_Get( &p_fifo->end_date ) != VLC_TS_INVALID )
355 p_buffer->i_pts = date_Get( &p_fifo->end_date );
356 p_buffer->i_length = date_Increment( &p_fifo->end_date,
357 p_buffer->i_nb_samples );
358 p_buffer->i_length -= p_buffer->i_pts;
362 date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
366 /*****************************************************************************
367 * aout_FifoReset: trash all buffers
368 *****************************************************************************/
369 void aout_FifoReset( aout_fifo_t * p_fifo )
371 aout_buffer_t * p_buffer;
373 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
374 p_buffer = p_fifo->p_first;
375 while ( p_buffer != NULL )
377 aout_buffer_t * p_next = p_buffer->p_next;
378 aout_BufferFree( p_buffer );
381 p_fifo->p_first = NULL;
382 p_fifo->pp_last = &p_fifo->p_first;
385 /*****************************************************************************
386 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
387 *****************************************************************************/
388 void aout_FifoMoveDates( aout_fifo_t *fifo, mtime_t difference )
390 if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
392 assert( fifo->p_first == NULL );
396 date_Move( &fifo->end_date, difference );
397 for( block_t *block = fifo->p_first; block != NULL; block = block->p_next )
398 block->i_pts += difference;
401 /*****************************************************************************
402 * aout_FifoNextStart : return the current end_date
403 *****************************************************************************/
404 mtime_t aout_FifoNextStart( const aout_fifo_t *p_fifo )
406 return date_Get( &p_fifo->end_date );
409 /*****************************************************************************
410 * aout_FifoFirstDate : return the playing date of the first buffer in the
412 *****************************************************************************/
413 mtime_t aout_FifoFirstDate( const aout_fifo_t *fifo )
415 block_t *first = fifo->p_first;
416 return (first != NULL) ? first->i_pts : VLC_TS_INVALID;
419 /*****************************************************************************
420 * aout_FifoPop : get the next buffer out of the FIFO
421 *****************************************************************************/
422 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
424 aout_buffer_t *p_buffer = p_fifo->p_first;
425 if( p_buffer != NULL )
427 p_fifo->p_first = p_buffer->p_next;
428 if( p_fifo->p_first == NULL )
429 p_fifo->pp_last = &p_fifo->p_first;
434 /*****************************************************************************
435 * aout_FifoDestroy : destroy a FIFO and its buffers
436 *****************************************************************************/
437 void aout_FifoDestroy( aout_fifo_t * p_fifo )
439 aout_buffer_t * p_buffer;
441 p_buffer = p_fifo->p_first;
442 while ( p_buffer != NULL )
444 aout_buffer_t * p_next = p_buffer->p_next;
445 aout_BufferFree( p_buffer );
449 p_fifo->p_first = NULL;
450 p_fifo->pp_last = &p_fifo->p_first;
453 /*****************************************************************************
454 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
455 *****************************************************************************/
456 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
457 const uint32_t *pi_chan_order_out,
458 uint32_t i_channel_mask,
459 int i_channels, int *pi_chan_table )
461 bool b_chan_reorder = false;
464 if( i_channels > AOUT_CHAN_MAX )
467 if( pi_chan_order_in == NULL )
468 pi_chan_order_in = pi_vlc_chan_order_wg4;
469 if( pi_chan_order_out == NULL )
470 pi_chan_order_out = pi_vlc_chan_order_wg4;
472 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
474 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
476 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
478 if( i_channel_mask & pi_chan_order_out[k] ) l++;
481 pi_chan_table[j++] = l;
484 for( i = 0; i < i_channels; i++ )
486 if( pi_chan_table[i] != i ) b_chan_reorder = true;
489 return b_chan_reorder;
492 /*****************************************************************************
493 * aout_ChannelReorder :
494 *****************************************************************************/
495 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
496 int i_channels, const int *pi_chan_table,
497 int i_bits_per_sample )
499 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
502 if( i_bits_per_sample == 8 )
504 for( i = 0; i < i_buffer / i_channels; i++ )
506 for( j = 0; j < i_channels; j++ )
508 p_tmp[pi_chan_table[j]] = p_buf[j];
511 memcpy( p_buf, p_tmp, i_channels );
515 else if( i_bits_per_sample == 16 )
517 for( i = 0; i < i_buffer / i_channels / 2; i++ )
519 for( j = 0; j < i_channels; j++ )
521 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
522 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
525 memcpy( p_buf, p_tmp, 2 * i_channels );
526 p_buf += 2 * i_channels;
529 else if( i_bits_per_sample == 24 )
531 for( i = 0; i < i_buffer / i_channels / 3; i++ )
533 for( j = 0; j < i_channels; j++ )
535 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
536 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
537 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
540 memcpy( p_buf, p_tmp, 3 * i_channels );
541 p_buf += 3 * i_channels;
544 else if( i_bits_per_sample == 32 )
546 for( i = 0; i < i_buffer / i_channels / 4; i++ )
548 for( j = 0; j < i_channels; j++ )
550 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
551 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
552 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
553 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
556 memcpy( p_buf, p_tmp, 4 * i_channels );
557 p_buf += 4 * i_channels;
562 /*****************************************************************************
563 * aout_ChannelExtract:
564 *****************************************************************************/
565 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
566 const uint8_t *pi_src, int i_src_channels,
568 const int *pi_selection, int i_bytes )
570 for( int i = 0; i < i_sample_count; i++ )
572 for( int j = 0; j < i_dst_channels; j++ )
573 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
574 pi_dst += i_dst_channels * i_bytes;
575 pi_src += i_src_channels * i_bytes;
579 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
580 const void *p_src, int i_src_channels,
581 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
583 /* It does not work in place */
584 assert( p_dst != p_src );
586 /* Force the compiler to inline for the specific cases so it can optimize */
587 if( i_bits_per_sample == 8 )
588 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
589 else if( i_bits_per_sample == 16 )
590 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
591 else if( i_bits_per_sample == 24 )
592 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
593 else if( i_bits_per_sample == 32 )
594 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
595 else if( i_bits_per_sample == 64 )
596 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
599 bool aout_CheckChannelExtraction( int *pi_selection,
600 uint32_t *pi_layout, int *pi_channels,
601 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
602 const uint32_t *pi_order_src, int i_channels )
604 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
605 uint32_t i_layout = 0;
607 int pi_index[AOUT_CHAN_MAX];
611 pi_order_dst = pi_vlc_chan_order_wg4;
613 /* Detect special dual mono case */
614 if( i_channels == 2 &&
615 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
617 i_layout |= AOUT_CHAN_DUALMONO;
618 pi_order_src = pi_order_dual_mono;
622 for( int i = 0; i < i_channels; i++ )
624 /* Ignore unknown or duplicated channels or not present in output */
625 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
628 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
630 if( pi_order_dst[j] == pi_order_src[i] )
632 assert( i_out < AOUT_CHAN_MAX );
633 pi_index[i_out++] = i;
634 i_layout |= pi_order_src[i];
641 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
643 for( int k = 0; k < i_out; k++ )
645 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
647 pi_selection[j++] = pi_index[k];
653 *pi_layout = i_layout;
654 *pi_channels = i_out;
656 for( int i = 0; i < i_out; i++ )
658 if( pi_selection[i] != i )
661 return i_out == i_channels;
664 /* Return the order in which filters should be inserted */
665 static int FilterOrder( const char *psz_name )
667 static const struct {
668 const char *psz_name;
674 for( int i = 0; filter[i].psz_name; i++ )
676 if( !strcmp( filter[i].psz_name, psz_name ) )
677 return filter[i].i_order;
682 /* This function will add or remove a a module from a string list (colon
683 * separated). It will return true if there is a modification
684 * In case p_aout is NULL, we will use configuration instead of variable */
685 bool aout_ChangeFilterString( vlc_object_t *p_obj, audio_output_t *p_aout,
686 const char *psz_variable,
687 const char *psz_name, bool b_add )
689 if( *psz_name == '\0' )
695 psz_list = var_GetString( p_aout, psz_variable );
699 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
700 var_Destroy( p_obj->p_libvlc, psz_variable );
703 /* Split the string into an array of filters */
705 for( char *p = psz_list; p && *p; p++ )
706 i_count += *p == ':';
709 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
715 bool b_present = false;
717 for( char *p = psz_list; p && *p; )
719 char *psz_end = strchr(p, ':');
723 psz_end = p + strlen(p);
726 b_present |= !strcmp( p, psz_name );
727 ppsz_filter[i_count++] = p;
731 if( b_present == b_add )
740 int i_order = FilterOrder( psz_name );
742 for( i = 0; i < i_count; i++ )
744 if( FilterOrder( ppsz_filter[i] ) > i_order )
748 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
749 ppsz_filter[i] = psz_name;
754 for( int i = 0; i < i_count; i++ )
756 if( !strcmp( ppsz_filter[i], psz_name ) )
761 for( int i = 0; i < i_count; i++ )
762 i_length += 1 + strlen( ppsz_filter[i] );
764 char *psz_new = malloc( i_length + 1 );
766 for( int i = 0; i < i_count; i++ )
768 if( *ppsz_filter[i] == '\0' )
771 strcat( psz_new, ":" );
772 strcat( psz_new, ppsz_filter[i] );
778 var_SetString( p_aout, psz_variable, psz_new );
780 config_PutPsz( p_obj, psz_variable, psz_new );