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