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