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