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