1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002-2005 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() */
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 )
311 *p_fifo->pp_last = p_buffer;
312 p_fifo->pp_last = &p_buffer->p_next;
313 *p_fifo->pp_last = NULL;
314 /* Enforce the continuity of the stream. */
315 if ( aout_DateGet( &p_fifo->end_date ) )
317 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
318 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
319 p_buffer->i_nb_samples );
323 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
327 /*****************************************************************************
328 * aout_FifoSet : set end_date and trash all buffers (because they aren't
330 *****************************************************************************/
331 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
334 aout_buffer_t * p_buffer;
336 aout_DateSet( &p_fifo->end_date, date );
337 p_buffer = p_fifo->p_first;
338 while ( p_buffer != NULL )
340 aout_buffer_t * p_next = p_buffer->p_next;
341 aout_BufferFree( p_buffer );
344 p_fifo->p_first = NULL;
345 p_fifo->pp_last = &p_fifo->p_first;
348 /*****************************************************************************
349 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
350 *****************************************************************************/
351 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
354 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 )
371 return aout_DateGet( &p_fifo->end_date );
374 /*****************************************************************************
375 * aout_FifoFirstDate : return the playing date of the first buffer in the
377 *****************************************************************************/
378 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
380 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
383 /*****************************************************************************
384 * aout_FifoPop : get the next buffer out of the FIFO
385 *****************************************************************************/
386 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
388 aout_buffer_t * p_buffer;
390 p_buffer = p_fifo->p_first;
391 if ( p_buffer == NULL ) return NULL;
392 p_fifo->p_first = p_buffer->p_next;
393 if ( p_fifo->p_first == NULL )
395 p_fifo->pp_last = &p_fifo->p_first;
401 /*****************************************************************************
402 * aout_FifoDestroy : destroy a FIFO and its buffers
403 *****************************************************************************/
404 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
406 aout_buffer_t * p_buffer;
408 p_buffer = p_fifo->p_first;
409 while ( p_buffer != NULL )
411 aout_buffer_t * p_next = p_buffer->p_next;
412 aout_BufferFree( p_buffer );
416 p_fifo->p_first = NULL;
417 p_fifo->pp_last = &p_fifo->p_first;
422 * Date management (internal and external)
425 /*****************************************************************************
426 * aout_DateInit : set the divider of an audio_date_t
427 *****************************************************************************/
428 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
431 p_date->i_divider = i_divider;
432 p_date->i_remainder = 0;
435 /*****************************************************************************
436 * aout_DateSet : set the date of an audio_date_t
437 *****************************************************************************/
438 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
440 p_date->date = new_date;
441 p_date->i_remainder = 0;
444 /*****************************************************************************
445 * aout_DateMove : move forwards or backwards the date of an audio_date_t
446 *****************************************************************************/
447 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
449 p_date->date += difference;
452 /*****************************************************************************
453 * aout_DateGet : get the date of an audio_date_t
454 *****************************************************************************/
455 mtime_t aout_DateGet( const audio_date_t * p_date )
460 /*****************************************************************************
461 * aout_DateIncrement : increment the date and return the result, taking
462 * into account rounding errors
463 *****************************************************************************/
464 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
466 mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
467 p_date->date += i_dividend / p_date->i_divider;
468 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
469 if ( p_date->i_remainder >= p_date->i_divider )
471 /* This is Bresenham algorithm. */
473 p_date->i_remainder -= p_date->i_divider;
478 /*****************************************************************************
479 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
480 *****************************************************************************/
481 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
482 const uint32_t *pi_chan_order_out,
483 uint32_t i_channel_mask,
484 int i_channels, int *pi_chan_table )
486 vlc_bool_t b_chan_reorder = VLC_FALSE;
489 if( i_channels > AOUT_CHAN_MAX ) return VLC_FALSE;
491 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
493 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
495 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
497 if( i_channel_mask & pi_chan_order_out[k] ) l++;
500 pi_chan_table[j++] = l;
503 for( i = 0; i < i_channels; i++ )
505 if( pi_chan_table[i] != i ) b_chan_reorder = VLC_TRUE;
508 return b_chan_reorder;
511 /*****************************************************************************
512 * aout_ChannelReorder :
513 *****************************************************************************/
514 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
515 int i_channels, const int *pi_chan_table,
516 int i_bits_per_sample )
518 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
521 if( i_bits_per_sample == 8 )
523 for( i = 0; i < i_buffer / i_channels; i++ )
525 for( j = 0; j < i_channels; j++ )
527 p_tmp[pi_chan_table[j]] = p_buf[j];
530 memcpy( p_buf, p_tmp, i_channels );
534 else if( i_bits_per_sample == 16 )
536 for( i = 0; i < i_buffer / i_channels / 2; i++ )
538 for( j = 0; j < i_channels; j++ )
540 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
541 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
544 memcpy( p_buf, p_tmp, 2 * i_channels );
545 p_buf += 2 * i_channels;
548 else if( i_bits_per_sample == 24 )
550 for( i = 0; i < i_buffer / i_channels / 3; i++ )
552 for( j = 0; j < i_channels; j++ )
554 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
555 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
556 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
559 memcpy( p_buf, p_tmp, 3 * i_channels );
560 p_buf += 3 * i_channels;
563 else if( i_bits_per_sample == 32 )
565 for( i = 0; i < i_buffer / i_channels / 4; i++ )
567 for( j = 0; j < i_channels; j++ )
569 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
570 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
571 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
572 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
575 memcpy( p_buf, p_tmp, 4 * i_channels );
576 p_buf += 4 * i_channels;