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 *****************************************************************************/
29 #include "aout_internal.h"
32 * Instances management (internal and external)
35 /*****************************************************************************
36 * aout_New: initialize aout structure
37 *****************************************************************************/
38 aout_instance_t * __aout_New( vlc_object_t * p_parent )
40 aout_instance_t * p_aout;
43 /* Allocate descriptor. */
44 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
50 /* Initialize members. */
51 vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
52 vlc_mutex_init( p_parent, &p_aout->mixer_lock );
53 vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
54 p_aout->i_nb_inputs = 0;
55 p_aout->mixer.f_multiplier = 1.0;
56 p_aout->mixer.b_error = 1;
57 p_aout->output.b_error = 1;
58 p_aout->output.b_starving = 1;
60 var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
61 val.b_bool = VLC_TRUE;
62 var_Set( p_aout, "intf-change", val );
67 /*****************************************************************************
68 * aout_Delete: destroy aout structure
69 *****************************************************************************/
70 void aout_Delete( aout_instance_t * p_aout )
72 var_Destroy( p_aout, "intf-change" );
74 vlc_mutex_destroy( &p_aout->input_fifos_lock );
75 vlc_mutex_destroy( &p_aout->mixer_lock );
76 vlc_mutex_destroy( &p_aout->output_fifo_lock );
79 vlc_object_destroy( p_aout );
84 * Formats management (internal and external)
87 /*****************************************************************************
88 * aout_FormatNbChannels : return the number of channels
89 *****************************************************************************/
90 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
92 static const uint32_t pi_channels[] =
93 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
94 AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
95 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
96 unsigned int i_nb = 0, i;
98 for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
100 if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
106 /*****************************************************************************
107 * aout_FormatPrepare : compute the number of bytes per frame & frame length
108 *****************************************************************************/
109 void aout_FormatPrepare( audio_sample_format_t * p_format )
113 switch ( p_format->i_format )
115 case VLC_FOURCC('u','8',' ',' '):
116 case VLC_FOURCC('s','8',' ',' '):
120 case VLC_FOURCC('u','1','6','l'):
121 case VLC_FOURCC('s','1','6','l'):
122 case VLC_FOURCC('u','1','6','b'):
123 case VLC_FOURCC('s','1','6','b'):
127 case VLC_FOURCC('u','2','4','l'):
128 case VLC_FOURCC('s','2','4','l'):
129 case VLC_FOURCC('u','2','4','b'):
130 case VLC_FOURCC('s','2','4','b'):
134 case VLC_FOURCC('f','l','3','2'):
135 case VLC_FOURCC('f','i','3','2'):
139 case VLC_FOURCC('s','p','d','i'):
140 case VLC_FOURCC('s','p','d','b'): /* Big endian spdif output */
141 case VLC_FOURCC('a','5','2',' '):
142 case VLC_FOURCC('d','t','s',' '):
143 case VLC_FOURCC('m','p','g','a'):
144 case VLC_FOURCC('m','p','g','3'):
146 /* For these formats the caller has to indicate the parameters
151 p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
152 p_format->i_frame_length = 1;
155 /*****************************************************************************
156 * aout_FormatPrintChannels : print a channel in a human-readable form
157 *****************************************************************************/
158 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
160 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
162 case AOUT_CHAN_CENTER:
163 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
164 || (p_format->i_original_channels
165 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
167 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
170 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
171 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
173 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
174 return "Dolby/Reverse";
175 return "Stereo/Reverse";
179 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
181 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
183 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
184 return "Stereo/Mono";
185 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
186 return "Stereo/Left";
187 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
188 return "Stereo/Right";
191 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
193 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
195 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
196 | AOUT_CHAN_REARCENTER:
198 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
199 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
201 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
202 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
205 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
206 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
207 || (p_format->i_original_channels
208 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
210 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
213 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
214 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
216 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
217 return "Dual-mono/LFE";
218 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
220 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
221 return "Stereo/Left/LFE";
222 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
223 return "Stereo/Right/LFE";
225 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
227 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
230 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
231 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
233 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
234 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
236 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
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_MIDDLELEFT
241 | AOUT_CHAN_MIDDLERIGHT:
243 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
244 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
245 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
252 /*****************************************************************************
253 * aout_FormatPrint : print a format in a human-readable form
254 *****************************************************************************/
255 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
256 const audio_sample_format_t * p_format )
258 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
259 (char *)&p_format->i_format, p_format->i_rate,
260 aout_FormatPrintChannels( p_format ),
261 p_format->i_frame_length, p_format->i_bytes_per_frame );
264 /*****************************************************************************
265 * aout_FormatsPrint : print two formats in a human-readable form
266 *****************************************************************************/
267 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
268 const audio_sample_format_t * p_format1,
269 const audio_sample_format_t * p_format2 )
271 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
273 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
274 p_format1->i_rate, p_format2->i_rate,
275 aout_FormatPrintChannels( p_format1 ),
276 aout_FormatPrintChannels( p_format2 ) );
281 * FIFO management (internal) - please understand that solving race conditions
282 * is _your_ job, ie. in the audio output you should own the mixer lock
283 * before calling any of these functions.
286 /*****************************************************************************
287 * aout_FifoInit : initialize the members of a FIFO
288 *****************************************************************************/
289 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
294 msg_Err( p_aout, "initialising fifo with zero divider" );
297 p_fifo->p_first = NULL;
298 p_fifo->pp_last = &p_fifo->p_first;
299 aout_DateInit( &p_fifo->end_date, i_rate );
302 /*****************************************************************************
303 * aout_FifoPush : push a packet into the FIFO
304 *****************************************************************************/
305 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
306 aout_buffer_t * p_buffer )
309 *p_fifo->pp_last = p_buffer;
310 p_fifo->pp_last = &p_buffer->p_next;
311 *p_fifo->pp_last = NULL;
312 /* Enforce the continuity of the stream. */
313 if ( aout_DateGet( &p_fifo->end_date ) )
315 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
316 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
317 p_buffer->i_nb_samples );
321 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
325 /*****************************************************************************
326 * aout_FifoSet : set end_date and trash all buffers (because they aren't
328 *****************************************************************************/
329 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
332 aout_buffer_t * p_buffer;
335 aout_DateSet( &p_fifo->end_date, date );
336 p_buffer = p_fifo->p_first;
337 while ( p_buffer != NULL )
339 aout_buffer_t * p_next = p_buffer->p_next;
340 aout_BufferFree( p_buffer );
343 p_fifo->p_first = NULL;
344 p_fifo->pp_last = &p_fifo->p_first;
347 /*****************************************************************************
348 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
349 *****************************************************************************/
350 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
353 aout_buffer_t * p_buffer;
356 aout_DateMove( &p_fifo->end_date, difference );
357 p_buffer = p_fifo->p_first;
358 while ( p_buffer != NULL )
360 p_buffer->start_date += difference;
361 p_buffer->end_date += difference;
362 p_buffer = p_buffer->p_next;
366 /*****************************************************************************
367 * aout_FifoNextStart : return the current end_date
368 *****************************************************************************/
369 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
372 return aout_DateGet( &p_fifo->end_date );
375 /*****************************************************************************
376 * aout_FifoFirstDate : return the playing date of the first buffer in the
378 *****************************************************************************/
379 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
382 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
385 /*****************************************************************************
386 * aout_FifoPop : get the next buffer out of the FIFO
387 *****************************************************************************/
388 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
390 aout_buffer_t * p_buffer;
393 p_buffer = p_fifo->p_first;
394 if ( p_buffer == NULL ) return NULL;
395 p_fifo->p_first = p_buffer->p_next;
396 if ( p_fifo->p_first == NULL )
398 p_fifo->pp_last = &p_fifo->p_first;
404 /*****************************************************************************
405 * aout_FifoDestroy : destroy a FIFO and its buffers
406 *****************************************************************************/
407 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
409 aout_buffer_t * p_buffer;
412 p_buffer = p_fifo->p_first;
413 while ( p_buffer != NULL )
415 aout_buffer_t * p_next = p_buffer->p_next;
416 aout_BufferFree( p_buffer );
420 p_fifo->p_first = NULL;
421 p_fifo->pp_last = &p_fifo->p_first;
426 * Date management (internal and external)
429 /*****************************************************************************
430 * aout_DateInit : set the divider of an audio_date_t
431 *****************************************************************************/
432 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
435 p_date->i_divider = i_divider;
436 p_date->i_remainder = 0;
439 /*****************************************************************************
440 * aout_DateSet : set the date of an audio_date_t
441 *****************************************************************************/
442 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
444 p_date->date = new_date;
445 p_date->i_remainder = 0;
448 /*****************************************************************************
449 * aout_DateMove : move forwards or backwards the date of an audio_date_t
450 *****************************************************************************/
451 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
453 p_date->date += difference;
456 /*****************************************************************************
457 * aout_DateGet : get the date of an audio_date_t
458 *****************************************************************************/
459 mtime_t aout_DateGet( const audio_date_t * p_date )
464 /*****************************************************************************
465 * aout_DateIncrement : increment the date and return the result, taking
466 * into account rounding errors
467 *****************************************************************************/
468 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
470 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
471 p_date->date += i_dividend / p_date->i_divider;
472 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
473 if ( p_date->i_remainder >= p_date->i_divider )
475 /* This is Bresenham algorithm. */
477 p_date->i_remainder -= p_date->i_divider;
482 /*****************************************************************************
483 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
484 *****************************************************************************/
485 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
486 const uint32_t *pi_chan_order_out,
487 uint32_t i_channel_mask,
488 int i_channels, int *pi_chan_table )
490 vlc_bool_t b_chan_reorder = VLC_FALSE;
493 if( i_channels > AOUT_CHAN_MAX ) return VLC_FALSE;
495 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
497 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
499 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
501 if( i_channel_mask & pi_chan_order_out[k] ) l++;
504 pi_chan_table[j++] = l;
507 for( i = 0; i < i_channels; i++ )
509 if( pi_chan_table[i] != i ) b_chan_reorder = VLC_TRUE;
512 return b_chan_reorder;
515 /*****************************************************************************
516 * aout_ChannelReorder :
517 *****************************************************************************/
518 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
519 int i_channels, const int *pi_chan_table,
520 int i_bits_per_sample )
522 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
525 if( i_bits_per_sample == 8 )
527 for( i = 0; i < i_buffer / i_channels; i++ )
529 for( j = 0; j < i_channels; j++ )
531 p_tmp[pi_chan_table[j]] = p_buf[j];
534 memcpy( p_buf, p_tmp, i_channels );
538 else if( i_bits_per_sample == 16 )
540 for( i = 0; i < i_buffer / i_channels / 2; i++ )
542 for( j = 0; j < i_channels; j++ )
544 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
545 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
548 memcpy( p_buf, p_tmp, 2 * i_channels );
549 p_buf += 2 * i_channels;
552 else if( i_bits_per_sample == 24 )
554 for( i = 0; i < i_buffer / i_channels / 3; i++ )
556 for( j = 0; j < i_channels; j++ )
558 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
559 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
560 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
563 memcpy( p_buf, p_tmp, 3 * i_channels );
564 p_buf += 3 * i_channels;
567 else if( i_bits_per_sample == 32 )
569 for( i = 0; i < i_buffer / i_channels / 4; i++ )
571 for( j = 0; j < i_channels; j++ )
573 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
574 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
575 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
576 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
579 memcpy( p_buf, p_tmp, 4 * i_channels );
580 p_buf += 4 * i_channels;