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 ) )
64 case VLC_CODEC_S24L32:
65 case VLC_CODEC_S24B32:
79 /* For these formats the caller has to indicate the parameters
85 /*****************************************************************************
86 * aout_FormatPrepare : compute the number of bytes per frame & frame length
87 *****************************************************************************/
88 void aout_FormatPrepare( audio_sample_format_t * p_format )
90 p_format->i_channels = aout_FormatNbChannels( p_format );
91 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
92 if( p_format->i_bitspersample > 0 )
94 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
95 * aout_FormatNbChannels( p_format );
96 p_format->i_frame_length = 1;
100 /*****************************************************************************
101 * aout_FormatPrintChannels : print a channel in a human-readable form
102 *****************************************************************************/
103 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
105 switch ( p_format->i_physical_channels )
108 case AOUT_CHAN_RIGHT:
109 case AOUT_CHAN_CENTER:
110 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
111 || (p_format->i_original_channels
112 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
114 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
117 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
118 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
120 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
121 return "Dolby/Reverse";
122 return "Stereo/Reverse";
126 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
128 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
130 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
131 return "Stereo/Mono";
132 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
133 return "Stereo/Left";
134 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
135 return "Stereo/Right";
138 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
140 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
142 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
143 | AOUT_CHAN_REARCENTER:
145 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
146 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
148 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
149 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
151 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
152 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
154 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
155 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
158 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
159 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
160 || (p_format->i_original_channels
161 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
163 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
166 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
167 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
169 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
170 return "Dual-mono/LFE";
171 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
173 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
174 return "Stereo/Left/LFE";
175 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
176 return "Stereo/Right/LFE";
178 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
180 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
183 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
184 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
186 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
187 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
189 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
190 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
192 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
193 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
195 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
196 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
198 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
199 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
200 | AOUT_CHAN_MIDDLERIGHT:
202 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
203 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
204 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
206 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
207 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
208 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
215 #undef aout_FormatPrint
217 * Prints an audio sample format in a human-readable form.
219 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
220 const audio_sample_format_t *p_format )
222 msg_Dbg( obj, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
223 (char *)&p_format->i_format, p_format->i_rate,
224 aout_FormatPrintChannels( p_format ),
225 p_format->i_frame_length, p_format->i_bytes_per_frame );
228 #undef aout_FormatsPrint
230 * Prints two formats in a human-readable form
232 void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
233 const audio_sample_format_t * p_format1,
234 const audio_sample_format_t * p_format2 )
236 msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
238 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
239 p_format1->i_rate, p_format2->i_rate,
240 aout_FormatPrintChannels( p_format1 ),
241 aout_FormatPrintChannels( p_format2 ) );
244 /*****************************************************************************
245 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
246 *****************************************************************************/
247 unsigned aout_CheckChannelReorder( const uint32_t *chans_in,
248 const uint32_t *chans_out,
249 uint32_t mask, uint8_t *restrict table )
251 unsigned channels = 0;
253 if( chans_in == NULL )
254 chans_in = pi_vlc_chan_order_wg4;
255 if( chans_out == NULL )
256 chans_out = pi_vlc_chan_order_wg4;
258 for( unsigned i = 0; chans_in[i]; i++ )
260 const uint32_t chan = chans_in[i];
265 for( unsigned j = 0; chan != chans_out[j]; j++ )
266 if( mask & chans_out[j] )
269 table[channels++] = index;
272 for( unsigned i = 0; i < channels; i++ )
279 * Reorders audio samples within a block of linear audio interleaved samples.
280 * \param ptr start address of the block of samples
281 * \param bytes size of the block in bytes (must be a multiple of the product
282 * of the channels count and the sample size)
283 * \param channels channels count (also length of the chans_table table)
284 * \param chans_table permutation table to reorder the channels
285 * (usually computed by aout_CheckChannelReorder())
286 * \param fourcc sample format (must be a linear sample format)
287 * \note The samples must be naturally aligned in memory.
289 void aout_ChannelReorder( void *ptr, size_t bytes, unsigned channels,
290 const uint8_t *restrict chans_table, vlc_fourcc_t fourcc )
292 assert( channels != 0 );
293 assert( channels <= AOUT_CHAN_MAX );
295 /* The audio formats supported in audio output are inlined. For other
296 * formats (used in demuxers and muxers), memcpy() is used to avoid
297 * breaking type punning. */
298 #define REORDER_TYPE(type) \
300 const size_t frames = (bytes / sizeof (type)) / channels; \
303 for( size_t i = 0; i < frames; i++ ) \
305 float tmp[AOUT_CHAN_MAX]; \
307 for( size_t j = 0; j < channels; j++ ) \
308 tmp[chans_table[j]] = buf[j]; \
309 memcpy( buf, tmp, sizeof (type) * channels ); \
316 case VLC_CODEC_U8: REORDER_TYPE(uint8_t); break;
317 case VLC_CODEC_S16N: REORDER_TYPE(int16_t); break;
318 case VLC_CODEC_FL32: REORDER_TYPE(float); break;
319 case VLC_CODEC_S32N: REORDER_TYPE(int32_t); break;
320 case VLC_CODEC_FL64: REORDER_TYPE(double); break;
324 unsigned size = aout_BitsPerSample( fourcc ) / 8;
325 const size_t frames = bytes / size;
326 unsigned char *buf = ptr;
328 assert( bytes != 0 );
329 for( size_t i = 0; i < frames; i++ )
331 unsigned char tmp[AOUT_CHAN_MAX * bytes];
333 for( size_t j = 0; j < channels; j++ )
334 memcpy( tmp + size * chans_table[j], buf + size * j, size );
335 memcpy( buf, tmp, size * channels );
336 buf += size * channels;
344 * Interleaves audio samples within a block of samples.
345 * \param dst destination buffer for interleaved samples
346 * \param src source buffer with consecutive planes of samples
347 * \param samples number of samples (per channel/per plane)
348 * \param chans channels/planes count
349 * \param fourcc sample format (must be a linear sample format)
350 * \note The samples must be naturally aligned in memory.
351 * \warning Destination and source buffers MUST NOT overlap.
353 void aout_Interleave( void *restrict dst, const void *restrict src,
354 unsigned samples, unsigned chans, vlc_fourcc_t fourcc )
356 #define INTERLEAVE_TYPE(type) \
359 const type *s = src; \
360 for( size_t i = 0; i < chans; i++ ) { \
361 for( size_t j = 0, k = 0; j < samples; j++, k += chans ) \
369 case VLC_CODEC_U8: INTERLEAVE_TYPE(uint8_t); break;
370 case VLC_CODEC_S16N: INTERLEAVE_TYPE(uint16_t); break;
371 case VLC_CODEC_FL32: INTERLEAVE_TYPE(float); break;
372 case VLC_CODEC_S32N: INTERLEAVE_TYPE(int32_t); break;
373 case VLC_CODEC_FL64: INTERLEAVE_TYPE(double); break;
376 #undef INTERLEAVE_TYPE
380 * Deinterleaves audio samples within a block of samples.
381 * \param dst destination buffer for planar samples
382 * \param src source buffer with interleaved samples
383 * \param samples number of samples (per channel/per plane)
384 * \param chans channels/planes count
385 * \param fourcc sample format (must be a linear sample format)
386 * \note The samples must be naturally aligned in memory.
387 * \warning Destination and source buffers MUST NOT overlap.
389 void aout_Deinterleave( void *restrict dst, const void *restrict src,
390 unsigned samples, unsigned chans, vlc_fourcc_t fourcc )
392 #define DEINTERLEAVE_TYPE(type) \
395 const type *s = src; \
396 for( size_t i = 0; i < chans; i++ ) { \
397 for( size_t j = 0, k = 0; j < samples; j++, k += chans ) \
405 case VLC_CODEC_U8: DEINTERLEAVE_TYPE(uint8_t); break;
406 case VLC_CODEC_S16N: DEINTERLEAVE_TYPE(uint16_t); break;
407 case VLC_CODEC_FL32: DEINTERLEAVE_TYPE(float); break;
408 case VLC_CODEC_S32N: DEINTERLEAVE_TYPE(int32_t); break;
409 case VLC_CODEC_FL64: DEINTERLEAVE_TYPE(double); break;
412 #undef DEINTERLEAVE_TYPE
415 /*****************************************************************************
416 * aout_ChannelExtract:
417 *****************************************************************************/
418 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
419 const uint8_t *pi_src, int i_src_channels,
421 const int *pi_selection, int i_bytes )
423 for( int i = 0; i < i_sample_count; i++ )
425 for( int j = 0; j < i_dst_channels; j++ )
426 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
427 pi_dst += i_dst_channels * i_bytes;
428 pi_src += i_src_channels * i_bytes;
432 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
433 const void *p_src, int i_src_channels,
434 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
436 /* It does not work in place */
437 assert( p_dst != p_src );
439 /* Force the compiler to inline for the specific cases so it can optimize */
440 if( i_bits_per_sample == 8 )
441 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
442 else if( i_bits_per_sample == 16 )
443 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
444 else if( i_bits_per_sample == 32 )
445 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
446 else if( i_bits_per_sample == 64 )
447 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
450 bool aout_CheckChannelExtraction( int *pi_selection,
451 uint32_t *pi_layout, int *pi_channels,
452 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
453 const uint32_t *pi_order_src, int i_channels )
455 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
456 uint32_t i_layout = 0;
458 int pi_index[AOUT_CHAN_MAX];
462 pi_order_dst = pi_vlc_chan_order_wg4;
464 /* Detect special dual mono case */
465 if( i_channels == 2 &&
466 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
468 i_layout |= AOUT_CHAN_DUALMONO;
469 pi_order_src = pi_order_dual_mono;
473 for( int i = 0; i < i_channels; i++ )
475 /* Ignore unknown or duplicated channels or not present in output */
476 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
479 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
481 if( pi_order_dst[j] == pi_order_src[i] )
483 assert( i_out < AOUT_CHAN_MAX );
484 pi_index[i_out++] = i;
485 i_layout |= pi_order_src[i];
492 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
494 for( int k = 0; k < i_out; k++ )
496 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
498 pi_selection[j++] = pi_index[k];
504 *pi_layout = i_layout;
505 *pi_channels = i_out;
507 for( int i = 0; i < i_out; i++ )
509 if( pi_selection[i] != i )
512 return i_out == i_channels;
515 /* Return the order in which filters should be inserted */
516 static int FilterOrder( const char *psz_name )
518 static const struct {
519 const char psz_name[10];
524 for( unsigned i = 0; i < ARRAY_SIZE(filter); i++ )
526 if( !strcmp( filter[i].psz_name, psz_name ) )
527 return filter[i].i_order;
532 /* This function will add or remove a a module from a string list (colon
533 * separated). It will return true if there is a modification
534 * In case p_aout is NULL, we will use configuration instead of variable */
535 bool aout_ChangeFilterString( vlc_object_t *p_obj, vlc_object_t *p_aout,
536 const char *psz_variable,
537 const char *psz_name, bool b_add )
539 if( *psz_name == '\0' )
545 psz_list = var_GetString( p_aout, psz_variable );
549 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
550 var_Destroy( p_obj->p_libvlc, psz_variable );
553 /* Split the string into an array of filters */
555 for( char *p = psz_list; p && *p; p++ )
556 i_count += *p == ':';
559 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
565 bool b_present = false;
567 for( char *p = psz_list; p && *p; )
569 char *psz_end = strchr(p, ':');
573 psz_end = p + strlen(p);
576 b_present |= !strcmp( p, psz_name );
577 ppsz_filter[i_count++] = p;
581 if( b_present == b_add )
590 int i_order = FilterOrder( psz_name );
592 for( i = 0; i < i_count; i++ )
594 if( FilterOrder( ppsz_filter[i] ) > i_order )
598 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
599 ppsz_filter[i] = psz_name;
604 for( int i = 0; i < i_count; i++ )
606 if( !strcmp( ppsz_filter[i], psz_name ) )
611 for( int i = 0; i < i_count; i++ )
612 i_length += 1 + strlen( ppsz_filter[i] );
614 char *psz_new = malloc( i_length + 1 );
616 for( int i = 0; i < i_count; i++ )
618 if( *ppsz_filter[i] == '\0' )
621 strcat( psz_new, ":" );
622 strcat( psz_new, ppsz_filter[i] );
628 var_SetString( p_aout, psz_variable, psz_new );
630 config_PutPsz( p_obj, psz_variable, psz_new );