1 /*****************************************************************************
2 * common.c : audio output management of common data structures
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
5 * $Id: f7dbc575578979ffa5588155fef8c08f4d2e14c5 $
6 *
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 *
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.
13 *
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.
18 *
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 *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <limits.h>
29 #include <assert.h>
30
31 #include <vlc_common.h>
32 #include <vlc_aout.h>
33 #include "aout_internal.h"
34
35 /*
36 * Formats management (internal and external)
37 */
38
39 /*****************************************************************************
40 * aout_BitsPerSample : get the number of bits per sample
41 *****************************************************************************/
aout_BitsPerSample(vlc_fourcc_t i_format)42 unsigned int aout_BitsPerSample( vlc_fourcc_t i_format )
43 {
44 switch( vlc_fourcc_GetCodec( AUDIO_ES, i_format ) )
45 {
46 case VLC_CODEC_U8:
47 case VLC_CODEC_S8:
48 case VLC_CODEC_ALAW:
49 case VLC_CODEC_MULAW:
50 return 8;
51
52 case VLC_CODEC_U16L:
53 case VLC_CODEC_S16L:
54 case VLC_CODEC_U16B:
55 case VLC_CODEC_S16B:
56 return 16;
57
58 case VLC_CODEC_U24L:
59 case VLC_CODEC_S24L:
60 case VLC_CODEC_U24B:
61 case VLC_CODEC_S24B:
62 return 24;
63
64 case VLC_CODEC_S24L32:
65 case VLC_CODEC_S24B32:
66 case VLC_CODEC_U32L:
67 case VLC_CODEC_U32B:
68 case VLC_CODEC_S32L:
69 case VLC_CODEC_S32B:
70 case VLC_CODEC_F32L:
71 case VLC_CODEC_F32B:
72 return 32;
73
74 case VLC_CODEC_F64L:
75 case VLC_CODEC_F64B:
76 return 64;
77
78 default:
79 /* For these formats the caller has to indicate the parameters
80 * by hand. */
81 return 0;
82 }
83 }
84
85 /*****************************************************************************
86 * aout_FormatPrepare : compute the number of bytes per frame & frame length
87 *****************************************************************************/
aout_FormatPrepare(audio_sample_format_t * p_format)88 void aout_FormatPrepare( audio_sample_format_t * p_format )
89 {
90
91 unsigned i_channels = aout_FormatNbChannels( p_format );
92 if( i_channels > 0 )
93 p_format->i_channels = i_channels;
94 p_format->i_bitspersample = aout_BitsPerSample( p_format->i_format );
95 if( p_format->i_bitspersample > 0 )
96 {
97 p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
98 * p_format->i_channels;
99 p_format->i_frame_length = 1;
100 }
101 }
102
103 /*****************************************************************************
104 * aout_FormatPrintChannels : print a channel in a human-readable form
105 *****************************************************************************/
aout_FormatPrintChannels(const audio_sample_format_t * p_format)106 const char * aout_FormatPrintChannels( const audio_sample_format_t * p_format )
107 {
108 if (p_format->channel_type == AUDIO_CHANNEL_TYPE_AMBISONICS)
109 return "Ambisonics";
110
111 /* AUDIO_CHANNEL_TYPE_BITMAP */
112 switch ( p_format->i_physical_channels )
113 {
114 case AOUT_CHAN_LEFT:
115 case AOUT_CHAN_RIGHT:
116 case AOUT_CHAN_CENTER:
117 if ( (p_format->i_physical_channels & AOUT_CHAN_CENTER)
118 || (p_format->i_physical_channels
119 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
120 return "Mono";
121 else if ( p_format->i_physical_channels & AOUT_CHAN_LEFT )
122 return "Left";
123 return "Right";
124 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
125 if ( p_format->i_chan_mode & AOUT_CHANMODE_DOLBYSTEREO )
126 return "Dolby";
127 else if ( p_format->i_chan_mode & AOUT_CHANMODE_DUALMONO )
128 return "Dual-mono";
129 else if ( p_format->i_physical_channels == AOUT_CHAN_CENTER )
130 return "Stereo/Mono";
131 else if ( !(p_format->i_physical_channels & AOUT_CHAN_RIGHT) )
132 return "Stereo/Left";
133 else if ( !(p_format->i_physical_channels & AOUT_CHAN_LEFT) )
134 return "Stereo/Right";
135 return "Stereo";
136 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
137 return "3F";
138 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
139 return "2F1R";
140 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
141 | AOUT_CHAN_REARCENTER:
142 return "3F1R";
143 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
144 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
145 return "2F2R";
146 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
147 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
148 return "2F2M";
149 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
150 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
151 return "3F2R";
152 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
153 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
154 return "3F2M";
155
156 case AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
157 if ( (p_format->i_physical_channels & AOUT_CHAN_CENTER)
158 || (p_format->i_physical_channels
159 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
160 return "Mono/LFE";
161 else if ( p_format->i_physical_channels & AOUT_CHAN_LEFT )
162 return "Left/LFE";
163 return "Right/LFE";
164 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
165 if ( p_format->i_chan_mode & AOUT_CHANMODE_DOLBYSTEREO )
166 return "Dolby/LFE";
167 else if ( p_format->i_chan_mode & AOUT_CHANMODE_DUALMONO )
168 return "Dual-mono/LFE";
169 else if ( p_format->i_physical_channels == AOUT_CHAN_CENTER )
170 return "Mono/LFE";
171 else if ( !(p_format->i_physical_channels & AOUT_CHAN_RIGHT) )
172 return "Stereo/Left/LFE";
173 else if ( !(p_format->i_physical_channels & AOUT_CHAN_LEFT) )
174 return "Stereo/Right/LFE";
175 return "Stereo/LFE";
176 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
177 return "3F/LFE";
178 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
179 | AOUT_CHAN_LFE:
180 return "2F1R/LFE";
181 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
182 | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
183 return "3F1R/LFE";
184 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
185 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
186 return "2F2R/LFE";
187 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
188 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
189 return "2F2M/LFE";
190 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
191 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
192 return "3F2R/LFE";
193 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
194 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER
195 | AOUT_CHAN_LFE:
196 return "3F3R/LFE";
197 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
198 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
199 return "3F2M/LFE";
200 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
201 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
202 | AOUT_CHAN_MIDDLERIGHT:
203 return "2F2M2R";
204 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
205 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
206 | AOUT_CHAN_MIDDLERIGHT:
207 return "3F2M2R";
208 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
209 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
210 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
211 return "3F2M2R/LFE";
212 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
213 | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
214 | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
215 return "3F2M1R/LFE";
216 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
217 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER
218 | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
219 return "3F2M3R/LFE";
220 }
221
222 return "Unknown-chan-mask";
223 }
224
225 #undef aout_FormatPrint
226 /**
227 * Prints an audio sample format in a human-readable form.
228 */
aout_FormatPrint(vlc_object_t * obj,const char * psz_text,const audio_sample_format_t * p_format)229 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
230 const audio_sample_format_t *p_format )
231 {
232 msg_Dbg( obj, "%s '%4.4s' %d Hz %s frame=%d samples/%d bytes", psz_text,
233 (char *)&p_format->i_format, p_format->i_rate,
234 aout_FormatPrintChannels( p_format ),
235 p_format->i_frame_length, p_format->i_bytes_per_frame );
236 }
237
238 #undef aout_FormatsPrint
239 /**
240 * Prints two formats in a human-readable form
241 */
aout_FormatsPrint(vlc_object_t * obj,const char * psz_text,const audio_sample_format_t * p_format1,const audio_sample_format_t * p_format2)242 void aout_FormatsPrint( vlc_object_t *obj, const char * psz_text,
243 const audio_sample_format_t * p_format1,
244 const audio_sample_format_t * p_format2 )
245 {
246 msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
247 psz_text,
248 (char *)&p_format1->i_format, (char *)&p_format2->i_format,
249 p_format1->i_rate, p_format2->i_rate,
250 aout_FormatPrintChannels( p_format1 ),
251 aout_FormatPrintChannels( p_format2 ) );
252 }
253
254 /*****************************************************************************
255 * aout_CheckChannelReorder : Check if we need to do some channel re-ordering
256 *****************************************************************************/
aout_CheckChannelReorder(const uint32_t * chans_in,const uint32_t * chans_out,uint32_t mask,uint8_t * restrict table)257 unsigned aout_CheckChannelReorder( const uint32_t *chans_in,
258 const uint32_t *chans_out,
259 uint32_t mask, uint8_t *restrict table )
260 {
261 static_assert(AOUT_CHAN_MAX <= (sizeof (mask) * CHAR_BIT), "Missing bits");
262
263 unsigned channels = 0;
264
265 if( chans_in == NULL )
266 chans_in = pi_vlc_chan_order_wg4;
267 if( chans_out == NULL )
268 chans_out = pi_vlc_chan_order_wg4;
269
270 for( unsigned i = 0; chans_in[i]; i++ )
271 {
272 const uint32_t chan = chans_in[i];
273 if( !(mask & chan) )
274 continue;
275
276 unsigned index = 0;
277 for( unsigned j = 0; chan != chans_out[j]; j++ )
278 if( mask & chans_out[j] )
279 index++;
280
281 table[channels++] = index;
282 }
283
284 for( unsigned i = 0; i < channels; i++ )
285 if( table[i] != i )
286 return channels;
287 return 0;
288 }
289
290 /**
291 * Reorders audio samples within a block of linear audio interleaved samples.
292 * \param ptr start address of the block of samples
293 * \param bytes size of the block in bytes (must be a multiple of the product
294 * of the channels count and the sample size)
295 * \param channels channels count (also length of the chans_table table)
296 * \param chans_table permutation table to reorder the channels
297 * (usually computed by aout_CheckChannelReorder())
298 * \param fourcc sample format (must be a linear sample format)
299 * \note The samples must be naturally aligned in memory.
300 */
aout_ChannelReorder(void * ptr,size_t bytes,uint8_t channels,const uint8_t * restrict chans_table,vlc_fourcc_t fourcc)301 void aout_ChannelReorder( void *ptr, size_t bytes, uint8_t channels,
302 const uint8_t *restrict chans_table, vlc_fourcc_t fourcc )
303 {
304 if( unlikely(bytes == 0) )
305 return;
306
307 assert( channels != 0 );
308
309 /* The audio formats supported in audio output are inlined. For other
310 * formats (used in demuxers and muxers), memcpy() is used to avoid
311 * breaking type punning. */
312 #define REORDER_TYPE(type) \
313 do { \
314 const size_t frames = (bytes / sizeof (type)) / channels; \
315 type *buf = ptr; \
316 \
317 for( size_t i = 0; i < frames; i++ ) \
318 { \
319 type tmp[AOUT_CHAN_MAX]; \
320 \
321 for( size_t j = 0; j < channels; j++ ) \
322 tmp[chans_table[j]] = buf[j]; \
323 memcpy( buf, tmp, sizeof (type) * channels ); \
324 buf += channels; \
325 } \
326 } while(0)
327
328 if( likely(channels <= AOUT_CHAN_MAX) )
329 {
330 switch( fourcc )
331 {
332 case VLC_CODEC_U8: REORDER_TYPE(uint8_t); return;
333 case VLC_CODEC_S16N: REORDER_TYPE(int16_t); return;
334 case VLC_CODEC_FL32: REORDER_TYPE(float); return;
335 case VLC_CODEC_S32N: REORDER_TYPE(int32_t); return;
336 case VLC_CODEC_FL64: REORDER_TYPE(double); return;
337 }
338 }
339
340 unsigned size = aout_BitsPerSample( fourcc ) / 8;
341 assert( size != 0 && size <= 8 );
342
343 const size_t frames = bytes / (size * channels);
344 unsigned char *buf = ptr;
345
346 for( size_t i = 0; i < frames; i++ )
347 {
348 unsigned char tmp[256 * 8];
349
350 for( size_t j = 0; j < channels; j++ )
351 memcpy( tmp + size * chans_table[j], buf + size * j, size );
352 memcpy( buf, tmp, size * channels );
353 buf += size * channels;
354 }
355 }
356
357 /**
358 * Interleaves audio samples within a block of samples.
359 * \param dst destination buffer for interleaved samples
360 * \param srcv source buffers (one per plane) of uninterleaved samples
361 * \param samples number of samples (per channel/per plane)
362 * \param chans channels/planes count
363 * \param fourcc sample format (must be a linear sample format)
364 * \note The samples must be naturally aligned in memory.
365 * \warning Destination and source buffers MUST NOT overlap.
366 */
aout_Interleave(void * restrict dst,const void * const * srcv,unsigned samples,unsigned chans,vlc_fourcc_t fourcc)367 void aout_Interleave( void *restrict dst, const void *const *srcv,
368 unsigned samples, unsigned chans, vlc_fourcc_t fourcc )
369 {
370 #define INTERLEAVE_TYPE(type) \
371 do { \
372 type *d = dst; \
373 for( size_t i = 0; i < chans; i++ ) { \
374 const type *s = srcv[i]; \
375 for( size_t j = 0, k = 0; j < samples; j++, k += chans ) \
376 d[k] = *(s++); \
377 d++; \
378 } \
379 } while(0)
380
381 switch( fourcc )
382 {
383 case VLC_CODEC_U8: INTERLEAVE_TYPE(uint8_t); break;
384 case VLC_CODEC_S16N: INTERLEAVE_TYPE(int16_t); break;
385 case VLC_CODEC_FL32: INTERLEAVE_TYPE(float); break;
386 case VLC_CODEC_S32N: INTERLEAVE_TYPE(int32_t); break;
387 case VLC_CODEC_FL64: INTERLEAVE_TYPE(double); break;
388 default: vlc_assert_unreachable();
389 }
390 #undef INTERLEAVE_TYPE
391 }
392
393 /**
394 * Deinterleaves audio samples within a block of samples.
395 * \param dst destination buffer for planar samples
396 * \param src source buffer with interleaved samples
397 * \param samples number of samples (per channel/per plane)
398 * \param chans channels/planes count
399 * \param fourcc sample format (must be a linear sample format)
400 * \note The samples must be naturally aligned in memory.
401 * \warning Destination and source buffers MUST NOT overlap.
402 */
aout_Deinterleave(void * restrict dst,const void * restrict src,unsigned samples,unsigned chans,vlc_fourcc_t fourcc)403 void aout_Deinterleave( void *restrict dst, const void *restrict src,
404 unsigned samples, unsigned chans, vlc_fourcc_t fourcc )
405 {
406 #define DEINTERLEAVE_TYPE(type) \
407 do { \
408 type *d = dst; \
409 const type *s = src; \
410 for( size_t i = 0; i < chans; i++ ) { \
411 for( size_t j = 0, k = 0; j < samples; j++, k += chans ) \
412 *(d++) = s[k]; \
413 s++; \
414 } \
415 } while(0)
416
417 switch( fourcc )
418 {
419 case VLC_CODEC_U8: DEINTERLEAVE_TYPE(uint8_t); break;
420 case VLC_CODEC_S16N: DEINTERLEAVE_TYPE(int16_t); break;
421 case VLC_CODEC_FL32: DEINTERLEAVE_TYPE(float); break;
422 case VLC_CODEC_S32N: DEINTERLEAVE_TYPE(int32_t); break;
423 case VLC_CODEC_FL64: DEINTERLEAVE_TYPE(double); break;
424 default: vlc_assert_unreachable();
425 }
426 #undef DEINTERLEAVE_TYPE
427 }
428
429 /*****************************************************************************
430 * aout_ChannelExtract:
431 *****************************************************************************/
ExtractChannel(uint8_t * pi_dst,int i_dst_channels,const uint8_t * pi_src,int i_src_channels,int i_sample_count,const int * pi_selection,int i_bytes)432 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
433 const uint8_t *pi_src, int i_src_channels,
434 int i_sample_count,
435 const int *pi_selection, int i_bytes )
436 {
437 for( int i = 0; i < i_sample_count; i++ )
438 {
439 for( int j = 0; j < i_dst_channels; j++ )
440 memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
441 pi_dst += i_dst_channels * i_bytes;
442 pi_src += i_src_channels * i_bytes;
443 }
444 }
445
aout_ChannelExtract(void * p_dst,int i_dst_channels,const void * p_src,int i_src_channels,int i_sample_count,const int * pi_selection,int i_bits_per_sample)446 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
447 const void *p_src, int i_src_channels,
448 int i_sample_count, const int *pi_selection, int i_bits_per_sample )
449 {
450 /* It does not work in place */
451 assert( p_dst != p_src );
452
453 /* Force the compiler to inline for the specific cases so it can optimize */
454 if( i_bits_per_sample == 8 )
455 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
456 else if( i_bits_per_sample == 16 )
457 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
458 else if( i_bits_per_sample == 32 )
459 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
460 else if( i_bits_per_sample == 64 )
461 ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
462 }
463
aout_CheckChannelExtraction(int * pi_selection,uint32_t * pi_layout,int * pi_channels,const uint32_t pi_order_dst[AOUT_CHAN_MAX],const uint32_t * pi_order_src,int i_channels)464 bool aout_CheckChannelExtraction( int *pi_selection,
465 uint32_t *pi_layout, int *pi_channels,
466 const uint32_t pi_order_dst[AOUT_CHAN_MAX],
467 const uint32_t *pi_order_src, int i_channels )
468 {
469 static_assert(AOUT_CHAN_MAX <= (sizeof (*pi_order_dst) * CHAR_BIT),
470 "Missing bits");
471
472 uint32_t i_layout = 0;
473 int i_out = 0;
474 int pi_index[AOUT_CHAN_MAX];
475
476 /* */
477 if( !pi_order_dst )
478 pi_order_dst = pi_vlc_chan_order_wg4;
479
480 /* */
481 for( int i = 0; i < i_channels; i++ )
482 {
483 /* Ignore unknown or duplicated channels or not present in output */
484 if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
485 continue;
486
487 for( int j = 0; j < AOUT_CHAN_MAX; j++ )
488 {
489 if( pi_order_dst[j] == pi_order_src[i] )
490 {
491 assert( i_out < AOUT_CHAN_MAX );
492 pi_index[i_out++] = i;
493 i_layout |= pi_order_src[i];
494 break;
495 }
496 }
497 }
498
499 /* */
500 for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
501 {
502 for( int k = 0; k < i_out; k++ )
503 {
504 if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
505 {
506 pi_selection[j++] = pi_index[k];
507 break;
508 }
509 }
510 }
511
512 *pi_layout = i_layout;
513 *pi_channels = i_out;
514
515 for( int i = 0; i < i_out; i++ )
516 {
517 if( pi_selection[i] != i )
518 return true;
519 }
520 return i_out != i_channels;
521 }
522
523 /* Return the order in which filters should be inserted */
FilterOrder(const char * psz_name)524 static int FilterOrder( const char *psz_name )
525 {
526 static const struct {
527 const char psz_name[10];
528 int i_order;
529 } filter[] = {
530 { "equalizer", 0 },
531 };
532 for( unsigned i = 0; i < ARRAY_SIZE(filter); i++ )
533 {
534 if( !strcmp( filter[i].psz_name, psz_name ) )
535 return filter[i].i_order;
536 }
537 return INT_MAX;
538 }
539
540 /* This function will add or remove a module from a string list (colon
541 * separated). It will return true if there is a modification
542 * In case p_aout is NULL, we will use configuration instead of variable */
aout_ChangeFilterString(vlc_object_t * p_obj,vlc_object_t * p_aout,const char * psz_variable,const char * psz_name,bool b_add)543 bool aout_ChangeFilterString( vlc_object_t *p_obj, vlc_object_t *p_aout,
544 const char *psz_variable,
545 const char *psz_name, bool b_add )
546 {
547 if( *psz_name == '\0' )
548 return false;
549
550 char *psz_list;
551 if( p_aout )
552 {
553 psz_list = var_GetString( p_aout, psz_variable );
554 }
555 else
556 {
557 psz_list = var_InheritString( p_obj, psz_variable );
558 }
559
560 /* Split the string into an array of filters */
561 int i_count = 1;
562 for( char *p = psz_list; p && *p; p++ )
563 i_count += *p == ':';
564 i_count += b_add;
565
566 const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
567 if( !ppsz_filter )
568 {
569 free( psz_list );
570 return false;
571 }
572 bool b_present = false;
573 i_count = 0;
574 for( char *p = psz_list; p && *p; )
575 {
576 char *psz_end = strchr(p, ':');
577 if( psz_end )
578 *psz_end++ = '\0';
579 else
580 psz_end = p + strlen(p);
581 if( *p )
582 {
583 b_present |= !strcmp( p, psz_name );
584 ppsz_filter[i_count++] = p;
585 }
586 p = psz_end;
587 }
588 if( b_present == b_add )
589 {
590 free( ppsz_filter );
591 free( psz_list );
592 return false;
593 }
594
595 if( b_add )
596 {
597 int i_order = FilterOrder( psz_name );
598 int i;
599 for( i = 0; i < i_count; i++ )
600 {
601 if( FilterOrder( ppsz_filter[i] ) > i_order )
602 break;
603 }
604 if( i < i_count )
605 memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
606 ppsz_filter[i] = psz_name;
607 i_count++;
608 }
609 else
610 {
611 for( int i = 0; i < i_count; i++ )
612 {
613 if( !strcmp( ppsz_filter[i], psz_name ) )
614 ppsz_filter[i] = "";
615 }
616 }
617 size_t i_length = 0;
618 for( int i = 0; i < i_count; i++ )
619 i_length += 1 + strlen( ppsz_filter[i] );
620
621 char *psz_new = malloc( i_length + 1 );
622
623 if( unlikely( !psz_new ) )
624 {
625 free( ppsz_filter );
626 free( psz_list );
627 return false;
628 }
629
630 *psz_new = '\0';
631 for( int i = 0; i < i_count; i++ )
632 {
633 if( *ppsz_filter[i] == '\0' )
634 continue;
635 if( *psz_new )
636 strcat( psz_new, ":" );
637 strcat( psz_new, ppsz_filter[i] );
638 }
639 free( ppsz_filter );
640 free( psz_list );
641
642 var_SetString( p_obj, psz_variable, psz_new );
643 if( p_aout )
644 var_SetString( p_aout, psz_variable, psz_new );
645 free( psz_new );
646
647 return true;
648 }
649