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