]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
190b7fb732721365d10e3a5d146d8b6fbdca9567
[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 #undef aout_New
47 /*****************************************************************************
48  * aout_New: initialize aout structure
49  *****************************************************************************/
50 audio_output_t *aout_New( vlc_object_t * p_parent )
51 {
52     audio_output_t *aout = vlc_custom_create (p_parent,
53                                               sizeof (aout_instance_t),
54                                               "audio output");
55     if (unlikely(aout == NULL))
56         return NULL;
57
58     aout_owner_t *owner = aout_owner (aout);
59
60     vlc_mutex_init (&owner->lock);
61     owner->module = NULL;
62     owner->input = NULL;
63     vlc_mutex_init (&owner->volume.lock);
64     owner->volume.multiplier = 1.0;
65     owner->volume.mixer = NULL;
66
67     aout_VolumeNoneInit (aout);
68     vlc_object_set_destructor (aout, aout_Destructor);
69     var_Create (aout, "intf-change", VLC_VAR_VOID);
70
71     return aout;
72 }
73
74 /*****************************************************************************
75  * aout_Destructor: destroy aout structure
76  *****************************************************************************/
77 static void aout_Destructor (vlc_object_t *obj)
78 {
79     audio_output_t *aout = (audio_output_t *)obj;
80     aout_owner_t *owner = aout_owner (aout);
81
82     vlc_mutex_destroy (&owner->volume.lock);
83     vlc_mutex_destroy (&owner->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( audio_output_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( audio_output_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     date_Set( &p_fifo->end_date, VLC_TS_INVALID );
340 }
341
342 /*****************************************************************************
343  * aout_FifoPush : push a packet into the FIFO
344  *****************************************************************************/
345 void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
346 {
347     *p_fifo->pp_last = p_buffer;
348     p_fifo->pp_last = &p_buffer->p_next;
349     *p_fifo->pp_last = NULL;
350     /* Enforce the continuity of the stream. */
351     if( date_Get( &p_fifo->end_date ) != VLC_TS_INVALID )
352     {
353         p_buffer->i_pts = date_Get( &p_fifo->end_date );
354         p_buffer->i_length = date_Increment( &p_fifo->end_date,
355                                              p_buffer->i_nb_samples );
356         p_buffer->i_length -= p_buffer->i_pts;
357     }
358     else
359     {
360         date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
361     }
362 }
363
364 /*****************************************************************************
365  * aout_FifoReset: trash all buffers
366  *****************************************************************************/
367 void aout_FifoReset( aout_fifo_t * p_fifo )
368 {
369     aout_buffer_t * p_buffer;
370
371     date_Set( &p_fifo->end_date, VLC_TS_INVALID );
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 *fifo, mtime_t difference )
387 {
388     if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
389     {
390         assert( fifo->p_first == NULL );
391         return;
392     }
393
394     date_Move( &fifo->end_date, difference );
395     for( block_t *block = fifo->p_first; block != NULL; block = block->p_next )
396         block->i_pts += difference;
397 }
398
399 /*****************************************************************************
400  * aout_FifoPop : get the next buffer out of the FIFO
401  *****************************************************************************/
402 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
403 {
404     aout_buffer_t *p_buffer = p_fifo->p_first;
405     if( p_buffer != NULL )
406     {
407         p_fifo->p_first = p_buffer->p_next;
408         if( p_fifo->p_first == NULL )
409             p_fifo->pp_last = &p_fifo->p_first;
410     }
411     return p_buffer;
412 }
413
414 /*****************************************************************************
415  * aout_FifoDestroy : destroy a FIFO and its buffers
416  *****************************************************************************/
417 void aout_FifoDestroy( aout_fifo_t * p_fifo )
418 {
419     aout_buffer_t * p_buffer;
420
421     p_buffer = p_fifo->p_first;
422     while ( p_buffer != NULL )
423     {
424         aout_buffer_t * p_next = p_buffer->p_next;
425         aout_BufferFree( p_buffer );
426         p_buffer = p_next;
427     }
428
429     p_fifo->p_first = NULL;
430     p_fifo->pp_last = &p_fifo->p_first;
431 }
432
433 /*****************************************************************************
434  * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
435  *****************************************************************************/
436 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
437                               const uint32_t *pi_chan_order_out,
438                               uint32_t i_channel_mask,
439                               int i_channels, int *pi_chan_table )
440 {
441     bool b_chan_reorder = false;
442     int i, j, k, l;
443
444     if( i_channels > AOUT_CHAN_MAX )
445         return false;
446
447     if( pi_chan_order_in == NULL )
448         pi_chan_order_in = pi_vlc_chan_order_wg4;
449     if( pi_chan_order_out == NULL )
450         pi_chan_order_out = pi_vlc_chan_order_wg4;
451
452     for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
453     {
454         if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
455
456         for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
457         {
458             if( i_channel_mask & pi_chan_order_out[k] ) l++;
459         }
460
461         pi_chan_table[j++] = l;
462     }
463
464     for( i = 0; i < i_channels; i++ )
465     {
466         if( pi_chan_table[i] != i ) b_chan_reorder = true;
467     }
468
469     return b_chan_reorder;
470 }
471
472 /*****************************************************************************
473  * aout_ChannelReorder :
474  *****************************************************************************/
475 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
476                           int i_channels, const int *pi_chan_table,
477                           int i_bits_per_sample )
478 {
479     uint8_t p_tmp[AOUT_CHAN_MAX * 4];
480     int i, j;
481
482     if( i_bits_per_sample == 8 )
483     {
484         for( i = 0; i < i_buffer / i_channels; i++ )
485         {
486             for( j = 0; j < i_channels; j++ )
487             {
488                 p_tmp[pi_chan_table[j]] = p_buf[j];
489             }
490
491             memcpy( p_buf, p_tmp, i_channels );
492             p_buf += i_channels;
493         }
494     }
495     else if( i_bits_per_sample == 16 )
496     {
497         for( i = 0; i < i_buffer / i_channels / 2; i++ )
498         {
499             for( j = 0; j < i_channels; j++ )
500             {
501                 p_tmp[2 * pi_chan_table[j]]     = p_buf[2 * j];
502                 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
503             }
504
505             memcpy( p_buf, p_tmp, 2 * i_channels );
506             p_buf += 2 * i_channels;
507         }
508     }
509     else if( i_bits_per_sample == 24 )
510     {
511         for( i = 0; i < i_buffer / i_channels / 3; i++ )
512         {
513             for( j = 0; j < i_channels; j++ )
514             {
515                 p_tmp[3 * pi_chan_table[j]]     = p_buf[3 * j];
516                 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
517                 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
518             }
519
520             memcpy( p_buf, p_tmp, 3 * i_channels );
521             p_buf += 3 * i_channels;
522         }
523     }
524     else if( i_bits_per_sample == 32 )
525     {
526         for( i = 0; i < i_buffer / i_channels / 4; i++ )
527         {
528             for( j = 0; j < i_channels; j++ )
529             {
530                 p_tmp[4 * pi_chan_table[j]]     = p_buf[4 * j];
531                 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
532                 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
533                 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
534             }
535
536             memcpy( p_buf, p_tmp, 4 * i_channels );
537             p_buf += 4 * i_channels;
538         }
539     }
540 }
541
542 /*****************************************************************************
543  * aout_ChannelExtract:
544  *****************************************************************************/
545 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
546                                    const uint8_t *pi_src, int i_src_channels,
547                                    int i_sample_count,
548                                    const int *pi_selection, int i_bytes )
549 {
550     for( int i = 0; i < i_sample_count; i++ )
551     {
552         for( int j = 0; j < i_dst_channels; j++ )
553             memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
554         pi_dst += i_dst_channels * i_bytes;
555         pi_src += i_src_channels * i_bytes;
556     }
557 }
558
559 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
560                           const void *p_src, int i_src_channels,
561                           int i_sample_count, const int *pi_selection, int i_bits_per_sample )
562 {
563     /* It does not work in place */
564     assert( p_dst != p_src );
565
566     /* Force the compiler to inline for the specific cases so it can optimize */
567     if( i_bits_per_sample == 8 )
568         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
569     else  if( i_bits_per_sample == 16 )
570         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
571     else  if( i_bits_per_sample == 24 )
572         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
573     else  if( i_bits_per_sample == 32 )
574         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
575     else  if( i_bits_per_sample == 64 )
576         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
577 }
578
579 bool aout_CheckChannelExtraction( int *pi_selection,
580                                   uint32_t *pi_layout, int *pi_channels,
581                                   const uint32_t pi_order_dst[AOUT_CHAN_MAX],
582                                   const uint32_t *pi_order_src, int i_channels )
583 {
584     const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
585     uint32_t i_layout = 0;
586     int i_out = 0;
587     int pi_index[AOUT_CHAN_MAX];
588
589     /* */
590     if( !pi_order_dst )
591         pi_order_dst = pi_vlc_chan_order_wg4;
592
593     /* Detect special dual mono case */
594     if( i_channels == 2 &&
595         pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
596     {
597         i_layout |= AOUT_CHAN_DUALMONO;
598         pi_order_src = pi_order_dual_mono;
599     }
600
601     /* */
602     for( int i = 0; i < i_channels; i++ )
603     {
604         /* Ignore unknown or duplicated channels or not present in output */
605         if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
606             continue;
607
608         for( int j = 0; j < AOUT_CHAN_MAX; j++ )
609         {
610             if( pi_order_dst[j] == pi_order_src[i] )
611             {
612                 assert( i_out < AOUT_CHAN_MAX );
613                 pi_index[i_out++] = i;
614                 i_layout |= pi_order_src[i];
615                 break;
616             }
617         }
618     }
619
620     /* */
621     for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
622     {
623         for( int k = 0; k < i_out; k++ )
624         {
625             if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
626             {
627                 pi_selection[j++] = pi_index[k];
628                 break;
629             }
630         }
631     }
632
633     *pi_layout = i_layout;
634     *pi_channels = i_out;
635
636     for( int i = 0; i < i_out; i++ )
637     {
638         if( pi_selection[i] != i )
639             return true;
640     }
641     return i_out == i_channels;
642 }
643
644 /* Return the order in which filters should be inserted */
645 static int FilterOrder( const char *psz_name )
646 {
647     static const struct {
648         const char *psz_name;
649         int        i_order;
650     } filter[] = {
651         { "equalizer",  0 },
652         { NULL,         INT_MAX },
653     };
654     for( int i = 0; filter[i].psz_name; i++ )
655     {
656         if( !strcmp( filter[i].psz_name, psz_name ) )
657             return filter[i].i_order;
658     }
659     return INT_MAX;
660 }
661
662 /* This function will add or remove a a module from a string list (colon
663  * separated). It will return true if there is a modification
664  * In case p_aout is NULL, we will use configuration instead of variable */
665 bool aout_ChangeFilterString( vlc_object_t *p_obj, audio_output_t *p_aout,
666                               const char *psz_variable,
667                               const char *psz_name, bool b_add )
668 {
669     if( *psz_name == '\0' )
670         return false;
671
672     char *psz_list;
673     if( p_aout )
674     {
675         psz_list = var_GetString( p_aout, psz_variable );
676     }
677     else
678     {
679         psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
680         var_Destroy( p_obj->p_libvlc, psz_variable );
681     }
682
683     /* Split the string into an array of filters */
684     int i_count = 1;
685     for( char *p = psz_list; p && *p; p++ )
686         i_count += *p == ':';
687     i_count += b_add;
688
689     const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
690     if( !ppsz_filter )
691     {
692         free( psz_list );
693         return false;
694     }
695     bool b_present = false;
696     i_count = 0;
697     for( char *p = psz_list; p && *p; )
698     {
699         char *psz_end = strchr(p, ':');
700         if( psz_end )
701             *psz_end++ = '\0';
702         else
703             psz_end = p + strlen(p);
704         if( *p )
705         {
706             b_present |= !strcmp( p, psz_name );
707             ppsz_filter[i_count++] = p;
708         }
709         p = psz_end;
710     }
711     if( b_present == b_add )
712     {
713         free( ppsz_filter );
714         free( psz_list );
715         return false;
716     }
717
718     if( b_add )
719     {
720         int i_order = FilterOrder( psz_name );
721         int i;
722         for( i = 0; i < i_count; i++ )
723         {
724             if( FilterOrder( ppsz_filter[i] ) > i_order )
725                 break;
726         }
727         if( i < i_count )
728             memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
729         ppsz_filter[i] = psz_name;
730         i_count++;
731     }
732     else
733     {
734         for( int i = 0; i < i_count; i++ )
735         {
736             if( !strcmp( ppsz_filter[i], psz_name ) )
737                 ppsz_filter[i] = "";
738         }
739     }
740     size_t i_length = 0;
741     for( int i = 0; i < i_count; i++ )
742         i_length += 1 + strlen( ppsz_filter[i] );
743
744     char *psz_new = malloc( i_length + 1 );
745     *psz_new = '\0';
746     for( int i = 0; i < i_count; i++ )
747     {
748         if( *ppsz_filter[i] == '\0' )
749             continue;
750         if( *psz_new )
751             strcat( psz_new, ":" );
752         strcat( psz_new, ppsz_filter[i] );
753     }
754     free( ppsz_filter );
755     free( psz_list );
756
757     if( p_aout )
758         var_SetString( p_aout, psz_variable, psz_new );
759     else
760         config_PutPsz( p_obj, psz_variable, psz_new );
761     free( psz_new );
762
763     return true;
764 }
765
766
767