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