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