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