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 <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_VolumeNoneInit (aout);
69 vlc_object_set_destructor (aout, aout_Destructor);
72 * Persistent audio output variables
74 vlc_value_t val, text;
77 var_Create (aout, "intf-change", VLC_VAR_VOID);
80 var_Create (aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
81 text.psz_string = _("Visualizations");
82 var_Change (aout, "visual", VLC_VAR_SETTEXT, &text, NULL);
83 val.psz_string = (char *)"";
84 text.psz_string = _("Disable");
85 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
86 val.psz_string = (char *)"spectrometer";
87 text.psz_string = _("Spectrometer");
88 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
89 val.psz_string = (char *)"scope";
90 text.psz_string = _("Scope");
91 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
92 val.psz_string = (char *)"spectrum";
93 text.psz_string = _("Spectrum");
94 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
95 val.psz_string = (char *)"vuMeter";
96 text.psz_string = _("Vu meter");
97 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
98 /* Look for goom plugin */
99 if (module_exists ("goom"))
101 val.psz_string = (char *)"goom";
102 text.psz_string = (char *)"Goom";
103 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
105 /* Look for libprojectM plugin */
106 if (module_exists ("projectm"))
108 val.psz_string = (char *)"projectm";
109 text.psz_string = (char*)"projectM";
110 var_Change (aout, "visual", VLC_VAR_ADDCHOICE, &val, &text);
112 str = var_GetNonEmptyString (aout, "effect-list");
115 var_SetString (aout, "visual", str);
120 var_Create (aout, "equalizer", VLC_VAR_STRING | VLC_VAR_HASCHOICE);
121 text.psz_string = _("Equalizer");
122 var_Change (aout, "equalizer", VLC_VAR_SETTEXT, &text, NULL);
123 val.psz_string = (char*)"";
124 text.psz_string = _("Disable");
125 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
127 module_config_t *cfg = config_FindConfig (VLC_OBJECT(aout),
130 for (int i = 0; i < cfg->i_list; i++)
132 val.psz_string = (char *)cfg->ppsz_list[i];
133 text.psz_string = (char *)cfg->ppsz_list_text[i];
134 var_Change (aout, "equalizer", VLC_VAR_ADDCHOICE, &val, &text);
139 var_Create (aout, "audio-filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
140 text.psz_string = _("Audio filters");
141 var_Change (aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL);
144 var_Create (aout, "audio-visual", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
145 text.psz_string = _("Audio visualizations");
146 var_Change (aout, "audio-visual", VLC_VAR_SETTEXT, &text, NULL);
150 var_Create (aout, "audio-replay-gain-mode",
151 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
152 text.psz_string = _("Replay gain");
153 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_SETTEXT, &text, NULL);
155 module_config_t *cfg = config_FindConfig (VLC_OBJECT(aout),
156 "audio-replay-gain-mode");
158 for (int i = 0; i < cfg->i_list; i++)
160 val.psz_string = (char *)cfg->ppsz_list[i];
161 text.psz_string = (char *)cfg->ppsz_list_text[i];
162 var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
171 /*****************************************************************************
172 * aout_Destructor: destroy aout structure
173 *****************************************************************************/
174 static void aout_Destructor (vlc_object_t *obj)
176 audio_output_t *aout = (audio_output_t *)obj;
177 aout_owner_t *owner = aout_owner (aout);
179 vlc_mutex_destroy (&owner->volume.lock);
180 vlc_mutex_destroy (&owner->lock);
185 static __thread unsigned aout_locks = 0;
187 void aout_lock_check (unsigned i)
196 allowed = VOLUME_LOCK;
202 if (aout_locks & ~allowed)
204 fprintf (stderr, "Illegal audio lock transition (%x -> %x)\n",
205 aout_locks, aout_locks|i);
212 void aout_unlock_check (unsigned i)
214 assert (aout_locks & i);
220 * Formats management (internal and external)
223 /*****************************************************************************
224 * aout_BitsPerSample : get the number of bits per sample
225 *****************************************************************************/
226 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
228 switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
258 /* For these formats the caller has to indicate the parameters
264 /*****************************************************************************
265 * aout_FormatPrepare : compute the number of bytes per frame & frame length
266 *****************************************************************************/
267 void aout_FormatPrepare( audio_sample_format_t * p_format )
269 p_format->i_channels = aout_FormatNbChannels( p_format );
270 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
271 if( p_format->i_bitspersample > 0 )
273 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
274 * aout_FormatNbChannels( p_format );
275 p_format->i_frame_length = 1;
279 /*****************************************************************************
280 * aout_FormatPrintChannels : print a channel in a human-readable form
281 *****************************************************************************/
282 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
284 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
287 case AOUT_CHAN_RIGHT:
288 case AOUT_CHAN_CENTER:
289 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
290 || (p_format->i_original_channels
291 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
293 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
296 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
297 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
299 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
300 return "Dolby/Reverse";
301 return "Stereo/Reverse";
305 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
307 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
309 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
310 return "Stereo/Mono";
311 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
312 return "Stereo/Left";
313 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
314 return "Stereo/Right";
317 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
319 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
321 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
322 | AOUT_CHAN_REARCENTER:
324 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
325 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
327 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
328 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
330 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
331 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
333 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
334 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
337 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
338 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
339 || (p_format->i_original_channels
340 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
342 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
345 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
346 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
348 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
349 return "Dual-mono/LFE";
350 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
352 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
353 return "Stereo/Left/LFE";
354 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
355 return "Stereo/Right/LFE";
357 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
359 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
362 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
363 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
365 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
366 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
368 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
369 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
371 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
372 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
374 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
375 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
377 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
378 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
379 | AOUT_CHAN_MIDDLERIGHT:
381 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
382 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
383 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
390 #undef aout_FormatPrint
392 * Prints an audio sample format in a human-readable form.
394 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
395 const audio_sample_format_t *p_format )
397 msg_Dbg( obj, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
398 (char *)&p_format->i_format, p_format->i_rate,
399 aout_FormatPrintChannels( p_format ),
400 p_format->i_frame_length, p_format->i_bytes_per_frame );
403 #undef aout_FormatsPrint
405 * Prints two formats in a human-readable form
407 void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
408 const audio_sample_format_t * p_format1,
409 const audio_sample_format_t * p_format2 )
411 msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
413 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
414 p_format1->i_rate, p_format2->i_rate,
415 aout_FormatPrintChannels( p_format1 ),
416 aout_FormatPrintChannels( p_format2 ) );
421 * FIFO management (internal) - please understand that solving race conditions
422 * is _your_ job, ie. in the audio output you should own the mixer lock
423 * before calling any of these functions.
427 /*****************************************************************************
428 * aout_FifoInit : initialize the members of a FIFO
429 *****************************************************************************/
430 void aout_FifoInit( vlc_object_t *obj, aout_fifo_t * p_fifo, uint32_t i_rate )
432 if( unlikely(i_rate == 0) )
433 msg_Err( obj, "initialising fifo with zero divider" );
435 p_fifo->p_first = NULL;
436 p_fifo->pp_last = &p_fifo->p_first;
437 date_Init( &p_fifo->end_date, i_rate, 1 );
438 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
441 /*****************************************************************************
442 * aout_FifoPush : push a packet into the FIFO
443 *****************************************************************************/
444 void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
446 *p_fifo->pp_last = p_buffer;
447 p_fifo->pp_last = &p_buffer->p_next;
448 *p_fifo->pp_last = NULL;
449 /* Enforce the continuity of the stream. */
450 if( date_Get( &p_fifo->end_date ) != VLC_TS_INVALID )
452 p_buffer->i_pts = date_Get( &p_fifo->end_date );
453 p_buffer->i_length = date_Increment( &p_fifo->end_date,
454 p_buffer->i_nb_samples );
455 p_buffer->i_length -= p_buffer->i_pts;
459 date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
463 /*****************************************************************************
464 * aout_FifoReset: trash all buffers
465 *****************************************************************************/
466 void aout_FifoReset( aout_fifo_t * p_fifo )
468 aout_buffer_t * p_buffer;
470 date_Set( &p_fifo->end_date, VLC_TS_INVALID );
471 p_buffer = p_fifo->p_first;
472 while ( p_buffer != NULL )
474 aout_buffer_t * p_next = p_buffer->p_next;
475 aout_BufferFree( p_buffer );
478 p_fifo->p_first = NULL;
479 p_fifo->pp_last = &p_fifo->p_first;
482 /*****************************************************************************
483 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
484 *****************************************************************************/
485 void aout_FifoMoveDates( aout_fifo_t *fifo, mtime_t difference )
487 if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
489 assert( fifo->p_first == NULL );
493 date_Move( &fifo->end_date, difference );
494 for( block_t *block = fifo->p_first; block != NULL; block = block->p_next )
495 block->i_pts += difference;
498 /*****************************************************************************
499 * aout_FifoPop : get the next buffer out of the FIFO
500 *****************************************************************************/
501 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
503 aout_buffer_t *p_buffer = p_fifo->p_first;
504 if( p_buffer != NULL )
506 p_fifo->p_first = p_buffer->p_next;
507 if( p_fifo->p_first == NULL )
508 p_fifo->pp_last = &p_fifo->p_first;
513 /*****************************************************************************
514 * aout_FifoDestroy : destroy a FIFO and its buffers
515 *****************************************************************************/
516 void aout_FifoDestroy( aout_fifo_t * p_fifo )
518 aout_buffer_t * p_buffer;
520 p_buffer = p_fifo->p_first;
521 while ( p_buffer != NULL )
523 aout_buffer_t * p_next = p_buffer->p_next;
524 aout_BufferFree( p_buffer );
528 p_fifo->p_first = NULL;
529 p_fifo->pp_last = &p_fifo->p_first;
532 /*****************************************************************************
533 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
534 *****************************************************************************/
535 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
536 const uint32_t *pi_chan_order_out,
537 uint32_t i_channel_mask,
538 int i_channels, int *pi_chan_table )
540 bool b_chan_reorder = false;
543 if( i_channels > AOUT_CHAN_MAX )
546 if( pi_chan_order_in == NULL )
547 pi_chan_order_in = pi_vlc_chan_order_wg4;
548 if( pi_chan_order_out == NULL )
549 pi_chan_order_out = pi_vlc_chan_order_wg4;
551 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
553 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
555 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
557 if( i_channel_mask & pi_chan_order_out[k] ) l++;
560 pi_chan_table[j++] = l;
563 for( i = 0; i < i_channels; i++ )
565 if( pi_chan_table[i] != i ) b_chan_reorder = true;
568 return b_chan_reorder;
571 /*****************************************************************************
572 * aout_ChannelReorder :
573 *****************************************************************************/
574 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
575 int i_channels, const int *pi_chan_table,
576 int i_bits_per_sample )
578 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
581 if( i_bits_per_sample == 8 )
583 for( i = 0; i < i_buffer / i_channels; i++ )
585 for( j = 0; j < i_channels; j++ )
587 p_tmp[pi_chan_table[j]] = p_buf[j];
590 memcpy( p_buf, p_tmp, i_channels );
594 else if( i_bits_per_sample == 16 )
596 for( i = 0; i < i_buffer / i_channels / 2; i++ )
598 for( j = 0; j < i_channels; j++ )
600 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
601 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
604 memcpy( p_buf, p_tmp, 2 * i_channels );
605 p_buf += 2 * i_channels;
608 else if( i_bits_per_sample == 24 )
610 for( i = 0; i < i_buffer / i_channels / 3; i++ )
612 for( j = 0; j < i_channels; j++ )
614 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
615 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
616 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
619 memcpy( p_buf, p_tmp, 3 * i_channels );
620 p_buf += 3 * i_channels;
623 else if( i_bits_per_sample == 32 )
625 for( i = 0; i < i_buffer / i_channels / 4; i++ )
627 for( j = 0; j < i_channels; j++ )
629 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
630 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
631 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
632 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
635 memcpy( p_buf, p_tmp, 4 * i_channels );
636 p_buf += 4 * i_channels;
641 /*****************************************************************************
642 * aout_ChannelExtract:
643 *****************************************************************************/
644 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
645 const uint8_t *pi_src, int i_src_channels,
647 const int *pi_selection, int i_bytes )
649 for( int i = 0; i < i_sample_count; i++ )
651 for( int j = 0; j < i_dst_channels; j++ )
652 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
653 pi_dst += i_dst_channels * i_bytes;
654 pi_src += i_src_channels * i_bytes;
658 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
659 const void *p_src, int i_src_channels,
660 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
662 /* It does not work in place */
663 assert( p_dst != p_src );
665 /* Force the compiler to inline for the specific cases so it can optimize */
666 if( i_bits_per_sample == 8 )
667 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
668 else if( i_bits_per_sample == 16 )
669 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
670 else if( i_bits_per_sample == 24 )
671 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
672 else if( i_bits_per_sample == 32 )
673 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
674 else if( i_bits_per_sample == 64 )
675 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
678 bool aout_CheckChannelExtraction( int *pi_selection,
679 uint32_t *pi_layout, int *pi_channels,
680 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
681 const uint32_t *pi_order_src, int i_channels )
683 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
684 uint32_t i_layout = 0;
686 int pi_index[AOUT_CHAN_MAX];
690 pi_order_dst = pi_vlc_chan_order_wg4;
692 /* Detect special dual mono case */
693 if( i_channels == 2 &&
694 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
696 i_layout |= AOUT_CHAN_DUALMONO;
697 pi_order_src = pi_order_dual_mono;
701 for( int i = 0; i < i_channels; i++ )
703 /* Ignore unknown or duplicated channels or not present in output */
704 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
707 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
709 if( pi_order_dst[j] == pi_order_src[i] )
711 assert( i_out < AOUT_CHAN_MAX );
712 pi_index[i_out++] = i;
713 i_layout |= pi_order_src[i];
720 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
722 for( int k = 0; k < i_out; k++ )
724 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
726 pi_selection[j++] = pi_index[k];
732 *pi_layout = i_layout;
733 *pi_channels = i_out;
735 for( int i = 0; i < i_out; i++ )
737 if( pi_selection[i] != i )
740 return i_out == i_channels;
743 /* Return the order in which filters should be inserted */
744 static int FilterOrder( const char *psz_name )
746 static const struct {
747 const char *psz_name;
753 for( int i = 0; filter[i].psz_name; i++ )
755 if( !strcmp( filter[i].psz_name, psz_name ) )
756 return filter[i].i_order;
761 /* This function will add or remove a a module from a string list (colon
762 * separated). It will return true if there is a modification
763 * In case p_aout is NULL, we will use configuration instead of variable */
764 bool aout_ChangeFilterString( vlc_object_t *p_obj, vlc_object_t *p_aout,
765 const char *psz_variable,
766 const char *psz_name, bool b_add )
768 if( *psz_name == '\0' )
774 psz_list = var_GetString( p_aout, psz_variable );
778 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
779 var_Destroy( p_obj->p_libvlc, psz_variable );
782 /* Split the string into an array of filters */
784 for( char *p = psz_list; p && *p; p++ )
785 i_count += *p == ':';
788 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
794 bool b_present = false;
796 for( char *p = psz_list; p && *p; )
798 char *psz_end = strchr(p, ':');
802 psz_end = p + strlen(p);
805 b_present |= !strcmp( p, psz_name );
806 ppsz_filter[i_count++] = p;
810 if( b_present == b_add )
819 int i_order = FilterOrder( psz_name );
821 for( i = 0; i < i_count; i++ )
823 if( FilterOrder( ppsz_filter[i] ) > i_order )
827 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
828 ppsz_filter[i] = psz_name;
833 for( int i = 0; i < i_count; i++ )
835 if( !strcmp( ppsz_filter[i], psz_name ) )
840 for( int i = 0; i < i_count; i++ )
841 i_length += 1 + strlen( ppsz_filter[i] );
843 char *psz_new = malloc( i_length + 1 );
845 for( int i = 0; i < i_count; i++ )
847 if( *ppsz_filter[i] == '\0' )
850 strcat( psz_new, ":" );
851 strcat( psz_new, ppsz_filter[i] );
857 var_SetString( p_aout, psz_variable, psz_new );
859 config_PutPsz( p_obj, psz_variable, psz_new );