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