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