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