]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
No need to check for the existence of the variable to destroy it.
[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_fifo == &p_aout->output.fifo )
47         vlc_assert_locked( &p_aout->output_fifo_lock );
48     else
49     {
50         int i;
51         for( i = 0; i < p_aout->i_nb_inputs; i++ )
52         {
53             if( p_fifo == &p_aout->pp_inputs[i]->fifo)
54             {
55                 vlc_assert_locked( &p_aout->input_fifos_lock );
56                 break;
57             }
58         }
59         if( i == p_aout->i_nb_inputs )
60             vlc_assert_locked( &p_aout->mixer_lock );
61     }
62 #else
63     (void)p_aout;
64     (void)p_fifo;
65 #endif
66 }
67
68 /* Local functions */
69 static void aout_Destructor( vlc_object_t * p_this );
70
71 /*****************************************************************************
72  * aout_New: initialize aout structure
73  *****************************************************************************/
74 aout_instance_t * __aout_New( vlc_object_t * p_parent )
75 {
76     aout_instance_t * p_aout;
77     vlc_value_t val;
78
79     /* Allocate descriptor. */
80     p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
81     if( p_aout == NULL )
82     {
83         return NULL;
84     }
85
86     /* Initialize members. */
87     vlc_mutex_init( &p_aout->input_fifos_lock );
88     vlc_mutex_init( &p_aout->mixer_lock );
89     vlc_mutex_init( &p_aout->output_fifo_lock );
90     p_aout->i_nb_inputs = 0;
91     p_aout->mixer.f_multiplier = 1.0;
92     p_aout->mixer.b_error = 1;
93     p_aout->output.b_error = 1;
94     p_aout->output.b_starving = 1;
95
96     var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
97     val.b_bool = true;
98     var_Set( p_aout, "intf-change", val );
99
100     vlc_object_set_destructor( p_aout, aout_Destructor );
101
102     return p_aout;
103 }
104
105 /*****************************************************************************
106  * aout_Destructor: destroy aout structure
107  *****************************************************************************/
108 static void aout_Destructor( vlc_object_t * p_this )
109 {
110     aout_instance_t * p_aout = (aout_instance_t *)p_this;
111     vlc_mutex_destroy( &p_aout->input_fifos_lock );
112     vlc_mutex_destroy( &p_aout->mixer_lock );
113     vlc_mutex_destroy( &p_aout->output_fifo_lock );
114 }
115
116
117 /*
118  * Formats management (internal and external)
119  */
120
121 /*****************************************************************************
122  * aout_FormatNbChannels : return the number of channels
123  *****************************************************************************/
124 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
125 {
126     static const uint32_t pi_channels[] =
127         { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
128           AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
129           AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
130     unsigned int i_nb = 0, i;
131
132     for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
133     {
134         if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
135     }
136
137     return i_nb;
138 }
139
140 /*****************************************************************************
141  * aout_BitsPerSample : get the number of bits per sample
142  *****************************************************************************/
143 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
144 {
145     switch( i_format )
146     {
147     case VLC_CODEC_U8:
148     case VLC_CODEC_S8:
149         return 8;
150
151     case VLC_CODEC_U16L:
152     case VLC_CODEC_S16L:
153     case VLC_CODEC_U16B:
154     case VLC_CODEC_S16B:
155         return 16;
156
157     case VLC_CODEC_U24L:
158     case VLC_CODEC_S24L:
159     case VLC_CODEC_U24B:
160     case VLC_CODEC_S24B:
161         return 24;
162
163     case VLC_CODEC_S32L:
164     case VLC_CODEC_S32B:
165     case VLC_CODEC_FL32:
166     case VLC_CODEC_FI32:
167         return 32;
168
169     case VLC_CODEC_FL64:
170         return 64;
171
172     default:
173         /* For these formats the caller has to indicate the parameters
174          * by hand. */
175         return 0;
176     }
177 }
178
179 /*****************************************************************************
180  * aout_FormatPrepare : compute the number of bytes per frame & frame length
181  *****************************************************************************/
182 void aout_FormatPrepare( audio_sample_format_t * p_format )
183 {
184     p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
185     if( p_format->i_bitspersample > 0 )
186     {
187         p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
188                                     * aout_FormatNbChannels( p_format );
189         p_format->i_frame_length = 1;
190     }
191 }
192
193 /*****************************************************************************
194  * aout_FormatPrintChannels : print a channel in a human-readable form
195  *****************************************************************************/
196 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
197 {
198     switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
199     {
200     case AOUT_CHAN_LEFT:
201     case AOUT_CHAN_RIGHT:
202     case AOUT_CHAN_CENTER:
203         if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
204               || (p_format->i_original_channels
205                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
206             return "Mono";
207         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
208             return "Left";
209         return "Right";
210     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
211         if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
212         {
213             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
214                 return "Dolby/Reverse";
215             return "Stereo/Reverse";
216         }
217         else
218         {
219             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
220                 return "Dolby";
221             else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
222                 return "Dual-mono";
223             else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
224                 return "Stereo/Mono";
225             else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
226                 return "Stereo/Left";
227             else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
228                 return "Stereo/Right";
229             return "Stereo";
230         }
231     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
232         return "3F";
233     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
234         return "2F1R";
235     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
236           | AOUT_CHAN_REARCENTER:
237         return "3F1R";
238     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
239           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
240         return "2F2R";
241     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
242           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
243         return "2F2M";
244     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
245           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
246         return "3F2R";
247     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
248           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
249         return "3F2M";
250
251     case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
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/LFE";
256         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
257             return "Left/LFE";
258         return "Right/LFE";
259     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
260         if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
261             return "Dolby/LFE";
262         else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
263             return "Dual-mono/LFE";
264         else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
265             return "Mono/LFE";
266         else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
267             return "Stereo/Left/LFE";
268         else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
269             return "Stereo/Right/LFE";
270          return "Stereo/LFE";
271     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
272         return "3F/LFE";
273     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
274           | AOUT_CHAN_LFE:
275         return "2F1R/LFE";
276     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
277           | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
278         return "3F1R/LFE";
279     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
280           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
281         return "2F2R/LFE";
282     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
283           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
284         return "2F2M/LFE";
285     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
286           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
287         return "3F2R/LFE";
288     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
289           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
290         return "3F2M/LFE";
291     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
292           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
293           | AOUT_CHAN_MIDDLERIGHT:
294         return "3F2M2R";
295     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
296           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
297           | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
298         return "3F2M2R/LFE";
299     }
300
301     return "ERROR";
302 }
303
304 /*****************************************************************************
305  * aout_FormatPrint : print a format in a human-readable form
306  *****************************************************************************/
307 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
308                        const audio_sample_format_t * p_format )
309 {
310     msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
311              (char *)&p_format->i_format, p_format->i_rate,
312              aout_FormatPrintChannels( p_format ),
313              p_format->i_frame_length, p_format->i_bytes_per_frame );
314 }
315
316 /*****************************************************************************
317  * aout_FormatsPrint : print two formats in a human-readable form
318  *****************************************************************************/
319 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
320                         const audio_sample_format_t * p_format1,
321                         const audio_sample_format_t * p_format2 )
322 {
323     msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
324              psz_text,
325              (char *)&p_format1->i_format, (char *)&p_format2->i_format,
326              p_format1->i_rate, p_format2->i_rate,
327              aout_FormatPrintChannels( p_format1 ),
328              aout_FormatPrintChannels( p_format2 ) );
329 }
330
331
332 /*
333  * FIFO management (internal) - please understand that solving race conditions
334  * is _your_ job, ie. in the audio output you should own the mixer lock
335  * before calling any of these functions.
336  */
337
338 /*****************************************************************************
339  * aout_FifoInit : initialize the members of a FIFO
340  *****************************************************************************/
341 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
342                     uint32_t i_rate )
343 {
344     AOUT_ASSERT_FIFO_LOCKED;
345
346     if( i_rate == 0 )
347     {
348         msg_Err( p_aout, "initialising fifo with zero divider" );
349     }
350
351     p_fifo->p_first = NULL;
352     p_fifo->pp_last = &p_fifo->p_first;
353     aout_DateInit( &p_fifo->end_date, i_rate );
354 }
355
356 /*****************************************************************************
357  * aout_FifoPush : push a packet into the FIFO
358  *****************************************************************************/
359 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
360                     aout_buffer_t * p_buffer )
361 {
362     (void)p_aout;
363     AOUT_ASSERT_FIFO_LOCKED;
364
365     *p_fifo->pp_last = p_buffer;
366     p_fifo->pp_last = &p_buffer->p_next;
367     *p_fifo->pp_last = NULL;
368     /* Enforce the continuity of the stream. */
369     if ( aout_DateGet( &p_fifo->end_date ) )
370     {
371         p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
372         p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
373                                                  p_buffer->i_nb_samples );
374     }
375     else
376     {
377         aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
378     }
379 }
380
381 /*****************************************************************************
382  * aout_FifoSet : set end_date and trash all buffers (because they aren't
383  * properly dated)
384  *****************************************************************************/
385 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
386                    mtime_t date )
387 {
388     aout_buffer_t * p_buffer;
389     (void)p_aout;
390     AOUT_ASSERT_FIFO_LOCKED;
391
392     aout_DateSet( &p_fifo->end_date, date );
393     p_buffer = p_fifo->p_first;
394     while ( p_buffer != NULL )
395     {
396         aout_buffer_t * p_next = p_buffer->p_next;
397         aout_BufferFree( p_buffer );
398         p_buffer = p_next;
399     }
400     p_fifo->p_first = NULL;
401     p_fifo->pp_last = &p_fifo->p_first;
402 }
403
404 /*****************************************************************************
405  * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
406  *****************************************************************************/
407 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
408                          mtime_t difference )
409 {
410     aout_buffer_t * p_buffer;
411     (void)p_aout;
412     AOUT_ASSERT_FIFO_LOCKED;
413
414     aout_DateMove( &p_fifo->end_date, difference );
415     p_buffer = p_fifo->p_first;
416     while ( p_buffer != NULL )
417     {
418         p_buffer->start_date += difference;
419         p_buffer->end_date += difference;
420         p_buffer = p_buffer->p_next;
421     }
422 }
423
424 /*****************************************************************************
425  * aout_FifoNextStart : return the current end_date
426  *****************************************************************************/
427 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
428 {
429     (void)p_aout;
430     AOUT_ASSERT_FIFO_LOCKED;
431     return aout_DateGet( &p_fifo->end_date );
432 }
433
434 /*****************************************************************************
435  * aout_FifoFirstDate : return the playing date of the first buffer in the
436  * FIFO
437  *****************************************************************************/
438 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
439 {
440     (void)p_aout;
441     AOUT_ASSERT_FIFO_LOCKED;
442     return p_fifo->p_first ?  p_fifo->p_first->start_date : 0;
443 }
444
445 /*****************************************************************************
446  * aout_FifoPop : get the next buffer out of the FIFO
447  *****************************************************************************/
448 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
449 {
450     aout_buffer_t * p_buffer;
451     (void)p_aout;
452     AOUT_ASSERT_FIFO_LOCKED;
453
454     p_buffer = p_fifo->p_first;
455     if ( p_buffer == NULL ) return NULL;
456     p_fifo->p_first = p_buffer->p_next;
457     if ( p_fifo->p_first == NULL )
458     {
459         p_fifo->pp_last = &p_fifo->p_first;
460     }
461
462     return p_buffer;
463 }
464
465 /*****************************************************************************
466  * aout_FifoDestroy : destroy a FIFO and its buffers
467  *****************************************************************************/
468 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
469 {
470     aout_buffer_t * p_buffer;
471     (void)p_aout;
472     AOUT_ASSERT_FIFO_LOCKED;
473
474     p_buffer = p_fifo->p_first;
475     while ( p_buffer != NULL )
476     {
477         aout_buffer_t * p_next = p_buffer->p_next;
478         aout_BufferFree( p_buffer );
479         p_buffer = p_next;
480     }
481
482     p_fifo->p_first = NULL;
483     p_fifo->pp_last = &p_fifo->p_first;
484 }
485
486
487 /*
488  * Date management (internal and external)
489  */
490
491 /*****************************************************************************
492  * aout_DateInit : set the divider of an audio_date_t
493  *****************************************************************************/
494 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
495 {
496     p_date->date = 0;
497     p_date->i_divider = i_divider;
498     p_date->i_remainder = 0;
499 }
500
501 /*****************************************************************************
502  * aout_DateSet : set the date of an audio_date_t
503  *****************************************************************************/
504 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
505 {
506     p_date->date = new_date;
507     p_date->i_remainder = 0;
508 }
509
510 /*****************************************************************************
511  * aout_DateMove : move forwards or backwards the date of an audio_date_t
512  *****************************************************************************/
513 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
514 {
515     p_date->date += difference;
516 }
517
518 /*****************************************************************************
519  * aout_DateGet : get the date of an audio_date_t
520  *****************************************************************************/
521 mtime_t aout_DateGet( const audio_date_t * p_date )
522 {
523     return p_date->date;
524 }
525
526 /*****************************************************************************
527  * aout_DateIncrement : increment the date and return the result, taking
528  * into account rounding errors
529  *****************************************************************************/
530 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
531 {
532     mtime_t i_dividend = INT64_C(1000000) * i_nb_samples;
533     assert( p_date->i_divider > 0 ); /* uninitialized audio_data_t ? */
534     p_date->date += i_dividend / p_date->i_divider;
535     p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
536     if ( p_date->i_remainder >= p_date->i_divider )
537     {
538         /* This is Bresenham algorithm. */
539         p_date->date++;
540         p_date->i_remainder -= p_date->i_divider;
541     }
542     return p_date->date;
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 }