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 /*****************************************************************************
83 * aout_FormatPrepare : compute the number of bytes per frame & frame length
84 *****************************************************************************/
85 void aout_FormatPrepare( audio_sample_format_t * p_format )
87 p_format->i_channels = aout_FormatNbChannels( p_format );
88 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
89 if( p_format->i_bitspersample > 0 )
91 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
92 * aout_FormatNbChannels( p_format );
93 p_format->i_frame_length = 1;
97 /*****************************************************************************
98 * aout_FormatPrintChannels : print a channel in a human-readable form
99 *****************************************************************************/
100 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
102 switch ( p_format->i_physical_channels )
105 case AOUT_CHAN_RIGHT:
106 case AOUT_CHAN_CENTER:
107 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
108 || (p_format->i_original_channels
109 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
111 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
114 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
115 if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
117 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
118 return "Dolby/Reverse";
119 return "Stereo/Reverse";
123 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
125 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
127 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
128 return "Stereo/Mono";
129 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
130 return "Stereo/Left";
131 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
132 return "Stereo/Right";
135 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
137 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
139 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
140 | AOUT_CHAN_REARCENTER:
142 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
143 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
145 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
146 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
148 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
149 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
151 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
152 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
155 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
156 if ( (p_format->i_original_channels & AOUT_CHAN_CENTER)
157 || (p_format->i_original_channels
158 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
160 else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
163 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
164 if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
166 else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
167 return "Dual-mono/LFE";
168 else if ( p_format->i_original_channels == AOUT_CHAN_CENTER )
170 else if ( !(p_format->i_original_channels & AOUT_CHAN_RIGHT) )
171 return "Stereo/Left/LFE";
172 else if ( !(p_format->i_original_channels & AOUT_CHAN_LEFT) )
173 return "Stereo/Right/LFE";
175 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
177 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
180 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
181 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
183 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
184 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
186 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
187 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
189 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
190 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
192 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
193 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
195 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
196 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
197 | AOUT_CHAN_MIDDLERIGHT:
199 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
200 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
201 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
203 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
204 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
205 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
212 #undef aout_FormatPrint
214 * Prints an audio sample format in a human-readable form.
216 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
217 const audio_sample_format_t *p_format )
219 msg_Dbg( obj, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
220 (char *)&p_format->i_format, p_format->i_rate,
221 aout_FormatPrintChannels( p_format ),
222 p_format->i_frame_length, p_format->i_bytes_per_frame );
225 #undef aout_FormatsPrint
227 * Prints two formats in a human-readable form
229 void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
230 const audio_sample_format_t * p_format1,
231 const audio_sample_format_t * p_format2 )
233 msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
235 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
236 p_format1->i_rate, p_format2->i_rate,
237 aout_FormatPrintChannels( p_format1 ),
238 aout_FormatPrintChannels( p_format2 ) );
241 /*****************************************************************************
242 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
243 *****************************************************************************/
244 int aout_CheckChannelReorder( const uint32_t *pi_chan_order_in,
245 const uint32_t *pi_chan_order_out,
246 uint32_t i_channel_mask,
247 int i_channels, int *pi_chan_table )
249 bool b_chan_reorder = false;
252 if( i_channels > AOUT_CHAN_MAX )
255 if( pi_chan_order_in == NULL )
256 pi_chan_order_in = pi_vlc_chan_order_wg4;
257 if( pi_chan_order_out == NULL )
258 pi_chan_order_out = pi_vlc_chan_order_wg4;
260 for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
262 if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
264 for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
266 if( i_channel_mask & pi_chan_order_out[k] ) l++;
269 pi_chan_table[j++] = l;
272 for( i = 0; i < i_channels; i++ )
274 if( pi_chan_table[i] != i ) b_chan_reorder = true;
277 return b_chan_reorder;
280 /*****************************************************************************
281 * aout_ChannelReorder :
282 *****************************************************************************/
283 void aout_ChannelReorder( uint8_t *p_buf, int i_buffer,
284 int i_channels, const int *pi_chan_table,
285 int i_bits_per_sample )
287 uint8_t p_tmp[AOUT_CHAN_MAX * 4];
290 if( i_bits_per_sample == 8 )
292 for( i = 0; i < i_buffer / i_channels; i++ )
294 for( j = 0; j < i_channels; j++ )
296 p_tmp[pi_chan_table[j]] = p_buf[j];
299 memcpy( p_buf, p_tmp, i_channels );
303 else if( i_bits_per_sample == 16 )
305 for( i = 0; i < i_buffer / i_channels / 2; i++ )
307 for( j = 0; j < i_channels; j++ )
309 p_tmp[2 * pi_chan_table[j]] = p_buf[2 * j];
310 p_tmp[2 * pi_chan_table[j] + 1] = p_buf[2 * j + 1];
313 memcpy( p_buf, p_tmp, 2 * i_channels );
314 p_buf += 2 * i_channels;
317 else if( i_bits_per_sample == 24 )
319 for( i = 0; i < i_buffer / i_channels / 3; i++ )
321 for( j = 0; j < i_channels; j++ )
323 p_tmp[3 * pi_chan_table[j]] = p_buf[3 * j];
324 p_tmp[3 * pi_chan_table[j] + 1] = p_buf[3 * j + 1];
325 p_tmp[3 * pi_chan_table[j] + 2] = p_buf[3 * j + 2];
328 memcpy( p_buf, p_tmp, 3 * i_channels );
329 p_buf += 3 * i_channels;
332 else if( i_bits_per_sample == 32 )
334 for( i = 0; i < i_buffer / i_channels / 4; i++ )
336 for( j = 0; j < i_channels; j++ )
338 p_tmp[4 * pi_chan_table[j]] = p_buf[4 * j];
339 p_tmp[4 * pi_chan_table[j] + 1] = p_buf[4 * j + 1];
340 p_tmp[4 * pi_chan_table[j] + 2] = p_buf[4 * j + 2];
341 p_tmp[4 * pi_chan_table[j] + 3] = p_buf[4 * j + 3];
344 memcpy( p_buf, p_tmp, 4 * i_channels );
345 p_buf += 4 * i_channels;
350 /*****************************************************************************
351 * aout_ChannelExtract:
352 *****************************************************************************/
353 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
354 const uint8_t *pi_src, int i_src_channels,
356 const int *pi_selection, int i_bytes )
358 for( int i = 0; i < i_sample_count; i++ )
360 for( int j = 0; j < i_dst_channels; j++ )
361 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
362 pi_dst += i_dst_channels * i_bytes;
363 pi_src += i_src_channels * i_bytes;
367 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
368 const void *p_src, int i_src_channels,
369 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
371 /* It does not work in place */
372 assert( p_dst != p_src );
374 /* Force the compiler to inline for the specific cases so it can optimize */
375 if( i_bits_per_sample == 8 )
376 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
377 else if( i_bits_per_sample == 16 )
378 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
379 else if( i_bits_per_sample == 24 )
380 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 3 );
381 else if( i_bits_per_sample == 32 )
382 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
383 else if( i_bits_per_sample == 64 )
384 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
387 bool aout_CheckChannelExtraction( int *pi_selection,
388 uint32_t *pi_layout, int *pi_channels,
389 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
390 const uint32_t *pi_order_src, int i_channels )
392 const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
393 uint32_t i_layout = 0;
395 int pi_index[AOUT_CHAN_MAX];
399 pi_order_dst = pi_vlc_chan_order_wg4;
401 /* Detect special dual mono case */
402 if( i_channels == 2 &&
403 pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
405 i_layout |= AOUT_CHAN_DUALMONO;
406 pi_order_src = pi_order_dual_mono;
410 for( int i = 0; i < i_channels; i++ )
412 /* Ignore unknown or duplicated channels or not present in output */
413 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
416 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
418 if( pi_order_dst[j] == pi_order_src[i] )
420 assert( i_out < AOUT_CHAN_MAX );
421 pi_index[i_out++] = i;
422 i_layout |= pi_order_src[i];
429 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
431 for( int k = 0; k < i_out; k++ )
433 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
435 pi_selection[j++] = pi_index[k];
441 *pi_layout = i_layout;
442 *pi_channels = i_out;
444 for( int i = 0; i < i_out; i++ )
446 if( pi_selection[i] != i )
449 return i_out == i_channels;
452 /* Return the order in which filters should be inserted */
453 static int FilterOrder( const char *psz_name )
455 static const struct {
456 const char psz_name[10];
461 for( unsigned i = 0; i < ARRAY_SIZE(filter); i++ )
463 if( !strcmp( filter[i].psz_name, psz_name ) )
464 return filter[i].i_order;
469 /* This function will add or remove a a module from a string list (colon
470 * separated). It will return true if there is a modification
471 * In case p_aout is NULL, we will use configuration instead of variable */
472 bool aout_ChangeFilterString( vlc_object_t *p_obj, vlc_object_t *p_aout,
473 const char *psz_variable,
474 const char *psz_name, bool b_add )
476 if( *psz_name == '\0' )
482 psz_list = var_GetString( p_aout, psz_variable );
486 psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
487 var_Destroy( p_obj->p_libvlc, psz_variable );
490 /* Split the string into an array of filters */
492 for( char *p = psz_list; p && *p; p++ )
493 i_count += *p == ':';
496 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
502 bool b_present = false;
504 for( char *p = psz_list; p && *p; )
506 char *psz_end = strchr(p, ':');
510 psz_end = p + strlen(p);
513 b_present |= !strcmp( p, psz_name );
514 ppsz_filter[i_count++] = p;
518 if( b_present == b_add )
527 int i_order = FilterOrder( psz_name );
529 for( i = 0; i < i_count; i++ )
531 if( FilterOrder( ppsz_filter[i] ) > i_order )
535 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
536 ppsz_filter[i] = psz_name;
541 for( int i = 0; i < i_count; i++ )
543 if( !strcmp( ppsz_filter[i], psz_name ) )
548 for( int i = 0; i < i_count; i++ )
549 i_length += 1 + strlen( ppsz_filter[i] );
551 char *psz_new = malloc( i_length + 1 );
553 for( int i = 0; i < i_count; i++ )
555 if( *ppsz_filter[i] == '\0' )
558 strcat( psz_new, ":" );
559 strcat( psz_new, ppsz_filter[i] );
565 var_SetString( p_aout, psz_variable, psz_new );
567 config_PutPsz( p_obj, psz_variable, psz_new );