]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
* Added -mdynamic-no-pic to darwin CFLAGS (can you believe I actually read ./ :)
[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.5 2002/10/22 23:08:00 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
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_starving = 1;
62
63     vlc_object_attach( p_aout, p_parent->p_vlc );
64
65     return p_aout;
66 }
67
68 /*****************************************************************************
69  * aout_Delete: destroy aout structure
70  *****************************************************************************/
71 void aout_Delete( aout_instance_t * p_aout )
72 {
73     vlc_mutex_destroy( &p_aout->input_fifos_lock );
74     vlc_mutex_destroy( &p_aout->mixer_lock );
75     vlc_mutex_destroy( &p_aout->output_fifo_lock );
76
77     /* Free structure. */
78     vlc_object_destroy( p_aout );
79 }
80
81
82 /*
83  * Formats management (internal and external)
84  */
85
86 /*****************************************************************************
87  * aout_FormatNbChannels : return the number of channels
88  *****************************************************************************/
89 int aout_FormatNbChannels( audio_sample_format_t * p_format )
90 {
91     int i_nb;
92
93     switch ( p_format->i_channels & AOUT_CHAN_MASK )
94     {
95     case AOUT_CHAN_CHANNEL1:
96     case AOUT_CHAN_CHANNEL2:
97     case AOUT_CHAN_MONO:
98         i_nb = 1;
99         break;
100
101     case AOUT_CHAN_CHANNEL:
102     case AOUT_CHAN_STEREO:
103     case AOUT_CHAN_DOLBY:
104         i_nb = 2;
105         break;
106
107     case AOUT_CHAN_3F:
108     case AOUT_CHAN_2F1R:
109         i_nb = 3;
110         break;
111
112     case AOUT_CHAN_3F1R:
113     case AOUT_CHAN_2F2R:
114         i_nb = 4;
115         break;
116
117     case AOUT_CHAN_3F2R:
118         i_nb = 5;
119         break;
120
121     default:
122         i_nb = 0;
123     }
124
125     if ( p_format->i_channels & AOUT_CHAN_LFE )
126         return i_nb + 1;
127     else
128         return i_nb;
129 }
130
131 /*****************************************************************************
132  * aout_FormatPrepare : compute the number of bytes per frame & frame length
133  *****************************************************************************/
134 void aout_FormatPrepare( audio_sample_format_t * p_format )
135 {
136     int i_result;
137
138     switch ( p_format->i_format )
139     {
140     case VLC_FOURCC('u','8',' ',' '):
141     case VLC_FOURCC('s','8',' ',' '):
142         i_result = 1;
143         break;
144
145     case VLC_FOURCC('u','1','6','l'):
146     case VLC_FOURCC('s','1','6','l'):
147     case VLC_FOURCC('u','1','6','b'):
148     case VLC_FOURCC('s','1','6','b'):
149         i_result = 2;
150         break;
151
152     case VLC_FOURCC('f','l','3','2'):
153     case VLC_FOURCC('f','i','3','2'):
154         i_result = 4;
155         break;
156
157     case VLC_FOURCC('s','p','d','i'):
158     case VLC_FOURCC('a','5','2',' '):
159     case VLC_FOURCC('d','t','s',' '):
160     default:
161         /* For these formats the caller has to indicate the parameters
162          * by hand. */
163         return;
164     }
165
166     p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
167     p_format->i_frame_length = 1;
168 }
169
170 /*****************************************************************************
171  * FormatPrintChannels : print a channel in a human-readable form
172  *****************************************************************************/
173 static const char * FormatPrintChannels( int i_channels )
174 {
175     switch ( i_channels )
176     {
177     case AOUT_CHAN_CHANNEL: return "CHANNEL";
178     case AOUT_CHAN_CHANNEL1: return "CHANNEL1";
179     case AOUT_CHAN_CHANNEL2: return "CHANNEL2";
180     case AOUT_CHAN_MONO: return "MONO";
181     case AOUT_CHAN_STEREO: return "STEREO";
182     case AOUT_CHAN_3F: return "3F";
183     case AOUT_CHAN_2F1R: return "2F1R";
184     case AOUT_CHAN_3F1R: return "3F1R";
185     case AOUT_CHAN_2F2R: return "2F2R";
186     case AOUT_CHAN_3F2R: return "3F2R";
187     case AOUT_CHAN_DOLBY: return "DOLBY";
188     case AOUT_CHAN_CHANNEL | AOUT_CHAN_LFE: return "CHANNEL|LFE";
189     case AOUT_CHAN_CHANNEL1 | AOUT_CHAN_LFE: return "CHANNEL1|LFE";
190     case AOUT_CHAN_CHANNEL2 | AOUT_CHAN_LFE: return "CHANNEL2|LFE";
191     case AOUT_CHAN_MONO | AOUT_CHAN_LFE: return "MONO|LFE";
192     case AOUT_CHAN_STEREO | AOUT_CHAN_LFE: return "STEREO|LFE";
193     case AOUT_CHAN_3F | AOUT_CHAN_LFE: return "3F|LFE";
194     case AOUT_CHAN_2F1R | AOUT_CHAN_LFE: return "2F1R|LFE";
195     case AOUT_CHAN_3F1R | AOUT_CHAN_LFE: return "3F1R|LFE";
196     case AOUT_CHAN_2F2R | AOUT_CHAN_LFE: return "2F2R|LFE";
197     case AOUT_CHAN_3F2R | AOUT_CHAN_LFE: return "3F2R|LFE";
198     case AOUT_CHAN_DOLBY | AOUT_CHAN_LFE: return "DOLBY|LFE";
199     }
200
201     return "ERROR";
202 }
203
204 /*****************************************************************************
205  * aout_FormatPrint : print a format in a human-readable form
206  *****************************************************************************/
207 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
208                        const audio_sample_format_t * p_format )
209 {
210     msg_Dbg( p_aout, "%s format='%4.4s' rate=%d channels=%s", psz_text,
211              (char *)&p_format->i_format, p_format->i_rate,
212              FormatPrintChannels( p_format->i_channels ) );
213 }
214
215 /*****************************************************************************
216  * aout_FormatsPrint : print two formats in a human-readable form
217  *****************************************************************************/
218 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
219                         const audio_sample_format_t * p_format1,
220                         const audio_sample_format_t * p_format2 )
221 {
222     msg_Dbg( p_aout, "%s format='%4.4s'->'%4.4s' rate=%d->%d channels=%s->%s",
223              psz_text,
224              (char *)&p_format1->i_format, (char *)&p_format2->i_format,
225              p_format1->i_rate, p_format2->i_rate,
226              FormatPrintChannels( p_format1->i_channels ),
227              FormatPrintChannels( p_format2->i_channels ) );
228 }
229
230
231 /*
232  * FIFO management (internal) - please understand that solving race conditions
233  * is _your_ job, ie. in the audio output you should own the mixer lock
234  * before calling any of these functions.
235  */
236
237 /*****************************************************************************
238  * aout_FifoInit : initialize the members of a FIFO
239  *****************************************************************************/
240 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
241                     u32 i_rate )
242 {
243     p_fifo->p_first = NULL;
244     p_fifo->pp_last = &p_fifo->p_first;
245     aout_DateInit( &p_fifo->end_date, i_rate );
246 }
247
248 /*****************************************************************************
249  * aout_FifoPush : push a packet into the FIFO
250  *****************************************************************************/
251 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
252                     aout_buffer_t * p_buffer )
253 {
254     *p_fifo->pp_last = p_buffer;
255     p_fifo->pp_last = &p_buffer->p_next;
256     *p_fifo->pp_last = NULL;
257     /* Enforce the continuity of the stream. */
258     if ( aout_DateGet( &p_fifo->end_date ) )
259     {
260         p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
261         p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
262                                                  p_buffer->i_nb_samples ); 
263     }
264     else
265     {
266         aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
267     }
268 }
269
270 /*****************************************************************************
271  * aout_FifoSet : set end_date and trash all buffers (because they aren't
272  * properly dated)
273  *****************************************************************************/
274 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
275                    mtime_t date )
276 {
277     aout_buffer_t * p_buffer;
278
279     aout_DateSet( &p_fifo->end_date, date );
280     p_buffer = p_fifo->p_first;
281     while ( p_buffer != NULL )
282     {
283         aout_buffer_t * p_next = p_buffer->p_next;
284         aout_BufferFree( p_buffer );
285         p_buffer = p_next;
286     }
287     p_fifo->p_first = NULL;
288     p_fifo->pp_last = &p_fifo->p_first;
289 }
290
291 /*****************************************************************************
292  * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
293  *****************************************************************************/
294 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
295                          mtime_t difference )
296 {
297     aout_buffer_t * p_buffer;
298
299     aout_DateMove( &p_fifo->end_date, difference );
300     p_buffer = p_fifo->p_first;
301     while ( p_buffer != NULL )
302     {
303         p_buffer->start_date += difference;
304         p_buffer->end_date += difference;
305         p_buffer = p_buffer->p_next;
306     }
307 }
308
309 /*****************************************************************************
310  * aout_FifoNextStart : return the current end_date
311  *****************************************************************************/
312 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
313 {
314     return aout_DateGet( &p_fifo->end_date );
315 }
316
317 /*****************************************************************************
318  * aout_FifoPop : get the next buffer out of the FIFO
319  *****************************************************************************/
320 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
321 {
322     aout_buffer_t * p_buffer;
323     p_buffer = p_fifo->p_first;
324     if ( p_buffer == NULL ) return NULL;
325     p_fifo->p_first = p_buffer->p_next;
326     if ( p_fifo->p_first == NULL )
327     {
328         p_fifo->pp_last = &p_fifo->p_first;
329     }
330
331     return p_buffer;
332 }
333
334 /*****************************************************************************
335  * aout_FifoDestroy : destroy a FIFO and its buffers
336  *****************************************************************************/
337 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
338 {
339     aout_buffer_t * p_buffer;
340
341     p_buffer = p_fifo->p_first;
342     while ( p_buffer != NULL )
343     {
344         aout_buffer_t * p_next = p_buffer->p_next;
345         aout_BufferFree( p_buffer );
346         p_buffer = p_next;
347     }
348 }
349
350
351 /*
352  * Date management (internal and external)
353  */
354
355 /*****************************************************************************
356  * aout_DateInit : set the divider of an audio_date_t
357  *****************************************************************************/
358 void aout_DateInit( audio_date_t * p_date, u32 i_divider )
359 {
360     p_date->date = 0;
361     p_date->i_divider = i_divider;
362     p_date->i_remainder = 0;
363 }
364
365 /*****************************************************************************
366  * aout_DateSet : set the date of an audio_date_t
367  *****************************************************************************/
368 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
369 {
370     p_date->date = new_date;
371     p_date->i_remainder = 0;
372 }
373
374 /*****************************************************************************
375  * aout_DateMove : move forwards or backwards the date of an audio_date_t
376  *****************************************************************************/
377 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
378 {
379     p_date->date += difference;
380 }
381
382 /*****************************************************************************
383  * aout_DateGet : get the date of an audio_date_t
384  *****************************************************************************/
385 mtime_t aout_DateGet( const audio_date_t * p_date )
386 {
387     return p_date->date;
388 }
389
390 /*****************************************************************************
391  * aout_DateIncrement : increment the date and return the result, taking
392  * into account rounding errors
393  *****************************************************************************/
394 mtime_t aout_DateIncrement( audio_date_t * p_date, u32 i_nb_samples )
395 {
396     mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
397     p_date->date += i_dividend / p_date->i_divider;
398     p_date->i_remainder += i_dividend % p_date->i_divider;
399     if ( p_date->i_remainder >= p_date->i_divider )
400     {
401         /* This is Bresenham algorithm. */
402         p_date->date++;
403         p_date->i_remainder -= p_date->i_divider;
404     }
405     return p_date->date;
406 }
407