]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
* configure.in: Fixed detection of Qt-embedded.
[vlc] / src / audio_output / audio_output.c
1 /*****************************************************************************
2  * audio_output.c : audio output instance miscellaneous functions
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: audio_output.c,v 1.102 2002/09/16 20:46:38 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 #ifdef HAVE_ALLOCA_H
33 #   include <alloca.h>
34 #endif
35
36 #include "audio_output.h"
37 #include "aout_internal.h"
38
39 /*
40  * Instances management (see also input.c:aout_InputNew())
41  */
42
43 /*****************************************************************************
44  * aout_NewInstance: initialize aout structure
45  *****************************************************************************/
46 aout_instance_t * __aout_NewInstance( vlc_object_t * p_parent )
47 {
48     aout_instance_t * p_aout;
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_parent, &p_aout->input_fifos_lock );
59     vlc_mutex_init( p_parent, &p_aout->mixer_lock );
60     vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
61     p_aout->i_nb_inputs = 0;
62     p_aout->mixer.f_multiplier = 1.0;
63
64     vlc_object_attach( p_aout, p_parent->p_vlc );
65
66     return p_aout;
67 }
68
69 /*****************************************************************************
70  * aout_DeleteInstance: destroy aout structure
71  *****************************************************************************/
72 void aout_DeleteInstance( aout_instance_t * p_aout )
73 {
74     vlc_mutex_destroy( &p_aout->input_fifos_lock );
75     vlc_mutex_destroy( &p_aout->mixer_lock );
76     vlc_mutex_destroy( &p_aout->output_fifo_lock );
77
78     /* Free structure. */
79     vlc_object_destroy( p_aout );
80 }
81
82
83 /*
84  * Buffer management (interface to the decoders)
85  */
86
87 /*****************************************************************************
88  * aout_BufferNew : ask for a new empty buffer
89  *****************************************************************************/
90 aout_buffer_t * aout_BufferNew( aout_instance_t * p_aout,
91                                 aout_input_t * p_input,
92                                 size_t i_nb_samples )
93 {
94     aout_buffer_t * p_buffer;
95     mtime_t duration = (1000000 * (mtime_t)i_nb_samples)
96                         / p_input->input.i_rate;
97
98     /* This necessarily allocates in the heap. */
99     aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
100     p_buffer->i_nb_samples = i_nb_samples;
101     p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
102                               / p_input->input.i_frame_length;
103
104     if ( p_buffer == NULL )
105     {
106         msg_Err( p_aout, "NULL buffer !" );
107     }
108     else
109     {
110         p_buffer->start_date = p_buffer->end_date = 0;
111     }
112
113     return p_buffer;
114 }
115
116 /*****************************************************************************
117  * aout_BufferDelete : destroy an undecoded buffer
118  *****************************************************************************/
119 void aout_BufferDelete( aout_instance_t * p_aout, aout_input_t * p_input,
120                         aout_buffer_t * p_buffer )
121 {
122     aout_BufferFree( p_buffer );
123 }
124
125 /*****************************************************************************
126  * aout_BufferPlay : filter & mix the decoded buffer
127  *****************************************************************************/
128 void aout_BufferPlay( aout_instance_t * p_aout, aout_input_t * p_input,
129                       aout_buffer_t * p_buffer )
130 {
131     if ( p_buffer->start_date == 0 )
132     {
133         msg_Warn( p_aout, "non-dated buffer received" );
134         aout_BufferFree( p_buffer );
135     }
136     else
137     {
138         p_buffer->end_date = p_buffer->start_date
139                                 + (mtime_t)(p_buffer->i_nb_samples * 1000000)
140                                     / p_input->input.i_rate;
141     }
142
143     /* If the buffer is too early, wait a while. */
144     mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
145
146     aout_InputPlay( p_aout, p_input, p_buffer );
147
148     /* Run the mixer if it is able to run. */
149     aout_MixerRun( p_aout );
150 }
151
152
153 /*
154  * Formats management
155  */
156
157 /*****************************************************************************
158  * aout_FormatNbChannels : return the number of channels
159  *****************************************************************************/
160 int aout_FormatNbChannels( audio_sample_format_t * p_format )
161 {
162     int i_nb;
163
164     switch ( p_format->i_channels & AOUT_CHAN_MASK )
165     {
166     case AOUT_CHAN_CHANNEL1:
167     case AOUT_CHAN_CHANNEL2:
168     case AOUT_CHAN_MONO:
169         i_nb = 1;
170         break;
171
172     case AOUT_CHAN_CHANNEL:
173     case AOUT_CHAN_STEREO:
174     case AOUT_CHAN_DOLBY:
175         i_nb = 2;
176         break;
177
178     case AOUT_CHAN_3F:
179     case AOUT_CHAN_2F1R:
180         i_nb = 3;
181         break;
182
183     case AOUT_CHAN_3F1R:
184     case AOUT_CHAN_2F2R:
185         i_nb = 4;
186         break;
187
188     case AOUT_CHAN_3F2R:
189         i_nb = 5;
190         break;
191
192     default:
193         i_nb = 0;
194     }
195
196     if ( p_format->i_channels & AOUT_CHAN_LFE )
197         return i_nb + 1;
198     else
199         return i_nb;
200 }
201
202 /*****************************************************************************
203  * aout_FormatPrepare : compute the number of bytes per frame & frame length
204  *****************************************************************************/
205 void aout_FormatPrepare( audio_sample_format_t * p_format )
206 {
207     int i_result;
208
209     switch ( p_format->i_format )
210     {
211     case AOUT_FMT_U8:
212     case AOUT_FMT_S8:
213         i_result = 1;
214         break;
215
216     case AOUT_FMT_U16_LE:
217     case AOUT_FMT_U16_BE:
218     case AOUT_FMT_S16_LE:
219     case AOUT_FMT_S16_BE:
220         i_result = 2;
221         break;
222
223     case AOUT_FMT_FLOAT32:
224     case AOUT_FMT_FIXED32:
225         i_result = 4;
226         break;
227
228     case AOUT_FMT_SPDIF:
229     case AOUT_FMT_A52:
230     case AOUT_FMT_DTS:
231         /* For these formats the caller has to indicate the parameters
232          * by hand. */
233         return;
234
235     default:
236         i_result = 0; /* will segfault much sooner... */
237     }
238
239     p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
240     p_format->i_frame_length = 1;
241 }
242
243
244 /*
245  * FIFO management (internal) - please understand that solving race conditions
246  * is _your_ job, ie. in the audio output you should own the mixer lock
247  * before calling any of these functions.
248  */
249
250 /*****************************************************************************
251  * aout_FifoInit : initialize the members of a FIFO
252  *****************************************************************************/
253 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
254                     u32 i_rate )
255 {
256     p_fifo->p_first = NULL;
257     p_fifo->pp_last = &p_fifo->p_first;
258     aout_DateInit( &p_fifo->end_date, i_rate );
259 }
260
261 /*****************************************************************************
262  * aout_FifoPush : push a packet into the FIFO
263  *****************************************************************************/
264 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
265                     aout_buffer_t * p_buffer )
266 {
267     *p_fifo->pp_last = p_buffer;
268     p_fifo->pp_last = &p_buffer->p_next;
269     *p_fifo->pp_last = NULL;
270     /* Enforce the continuity of the stream. */
271     if ( aout_DateGet( &p_fifo->end_date ) )
272     {
273         p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
274         p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
275                                                  p_buffer->i_nb_samples ); 
276     }
277     else
278     {
279         aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
280     }
281 }
282
283 /*****************************************************************************
284  * aout_FifoSet : set end_date and trash all buffers (because they aren't
285  * properly dated)
286  *****************************************************************************/
287 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
288                    mtime_t date )
289 {
290     aout_buffer_t * p_buffer;
291
292     aout_DateSet( &p_fifo->end_date, date );
293     p_buffer = p_fifo->p_first;
294     while ( p_buffer != NULL )
295     {
296         aout_buffer_t * p_next = p_buffer->p_next;
297         aout_BufferFree( p_buffer );
298         p_buffer = p_next;
299     }
300     p_fifo->p_first = NULL;
301     p_fifo->pp_last = &p_fifo->p_first;
302 }
303
304 /*****************************************************************************
305  * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
306  *****************************************************************************/
307 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
308                          mtime_t difference )
309 {
310     aout_buffer_t * p_buffer;
311
312     aout_DateMove( &p_fifo->end_date, difference );
313     p_buffer = p_fifo->p_first;
314     while ( p_buffer != NULL )
315     {
316         p_buffer->start_date += difference;
317         p_buffer->end_date += difference;
318         p_buffer = p_buffer->p_next;
319     }
320 }
321
322 /*****************************************************************************
323  * aout_FifoNextStart : return the current end_date
324  *****************************************************************************/
325 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
326 {
327     return aout_DateGet( &p_fifo->end_date );
328 }
329
330 /*****************************************************************************
331  * aout_FifoPop : get the next buffer out of the FIFO
332  *****************************************************************************/
333 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
334 {
335     aout_buffer_t * p_buffer;
336     p_buffer = p_fifo->p_first;
337     if ( p_buffer == NULL ) return NULL;
338     p_fifo->p_first = p_buffer->p_next;
339     if ( p_fifo->p_first == NULL )
340     {
341         p_fifo->pp_last = &p_fifo->p_first;
342     }
343
344     return p_buffer;
345 }
346
347 /*****************************************************************************
348  * aout_FifoDestroy : destroy a FIFO and its buffers
349  *****************************************************************************/
350 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
351 {
352     aout_buffer_t * p_buffer;
353
354     p_buffer = p_fifo->p_first;
355     while ( p_buffer != NULL )
356     {
357         aout_buffer_t * p_next = p_buffer->p_next;
358         aout_BufferFree( p_buffer );
359         p_buffer = p_next;
360     }
361 }
362
363
364 /*
365  * Date management (internal and external)
366  */
367
368 /*****************************************************************************
369  * aout_DateInit : set the divider of an audio_date_t
370  *****************************************************************************/
371 void aout_DateInit( audio_date_t * p_date, u32 i_divider )
372 {
373     p_date->date = 0;
374     p_date->i_divider = i_divider;
375     p_date->i_remainder = 0;
376 }
377
378 /*****************************************************************************
379  * aout_DateSet : set the date of an audio_date_t
380  *****************************************************************************/
381 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
382 {
383     p_date->date = new_date;
384     p_date->i_remainder = 0;
385 }
386
387 /*****************************************************************************
388  * aout_DateMove : move forwards or backwards the date of an audio_date_t
389  *****************************************************************************/
390 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
391 {
392     p_date->date += difference;
393 }
394
395 /*****************************************************************************
396  * aout_DateGet : get the date of an audio_date_t
397  *****************************************************************************/
398 mtime_t aout_DateGet( const audio_date_t * p_date )
399 {
400     return p_date->date;
401 }
402
403 /*****************************************************************************
404  * aout_DateIncrement : increment the date and return the result, taking
405  * into account rounding errors
406  *****************************************************************************/
407 mtime_t aout_DateIncrement( audio_date_t * p_date, u32 i_nb_samples )
408 {
409     mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
410     p_date->date += i_dividend / p_date->i_divider;
411     p_date->i_remainder += i_dividend % p_date->i_divider;
412     if ( p_date->i_remainder >= p_date->i_divider )
413     {
414         /* This is Bresenham algorithm. */
415         p_date->date++;
416         p_date->i_remainder -= p_date->i_divider;
417     }
418     return p_date->date;
419 }
420