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 "aout_internal.h"
36 * Instances management (internal and external)
40 static void aout_Destructor( vlc_object_t * p_this );
42 /*****************************************************************************
43 * aout_New: initialize aout structure
44 *****************************************************************************/
45 aout_instance_t * __aout_New( vlc_object_t * p_parent )
47 aout_instance_t * p_aout;
50 /* Allocate descriptor. */
51 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
57 /* Initialize members. */
58 vlc_mutex_init( &p_aout->input_fifos_lock );
59 vlc_mutex_init( &p_aout->mixer_lock );
60 vlc_mutex_init( &p_aout->output_fifo_lock );
61 p_aout->i_nb_inputs = 0;
62 p_aout->mixer.f_multiplier = 1.0;
63 p_aout->mixer.b_error = 1;
64 p_aout->output.b_error = 1;
65 p_aout->output.b_starving = 1;
67 var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
69 var_Set( p_aout, "intf-change", val );
71 vlc_object_set_destructor( p_aout, aout_Destructor );
76 /*****************************************************************************
77 * aout_Destructor: destroy aout structure
78 *****************************************************************************/
79 static void aout_Destructor( vlc_object_t * p_this )
81 aout_instance_t * p_aout = (aout_instance_t *)p_this;
82 vlc_mutex_destroy( &p_aout->input_fifos_lock );
83 vlc_mutex_destroy( &p_aout->mixer_lock );
84 vlc_mutex_destroy( &p_aout->output_fifo_lock );
89 * Formats management (internal and external)
92 /*****************************************************************************
93 * aout_FormatNbChannels : return the number of channels
94 *****************************************************************************/
95 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
97 static const uint32_t pi_channels[] =
98 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
99 AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
100 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
101 unsigned int i_nb = 0, i;
103 for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
105 if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
111 /*****************************************************************************
112 * aout_FormatPrepare : compute the number of bytes per frame & frame length
113 *****************************************************************************/
114 void aout_FormatPrepare( audio_sample_format_t * p_format )
118 switch ( p_format->i_format )
120 case VLC_FOURCC('u','8',' ',' '):
121 case VLC_FOURCC('s','8',' ',' '):
125 case VLC_FOURCC('u','1','6','l'):
126 case VLC_FOURCC('s','1','6','l'):
127 case VLC_FOURCC('u','1','6','b'):
128 case VLC_FOURCC('s','1','6','b'):
132 case VLC_FOURCC('u','2','4','l'):
133 case VLC_FOURCC('s','2','4','l'):
134 case VLC_FOURCC('u','2','4','b'):
135 case VLC_FOURCC('s','2','4','b'):
139 case VLC_FOURCC('f','l','3','2'):
140 case VLC_FOURCC('f','i','3','2'):
144 case VLC_FOURCC('s','p','d','i'):
145 case VLC_FOURCC('s','p','d','b'): /* Big endian spdif output */
146 case VLC_FOURCC('a','5','2',' '):
147 case VLC_FOURCC('d','t','s',' '):
148 case VLC_FOURCC('m','p','g','a'):
149 case VLC_FOURCC('m','p','g','3'):
151 /* For these formats the caller has to indicate the parameters
156 p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
157 p_format->i_frame_length = 1;
160 /*****************************************************************************
161 * aout_FormatPrintChannels : print a channel in a human-readable form
162 *****************************************************************************/
163 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
165 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
167 case AOUT_CHAN_CENTER:
168 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
169 || (p_format->i_original_channels
170 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
172 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
175 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
176 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
178 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
179 return "Dolby/Reverse";
180 return "Stereo/Reverse";
184 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
186 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
188 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
189 return "Stereo/Mono";
190 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
191 return "Stereo/Left";
192 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
193 return "Stereo/Right";
196 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
198 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
200 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
201 | AOUT_CHAN_REARCENTER:
203 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
204 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
206 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
207 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
210 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
211 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
212 || (p_format->i_original_channels
213 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
215 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
218 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
219 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
221 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
222 return "Dual-mono/LFE";
223 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
225 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
226 return "Stereo/Left/LFE";
227 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
228 return "Stereo/Right/LFE";
230 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
232 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
235 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
236 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
238 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
239 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
241 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
242 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
244 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
245 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
246 | AOUT_CHAN_MIDDLERIGHT:
248 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
249 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
250 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
257 /*****************************************************************************
258 * aout_FormatPrint : print a format in a human-readable form
259 *****************************************************************************/
260 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
261 const audio_sample_format_t * p_format )
263 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
264 (char *)&p_format->i_format, p_format->i_rate,
265 aout_FormatPrintChannels( p_format ),
266 p_format->i_frame_length, p_format->i_bytes_per_frame );
269 /*****************************************************************************
270 * aout_FormatsPrint : print two formats in a human-readable form
271 *****************************************************************************/
272 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
273 const audio_sample_format_t * p_format1,
274 const audio_sample_format_t * p_format2 )
276 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
278 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
279 p_format1->i_rate, p_format2->i_rate,
280 aout_FormatPrintChannels( p_format1 ),
281 aout_FormatPrintChannels( p_format2 ) );
286 * FIFO management (internal) - please understand that solving race conditions
287 * is _your_ job, ie. in the audio output you should own the mixer lock
288 * before calling any of these functions.
291 /*****************************************************************************
292 * aout_FifoInit : initialize the members of a FIFO
293 *****************************************************************************/
294 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
299 msg_Err( p_aout, "initialising fifo with zero divider" );
302 p_fifo->p_first = NULL;
303 p_fifo->pp_last = &p_fifo->p_first;
304 aout_DateInit( &p_fifo->end_date, i_rate );
307 /*****************************************************************************
308 * aout_FifoPush : push a packet into the FIFO
309 *****************************************************************************/
310 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
311 aout_buffer_t * p_buffer )
314 *p_fifo->pp_last = p_buffer;
315 p_fifo->pp_last = &p_buffer->p_next;
316 *p_fifo->pp_last = NULL;
317 /* Enforce the continuity of the stream. */
318 if ( aout_DateGet( &p_fifo->end_date ) )
320 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
321 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
322 p_buffer->i_nb_samples );
326 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
330 /*****************************************************************************
331 * aout_FifoSet : set end_date and trash all buffers (because they aren't
333 *****************************************************************************/
334 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
337 aout_buffer_t * p_buffer;
340 aout_DateSet( &p_fifo->end_date, date );
341 p_buffer = p_fifo->p_first;
342 while ( p_buffer != NULL )
344 aout_buffer_t * p_next = p_buffer->p_next;
345 aout_BufferFree( p_buffer );
348 p_fifo->p_first = NULL;
349 p_fifo->pp_last = &p_fifo->p_first;
352 /*****************************************************************************
353 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
354 *****************************************************************************/
355 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
358 aout_buffer_t * p_buffer;
361 aout_DateMove( &p_fifo->end_date, difference );
362 p_buffer = p_fifo->p_first;
363 while ( p_buffer != NULL )
365 p_buffer->start_date += difference;
366 p_buffer->end_date += difference;
367 p_buffer = p_buffer->p_next;
371 /*****************************************************************************
372 * aout_FifoNextStart : return the current end_date
373 *****************************************************************************/
374 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
377 return aout_DateGet( &p_fifo->end_date );
380 /*****************************************************************************
381 * aout_FifoFirstDate : return the playing date of the first buffer in the
383 *****************************************************************************/
384 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
387 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
390 /*****************************************************************************
391 * aout_FifoPop : get the next buffer out of the FIFO
392 *****************************************************************************/
393 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
395 aout_buffer_t * p_buffer;
398 p_buffer = p_fifo->p_first;
399 if ( p_buffer == NULL ) return NULL;
400 p_fifo->p_first = p_buffer->p_next;
401 if ( p_fifo->p_first == NULL )
403 p_fifo->pp_last = &p_fifo->p_first;
409 /*****************************************************************************
410 * aout_FifoDestroy : destroy a FIFO and its buffers
411 *****************************************************************************/
412 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
414 aout_buffer_t * p_buffer;
417 p_buffer = p_fifo->p_first;
418 while ( p_buffer != NULL )
420 aout_buffer_t * p_next = p_buffer->p_next;
421 aout_BufferFree( p_buffer );
425 p_fifo->p_first = NULL;
426 p_fifo->pp_last = &p_fifo->p_first;
431 * Date management (internal and external)
434 /*****************************************************************************
435 * aout_DateInit : set the divider of an audio_date_t
436 *****************************************************************************/
437 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
440 p_date->i_divider = i_divider;
441 p_date->i_remainder = 0;
444 /*****************************************************************************
445 * aout_DateSet : set the date of an audio_date_t
446 *****************************************************************************/
447 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
449 p_date->date = new_date;
450 p_date->i_remainder = 0;
453 /*****************************************************************************
454 * aout_DateMove : move forwards or backwards the date of an audio_date_t
455 *****************************************************************************/
456 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
458 p_date->date += difference;
461 /*****************************************************************************
462 * aout_DateGet : get the date of an audio_date_t
463 *****************************************************************************/
464 mtime_t aout_DateGet( const audio_date_t * p_date )
469 /*****************************************************************************
470 * aout_DateIncrement : increment the date and return the result, taking
471 * into account rounding errors
472 *****************************************************************************/
473 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
475 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
476 p_date->date += i_dividend / p_date->i_divider;
477 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
478 if ( p_date->i_remainder >= p_date->i_divider )
480 /* This is Bresenham algorithm. */
482 p_date->i_remainder -= p_date->i_divider;
487 /*****************************************************************************
488 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
489 *****************************************************************************/
490 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
491 const uint32_t *pi_chan_order_out,
492 uint32_t i_channel_mask,
493 int i_channels, int *pi_chan_table )
495 bool b_chan_reorder = false;
498 if( i_channels > AOUT_CHAN_MAX ) return false;
500 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
502 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
504 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
506 if( i_channel_mask & pi_chan_order_out[k] ) l++;
509 pi_chan_table[j++] = l;
512 for( i = 0; i < i_channels; i++ )
514 if( pi_chan_table[i] != i ) b_chan_reorder = true;
517 return b_chan_reorder;
520 /*****************************************************************************
521 * aout_ChannelReorder :
522 *****************************************************************************/
523 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
524 int i_channels, const int *pi_chan_table,
525 int i_bits_per_sample )
527 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
530 if( i_bits_per_sample == 8 )
532 for( i = 0; i < i_buffer / i_channels; i++ )
534 for( j = 0; j < i_channels; j++ )
536 p_tmp[pi_chan_table[j]] = p_buf[j];
539 memcpy( p_buf, p_tmp, i_channels );
543 else if( i_bits_per_sample == 16 )
545 for( i = 0; i < i_buffer / i_channels / 2; i++ )
547 for( j = 0; j < i_channels; j++ )
549 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
550 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
553 memcpy( p_buf, p_tmp, 2 * i_channels );
554 p_buf += 2 * i_channels;
557 else if( i_bits_per_sample == 24 )
559 for( i = 0; i < i_buffer / i_channels / 3; i++ )
561 for( j = 0; j < i_channels; j++ )
563 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
564 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
565 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
568 memcpy( p_buf, p_tmp, 3 * i_channels );
569 p_buf += 3 * i_channels;
572 else if( i_bits_per_sample == 32 )
574 for( i = 0; i < i_buffer / i_channels / 4; i++ )
576 for( j = 0; j < i_channels; j++ )
578 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
579 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
580 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
581 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
584 memcpy( p_buf, p_tmp, 4 * i_channels );
585 p_buf += 4 * i_channels;