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