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