]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
474560f17f95b724f3a5046ebd949bd7ed8b2d34
[vlc] / src / audio_output / common.c
1 /*****************************************************************************
2  * common.c : audio output management of common data structures
3  *****************************************************************************
4  * Copyright (C) 2002-2005 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
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     *p_fifo->pp_last = p_buffer;
312     p_fifo->pp_last = &p_buffer->p_next;
313     *p_fifo->pp_last = NULL;
314     /* Enforce the continuity of the stream. */
315     if ( aout_DateGet( &p_fifo->end_date ) )
316     {
317         p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
318         p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
319                                                  p_buffer->i_nb_samples );
320     }
321     else
322     {
323         aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
324     }
325 }
326
327 /*****************************************************************************
328  * aout_FifoSet : set end_date and trash all buffers (because they aren't
329  * properly dated)
330  *****************************************************************************/
331 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
332                    mtime_t date )
333 {
334     aout_buffer_t * p_buffer;
335
336     aout_DateSet( &p_fifo->end_date, date );
337     p_buffer = p_fifo->p_first;
338     while ( p_buffer != NULL )
339     {
340         aout_buffer_t * p_next = p_buffer->p_next;
341         aout_BufferFree( p_buffer );
342         p_buffer = p_next;
343     }
344     p_fifo->p_first = NULL;
345     p_fifo->pp_last = &p_fifo->p_first;
346 }
347
348 /*****************************************************************************
349  * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
350  *****************************************************************************/
351 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
352                          mtime_t difference )
353 {
354     aout_buffer_t * p_buffer;
355
356     aout_DateMove( &p_fifo->end_date, difference );
357     p_buffer = p_fifo->p_first;
358     while ( p_buffer != NULL )
359     {
360         p_buffer->start_date += difference;
361         p_buffer->end_date += difference;
362         p_buffer = p_buffer->p_next;
363     }
364 }
365
366 /*****************************************************************************
367  * aout_FifoNextStart : return the current end_date
368  *****************************************************************************/
369 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
370 {
371     return aout_DateGet( &p_fifo->end_date );
372 }
373
374 /*****************************************************************************
375  * aout_FifoFirstDate : return the playing date of the first buffer in the
376  * FIFO
377  *****************************************************************************/
378 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
379 {
380     return p_fifo->p_first ?  p_fifo->p_first->start_date : 0;
381 }
382
383 /*****************************************************************************
384  * aout_FifoPop : get the next buffer out of the FIFO
385  *****************************************************************************/
386 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
387 {
388     aout_buffer_t * p_buffer;
389
390     p_buffer = p_fifo->p_first;
391     if ( p_buffer == NULL ) return NULL;
392     p_fifo->p_first = p_buffer->p_next;
393     if ( p_fifo->p_first == NULL )
394     {
395         p_fifo->pp_last = &p_fifo->p_first;
396     }
397
398     return p_buffer;
399 }
400
401 /*****************************************************************************
402  * aout_FifoDestroy : destroy a FIFO and its buffers
403  *****************************************************************************/
404 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
405 {
406     aout_buffer_t * p_buffer;
407
408     p_buffer = p_fifo->p_first;
409     while ( p_buffer != NULL )
410     {
411         aout_buffer_t * p_next = p_buffer->p_next;
412         aout_BufferFree( p_buffer );
413         p_buffer = p_next;
414     }
415
416     p_fifo->p_first = NULL;
417     p_fifo->pp_last = &p_fifo->p_first;
418 }
419
420
421 /*
422  * Date management (internal and external)
423  */
424
425 /*****************************************************************************
426  * aout_DateInit : set the divider of an audio_date_t
427  *****************************************************************************/
428 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
429 {
430     p_date->date = 0;
431     p_date->i_divider = i_divider;
432     p_date->i_remainder = 0;
433 }
434
435 /*****************************************************************************
436  * aout_DateSet : set the date of an audio_date_t
437  *****************************************************************************/
438 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
439 {
440     p_date->date = new_date;
441     p_date->i_remainder = 0;
442 }
443
444 /*****************************************************************************
445  * aout_DateMove : move forwards or backwards the date of an audio_date_t
446  *****************************************************************************/
447 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
448 {
449     p_date->date += difference;
450 }
451
452 /*****************************************************************************
453  * aout_DateGet : get the date of an audio_date_t
454  *****************************************************************************/
455 mtime_t aout_DateGet( const audio_date_t * p_date )
456 {
457     return p_date->date;
458 }
459
460 /*****************************************************************************
461  * aout_DateIncrement : increment the date and return the result, taking
462  * into account rounding errors
463  *****************************************************************************/
464 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
465 {
466     mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
467     p_date->date += i_dividend / p_date->i_divider;
468     p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
469     if ( p_date->i_remainder >= p_date->i_divider )
470     {
471         /* This is Bresenham algorithm. */
472         p_date->date++;
473         p_date->i_remainder -= p_date->i_divider;
474     }
475     return p_date->date;
476 }
477
478 /*****************************************************************************
479  * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
480  *****************************************************************************/
481 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
482                               const uint32_t *pi_chan_order_out,
483                               uint32_t i_channel_mask,
484                               int i_channels, int *pi_chan_table )
485 {
486     vlc_bool_t b_chan_reorder = VLC_FALSE;
487     int i, j, k, l;
488
489     if( i_channels > AOUT_CHAN_MAX ) return VLC_FALSE;
490
491     for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
492     {
493         if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
494
495         for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
496         {
497             if( i_channel_mask & pi_chan_order_out[k] ) l++;
498         }
499
500         pi_chan_table[j++] = l;
501     }
502
503     for( i = 0; i < i_channels; i++ )
504     {
505         if( pi_chan_table[i] != i ) b_chan_reorder = VLC_TRUE;
506     }
507
508     return b_chan_reorder;
509 }
510
511 /*****************************************************************************
512  * aout_ChannelReorder :
513  *****************************************************************************/
514 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
515                           int i_channels, const int *pi_chan_table,
516                           int i_bits_per_sample )
517 {
518     uint8_t p_tmp[AOUT_CHAN_MAX * 4];
519     int i, j;
520
521     if( i_bits_per_sample == 8 )
522     {
523         for( i = 0; i < i_buffer / i_channels; i++ )
524         {
525             for( j = 0; j < i_channels; j++ )
526             {
527                 p_tmp[pi_chan_table[j]] = p_buf[j];
528             }
529
530             memcpy( p_buf, p_tmp, i_channels );
531             p_buf += i_channels;
532         }
533     }
534     else if( i_bits_per_sample == 16 )
535     {
536         for( i = 0; i < i_buffer / i_channels / 2; i++ )
537         {
538             for( j = 0; j < i_channels; j++ )
539             {
540                 p_tmp[2 * pi_chan_table[j]]     = p_buf[2 * j];
541                 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
542             }
543
544             memcpy( p_buf, p_tmp, 2 * i_channels );
545             p_buf += 2 * i_channels;
546         }
547     }
548     else if( i_bits_per_sample == 24 )
549     {
550         for( i = 0; i < i_buffer / i_channels / 3; i++ )
551         {
552             for( j = 0; j < i_channels; j++ )
553             {
554                 p_tmp[3 * pi_chan_table[j]]     = p_buf[3 * j];
555                 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
556                 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
557             }
558
559             memcpy( p_buf, p_tmp, 3 * i_channels );
560             p_buf += 3 * i_channels;
561         }
562     }
563     else if( i_bits_per_sample == 32 )
564     {
565         for( i = 0; i < i_buffer / i_channels / 4; i++ )
566         {
567             for( j = 0; j < i_channels; j++ )
568             {
569                 p_tmp[4 * pi_chan_table[j]]     = p_buf[4 * j];
570                 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
571                 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
572                 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
573             }
574
575             memcpy( p_buf, p_tmp, 4 * i_channels );
576             p_buf += 4 * i_channels;
577         }
578     }
579 }