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