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