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