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 *****************************************************************************/
33 #include <vlc_common.h>
35 #include "aout_internal.h"
38 * Instances management (internal and external)
41 #define AOUT_ASSERT_FIFO_LOCKED aout_assert_fifo_locked(p_aout, p_fifo)
42 static inline void aout_assert_fifo_locked( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
45 if( p_fifo == &p_aout->output.fifo )
46 vlc_assert_locked( &p_aout->output_fifo_lock );
50 for( i = 0; i < p_aout->i_nb_inputs; i++ )
52 if( p_fifo == &p_aout->pp_inputs[i]->fifo)
54 vlc_assert_locked( &p_aout->input_fifos_lock );
58 if( i == p_aout->i_nb_inputs )
59 vlc_assert_locked( &p_aout->mixer_lock );
68 static void aout_Destructor( vlc_object_t * p_this );
70 /*****************************************************************************
71 * aout_New: initialize aout structure
72 *****************************************************************************/
73 aout_instance_t * __aout_New( vlc_object_t * p_parent )
75 aout_instance_t * p_aout;
78 /* Allocate descriptor. */
79 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
85 /* Initialize members. */
86 vlc_mutex_init( &p_aout->input_fifos_lock );
87 vlc_mutex_init( &p_aout->mixer_lock );
88 vlc_mutex_init( &p_aout->output_fifo_lock );
89 p_aout->i_nb_inputs = 0;
90 p_aout->mixer.f_multiplier = 1.0;
91 p_aout->mixer.b_error = 1;
92 p_aout->output.b_error = 1;
93 p_aout->output.b_starving = 1;
95 var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
97 var_Set( p_aout, "intf-change", val );
99 vlc_object_set_destructor( p_aout, aout_Destructor );
104 /*****************************************************************************
105 * aout_Destructor: destroy aout structure
106 *****************************************************************************/
107 static void aout_Destructor( vlc_object_t * p_this )
109 aout_instance_t * p_aout = (aout_instance_t *)p_this;
110 vlc_mutex_destroy( &p_aout->input_fifos_lock );
111 vlc_mutex_destroy( &p_aout->mixer_lock );
112 vlc_mutex_destroy( &p_aout->output_fifo_lock );
117 * Formats management (internal and external)
120 /*****************************************************************************
121 * aout_FormatNbChannels : return the number of channels
122 *****************************************************************************/
123 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
125 static const uint32_t pi_channels[] =
126 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
127 AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
128 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
129 unsigned int i_nb = 0, i;
131 for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
133 if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
139 /*****************************************************************************
140 * aout_BitsPerSample : get the number of bits per sample
141 *****************************************************************************/
142 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
146 case VLC_FOURCC('u','8',' ',' '):
147 case VLC_FOURCC('s','8',' ',' '):
150 case VLC_FOURCC('u','1','6','l'):
151 case VLC_FOURCC('s','1','6','l'):
152 case VLC_FOURCC('u','1','6','b'):
153 case VLC_FOURCC('s','1','6','b'):
156 case VLC_FOURCC('u','2','4','l'):
157 case VLC_FOURCC('s','2','4','l'):
158 case VLC_FOURCC('u','2','4','b'):
159 case VLC_FOURCC('s','2','4','b'):
162 case VLC_FOURCC('s','3','2','l'):
163 case VLC_FOURCC('s','3','2','b'):
164 case VLC_FOURCC('f','l','3','2'):
165 case VLC_FOURCC('f','i','3','2'):
168 case VLC_FOURCC('f','l','6','4'):
172 /* For these formats the caller has to indicate the parameters
178 /*****************************************************************************
179 * aout_FormatPrepare : compute the number of bytes per frame & frame length
180 *****************************************************************************/
181 void aout_FormatPrepare( audio_sample_format_t * p_format )
183 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
184 if( p_format->i_bitspersample > 0 )
186 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
187 * aout_FormatNbChannels( p_format );
188 p_format->i_frame_length = 1;
192 /*****************************************************************************
193 * aout_FormatPrintChannels : print a channel in a human-readable form
194 *****************************************************************************/
195 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
197 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
199 case AOUT_CHAN_CENTER:
200 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
201 || (p_format->i_original_channels
202 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
204 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
207 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
208 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
210 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
211 return "Dolby/Reverse";
212 return "Stereo/Reverse";
216 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
218 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
220 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
221 return "Stereo/Mono";
222 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
223 return "Stereo/Left";
224 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
225 return "Stereo/Right";
228 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
230 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
232 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
233 | AOUT_CHAN_REARCENTER:
235 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
236 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
238 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
239 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
242 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
243 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
244 || (p_format->i_original_channels
245 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
247 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
250 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
251 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
253 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
254 return "Dual-mono/LFE";
255 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
257 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
258 return "Stereo/Left/LFE";
259 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
260 return "Stereo/Right/LFE";
262 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
264 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
267 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
268 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
270 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
271 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
273 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
274 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
276 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
277 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
278 | AOUT_CHAN_MIDDLERIGHT:
280 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
281 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
282 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
289 /*****************************************************************************
290 * aout_FormatPrint : print a format in a human-readable form
291 *****************************************************************************/
292 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
293 const audio_sample_format_t * p_format )
295 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
296 (char *)&p_format->i_format, p_format->i_rate,
297 aout_FormatPrintChannels( p_format ),
298 p_format->i_frame_length, p_format->i_bytes_per_frame );
301 /*****************************************************************************
302 * aout_FormatsPrint : print two formats in a human-readable form
303 *****************************************************************************/
304 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
305 const audio_sample_format_t * p_format1,
306 const audio_sample_format_t * p_format2 )
308 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
310 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
311 p_format1->i_rate, p_format2->i_rate,
312 aout_FormatPrintChannels( p_format1 ),
313 aout_FormatPrintChannels( p_format2 ) );
318 * FIFO management (internal) - please understand that solving race conditions
319 * is _your_ job, ie. in the audio output you should own the mixer lock
320 * before calling any of these functions.
323 /*****************************************************************************
324 * aout_FifoInit : initialize the members of a FIFO
325 *****************************************************************************/
326 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
329 AOUT_ASSERT_FIFO_LOCKED;
333 msg_Err( p_aout, "initialising fifo with zero divider" );
336 p_fifo->p_first = NULL;
337 p_fifo->pp_last = &p_fifo->p_first;
338 aout_DateInit( &p_fifo->end_date, i_rate );
341 /*****************************************************************************
342 * aout_FifoPush : push a packet into the FIFO
343 *****************************************************************************/
344 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
345 aout_buffer_t * p_buffer )
348 AOUT_ASSERT_FIFO_LOCKED;
350 *p_fifo->pp_last = p_buffer;
351 p_fifo->pp_last = &p_buffer->p_next;
352 *p_fifo->pp_last = NULL;
353 /* Enforce the continuity of the stream. */
354 if ( aout_DateGet( &p_fifo->end_date ) )
356 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
357 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
358 p_buffer->i_nb_samples );
362 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
366 /*****************************************************************************
367 * aout_FifoSet : set end_date and trash all buffers (because they aren't
369 *****************************************************************************/
370 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
373 aout_buffer_t * p_buffer;
375 AOUT_ASSERT_FIFO_LOCKED;
377 aout_DateSet( &p_fifo->end_date, date );
378 p_buffer = p_fifo->p_first;
379 while ( p_buffer != NULL )
381 aout_buffer_t * p_next = p_buffer->p_next;
382 aout_BufferFree( p_buffer );
385 p_fifo->p_first = NULL;
386 p_fifo->pp_last = &p_fifo->p_first;
389 /*****************************************************************************
390 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
391 *****************************************************************************/
392 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
395 aout_buffer_t * p_buffer;
397 AOUT_ASSERT_FIFO_LOCKED;
399 aout_DateMove( &p_fifo->end_date, difference );
400 p_buffer = p_fifo->p_first;
401 while ( p_buffer != NULL )
403 p_buffer->start_date += difference;
404 p_buffer->end_date += difference;
405 p_buffer = p_buffer->p_next;
409 /*****************************************************************************
410 * aout_FifoNextStart : return the current end_date
411 *****************************************************************************/
412 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
415 AOUT_ASSERT_FIFO_LOCKED;
416 return aout_DateGet( &p_fifo->end_date );
419 /*****************************************************************************
420 * aout_FifoFirstDate : return the playing date of the first buffer in the
422 *****************************************************************************/
423 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
426 AOUT_ASSERT_FIFO_LOCKED;
427 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
430 /*****************************************************************************
431 * aout_FifoPop : get the next buffer out of the FIFO
432 *****************************************************************************/
433 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
435 aout_buffer_t * p_buffer;
437 AOUT_ASSERT_FIFO_LOCKED;
439 p_buffer = p_fifo->p_first;
440 if ( p_buffer == NULL ) return NULL;
441 p_fifo->p_first = p_buffer->p_next;
442 if ( p_fifo->p_first == NULL )
444 p_fifo->pp_last = &p_fifo->p_first;
450 /*****************************************************************************
451 * aout_FifoDestroy : destroy a FIFO and its buffers
452 *****************************************************************************/
453 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
455 aout_buffer_t * p_buffer;
457 AOUT_ASSERT_FIFO_LOCKED;
459 p_buffer = p_fifo->p_first;
460 while ( p_buffer != NULL )
462 aout_buffer_t * p_next = p_buffer->p_next;
463 aout_BufferFree( p_buffer );
467 p_fifo->p_first = NULL;
468 p_fifo->pp_last = &p_fifo->p_first;
473 * Date management (internal and external)
476 /*****************************************************************************
477 * aout_DateInit : set the divider of an audio_date_t
478 *****************************************************************************/
479 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
482 p_date->i_divider = i_divider;
483 p_date->i_remainder = 0;
486 /*****************************************************************************
487 * aout_DateSet : set the date of an audio_date_t
488 *****************************************************************************/
489 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
491 p_date->date = new_date;
492 p_date->i_remainder = 0;
495 /*****************************************************************************
496 * aout_DateMove : move forwards or backwards the date of an audio_date_t
497 *****************************************************************************/
498 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
500 p_date->date += difference;
503 /*****************************************************************************
504 * aout_DateGet : get the date of an audio_date_t
505 *****************************************************************************/
506 mtime_t aout_DateGet( const audio_date_t * p_date )
511 /*****************************************************************************
512 * aout_DateIncrement : increment the date and return the result, taking
513 * into account rounding errors
514 *****************************************************************************/
515 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
517 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
518 p_date->date += i_dividend / p_date->i_divider;
519 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
520 if ( p_date->i_remainder >= p_date->i_divider )
522 /* This is Bresenham algorithm. */
524 p_date->i_remainder -= p_date->i_divider;
529 /*****************************************************************************
530 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
531 *****************************************************************************/
532 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
533 const uint32_t *pi_chan_order_out,
534 uint32_t i_channel_mask,
535 int i_channels, int *pi_chan_table )
537 bool b_chan_reorder = false;
540 if( i_channels > AOUT_CHAN_MAX )
543 if( pi_chan_order_in == NULL )
544 pi_chan_order_in = pi_vlc_chan_order_wg4;
545 if( pi_chan_order_out == NULL )
546 pi_chan_order_out = pi_vlc_chan_order_wg4;
548 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
550 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
552 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
554 if( i_channel_mask & pi_chan_order_out[k] ) l++;
557 pi_chan_table[j++] = l;
560 for( i = 0; i < i_channels; i++ )
562 if( pi_chan_table[i] != i ) b_chan_reorder = true;
565 return b_chan_reorder;
568 /*****************************************************************************
569 * aout_ChannelReorder :
570 *****************************************************************************/
571 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
572 int i_channels, const int *pi_chan_table,
573 int i_bits_per_sample )
575 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
578 if( i_bits_per_sample == 8 )
580 for( i = 0; i < i_buffer / i_channels; i++ )
582 for( j = 0; j < i_channels; j++ )
584 p_tmp[pi_chan_table[j]] = p_buf[j];
587 memcpy( p_buf, p_tmp, i_channels );
591 else if( i_bits_per_sample == 16 )
593 for( i = 0; i < i_buffer / i_channels / 2; i++ )
595 for( j = 0; j < i_channels; j++ )
597 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
598 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
601 memcpy( p_buf, p_tmp, 2 * i_channels );
602 p_buf += 2 * i_channels;
605 else if( i_bits_per_sample == 24 )
607 for( i = 0; i < i_buffer / i_channels / 3; i++ )
609 for( j = 0; j < i_channels; j++ )
611 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
612 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
613 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
616 memcpy( p_buf, p_tmp, 3 * i_channels );
617 p_buf += 3 * i_channels;
620 else if( i_bits_per_sample == 32 )
622 for( i = 0; i < i_buffer / i_channels / 4; i++ )
624 for( j = 0; j < i_channels; j++ )
626 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
627 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
628 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
629 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
632 memcpy( p_buf, p_tmp, 4 * i_channels );
633 p_buf += 4 * i_channels;