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