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