]> git.sesse.net Git - vlc/blob - src/audio_output/common.c
aout: remove FI32 intermediate-only codec
[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_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  *****************************************************************************/
88 void aout_FormatPrepare( audio_sample_format_t * p_format )
89 {
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 )
93     {
94         p_format->i_bytes_per_frame = ( p_format->i_bitspersample / 8 )
95                                     * aout_FormatNbChannels( p_format );
96         p_format->i_frame_length = 1;
97     }
98 }
99
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 )
104 {
105     switch ( p_format->i_physical_channels )
106     {
107     case AOUT_CHAN_LEFT:
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)) )
113             return "Mono";
114         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
115             return "Left";
116         return "Right";
117     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
118         if ( p_format->i_original_channels & AOUT_CHAN_REVERSESTEREO )
119         {
120             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
121                 return "Dolby/Reverse";
122             return "Stereo/Reverse";
123         }
124         else
125         {
126             if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
127                 return "Dolby";
128             else if ( p_format->i_original_channels & AOUT_CHAN_DUALMONO )
129                 return "Dual-mono";
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";
136             return "Stereo";
137         }
138     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
139         return "3F";
140     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
141         return "2F1R";
142     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
143           | AOUT_CHAN_REARCENTER:
144         return "3F1R";
145     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
146           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
147         return "2F2R";
148     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
149           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
150         return "2F2M";
151     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
152           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
153         return "3F2R";
154     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
155           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT:
156         return "3F2M";
157
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)) )
162             return "Mono/LFE";
163         else if ( p_format->i_original_channels & AOUT_CHAN_LEFT )
164             return "Left/LFE";
165         return "Right/LFE";
166     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE:
167         if ( p_format->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
168             return "Dolby/LFE";
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 )
172             return "Mono/LFE";
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";
177          return "Stereo/LFE";
178     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE:
179         return "3F/LFE";
180     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER
181           | AOUT_CHAN_LFE:
182         return "2F1R/LFE";
183     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
184           | AOUT_CHAN_REARCENTER | AOUT_CHAN_LFE:
185         return "3F1R/LFE";
186     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
187           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
188         return "2F2R/LFE";
189     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
190           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
191         return "2F2M/LFE";
192     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
193           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE:
194         return "3F2R/LFE";
195     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
196           | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE:
197         return "3F2M/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:
201         return "3F2M2R";
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:
205         return "3F2M2R/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:
209         return "3F2M1R/LFE";
210     }
211
212     return "ERROR";
213 }
214
215 #undef aout_FormatPrint
216 /**
217  * Prints an audio sample format in a human-readable form.
218  */
219 void aout_FormatPrint( vlc_object_t *obj, const char *psz_text,
220                        const audio_sample_format_t *p_format )
221 {
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 );
226 }
227
228 #undef aout_FormatsPrint
229 /**
230  * Prints two formats in a human-readable form
231  */
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 )
235 {
236     msg_Dbg( obj, "%s '%4.4s'->'%4.4s' %d Hz->%d Hz %s->%s",
237              psz_text,
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 ) );
242 }
243
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 )
250 {
251     unsigned channels = 0;
252
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;
257
258     for( unsigned i = 0; chans_in[i]; i++ )
259     {
260         const uint32_t chan = chans_in[i];
261         if( !(mask & chan) )
262             continue;
263
264         unsigned index = 0;
265         for( unsigned j = 0; chan != chans_out[j]; j++ )
266             if( mask & chans_out[j] )
267                 index++;
268
269         table[channels++] = index;
270     }
271
272     for( unsigned i = 0; i < channels; i++ )
273         if( table[i] != i )
274             return channels;
275     return 0;
276 }
277
278 /**
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 of the
282  *              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.
288  */
289 void aout_ChannelReorder( void *ptr, size_t bytes, unsigned channels,
290                           const uint8_t *restrict chans_table, vlc_fourcc_t fourcc )
291 {
292     assert( channels != 0 );
293     assert( channels <= AOUT_CHAN_MAX );
294
295     /* The audio formats supported in audio output are inlined. For other formats (used in
296      * demuxers and muxers), memcpy() is used to avoid breaking type punning. */
297     switch( fourcc )
298     {
299         case VLC_CODEC_FL32:
300         {
301             const size_t frames = (bytes / 4) / channels;
302             float *buf = ptr;
303
304             for( size_t i = 0; i < frames; i++ )
305             {
306                 float tmp[AOUT_CHAN_MAX];
307
308                 for( size_t j = 0; j < channels; j++ )
309                     tmp[chans_table[j]] = buf[j];
310                 memcpy( buf, tmp, 4 * channels );
311                 buf += channels;
312             }
313             break;
314         }
315
316         case VLC_CODEC_S16N:
317         {
318             const size_t frames = (bytes / 2) / channels;
319             int16_t *buf = ptr;
320
321             for( size_t i = 0; i < frames; i++ )
322             {
323                 int16_t tmp[AOUT_CHAN_MAX];
324
325                 for( size_t j = 0; j < channels; j++ )
326                     tmp[chans_table[j]] = buf[j];
327                 memcpy( buf, tmp, 2 * channels );
328                 buf += channels;
329             }
330             break;
331         }
332
333         case VLC_CODEC_FL64:
334         {
335             const size_t frames = (bytes / 8) / channels;
336             double *buf = ptr;
337
338             for( size_t i = 0; i < frames; i++ )
339             {
340                 double tmp[AOUT_CHAN_MAX];
341
342                 for( size_t j = 0; j < channels; j++ )
343                     tmp[chans_table[j]] = buf[j];
344                 memcpy( buf, tmp, 8 * channels );
345                 buf += channels;
346             }
347             break;
348         }
349
350         case VLC_CODEC_S32N:
351         {
352             const size_t frames = (bytes / 4) / channels;
353             int32_t *buf = ptr;
354
355             for( size_t i = 0; i < frames; i++ )
356             {
357                 int32_t tmp[AOUT_CHAN_MAX];
358
359                 for( size_t j = 0; j < channels; j++ )
360                     tmp[chans_table[j]] = buf[j];
361                 memcpy( buf, tmp, 4 * channels );
362                 buf += channels;
363             }
364             break;
365         }
366
367         case VLC_CODEC_U8:
368         {
369             const size_t frames = bytes / channels;
370             uint8_t *buf = ptr;
371
372             for( size_t i = 0; i < frames; i++ )
373             {
374                 uint8_t tmp[AOUT_CHAN_MAX];
375
376                 for( size_t j = 0; j < channels; j++ )
377                     tmp[chans_table[j]] = buf[j];
378                 memcpy( buf, tmp, channels );
379                 buf += channels;
380             }
381             break;
382         }
383
384         default:
385         {
386             unsigned size = aout_BitsPerSample( fourcc ) / 8;
387             const size_t frames = bytes / size;
388             unsigned char *buf = ptr;
389
390             assert( bytes != 0 );
391             for( size_t i = 0; i < frames; i++ )
392             {
393                 unsigned char tmp[AOUT_CHAN_MAX * bytes];
394
395                 for( size_t j = 0; j < channels; j++ )
396                     memcpy( tmp + size * chans_table[j], buf + size * j, size );
397                 memcpy( buf, tmp, size * channels );
398                 buf += size * channels;
399             }
400             break;
401         }
402     }
403 }
404
405 /*****************************************************************************
406  * aout_ChannelExtract:
407  *****************************************************************************/
408 static inline void ExtractChannel( uint8_t *pi_dst, int i_dst_channels,
409                                    const uint8_t *pi_src, int i_src_channels,
410                                    int i_sample_count,
411                                    const int *pi_selection, int i_bytes )
412 {
413     for( int i = 0; i < i_sample_count; i++ )
414     {
415         for( int j = 0; j < i_dst_channels; j++ )
416             memcpy( &pi_dst[j * i_bytes], &pi_src[pi_selection[j] * i_bytes], i_bytes );
417         pi_dst += i_dst_channels * i_bytes;
418         pi_src += i_src_channels * i_bytes;
419     }
420 }
421
422 void aout_ChannelExtract( void *p_dst, int i_dst_channels,
423                           const void *p_src, int i_src_channels,
424                           int i_sample_count, const int *pi_selection, int i_bits_per_sample )
425 {
426     /* It does not work in place */
427     assert( p_dst != p_src );
428
429     /* Force the compiler to inline for the specific cases so it can optimize */
430     if( i_bits_per_sample == 8 )
431         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 1 );
432     else  if( i_bits_per_sample == 16 )
433         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 2 );
434     else  if( i_bits_per_sample == 32 )
435         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 4 );
436     else  if( i_bits_per_sample == 64 )
437         ExtractChannel( p_dst, i_dst_channels, p_src, i_src_channels, i_sample_count, pi_selection, 8 );
438 }
439
440 bool aout_CheckChannelExtraction( int *pi_selection,
441                                   uint32_t *pi_layout, int *pi_channels,
442                                   const uint32_t pi_order_dst[AOUT_CHAN_MAX],
443                                   const uint32_t *pi_order_src, int i_channels )
444 {
445     const uint32_t pi_order_dual_mono[] = { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT };
446     uint32_t i_layout = 0;
447     int i_out = 0;
448     int pi_index[AOUT_CHAN_MAX];
449
450     /* */
451     if( !pi_order_dst )
452         pi_order_dst = pi_vlc_chan_order_wg4;
453
454     /* Detect special dual mono case */
455     if( i_channels == 2 &&
456         pi_order_src[0] == AOUT_CHAN_CENTER && pi_order_src[1] == AOUT_CHAN_CENTER )
457     {
458         i_layout |= AOUT_CHAN_DUALMONO;
459         pi_order_src = pi_order_dual_mono;
460     }
461
462     /* */
463     for( int i = 0; i < i_channels; i++ )
464     {
465         /* Ignore unknown or duplicated channels or not present in output */
466         if( !pi_order_src[i] || (i_layout & pi_order_src[i]) )
467             continue;
468
469         for( int j = 0; j < AOUT_CHAN_MAX; j++ )
470         {
471             if( pi_order_dst[j] == pi_order_src[i] )
472             {
473                 assert( i_out < AOUT_CHAN_MAX );
474                 pi_index[i_out++] = i;
475                 i_layout |= pi_order_src[i];
476                 break;
477             }
478         }
479     }
480
481     /* */
482     for( int i = 0, j = 0; i < AOUT_CHAN_MAX; i++ )
483     {
484         for( int k = 0; k < i_out; k++ )
485         {
486             if( pi_order_dst[i] == pi_order_src[pi_index[k]] )
487             {
488                 pi_selection[j++] = pi_index[k];
489                 break;
490             }
491         }
492     }
493
494     *pi_layout = i_layout;
495     *pi_channels = i_out;
496
497     for( int i = 0; i < i_out; i++ )
498     {
499         if( pi_selection[i] != i )
500             return true;
501     }
502     return i_out == i_channels;
503 }
504
505 /* Return the order in which filters should be inserted */
506 static int FilterOrder( const char *psz_name )
507 {
508     static const struct {
509         const char psz_name[10];
510         int        i_order;
511     } filter[] = {
512         { "equalizer",  0 },
513     };
514     for( unsigned i = 0; i < ARRAY_SIZE(filter); i++ )
515     {
516         if( !strcmp( filter[i].psz_name, psz_name ) )
517             return filter[i].i_order;
518     }
519     return INT_MAX;
520 }
521
522 /* This function will add or remove a a module from a string list (colon
523  * separated). It will return true if there is a modification
524  * In case p_aout is NULL, we will use configuration instead of variable */
525 bool aout_ChangeFilterString( vlc_object_t *p_obj, vlc_object_t *p_aout,
526                               const char *psz_variable,
527                               const char *psz_name, bool b_add )
528 {
529     if( *psz_name == '\0' )
530         return false;
531
532     char *psz_list;
533     if( p_aout )
534     {
535         psz_list = var_GetString( p_aout, psz_variable );
536     }
537     else
538     {
539         psz_list = var_CreateGetString( p_obj->p_libvlc, psz_variable );
540         var_Destroy( p_obj->p_libvlc, psz_variable );
541     }
542
543     /* Split the string into an array of filters */
544     int i_count = 1;
545     for( char *p = psz_list; p && *p; p++ )
546         i_count += *p == ':';
547     i_count += b_add;
548
549     const char **ppsz_filter = calloc( i_count, sizeof(*ppsz_filter) );
550     if( !ppsz_filter )
551     {
552         free( psz_list );
553         return false;
554     }
555     bool b_present = false;
556     i_count = 0;
557     for( char *p = psz_list; p && *p; )
558     {
559         char *psz_end = strchr(p, ':');
560         if( psz_end )
561             *psz_end++ = '\0';
562         else
563             psz_end = p + strlen(p);
564         if( *p )
565         {
566             b_present |= !strcmp( p, psz_name );
567             ppsz_filter[i_count++] = p;
568         }
569         p = psz_end;
570     }
571     if( b_present == b_add )
572     {
573         free( ppsz_filter );
574         free( psz_list );
575         return false;
576     }
577
578     if( b_add )
579     {
580         int i_order = FilterOrder( psz_name );
581         int i;
582         for( i = 0; i < i_count; i++ )
583         {
584             if( FilterOrder( ppsz_filter[i] ) > i_order )
585                 break;
586         }
587         if( i < i_count )
588             memmove( &ppsz_filter[i+1], &ppsz_filter[i], (i_count - i) * sizeof(*ppsz_filter) );
589         ppsz_filter[i] = psz_name;
590         i_count++;
591     }
592     else
593     {
594         for( int i = 0; i < i_count; i++ )
595         {
596             if( !strcmp( ppsz_filter[i], psz_name ) )
597                 ppsz_filter[i] = "";
598         }
599     }
600     size_t i_length = 0;
601     for( int i = 0; i < i_count; i++ )
602         i_length += 1 + strlen( ppsz_filter[i] );
603
604     char *psz_new = malloc( i_length + 1 );
605     *psz_new = '\0';
606     for( int i = 0; i < i_count; i++ )
607     {
608         if( *ppsz_filter[i] == '\0' )
609             continue;
610         if( *psz_new )
611             strcat( psz_new, ":" );
612         strcat( psz_new, ppsz_filter[i] );
613     }
614     free( ppsz_filter );
615     free( psz_list );
616
617     if( p_aout )
618         var_SetString( p_aout, psz_variable, psz_new );
619     else
620         config_PutPsz( p_obj, psz_variable, psz_new );
621     free( psz_new );
622
623     return true;
624 }