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)
43 #define AOUT_ASSERT_FIFO_LOCKED aout_assert_fifo_locked(p_aout, p_fifo)
44 static inline void aout_assert_fifo_locked( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
50 if( p_fifo == &p_aout->output.fifo )
51 vlc_assert_locked( &p_aout->output_fifo_lock );
53 if( p_aout->p_input != NULL
54 && p_fifo == &p_aout->p_input->mixer.fifo )
55 vlc_assert_locked( &p_aout->input_fifos_lock );
57 vlc_assert_locked( &p_aout->mixer_lock );
65 static void aout_Destructor( vlc_object_t * p_this );
67 /*****************************************************************************
68 * aout_New: initialize aout structure
69 *****************************************************************************/
70 aout_instance_t * __aout_New( vlc_object_t * p_parent )
72 aout_instance_t * p_aout;
74 /* Allocate descriptor. */
75 p_aout = vlc_custom_create( p_parent, sizeof( *p_aout ), VLC_OBJECT_AOUT,
82 /* Initialize members. */
83 vlc_mutex_init( &p_aout->input_fifos_lock );
84 vlc_mutex_init( &p_aout->mixer_lock );
85 vlc_mutex_init( &p_aout->volume_vars_lock );
86 vlc_mutex_init( &p_aout->output_fifo_lock );
87 p_aout->p_input = NULL;
88 p_aout->mixer_multiplier = 1.0;
89 p_aout->p_mixer = NULL;
90 p_aout->output.b_starving = 1;
91 p_aout->output.p_module = NULL;
93 var_Create( p_aout, "intf-change", VLC_VAR_VOID );
95 vlc_object_set_destructor( p_aout, aout_Destructor );
100 /*****************************************************************************
101 * aout_Destructor: destroy aout structure
102 *****************************************************************************/
103 static void aout_Destructor( vlc_object_t * p_this )
105 aout_instance_t * p_aout = (aout_instance_t *)p_this;
106 vlc_mutex_destroy( &p_aout->input_fifos_lock );
107 vlc_mutex_destroy( &p_aout->mixer_lock );
108 vlc_mutex_destroy( &p_aout->volume_vars_lock );
109 vlc_mutex_destroy( &p_aout->output_fifo_lock );
112 /* Lock ordering rules:
114 * Vars Mixer Input IFIFO OFIFO (< Inner lock)
115 * Vars No! Yes Yes Yes Yes
116 * Mixer No! No! Yes Yes Yes
117 * Input No! No! No! Yes Yes
118 * In FIFOs No! No! No! No! Yes
119 * Out FIFOs No! No! No! No! No!
124 static __thread unsigned aout_locks = 0;
126 void aout_lock (unsigned i)
131 case VOLUME_VARS_LOCK:
135 allowed = VOLUME_VARS_LOCK;
138 allowed = VOLUME_VARS_LOCK|MIXER_LOCK;
140 case INPUT_FIFO_LOCK:
141 allowed = VOLUME_VARS_LOCK|MIXER_LOCK|INPUT_LOCK;
143 case OUTPUT_FIFO_LOCK:
144 allowed = VOLUME_VARS_LOCK|MIXER_LOCK|INPUT_LOCK|INPUT_FIFO_LOCK;
150 if (aout_locks & ~allowed)
152 fprintf (stderr, "Illegal audio lock transition (%x -> %x)\n",
153 aout_locks, aout_locks|i);
160 void aout_unlock (unsigned i)
162 assert (aout_locks & i);
168 * Formats management (internal and external)
171 /*****************************************************************************
172 * aout_BitsPerSample : get the number of bits per sample
173 *****************************************************************************/
174 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
176 switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
206 /* For these formats the caller has to indicate the parameters
212 /*****************************************************************************
213 * aout_FormatPrepare : compute the number of bytes per frame & frame length
214 *****************************************************************************/
215 void aout_FormatPrepare( audio_sample_format_t * p_format )
217 p_format->i_channels = aout_FormatNbChannels( p_format );
218 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
219 if( p_format->i_bitspersample > 0 )
221 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
222 * aout_FormatNbChannels( p_format );
223 p_format->i_frame_length = 1;
227 /*****************************************************************************
228 * aout_FormatPrintChannels : print a channel in a human-readable form
229 *****************************************************************************/
230 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
232 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
235 case AOUT_CHAN_RIGHT:
236 case AOUT_CHAN_CENTER:
237 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
238 || (p_format->i_original_channels
239 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
241 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
244 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
245 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
247 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
248 return "Dolby/Reverse";
249 return "Stereo/Reverse";
253 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
255 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
257 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
258 return "Stereo/Mono";
259 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
260 return "Stereo/Left";
261 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
262 return "Stereo/Right";
265 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
267 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
269 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
270 | AOUT_CHAN_REARCENTER:
272 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
273 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
275 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
276 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
278 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
279 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
281 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
282 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
285 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
286 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
287 || (p_format->i_original_channels
288 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
290 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
293 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
294 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
296 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
297 return "Dual-mono/LFE";
298 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
300 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
301 return "Stereo/Left/LFE";
302 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
303 return "Stereo/Right/LFE";
305 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
307 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
310 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
311 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
313 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
314 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
316 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
317 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
319 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
320 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
322 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
323 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
325 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
326 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
327 | AOUT_CHAN_MIDDLERIGHT:
329 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
330 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
331 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
338 /*****************************************************************************
339 * aout_FormatPrint : print a format in a human-readable form
340 *****************************************************************************/
341 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
342 const audio_sample_format_t * p_format )
344 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
345 (char *)&p_format->i_format, p_format->i_rate,
346 aout_FormatPrintChannels( p_format ),
347 p_format->i_frame_length, p_format->i_bytes_per_frame );
350 /*****************************************************************************
351 * aout_FormatsPrint : print two formats in a human-readable form
352 *****************************************************************************/
353 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
354 const audio_sample_format_t * p_format1,
355 const audio_sample_format_t * p_format2 )
357 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
359 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
360 p_format1->i_rate, p_format2->i_rate,
361 aout_FormatPrintChannels( p_format1 ),
362 aout_FormatPrintChannels( p_format2 ) );
367 * FIFO management (internal) - please understand that solving race conditions
368 * is _your_ job, ie. in the audio output you should own the mixer lock
369 * before calling any of these functions.
372 /*****************************************************************************
373 * aout_FifoInit : initialize the members of a FIFO
374 *****************************************************************************/
375 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
378 AOUT_ASSERT_FIFO_LOCKED;
382 msg_Err( p_aout, "initialising fifo with zero divider" );
385 p_fifo->p_first = NULL;
386 p_fifo->pp_last = &p_fifo->p_first;
387 date_Init( &p_fifo->end_date, i_rate, 1 );
390 /*****************************************************************************
391 * aout_FifoPush : push a packet into the FIFO
392 *****************************************************************************/
393 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
394 aout_buffer_t * p_buffer )
397 AOUT_ASSERT_FIFO_LOCKED;
399 *p_fifo->pp_last = p_buffer;
400 p_fifo->pp_last = &p_buffer->p_next;
401 *p_fifo->pp_last = NULL;
402 /* Enforce the continuity of the stream. */
403 if ( date_Get( &p_fifo->end_date ) )
405 p_buffer->i_pts = date_Get( &p_fifo->end_date );
406 p_buffer->i_length = date_Increment( &p_fifo->end_date,
407 p_buffer->i_nb_samples );
408 p_buffer->i_length -= p_buffer->i_pts;
412 date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
416 /*****************************************************************************
417 * aout_FifoSet : set end_date and trash all buffers (because they aren't
419 *****************************************************************************/
420 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
423 aout_buffer_t * p_buffer;
425 AOUT_ASSERT_FIFO_LOCKED;
427 date_Set( &p_fifo->end_date, date );
428 p_buffer = p_fifo->p_first;
429 while ( p_buffer != NULL )
431 aout_buffer_t * p_next = p_buffer->p_next;
432 aout_BufferFree( p_buffer );
435 p_fifo->p_first = NULL;
436 p_fifo->pp_last = &p_fifo->p_first;
439 /*****************************************************************************
440 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
441 *****************************************************************************/
442 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
445 aout_buffer_t * p_buffer;
447 AOUT_ASSERT_FIFO_LOCKED;
449 date_Move( &p_fifo->end_date, difference );
450 p_buffer = p_fifo->p_first;
451 while ( p_buffer != NULL )
453 p_buffer->i_pts += difference;
454 p_buffer = p_buffer->p_next;
458 /*****************************************************************************
459 * aout_FifoNextStart : return the current end_date
460 *****************************************************************************/
461 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
464 AOUT_ASSERT_FIFO_LOCKED;
465 return date_Get( &p_fifo->end_date );
468 /*****************************************************************************
469 * aout_FifoFirstDate : return the playing date of the first buffer in the
471 *****************************************************************************/
472 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
475 AOUT_ASSERT_FIFO_LOCKED;
476 return p_fifo->p_first ? p_fifo->p_first->i_pts : 0;
479 /*****************************************************************************
480 * aout_FifoPop : get the next buffer out of the FIFO
481 *****************************************************************************/
482 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
484 aout_buffer_t * p_buffer;
486 AOUT_ASSERT_FIFO_LOCKED;
488 p_buffer = p_fifo->p_first;
489 if ( p_buffer == NULL ) return NULL;
490 p_fifo->p_first = p_buffer->p_next;
491 if ( p_fifo->p_first == NULL )
493 p_fifo->pp_last = &p_fifo->p_first;
499 /*****************************************************************************
500 * aout_FifoDestroy : destroy a FIFO and its buffers
501 *****************************************************************************/
502 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
504 aout_buffer_t * p_buffer;
506 AOUT_ASSERT_FIFO_LOCKED;
508 p_buffer = p_fifo->p_first;
509 while ( p_buffer != NULL )
511 aout_buffer_t * p_next = p_buffer->p_next;
512 aout_BufferFree( p_buffer );
516 p_fifo->p_first = NULL;
517 p_fifo->pp_last = &p_fifo->p_first;
520 /*****************************************************************************
521 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
522 *****************************************************************************/
523 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
524 const uint32_t *pi_chan_order_out,
525 uint32_t i_channel_mask,
526 int i_channels, int *pi_chan_table )
528 bool b_chan_reorder = false;
531 if( i_channels > AOUT_CHAN_MAX )
534 if( pi_chan_order_in == NULL )
535 pi_chan_order_in = pi_vlc_chan_order_wg4;
536 if( pi_chan_order_out == NULL )
537 pi_chan_order_out = pi_vlc_chan_order_wg4;
539 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
541 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
543 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
545 if( i_channel_mask & pi_chan_order_out[k] ) l++;
548 pi_chan_table[j++] = l;
551 for( i = 0; i < i_channels; i++ )
553 if( pi_chan_table[i] != i ) b_chan_reorder = true;
556 return b_chan_reorder;
559 /*****************************************************************************
560 * aout_ChannelReorder :
561 *****************************************************************************/
562 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
563 int i_channels, const int *pi_chan_table,
564 int i_bits_per_sample )
566 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
569 if( i_bits_per_sample == 8 )
571 for( i = 0; i < i_buffer / i_channels; i++ )
573 for( j = 0; j < i_channels; j++ )
575 p_tmp[pi_chan_table[j]] = p_buf[j];
578 memcpy( p_buf, p_tmp, i_channels );
582 else if( i_bits_per_sample == 16 )
584 for( i = 0; i < i_buffer / i_channels / 2; i++ )
586 for( j = 0; j < i_channels; j++ )
588 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
589 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
592 memcpy( p_buf, p_tmp, 2 * i_channels );
593 p_buf += 2 * i_channels;
596 else if( i_bits_per_sample == 24 )
598 for( i = 0; i < i_buffer / i_channels / 3; i++ )
600 for( j = 0; j < i_channels; j++ )
602 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
603 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
604 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
607 memcpy( p_buf, p_tmp, 3 * i_channels );
608 p_buf += 3 * i_channels;
611 else if( i_bits_per_sample == 32 )
613 for( i = 0; i < i_buffer / i_channels / 4; i++ )
615 for( j = 0; j < i_channels; j++ )
617 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
618 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
619 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
620 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
623 memcpy( p_buf, p_tmp, 4 * i_channels );
624 p_buf += 4 * i_channels;
629 /*****************************************************************************
630 * aout_ChannelExtract:
631 *****************************************************************************/
632 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
633 const uint8_t *pi_src, int i_src_channels,
635 const int *pi_selection, int i_bytes )
637 for( int i = 0; i < i_sample_count; i++ )
639 for( int j = 0; j < i_dst_channels; j++ )
640 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
641 pi_dst += i_dst_channels * i_bytes;
642 pi_src += i_src_channels * i_bytes;
646 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
647 const void *p_src, int i_src_channels,
648 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
650 /* It does not work in place */
651 assert( p_dst != p_src );
653 /* Force the compiler to inline for the specific cases so it can optimize */
654 if( i_bits_per_sample == 8 )
655 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
656 else if( i_bits_per_sample == 16 )
657 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
658 else if( i_bits_per_sample == 24 )
659 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
660 else if( i_bits_per_sample == 32 )
661 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
662 else if( i_bits_per_sample == 64 )
663 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
666 bool aout_CheckChannelExtraction( int *pi_selection,
667 uint32_t *pi_layout, int *pi_channels,
668 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
669 const uint32_t *pi_order_src, int i_channels )
671 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
672 uint32_t i_layout = 0;
674 int pi_index[AOUT_CHAN_MAX];
678 pi_order_dst = pi_vlc_chan_order_wg4;
680 /* Detect special dual mono case */
681 if( i_channels == 2 &&
682 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
684 i_layout |= AOUT_CHAN_DUALMONO;
685 pi_order_src = pi_order_dual_mono;
689 for( int i = 0; i < i_channels; i++ )
691 /* Ignore unknown or duplicated channels or not present in output */
692 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
695 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
697 if( pi_order_dst[j] == pi_order_src[i] )
699 assert( i_out < AOUT_CHAN_MAX );
700 pi_index[i_out++] = i;
701 i_layout |= pi_order_src[i];
708 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
710 for( int k = 0; k < i_out; k++ )
712 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
714 pi_selection[j++] = pi_index[k];
720 *pi_layout = i_layout;
721 *pi_channels = i_out;
723 for( int i = 0; i < i_out; i++ )
725 if( pi_selection[i] != i )
728 return i_out == i_channels;
731 /* Return the order in which filters should be inserted */
732 static int FilterOrder( const char *psz_name )
734 static const struct {
735 const char *psz_name;
741 for( int i = 0; filter[i].psz_name; i++ )
743 if( !strcmp( filter[i].psz_name, psz_name ) )
744 return filter[i].i_order;
749 /* This function will add or remove a a module from a string list (colon
750 * separated). It will return true if there is a modification
751 * In case p_aout is NULL, we will use configuration instead of variable */
752 bool aout_ChangeFilterString( vlc_object_t *p_obj, aout_instance_t *p_aout,
753 const char *psz_variable,
754 const char *psz_name, bool b_add )
756 if( *psz_name == '\0' )
762 psz_list = var_GetString( p_aout, psz_variable );
766 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
767 var_Destroy( p_obj->p_libvlc, psz_variable );
770 /* Split the string into an array of filters */
772 for( char *p = psz_list; p && *p; p++ )
773 i_count += *p == ':';
776 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
782 bool b_present = false;
784 for( char *p = psz_list; p && *p; )
786 char *psz_end = strchr(p, ':');
790 psz_end = p + strlen(p);
793 b_present |= !strcmp( p, psz_name );
794 ppsz_filter[i_count++] = p;
798 if( b_present == b_add )
807 int i_order = FilterOrder( psz_name );
809 for( i = 0; i < i_count; i++ )
811 if( FilterOrder( ppsz_filter[i] ) > i_order )
815 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
816 ppsz_filter[i] = psz_name;
821 for( int i = 0; i < i_count; i++ )
823 if( !strcmp( ppsz_filter[i], psz_name ) )
828 for( int i = 0; i < i_count; i++ )
829 i_length += 1 + strlen( ppsz_filter[i] );
831 char *psz_new = malloc( i_length + 1 );
833 for( int i = 0; i < i_count; i++ )
835 if( *ppsz_filter[i] == '\0' )
838 strcat( psz_new, ":" );
839 strcat( psz_new, ppsz_filter[i] );
845 var_SetString( p_aout, psz_variable, psz_new );
847 config_PutPsz( p_obj, psz_variable, psz_new );