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