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