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