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