1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
30 #include <vlc_common.h>
32 #include "aout_internal.h"
35 * Formats management (internal and external)
38 /*****************************************************************************
39 * aout_BitsPerSample : get the number of bits per sample
40 *****************************************************************************/
41 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format
)
43 switch( vlc_fourcc_GetCodec( AUDIO_ES
, i_format
) )
63 case VLC_CODEC_S24L32
:
64 case VLC_CODEC_S24B32
:
78 /* For these formats the caller has to indicate the parameters
84 /*****************************************************************************
85 * aout_FormatPrepare : compute the number of bytes per frame & frame length
86 *****************************************************************************/
87 void aout_FormatPrepare( audio_sample_format_t
* p_format
)
90 unsigned i_channels
= aout_FormatNbChannels( p_format
);
92 p_format
->i_channels
= i_channels
;
93 p_format
->i_bitspersample
= aout_BitsPerSample( p_format
->i_format
);
94 if( p_format
->i_bitspersample
> 0 )
96 p_format
->i_bytes_per_frame
= ( p_format
->i_bitspersample
/ 8 )
97 * p_format
->i_channels
;
98 p_format
->i_frame_length
= 1;
102 /*****************************************************************************
103 * aout_FormatPrintChannels : print a channel in a human-readable form
104 *****************************************************************************/
105 const char * aout_FormatPrintChannels( const audio_sample_format_t
* p_format
)
107 if (p_format
->channel_type
== AUDIO_CHANNEL_TYPE_AMBISONICS
)
110 /* AUDIO_CHANNEL_TYPE_BITMAP */
111 switch ( p_format
->i_physical_channels
)
114 case AOUT_CHAN_RIGHT
:
115 case AOUT_CHAN_CENTER
:
116 if ( (p_format
->i_physical_channels
& AOUT_CHAN_CENTER
)
117 || (p_format
->i_physical_channels
118 & (AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
)) )
120 else if ( p_format
->i_physical_channels
& AOUT_CHAN_LEFT
)
123 case AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
:
124 if ( p_format
->i_chan_mode
& AOUT_CHANMODE_DOLBYSTEREO
)
126 else if ( p_format
->i_chan_mode
& AOUT_CHANMODE_DUALMONO
)
128 else if ( p_format
->i_physical_channels
== AOUT_CHAN_CENTER
)
129 return "Stereo/Mono";
130 else if ( !(p_format
->i_physical_channels
& AOUT_CHAN_RIGHT
) )
131 return "Stereo/Left";
132 else if ( !(p_format
->i_physical_channels
& AOUT_CHAN_LEFT
) )
133 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_physical_channels
& AOUT_CHAN_CENTER
)
157 || (p_format
->i_physical_channels
158 & (AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
)) )
160 else if ( p_format
->i_physical_channels
& AOUT_CHAN_LEFT
)
163 case AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_LFE
:
164 if ( p_format
->i_chan_mode
& AOUT_CHANMODE_DOLBYSTEREO
)
166 else if ( p_format
->i_chan_mode
& AOUT_CHANMODE_DUALMONO
)
167 return "Dual-mono/LFE";
168 else if ( p_format
->i_physical_channels
== AOUT_CHAN_CENTER
)
170 else if ( !(p_format
->i_physical_channels
& AOUT_CHAN_RIGHT
) )
171 return "Stereo/Left/LFE";
172 else if ( !(p_format
->i_physical_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_REARLEFT
| AOUT_CHAN_REARRIGHT
| AOUT_CHAN_REARCENTER
196 case AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
197 | AOUT_CHAN_MIDDLELEFT
| AOUT_CHAN_MIDDLERIGHT
| AOUT_CHAN_LFE
:
199 case AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
200 | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
| AOUT_CHAN_MIDDLELEFT
201 | AOUT_CHAN_MIDDLERIGHT
:
203 case AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
204 | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
| AOUT_CHAN_MIDDLELEFT
205 | AOUT_CHAN_MIDDLERIGHT
:
207 case AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
208 | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
| AOUT_CHAN_MIDDLELEFT
209 | AOUT_CHAN_MIDDLERIGHT
| AOUT_CHAN_LFE
:
211 case AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
212 | AOUT_CHAN_REARCENTER
| AOUT_CHAN_MIDDLELEFT
213 | AOUT_CHAN_MIDDLERIGHT
| AOUT_CHAN_LFE
:
215 case AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
216 | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
| AOUT_CHAN_REARCENTER
217 | AOUT_CHAN_MIDDLELEFT
| AOUT_CHAN_MIDDLERIGHT
| AOUT_CHAN_LFE
:
221 return "Unknown-chan-mask";
224 #undef aout_FormatPrint
226 * Prints an audio sample format in a human-readable form.
228 void aout_FormatPrint( vlc_object_t
*obj
, const char *psz_text
,
229 const audio_sample_format_t
*p_format
)
231 msg_Dbg( obj
, "%s '%4.4s' %d Hz %s frame=%u samples/%u bytes", psz_text
,
232 (char *)&p_format
->i_format
, p_format
->i_rate
,
233 aout_FormatPrintChannels( p_format
),
234 p_format
->i_frame_length
, p_format
->i_bytes_per_frame
);
237 #undef aout_FormatsPrint
239 * Prints two formats in a human-readable form
241 void aout_FormatsPrint( vlc_object_t
*obj
, const char * psz_text
,
242 const audio_sample_format_t
* p_format1
,
243 const audio_sample_format_t
* p_format2
)
245 msg_Dbg( obj
, "%s '%4.4s'->'%4.4s' %u Hz->%u Hz %s->%s",
247 (char *)&p_format1
->i_format
, (char *)&p_format2
->i_format
,
248 p_format1
->i_rate
, p_format2
->i_rate
,
249 aout_FormatPrintChannels( p_format1
),
250 aout_FormatPrintChannels( p_format2
) );
253 /*****************************************************************************
254 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
255 *****************************************************************************/
256 unsigned aout_CheckChannelReorder( const uint32_t *chans_in
,
257 const uint32_t *chans_out
,
258 uint32_t mask
, uint8_t *restrict table
)
260 static_assert(AOUT_CHAN_MAX
<= (sizeof (mask
) * CHAR_BIT
), "Missing bits");
262 unsigned channels
= 0;
264 if( chans_in
== NULL
)
265 chans_in
= pi_vlc_chan_order_wg4
;
266 if( chans_out
== NULL
)
267 chans_out
= pi_vlc_chan_order_wg4
;
269 for( unsigned i
= 0; chans_in
[i
]; i
++ )
271 const uint32_t chan
= chans_in
[i
];
276 for( unsigned j
= 0; chan
!= chans_out
[j
]; j
++ )
277 if( mask
& chans_out
[j
] )
280 table
[channels
++] = index
;
283 for( unsigned i
= 0; i
< channels
; i
++ )
290 * Reorders audio samples within a block of linear audio interleaved samples.
291 * \param ptr start address of the block of samples
292 * \param bytes size of the block in bytes (must be a multiple of the product
293 * of the channels count and the sample size)
294 * \param channels channels count (also length of the chans_table table)
295 * \param chans_table permutation table to reorder the channels
296 * (usually computed by aout_CheckChannelReorder())
297 * \param fourcc sample format (must be a linear sample format)
298 * \note The samples must be naturally aligned in memory.
300 void aout_ChannelReorder( void *ptr
, size_t bytes
, uint8_t channels
,
301 const uint8_t *restrict chans_table
, vlc_fourcc_t fourcc
)
303 if( unlikely(bytes
== 0) )
306 assert( channels
!= 0 );
308 /* The audio formats supported in audio output are inlined. For other
309 * formats (used in demuxers and muxers), memcpy() is used to avoid
310 * breaking type punning. */
311 #define REORDER_TYPE(type) \
313 const size_t frames = (bytes / sizeof (type)) / channels; \
316 for( size_t i = 0; i < frames; i++ ) \
318 type tmp[AOUT_CHAN_MAX]; \
320 for( size_t j = 0; j < channels; j++ ) \
321 tmp[chans_table[j]] = buf[j]; \
322 memcpy( buf, tmp, sizeof (type) * channels ); \
327 if( likely(channels
<= AOUT_CHAN_MAX
) )
331 case VLC_CODEC_U8
: REORDER_TYPE(uint8_t); return;
332 case VLC_CODEC_S16N
: REORDER_TYPE(int16_t); return;
333 case VLC_CODEC_FL32
: REORDER_TYPE(float); return;
334 case VLC_CODEC_S32N
: REORDER_TYPE(int32_t); return;
335 case VLC_CODEC_FL64
: REORDER_TYPE(double); return;
339 unsigned size
= aout_BitsPerSample( fourcc
) / 8;
340 assert( size
!= 0 && size
<= 8 );
342 const size_t frames
= bytes
/ (size
* channels
);
343 unsigned char *buf
= ptr
;
345 for( size_t i
= 0; i
< frames
; i
++ )
347 unsigned char tmp
[256 * 8];
349 for( size_t j
= 0; j
< channels
; j
++ )
350 memcpy( tmp
+ size
* chans_table
[j
], buf
+ size
* j
, size
);
351 memcpy( buf
, tmp
, size
* channels
);
352 buf
+= size
* channels
;
357 * Interleaves audio samples within a block of samples.
358 * \param dst destination buffer for interleaved samples
359 * \param srcv source buffers (one per plane) of uninterleaved samples
360 * \param samples number of samples (per channel/per plane)
361 * \param chans channels/planes count
362 * \param fourcc sample format (must be a linear sample format)
363 * \note The samples must be naturally aligned in memory.
364 * \warning Destination and source buffers MUST NOT overlap.
366 void aout_Interleave( void *restrict dst
, const void *const *srcv
,
367 unsigned samples
, unsigned chans
, vlc_fourcc_t fourcc
)
369 #define INTERLEAVE_TYPE(type) \
372 for( size_t i = 0; i < chans; i++ ) { \
373 const type *s = srcv[i]; \
374 for( size_t j = 0, k = 0; j < samples; j++, k += chans ) \
382 case VLC_CODEC_U8
: INTERLEAVE_TYPE(uint8_t); break;
383 case VLC_CODEC_S16N
: INTERLEAVE_TYPE(int16_t); break;
384 case VLC_CODEC_FL32
: INTERLEAVE_TYPE(float); break;
385 case VLC_CODEC_S32N
: INTERLEAVE_TYPE(int32_t); break;
386 case VLC_CODEC_FL64
: INTERLEAVE_TYPE(double); break;
387 default: vlc_assert_unreachable();
389 #undef INTERLEAVE_TYPE
393 * Deinterleaves audio samples within a block of samples.
394 * \param dst destination buffer for planar samples
395 * \param src source buffer with interleaved samples
396 * \param samples number of samples (per channel/per plane)
397 * \param chans channels/planes count
398 * \param fourcc sample format (must be a linear sample format)
399 * \note The samples must be naturally aligned in memory.
400 * \warning Destination and source buffers MUST NOT overlap.
402 void aout_Deinterleave( void *restrict dst
, const void *restrict src
,
403 unsigned samples
, unsigned chans
, vlc_fourcc_t fourcc
)
405 #define DEINTERLEAVE_TYPE(type) \
408 const type *s = src; \
409 for( size_t i = 0; i < chans; i++ ) { \
410 for( size_t j = 0, k = 0; j < samples; j++, k += chans ) \
418 case VLC_CODEC_U8
: DEINTERLEAVE_TYPE(uint8_t); break;
419 case VLC_CODEC_S16N
: DEINTERLEAVE_TYPE(int16_t); break;
420 case VLC_CODEC_FL32
: DEINTERLEAVE_TYPE(float); break;
421 case VLC_CODEC_S32N
: DEINTERLEAVE_TYPE(int32_t); break;
422 case VLC_CODEC_FL64
: DEINTERLEAVE_TYPE(double); break;
423 default: vlc_assert_unreachable();
425 #undef DEINTERLEAVE_TYPE
428 /*****************************************************************************
429 * aout_ChannelExtract:
430 *****************************************************************************/
431 static inline void ExtractChannel( uint8_t *pi_dst
, int i_dst_channels
,
432 const uint8_t *pi_src
, int i_src_channels
,
434 const int *pi_selection
, int i_bytes
)
436 for( int i
= 0; i
< i_sample_count
; i
++ )
438 for( int j
= 0; j
< i_dst_channels
; j
++ )
439 memcpy( &pi_dst
[j
* i_bytes
], &pi_src
[pi_selection
[j
] * i_bytes
], i_bytes
);
440 pi_dst
+= i_dst_channels
* i_bytes
;
441 pi_src
+= i_src_channels
* i_bytes
;
445 void aout_ChannelExtract( void *p_dst
, int i_dst_channels
,
446 const void *p_src
, int i_src_channels
,
447 int i_sample_count
, const int *pi_selection
, int i_bits_per_sample
)
449 /* It does not work in place */
450 assert( p_dst
!= p_src
);
452 /* Force the compiler to inline for the specific cases so it can optimize */
453 if( i_bits_per_sample
== 8 )
454 ExtractChannel( p_dst
, i_dst_channels
, p_src
, i_src_channels
, i_sample_count
, pi_selection
, 1 );
455 else if( i_bits_per_sample
== 16 )
456 ExtractChannel( p_dst
, i_dst_channels
, p_src
, i_src_channels
, i_sample_count
, pi_selection
, 2 );
457 else if( i_bits_per_sample
== 32 )
458 ExtractChannel( p_dst
, i_dst_channels
, p_src
, i_src_channels
, i_sample_count
, pi_selection
, 4 );
459 else if( i_bits_per_sample
== 64 )
460 ExtractChannel( p_dst
, i_dst_channels
, p_src
, i_src_channels
, i_sample_count
, pi_selection
, 8 );
463 bool aout_CheckChannelExtraction( int *pi_selection
,
464 uint32_t *pi_layout
, int *pi_channels
,
465 const uint32_t pi_order_dst
[AOUT_CHAN_MAX
],
466 const uint32_t *pi_order_src
, int i_channels
)
468 static_assert(AOUT_CHAN_MAX
<= (sizeof (*pi_order_dst
) * CHAR_BIT
),
471 uint32_t i_layout
= 0;
473 int pi_index
[AOUT_CHAN_MAX
];
477 pi_order_dst
= pi_vlc_chan_order_wg4
;
480 for( int i
= 0; i
< i_channels
; i
++ )
482 /* Ignore unknown or duplicated channels or not present in output */
483 if( !pi_order_src
[i
] || (i_layout
& pi_order_src
[i
]) )
486 for( int j
= 0; j
< AOUT_CHAN_MAX
; j
++ )
488 if( pi_order_dst
[j
] == pi_order_src
[i
] )
490 assert( i_out
< AOUT_CHAN_MAX
);
491 pi_index
[i_out
++] = i
;
492 i_layout
|= pi_order_src
[i
];
499 for( int i
= 0, j
= 0; i
< AOUT_CHAN_MAX
; i
++ )
501 for( int k
= 0; k
< i_out
; k
++ )
503 if( pi_order_dst
[i
] == pi_order_src
[pi_index
[k
]] )
505 pi_selection
[j
++] = pi_index
[k
];
511 *pi_layout
= i_layout
;
512 *pi_channels
= i_out
;
514 for( int i
= 0; i
< i_out
; i
++ )
516 if( pi_selection
[i
] != i
)
519 return i_out
!= i_channels
;
522 /* Return the order in which filters should be inserted */
523 static int FilterOrder( const char *psz_name
)
525 static const struct {
526 const char psz_name
[10];
531 for( unsigned i
= 0; i
< ARRAY_SIZE(filter
); i
++ )
533 if( !strcmp( filter
[i
].psz_name
, psz_name
) )
534 return filter
[i
].i_order
;
539 int aout_EnableFilter( audio_output_t
*p_aout
, const char *psz_name
, bool b_add
)
541 if( *psz_name
== '\0' )
544 const char *psz_variable
= "audio-filter";
546 psz_list
= var_GetString( p_aout
, psz_variable
);
548 /* Split the string into an array of filters */
550 for( char *p
= psz_list
; p
&& *p
; p
++ )
551 i_count
+= *p
== ':';
554 const char **ppsz_filter
= calloc( i_count
, sizeof(*ppsz_filter
) );
560 bool b_present
= false;
562 for( char *p
= psz_list
; p
&& *p
; )
564 char *psz_end
= strchr(p
, ':');
568 psz_end
= p
+ strlen(p
);
571 b_present
|= !strcmp( p
, psz_name
);
572 ppsz_filter
[i_count
++] = p
;
576 if( b_present
== b_add
)
585 int i_order
= FilterOrder( psz_name
);
587 for( i
= 0; i
< i_count
; i
++ )
589 if( FilterOrder( ppsz_filter
[i
] ) > i_order
)
593 memmove( &ppsz_filter
[i
+1], &ppsz_filter
[i
], (i_count
- i
) * sizeof(*ppsz_filter
) );
594 ppsz_filter
[i
] = psz_name
;
599 for( int i
= 0; i
< i_count
; i
++ )
601 if( !strcmp( ppsz_filter
[i
], psz_name
) )
606 for( int i
= 0; i
< i_count
; i
++ )
607 i_length
+= 1 + strlen( ppsz_filter
[i
] );
609 char *psz_new
= malloc( i_length
+ 1 );
611 if( unlikely( !psz_new
) )
619 for( int i
= 0; i
< i_count
; i
++ )
621 if( *ppsz_filter
[i
] == '\0' )
624 strcat( psz_new
, ":" );
625 strcat( psz_new
, ppsz_filter
[i
] );
630 var_SetString( p_aout
, psz_variable
, psz_new
);