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