]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/a52tofloat32.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / audio_filter / converter / a52tofloat32.c
1 /*****************************************************************************
2  * a52tofloat32.c: ATSC A/52 aka AC-3 decoder plugin for VLC.
3  *   This plugin makes use of liba52 to decode A/52 audio
4  *   (http://liba52.sf.net/).
5  *****************************************************************************
6  * Copyright (C) 2001-2009 the VideoLAN team
7  * $Id$
8  *
9  * Authors: Gildas Bazin <gbazin@videolan.org>
10  *          Christophe Massiot <massiot@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_cpu.h>
37
38 #include <stdint.h>                                         /* int16_t .. */
39
40 #if !HAVE_FPU
41 # define LIBA52_FIXED
42 #endif
43 #ifdef USE_A52DEC_TREE                                 /* liba52 header file */
44 #   include "include/a52.h"
45 #else
46 #   include "a52dec/a52.h"
47 #endif
48
49 #include <vlc_aout.h>
50 #include <vlc_block.h>
51 #include <vlc_filter.h>
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int  OpenFilter ( vlc_object_t * );
57 static void CloseFilter( vlc_object_t * );
58 static block_t *Convert( filter_t *, block_t * );
59
60 /* liba52 channel order */
61 static const uint32_t pi_channels_in[] =
62 { AOUT_CHAN_LFE, AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
63   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARCENTER, AOUT_CHAN_REARRIGHT, 0 };
64
65 /*****************************************************************************
66  * Local structures
67  *****************************************************************************/
68 struct filter_sys_t
69 {
70     a52_state_t * p_liba52; /* liba52 internal structure */
71     bool b_dynrng; /* see below */
72     int i_flags; /* liba52 flags, see a52dec/doc/liba52.txt */
73     bool b_dontwarn;
74     int i_nb_channels; /* number of float32 per sample */
75
76     int pi_chan_table[AOUT_CHAN_MAX]; /* channel reordering */
77 };
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
83 #define DYNRNG_LONGTEXT N_( \
84     "Dynamic range compression makes the loud sounds softer, and the soft " \
85     "sounds louder, so you can more easily listen to the stream in a noisy " \
86     "environment without disturbing anyone. If you disable the dynamic range "\
87     "compression the playback will be more adapted to a movie theater or a " \
88     "listening room.")
89 #define UPMIX_TEXT N_("Enable internal upmixing")
90 #define UPMIX_LONGTEXT N_( \
91     "Enable the internal upmixing algorithm (not recommended).")
92
93 vlc_module_begin ()
94     set_shortname( "A/52" )
95     set_description( N_("ATSC A/52 (AC-3) audio decoder") )
96     set_category( CAT_INPUT )
97     set_subcategory( SUBCAT_INPUT_ACODEC )
98     add_bool( "a52-dynrng", true, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, false )
99     add_bool( "a52-upmix", false, NULL, UPMIX_TEXT, UPMIX_LONGTEXT, true )
100     set_capability( "audio filter", 100 )
101     set_callbacks( OpenFilter, CloseFilter )
102 vlc_module_end ()
103
104 /*****************************************************************************
105  * Open:
106  *****************************************************************************/
107 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
108                  audio_format_t input, audio_format_t output )
109 {
110     p_sys->b_dynrng = var_InheritBool( p_this, "a52-dynrng" );
111     p_sys->b_dontwarn = 0;
112
113     /* No upmixing: it's not necessary and some other filters may want to do
114      * it themselves. */
115     if ( aout_FormatNbChannels( &output ) > aout_FormatNbChannels( &input ) )
116     {
117         if ( ! var_InheritBool( p_this, "a52-upmix" ) )
118         {
119             return VLC_EGENERIC;
120         }
121     }
122
123     /* We'll do our own downmixing, thanks. */
124     p_sys->i_nb_channels = aout_FormatNbChannels( &output );
125     switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
126               & ~AOUT_CHAN_LFE )
127     {
128     case AOUT_CHAN_CENTER:
129         if ( (output.i_original_channels & AOUT_CHAN_CENTER)
130               || (output.i_original_channels
131                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
132         {
133             p_sys->i_flags = A52_MONO;
134         }
135         else if ( output.i_original_channels & AOUT_CHAN_LEFT )
136         {
137             p_sys->i_flags = A52_CHANNEL1;
138         }
139         else
140         {
141             p_sys->i_flags = A52_CHANNEL2;
142         }
143         break;
144
145     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
146         if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
147         {
148             p_sys->i_flags = A52_DOLBY;
149         }
150         else if ( input.i_original_channels == AOUT_CHAN_CENTER )
151         {
152             p_sys->i_flags = A52_MONO;
153         }
154         else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
155         {
156             p_sys->i_flags = A52_CHANNEL;
157         }
158         else if ( !(output.i_original_channels & AOUT_CHAN_RIGHT) )
159         {
160             p_sys->i_flags = A52_CHANNEL1;
161         }
162         else if ( !(output.i_original_channels & AOUT_CHAN_LEFT) )
163         {
164             p_sys->i_flags = A52_CHANNEL2;
165         }
166         else
167         {
168             p_sys->i_flags = A52_STEREO;
169         }
170         break;
171
172     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
173         p_sys->i_flags = A52_3F;
174         break;
175
176     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
177         p_sys->i_flags = A52_2F1R;
178         break;
179
180     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
181           | AOUT_CHAN_REARCENTER:
182         p_sys->i_flags = A52_3F1R;
183         break;
184
185     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
186           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
187         p_sys->i_flags = A52_2F2R;
188         break;
189
190     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
191           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
192         p_sys->i_flags = A52_3F2R;
193         break;
194
195     default:
196         msg_Warn( p_this, "unknown sample format!" );
197         free( p_sys );
198         return VLC_EGENERIC;
199     }
200     if ( output.i_physical_channels & AOUT_CHAN_LFE )
201     {
202         p_sys->i_flags |= A52_LFE;
203     }
204     p_sys->i_flags |= A52_ADJUST_LEVEL;
205
206     /* Initialize liba52 */
207     p_sys->p_liba52 = a52_init( 0 );
208     if( p_sys->p_liba52 == NULL )
209     {
210         msg_Err( p_this, "unable to initialize liba52" );
211         free( p_sys );
212         return VLC_EGENERIC;
213     }
214
215     aout_CheckChannelReorder( pi_channels_in, NULL,
216                               output.i_physical_channels & AOUT_CHAN_PHYSMASK,
217                               p_sys->i_nb_channels,
218                               p_sys->pi_chan_table );
219
220     return VLC_SUCCESS;
221 }
222
223 /*****************************************************************************
224  * Interleave: helper function to interleave channels
225  *****************************************************************************/
226 static void Interleave( sample_t * p_out, const sample_t * p_in,
227                         int i_nb_channels, int *pi_chan_table )
228 {
229     /* We do not only have to interleave, but also reorder the channels */
230
231     int i, j;
232     for ( j = 0; j < i_nb_channels; j++ )
233     {
234         for ( i = 0; i < 256; i++ )
235         {
236             p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
237         }
238     }
239 }
240
241 /*****************************************************************************
242  * Duplicate: helper function to duplicate a unique channel
243  *****************************************************************************/
244 static void Duplicate( sample_t * p_out, const sample_t * p_in )
245 {
246     int i;
247
248     for ( i = 256; i--; )
249     {
250         *p_out++ = *p_in;
251         *p_out++ = *p_in;
252         p_in++;
253     }
254 }
255
256 /*****************************************************************************
257  * Exchange: helper function to exchange left & right channels
258  *****************************************************************************/
259 static void Exchange( sample_t * p_out, const sample_t * p_in )
260 {
261     int i;
262     const sample_t * p_first = p_in + 256;
263     const sample_t * p_second = p_in;
264
265     for ( i = 0; i < 256; i++ )
266     {
267         *p_out++ = *p_first++;
268         *p_out++ = *p_second++;
269     }
270 }
271
272 /*****************************************************************************
273  * DoWork: decode an ATSC A/52 frame.
274  *****************************************************************************/
275 static void DoWork( filter_t * p_filter,
276                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
277 {
278     filter_sys_t    *p_sys = p_filter->p_sys;
279 #ifdef LIBA52_FIXED
280     sample_t        i_sample_level = (1 << 24);
281 #else
282     sample_t        i_sample_level = 1;
283 #endif
284     int             i_flags = p_sys->i_flags;
285     int             i_bytes_per_block = 256 * p_sys->i_nb_channels
286                       * sizeof(sample_t);
287     int             i;
288
289     /* Do the actual decoding now. */
290     a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
291                &i_flags, &i_sample_level, 0 );
292
293     if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
294           && !p_sys->b_dontwarn )
295     {
296         msg_Warn( p_filter,
297                   "liba52 couldn't do the requested downmix 0x%x->0x%x",
298                   p_sys->i_flags  & A52_CHANNEL_MASK,
299                   i_flags & A52_CHANNEL_MASK );
300
301         p_sys->b_dontwarn = 1;
302     }
303
304     if( !p_sys->b_dynrng )
305     {
306         a52_dynrng( p_sys->p_liba52, NULL, NULL );
307     }
308
309     for ( i = 0; i < 6; i++ )
310     {
311         sample_t * p_samples;
312
313         if( a52_block( p_sys->p_liba52 ) )
314         {
315             msg_Warn( p_filter, "a52_block failed for block %d", i );
316         }
317
318         p_samples = a52_samples( p_sys->p_liba52 );
319
320         if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
321                || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
322                || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
323               && (p_filter->fmt_out.audio.i_physical_channels
324                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
325         {
326             Duplicate( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
327                        p_samples );
328         }
329         else if ( p_filter->fmt_out.audio.i_original_channels
330                     & AOUT_CHAN_REVERSESTEREO )
331         {
332             Exchange( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
333                       p_samples );
334         }
335         else
336         {
337             /* Interleave the *$£%ù samples. */
338             Interleave( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
339                         p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
340         }
341     }
342
343     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
344     p_out_buf->i_buffer = i_bytes_per_block * 6;
345 }
346
347 /*****************************************************************************
348  * OpenFilter:
349  *****************************************************************************/
350 static int OpenFilter( vlc_object_t *p_this )
351 {
352     filter_t *p_filter = (filter_t *)p_this;
353     filter_sys_t *p_sys;
354     int i_ret;
355
356     if( p_filter->fmt_in.i_codec != VLC_CODEC_A52 ||
357         p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB ||
358         p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFL )
359     {
360         return VLC_EGENERIC;
361     }
362
363     p_filter->fmt_out.audio.i_format =
364 #ifdef LIBA52_FIXED
365         p_filter->fmt_out.i_codec = VLC_CODEC_FI32;
366 #else
367         p_filter->fmt_out.i_codec = VLC_CODEC_FL32;
368 #endif
369     p_filter->fmt_out.audio.i_bitspersample =
370         aout_BitsPerSample( p_filter->fmt_out.i_codec );
371
372     /* Allocate the memory needed to store the module's structure */
373     p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
374     if( p_sys == NULL )
375         return VLC_ENOMEM;
376
377     i_ret = Open( VLC_OBJECT(p_filter), p_sys,
378                   p_filter->fmt_in.audio, p_filter->fmt_out.audio );
379
380     p_filter->pf_audio_filter = Convert;
381     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
382
383     return i_ret;
384 }
385
386 /*****************************************************************************
387  * CloseFilter : deallocate data structures
388  *****************************************************************************/
389 static void CloseFilter( vlc_object_t *p_this )
390 {
391     filter_t *p_filter = (filter_t *)p_this;
392     filter_sys_t *p_sys = p_filter->p_sys;
393
394     a52_free( p_sys->p_liba52 );
395     free( p_sys );
396 }
397
398 static block_t *Convert( filter_t *p_filter, block_t *p_block )
399 {
400     if( !p_block || !p_block->i_nb_samples )
401     {
402         if( p_block )
403             block_Release( p_block );
404         return NULL;
405     }
406
407     size_t i_out_size = p_block->i_nb_samples *
408       p_filter->fmt_out.audio.i_bitspersample *
409         p_filter->fmt_out.audio.i_channels / 8;
410
411     block_t *p_out = filter_NewAudioBuffer( p_filter, i_out_size );
412     if( !p_out )
413     {
414         msg_Warn( p_filter, "can't get output buffer" );
415         block_Release( p_block );
416         return NULL;
417     }
418
419     p_out->i_nb_samples = p_block->i_nb_samples;
420     p_out->i_dts = p_block->i_dts;
421     p_out->i_pts = p_block->i_pts;
422     p_out->i_length = p_block->i_length;
423
424     DoWork( p_filter, p_block, p_out );
425
426     block_Release( p_block );
427
428     return p_out;
429 }