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