1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
31 #include <vlc_common.h>
33 #include "aout_internal.h"
36 * Formats management (internal and external)
39 /*****************************************************************************
40 * aout_BitsPerSample : get the number of bits per sample
41 *****************************************************************************/
42 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
44 switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
76 /* For these formats the caller has to indicate the parameters
82 vlc_fourcc_t aout_NativeEndian( vlc_fourcc_t i_format )
88 return VLC_CODEC_FL64;
92 return VLC_CODEC_FL32;
96 return VLC_CODEC_S32N;
100 return VLC_CODEC_U32N;
104 return VLC_CODEC_S24N;
108 return VLC_CODEC_U24N;
112 return VLC_CODEC_S16N;
116 return VLC_CODEC_U16N;
122 /*****************************************************************************
123 * aout_FormatPrepare : compute the number of bytes per frame & frame length
124 *****************************************************************************/
125 void aout_FormatPrepare( audio_sample_format_t * p_format )
127 p_format->i_channels = aout_FormatNbChannels( p_format );
128 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
129 if( p_format->i_bitspersample > 0 )
131 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
132 * aout_FormatNbChannels( p_format );
133 p_format->i_frame_length = 1;
137 /*****************************************************************************
138 * aout_FormatPrintChannels : print a channel in a human-readable form
139 *****************************************************************************/
140 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
142 switch ( p_format->i_physical_channels )
145 case AOUT_CHAN_RIGHT:
146 case AOUT_CHAN_CENTER:
147 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
148 || (p_format->i_original_channels
149 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
151 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
154 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
155 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
157 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
158 return "Dolby/Reverse";
159 return "Stereo/Reverse";
163 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
165 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
167 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
168 return "Stereo/Mono";
169 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
170 return "Stereo/Left";
171 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
172 return "Stereo/Right";
175 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
177 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
179 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
180 | AOUT_CHAN_REARCENTER:
182 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
183 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
185 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
186 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
188 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
189 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
191 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
192 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
195 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
196 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
197 || (p_format->i_original_channels
198 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
200 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
203 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
204 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
206 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
207 return "Dual-mono/LFE";
208 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
210 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
211 return "Stereo/Left/LFE";
212 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
213 return "Stereo/Right/LFE";
215 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
217 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
220 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
221 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
223 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
224 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
226 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
227 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
229 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
230 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
232 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
233 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
235 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
236 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
237 | AOUT_CHAN_MIDDLERIGHT:
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 | AOUT_CHAN_LFE:
243 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
244 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
245 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
252 #undef aout_FormatPrint
254 * Prints an audio sample format in a human-readable form.
256 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
257 const audio_sample_format_t *p_format )
259 msg_Dbg( obj, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
260 (char *)&p_format->i_format, p_format->i_rate,
261 aout_FormatPrintChannels( p_format ),
262 p_format->i_frame_length, p_format->i_bytes_per_frame );
265 #undef aout_FormatsPrint
267 * Prints two formats in a human-readable form
269 void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
270 const audio_sample_format_t * p_format1,
271 const audio_sample_format_t * p_format2 )
273 msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
275 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
276 p_format1->i_rate, p_format2->i_rate,
277 aout_FormatPrintChannels( p_format1 ),
278 aout_FormatPrintChannels( p_format2 ) );
281 /*****************************************************************************
282 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
283 *****************************************************************************/
284 unsigned aout_CheckChannelReorder( const uint32_t *chans_in,
285 const uint32_t *chans_out,
286 uint32_t mask, uint8_t *restrict table )
288 unsigned channels = 0;
290 if( chans_in == NULL )
291 chans_in = pi_vlc_chan_order_wg4;
292 if( chans_out == NULL )
293 chans_out = pi_vlc_chan_order_wg4;
295 for( unsigned i = 0; chans_in[i]; i++ )
297 const uint32_t chan = chans_in[i];
302 for( unsigned j = 0; chan != chans_out[j]; j++ )
303 if( mask & chans_out[j] )
306 table[channels++] = index;
309 for( unsigned i = 0; i < channels; i++ )
315 /*****************************************************************************
316 * aout_ChannelReorder :
317 *****************************************************************************/
318 void aout_ChannelReorder( void *ptr, size_t bytes, unsigned channels,
319 const uint8_t *chans_table, unsigned bits_per_sample )
321 size_t samples = bytes / (channels * (bits_per_sample >> 3));
323 assert( channels <= AOUT_CHAN_MAX );
325 switch( bits_per_sample )
331 for( size_t i = 0; i < samples; i++ )
333 uint32_t tmp[AOUT_CHAN_MAX];
335 for( size_t j = 0; j < channels; j++ )
336 tmp[chans_table[j]] = buf[j];
338 memcpy( buf, tmp, 4 * channels );
348 for( size_t i = 0; i < samples; i++ )
350 uint16_t tmp[AOUT_CHAN_MAX];
352 for( size_t j = 0; j < channels; j++ )
353 tmp[chans_table[j]] = buf[j];
355 memcpy( buf, tmp, 2 * channels );
365 for( size_t i = 0; i < samples; i++ )
367 uint8_t tmp[AOUT_CHAN_MAX];
369 for( size_t j = 0; j < channels; j++ )
370 tmp[chans_table[j]] = buf[j];
372 memcpy( buf, tmp, channels );
382 for( size_t i = 0; i < samples; i++ )
384 uint8_t tmp[3 * AOUT_CHAN_MAX];
386 for( size_t j = 0; j < channels; j++ )
387 memcpy( tmp + (3 * chans_table[j]), buf + (3 * j), 3 );
389 memcpy( buf, tmp, 3 * channels );
396 /*****************************************************************************
397 * aout_ChannelExtract:
398 *****************************************************************************/
399 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
400 const uint8_t *pi_src, int i_src_channels,
402 const int *pi_selection, int i_bytes )
404 for( int i = 0; i < i_sample_count; i++ )
406 for( int j = 0; j < i_dst_channels; j++ )
407 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
408 pi_dst += i_dst_channels * i_bytes;
409 pi_src += i_src_channels * i_bytes;
413 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
414 const void *p_src, int i_src_channels,
415 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
417 /* It does not work in place */
418 assert( p_dst != p_src );
420 /* Force the compiler to inline for the specific cases so it can optimize */
421 if( i_bits_per_sample == 8 )
422 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
423 else if( i_bits_per_sample == 16 )
424 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
425 else if( i_bits_per_sample == 24 )
426 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
427 else if( i_bits_per_sample == 32 )
428 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
429 else if( i_bits_per_sample == 64 )
430 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
433 bool aout_CheckChannelExtraction( int *pi_selection,
434 uint32_t *pi_layout, int *pi_channels,
435 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
436 const uint32_t *pi_order_src, int i_channels )
438 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
439 uint32_t i_layout = 0;
441 int pi_index[AOUT_CHAN_MAX];
445 pi_order_dst = pi_vlc_chan_order_wg4;
447 /* Detect special dual mono case */
448 if( i_channels == 2 &&
449 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
451 i_layout |= AOUT_CHAN_DUALMONO;
452 pi_order_src = pi_order_dual_mono;
456 for( int i = 0; i < i_channels; i++ )
458 /* Ignore unknown or duplicated channels or not present in output */
459 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
462 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
464 if( pi_order_dst[j] == pi_order_src[i] )
466 assert( i_out < AOUT_CHAN_MAX );
467 pi_index[i_out++] = i;
468 i_layout |= pi_order_src[i];
475 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
477 for( int k = 0; k < i_out; k++ )
479 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
481 pi_selection[j++] = pi_index[k];
487 *pi_layout = i_layout;
488 *pi_channels = i_out;
490 for( int i = 0; i < i_out; i++ )
492 if( pi_selection[i] != i )
495 return i_out == i_channels;
498 /* Return the order in which filters should be inserted */
499 static int FilterOrder( const char *psz_name )
501 static const struct {
502 const char psz_name[10];
507 for( unsigned i = 0; i < ARRAY_SIZE(filter); i++ )
509 if( !strcmp( filter[i].psz_name, psz_name ) )
510 return filter[i].i_order;
515 /* This function will add or remove a a module from a string list (colon
516 * separated). It will return true if there is a modification
517 * In case p_aout is NULL, we will use configuration instead of variable */
518 bool aout_ChangeFilterString( vlc_object_t *p_obj, vlc_object_t *p_aout,
519 const char *psz_variable,
520 const char *psz_name, bool b_add )
522 if( *psz_name == '\0' )
528 psz_list = var_GetString( p_aout, psz_variable );
532 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
533 var_Destroy( p_obj->p_libvlc, psz_variable );
536 /* Split the string into an array of filters */
538 for( char *p = psz_list; p && *p; p++ )
539 i_count += *p == ':';
542 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
548 bool b_present = false;
550 for( char *p = psz_list; p && *p; )
552 char *psz_end = strchr(p, ':');
556 psz_end = p + strlen(p);
559 b_present |= !strcmp( p, psz_name );
560 ppsz_filter[i_count++] = p;
564 if( b_present == b_add )
573 int i_order = FilterOrder( psz_name );
575 for( i = 0; i < i_count; i++ )
577 if( FilterOrder( ppsz_filter[i] ) > i_order )
581 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
582 ppsz_filter[i] = psz_name;
587 for( int i = 0; i < i_count; i++ )
589 if( !strcmp( ppsz_filter[i], psz_name ) )
594 for( int i = 0; i < i_count; i++ )
595 i_length += 1 + strlen( ppsz_filter[i] );
597 char *psz_new = malloc( i_length + 1 );
599 for( int i = 0; i < i_count; i++ )
601 if( *ppsz_filter[i] == '\0' )
604 strcat( psz_new, ":" );
605 strcat( psz_new, ppsz_filter[i] );
611 var_SetString( p_aout, psz_variable, psz_new );
613 config_PutPsz( p_obj, psz_variable, psz_new );