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