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