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