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