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);
62 vlc_mutex_init (&owner->volume.lock);
63 owner->volume.multiplier = 1.0;
64 owner->volume.mixer = NULL;
66 vlc_mutex_init (&aout->lock);
68 aout_VolumeNoneInit (aout);
69 vlc_object_set_destructor (aout, aout_Destructor);
70 var_Create (aout, "intf-change", VLC_VAR_VOID);
75 /*****************************************************************************
76 * aout_Destructor: destroy aout structure
77 *****************************************************************************/
78 static void aout_Destructor (vlc_object_t *obj)
80 audio_output_t *aout = (audio_output_t *)obj;
81 aout_owner_t *owner = aout_owner (aout);
83 vlc_mutex_destroy (&owner->volume.lock);
84 vlc_mutex_destroy (&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( audio_output_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( audio_output_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 );
340 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
343 /*****************************************************************************
344 * aout_FifoPush : push a packet into the FIFO
345 *****************************************************************************/
346 void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
348 *p_fifo->pp_last = p_buffer;
349 p_fifo->pp_last = &p_buffer->p_next;
350 *p_fifo->pp_last = NULL;
351 /* Enforce the continuity of the stream. */
352 if( date_Get( &p_fifo->end_date ) != VLC_TS_INVALID )
354 p_buffer->i_pts = date_Get( &p_fifo->end_date );
355 p_buffer->i_length = date_Increment( &p_fifo->end_date,
356 p_buffer->i_nb_samples );
357 p_buffer->i_length -= p_buffer->i_pts;
361 date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
365 /*****************************************************************************
366 * aout_FifoReset: trash all buffers
367 *****************************************************************************/
368 void aout_FifoReset( aout_fifo_t * p_fifo )
370 aout_buffer_t * p_buffer;
372 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
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 *fifo, mtime_t difference )
389 if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
391 assert( fifo->p_first == NULL );
395 date_Move( &fifo->end_date, difference );
396 for( block_t *block = fifo->p_first; block != NULL; block = block->p_next )
397 block->i_pts += difference;
400 /*****************************************************************************
401 * aout_FifoPop : get the next buffer out of the FIFO
402 *****************************************************************************/
403 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
405 aout_buffer_t *p_buffer = p_fifo->p_first;
406 if( p_buffer != NULL )
408 p_fifo->p_first = p_buffer->p_next;
409 if( p_fifo->p_first == NULL )
410 p_fifo->pp_last = &p_fifo->p_first;
415 /*****************************************************************************
416 * aout_FifoDestroy : destroy a FIFO and its buffers
417 *****************************************************************************/
418 void aout_FifoDestroy( aout_fifo_t * p_fifo )
420 aout_buffer_t * p_buffer;
422 p_buffer = p_fifo->p_first;
423 while ( p_buffer != NULL )
425 aout_buffer_t * p_next = p_buffer->p_next;
426 aout_BufferFree( p_buffer );
430 p_fifo->p_first = NULL;
431 p_fifo->pp_last = &p_fifo->p_first;
434 /*****************************************************************************
435 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
436 *****************************************************************************/
437 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
438 const uint32_t *pi_chan_order_out,
439 uint32_t i_channel_mask,
440 int i_channels, int *pi_chan_table )
442 bool b_chan_reorder = false;
445 if( i_channels > AOUT_CHAN_MAX )
448 if( pi_chan_order_in == NULL )
449 pi_chan_order_in = pi_vlc_chan_order_wg4;
450 if( pi_chan_order_out == NULL )
451 pi_chan_order_out = pi_vlc_chan_order_wg4;
453 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
455 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
457 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
459 if( i_channel_mask & pi_chan_order_out[k] ) l++;
462 pi_chan_table[j++] = l;
465 for( i = 0; i < i_channels; i++ )
467 if( pi_chan_table[i] != i ) b_chan_reorder = true;
470 return b_chan_reorder;
473 /*****************************************************************************
474 * aout_ChannelReorder :
475 *****************************************************************************/
476 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
477 int i_channels, const int *pi_chan_table,
478 int i_bits_per_sample )
480 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
483 if( i_bits_per_sample == 8 )
485 for( i = 0; i < i_buffer / i_channels; i++ )
487 for( j = 0; j < i_channels; j++ )
489 p_tmp[pi_chan_table[j]] = p_buf[j];
492 memcpy( p_buf, p_tmp, i_channels );
496 else if( i_bits_per_sample == 16 )
498 for( i = 0; i < i_buffer / i_channels / 2; i++ )
500 for( j = 0; j < i_channels; j++ )
502 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
503 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
506 memcpy( p_buf, p_tmp, 2 * i_channels );
507 p_buf += 2 * i_channels;
510 else if( i_bits_per_sample == 24 )
512 for( i = 0; i < i_buffer / i_channels / 3; i++ )
514 for( j = 0; j < i_channels; j++ )
516 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
517 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
518 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
521 memcpy( p_buf, p_tmp, 3 * i_channels );
522 p_buf += 3 * i_channels;
525 else if( i_bits_per_sample == 32 )
527 for( i = 0; i < i_buffer / i_channels / 4; i++ )
529 for( j = 0; j < i_channels; j++ )
531 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
532 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
533 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
534 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
537 memcpy( p_buf, p_tmp, 4 * i_channels );
538 p_buf += 4 * i_channels;
543 /*****************************************************************************
544 * aout_ChannelExtract:
545 *****************************************************************************/
546 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
547 const uint8_t *pi_src, int i_src_channels,
549 const int *pi_selection, int i_bytes )
551 for( int i = 0; i < i_sample_count; i++ )
553 for( int j = 0; j < i_dst_channels; j++ )
554 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
555 pi_dst += i_dst_channels * i_bytes;
556 pi_src += i_src_channels * i_bytes;
560 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
561 const void *p_src, int i_src_channels,
562 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
564 /* It does not work in place */
565 assert( p_dst != p_src );
567 /* Force the compiler to inline for the specific cases so it can optimize */
568 if( i_bits_per_sample == 8 )
569 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
570 else if( i_bits_per_sample == 16 )
571 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
572 else if( i_bits_per_sample == 24 )
573 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
574 else if( i_bits_per_sample == 32 )
575 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
576 else if( i_bits_per_sample == 64 )
577 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
580 bool aout_CheckChannelExtraction( int *pi_selection,
581 uint32_t *pi_layout, int *pi_channels,
582 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
583 const uint32_t *pi_order_src, int i_channels )
585 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
586 uint32_t i_layout = 0;
588 int pi_index[AOUT_CHAN_MAX];
592 pi_order_dst = pi_vlc_chan_order_wg4;
594 /* Detect special dual mono case */
595 if( i_channels == 2 &&
596 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
598 i_layout |= AOUT_CHAN_DUALMONO;
599 pi_order_src = pi_order_dual_mono;
603 for( int i = 0; i < i_channels; i++ )
605 /* Ignore unknown or duplicated channels or not present in output */
606 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
609 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
611 if( pi_order_dst[j] == pi_order_src[i] )
613 assert( i_out < AOUT_CHAN_MAX );
614 pi_index[i_out++] = i;
615 i_layout |= pi_order_src[i];
622 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
624 for( int k = 0; k < i_out; k++ )
626 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
628 pi_selection[j++] = pi_index[k];
634 *pi_layout = i_layout;
635 *pi_channels = i_out;
637 for( int i = 0; i < i_out; i++ )
639 if( pi_selection[i] != i )
642 return i_out == i_channels;
645 /* Return the order in which filters should be inserted */
646 static int FilterOrder( const char *psz_name )
648 static const struct {
649 const char *psz_name;
655 for( int i = 0; filter[i].psz_name; i++ )
657 if( !strcmp( filter[i].psz_name, psz_name ) )
658 return filter[i].i_order;
663 /* This function will add or remove a a module from a string list (colon
664 * separated). It will return true if there is a modification
665 * In case p_aout is NULL, we will use configuration instead of variable */
666 bool aout_ChangeFilterString( vlc_object_t *p_obj, audio_output_t *p_aout,
667 const char *psz_variable,
668 const char *psz_name, bool b_add )
670 if( *psz_name == '\0' )
676 psz_list = var_GetString( p_aout, psz_variable );
680 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
681 var_Destroy( p_obj->p_libvlc, psz_variable );
684 /* Split the string into an array of filters */
686 for( char *p = psz_list; p && *p; p++ )
687 i_count += *p == ':';
690 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
696 bool b_present = false;
698 for( char *p = psz_list; p && *p; )
700 char *psz_end = strchr(p, ':');
704 psz_end = p + strlen(p);
707 b_present |= !strcmp( p, psz_name );
708 ppsz_filter[i_count++] = p;
712 if( b_present == b_add )
721 int i_order = FilterOrder( psz_name );
723 for( i = 0; i < i_count; i++ )
725 if( FilterOrder( ppsz_filter[i] ) > i_order )
729 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
730 ppsz_filter[i] = psz_name;
735 for( int i = 0; i < i_count; i++ )
737 if( !strcmp( ppsz_filter[i], psz_name ) )
742 for( int i = 0; i < i_count; i++ )
743 i_length += 1 + strlen( ppsz_filter[i] );
745 char *psz_new = malloc( i_length + 1 );
747 for( int i = 0; i < i_count; i++ )
749 if( *ppsz_filter[i] == '\0' )
752 strcat( psz_new, ":" );
753 strcat( psz_new, ppsz_filter[i] );
759 var_SetString( p_aout, psz_variable, psz_new );
761 config_PutPsz( p_obj, psz_variable, psz_new );