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 /*****************************************************************************
294 * aout_FormatPrint : print a format in a human-readable form
295 *****************************************************************************/
296 void aout_FormatPrint( audio_output_t * p_aout, const char * psz_text,
297 const audio_sample_format_t * p_format )
299 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
300 (char *)&p_format->i_format, p_format->i_rate,
301 aout_FormatPrintChannels( p_format ),
302 p_format->i_frame_length, p_format->i_bytes_per_frame );
305 /*****************************************************************************
306 * aout_FormatsPrint : print two formats in a human-readable form
307 *****************************************************************************/
308 void aout_FormatsPrint( audio_output_t * p_aout, const char * psz_text,
309 const audio_sample_format_t * p_format1,
310 const audio_sample_format_t * p_format2 )
312 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
314 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
315 p_format1->i_rate, p_format2->i_rate,
316 aout_FormatPrintChannels( p_format1 ),
317 aout_FormatPrintChannels( p_format2 ) );
322 * FIFO management (internal) - please understand that solving race conditions
323 * is _your_ job, ie. in the audio output you should own the mixer lock
324 * before calling any of these functions.
328 /*****************************************************************************
329 * aout_FifoInit : initialize the members of a FIFO
330 *****************************************************************************/
331 void aout_FifoInit( vlc_object_t *obj, aout_fifo_t * p_fifo, uint32_t i_rate )
333 if( unlikely(i_rate == 0) )
334 msg_Err( obj, "initialising fifo with zero divider" );
336 p_fifo->p_first = NULL;
337 p_fifo->pp_last = &p_fifo->p_first;
338 date_Init( &p_fifo->end_date, i_rate, 1 );
339 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
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 ) != VLC_TS_INVALID )
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_FifoReset: trash all buffers
366 *****************************************************************************/
367 void aout_FifoReset( aout_fifo_t * p_fifo )
369 aout_buffer_t * p_buffer;
371 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
372 p_buffer = p_fifo->p_first;
373 while ( p_buffer != NULL )
375 aout_buffer_t * p_next = p_buffer->p_next;
376 aout_BufferFree( p_buffer );
379 p_fifo->p_first = NULL;
380 p_fifo->pp_last = &p_fifo->p_first;
383 /*****************************************************************************
384 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
385 *****************************************************************************/
386 void aout_FifoMoveDates( aout_fifo_t *fifo, mtime_t difference )
388 if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
390 assert( fifo->p_first == NULL );
394 date_Move( &fifo->end_date, difference );
395 for( block_t *block = fifo->p_first; block != NULL; block = block->p_next )
396 block->i_pts += difference;
399 /*****************************************************************************
400 * aout_FifoPop : get the next buffer out of the FIFO
401 *****************************************************************************/
402 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
404 aout_buffer_t *p_buffer = p_fifo->p_first;
405 if( p_buffer != NULL )
407 p_fifo->p_first = p_buffer->p_next;
408 if( p_fifo->p_first == NULL )
409 p_fifo->pp_last = &p_fifo->p_first;
414 /*****************************************************************************
415 * aout_FifoDestroy : destroy a FIFO and its buffers
416 *****************************************************************************/
417 void aout_FifoDestroy( aout_fifo_t * p_fifo )
419 aout_buffer_t * p_buffer;
421 p_buffer = p_fifo->p_first;
422 while ( p_buffer != NULL )
424 aout_buffer_t * p_next = p_buffer->p_next;
425 aout_BufferFree( p_buffer );
429 p_fifo->p_first = NULL;
430 p_fifo->pp_last = &p_fifo->p_first;
433 /*****************************************************************************
434 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
435 *****************************************************************************/
436 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
437 const uint32_t *pi_chan_order_out,
438 uint32_t i_channel_mask,
439 int i_channels, int *pi_chan_table )
441 bool b_chan_reorder = false;
444 if( i_channels > AOUT_CHAN_MAX )
447 if( pi_chan_order_in == NULL )
448 pi_chan_order_in = pi_vlc_chan_order_wg4;
449 if( pi_chan_order_out == NULL )
450 pi_chan_order_out = pi_vlc_chan_order_wg4;
452 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
454 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
456 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
458 if( i_channel_mask & pi_chan_order_out[k] ) l++;
461 pi_chan_table[j++] = l;
464 for( i = 0; i < i_channels; i++ )
466 if( pi_chan_table[i] != i ) b_chan_reorder = true;
469 return b_chan_reorder;
472 /*****************************************************************************
473 * aout_ChannelReorder :
474 *****************************************************************************/
475 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
476 int i_channels, const int *pi_chan_table,
477 int i_bits_per_sample )
479 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
482 if( i_bits_per_sample == 8 )
484 for( i = 0; i < i_buffer / i_channels; i++ )
486 for( j = 0; j < i_channels; j++ )
488 p_tmp[pi_chan_table[j]] = p_buf[j];
491 memcpy( p_buf, p_tmp, i_channels );
495 else if( i_bits_per_sample == 16 )
497 for( i = 0; i < i_buffer / i_channels / 2; i++ )
499 for( j = 0; j < i_channels; j++ )
501 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
502 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
505 memcpy( p_buf, p_tmp, 2 * i_channels );
506 p_buf += 2 * i_channels;
509 else if( i_bits_per_sample == 24 )
511 for( i = 0; i < i_buffer / i_channels / 3; i++ )
513 for( j = 0; j < i_channels; j++ )
515 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
516 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
517 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
520 memcpy( p_buf, p_tmp, 3 * i_channels );
521 p_buf += 3 * i_channels;
524 else if( i_bits_per_sample == 32 )
526 for( i = 0; i < i_buffer / i_channels / 4; i++ )
528 for( j = 0; j < i_channels; j++ )
530 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
531 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
532 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
533 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
536 memcpy( p_buf, p_tmp, 4 * i_channels );
537 p_buf += 4 * i_channels;
542 /*****************************************************************************
543 * aout_ChannelExtract:
544 *****************************************************************************/
545 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
546 const uint8_t *pi_src, int i_src_channels,
548 const int *pi_selection, int i_bytes )
550 for( int i = 0; i < i_sample_count; i++ )
552 for( int j = 0; j < i_dst_channels; j++ )
553 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
554 pi_dst += i_dst_channels * i_bytes;
555 pi_src += i_src_channels * i_bytes;
559 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
560 const void *p_src, int i_src_channels,
561 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
563 /* It does not work in place */
564 assert( p_dst != p_src );
566 /* Force the compiler to inline for the specific cases so it can optimize */
567 if( i_bits_per_sample == 8 )
568 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
569 else if( i_bits_per_sample == 16 )
570 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
571 else if( i_bits_per_sample == 24 )
572 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
573 else if( i_bits_per_sample == 32 )
574 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
575 else if( i_bits_per_sample == 64 )
576 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
579 bool aout_CheckChannelExtraction( int *pi_selection,
580 uint32_t *pi_layout, int *pi_channels,
581 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
582 const uint32_t *pi_order_src, int i_channels )
584 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
585 uint32_t i_layout = 0;
587 int pi_index[AOUT_CHAN_MAX];
591 pi_order_dst = pi_vlc_chan_order_wg4;
593 /* Detect special dual mono case */
594 if( i_channels == 2 &&
595 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
597 i_layout |= AOUT_CHAN_DUALMONO;
598 pi_order_src = pi_order_dual_mono;
602 for( int i = 0; i < i_channels; i++ )
604 /* Ignore unknown or duplicated channels or not present in output */
605 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
608 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
610 if( pi_order_dst[j] == pi_order_src[i] )
612 assert( i_out < AOUT_CHAN_MAX );
613 pi_index[i_out++] = i;
614 i_layout |= pi_order_src[i];
621 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
623 for( int k = 0; k < i_out; k++ )
625 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
627 pi_selection[j++] = pi_index[k];
633 *pi_layout = i_layout;
634 *pi_channels = i_out;
636 for( int i = 0; i < i_out; i++ )
638 if( pi_selection[i] != i )
641 return i_out == i_channels;
644 /* Return the order in which filters should be inserted */
645 static int FilterOrder( const char *psz_name )
647 static const struct {
648 const char *psz_name;
654 for( int i = 0; filter[i].psz_name; i++ )
656 if( !strcmp( filter[i].psz_name, psz_name ) )
657 return filter[i].i_order;
662 /* This function will add or remove a a module from a string list (colon
663 * separated). It will return true if there is a modification
664 * In case p_aout is NULL, we will use configuration instead of variable */
665 bool aout_ChangeFilterString( vlc_object_t *p_obj, audio_output_t *p_aout,
666 const char *psz_variable,
667 const char *psz_name, bool b_add )
669 if( *psz_name == '\0' )
675 psz_list = var_GetString( p_aout, psz_variable );
679 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
680 var_Destroy( p_obj->p_libvlc, psz_variable );
683 /* Split the string into an array of filters */
685 for( char *p = psz_list; p && *p; p++ )
686 i_count += *p == ':';
689 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
695 bool b_present = false;
697 for( char *p = psz_list; p && *p; )
699 char *psz_end = strchr(p, ':');
703 psz_end = p + strlen(p);
706 b_present |= !strcmp( p, psz_name );
707 ppsz_filter[i_count++] = p;
711 if( b_present == b_add )
720 int i_order = FilterOrder( psz_name );
722 for( i = 0; i < i_count; i++ )
724 if( FilterOrder( ppsz_filter[i] ) > i_order )
728 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
729 ppsz_filter[i] = psz_name;
734 for( int i = 0; i < i_count; i++ )
736 if( !strcmp( ppsz_filter[i], psz_name ) )
741 for( int i = 0; i < i_count; i++ )
742 i_length += 1 + strlen( ppsz_filter[i] );
744 char *psz_new = malloc( i_length + 1 );
746 for( int i = 0; i < i_count; i++ )
748 if( *ppsz_filter[i] == '\0' )
751 strcat( psz_new, ":" );
752 strcat( psz_new, ppsz_filter[i] );
758 var_SetString( p_aout, psz_variable, psz_new );
760 config_PutPsz( p_obj, psz_variable, psz_new );