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