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