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"
39 * Instances management (internal and external)
42 #define AOUT_ASSERT_FIFO_LOCKED aout_assert_fifo_locked(p_aout, p_fifo)
43 static inline void aout_assert_fifo_locked( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
46 if( p_fifo == &p_aout->output.fifo )
47 vlc_assert_locked( &p_aout->output_fifo_lock );
51 for( i = 0; i < p_aout->i_nb_inputs; i++ )
53 if( p_fifo == &p_aout->pp_inputs[i]->fifo)
55 vlc_assert_locked( &p_aout->input_fifos_lock );
59 if( i == p_aout->i_nb_inputs )
60 vlc_assert_locked( &p_aout->mixer_lock );
69 static void aout_Destructor( vlc_object_t * p_this );
71 /*****************************************************************************
72 * aout_New: initialize aout structure
73 *****************************************************************************/
74 aout_instance_t * __aout_New( vlc_object_t * p_parent )
76 aout_instance_t * p_aout;
79 /* Allocate descriptor. */
80 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
86 /* Initialize members. */
87 vlc_mutex_init( &p_aout->input_fifos_lock );
88 vlc_mutex_init( &p_aout->mixer_lock );
89 vlc_mutex_init( &p_aout->output_fifo_lock );
90 p_aout->i_nb_inputs = 0;
91 p_aout->mixer.f_multiplier = 1.0;
92 p_aout->mixer.b_error = 1;
93 p_aout->output.b_error = 1;
94 p_aout->output.b_starving = 1;
96 var_Create( p_aout, "intf-change", VLC_VAR_BOOL );
98 var_Set( p_aout, "intf-change", val );
100 vlc_object_set_destructor( p_aout, aout_Destructor );
105 /*****************************************************************************
106 * aout_Destructor: destroy aout structure
107 *****************************************************************************/
108 static void aout_Destructor( vlc_object_t * p_this )
110 aout_instance_t * p_aout = (aout_instance_t *)p_this;
111 vlc_mutex_destroy( &p_aout->input_fifos_lock );
112 vlc_mutex_destroy( &p_aout->mixer_lock );
113 vlc_mutex_destroy( &p_aout->output_fifo_lock );
118 * Formats management (internal and external)
121 /*****************************************************************************
122 * aout_FormatNbChannels : return the number of channels
123 *****************************************************************************/
124 unsigned int aout_FormatNbChannels( const audio_sample_format_t * p_format )
126 static const uint32_t pi_channels[] =
127 { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
128 AOUT_CHAN_REARCENTER, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
129 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_LFE };
130 unsigned int i_nb = 0, i;
132 for ( i = 0; i < sizeof(pi_channels)/sizeof(uint32_t); i++ )
134 if ( p_format->i_physical_channels & pi_channels[i] ) i_nb++;
140 /*****************************************************************************
141 * aout_BitsPerSample : get the number of bits per sample
142 *****************************************************************************/
143 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
147 case VLC_FOURCC('u','8',' ',' '):
148 case VLC_FOURCC('s','8',' ',' '):
151 case VLC_FOURCC('u','1','6','l'):
152 case VLC_FOURCC('s','1','6','l'):
153 case VLC_FOURCC('u','1','6','b'):
154 case VLC_FOURCC('s','1','6','b'):
157 case VLC_FOURCC('u','2','4','l'):
158 case VLC_FOURCC('s','2','4','l'):
159 case VLC_FOURCC('u','2','4','b'):
160 case VLC_FOURCC('s','2','4','b'):
163 case VLC_FOURCC('s','3','2','l'):
164 case VLC_FOURCC('s','3','2','b'):
165 case VLC_FOURCC('f','l','3','2'):
166 case VLC_FOURCC('f','i','3','2'):
169 case VLC_FOURCC('f','l','6','4'):
173 /* For these formats the caller has to indicate the parameters
179 /*****************************************************************************
180 * aout_FormatPrepare : compute the number of bytes per frame & frame length
181 *****************************************************************************/
182 void aout_FormatPrepare( audio_sample_format_t * p_format )
184 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
185 if( p_format->i_bitspersample > 0 )
187 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
188 * aout_FormatNbChannels( p_format );
189 p_format->i_frame_length = 1;
193 /*****************************************************************************
194 * aout_FormatPrintChannels : print a channel in a human-readable form
195 *****************************************************************************/
196 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
198 switch ( p_format->i_physical_channels & AOUT_CHAN_PHYSMASK )
200 case AOUT_CHAN_CENTER:
201 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
202 || (p_format->i_original_channels
203 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
205 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
208 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
209 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
211 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
212 return "Dolby/Reverse";
213 return "Stereo/Reverse";
217 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
219 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
221 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
222 return "Stereo/Mono";
223 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
224 return "Stereo/Left";
225 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
226 return "Stereo/Right";
229 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
231 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:
236 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
237 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
239 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
240 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
243 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
244 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
245 || (p_format->i_original_channels
246 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
248 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
251 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
252 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
254 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
255 return "Dual-mono/LFE";
256 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
258 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
259 return "Stereo/Left/LFE";
260 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
261 return "Stereo/Right/LFE";
263 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
265 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
268 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
269 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
271 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
272 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
274 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
275 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
277 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
278 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
279 | AOUT_CHAN_MIDDLERIGHT:
281 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
282 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
283 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
290 /*****************************************************************************
291 * aout_FormatPrint : print a format in a human-readable form
292 *****************************************************************************/
293 void aout_FormatPrint( aout_instance_t * p_aout, const char * psz_text,
294 const audio_sample_format_t * p_format )
296 msg_Dbg( p_aout, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
297 (char *)&p_format->i_format, p_format->i_rate,
298 aout_FormatPrintChannels( p_format ),
299 p_format->i_frame_length, p_format->i_bytes_per_frame );
302 /*****************************************************************************
303 * aout_FormatsPrint : print two formats in a human-readable form
304 *****************************************************************************/
305 void aout_FormatsPrint( aout_instance_t * p_aout, const char * psz_text,
306 const audio_sample_format_t * p_format1,
307 const audio_sample_format_t * p_format2 )
309 msg_Dbg( p_aout, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
311 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
312 p_format1->i_rate, p_format2->i_rate,
313 aout_FormatPrintChannels( p_format1 ),
314 aout_FormatPrintChannels( p_format2 ) );
319 * FIFO management (internal) - please understand that solving race conditions
320 * is _your_ job, ie. in the audio output you should own the mixer lock
321 * before calling any of these functions.
324 /*****************************************************************************
325 * aout_FifoInit : initialize the members of a FIFO
326 *****************************************************************************/
327 void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
330 AOUT_ASSERT_FIFO_LOCKED;
334 msg_Err( p_aout, "initialising fifo with zero divider" );
337 p_fifo->p_first = NULL;
338 p_fifo->pp_last = &p_fifo->p_first;
339 aout_DateInit( &p_fifo->end_date, i_rate );
342 /*****************************************************************************
343 * aout_FifoPush : push a packet into the FIFO
344 *****************************************************************************/
345 void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
346 aout_buffer_t * p_buffer )
349 AOUT_ASSERT_FIFO_LOCKED;
351 *p_fifo->pp_last = p_buffer;
352 p_fifo->pp_last = &p_buffer->p_next;
353 *p_fifo->pp_last = NULL;
354 /* Enforce the continuity of the stream. */
355 if ( aout_DateGet( &p_fifo->end_date ) )
357 p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
358 p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
359 p_buffer->i_nb_samples );
363 aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
367 /*****************************************************************************
368 * aout_FifoSet : set end_date and trash all buffers (because they aren't
370 *****************************************************************************/
371 void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
374 aout_buffer_t * p_buffer;
376 AOUT_ASSERT_FIFO_LOCKED;
378 aout_DateSet( &p_fifo->end_date, date );
379 p_buffer = p_fifo->p_first;
380 while ( p_buffer != NULL )
382 aout_buffer_t * p_next = p_buffer->p_next;
383 aout_BufferFree( p_buffer );
386 p_fifo->p_first = NULL;
387 p_fifo->pp_last = &p_fifo->p_first;
390 /*****************************************************************************
391 * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
392 *****************************************************************************/
393 void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
396 aout_buffer_t * p_buffer;
398 AOUT_ASSERT_FIFO_LOCKED;
400 aout_DateMove( &p_fifo->end_date, difference );
401 p_buffer = p_fifo->p_first;
402 while ( p_buffer != NULL )
404 p_buffer->start_date += difference;
405 p_buffer->end_date += difference;
406 p_buffer = p_buffer->p_next;
410 /*****************************************************************************
411 * aout_FifoNextStart : return the current end_date
412 *****************************************************************************/
413 mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
416 AOUT_ASSERT_FIFO_LOCKED;
417 return aout_DateGet( &p_fifo->end_date );
420 /*****************************************************************************
421 * aout_FifoFirstDate : return the playing date of the first buffer in the
423 *****************************************************************************/
424 mtime_t aout_FifoFirstDate( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
427 AOUT_ASSERT_FIFO_LOCKED;
428 return p_fifo->p_first ? p_fifo->p_first->start_date : 0;
431 /*****************************************************************************
432 * aout_FifoPop : get the next buffer out of the FIFO
433 *****************************************************************************/
434 aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
436 aout_buffer_t * p_buffer;
438 AOUT_ASSERT_FIFO_LOCKED;
440 p_buffer = p_fifo->p_first;
441 if ( p_buffer == NULL ) return NULL;
442 p_fifo->p_first = p_buffer->p_next;
443 if ( p_fifo->p_first == NULL )
445 p_fifo->pp_last = &p_fifo->p_first;
451 /*****************************************************************************
452 * aout_FifoDestroy : destroy a FIFO and its buffers
453 *****************************************************************************/
454 void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
456 aout_buffer_t * p_buffer;
458 AOUT_ASSERT_FIFO_LOCKED;
460 p_buffer = p_fifo->p_first;
461 while ( p_buffer != NULL )
463 aout_buffer_t * p_next = p_buffer->p_next;
464 aout_BufferFree( p_buffer );
468 p_fifo->p_first = NULL;
469 p_fifo->pp_last = &p_fifo->p_first;
474 * Date management (internal and external)
477 /*****************************************************************************
478 * aout_DateInit : set the divider of an audio_date_t
479 *****************************************************************************/
480 void aout_DateInit( audio_date_t * p_date, uint32_t i_divider )
484 p_date->i_divider = i_divider;
485 p_date->i_remainder = 0;
488 /*****************************************************************************
489 * aout_DateSet : set the date of an audio_date_t
490 *****************************************************************************/
491 void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
493 p_date->date = new_date;
494 p_date->i_remainder = 0;
497 /*****************************************************************************
498 * aout_DateMove : move forwards or backwards the date of an audio_date_t
499 *****************************************************************************/
500 void aout_DateMove( audio_date_t * p_date, mtime_t difference )
502 p_date->date += difference;
505 /*****************************************************************************
506 * aout_DateGet : get the date of an audio_date_t
507 *****************************************************************************/
508 mtime_t aout_DateGet( const audio_date_t * p_date )
513 /*****************************************************************************
514 * aout_DateIncrement : increment the date and return the result, taking
515 * into account rounding errors
516 *****************************************************************************/
517 mtime_t aout_DateIncrement( audio_date_t * p_date, uint32_t i_nb_samples )
519 mtime_t i_dividend = INT64_C(1000000) * i_nb_samples;
520 assert (p_date->i_divider); /* uninitialized audio_data_t ? */
521 p_date->date += i_dividend / p_date->i_divider;
522 p_date->i_remainder += (int)(i_dividend % p_date->i_divider);
523 if ( p_date->i_remainder >= p_date->i_divider )
525 /* This is Bresenham algorithm. */
527 p_date->i_remainder -= p_date->i_divider;
532 /*****************************************************************************
533 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
534 *****************************************************************************/
535 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
536 const uint32_t *pi_chan_order_out,
537 uint32_t i_channel_mask,
538 int i_channels, int *pi_chan_table )
540 bool b_chan_reorder = false;
543 if( i_channels > AOUT_CHAN_MAX )
546 if( pi_chan_order_in == NULL )
547 pi_chan_order_in = pi_vlc_chan_order_wg4;
548 if( pi_chan_order_out == NULL )
549 pi_chan_order_out = pi_vlc_chan_order_wg4;
551 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
553 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
555 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
557 if( i_channel_mask & pi_chan_order_out[k] ) l++;
560 pi_chan_table[j++] = l;
563 for( i = 0; i < i_channels; i++ )
565 if( pi_chan_table[i] != i ) b_chan_reorder = true;
568 return b_chan_reorder;
571 /*****************************************************************************
572 * aout_ChannelReorder :
573 *****************************************************************************/
574 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
575 int i_channels, const int *pi_chan_table,
576 int i_bits_per_sample )
578 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
581 if( i_bits_per_sample == 8 )
583 for( i = 0; i < i_buffer / i_channels; i++ )
585 for( j = 0; j < i_channels; j++ )
587 p_tmp[pi_chan_table[j]] = p_buf[j];
590 memcpy( p_buf, p_tmp, i_channels );
594 else if( i_bits_per_sample == 16 )
596 for( i = 0; i < i_buffer / i_channels / 2; i++ )
598 for( j = 0; j < i_channels; j++ )
600 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
601 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
604 memcpy( p_buf, p_tmp, 2 * i_channels );
605 p_buf += 2 * i_channels;
608 else if( i_bits_per_sample == 24 )
610 for( i = 0; i < i_buffer / i_channels / 3; i++ )
612 for( j = 0; j < i_channels; j++ )
614 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
615 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
616 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
619 memcpy( p_buf, p_tmp, 3 * i_channels );
620 p_buf += 3 * i_channels;
623 else if( i_bits_per_sample == 32 )
625 for( i = 0; i < i_buffer / i_channels / 4; i++ )
627 for( j = 0; j < i_channels; j++ )
629 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
630 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
631 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
632 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
635 memcpy( p_buf, p_tmp, 4 * i_channels );
636 p_buf += 4 * i_channels;