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 str = var_GetNonEmptyString (aout, "effect-list");
116 var_SetString (aout, "visual", str);
121 var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
122 text.psz_string = _("Equalizer");
123 var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
124 val.psz_string = (char*)"";
125 text.psz_string = _("Disable");
126 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
128 module_config_t *cfg = config_FindConfig (VLC_OBJECT(aout),
131 for (int i = 0; i < cfg->i_list; i++)
133 val.psz_string = (char *)cfg->ppsz_list[i];
134 text.psz_string = (char *)cfg->ppsz_list_text[i];
135 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
140 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
141 text.psz_string = _("Audio filters");
142 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
145 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
146 text.psz_string = _("Audio visualizations");
147 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
151 var_Create (aout, "audio-replay-gain-mode",
152 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
153 text.psz_string = _("Replay gain");
154 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
156 module_config_t *cfg = config_FindConfig (VLC_OBJECT(aout),
157 "audio-replay-gain-mode");
159 for (int i = 0; i < cfg->i_list; i++)
161 val.psz_string = (char *)cfg->ppsz_list[i];
162 text.psz_string = (char *)cfg->ppsz_list_text[i];
163 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
172 void aout_Destroy (audio_output_t *aout)
174 aout_owner_t *owner = aout_owner (aout);
176 if (owner->module != NULL)
177 aout_Shutdown (aout);
178 vlc_object_release (aout);
181 /*****************************************************************************
182 * aout_Destructor: destroy aout structure
183 *****************************************************************************/
184 static void aout_Destructor (vlc_object_t *obj)
186 audio_output_t *aout = (audio_output_t *)obj;
187 aout_owner_t *owner = aout_owner (aout);
189 vlc_mutex_destroy (&owner->volume.lock);
190 vlc_mutex_destroy (&owner->lock);
195 static __thread unsigned aout_locks = 0;
197 void aout_lock_check (unsigned i)
206 allowed = VOLUME_LOCK;
212 if (aout_locks & ~allowed)
214 fprintf (stderr, "Illegal audio lock transition (%x -> %x)\n",
215 aout_locks, aout_locks|i);
222 void aout_unlock_check (unsigned i)
224 assert (aout_locks & i);
230 * Formats management (internal and external)
233 /*****************************************************************************
234 * aout_BitsPerSample : get the number of bits per sample
235 *****************************************************************************/
236 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
238 switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
243 case VLC_CODEC_MULAW:
270 /* For these formats the caller has to indicate the parameters
276 /*****************************************************************************
277 * aout_FormatPrepare : compute the number of bytes per frame & frame length
278 *****************************************************************************/
279 void aout_FormatPrepare( audio_sample_format_t * p_format )
281 p_format->i_channels = aout_FormatNbChannels( p_format );
282 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
283 if( p_format->i_bitspersample > 0 )
285 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
286 * aout_FormatNbChannels( p_format );
287 p_format->i_frame_length = 1;
291 /*****************************************************************************
292 * aout_FormatPrintChannels : print a channel in a human-readable form
293 *****************************************************************************/
294 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
296 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
299 case AOUT_CHAN_RIGHT:
300 case AOUT_CHAN_CENTER:
301 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
302 || (p_format->i_original_channels
303 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
305 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
308 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
309 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
311 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
312 return "Dolby/Reverse";
313 return "Stereo/Reverse";
317 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
319 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
321 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
322 return "Stereo/Mono";
323 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
324 return "Stereo/Left";
325 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
326 return "Stereo/Right";
329 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
331 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
333 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
334 | AOUT_CHAN_REARCENTER:
336 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
337 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
339 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
340 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
342 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
343 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
345 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
346 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
349 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
350 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
351 || (p_format->i_original_channels
352 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
354 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
357 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
358 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
360 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
361 return "Dual-mono/LFE";
362 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
364 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
365 return "Stereo/Left/LFE";
366 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
367 return "Stereo/Right/LFE";
369 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
371 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
374 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
375 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
377 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
378 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
380 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
381 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
383 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
384 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
386 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
387 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
389 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
390 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
391 | AOUT_CHAN_MIDDLERIGHT:
393 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
394 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
395 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
402 #undef aout_FormatPrint
404 * Prints an audio sample format in a human-readable form.
406 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
407 const audio_sample_format_t *p_format )
409 msg_Dbg( obj, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
410 (char *)&p_format->i_format, p_format->i_rate,
411 aout_FormatPrintChannels( p_format ),
412 p_format->i_frame_length, p_format->i_bytes_per_frame );
415 #undef aout_FormatsPrint
417 * Prints two formats in a human-readable form
419 void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
420 const audio_sample_format_t * p_format1,
421 const audio_sample_format_t * p_format2 )
423 msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
425 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
426 p_format1->i_rate, p_format2->i_rate,
427 aout_FormatPrintChannels( p_format1 ),
428 aout_FormatPrintChannels( p_format2 ) );
433 * FIFO management (internal) - please understand that solving race conditions
434 * is _your_ job, ie. in the audio output you should own the mixer lock
435 * before calling any of these functions.
439 /*****************************************************************************
440 * aout_FifoInit : initialize the members of a FIFO
441 *****************************************************************************/
442 void aout_FifoInit( vlc_object_t *obj, aout_fifo_t * p_fifo, uint32_t i_rate )
444 if( unlikely(i_rate == 0) )
445 msg_Err( obj, "initialising fifo with zero divider" );
447 p_fifo->p_first = NULL;
448 p_fifo->pp_last = &p_fifo->p_first;
449 date_Init( &p_fifo->end_date, i_rate, 1 );
450 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
453 /*****************************************************************************
454 * aout_FifoPush : push a packet into the FIFO
455 *****************************************************************************/
456 void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
458 *p_fifo->pp_last = p_buffer;
459 p_fifo->pp_last = &p_buffer->p_next;
460 *p_fifo->pp_last = NULL;
461 /* Enforce the continuity of the stream. */
462 if( date_Get( &p_fifo->end_date ) != VLC_TS_INVALID )
464 p_buffer->i_pts = date_Get( &p_fifo->end_date );
465 p_buffer->i_length = date_Increment( &p_fifo->end_date,
466 p_buffer->i_nb_samples );
467 p_buffer->i_length -= p_buffer->i_pts;
471 date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
475 /*****************************************************************************
476 * aout_FifoReset: trash all buffers
477 *****************************************************************************/
478 void aout_FifoReset( aout_fifo_t * p_fifo )
480 aout_buffer_t * p_buffer;
482 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
483 p_buffer = p_fifo->p_first;
484 while ( p_buffer != NULL )
486 aout_buffer_t * p_next = p_buffer->p_next;
487 aout_BufferFree( p_buffer );
490 p_fifo->p_first = NULL;
491 p_fifo->pp_last = &p_fifo->p_first;
494 /*****************************************************************************
495 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
496 *****************************************************************************/
497 void aout_FifoMoveDates( aout_fifo_t *fifo, mtime_t difference )
499 if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
501 assert( fifo->p_first == NULL );
505 date_Move( &fifo->end_date, difference );
506 for( block_t *block = fifo->p_first; block != NULL; block = block->p_next )
507 block->i_pts += difference;
510 /*****************************************************************************
511 * aout_FifoPop : get the next buffer out of the FIFO
512 *****************************************************************************/
513 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
515 aout_buffer_t *p_buffer = p_fifo->p_first;
516 if( p_buffer != NULL )
518 p_fifo->p_first = p_buffer->p_next;
519 if( p_fifo->p_first == NULL )
520 p_fifo->pp_last = &p_fifo->p_first;
525 /*****************************************************************************
526 * aout_FifoDestroy : destroy a FIFO and its buffers
527 *****************************************************************************/
528 void aout_FifoDestroy( aout_fifo_t * p_fifo )
530 aout_buffer_t * p_buffer;
532 p_buffer = p_fifo->p_first;
533 while ( p_buffer != NULL )
535 aout_buffer_t * p_next = p_buffer->p_next;
536 aout_BufferFree( p_buffer );
540 p_fifo->p_first = NULL;
541 p_fifo->pp_last = &p_fifo->p_first;
544 /*****************************************************************************
545 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
546 *****************************************************************************/
547 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
548 const uint32_t *pi_chan_order_out,
549 uint32_t i_channel_mask,
550 int i_channels, int *pi_chan_table )
552 bool b_chan_reorder = false;
555 if( i_channels > AOUT_CHAN_MAX )
558 if( pi_chan_order_in == NULL )
559 pi_chan_order_in = pi_vlc_chan_order_wg4;
560 if( pi_chan_order_out == NULL )
561 pi_chan_order_out = pi_vlc_chan_order_wg4;
563 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
565 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
567 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
569 if( i_channel_mask & pi_chan_order_out[k] ) l++;
572 pi_chan_table[j++] = l;
575 for( i = 0; i < i_channels; i++ )
577 if( pi_chan_table[i] != i ) b_chan_reorder = true;
580 return b_chan_reorder;
583 /*****************************************************************************
584 * aout_ChannelReorder :
585 *****************************************************************************/
586 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
587 int i_channels, const int *pi_chan_table,
588 int i_bits_per_sample )
590 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
593 if( i_bits_per_sample == 8 )
595 for( i = 0; i < i_buffer / i_channels; i++ )
597 for( j = 0; j < i_channels; j++ )
599 p_tmp[pi_chan_table[j]] = p_buf[j];
602 memcpy( p_buf, p_tmp, i_channels );
606 else if( i_bits_per_sample == 16 )
608 for( i = 0; i < i_buffer / i_channels / 2; i++ )
610 for( j = 0; j < i_channels; j++ )
612 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
613 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
616 memcpy( p_buf, p_tmp, 2 * i_channels );
617 p_buf += 2 * i_channels;
620 else if( i_bits_per_sample == 24 )
622 for( i = 0; i < i_buffer / i_channels / 3; i++ )
624 for( j = 0; j < i_channels; j++ )
626 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
627 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
628 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
631 memcpy( p_buf, p_tmp, 3 * i_channels );
632 p_buf += 3 * i_channels;
635 else if( i_bits_per_sample == 32 )
637 for( i = 0; i < i_buffer / i_channels / 4; i++ )
639 for( j = 0; j < i_channels; j++ )
641 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
642 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
643 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
644 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
647 memcpy( p_buf, p_tmp, 4 * i_channels );
648 p_buf += 4 * i_channels;
653 /*****************************************************************************
654 * aout_ChannelExtract:
655 *****************************************************************************/
656 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
657 const uint8_t *pi_src, int i_src_channels,
659 const int *pi_selection, int i_bytes )
661 for( int i = 0; i < i_sample_count; i++ )
663 for( int j = 0; j < i_dst_channels; j++ )
664 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
665 pi_dst += i_dst_channels * i_bytes;
666 pi_src += i_src_channels * i_bytes;
670 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
671 const void *p_src, int i_src_channels,
672 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
674 /* It does not work in place */
675 assert( p_dst != p_src );
677 /* Force the compiler to inline for the specific cases so it can optimize */
678 if( i_bits_per_sample == 8 )
679 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
680 else if( i_bits_per_sample == 16 )
681 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
682 else if( i_bits_per_sample == 24 )
683 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
684 else if( i_bits_per_sample == 32 )
685 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
686 else if( i_bits_per_sample == 64 )
687 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
690 bool aout_CheckChannelExtraction( int *pi_selection,
691 uint32_t *pi_layout, int *pi_channels,
692 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
693 const uint32_t *pi_order_src, int i_channels )
695 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
696 uint32_t i_layout = 0;
698 int pi_index[AOUT_CHAN_MAX];
702 pi_order_dst = pi_vlc_chan_order_wg4;
704 /* Detect special dual mono case */
705 if( i_channels == 2 &&
706 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
708 i_layout |= AOUT_CHAN_DUALMONO;
709 pi_order_src = pi_order_dual_mono;
713 for( int i = 0; i < i_channels; i++ )
715 /* Ignore unknown or duplicated channels or not present in output */
716 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
719 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
721 if( pi_order_dst[j] == pi_order_src[i] )
723 assert( i_out < AOUT_CHAN_MAX );
724 pi_index[i_out++] = i;
725 i_layout |= pi_order_src[i];
732 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
734 for( int k = 0; k < i_out; k++ )
736 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
738 pi_selection[j++] = pi_index[k];
744 *pi_layout = i_layout;
745 *pi_channels = i_out;
747 for( int i = 0; i < i_out; i++ )
749 if( pi_selection[i] != i )
752 return i_out == i_channels;
755 /* Return the order in which filters should be inserted */
756 static int FilterOrder( const char *psz_name )
758 static const struct {
759 const char *psz_name;
765 for( int i = 0; filter[i].psz_name; i++ )
767 if( !strcmp( filter[i].psz_name, psz_name ) )
768 return filter[i].i_order;
773 /* This function will add or remove a a module from a string list (colon
774 * separated). It will return true if there is a modification
775 * In case p_aout is NULL, we will use configuration instead of variable */
776 bool aout_ChangeFilterString( vlc_object_t *p_obj, vlc_object_t *p_aout,
777 const char *psz_variable,
778 const char *psz_name, bool b_add )
780 if( *psz_name == '\0' )
786 psz_list = var_GetString( p_aout, psz_variable );
790 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
791 var_Destroy( p_obj->p_libvlc, psz_variable );
794 /* Split the string into an array of filters */
796 for( char *p = psz_list; p && *p; p++ )
797 i_count += *p == ':';
800 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
806 bool b_present = false;
808 for( char *p = psz_list; p && *p; )
810 char *psz_end = strchr(p, ':');
814 psz_end = p + strlen(p);
817 b_present |= !strcmp( p, psz_name );
818 ppsz_filter[i_count++] = p;
822 if( b_present == b_add )
831 int i_order = FilterOrder( psz_name );
833 for( i = 0; i < i_count; i++ )
835 if( FilterOrder( ppsz_filter[i] ) > i_order )
839 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
840 ppsz_filter[i] = psz_name;
845 for( int i = 0; i < i_count; i++ )
847 if( !strcmp( ppsz_filter[i], psz_name ) )
852 for( int i = 0; i < i_count; i++ )
853 i_length += 1 + strlen( ppsz_filter[i] );
855 char *psz_new = malloc( i_length + 1 );
857 for( int i = 0; i < i_count; i++ )
859 if( *ppsz_filter[i] == '\0' )
862 strcat( psz_new, ":" );
863 strcat( psz_new, ppsz_filter[i] );
869 var_SetString( p_aout, psz_variable, psz_new );
871 config_PutPsz( p_obj, psz_variable, psz_new );