]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
aout_Format(s)Print: use vlc_object rather than audio_output
[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 #undef aout_FormatPrint
294 /**
295  * Prints an audio sample format in a human-readable form.
296  */
297 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
298                        const audio_sample_format_t *p_format )
299 {
300     msg_Dbg( obj, "%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 #undef aout_FormatsPrint
307 /**
308  * Prints two formats in a human-readable form
309  */
310 void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
311                         const audio_sample_format_t * p_format1,
312                         const audio_sample_format_t * p_format2 )
313 {
314     msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
315              psz_text,
316              (char *)&p_format1->i_format, (char *)&p_format2->i_format,
317              p_format1->i_rate, p_format2->i_rate,
318              aout_FormatPrintChannels( p_format1 ),
319              aout_FormatPrintChannels( p_format2 ) );
320 }
321
322
323 /*
324  * FIFO management (internal) - please understand that solving race conditions
325  * is _your_ job, ie. in the audio output you should own the mixer lock
326  * before calling any of these functions.
327  */
328
329 #undef aout_FifoInit
330 /*****************************************************************************
331  * aout_FifoInit : initialize the members of a FIFO
332  *****************************************************************************/
333 void aout_FifoInit( vlc_object_t *obj, aout_fifo_t * p_fifo, uint32_t i_rate )
334 {
335     if( unlikely(i_rate == 0) )
336         msg_Err( obj, "initialising fifo with zero divider" );
337
338     p_fifo->p_first = NULL;
339     p_fifo->pp_last = &p_fifo->p_first;
340     date_Init( &p_fifo->end_date, i_rate, 1 );
341     date_Set( &p_fifo->end_date, VLC_TS_INVALID );
342 }
343
344 /*****************************************************************************
345  * aout_FifoPush : push a packet into the FIFO
346  *****************************************************************************/
347 void aout_FifoPush( aout_fifo_t * p_fifo, aout_buffer_t * p_buffer )
348 {
349     *p_fifo->pp_last = p_buffer;
350     p_fifo->pp_last = &p_buffer->p_next;
351     *p_fifo->pp_last = NULL;
352     /* Enforce the continuity of the stream. */
353     if( date_Get( &p_fifo->end_date ) != VLC_TS_INVALID )
354     {
355         p_buffer->i_pts = date_Get( &p_fifo->end_date );
356         p_buffer->i_length = date_Increment( &p_fifo->end_date,
357                                              p_buffer->i_nb_samples );
358         p_buffer->i_length -= p_buffer->i_pts;
359     }
360     else
361     {
362         date_Set( &p_fifo->end_date, p_buffer->i_pts + p_buffer->i_length );
363     }
364 }
365
366 /*****************************************************************************
367  * aout_FifoReset: trash all buffers
368  *****************************************************************************/
369 void aout_FifoReset( aout_fifo_t * p_fifo )
370 {
371     aout_buffer_t * p_buffer;
372
373     date_Set( &p_fifo->end_date, VLC_TS_INVALID );
374     p_buffer = p_fifo->p_first;
375     while ( p_buffer != NULL )
376     {
377         aout_buffer_t * p_next = p_buffer->p_next;
378         aout_BufferFree( p_buffer );
379         p_buffer = p_next;
380     }
381     p_fifo->p_first = NULL;
382     p_fifo->pp_last = &p_fifo->p_first;
383 }
384
385 /*****************************************************************************
386  * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
387  *****************************************************************************/
388 void aout_FifoMoveDates( aout_fifo_t *fifo, mtime_t difference )
389 {
390     if( date_Get( &fifo->end_date ) == VLC_TS_INVALID )
391     {
392         assert( fifo->p_first == NULL );
393         return;
394     }
395
396     date_Move( &fifo->end_date, difference );
397     for( block_t *block = fifo->p_first; block != NULL; block = block->p_next )
398         block->i_pts += difference;
399 }
400
401 /*****************************************************************************
402  * aout_FifoPop : get the next buffer out of the FIFO
403  *****************************************************************************/
404 aout_buffer_t *aout_FifoPop( aout_fifo_t * p_fifo )
405 {
406     aout_buffer_t *p_buffer = p_fifo->p_first;
407     if( p_buffer != NULL )
408     {
409         p_fifo->p_first = p_buffer->p_next;
410         if( p_fifo->p_first == NULL )
411             p_fifo->pp_last = &p_fifo->p_first;
412     }
413     return p_buffer;
414 }
415
416 /*****************************************************************************
417  * aout_FifoDestroy : destroy a FIFO and its buffers
418  *****************************************************************************/
419 void aout_FifoDestroy( aout_fifo_t * p_fifo )
420 {
421     aout_buffer_t * p_buffer;
422
423     p_buffer = p_fifo->p_first;
424     while ( p_buffer != NULL )
425     {
426         aout_buffer_t * p_next = p_buffer->p_next;
427         aout_BufferFree( p_buffer );
428         p_buffer = p_next;
429     }
430
431     p_fifo->p_first = NULL;
432     p_fifo->pp_last = &p_fifo->p_first;
433 }
434
435 /*****************************************************************************
436  * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
437  *****************************************************************************/
438 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
439                               const uint32_t *pi_chan_order_out,
440                               uint32_t i_channel_mask,
441                               int i_channels, int *pi_chan_table )
442 {
443     bool b_chan_reorder = false;
444     int i, j, k, l;
445
446     if( i_channels > AOUT_CHAN_MAX )
447         return false;
448
449     if( pi_chan_order_in == NULL )
450         pi_chan_order_in = pi_vlc_chan_order_wg4;
451     if( pi_chan_order_out == NULL )
452         pi_chan_order_out = pi_vlc_chan_order_wg4;
453
454     for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
455     {
456         if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
457
458         for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
459         {
460             if( i_channel_mask & pi_chan_order_out[k] ) l++;
461         }
462
463         pi_chan_table[j++] = l;
464     }
465
466     for( i = 0; i < i_channels; i++ )
467     {
468         if( pi_chan_table[i] != i ) b_chan_reorder = true;
469     }
470
471     return b_chan_reorder;
472 }
473
474 /*****************************************************************************
475  * aout_ChannelReorder :
476  *****************************************************************************/
477 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
478                           int i_channels, const int *pi_chan_table,
479                           int i_bits_per_sample )
480 {
481     uint8_t p_tmp[AOUT_CHAN_MAX * 4];
482     int i, j;
483
484     if( i_bits_per_sample == 8 )
485     {
486         for( i = 0; i < i_buffer / i_channels; i++ )
487         {
488             for( j = 0; j < i_channels; j++ )
489             {
490                 p_tmp[pi_chan_table[j]] = p_buf[j];
491             }
492
493             memcpy( p_buf, p_tmp, i_channels );
494             p_buf += i_channels;
495         }
496     }
497     else if( i_bits_per_sample == 16 )
498     {
499         for( i = 0; i < i_buffer / i_channels / 2; i++ )
500         {
501             for( j = 0; j < i_channels; j++ )
502             {
503                 p_tmp[2 * pi_chan_table[j]]     = p_buf[2 * j];
504                 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
505             }
506
507             memcpy( p_buf, p_tmp, 2 * i_channels );
508             p_buf += 2 * i_channels;
509         }
510     }
511     else if( i_bits_per_sample == 24 )
512     {
513         for( i = 0; i < i_buffer / i_channels / 3; i++ )
514         {
515             for( j = 0; j < i_channels; j++ )
516             {
517                 p_tmp[3 * pi_chan_table[j]]     = p_buf[3 * j];
518                 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
519                 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
520             }
521
522             memcpy( p_buf, p_tmp, 3 * i_channels );
523             p_buf += 3 * i_channels;
524         }
525     }
526     else if( i_bits_per_sample == 32 )
527     {
528         for( i = 0; i < i_buffer / i_channels / 4; i++ )
529         {
530             for( j = 0; j < i_channels; j++ )
531             {
532                 p_tmp[4 * pi_chan_table[j]]     = p_buf[4 * j];
533                 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
534                 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
535                 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
536             }
537
538             memcpy( p_buf, p_tmp, 4 * i_channels );
539             p_buf += 4 * i_channels;
540         }
541     }
542 }
543
544 /*****************************************************************************
545  * aout_ChannelExtract:
546  *****************************************************************************/
547 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
548                                    const uint8_t *pi_src, int i_src_channels,
549                                    int i_sample_count,
550                                    const int *pi_selection, int i_bytes )
551 {
552     for( int i = 0; i < i_sample_count; i++ )
553     {
554         for( int j = 0; j < i_dst_channels; j++ )
555             memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
556         pi_dst += i_dst_channels * i_bytes;
557         pi_src += i_src_channels * i_bytes;
558     }
559 }
560
561 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
562                           const void *p_src, int i_src_channels,
563                           int i_sample_count, const int *pi_selection, int i_bits_per_sample )
564 {
565     /* It does not work in place */
566     assert( p_dst != p_src );
567
568     /* Force the compiler to inline for the specific cases so it can optimize */
569     if( i_bits_per_sample == 8 )
570         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
571     else  if( i_bits_per_sample == 16 )
572         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
573     else  if( i_bits_per_sample == 24 )
574         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
575     else  if( i_bits_per_sample == 32 )
576         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
577     else  if( i_bits_per_sample == 64 )
578         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
579 }
580
581 bool aout_CheckChannelExtraction( int *pi_selection,
582                                   uint32_t *pi_layout, int *pi_channels,
583                                   const uint32_t pi_order_dst[AOUT_CHAN_MAX],
584                                   const uint32_t *pi_order_src, int i_channels )
585 {
586     const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
587     uint32_t i_layout = 0;
588     int i_out = 0;
589     int pi_index[AOUT_CHAN_MAX];
590
591     /* */
592     if( !pi_order_dst )
593         pi_order_dst = pi_vlc_chan_order_wg4;
594
595     /* Detect special dual mono case */
596     if( i_channels == 2 &&
597         pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
598     {
599         i_layout |= AOUT_CHAN_DUALMONO;
600         pi_order_src = pi_order_dual_mono;
601     }
602
603     /* */
604     for( int i = 0; i < i_channels; i++ )
605     {
606         /* Ignore unknown or duplicated channels or not present in output */
607         if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
608             continue;
609
610         for( int j = 0; j < AOUT_CHAN_MAX; j++ )
611         {
612             if( pi_order_dst[j] == pi_order_src[i] )
613             {
614                 assert( i_out < AOUT_CHAN_MAX );
615                 pi_index[i_out++] = i;
616                 i_layout |= pi_order_src[i];
617                 break;
618             }
619         }
620     }
621
622     /* */
623     for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
624     {
625         for( int k = 0; k < i_out; k++ )
626         {
627             if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
628             {
629                 pi_selection[j++] = pi_index[k];
630                 break;
631             }
632         }
633     }
634
635     *pi_layout = i_layout;
636     *pi_channels = i_out;
637
638     for( int i = 0; i < i_out; i++ )
639     {
640         if( pi_selection[i] != i )
641             return true;
642     }
643     return i_out == i_channels;
644 }
645
646 /* Return the order in which filters should be inserted */
647 static int FilterOrder( const char *psz_name )
648 {
649     static const struct {
650         const char *psz_name;
651         int        i_order;
652     } filter[] = {
653         { "equalizer",  0 },
654         { NULL,         INT_MAX },
655     };
656     for( int i = 0; filter[i].psz_name; i++ )
657     {
658         if( !strcmp( filter[i].psz_name, psz_name ) )
659             return filter[i].i_order;
660     }
661     return INT_MAX;
662 }
663
664 /* This function will add or remove a a module from a string list (colon
665  * separated). It will return true if there is a modification
666  * In case p_aout is NULL, we will use configuration instead of variable */
667 bool aout_ChangeFilterString( vlc_object_t *p_obj, audio_output_t *p_aout,
668                               const char *psz_variable,
669                               const char *psz_name, bool b_add )
670 {
671     if( *psz_name == '\0' )
672         return false;
673
674     char *psz_list;
675     if( p_aout )
676     {
677         psz_list = var_GetString( p_aout, psz_variable );
678     }
679     else
680     {
681         psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
682         var_Destroy( p_obj->p_libvlc, psz_variable );
683     }
684
685     /* Split the string into an array of filters */
686     int i_count = 1;
687     for( char *p = psz_list; p && *p; p++ )
688         i_count += *p == ':';
689     i_count += b_add;
690
691     const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
692     if( !ppsz_filter )
693     {
694         free( psz_list );
695         return false;
696     }
697     bool b_present = false;
698     i_count = 0;
699     for( char *p = psz_list; p && *p; )
700     {
701         char *psz_end = strchr(p, ':');
702         if( psz_end )
703             *psz_end++ = '\0';
704         else
705             psz_end = p + strlen(p);
706         if( *p )
707         {
708             b_present |= !strcmp( p, psz_name );
709             ppsz_filter[i_count++] = p;
710         }
711         p = psz_end;
712     }
713     if( b_present == b_add )
714     {
715         free( ppsz_filter );
716         free( psz_list );
717         return false;
718     }
719
720     if( b_add )
721     {
722         int i_order = FilterOrder( psz_name );
723         int i;
724         for( i = 0; i < i_count; i++ )
725         {
726             if( FilterOrder( ppsz_filter[i] ) > i_order )
727                 break;
728         }
729         if( i < i_count )
730             memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
731         ppsz_filter[i] = psz_name;
732         i_count++;
733     }
734     else
735     {
736         for( int i = 0; i < i_count; i++ )
737         {
738             if( !strcmp( ppsz_filter[i], psz_name ) )
739                 ppsz_filter[i] = "";
740         }
741     }
742     size_t i_length = 0;
743     for( int i = 0; i < i_count; i++ )
744         i_length += 1 + strlen( ppsz_filter[i] );
745
746     char *psz_new = malloc( i_length + 1 );
747     *psz_new = '\0';
748     for( int i = 0; i < i_count; i++ )
749     {
750         if( *ppsz_filter[i] == '\0' )
751             continue;
752         if( *psz_new )
753             strcat( psz_new, ":" );
754         strcat( psz_new, ppsz_filter[i] );
755     }
756     free( ppsz_filter );
757     free( psz_list );
758
759     if( p_aout )
760         var_SetString( p_aout, psz_variable, psz_new );
761     else
762         config_PutPsz( p_obj, psz_variable, psz_new );
763     free( psz_new );
764
765     return true;
766 }
767
768
769