1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_modules.h>
37 #include "aout_internal.h"
41 * Instances management (internal and external)
45 static void aout_Destructor( vlc_object_t * p_this );
48 /*****************************************************************************
49 * aout_New: initialize aout structure
50 *****************************************************************************/
51 audio_output_t *aout_New( vlc_object_t * p_parent )
53 audio_output_t *aout = vlc_custom_create (p_parent,
54 sizeof (aout_instance_t),
56 if (unlikely(aout == NULL))
59 aout_owner_t *owner = aout_owner (aout);
61 vlc_mutex_init (&owner->lock);
64 vlc_mutex_init (&owner->volume.lock);
65 owner->volume.multiplier = 1.0;
66 owner->volume.mixer = NULL;
68 aout->pf_play = aout_DecDeleteBuffer;
69 aout_VolumeNoneInit (aout);
70 vlc_object_set_destructor (aout, aout_Destructor);
73 * Persistent audio output variables
75 vlc_value_t val, text;
78 var_Create (aout, "intf-change", VLC_VAR_VOID);
81 var_Create (aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
82 text.psz_string = _("Visualizations");
83 var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
84 val.psz_string = (char *)"";
85 text.psz_string = _("Disable");
86 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
87 val.psz_string = (char *)"spectrometer";
88 text.psz_string = _("Spectrometer");
89 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
90 val.psz_string = (char *)"scope";
91 text.psz_string = _("Scope");
92 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
93 val.psz_string = (char *)"spectrum";
94 text.psz_string = _("Spectrum");
95 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
96 val.psz_string = (char *)"vuMeter";
97 text.psz_string = _("Vu meter");
98 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
99 /* Look for goom plugin */
100 if (module_exists ("goom"))
102 val.psz_string = (char *)"goom";
103 text.psz_string = (char *)"Goom";
104 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
106 /* Look for libprojectM plugin */
107 if (module_exists ("projectm"))
109 val.psz_string = (char *)"projectm";
110 text.psz_string = (char*)"projectM";
111 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
113 /* Look for VSXu plugin */
114 if (module_exists ("vsxu"))
116 val.psz_string = (char *)"vsxu";
117 text.psz_string = (char*)"Vovoid VSXu";
118 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
120 str = var_GetNonEmptyString (aout, "effect-list");
123 var_SetString (aout, "visual", str);
128 var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
129 text.psz_string = _("Equalizer");
130 var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
131 val.psz_string = (char*)"";
132 text.psz_string = _("Disable");
133 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
135 module_config_t *cfg = config_FindConfig (VLC_OBJECT(aout),
138 for (int i = 0; i < cfg->i_list; i++)
140 val.psz_string = (char *)cfg->ppsz_list[i];
141 text.psz_string = (char *)cfg->ppsz_list_text[i];
142 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
147 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
148 text.psz_string = _("Audio filters");
149 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
152 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
153 text.psz_string = _("Audio visualizations");
154 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
158 var_Create (aout, "audio-replay-gain-mode",
159 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
160 text.psz_string = _("Replay gain");
161 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
163 module_config_t *cfg = config_FindConfig (VLC_OBJECT(aout),
164 "audio-replay-gain-mode");
166 for (int i = 0; i < cfg->i_list; i++)
168 val.psz_string = (char *)cfg->ppsz_list[i];
169 text.psz_string = (char *)cfg->ppsz_list_text[i];
170 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
179 void aout_Destroy (audio_output_t *aout)
181 aout_owner_t *owner = aout_owner (aout);
183 if (owner->module != NULL)
184 aout_Shutdown (aout);
185 vlc_object_release (aout);
188 /*****************************************************************************
189 * aout_Destructor: destroy aout structure
190 *****************************************************************************/
191 static void aout_Destructor (vlc_object_t *obj)
193 audio_output_t *aout = (audio_output_t *)obj;
194 aout_owner_t *owner = aout_owner (aout);
196 vlc_mutex_destroy (&owner->volume.lock);
197 vlc_mutex_destroy (&owner->lock);
202 static __thread unsigned aout_locks = 0;
204 void aout_lock_check (unsigned i)
213 allowed = VOLUME_LOCK;
219 if (aout_locks & ~allowed)
221 fprintf (stderr, "Illegal audio lock transition (%x -> %x)\n",
222 aout_locks, aout_locks|i);
229 void aout_unlock_check (unsigned i)
231 assert (aout_locks & i);
237 * Formats management (internal and external)
240 /*****************************************************************************
241 * aout_BitsPerSample : get the number of bits per sample
242 *****************************************************************************/
243 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
245 switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
250 case VLC_CODEC_MULAW:
277 /* For these formats the caller has to indicate the parameters
283 /*****************************************************************************
284 * aout_FormatPrepare : compute the number of bytes per frame & frame length
285 *****************************************************************************/
286 void aout_FormatPrepare( audio_sample_format_t * p_format )
288 p_format->i_channels = aout_FormatNbChannels( p_format );
289 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
290 if( p_format->i_bitspersample > 0 )
292 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
293 * aout_FormatNbChannels( p_format );
294 p_format->i_frame_length = 1;
298 /*****************************************************************************
299 * aout_FormatPrintChannels : print a channel in a human-readable form
300 *****************************************************************************/
301 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
303 switch ( p_format->i_physical_channels )
306 case AOUT_CHAN_RIGHT:
307 case AOUT_CHAN_CENTER:
308 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
309 || (p_format->i_original_channels
310 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
312 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
315 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
316 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
318 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
319 return "Dolby/Reverse";
320 return "Stereo/Reverse";
324 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
326 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
328 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
329 return "Stereo/Mono";
330 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
331 return "Stereo/Left";
332 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
333 return "Stereo/Right";
336 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
338 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
340 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
341 | AOUT_CHAN_REARCENTER:
343 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
344 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
346 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
347 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
349 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
350 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
352 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
353 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
356 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
357 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
358 || (p_format->i_original_channels
359 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
361 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
364 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
365 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
367 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
368 return "Dual-mono/LFE";
369 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
371 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
372 return "Stereo/Left/LFE";
373 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
374 return "Stereo/Right/LFE";
376 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
378 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
381 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
382 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
384 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
385 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
387 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
388 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
390 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
391 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
393 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
394 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
396 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
397 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
398 | AOUT_CHAN_MIDDLERIGHT:
400 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
401 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
402 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
409 #undef aout_FormatPrint
411 * Prints an audio sample format in a human-readable form.
413 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
414 const audio_sample_format_t *p_format )
416 msg_Dbg( obj, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
417 (char *)&p_format->i_format, p_format->i_rate,
418 aout_FormatPrintChannels( p_format ),
419 p_format->i_frame_length, p_format->i_bytes_per_frame );
422 #undef aout_FormatsPrint
424 * Prints two formats in a human-readable form
426 void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
427 const audio_sample_format_t * p_format1,
428 const audio_sample_format_t * p_format2 )
430 msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
432 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
433 p_format1->i_rate, p_format2->i_rate,
434 aout_FormatPrintChannels( p_format1 ),
435 aout_FormatPrintChannels( p_format2 ) );
438 /*****************************************************************************
439 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
440 *****************************************************************************/
441 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
442 const uint32_t *pi_chan_order_out,
443 uint32_t i_channel_mask,
444 int i_channels, int *pi_chan_table )
446 bool b_chan_reorder = false;
449 if( i_channels > AOUT_CHAN_MAX )
452 if( pi_chan_order_in == NULL )
453 pi_chan_order_in = pi_vlc_chan_order_wg4;
454 if( pi_chan_order_out == NULL )
455 pi_chan_order_out = pi_vlc_chan_order_wg4;
457 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
459 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
461 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
463 if( i_channel_mask & pi_chan_order_out[k] ) l++;
466 pi_chan_table[j++] = l;
469 for( i = 0; i < i_channels; i++ )
471 if( pi_chan_table[i] != i ) b_chan_reorder = true;
474 return b_chan_reorder;
477 /*****************************************************************************
478 * aout_ChannelReorder :
479 *****************************************************************************/
480 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
481 int i_channels, const int *pi_chan_table,
482 int i_bits_per_sample )
484 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
487 if( i_bits_per_sample == 8 )
489 for( i = 0; i < i_buffer / i_channels; i++ )
491 for( j = 0; j < i_channels; j++ )
493 p_tmp[pi_chan_table[j]] = p_buf[j];
496 memcpy( p_buf, p_tmp, i_channels );
500 else if( i_bits_per_sample == 16 )
502 for( i = 0; i < i_buffer / i_channels / 2; i++ )
504 for( j = 0; j < i_channels; j++ )
506 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
507 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
510 memcpy( p_buf, p_tmp, 2 * i_channels );
511 p_buf += 2 * i_channels;
514 else if( i_bits_per_sample == 24 )
516 for( i = 0; i < i_buffer / i_channels / 3; i++ )
518 for( j = 0; j < i_channels; j++ )
520 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
521 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
522 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
525 memcpy( p_buf, p_tmp, 3 * i_channels );
526 p_buf += 3 * i_channels;
529 else if( i_bits_per_sample == 32 )
531 for( i = 0; i < i_buffer / i_channels / 4; i++ )
533 for( j = 0; j < i_channels; j++ )
535 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
536 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
537 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
538 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
541 memcpy( p_buf, p_tmp, 4 * i_channels );
542 p_buf += 4 * i_channels;
547 /*****************************************************************************
548 * aout_ChannelExtract:
549 *****************************************************************************/
550 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
551 const uint8_t *pi_src, int i_src_channels,
553 const int *pi_selection, int i_bytes )
555 for( int i = 0; i < i_sample_count; i++ )
557 for( int j = 0; j < i_dst_channels; j++ )
558 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
559 pi_dst += i_dst_channels * i_bytes;
560 pi_src += i_src_channels * i_bytes;
564 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
565 const void *p_src, int i_src_channels,
566 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
568 /* It does not work in place */
569 assert( p_dst != p_src );
571 /* Force the compiler to inline for the specific cases so it can optimize */
572 if( i_bits_per_sample == 8 )
573 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
574 else if( i_bits_per_sample == 16 )
575 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
576 else if( i_bits_per_sample == 24 )
577 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
578 else if( i_bits_per_sample == 32 )
579 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
580 else if( i_bits_per_sample == 64 )
581 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
584 bool aout_CheckChannelExtraction( int *pi_selection,
585 uint32_t *pi_layout, int *pi_channels,
586 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
587 const uint32_t *pi_order_src, int i_channels )
589 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
590 uint32_t i_layout = 0;
592 int pi_index[AOUT_CHAN_MAX];
596 pi_order_dst = pi_vlc_chan_order_wg4;
598 /* Detect special dual mono case */
599 if( i_channels == 2 &&
600 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
602 i_layout |= AOUT_CHAN_DUALMONO;
603 pi_order_src = pi_order_dual_mono;
607 for( int i = 0; i < i_channels; i++ )
609 /* Ignore unknown or duplicated channels or not present in output */
610 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
613 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
615 if( pi_order_dst[j] == pi_order_src[i] )
617 assert( i_out < AOUT_CHAN_MAX );
618 pi_index[i_out++] = i;
619 i_layout |= pi_order_src[i];
626 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
628 for( int k = 0; k < i_out; k++ )
630 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
632 pi_selection[j++] = pi_index[k];
638 *pi_layout = i_layout;
639 *pi_channels = i_out;
641 for( int i = 0; i < i_out; i++ )
643 if( pi_selection[i] != i )
646 return i_out == i_channels;
649 /* Return the order in which filters should be inserted */
650 static int FilterOrder( const char *psz_name )
652 static const struct {
653 const char *psz_name;
659 for( int i = 0; filter[i].psz_name; i++ )
661 if( !strcmp( filter[i].psz_name, psz_name ) )
662 return filter[i].i_order;
667 /* This function will add or remove a a module from a string list (colon
668 * separated). It will return true if there is a modification
669 * In case p_aout is NULL, we will use configuration instead of variable */
670 bool aout_ChangeFilterString( vlc_object_t *p_obj, vlc_object_t *p_aout,
671 const char *psz_variable,
672 const char *psz_name, bool b_add )
674 if( *psz_name == '\0' )
680 psz_list = var_GetString( p_aout, psz_variable );
684 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
685 var_Destroy( p_obj->p_libvlc, psz_variable );
688 /* Split the string into an array of filters */
690 for( char *p = psz_list; p && *p; p++ )
691 i_count += *p == ':';
694 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
700 bool b_present = false;
702 for( char *p = psz_list; p && *p; )
704 char *psz_end = strchr(p, ':');
708 psz_end = p + strlen(p);
711 b_present |= !strcmp( p, psz_name );
712 ppsz_filter[i_count++] = p;
716 if( b_present == b_add )
725 int i_order = FilterOrder( psz_name );
727 for( i = 0; i < i_count; i++ )
729 if( FilterOrder( ppsz_filter[i] ) > i_order )
733 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
734 ppsz_filter[i] = psz_name;
739 for( int i = 0; i < i_count; i++ )
741 if( !strcmp( ppsz_filter[i], psz_name ) )
746 for( int i = 0; i < i_count; i++ )
747 i_length += 1 + strlen( ppsz_filter[i] );
749 char *psz_new = malloc( i_length + 1 );
751 for( int i = 0; i < i_count; i++ )
753 if( *ppsz_filter[i] == '\0' )
756 strcat( psz_new, ":" );
757 strcat( psz_new, ppsz_filter[i] );
763 var_SetString( p_aout, psz_variable, psz_new );
765 config_PutPsz( p_obj, psz_variable, psz_new );