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