]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/a52tofloat32.c
* Stringreview
[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, 2002 VideoLAN
7  * $Id: a52tofloat32.c,v 1.16 2004/01/25 13:37:11 kuehne Exp $
8  *
9  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <vlc/vlc.h>
31
32 #include <stdlib.h>                                      /* malloc(), free() */
33 #include <string.h>                                              /* strdup() */
34 #ifdef HAVE_STDINT_H
35 #   include <stdint.h>                                         /* int16_t .. */
36 #elif HAVE_INTTYPES_H
37 #   include <inttypes.h>                                       /* int16_t .. */
38 #endif
39
40 #ifdef HAVE_UNISTD_H
41 #   include <unistd.h>
42 #endif
43
44 #ifdef USE_A52DEC_TREE                                 /* liba52 header file */
45 #   include "include/a52.h"
46 #else
47 #   include "a52dec/a52.h"
48 #endif
49
50 #include "audio_output.h"
51 #include "aout_internal.h"
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int  Create    ( vlc_object_t * );
57 static void Destroy   ( vlc_object_t * );
58 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,  
59                         aout_buffer_t * );
60
61 /*****************************************************************************
62  * Local structures
63  *****************************************************************************/
64 struct aout_filter_sys_t
65 {
66     a52_state_t * p_liba52; /* liba52 internal structure */
67     vlc_bool_t b_dynrng; /* see below */
68     int i_flags; /* liba52 flags, see a52dec/doc/liba52.txt */
69     vlc_bool_t b_dontwarn;
70     int i_nb_channels; /* number of float32 per sample */
71 };
72
73 /*****************************************************************************
74  * Module descriptor
75  *****************************************************************************/
76 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
77 #define DYNRNG_LONGTEXT N_( \
78     "Dynamic range compression makes the loud sounds softer, and the soft " \
79     "sounds louder, so you can more easily listen to the stream in a noisy " \
80     "environment without disturbing anyone. If you disable the dynamic range "\
81     "compression the playback will be more adapted to a movie theater or a " \
82     "listening room.")
83
84 vlc_module_begin();
85     add_category_hint( N_("Miscellaneous"), NULL, VLC_FALSE );
86     add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
87     set_description( _("ATSC A/52 aka AC-3 audio decoder") );
88     set_capability( "audio filter", 100 );
89     set_callbacks( Create, Destroy );
90 vlc_module_end();
91
92 /*****************************************************************************
93  * Create: 
94  *****************************************************************************/
95 static int Create( vlc_object_t * _p_filter )
96 {
97     aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
98     struct aout_filter_sys_t * p_sys;
99
100     if ( p_filter->input.i_format != VLC_FOURCC('a','5','2',' ')
101           || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
102     {
103         return -1;
104     }
105
106     if ( p_filter->input.i_rate != p_filter->output.i_rate )
107     {
108         return -1;
109     }
110
111     /* Allocate the memory needed to store the module's structure */
112     p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
113     if( p_sys == NULL )
114     {
115         msg_Err( p_filter, "out of memory" );
116         return -1;
117     }
118
119     p_sys->b_dynrng = config_GetInt( p_filter, "a52-dynrng" );
120     p_sys->b_dontwarn = 0;
121
122     /* We'll do our own downmixing, thanks. */
123     p_sys->i_nb_channels = aout_FormatNbChannels( &p_filter->output );
124     switch ( (p_filter->output.i_physical_channels & AOUT_CHAN_PHYSMASK)
125               & ~AOUT_CHAN_LFE )
126     {
127     case AOUT_CHAN_CENTER:
128         if ( (p_filter->output.i_original_channels & AOUT_CHAN_CENTER)
129               || (p_filter->output.i_original_channels
130                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
131         {
132             p_sys->i_flags = A52_MONO;
133         }
134         else if ( p_filter->output.i_original_channels & AOUT_CHAN_LEFT )
135         {
136             p_sys->i_flags = A52_CHANNEL1;
137         }
138         else
139         {
140             p_sys->i_flags = A52_CHANNEL2;
141         }
142         break;
143
144     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
145         if ( p_filter->output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
146         {
147             p_sys->i_flags = A52_DOLBY;
148         }
149         else if ( p_filter->input.i_original_channels == AOUT_CHAN_CENTER )
150         {
151             p_sys->i_flags = A52_MONO;
152         }
153         else if ( p_filter->input.i_original_channels & AOUT_CHAN_DUALMONO )
154         {
155             p_sys->i_flags = A52_CHANNEL;
156         }
157         else if ( !(p_filter->output.i_original_channels & AOUT_CHAN_RIGHT) )
158         {
159             p_sys->i_flags = A52_CHANNEL1;
160         }
161         else if ( !(p_filter->output.i_original_channels & AOUT_CHAN_LEFT) )
162         {
163             p_sys->i_flags = A52_CHANNEL2;
164         }
165         else
166         {
167             p_sys->i_flags = A52_STEREO;
168         }
169         break;
170
171     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
172         p_sys->i_flags = A52_3F;
173         break;
174
175     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
176         p_sys->i_flags = A52_2F1R;
177         break;
178
179     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
180           | AOUT_CHAN_REARCENTER:
181         p_sys->i_flags = A52_3F1R;
182         break;
183
184     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
185           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
186         p_sys->i_flags = A52_2F2R;
187         break;
188
189     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
190           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
191         p_sys->i_flags = A52_3F2R;
192         break;
193
194     default:
195         msg_Warn( p_filter, "unknown sample format!" );
196         free( p_sys );
197         return -1;
198     }
199     if ( p_filter->output.i_physical_channels & AOUT_CHAN_LFE )
200     {
201         p_sys->i_flags |= A52_LFE;
202     }
203     p_sys->i_flags |= A52_ADJUST_LEVEL;
204
205     /* Initialize liba52 */
206     p_sys->p_liba52 = a52_init( 0 );
207     if( p_sys->p_liba52 == NULL )
208     {
209         msg_Err( p_filter, "unable to initialize liba52" );
210         return -1;
211     }
212
213     p_filter->pf_do_work = DoWork;
214     p_filter->b_in_place = 0;
215
216     return 0;
217 }
218
219 /*****************************************************************************
220  * Interleave: helper function to interleave channels
221  *****************************************************************************/
222 static void Interleave( float * p_out, const float * p_in, int i_nb_channels )
223 {
224     /* We do not only have to interleave, but also reorder the channels
225      * Channel reordering according to number of output channels of libA52
226      * The reordering needs to be different for different channel configurations
227      * (3F2R, 1F2R etc), so this is only temporary.
228      * The WG-4 order is appropriate for stereo, quadrophonia, and 5.1 surround.
229      *
230      * 6 channel mode
231      * channel  liba52 order    WG-4 order
232      * 0        LFE             // L
233      * 1        L               // R
234      * 2        C               // LS
235      * 3        R               // RS
236      * 4        LS              // C
237      * 5        RS              // LFE
238      *
239      * The liba52 moves channels to the front if there are unused spaces, so
240      * there is no gap between channels. The translation table says which
241      * channel of the new stream is taken from which original channel [use
242      * the new channel as the array index, use the number you get from the
243      * array to address the original channel].
244      */
245
246     static const int translation[7][6] =
247     {{ 0, 0, 0, 0, 0, 0 },      /* 0 channels (rarely used) */
248     { 0, 0, 0, 0, 0, 0 },       /* 1 ch */
249     { 0, 1, 0, 0, 0, 0 },       /* 2 */
250     { 1, 2, 0, 0, 0, 0 },       /* 3 */
251     { 1, 3, 2, 0, 0, 0 },       /* 4 */
252     { 1, 3, 4, 2, 0, 0 },       /* 5 */
253     { 1, 3, 4, 5, 2, 0 }};      /* 6 */
254
255     int i, j;
256     for ( j = 0; j < i_nb_channels; j++ )
257     {
258         for ( i = 0; i < 256; i++ )
259         {
260             p_out[i * i_nb_channels + j] = p_in[translation[i_nb_channels][j]
261                                                  * 256 + i];
262         }
263     }
264 }
265
266 /*****************************************************************************
267  * Duplicate: helper function to duplicate a unique channel
268  *****************************************************************************/
269 static void Duplicate( float * p_out, const float * p_in )
270 {
271     int i;
272
273     for ( i = 256; i--; )
274     {
275         *p_out++ = *p_in;
276         *p_out++ = *p_in;
277         p_in++;
278     }
279 }
280
281 /*****************************************************************************
282  * Exchange: helper function to exchange left & right channels
283  *****************************************************************************/
284 static void Exchange( float * p_out, const float * p_in )
285 {
286     int i;
287     const float * p_first = p_in + 256;
288     const float * p_second = p_in;
289
290     for ( i = 0; i < 256; i++ )
291     {
292         *p_out++ = *p_first++;
293         *p_out++ = *p_second++;
294     }
295 }
296
297 /*****************************************************************************
298  * DoWork: decode an ATSC A/52 frame.
299  *****************************************************************************/
300 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
301                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
302 {
303     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
304     sample_t        i_sample_level = 1;
305     int             i_flags = p_sys->i_flags;
306     int             i_bytes_per_block = 256 * p_sys->i_nb_channels
307                       * sizeof(float);
308     int             i;
309
310     /* Do the actual decoding now. */
311     a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
312                &i_flags, &i_sample_level, 0 );
313
314     if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
315           && !p_sys->b_dontwarn )
316     {
317         msg_Warn( p_filter,
318                   "liba52 couldn't do the requested downmix 0x%x->0x%x",
319                   p_sys->i_flags  & A52_CHANNEL_MASK,
320                   i_flags & A52_CHANNEL_MASK );
321
322         p_sys->b_dontwarn = 1;
323     }
324
325     if( !p_sys->b_dynrng )
326     {
327         a52_dynrng( p_filter->p_sys->p_liba52, NULL, NULL );
328     }
329
330     for ( i = 0; i < 6; i++ )
331     {
332         sample_t * p_samples;
333
334         if( a52_block( p_sys->p_liba52 ) )
335         {
336             msg_Warn( p_filter, "a52_block failed for block %d", i );
337         }
338
339         p_samples = a52_samples( p_sys->p_liba52 );
340
341         if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
342                || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
343                || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
344               && (p_filter->output.i_physical_channels 
345                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
346         {
347             Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
348                        p_samples );
349         }
350         else if ( p_filter->output.i_original_channels
351                     & AOUT_CHAN_REVERSESTEREO )
352         {
353             Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
354                       p_samples );
355         }
356         else
357         {
358             /* Interleave the *$£%ù samples. */
359             Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
360                         p_samples, p_sys->i_nb_channels );
361         }
362     }
363
364     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
365     p_out_buf->i_nb_bytes = i_bytes_per_block * 6;
366 }
367
368 /*****************************************************************************
369  * Destroy : deallocate data structures
370  *****************************************************************************/
371 static void Destroy( vlc_object_t * _p_filter )
372 {
373     aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
374     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
375
376     a52_free( p_sys->p_liba52 );
377     free( p_sys );
378 }
379