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 *aout = vlc_custom_create (p_parent,
53 sizeof (aout_instance_t),
55 if (unlikely(aout == NULL))
58 aout_owner_t *owner = aout_owner (aout);
60 vlc_mutex_init (&owner->lock);
63 vlc_mutex_init (&owner->volume.lock);
64 owner->volume.multiplier = 1.0;
65 owner->volume.mixer = NULL;
67 aout_VolumeNoneInit (aout);
68 vlc_object_set_destructor (aout, aout_Destructor);
69 var_Create (aout, "intf-change", VLC_VAR_VOID);
74 /*****************************************************************************
75 * aout_Destructor: destroy aout structure
76 *****************************************************************************/
77 static void aout_Destructor (vlc_object_t *obj)
79 audio_output_t *aout = (audio_output_t *)obj;
80 aout_owner_t *owner = aout_owner (aout);
82 vlc_mutex_destroy (&owner->volume.lock);
83 vlc_mutex_destroy (&owner->lock);
88 static __thread unsigned aout_locks = 0;
90 void aout_lock_check (unsigned i)
99 allowed = VOLUME_LOCK;
105 if (aout_locks & ~allowed)
107 fprintf (stderr, "Illegal audio lock transition (%x -> %x)\n",
108 aout_locks, aout_locks|i);
115 void aout_unlock_check (unsigned i)
117 assert (aout_locks & i);
123 * Formats management (internal and external)
126 /*****************************************************************************
127 * aout_BitsPerSample : get the number of bits per sample
128 *****************************************************************************/
129 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
131 switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
161 /* For these formats the caller has to indicate the parameters
167 /*****************************************************************************
168 * aout_FormatPrepare : compute the number of bytes per frame & frame length
169 *****************************************************************************/
170 void aout_FormatPrepare( audio_sample_format_t * p_format )
172 p_format->i_channels = aout_FormatNbChannels( p_format );
173 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
174 if( p_format->i_bitspersample > 0 )
176 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
177 * aout_FormatNbChannels( p_format );
178 p_format->i_frame_length = 1;
182 /*****************************************************************************
183 * aout_FormatPrintChannels : print a channel in a human-readable form
184 *****************************************************************************/
185 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
187 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
190 case AOUT_CHAN_RIGHT:
191 case AOUT_CHAN_CENTER:
192 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
193 || (p_format->i_original_channels
194 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
196 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
199 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
200 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
202 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
203 return "Dolby/Reverse";
204 return "Stereo/Reverse";
208 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
210 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
212 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
213 return "Stereo/Mono";
214 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
215 return "Stereo/Left";
216 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
217 return "Stereo/Right";
220 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
222 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
224 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
225 | AOUT_CHAN_REARCENTER:
227 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
228 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
230 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
231 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
233 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
234 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
236 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
237 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
240 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
241 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
242 || (p_format->i_original_channels
243 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
245 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
248 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
249 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
251 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
252 return "Dual-mono/LFE";
253 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
255 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
256 return "Stereo/Left/LFE";
257 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
258 return "Stereo/Right/LFE";
260 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
262 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
265 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
266 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
268 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
269 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
271 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
272 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
274 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
275 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
277 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
278 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
280 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
281 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
282 | AOUT_CHAN_MIDDLERIGHT:
284 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
285 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
286 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
293 #undef aout_FormatPrint
295 * Prints an audio sample format in a human-readable form.
297 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
298 const audio_sample_format_t *p_format )
300 msg_Dbg( obj, "%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 #undef aout_FormatsPrint
308 * Prints two formats in a human-readable form
310 void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
311 const audio_sample_format_t * p_format1,
312 const audio_sample_format_t * p_format2 )
314 msg_Dbg( obj, "%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_FifoPop : get the next buffer out of the FIFO
403 *****************************************************************************/
404 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
406 aout_buffer_t *p_buffer = p_fifo->p_first;
407 if( p_buffer != NULL )
409 p_fifo->p_first = p_buffer->p_next;
410 if( p_fifo->p_first == NULL )
411 p_fifo->pp_last = &p_fifo->p_first;
416 /*****************************************************************************
417 * aout_FifoDestroy : destroy a FIFO and its buffers
418 *****************************************************************************/
419 void aout_FifoDestroy( aout_fifo_t * p_fifo )
421 aout_buffer_t * p_buffer;
423 p_buffer = p_fifo->p_first;
424 while ( p_buffer != NULL )
426 aout_buffer_t * p_next = p_buffer->p_next;
427 aout_BufferFree( p_buffer );
431 p_fifo->p_first = NULL;
432 p_fifo->pp_last = &p_fifo->p_first;
435 /*****************************************************************************
436 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
437 *****************************************************************************/
438 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
439 const uint32_t *pi_chan_order_out,
440 uint32_t i_channel_mask,
441 int i_channels, int *pi_chan_table )
443 bool b_chan_reorder = false;
446 if( i_channels > AOUT_CHAN_MAX )
449 if( pi_chan_order_in == NULL )
450 pi_chan_order_in = pi_vlc_chan_order_wg4;
451 if( pi_chan_order_out == NULL )
452 pi_chan_order_out = pi_vlc_chan_order_wg4;
454 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
456 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
458 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
460 if( i_channel_mask & pi_chan_order_out[k] ) l++;
463 pi_chan_table[j++] = l;
466 for( i = 0; i < i_channels; i++ )
468 if( pi_chan_table[i] != i ) b_chan_reorder = true;
471 return b_chan_reorder;
474 /*****************************************************************************
475 * aout_ChannelReorder :
476 *****************************************************************************/
477 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
478 int i_channels, const int *pi_chan_table,
479 int i_bits_per_sample )
481 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
484 if( i_bits_per_sample == 8 )
486 for( i = 0; i < i_buffer / i_channels; i++ )
488 for( j = 0; j < i_channels; j++ )
490 p_tmp[pi_chan_table[j]] = p_buf[j];
493 memcpy( p_buf, p_tmp, i_channels );
497 else if( i_bits_per_sample == 16 )
499 for( i = 0; i < i_buffer / i_channels / 2; i++ )
501 for( j = 0; j < i_channels; j++ )
503 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
504 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
507 memcpy( p_buf, p_tmp, 2 * i_channels );
508 p_buf += 2 * i_channels;
511 else if( i_bits_per_sample == 24 )
513 for( i = 0; i < i_buffer / i_channels / 3; i++ )
515 for( j = 0; j < i_channels; j++ )
517 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
518 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
519 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
522 memcpy( p_buf, p_tmp, 3 * i_channels );
523 p_buf += 3 * i_channels;
526 else if( i_bits_per_sample == 32 )
528 for( i = 0; i < i_buffer / i_channels / 4; i++ )
530 for( j = 0; j < i_channels; j++ )
532 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
533 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
534 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
535 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
538 memcpy( p_buf, p_tmp, 4 * i_channels );
539 p_buf += 4 * i_channels;
544 /*****************************************************************************
545 * aout_ChannelExtract:
546 *****************************************************************************/
547 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
548 const uint8_t *pi_src, int i_src_channels,
550 const int *pi_selection, int i_bytes )
552 for( int i = 0; i < i_sample_count; i++ )
554 for( int j = 0; j < i_dst_channels; j++ )
555 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
556 pi_dst += i_dst_channels * i_bytes;
557 pi_src += i_src_channels * i_bytes;
561 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
562 const void *p_src, int i_src_channels,
563 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
565 /* It does not work in place */
566 assert( p_dst != p_src );
568 /* Force the compiler to inline for the specific cases so it can optimize */
569 if( i_bits_per_sample == 8 )
570 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
571 else if( i_bits_per_sample == 16 )
572 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
573 else if( i_bits_per_sample == 24 )
574 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
575 else if( i_bits_per_sample == 32 )
576 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
577 else if( i_bits_per_sample == 64 )
578 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
581 bool aout_CheckChannelExtraction( int *pi_selection,
582 uint32_t *pi_layout, int *pi_channels,
583 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
584 const uint32_t *pi_order_src, int i_channels )
586 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
587 uint32_t i_layout = 0;
589 int pi_index[AOUT_CHAN_MAX];
593 pi_order_dst = pi_vlc_chan_order_wg4;
595 /* Detect special dual mono case */
596 if( i_channels == 2 &&
597 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
599 i_layout |= AOUT_CHAN_DUALMONO;
600 pi_order_src = pi_order_dual_mono;
604 for( int i = 0; i < i_channels; i++ )
606 /* Ignore unknown or duplicated channels or not present in output */
607 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
610 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
612 if( pi_order_dst[j] == pi_order_src[i] )
614 assert( i_out < AOUT_CHAN_MAX );
615 pi_index[i_out++] = i;
616 i_layout |= pi_order_src[i];
623 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
625 for( int k = 0; k < i_out; k++ )
627 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
629 pi_selection[j++] = pi_index[k];
635 *pi_layout = i_layout;
636 *pi_channels = i_out;
638 for( int i = 0; i < i_out; i++ )
640 if( pi_selection[i] != i )
643 return i_out == i_channels;
646 /* Return the order in which filters should be inserted */
647 static int FilterOrder( const char *psz_name )
649 static const struct {
650 const char *psz_name;
656 for( int i = 0; filter[i].psz_name; i++ )
658 if( !strcmp( filter[i].psz_name, psz_name ) )
659 return filter[i].i_order;
664 /* This function will add or remove a a module from a string list (colon
665 * separated). It will return true if there is a modification
666 * In case p_aout is NULL, we will use configuration instead of variable */
667 bool aout_ChangeFilterString( vlc_object_t *p_obj, audio_output_t *p_aout,
668 const char *psz_variable,
669 const char *psz_name, bool b_add )
671 if( *psz_name == '\0' )
677 psz_list = var_GetString( p_aout, psz_variable );
681 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
682 var_Destroy( p_obj->p_libvlc, psz_variable );
685 /* Split the string into an array of filters */
687 for( char *p = psz_list; p && *p; p++ )
688 i_count += *p == ':';
691 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
697 bool b_present = false;
699 for( char *p = psz_list; p && *p; )
701 char *psz_end = strchr(p, ':');
705 psz_end = p + strlen(p);
708 b_present |= !strcmp( p, psz_name );
709 ppsz_filter[i_count++] = p;
713 if( b_present == b_add )
722 int i_order = FilterOrder( psz_name );
724 for( i = 0; i < i_count; i++ )
726 if( FilterOrder( ppsz_filter[i] ) > i_order )
730 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
731 ppsz_filter[i] = psz_name;
736 for( int i = 0; i < i_count; i++ )
738 if( !strcmp( ppsz_filter[i], psz_name ) )
743 for( int i = 0; i < i_count; i++ )
744 i_length += 1 + strlen( ppsz_filter[i] );
746 char *psz_new = malloc( i_length + 1 );
748 for( int i = 0; i < i_count; i++ )
750 if( *ppsz_filter[i] == '\0' )
753 strcat( psz_new, ":" );
754 strcat( psz_new, ppsz_filter[i] );
760 var_SetString( p_aout, psz_variable, psz_new );
762 config_PutPsz( p_obj, psz_variable, psz_new );