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