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