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