]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
b11e08631ca6555934402421d09cd12180a0c8db
[vlc] / src / audio_output / common.c
1 /*****************************************************************************
2  * common.c : audio output management of common data structures
3  *****************************************************************************
4  * Copyright (C) 2002-2007 VLC authors and VideoLAN
5  * $Id$
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  *****************************************************************************/
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_S32L:
65     case VLC_CODEC_S32B:
66     case VLC_CODEC_F32L:
67     case VLC_CODEC_F32B:
68     case VLC_CODEC_FI32:
69         return 32;
70
71     case VLC_CODEC_F64L:
72     case VLC_CODEC_F64B:
73         return 64;
74
75     default:
76         /* For these formats the caller has to indicate the parameters
77          * by hand. */
78         return 0;
79     }
80 }
81
82 /*****************************************************************************
83  * aout_FormatPrepare : compute the number of bytes per frame & frame length
84  *****************************************************************************/
85 void aout_FormatPrepare( audio_sample_format_t * p_format )
86 {
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 )
90     {
91         p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
92                                     * aout_FormatNbChannels( p_format );
93         p_format->i_frame_length = 1;
94     }
95 }
96
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 )
101 {
102     switch ( p_format->i_physical_channels )
103     {
104     case AOUT_CHAN_LEFT:
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)) )
110             return "Mono";
111         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
112             return "Left";
113         return "Right";
114     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
115         if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
116         {
117             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
118                 return "Dolby/Reverse";
119             return "Stereo/Reverse";
120         }
121         else
122         {
123             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
124                 return "Dolby";
125             else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
126                 return "Dual-mono";
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";
133             return "Stereo";
134         }
135     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
136         return "3F";
137     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
138         return "2F1R";
139     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
140           | AOUT_CHAN_REARCENTER:
141         return "3F1R";
142     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
143           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
144         return "2F2R";
145     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
146           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
147         return "2F2M";
148     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
149           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
150         return "3F2R";
151     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
152           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
153         return "3F2M";
154
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)) )
159             return "Mono/LFE";
160         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
161             return "Left/LFE";
162         return "Right/LFE";
163     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
164         if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
165             return "Dolby/LFE";
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 )
169             return "Mono/LFE";
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";
174          return "Stereo/LFE";
175     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
176         return "3F/LFE";
177     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
178           | AOUT_CHAN_LFE:
179         return "2F1R/LFE";
180     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
181           | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
182         return "3F1R/LFE";
183     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
184           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
185         return "2F2R/LFE";
186     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
187           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
188         return "2F2M/LFE";
189     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
190           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
191         return "3F2R/LFE";
192     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
193           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
194         return "3F2M/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:
198         return "3F2M2R";
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:
202         return "3F2M2R/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:
206         return "3F2M1R/LFE";
207     }
208
209     return "ERROR";
210 }
211
212 #undef aout_FormatPrint
213 /**
214  * Prints an audio sample format in a human-readable form.
215  */
216 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
217                        const audio_sample_format_t *p_format )
218 {
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 );
223 }
224
225 #undef aout_FormatsPrint
226 /**
227  * Prints two formats in a human-readable form
228  */
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 )
232 {
233     msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
234              psz_text,
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 ) );
239 }
240
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 )
248 {
249     bool b_chan_reorder = false;
250     int i, j, k, l;
251
252     if( i_channels > AOUT_CHAN_MAX )
253         return false;
254
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;
259
260     for( i = 0, j = 0; pi_chan_order_in[i]; i++ )
261     {
262         if( !(i_channel_mask & pi_chan_order_in[i]) ) continue;
263
264         for( k = 0, l = 0; pi_chan_order_in[i] != pi_chan_order_out[k]; k++ )
265         {
266             if( i_channel_mask & pi_chan_order_out[k] ) l++;
267         }
268
269         pi_chan_table[j++] = l;
270     }
271
272     for( i = 0; i < i_channels; i++ )
273     {
274         if( pi_chan_table[i] != i ) b_chan_reorder = true;
275     }
276
277     return b_chan_reorder;
278 }
279
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 )
286 {
287     uint8_t p_tmp[AOUT_CHAN_MAX * 4];
288     int i, j;
289
290     if( i_bits_per_sample == 8 )
291     {
292         for( i = 0; i < i_buffer / i_channels; i++ )
293         {
294             for( j = 0; j < i_channels; j++ )
295             {
296                 p_tmp[pi_chan_table[j]] = p_buf[j];
297             }
298
299             memcpy( p_buf, p_tmp, i_channels );
300             p_buf += i_channels;
301         }
302     }
303     else if( i_bits_per_sample == 16 )
304     {
305         for( i = 0; i < i_buffer / i_channels / 2; i++ )
306         {
307             for( j = 0; j < i_channels; j++ )
308             {
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];
311             }
312
313             memcpy( p_buf, p_tmp, 2 * i_channels );
314             p_buf += 2 * i_channels;
315         }
316     }
317     else if( i_bits_per_sample == 24 )
318     {
319         for( i = 0; i < i_buffer / i_channels / 3; i++ )
320         {
321             for( j = 0; j < i_channels; j++ )
322             {
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];
326             }
327
328             memcpy( p_buf, p_tmp, 3 * i_channels );
329             p_buf += 3 * i_channels;
330         }
331     }
332     else if( i_bits_per_sample == 32 )
333     {
334         for( i = 0; i < i_buffer / i_channels / 4; i++ )
335         {
336             for( j = 0; j < i_channels; j++ )
337             {
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];
342             }
343
344             memcpy( p_buf, p_tmp, 4 * i_channels );
345             p_buf += 4 * i_channels;
346         }
347     }
348 }
349
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,
355                                    int i_sample_count,
356                                    const int *pi_selection, int i_bytes )
357 {
358     for( int i = 0; i < i_sample_count; i++ )
359     {
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;
364     }
365 }
366
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 )
370 {
371     /* It does not work in place */
372     assert( p_dst != p_src );
373
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 );
385 }
386
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 )
391 {
392     const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
393     uint32_t i_layout = 0;
394     int i_out = 0;
395     int pi_index[AOUT_CHAN_MAX];
396
397     /* */
398     if( !pi_order_dst )
399         pi_order_dst = pi_vlc_chan_order_wg4;
400
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 )
404     {
405         i_layout |= AOUT_CHAN_DUALMONO;
406         pi_order_src = pi_order_dual_mono;
407     }
408
409     /* */
410     for( int i = 0; i < i_channels; i++ )
411     {
412         /* Ignore unknown or duplicated channels or not present in output */
413         if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
414             continue;
415
416         for( int j = 0; j < AOUT_CHAN_MAX; j++ )
417         {
418             if( pi_order_dst[j] == pi_order_src[i] )
419             {
420                 assert( i_out < AOUT_CHAN_MAX );
421                 pi_index[i_out++] = i;
422                 i_layout |= pi_order_src[i];
423                 break;
424             }
425         }
426     }
427
428     /* */
429     for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
430     {
431         for( int k = 0; k < i_out; k++ )
432         {
433             if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
434             {
435                 pi_selection[j++] = pi_index[k];
436                 break;
437             }
438         }
439     }
440
441     *pi_layout = i_layout;
442     *pi_channels = i_out;
443
444     for( int i = 0; i < i_out; i++ )
445     {
446         if( pi_selection[i] != i )
447             return true;
448     }
449     return i_out == i_channels;
450 }
451
452 /* Return the order in which filters should be inserted */
453 static int FilterOrder( const char *psz_name )
454 {
455     static const struct {
456         const char psz_name[10];
457         int        i_order;
458     } filter[] = {
459         { "equalizer",  0 },
460     };
461     for( unsigned i = 0; i < ARRAY_SIZE(filter); i++ )
462     {
463         if( !strcmp( filter[i].psz_name, psz_name ) )
464             return filter[i].i_order;
465     }
466     return INT_MAX;
467 }
468
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 )
475 {
476     if( *psz_name == '\0' )
477         return false;
478
479     char *psz_list;
480     if( p_aout )
481     {
482         psz_list = var_GetString( p_aout, psz_variable );
483     }
484     else
485     {
486         psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
487         var_Destroy( p_obj->p_libvlc, psz_variable );
488     }
489
490     /* Split the string into an array of filters */
491     int i_count = 1;
492     for( char *p = psz_list; p && *p; p++ )
493         i_count += *p == ':';
494     i_count += b_add;
495
496     const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
497     if( !ppsz_filter )
498     {
499         free( psz_list );
500         return false;
501     }
502     bool b_present = false;
503     i_count = 0;
504     for( char *p = psz_list; p && *p; )
505     {
506         char *psz_end = strchr(p, ':');
507         if( psz_end )
508             *psz_end++ = '\0';
509         else
510             psz_end = p + strlen(p);
511         if( *p )
512         {
513             b_present |= !strcmp( p, psz_name );
514             ppsz_filter[i_count++] = p;
515         }
516         p = psz_end;
517     }
518     if( b_present == b_add )
519     {
520         free( ppsz_filter );
521         free( psz_list );
522         return false;
523     }
524
525     if( b_add )
526     {
527         int i_order = FilterOrder( psz_name );
528         int i;
529         for( i = 0; i < i_count; i++ )
530         {
531             if( FilterOrder( ppsz_filter[i] ) > i_order )
532                 break;
533         }
534         if( i < i_count )
535             memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
536         ppsz_filter[i] = psz_name;
537         i_count++;
538     }
539     else
540     {
541         for( int i = 0; i < i_count; i++ )
542         {
543             if( !strcmp( ppsz_filter[i], psz_name ) )
544                 ppsz_filter[i] = "";
545         }
546     }
547     size_t i_length = 0;
548     for( int i = 0; i < i_count; i++ )
549         i_length += 1 + strlen( ppsz_filter[i] );
550
551     char *psz_new = malloc( i_length + 1 );
552     *psz_new = '\0';
553     for( int i = 0; i < i_count; i++ )
554     {
555         if( *ppsz_filter[i] == '\0' )
556             continue;
557         if( *psz_new )
558             strcat( psz_new, ":" );
559         strcat( psz_new, ppsz_filter[i] );
560     }
561     free( ppsz_filter );
562     free( psz_list );
563
564     if( p_aout )
565         var_SetString( p_aout, psz_variable, psz_new );
566     else
567         config_PutPsz( p_obj, psz_variable, psz_new );
568     free( psz_new );
569
570     return true;
571 }