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