1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
32 #include "aout_internal.h"
35 * Instances management (internal and external)
38 /*****************************************************************************
39 * aout_New: initialize aout structure
40 *****************************************************************************/
41 aout_instance_t * __aout_New( vlc_object_t * p_parent )
43 aout_instance_t * p_aout;
46 /* Allocate descriptor. */
47 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
53 /* Initialize members. */
54 vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
55 vlc_mutex_init( p_parent, &p_aout->mixer_lock );
56 vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
57 p_aout->i_nb_inputs = 0;
58 p_aout->mixer.f_multiplier = 1.0;
59 p_aout->mixer.b_error = 1;
60 p_aout->output.b_error = 1;
61 p_aout->output.b_starving = 1;
63 var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
64 val.b_bool = VLC_TRUE;
65 var_Set( p_aout, "intf-change", val );
70 /*****************************************************************************
71 * aout_Delete: destroy aout structure
72 *****************************************************************************/
73 void aout_Delete( aout_instance_t * p_aout )
75 var_Destroy( p_aout, "intf-change" );
77 vlc_mutex_destroy( &p_aout->input_fifos_lock );
78 vlc_mutex_destroy( &p_aout->mixer_lock );
79 vlc_mutex_destroy( &p_aout->output_fifo_lock );
82 vlc_object_destroy( p_aout );
87 * Formats management (internal and external)
90 /*****************************************************************************
91 * aout_FormatNbChannels : return the number of channels
92 *****************************************************************************/
93 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
95 static const uint32_t pi_channels[] =
96 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
97 AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
98 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
99 unsigned int i_nb = 0, i;
101 for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
103 if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
109 /*****************************************************************************
110 * aout_FormatPrepare : compute the number of bytes per frame & frame length
111 *****************************************************************************/
112 void aout_FormatPrepare( audio_sample_format_t * p_format )
116 switch ( p_format->i_format )
118 case VLC_FOURCC('u','8',' ',' '):
119 case VLC_FOURCC('s','8',' ',' '):
123 case VLC_FOURCC('u','1','6','l'):
124 case VLC_FOURCC('s','1','6','l'):
125 case VLC_FOURCC('u','1','6','b'):
126 case VLC_FOURCC('s','1','6','b'):
130 case VLC_FOURCC('u','2','4','l'):
131 case VLC_FOURCC('s','2','4','l'):
132 case VLC_FOURCC('u','2','4','b'):
133 case VLC_FOURCC('s','2','4','b'):
137 case VLC_FOURCC('f','l','3','2'):
138 case VLC_FOURCC('f','i','3','2'):
142 case VLC_FOURCC('s','p','d','i'):
143 case VLC_FOURCC('s','p','d','b'): /* Big endian spdif output */
144 case VLC_FOURCC('a','5','2',' '):
145 case VLC_FOURCC('d','t','s',' '):
146 case VLC_FOURCC('m','p','g','a'):
147 case VLC_FOURCC('m','p','g','3'):
149 /* For these formats the caller has to indicate the parameters
154 p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
155 p_format->i_frame_length = 1;
158 /*****************************************************************************
159 * aout_FormatPrintChannels : print a channel in a human-readable form
160 *****************************************************************************/
161 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
163 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
165 case AOUT_CHAN_CENTER:
166 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
167 || (p_format->i_original_channels
168 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
170 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
173 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
174 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
176 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
177 return "Dolby/Reverse";
178 return "Stereo/Reverse";
182 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
184 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
186 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
187 return "Stereo/Mono";
188 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
189 return "Stereo/Left";
190 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
191 return "Stereo/Right";
194 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
196 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
198 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
199 | AOUT_CHAN_REARCENTER:
201 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
202 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
204 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
205 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
208 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
209 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
210 || (p_format->i_original_channels
211 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
213 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
216 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
217 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
219 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
220 return "Dual-mono/LFE";
221 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
223 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
224 return "Stereo/Left/LFE";
225 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
226 return "Stereo/Right/LFE";
228 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
230 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
233 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
234 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
236 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
237 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
239 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
240 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
242 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
243 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
244 | AOUT_CHAN_MIDDLERIGHT:
246 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
247 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
248 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
255 /*****************************************************************************
256 * aout_FormatPrint : print a format in a human-readable form
257 *****************************************************************************/
258 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
259 const audio_sample_format_t * p_format )
261 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
262 (char *)&p_format->i_format, p_format->i_rate,
263 aout_FormatPrintChannels( p_format ),
264 p_format->i_frame_length, p_format->i_bytes_per_frame );
267 /*****************************************************************************
268 * aout_FormatsPrint : print two formats in a human-readable form
269 *****************************************************************************/
270 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
271 const audio_sample_format_t * p_format1,
272 const audio_sample_format_t * p_format2 )
274 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
276 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
277 p_format1->i_rate, p_format2->i_rate,
278 aout_FormatPrintChannels( p_format1 ),
279 aout_FormatPrintChannels( p_format2 ) );
284 * FIFO management (internal) - please understand that solving race conditions
285 * is _your_ job, ie. in the audio output you should own the mixer lock
286 * before calling any of these functions.
289 /*****************************************************************************
290 * aout_FifoInit : initialize the members of a FIFO
291 *****************************************************************************/
292 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
297 msg_Err( p_aout, "initialising fifo with zero divider" );
300 p_fifo->p_first = NULL;
301 p_fifo->pp_last = &p_fifo->p_first;
302 aout_DateInit( &p_fifo->end_date, i_rate );
305 /*****************************************************************************
306 * aout_FifoPush : push a packet into the FIFO
307 *****************************************************************************/
308 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
309 aout_buffer_t * p_buffer )
312 *p_fifo->pp_last = p_buffer;
313 p_fifo->pp_last = &p_buffer->p_next;
314 *p_fifo->pp_last = NULL;
315 /* Enforce the continuity of the stream. */
316 if ( aout_DateGet( &p_fifo->end_date ) )
318 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
319 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
320 p_buffer->i_nb_samples );
324 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
328 /*****************************************************************************
329 * aout_FifoSet : set end_date and trash all buffers (because they aren't
331 *****************************************************************************/
332 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
335 aout_buffer_t * p_buffer;
338 aout_DateSet( &p_fifo->end_date, date );
339 p_buffer = p_fifo->p_first;
340 while ( p_buffer != NULL )
342 aout_buffer_t * p_next = p_buffer->p_next;
343 aout_BufferFree( p_buffer );
346 p_fifo->p_first = NULL;
347 p_fifo->pp_last = &p_fifo->p_first;
350 /*****************************************************************************
351 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
352 *****************************************************************************/
353 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
356 aout_buffer_t * p_buffer;
359 aout_DateMove( &p_fifo->end_date, difference );
360 p_buffer = p_fifo->p_first;
361 while ( p_buffer != NULL )
363 p_buffer->start_date += difference;
364 p_buffer->end_date += difference;
365 p_buffer = p_buffer->p_next;
369 /*****************************************************************************
370 * aout_FifoNextStart : return the current end_date
371 *****************************************************************************/
372 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
375 return aout_DateGet( &p_fifo->end_date );
378 /*****************************************************************************
379 * aout_FifoFirstDate : return the playing date of the first buffer in the
381 *****************************************************************************/
382 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
385 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
388 /*****************************************************************************
389 * aout_FifoPop : get the next buffer out of the FIFO
390 *****************************************************************************/
391 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
393 aout_buffer_t * p_buffer;
396 p_buffer = p_fifo->p_first;
397 if ( p_buffer == NULL ) return NULL;
398 p_fifo->p_first = p_buffer->p_next;
399 if ( p_fifo->p_first == NULL )
401 p_fifo->pp_last = &p_fifo->p_first;
407 /*****************************************************************************
408 * aout_FifoDestroy : destroy a FIFO and its buffers
409 *****************************************************************************/
410 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
412 aout_buffer_t * p_buffer;
415 p_buffer = p_fifo->p_first;
416 while ( p_buffer != NULL )
418 aout_buffer_t * p_next = p_buffer->p_next;
419 aout_BufferFree( p_buffer );
423 p_fifo->p_first = NULL;
424 p_fifo->pp_last = &p_fifo->p_first;
429 * Date management (internal and external)
432 /*****************************************************************************
433 * aout_DateInit : set the divider of an audio_date_t
434 *****************************************************************************/
435 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
438 p_date->i_divider = i_divider;
439 p_date->i_remainder = 0;
442 /*****************************************************************************
443 * aout_DateSet : set the date of an audio_date_t
444 *****************************************************************************/
445 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
447 p_date->date = new_date;
448 p_date->i_remainder = 0;
451 /*****************************************************************************
452 * aout_DateMove : move forwards or backwards the date of an audio_date_t
453 *****************************************************************************/
454 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
456 p_date->date += difference;
459 /*****************************************************************************
460 * aout_DateGet : get the date of an audio_date_t
461 *****************************************************************************/
462 mtime_t aout_DateGet( const audio_date_t * p_date )
467 /*****************************************************************************
468 * aout_DateIncrement : increment the date and return the result, taking
469 * into account rounding errors
470 *****************************************************************************/
471 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
473 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
474 p_date->date += i_dividend / p_date->i_divider;
475 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
476 if ( p_date->i_remainder >= p_date->i_divider )
478 /* This is Bresenham algorithm. */
480 p_date->i_remainder -= p_date->i_divider;
485 /*****************************************************************************
486 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
487 *****************************************************************************/
488 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
489 const uint32_t *pi_chan_order_out,
490 uint32_t i_channel_mask,
491 int i_channels, int *pi_chan_table )
493 vlc_bool_t b_chan_reorder = VLC_FALSE;
496 if( i_channels > AOUT_CHAN_MAX ) return VLC_FALSE;
498 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
500 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
502 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
504 if( i_channel_mask & pi_chan_order_out[k] ) l++;
507 pi_chan_table[j++] = l;
510 for( i = 0; i < i_channels; i++ )
512 if( pi_chan_table[i] != i ) b_chan_reorder = VLC_TRUE;
515 return b_chan_reorder;
518 /*****************************************************************************
519 * aout_ChannelReorder :
520 *****************************************************************************/
521 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
522 int i_channels, const int *pi_chan_table,
523 int i_bits_per_sample )
525 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
528 if( i_bits_per_sample == 8 )
530 for( i = 0; i < i_buffer / i_channels; i++ )
532 for( j = 0; j < i_channels; j++ )
534 p_tmp[pi_chan_table[j]] = p_buf[j];
537 memcpy( p_buf, p_tmp, i_channels );
541 else if( i_bits_per_sample == 16 )
543 for( i = 0; i < i_buffer / i_channels / 2; i++ )
545 for( j = 0; j < i_channels; j++ )
547 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
548 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
551 memcpy( p_buf, p_tmp, 2 * i_channels );
552 p_buf += 2 * i_channels;
555 else if( i_bits_per_sample == 24 )
557 for( i = 0; i < i_buffer / i_channels / 3; i++ )
559 for( j = 0; j < i_channels; j++ )
561 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
562 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
563 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
566 memcpy( p_buf, p_tmp, 3 * i_channels );
567 p_buf += 3 * i_channels;
570 else if( i_bits_per_sample == 32 )
572 for( i = 0; i < i_buffer / i_channels / 4; i++ )
574 for( j = 0; j < i_channels; j++ )
576 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
577 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
578 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
579 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
582 memcpy( p_buf, p_tmp, 4 * i_channels );
583 p_buf += 4 * i_channels;