]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
Remove object type field
[vlc] / src / audio_output / common.c
1 /*****************************************************************************
2  * common.c : audio output management of common data structures
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <limits.h>
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_aout.h>
36 #include "aout_internal.h"
37 #include "libvlc.h"
38
39 /*
40  * Instances management (internal and external)
41  */
42
43 /* Local functions */
44 static void aout_Destructor( vlc_object_t * p_this );
45
46 /*****************************************************************************
47  * aout_New: initialize aout structure
48  *****************************************************************************/
49 aout_instance_t * __aout_New( vlc_object_t * p_parent )
50 {
51     aout_instance_t * p_aout;
52
53     /* Allocate descriptor. */
54     p_aout = vlc_custom_create( p_parent, sizeof( *p_aout ), "audio output" );
55     if( p_aout == NULL )
56     {
57         return NULL;
58     }
59
60     /* Initialize members. */
61     vlc_mutex_init( &p_aout->volume_lock );
62     vlc_mutex_init( &p_aout->lock );
63     p_aout->p_input = NULL;
64     p_aout->mixer_multiplier = 1.0;
65     p_aout->p_mixer = NULL;
66     p_aout->output.b_starving = 1;
67     p_aout->output.p_module = NULL;
68
69     var_Create( p_aout, "intf-change", VLC_VAR_VOID );
70
71     vlc_object_set_destructor( p_aout, aout_Destructor );
72
73     return p_aout;
74 }
75
76 /*****************************************************************************
77  * aout_Destructor: destroy aout structure
78  *****************************************************************************/
79 static void aout_Destructor( vlc_object_t * p_this )
80 {
81     aout_instance_t * p_aout = (aout_instance_t *)p_this;
82     vlc_mutex_destroy( &p_aout->volume_lock );
83     vlc_mutex_destroy( &p_aout->lock );
84 }
85
86 #ifdef AOUT_DEBUG
87 /* Lock debugging */
88 static __thread unsigned aout_locks = 0;
89
90 void aout_lock_check (unsigned i)
91 {
92     unsigned allowed;
93     switch (i)
94     {
95         case VOLUME_LOCK:
96             allowed = 0;
97             break;
98         case OUTPUT_LOCK:
99             allowed = VOLUME_LOCK;
100             break;
101         default:
102             abort ();
103     }
104
105     if (aout_locks & ~allowed)
106     {
107         fprintf (stderr, "Illegal audio lock transition (%x -> %x)\n",
108                  aout_locks, aout_locks|i);
109         vlc_backtrace ();
110         abort ();
111     }
112     aout_locks |= i;
113 }
114
115 void aout_unlock_check (unsigned i)
116 {
117     assert (aout_locks & i);
118     aout_locks &= ~i;
119 }
120 #endif
121
122 /*
123  * Formats management (internal and external)
124  */
125
126 /*****************************************************************************
127  * aout_BitsPerSample : get the number of bits per sample
128  *****************************************************************************/
129 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
130 {
131     switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
132     {
133     case VLC_CODEC_U8:
134     case VLC_CODEC_S8:
135         return 8;
136
137     case VLC_CODEC_U16L:
138     case VLC_CODEC_S16L:
139     case VLC_CODEC_U16B:
140     case VLC_CODEC_S16B:
141         return 16;
142
143     case VLC_CODEC_U24L:
144     case VLC_CODEC_S24L:
145     case VLC_CODEC_U24B:
146     case VLC_CODEC_S24B:
147         return 24;
148
149     case VLC_CODEC_S32L:
150     case VLC_CODEC_S32B:
151     case VLC_CODEC_F32L:
152     case VLC_CODEC_F32B:
153     case VLC_CODEC_FI32:
154         return 32;
155
156     case VLC_CODEC_F64L:
157     case VLC_CODEC_F64B:
158         return 64;
159
160     default:
161         /* For these formats the caller has to indicate the parameters
162          * by hand. */
163         return 0;
164     }
165 }
166
167 /*****************************************************************************
168  * aout_FormatPrepare : compute the number of bytes per frame & frame length
169  *****************************************************************************/
170 void aout_FormatPrepare( audio_sample_format_t * p_format )
171 {
172     p_format->i_channels = aout_FormatNbChannels( p_format );
173     p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
174     if( p_format->i_bitspersample > 0 )
175     {
176         p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
177                                     * aout_FormatNbChannels( p_format );
178         p_format->i_frame_length = 1;
179     }
180 }
181
182 /*****************************************************************************
183  * aout_FormatPrintChannels : print a channel in a human-readable form
184  *****************************************************************************/
185 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
186 {
187     switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
188     {
189     case AOUT_CHAN_LEFT:
190     case AOUT_CHAN_RIGHT:
191     case AOUT_CHAN_CENTER:
192         if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
193               || (p_format->i_original_channels
194                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
195             return "Mono";
196         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
197             return "Left";
198         return "Right";
199     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
200         if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
201         {
202             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
203                 return "Dolby/Reverse";
204             return "Stereo/Reverse";
205         }
206         else
207         {
208             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
209                 return "Dolby";
210             else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
211                 return "Dual-mono";
212             else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
213                 return "Stereo/Mono";
214             else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
215                 return "Stereo/Left";
216             else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
217                 return "Stereo/Right";
218             return "Stereo";
219         }
220     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
221         return "3F";
222     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
223         return "2F1R";
224     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
225           | AOUT_CHAN_REARCENTER:
226         return "3F1R";
227     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
228           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
229         return "2F2R";
230     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
231           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
232         return "2F2M";
233     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
234           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
235         return "3F2R";
236     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
237           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
238         return "3F2M";
239
240     case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
241         if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
242               || (p_format->i_original_channels
243                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
244             return "Mono/LFE";
245         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
246             return "Left/LFE";
247         return "Right/LFE";
248     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
249         if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
250             return "Dolby/LFE";
251         else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
252             return "Dual-mono/LFE";
253         else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
254             return "Mono/LFE";
255         else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
256             return "Stereo/Left/LFE";
257         else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
258             return "Stereo/Right/LFE";
259          return "Stereo/LFE";
260     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
261         return "3F/LFE";
262     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
263           | AOUT_CHAN_LFE:
264         return "2F1R/LFE";
265     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
266           | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
267         return "3F1R/LFE";
268     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
269           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
270         return "2F2R/LFE";
271     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
272           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
273         return "2F2M/LFE";
274     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
275           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
276         return "3F2R/LFE";
277     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
278           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
279         return "3F2M/LFE";
280     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
281           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
282           | AOUT_CHAN_MIDDLERIGHT:
283         return "3F2M2R";
284     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
285           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
286           | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
287         return "3F2M2R/LFE";
288     }
289
290     return "ERROR";
291 }
292
293 /*****************************************************************************
294  * aout_FormatPrint : print a format in a human-readable form
295  *****************************************************************************/
296 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
297                        const audio_sample_format_t * p_format )
298 {
299     msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
300              (char *)&p_format->i_format, p_format->i_rate,
301              aout_FormatPrintChannels( p_format ),
302              p_format->i_frame_length, p_format->i_bytes_per_frame );
303 }
304
305 /*****************************************************************************
306  * aout_FormatsPrint : print two formats in a human-readable form
307  *****************************************************************************/
308 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
309                         const audio_sample_format_t * p_format1,
310                         const audio_sample_format_t * p_format2 )
311 {
312     msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
313              psz_text,
314              (char *)&p_format1->i_format, (char *)&p_format2->i_format,
315              p_format1->i_rate, p_format2->i_rate,
316              aout_FormatPrintChannels( p_format1 ),
317              aout_FormatPrintChannels( p_format2 ) );
318 }
319
320
321 /*
322  * FIFO management (internal) - please understand that solving race conditions
323  * is _your_ job, ie. in the audio output you should own the mixer lock
324  * before calling any of these functions.
325  */
326
327 #undef aout_FifoInit
328 /*****************************************************************************
329  * aout_FifoInit : initialize the members of a FIFO
330  *****************************************************************************/
331 void aout_FifoInit( vlc_object_t *obj, aout_fifo_t * p_fifo, uint32_t i_rate )
332 {
333     if( unlikely(i_rate == 0) )
334         msg_Err( obj, "initialising fifo with zero divider" );
335
336     p_fifo->p_first = NULL;
337     p_fifo->pp_last = &p_fifo->p_first;
338     date_Init( &p_fifo->end_date, i_rate, 1 );
339 }
340
341 /*****************************************************************************
342  * aout_FifoPush : push a packet into the FIFO
343  *****************************************************************************/
344 void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
345 {
346     *p_fifo->pp_last = p_buffer;
347     p_fifo->pp_last = &p_buffer->p_next;
348     *p_fifo->pp_last = NULL;
349     /* Enforce the continuity of the stream. */
350     if ( date_Get( &p_fifo->end_date ) )
351     {
352         p_buffer->i_pts = date_Get( &p_fifo->end_date );
353         p_buffer->i_length = date_Increment( &p_fifo->end_date,
354                                              p_buffer->i_nb_samples );
355         p_buffer->i_length -= p_buffer->i_pts;
356     }
357     else
358     {
359         date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
360     }
361 }
362
363 /*****************************************************************************
364  * aout_FifoSet : set end_date and trash all buffers (because they aren't
365  * properly dated)
366  *****************************************************************************/
367 void aout_FifoSet( aout_fifo_t * p_fifo, mtime_t date )
368 {
369     aout_buffer_t * p_buffer;
370
371     date_Set( &p_fifo->end_date, date );
372     p_buffer = p_fifo->p_first;
373     while ( p_buffer != NULL )
374     {
375         aout_buffer_t * p_next = p_buffer->p_next;
376         aout_BufferFree( p_buffer );
377         p_buffer = p_next;
378     }
379     p_fifo->p_first = NULL;
380     p_fifo->pp_last = &p_fifo->p_first;
381 }
382
383 /*****************************************************************************
384  * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
385  *****************************************************************************/
386 void aout_FifoMoveDates( aout_fifo_t * p_fifo, mtime_t difference )
387 {
388     aout_buffer_t * p_buffer;
389
390     date_Move( &p_fifo->end_date, difference );
391     p_buffer = p_fifo->p_first;
392     while ( p_buffer != NULL )
393     {
394         p_buffer->i_pts += difference;
395         p_buffer = p_buffer->p_next;
396     }
397 }
398
399 /*****************************************************************************
400  * aout_FifoNextStart : return the current end_date
401  *****************************************************************************/
402 mtime_t aout_FifoNextStart( const aout_fifo_t *p_fifo )
403 {
404     return date_Get( &p_fifo->end_date );
405 }
406
407 /*****************************************************************************
408  * aout_FifoFirstDate : return the playing date of the first buffer in the
409  * FIFO
410  *****************************************************************************/
411 mtime_t aout_FifoFirstDate( const aout_fifo_t *p_fifo )
412 {
413     return (p_fifo->p_first != NULL) ? p_fifo->p_first->i_pts : 0;
414 }
415
416 /*****************************************************************************
417  * aout_FifoPop : get the next buffer out of the FIFO
418  *****************************************************************************/
419 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
420 {
421     aout_buffer_t *p_buffer = p_fifo->p_first;
422     if( p_buffer != NULL )
423     {
424         p_fifo->p_first = p_buffer->p_next;
425         if( p_fifo->p_first == NULL )
426             p_fifo->pp_last = &p_fifo->p_first;
427     }
428     return p_buffer;
429 }
430
431 /*****************************************************************************
432  * aout_FifoDestroy : destroy a FIFO and its buffers
433  *****************************************************************************/
434 void aout_FifoDestroy( aout_fifo_t * p_fifo )
435 {
436     aout_buffer_t * p_buffer;
437
438     p_buffer = p_fifo->p_first;
439     while ( p_buffer != NULL )
440     {
441         aout_buffer_t * p_next = p_buffer->p_next;
442         aout_BufferFree( p_buffer );
443         p_buffer = p_next;
444     }
445
446     p_fifo->p_first = NULL;
447     p_fifo->pp_last = &p_fifo->p_first;
448 }
449
450 /*****************************************************************************
451  * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
452  *****************************************************************************/
453 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
454                               const uint32_t *pi_chan_order_out,
455                               uint32_t i_channel_mask,
456                               int i_channels, int *pi_chan_table )
457 {
458     bool b_chan_reorder = false;
459     int i, j, k, l;
460
461     if( i_channels > AOUT_CHAN_MAX )
462         return false;
463
464     if( pi_chan_order_in == NULL )
465         pi_chan_order_in = pi_vlc_chan_order_wg4;
466     if( pi_chan_order_out == NULL )
467         pi_chan_order_out = pi_vlc_chan_order_wg4;
468
469     for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
470     {
471         if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
472
473         for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
474         {
475             if( i_channel_mask & pi_chan_order_out[k] ) l++;
476         }
477
478         pi_chan_table[j++] = l;
479     }
480
481     for( i = 0; i < i_channels; i++ )
482     {
483         if( pi_chan_table[i] != i ) b_chan_reorder = true;
484     }
485
486     return b_chan_reorder;
487 }
488
489 /*****************************************************************************
490  * aout_ChannelReorder :
491  *****************************************************************************/
492 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
493                           int i_channels, const int *pi_chan_table,
494                           int i_bits_per_sample )
495 {
496     uint8_t p_tmp[AOUT_CHAN_MAX * 4];
497     int i, j;
498
499     if( i_bits_per_sample == 8 )
500     {
501         for( i = 0; i < i_buffer / i_channels; i++ )
502         {
503             for( j = 0; j < i_channels; j++ )
504             {
505                 p_tmp[pi_chan_table[j]] = p_buf[j];
506             }
507
508             memcpy( p_buf, p_tmp, i_channels );
509             p_buf += i_channels;
510         }
511     }
512     else if( i_bits_per_sample == 16 )
513     {
514         for( i = 0; i < i_buffer / i_channels / 2; i++ )
515         {
516             for( j = 0; j < i_channels; j++ )
517             {
518                 p_tmp[2 * pi_chan_table[j]]     = p_buf[2 * j];
519                 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
520             }
521
522             memcpy( p_buf, p_tmp, 2 * i_channels );
523             p_buf += 2 * i_channels;
524         }
525     }
526     else if( i_bits_per_sample == 24 )
527     {
528         for( i = 0; i < i_buffer / i_channels / 3; i++ )
529         {
530             for( j = 0; j < i_channels; j++ )
531             {
532                 p_tmp[3 * pi_chan_table[j]]     = p_buf[3 * j];
533                 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
534                 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
535             }
536
537             memcpy( p_buf, p_tmp, 3 * i_channels );
538             p_buf += 3 * i_channels;
539         }
540     }
541     else if( i_bits_per_sample == 32 )
542     {
543         for( i = 0; i < i_buffer / i_channels / 4; i++ )
544         {
545             for( j = 0; j < i_channels; j++ )
546             {
547                 p_tmp[4 * pi_chan_table[j]]     = p_buf[4 * j];
548                 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
549                 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
550                 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
551             }
552
553             memcpy( p_buf, p_tmp, 4 * i_channels );
554             p_buf += 4 * i_channels;
555         }
556     }
557 }
558
559 /*****************************************************************************
560  * aout_ChannelExtract:
561  *****************************************************************************/
562 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
563                                    const uint8_t *pi_src, int i_src_channels,
564                                    int i_sample_count,
565                                    const int *pi_selection, int i_bytes )
566 {
567     for( int i = 0; i < i_sample_count; i++ )
568     {
569         for( int j = 0; j < i_dst_channels; j++ )
570             memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
571         pi_dst += i_dst_channels * i_bytes;
572         pi_src += i_src_channels * i_bytes;
573     }
574 }
575
576 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
577                           const void *p_src, int i_src_channels,
578                           int i_sample_count, const int *pi_selection, int i_bits_per_sample )
579 {
580     /* It does not work in place */
581     assert( p_dst != p_src );
582
583     /* Force the compiler to inline for the specific cases so it can optimize */
584     if( i_bits_per_sample == 8 )
585         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
586     else  if( i_bits_per_sample == 16 )
587         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
588     else  if( i_bits_per_sample == 24 )
589         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
590     else  if( i_bits_per_sample == 32 )
591         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
592     else  if( i_bits_per_sample == 64 )
593         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
594 }
595
596 bool aout_CheckChannelExtraction( int *pi_selection,
597                                   uint32_t *pi_layout, int *pi_channels,
598                                   const uint32_t pi_order_dst[AOUT_CHAN_MAX],
599                                   const uint32_t *pi_order_src, int i_channels )
600 {
601     const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
602     uint32_t i_layout = 0;
603     int i_out = 0;
604     int pi_index[AOUT_CHAN_MAX];
605
606     /* */
607     if( !pi_order_dst )
608         pi_order_dst = pi_vlc_chan_order_wg4;
609
610     /* Detect special dual mono case */
611     if( i_channels == 2 &&
612         pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
613     {
614         i_layout |= AOUT_CHAN_DUALMONO;
615         pi_order_src = pi_order_dual_mono;
616     }
617
618     /* */
619     for( int i = 0; i < i_channels; i++ )
620     {
621         /* Ignore unknown or duplicated channels or not present in output */
622         if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
623             continue;
624
625         for( int j = 0; j < AOUT_CHAN_MAX; j++ )
626         {
627             if( pi_order_dst[j] == pi_order_src[i] )
628             {
629                 assert( i_out < AOUT_CHAN_MAX );
630                 pi_index[i_out++] = i;
631                 i_layout |= pi_order_src[i];
632                 break;
633             }
634         }
635     }
636
637     /* */
638     for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
639     {
640         for( int k = 0; k < i_out; k++ )
641         {
642             if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
643             {
644                 pi_selection[j++] = pi_index[k];
645                 break;
646             }
647         }
648     }
649
650     *pi_layout = i_layout;
651     *pi_channels = i_out;
652
653     for( int i = 0; i < i_out; i++ )
654     {
655         if( pi_selection[i] != i )
656             return true;
657     }
658     return i_out == i_channels;
659 }
660
661 /* Return the order in which filters should be inserted */
662 static int FilterOrder( const char *psz_name )
663 {
664     static const struct {
665         const char *psz_name;
666         int        i_order;
667     } filter[] = {
668         { "equalizer",  0 },
669         { NULL,         INT_MAX },
670     };
671     for( int i = 0; filter[i].psz_name; i++ )
672     {
673         if( !strcmp( filter[i].psz_name, psz_name ) )
674             return filter[i].i_order;
675     }
676     return INT_MAX;
677 }
678
679 /* This function will add or remove a a module from a string list (colon
680  * separated). It will return true if there is a modification
681  * In case p_aout is NULL, we will use configuration instead of variable */
682 bool aout_ChangeFilterString( vlc_object_t *p_obj, aout_instance_t *p_aout,
683                               const char *psz_variable,
684                               const char *psz_name, bool b_add )
685 {
686     if( *psz_name == '\0' )
687         return false;
688
689     char *psz_list;
690     if( p_aout )
691     {
692         psz_list = var_GetString( p_aout, psz_variable );
693     }
694     else
695     {
696         psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
697         var_Destroy( p_obj->p_libvlc, psz_variable );
698     }
699
700     /* Split the string into an array of filters */
701     int i_count = 1;
702     for( char *p = psz_list; p && *p; p++ )
703         i_count += *p == ':';
704     i_count += b_add;
705
706     const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
707     if( !ppsz_filter )
708     {
709         free( psz_list );
710         return false;
711     }
712     bool b_present = false;
713     i_count = 0;
714     for( char *p = psz_list; p && *p; )
715     {
716         char *psz_end = strchr(p, ':');
717         if( psz_end )
718             *psz_end++ = '\0';
719         else
720             psz_end = p + strlen(p);
721         if( *p )
722         {
723             b_present |= !strcmp( p, psz_name );
724             ppsz_filter[i_count++] = p;
725         }
726         p = psz_end;
727     }
728     if( b_present == b_add )
729     {
730         free( ppsz_filter );
731         free( psz_list );
732         return false;
733     }
734
735     if( b_add )
736     {
737         int i_order = FilterOrder( psz_name );
738         int i;
739         for( i = 0; i < i_count; i++ )
740         {
741             if( FilterOrder( ppsz_filter[i] ) > i_order )
742                 break;
743         }
744         if( i < i_count )
745             memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
746         ppsz_filter[i] = psz_name;
747         i_count++;
748     }
749     else
750     {
751         for( int i = 0; i < i_count; i++ )
752         {
753             if( !strcmp( ppsz_filter[i], psz_name ) )
754                 ppsz_filter[i] = "";
755         }
756     }
757     size_t i_length = 0;
758     for( int i = 0; i < i_count; i++ )
759         i_length += 1 + strlen( ppsz_filter[i] );
760
761     char *psz_new = malloc( i_length + 1 );
762     *psz_new = '\0';
763     for( int i = 0; i < i_count; i++ )
764     {
765         if( *ppsz_filter[i] == '\0' )
766             continue;
767         if( *psz_new )
768             strcat( psz_new, ":" );
769         strcat( psz_new, ppsz_filter[i] );
770     }
771     free( ppsz_filter );
772     free( psz_list );
773
774     if( p_aout )
775         var_SetString( p_aout, psz_variable, psz_new );
776     else
777         config_PutPsz( p_obj, psz_variable, psz_new );
778     free( psz_new );
779
780     return true;
781 }
782
783
784