1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: common.c,v 1.3 2002/10/20 12:23:48 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
32 #include "audio_output.h"
33 #include "aout_internal.h"
37 * Instances management (internal and external)
40 /*****************************************************************************
41 * aout_New: initialize aout structure
42 *****************************************************************************/
43 aout_instance_t * __aout_New( vlc_object_t * p_parent )
45 aout_instance_t * p_aout;
47 /* Allocate descriptor. */
48 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
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;
62 vlc_object_attach( p_aout, p_parent->p_vlc );
67 /*****************************************************************************
68 * aout_Delete: destroy aout structure
69 *****************************************************************************/
70 void aout_Delete( aout_instance_t * p_aout )
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 );
77 vlc_object_destroy( p_aout );
82 * Formats management (internal and external)
85 /*****************************************************************************
86 * aout_FormatNbChannels : return the number of channels
87 *****************************************************************************/
88 int aout_FormatNbChannels( audio_sample_format_t * p_format )
92 switch ( p_format->i_channels & AOUT_CHAN_MASK )
94 case AOUT_CHAN_CHANNEL1:
95 case AOUT_CHAN_CHANNEL2:
100 case AOUT_CHAN_CHANNEL:
101 case AOUT_CHAN_STEREO:
102 case AOUT_CHAN_DOLBY:
124 if ( p_format->i_channels & AOUT_CHAN_LFE )
130 /*****************************************************************************
131 * aout_FormatPrepare : compute the number of bytes per frame & frame length
132 *****************************************************************************/
133 void aout_FormatPrepare( audio_sample_format_t * p_format )
137 switch ( p_format->i_format )
139 case VLC_FOURCC('u','8',' ',' '):
140 case VLC_FOURCC('s','8',' ',' '):
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'):
151 case VLC_FOURCC('f','l','3','2'):
152 case VLC_FOURCC('f','i','3','2'):
156 case VLC_FOURCC('s','p','d','i'):
157 case VLC_FOURCC('a','5','2',' '):
158 case VLC_FOURCC('d','t','s',' '):
160 /* For these formats the caller has to indicate the parameters
165 p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
166 p_format->i_frame_length = 1;
169 /*****************************************************************************
170 * FormatPrintChannels : print a channel in a human-readable form
171 *****************************************************************************/
172 static const char * FormatPrintChannels( int i_channels )
174 switch ( i_channels )
176 case AOUT_CHAN_CHANNEL: return "CHANNEL";
177 case AOUT_CHAN_CHANNEL1: return "CHANNEL1";
178 case AOUT_CHAN_CHANNEL2: return "CHANNEL2";
179 case AOUT_CHAN_MONO: return "MONO";
180 case AOUT_CHAN_STEREO: return "STEREO";
181 case AOUT_CHAN_3F: return "3F";
182 case AOUT_CHAN_2F1R: return "2F1R";
183 case AOUT_CHAN_3F1R: return "3F1R";
184 case AOUT_CHAN_2F2R: return "2F2R";
185 case AOUT_CHAN_3F2R: return "3F2R";
186 case AOUT_CHAN_DOLBY: return "DOLBY";
187 case AOUT_CHAN_CHANNEL | AOUT_CHAN_LFE: return "CHANNEL|LFE";
188 case AOUT_CHAN_CHANNEL1 | AOUT_CHAN_LFE: return "CHANNEL1|LFE";
189 case AOUT_CHAN_CHANNEL2 | AOUT_CHAN_LFE: return "CHANNEL2|LFE";
190 case AOUT_CHAN_MONO | AOUT_CHAN_LFE: return "MONO|LFE";
191 case AOUT_CHAN_STEREO | AOUT_CHAN_LFE: return "STEREO|LFE";
192 case AOUT_CHAN_3F | AOUT_CHAN_LFE: return "3F|LFE";
193 case AOUT_CHAN_2F1R | AOUT_CHAN_LFE: return "2F1R|LFE";
194 case AOUT_CHAN_3F1R | AOUT_CHAN_LFE: return "3F1R|LFE";
195 case AOUT_CHAN_2F2R | AOUT_CHAN_LFE: return "2F2R|LFE";
196 case AOUT_CHAN_3F2R | AOUT_CHAN_LFE: return "3F2R|LFE";
197 case AOUT_CHAN_DOLBY | AOUT_CHAN_LFE: return "DOLBY|LFE";
203 /*****************************************************************************
204 * aout_FormatPrint : print a format in a human-readable form
205 *****************************************************************************/
206 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
207 audio_sample_format_t * p_format )
209 msg_Dbg( p_aout, "%s format='%4.4s' rate=%d channels=%s", psz_text,
210 (char *)&p_format->i_format, p_format->i_rate,
211 FormatPrintChannels( p_format->i_channels ) );
214 /*****************************************************************************
215 * aout_FormatsPrint : print two formats in a human-readable form
216 *****************************************************************************/
217 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
218 audio_sample_format_t * p_format1,
219 audio_sample_format_t * p_format2 )
221 msg_Dbg( p_aout, "%s format='%4.4s'->'%4.4s' rate=%d->%d channels=%s->%s",
223 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
224 p_format1->i_rate, p_format2->i_rate,
225 FormatPrintChannels( p_format1->i_channels ),
226 FormatPrintChannels( p_format2->i_channels ) );
231 * FIFO management (internal) - please understand that solving race conditions
232 * is _your_ job, ie. in the audio output you should own the mixer lock
233 * before calling any of these functions.
236 /*****************************************************************************
237 * aout_FifoInit : initialize the members of a FIFO
238 *****************************************************************************/
239 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
242 p_fifo->p_first = NULL;
243 p_fifo->pp_last = &p_fifo->p_first;
244 aout_DateInit( &p_fifo->end_date, i_rate );
247 /*****************************************************************************
248 * aout_FifoPush : push a packet into the FIFO
249 *****************************************************************************/
250 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
251 aout_buffer_t * p_buffer )
253 *p_fifo->pp_last = p_buffer;
254 p_fifo->pp_last = &p_buffer->p_next;
255 *p_fifo->pp_last = NULL;
256 /* Enforce the continuity of the stream. */
257 if ( aout_DateGet( &p_fifo->end_date ) )
259 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
260 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
261 p_buffer->i_nb_samples );
265 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
269 /*****************************************************************************
270 * aout_FifoSet : set end_date and trash all buffers (because they aren't
272 *****************************************************************************/
273 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
276 aout_buffer_t * p_buffer;
278 aout_DateSet( &p_fifo->end_date, date );
279 p_buffer = p_fifo->p_first;
280 while ( p_buffer != NULL )
282 aout_buffer_t * p_next = p_buffer->p_next;
283 aout_BufferFree( p_buffer );
286 p_fifo->p_first = NULL;
287 p_fifo->pp_last = &p_fifo->p_first;
290 /*****************************************************************************
291 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
292 *****************************************************************************/
293 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
296 aout_buffer_t * p_buffer;
298 aout_DateMove( &p_fifo->end_date, difference );
299 p_buffer = p_fifo->p_first;
300 while ( p_buffer != NULL )
302 p_buffer->start_date += difference;
303 p_buffer->end_date += difference;
304 p_buffer = p_buffer->p_next;
308 /*****************************************************************************
309 * aout_FifoNextStart : return the current end_date
310 *****************************************************************************/
311 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
313 return aout_DateGet( &p_fifo->end_date );
316 /*****************************************************************************
317 * aout_FifoPop : get the next buffer out of the FIFO
318 *****************************************************************************/
319 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
321 aout_buffer_t * p_buffer;
322 p_buffer = p_fifo->p_first;
323 if ( p_buffer == NULL ) return NULL;
324 p_fifo->p_first = p_buffer->p_next;
325 if ( p_fifo->p_first == NULL )
327 p_fifo->pp_last = &p_fifo->p_first;
333 /*****************************************************************************
334 * aout_FifoDestroy : destroy a FIFO and its buffers
335 *****************************************************************************/
336 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
338 aout_buffer_t * p_buffer;
340 p_buffer = p_fifo->p_first;
341 while ( p_buffer != NULL )
343 aout_buffer_t * p_next = p_buffer->p_next;
344 aout_BufferFree( p_buffer );
351 * Date management (internal and external)
354 /*****************************************************************************
355 * aout_DateInit : set the divider of an audio_date_t
356 *****************************************************************************/
357 void aout_DateInit( audio_date_t * p_date, u32 i_divider )
360 p_date->i_divider = i_divider;
361 p_date->i_remainder = 0;
364 /*****************************************************************************
365 * aout_DateSet : set the date of an audio_date_t
366 *****************************************************************************/
367 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
369 p_date->date = new_date;
370 p_date->i_remainder = 0;
373 /*****************************************************************************
374 * aout_DateMove : move forwards or backwards the date of an audio_date_t
375 *****************************************************************************/
376 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
378 p_date->date += difference;
381 /*****************************************************************************
382 * aout_DateGet : get the date of an audio_date_t
383 *****************************************************************************/
384 mtime_t aout_DateGet( const audio_date_t * p_date )
389 /*****************************************************************************
390 * aout_DateIncrement : increment the date and return the result, taking
391 * into account rounding errors
392 *****************************************************************************/
393 mtime_t aout_DateIncrement( audio_date_t * p_date, u32 i_nb_samples )
395 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
396 p_date->date += i_dividend / p_date->i_divider;
397 p_date->i_remainder += i_dividend % p_date->i_divider;
398 if ( p_date->i_remainder >= p_date->i_divider )
400 /* This is Bresenham algorithm. */
402 p_date->i_remainder -= p_date->i_divider;