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_parent, 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 );
87 vlc_object_release( p_aout );
92 * Formats management (internal and external)
95 /*****************************************************************************
96 * aout_FormatNbChannels : return the number of channels
97 *****************************************************************************/
98 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
100 static const uint32_t pi_channels[] =
101 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
102 AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
103 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
104 unsigned int i_nb = 0, i;
106 for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
108 if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
114 /*****************************************************************************
115 * aout_FormatPrepare : compute the number of bytes per frame & frame length
116 *****************************************************************************/
117 void aout_FormatPrepare( audio_sample_format_t * p_format )
121 switch ( p_format->i_format )
123 case VLC_FOURCC('u','8',' ',' '):
124 case VLC_FOURCC('s','8',' ',' '):
128 case VLC_FOURCC('u','1','6','l'):
129 case VLC_FOURCC('s','1','6','l'):
130 case VLC_FOURCC('u','1','6','b'):
131 case VLC_FOURCC('s','1','6','b'):
135 case VLC_FOURCC('u','2','4','l'):
136 case VLC_FOURCC('s','2','4','l'):
137 case VLC_FOURCC('u','2','4','b'):
138 case VLC_FOURCC('s','2','4','b'):
142 case VLC_FOURCC('f','l','3','2'):
143 case VLC_FOURCC('f','i','3','2'):
147 case VLC_FOURCC('s','p','d','i'):
148 case VLC_FOURCC('s','p','d','b'): /* Big endian spdif output */
149 case VLC_FOURCC('a','5','2',' '):
150 case VLC_FOURCC('d','t','s',' '):
151 case VLC_FOURCC('m','p','g','a'):
152 case VLC_FOURCC('m','p','g','3'):
154 /* For these formats the caller has to indicate the parameters
159 p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
160 p_format->i_frame_length = 1;
163 /*****************************************************************************
164 * aout_FormatPrintChannels : print a channel in a human-readable form
165 *****************************************************************************/
166 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
168 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
170 case AOUT_CHAN_CENTER:
171 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
172 || (p_format->i_original_channels
173 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
175 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
178 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
179 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
181 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
182 return "Dolby/Reverse";
183 return "Stereo/Reverse";
187 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
189 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
191 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
192 return "Stereo/Mono";
193 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
194 return "Stereo/Left";
195 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
196 return "Stereo/Right";
199 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
201 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
203 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
204 | AOUT_CHAN_REARCENTER:
206 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
207 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
209 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
210 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
213 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
214 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
215 || (p_format->i_original_channels
216 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
218 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
221 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
222 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
224 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
225 return "Dual-mono/LFE";
226 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
228 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
229 return "Stereo/Left/LFE";
230 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
231 return "Stereo/Right/LFE";
233 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
235 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
238 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
239 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
241 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
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_LFE:
247 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
248 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
249 | AOUT_CHAN_MIDDLERIGHT:
251 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
252 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
253 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
260 /*****************************************************************************
261 * aout_FormatPrint : print a format in a human-readable form
262 *****************************************************************************/
263 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
264 const audio_sample_format_t * p_format )
266 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
267 (char *)&p_format->i_format, p_format->i_rate,
268 aout_FormatPrintChannels( p_format ),
269 p_format->i_frame_length, p_format->i_bytes_per_frame );
272 /*****************************************************************************
273 * aout_FormatsPrint : print two formats in a human-readable form
274 *****************************************************************************/
275 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
276 const audio_sample_format_t * p_format1,
277 const audio_sample_format_t * p_format2 )
279 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
281 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
282 p_format1->i_rate, p_format2->i_rate,
283 aout_FormatPrintChannels( p_format1 ),
284 aout_FormatPrintChannels( p_format2 ) );
289 * FIFO management (internal) - please understand that solving race conditions
290 * is _your_ job, ie. in the audio output you should own the mixer lock
291 * before calling any of these functions.
294 /*****************************************************************************
295 * aout_FifoInit : initialize the members of a FIFO
296 *****************************************************************************/
297 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
302 msg_Err( p_aout, "initialising fifo with zero divider" );
305 p_fifo->p_first = NULL;
306 p_fifo->pp_last = &p_fifo->p_first;
307 aout_DateInit( &p_fifo->end_date, i_rate );
310 /*****************************************************************************
311 * aout_FifoPush : push a packet into the FIFO
312 *****************************************************************************/
313 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
314 aout_buffer_t * p_buffer )
317 *p_fifo->pp_last = p_buffer;
318 p_fifo->pp_last = &p_buffer->p_next;
319 *p_fifo->pp_last = NULL;
320 /* Enforce the continuity of the stream. */
321 if ( aout_DateGet( &p_fifo->end_date ) )
323 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
324 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
325 p_buffer->i_nb_samples );
329 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
333 /*****************************************************************************
334 * aout_FifoSet : set end_date and trash all buffers (because they aren't
336 *****************************************************************************/
337 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
340 aout_buffer_t * p_buffer;
343 aout_DateSet( &p_fifo->end_date, date );
344 p_buffer = p_fifo->p_first;
345 while ( p_buffer != NULL )
347 aout_buffer_t * p_next = p_buffer->p_next;
348 aout_BufferFree( p_buffer );
351 p_fifo->p_first = NULL;
352 p_fifo->pp_last = &p_fifo->p_first;
355 /*****************************************************************************
356 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
357 *****************************************************************************/
358 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
361 aout_buffer_t * p_buffer;
364 aout_DateMove( &p_fifo->end_date, difference );
365 p_buffer = p_fifo->p_first;
366 while ( p_buffer != NULL )
368 p_buffer->start_date += difference;
369 p_buffer->end_date += difference;
370 p_buffer = p_buffer->p_next;
374 /*****************************************************************************
375 * aout_FifoNextStart : return the current end_date
376 *****************************************************************************/
377 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
380 return aout_DateGet( &p_fifo->end_date );
383 /*****************************************************************************
384 * aout_FifoFirstDate : return the playing date of the first buffer in the
386 *****************************************************************************/
387 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
390 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
393 /*****************************************************************************
394 * aout_FifoPop : get the next buffer out of the FIFO
395 *****************************************************************************/
396 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
398 aout_buffer_t * p_buffer;
401 p_buffer = p_fifo->p_first;
402 if ( p_buffer == NULL ) return NULL;
403 p_fifo->p_first = p_buffer->p_next;
404 if ( p_fifo->p_first == NULL )
406 p_fifo->pp_last = &p_fifo->p_first;
412 /*****************************************************************************
413 * aout_FifoDestroy : destroy a FIFO and its buffers
414 *****************************************************************************/
415 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
417 aout_buffer_t * p_buffer;
420 p_buffer = p_fifo->p_first;
421 while ( p_buffer != NULL )
423 aout_buffer_t * p_next = p_buffer->p_next;
424 aout_BufferFree( p_buffer );
428 p_fifo->p_first = NULL;
429 p_fifo->pp_last = &p_fifo->p_first;
434 * Date management (internal and external)
437 /*****************************************************************************
438 * aout_DateInit : set the divider of an audio_date_t
439 *****************************************************************************/
440 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
443 p_date->i_divider = i_divider;
444 p_date->i_remainder = 0;
447 /*****************************************************************************
448 * aout_DateSet : set the date of an audio_date_t
449 *****************************************************************************/
450 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
452 p_date->date = new_date;
453 p_date->i_remainder = 0;
456 /*****************************************************************************
457 * aout_DateMove : move forwards or backwards the date of an audio_date_t
458 *****************************************************************************/
459 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
461 p_date->date += difference;
464 /*****************************************************************************
465 * aout_DateGet : get the date of an audio_date_t
466 *****************************************************************************/
467 mtime_t aout_DateGet( const audio_date_t * p_date )
472 /*****************************************************************************
473 * aout_DateIncrement : increment the date and return the result, taking
474 * into account rounding errors
475 *****************************************************************************/
476 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
478 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
479 p_date->date += i_dividend / p_date->i_divider;
480 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
481 if ( p_date->i_remainder >= p_date->i_divider )
483 /* This is Bresenham algorithm. */
485 p_date->i_remainder -= p_date->i_divider;
490 /*****************************************************************************
491 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
492 *****************************************************************************/
493 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
494 const uint32_t *pi_chan_order_out,
495 uint32_t i_channel_mask,
496 int i_channels, int *pi_chan_table )
498 bool b_chan_reorder = false;
501 if( i_channels > AOUT_CHAN_MAX ) return false;
503 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
505 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
507 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
509 if( i_channel_mask & pi_chan_order_out[k] ) l++;
512 pi_chan_table[j++] = l;
515 for( i = 0; i < i_channels; i++ )
517 if( pi_chan_table[i] != i ) b_chan_reorder = true;
520 return b_chan_reorder;
523 /*****************************************************************************
524 * aout_ChannelReorder :
525 *****************************************************************************/
526 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
527 int i_channels, const int *pi_chan_table,
528 int i_bits_per_sample )
530 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
533 if( i_bits_per_sample == 8 )
535 for( i = 0; i < i_buffer / i_channels; i++ )
537 for( j = 0; j < i_channels; j++ )
539 p_tmp[pi_chan_table[j]] = p_buf[j];
542 memcpy( p_buf, p_tmp, i_channels );
546 else if( i_bits_per_sample == 16 )
548 for( i = 0; i < i_buffer / i_channels / 2; i++ )
550 for( j = 0; j < i_channels; j++ )
552 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
553 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
556 memcpy( p_buf, p_tmp, 2 * i_channels );
557 p_buf += 2 * i_channels;
560 else if( i_bits_per_sample == 24 )
562 for( i = 0; i < i_buffer / i_channels / 3; i++ )
564 for( j = 0; j < i_channels; j++ )
566 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
567 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
568 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
571 memcpy( p_buf, p_tmp, 3 * i_channels );
572 p_buf += 3 * i_channels;
575 else if( i_bits_per_sample == 32 )
577 for( i = 0; i < i_buffer / i_channels / 4; i++ )
579 for( j = 0; j < i_channels; j++ )
581 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
582 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
583 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
584 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
587 memcpy( p_buf, p_tmp, 4 * i_channels );
588 p_buf += 4 * i_channels;