]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
* src/audio_ouput/input.c: better on-the-fly switching of audio filters.
[vlc] / src / audio_output / common.c
1 /*****************************************************************************
2  * common.c : audio output management of common data structures
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
32 #include "audio_output.h"
33 #include "aout_internal.h"
34
35
36 /*
37  * Instances management (internal and external)
38  */
39
40 /*****************************************************************************
41  * aout_New: initialize aout structure
42  *****************************************************************************/
43 aout_instance_t * __aout_New( vlc_object_t * p_parent )
44 {
45     aout_instance_t * p_aout;
46     vlc_value_t val;
47
48     /* Allocate descriptor. */
49     p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
50     if( p_aout == NULL )
51     {
52         return NULL;
53     }
54
55     /* Initialize members. */
56     vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
57     vlc_mutex_init( p_parent, &p_aout->mixer_lock );
58     vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
59     p_aout->i_nb_inputs = 0;
60     p_aout->mixer.f_multiplier = 1.0;
61     p_aout->mixer.b_error = 1;
62     p_aout->output.b_error = 1;
63     p_aout->output.b_starving = 1;
64
65     var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
66     val.b_bool = VLC_TRUE;
67     var_Set( p_aout, "intf-change", val );
68
69     return p_aout;
70 }
71
72 /*****************************************************************************
73  * aout_Delete: destroy aout structure
74  *****************************************************************************/
75 void aout_Delete( aout_instance_t * p_aout )
76 {
77     var_Destroy( p_aout, "intf-change" );
78
79     vlc_mutex_destroy( &p_aout->input_fifos_lock );
80     vlc_mutex_destroy( &p_aout->mixer_lock );
81     vlc_mutex_destroy( &p_aout->output_fifo_lock );
82
83     /* Free structure. */
84     vlc_object_destroy( p_aout );
85 }
86
87
88 /*
89  * Formats management (internal and external)
90  */
91
92 /*****************************************************************************
93  * aout_FormatNbChannels : return the number of channels
94  *****************************************************************************/
95 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
96 {
97     static const uint32_t pi_channels[] =
98         { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
99           AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
100           AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
101     unsigned int i_nb = 0, i;
102
103     for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
104     {
105         if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
106     }
107
108     return i_nb;
109 }
110
111 /*****************************************************************************
112  * aout_FormatPrepare : compute the number of bytes per frame & frame length
113  *****************************************************************************/
114 void aout_FormatPrepare( audio_sample_format_t * p_format )
115 {
116     int i_result;
117
118     switch ( p_format->i_format )
119     {
120     case VLC_FOURCC('u','8',' ',' '):
121     case VLC_FOURCC('s','8',' ',' '):
122         i_result = 1;
123         break;
124
125     case VLC_FOURCC('u','1','6','l'):
126     case VLC_FOURCC('s','1','6','l'):
127     case VLC_FOURCC('u','1','6','b'):
128     case VLC_FOURCC('s','1','6','b'):
129         i_result = 2;
130         break;
131
132     case VLC_FOURCC('f','l','3','2'):
133     case VLC_FOURCC('f','i','3','2'):
134         i_result = 4;
135         break;
136
137     case VLC_FOURCC('s','p','d','i'):
138     case VLC_FOURCC('a','5','2',' '):
139     case VLC_FOURCC('d','t','s',' '):
140     case VLC_FOURCC('m','p','g','a'):
141     case VLC_FOURCC('m','p','g','3'):
142     default:
143         /* For these formats the caller has to indicate the parameters
144          * by hand. */
145         return;
146     }
147
148     p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
149     p_format->i_frame_length = 1;
150 }
151
152 /*****************************************************************************
153  * aout_FormatPrintChannels : print a channel in a human-readable form
154  *****************************************************************************/
155 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
156 {
157     switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
158     {
159     case AOUT_CHAN_CENTER:
160         if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
161               || (p_format->i_original_channels
162                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
163             return "Mono";
164         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
165             return "Left";
166         return "Right";
167     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
168         if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
169         {
170             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
171                 return "Dolby/Reverse";
172             return "Stereo/Reverse";
173         }
174         else
175         {
176             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
177                 return "Dolby";
178             else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
179                 return "Dual-mono";
180             else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
181                 return "Stereo/Mono";
182             else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
183                 return "Stereo/Left";
184             else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
185                 return "Stereo/Right";
186             return "Stereo";
187         }
188     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
189         return "3F";
190     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
191         return "2F1R";
192     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
193           | AOUT_CHAN_REARCENTER:
194         return "3F1R";
195     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
196           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
197         return "2F2R";
198     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
199           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
200         return "3F2R";
201
202     case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
203         if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
204               || (p_format->i_original_channels
205                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
206             return "Mono/LFE";
207         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
208             return "Left/LFE";
209         return "Right/LFE";
210     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
211         if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
212             return "Dolby/LFE";
213         else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
214             return "Dual-mono/LFE";
215         else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
216             return "Mono/LFE";
217         else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
218             return "Stereo/Left/LFE";
219         else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
220             return "Stereo/Right/LFE";
221          return "Stereo/LFE";
222     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
223         return "3F/LFE";
224     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
225           | AOUT_CHAN_LFE:
226         return "2F1R/LFE";
227     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
228           | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
229         return "3F1R/LFE";
230     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
231           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
232         return "2F2R/LFE";
233     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
234           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
235         return "3F2R/LFE";
236     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
237           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
238           | AOUT_CHAN_MIDDLERIGHT:
239         return "3F2M2R";
240     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
241           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
242           | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
243         return "3F2M2R/LFE";
244     }
245
246     return "ERROR";
247 }
248
249 /*****************************************************************************
250  * aout_FormatPrint : print a format in a human-readable form
251  *****************************************************************************/
252 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
253                        const audio_sample_format_t * p_format )
254 {
255     msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
256              (char *)&p_format->i_format, p_format->i_rate,
257              aout_FormatPrintChannels( p_format ),
258              p_format->i_frame_length, p_format->i_bytes_per_frame );
259 }
260
261 /*****************************************************************************
262  * aout_FormatsPrint : print two formats in a human-readable form
263  *****************************************************************************/
264 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
265                         const audio_sample_format_t * p_format1,
266                         const audio_sample_format_t * p_format2 )
267 {
268     msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
269              psz_text,
270              (char *)&p_format1->i_format, (char *)&p_format2->i_format,
271              p_format1->i_rate, p_format2->i_rate,
272              aout_FormatPrintChannels( p_format1 ),
273              aout_FormatPrintChannels( p_format2 ) );
274 }
275
276
277 /*
278  * FIFO management (internal) - please understand that solving race conditions
279  * is _your_ job, ie. in the audio output you should own the mixer lock
280  * before calling any of these functions.
281  */
282
283 /*****************************************************************************
284  * aout_FifoInit : initialize the members of a FIFO
285  *****************************************************************************/
286 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
287                     uint32_t i_rate )
288 {
289     p_fifo->p_first = NULL;
290     p_fifo->pp_last = &p_fifo->p_first;
291     aout_DateInit( &p_fifo->end_date, i_rate );
292 }
293
294 /*****************************************************************************
295  * aout_FifoPush : push a packet into the FIFO
296  *****************************************************************************/
297 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
298                     aout_buffer_t * p_buffer )
299 {
300     *p_fifo->pp_last = p_buffer;
301     p_fifo->pp_last = &p_buffer->p_next;
302     *p_fifo->pp_last = NULL;
303     /* Enforce the continuity of the stream. */
304     if ( aout_DateGet( &p_fifo->end_date ) )
305     {
306         p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
307         p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
308                                                  p_buffer->i_nb_samples );
309     }
310     else
311     {
312         aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
313     }
314 }
315
316 /*****************************************************************************
317  * aout_FifoSet : set end_date and trash all buffers (because they aren't
318  * properly dated)
319  *****************************************************************************/
320 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
321                    mtime_t date )
322 {
323     aout_buffer_t * p_buffer;
324
325     aout_DateSet( &p_fifo->end_date, date );
326     p_buffer = p_fifo->p_first;
327     while ( p_buffer != NULL )
328     {
329         aout_buffer_t * p_next = p_buffer->p_next;
330         aout_BufferFree( p_buffer );
331         p_buffer = p_next;
332     }
333     p_fifo->p_first = NULL;
334     p_fifo->pp_last = &p_fifo->p_first;
335 }
336
337 /*****************************************************************************
338  * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
339  *****************************************************************************/
340 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
341                          mtime_t difference )
342 {
343     aout_buffer_t * p_buffer;
344
345     aout_DateMove( &p_fifo->end_date, difference );
346     p_buffer = p_fifo->p_first;
347     while ( p_buffer != NULL )
348     {
349         p_buffer->start_date += difference;
350         p_buffer->end_date += difference;
351         p_buffer = p_buffer->p_next;
352     }
353 }
354
355 /*****************************************************************************
356  * aout_FifoNextStart : return the current end_date
357  *****************************************************************************/
358 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
359 {
360     return aout_DateGet( &p_fifo->end_date );
361 }
362
363 /*****************************************************************************
364  * aout_FifoFirstDate : return the playing date of the first buffer in the
365  * FIFO
366  *****************************************************************************/
367 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
368 {
369     return p_fifo->p_first ?  p_fifo->p_first->start_date : 0;
370 }
371
372 /*****************************************************************************
373  * aout_FifoPop : get the next buffer out of the FIFO
374  *****************************************************************************/
375 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
376 {
377     aout_buffer_t * p_buffer;
378
379     p_buffer = p_fifo->p_first;
380     if ( p_buffer == NULL ) return NULL;
381     p_fifo->p_first = p_buffer->p_next;
382     if ( p_fifo->p_first == NULL )
383     {
384         p_fifo->pp_last = &p_fifo->p_first;
385     }
386
387     return p_buffer;
388 }
389
390 /*****************************************************************************
391  * aout_FifoDestroy : destroy a FIFO and its buffers
392  *****************************************************************************/
393 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
394 {
395     aout_buffer_t * p_buffer;
396
397     p_buffer = p_fifo->p_first;
398     while ( p_buffer != NULL )
399     {
400         aout_buffer_t * p_next = p_buffer->p_next;
401         aout_BufferFree( p_buffer );
402         p_buffer = p_next;
403     }
404
405     p_fifo->p_first = NULL;
406     p_fifo->pp_last = &p_fifo->p_first;
407 }
408
409
410 /*
411  * Date management (internal and external)
412  */
413
414 /*****************************************************************************
415  * aout_DateInit : set the divider of an audio_date_t
416  *****************************************************************************/
417 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
418 {
419     p_date->date = 0;
420     p_date->i_divider = i_divider;
421     p_date->i_remainder = 0;
422 }
423
424 /*****************************************************************************
425  * aout_DateSet : set the date of an audio_date_t
426  *****************************************************************************/
427 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
428 {
429     p_date->date = new_date;
430     p_date->i_remainder = 0;
431 }
432
433 /*****************************************************************************
434  * aout_DateMove : move forwards or backwards the date of an audio_date_t
435  *****************************************************************************/
436 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
437 {
438     p_date->date += difference;
439 }
440
441 /*****************************************************************************
442  * aout_DateGet : get the date of an audio_date_t
443  *****************************************************************************/
444 mtime_t aout_DateGet( const audio_date_t * p_date )
445 {
446     return p_date->date;
447 }
448
449 /*****************************************************************************
450  * aout_DateIncrement : increment the date and return the result, taking
451  * into account rounding errors
452  *****************************************************************************/
453 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
454 {
455     mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
456     p_date->date += i_dividend / p_date->i_divider;
457     p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
458     if ( p_date->i_remainder >= p_date->i_divider )
459     {
460         /* This is Bresenham algorithm. */
461         p_date->date++;
462         p_date->i_remainder -= p_date->i_divider;
463     }
464     return p_date->date;
465 }
466