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
9 * Authors: Gildas Bazin <gbazin@videolan.org>
10 * Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
38 # include <stdint.h> /* int16_t .. */
40 # include <inttypes.h> /* int16_t .. */
47 #ifdef USE_A52DEC_TREE /* liba52 header file */
48 # include "include/a52.h"
50 # include "a52dec/a52.h"
54 #include <vlc_block.h>
55 #include <vlc_filter.h>
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Create ( vlc_object_t * );
61 static void Destroy ( vlc_object_t * );
62 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
64 static int Open ( vlc_object_t *, filter_sys_t *,
65 audio_format_t, audio_format_t );
67 static int OpenFilter ( vlc_object_t * );
68 static void CloseFilter( vlc_object_t * );
69 static block_t *Convert( filter_t *, block_t * );
71 /* liba52 channel order */
72 static const uint32_t pi_channels_in[] =
73 { AOUT_CHAN_LFE, AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
74 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
75 /* our internal channel order (WG-4 order) */
76 static const uint32_t pi_channels_out[] =
77 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
78 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
80 /*****************************************************************************
82 *****************************************************************************/
85 a52_state_t * p_liba52; /* liba52 internal structure */
86 bool b_dynrng; /* see below */
87 int i_flags; /* liba52 flags, see a52dec/doc/liba52.txt */
89 int i_nb_channels; /* number of float32 per sample */
91 int pi_chan_table[AOUT_CHAN_MAX]; /* channel reordering */
94 /*****************************************************************************
96 *****************************************************************************/
97 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
98 #define DYNRNG_LONGTEXT N_( \
99 "Dynamic range compression makes the loud sounds softer, and the soft " \
100 "sounds louder, so you can more easily listen to the stream in a noisy " \
101 "environment without disturbing anyone. If you disable the dynamic range "\
102 "compression the playback will be more adapted to a movie theater or a " \
104 #define UPMIX_TEXT N_("Enable internal upmixing")
105 #define UPMIX_LONGTEXT N_( \
106 "Enable the internal upmixing algorithm (not recommended).")
109 set_shortname( "A/52" );
110 set_description( N_("ATSC A/52 (AC-3) audio decoder") );
111 set_category( CAT_INPUT );
112 set_subcategory( SUBCAT_INPUT_ACODEC );
113 add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, false );
114 add_bool( "a52-upmix", 0, NULL, UPMIX_TEXT, UPMIX_LONGTEXT, true );
115 set_capability( "audio filter", 100 );
116 set_callbacks( Create, Destroy );
119 set_description( N_("ATSC A/52 (AC-3) audio decoder") );
120 set_capability( "audio filter2", 100 );
121 set_callbacks( OpenFilter, CloseFilter );
124 /*****************************************************************************
126 *****************************************************************************/
127 static int Create( vlc_object_t *p_this )
129 aout_filter_t *p_filter = (aout_filter_t *)p_this;
130 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
133 if ( p_filter->input.i_format != VLC_FOURCC('a','5','2',' ')
135 || p_filter->output.i_format != VLC_FOURCC('f','i','3','2') )
137 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
143 if ( p_filter->input.i_rate != p_filter->output.i_rate )
148 /* Allocate the memory needed to store the module's structure */
149 p_sys = malloc( sizeof(filter_sys_t) );
150 p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
153 msg_Err( p_filter, "out of memory" );
157 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
158 p_filter->input, p_filter->output );
160 p_filter->pf_do_work = DoWork;
161 p_filter->b_in_place = 0;
166 /*****************************************************************************
168 *****************************************************************************/
169 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
170 audio_format_t input, audio_format_t output )
172 p_sys->b_dynrng = config_GetInt( p_this, "a52-dynrng" );
173 p_sys->b_dontwarn = 0;
175 /* No upmixing: it's not necessary and some other filters may want to do
177 if ( aout_FormatNbChannels( &output ) > aout_FormatNbChannels( &input ) )
179 if ( ! config_GetInt( p_this, "a52-upmix" ) )
185 /* We'll do our own downmixing, thanks. */
186 p_sys->i_nb_channels = aout_FormatNbChannels( &output );
187 switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
190 case AOUT_CHAN_CENTER:
191 if ( (output.i_original_channels & AOUT_CHAN_CENTER)
192 || (output.i_original_channels
193 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
195 p_sys->i_flags = A52_MONO;
197 else if ( output.i_original_channels & AOUT_CHAN_LEFT )
199 p_sys->i_flags = A52_CHANNEL1;
203 p_sys->i_flags = A52_CHANNEL2;
207 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
208 if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
210 p_sys->i_flags = A52_DOLBY;
212 else if ( input.i_original_channels == AOUT_CHAN_CENTER )
214 p_sys->i_flags = A52_MONO;
216 else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
218 p_sys->i_flags = A52_CHANNEL;
220 else if ( !(output.i_original_channels & AOUT_CHAN_RIGHT) )
222 p_sys->i_flags = A52_CHANNEL1;
224 else if ( !(output.i_original_channels & AOUT_CHAN_LEFT) )
226 p_sys->i_flags = A52_CHANNEL2;
230 p_sys->i_flags = A52_STEREO;
234 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
235 p_sys->i_flags = A52_3F;
238 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
239 p_sys->i_flags = A52_2F1R;
242 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
243 | AOUT_CHAN_REARCENTER:
244 p_sys->i_flags = A52_3F1R;
247 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
248 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
249 p_sys->i_flags = A52_2F2R;
252 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
253 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
254 p_sys->i_flags = A52_3F2R;
258 msg_Warn( p_this, "unknown sample format!" );
262 if ( output.i_physical_channels & AOUT_CHAN_LFE )
264 p_sys->i_flags |= A52_LFE;
266 p_sys->i_flags |= A52_ADJUST_LEVEL;
268 /* Initialize liba52 */
269 p_sys->p_liba52 = a52_init( 0 );
270 if( p_sys->p_liba52 == NULL )
272 msg_Err( p_this, "unable to initialize liba52" );
277 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
278 output.i_physical_channels & AOUT_CHAN_PHYSMASK,
279 p_sys->i_nb_channels,
280 p_sys->pi_chan_table );
285 /*****************************************************************************
286 * Interleave: helper function to interleave channels
287 *****************************************************************************/
288 static void Interleave( float * p_out, const float * p_in, int i_nb_channels,
291 /* We do not only have to interleave, but also reorder the channels */
294 for ( j = 0; j < i_nb_channels; j++ )
296 for ( i = 0; i < 256; i++ )
298 p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
303 /*****************************************************************************
304 * Duplicate: helper function to duplicate a unique channel
305 *****************************************************************************/
306 static void Duplicate( float * p_out, const float * p_in )
310 for ( i = 256; i--; )
318 /*****************************************************************************
319 * Exchange: helper function to exchange left & right channels
320 *****************************************************************************/
321 static void Exchange( float * p_out, const float * p_in )
324 const float * p_first = p_in + 256;
325 const float * p_second = p_in;
327 for ( i = 0; i < 256; i++ )
329 *p_out++ = *p_first++;
330 *p_out++ = *p_second++;
334 /*****************************************************************************
335 * DoWork: decode an ATSC A/52 frame.
336 *****************************************************************************/
337 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
338 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
340 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
342 sample_t i_sample_level = (1 << 24);
344 sample_t i_sample_level = 1;
346 int i_flags = p_sys->i_flags;
347 int i_bytes_per_block = 256 * p_sys->i_nb_channels
351 /* Do the actual decoding now. */
352 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
353 &i_flags, &i_sample_level, 0 );
355 if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
356 && !p_sys->b_dontwarn )
359 "liba52 couldn't do the requested downmix 0x%x->0x%x",
360 p_sys->i_flags & A52_CHANNEL_MASK,
361 i_flags & A52_CHANNEL_MASK );
363 p_sys->b_dontwarn = 1;
366 if( !p_sys->b_dynrng )
368 a52_dynrng( p_sys->p_liba52, NULL, NULL );
371 for ( i = 0; i < 6; i++ )
373 sample_t * p_samples;
375 if( a52_block( p_sys->p_liba52 ) )
377 msg_Warn( p_aout, "a52_block failed for block %d", i );
380 p_samples = a52_samples( p_sys->p_liba52 );
382 if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
383 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
384 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
385 && (p_filter->output.i_physical_channels
386 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
388 Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
391 else if ( p_filter->output.i_original_channels
392 & AOUT_CHAN_REVERSESTEREO )
394 Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
399 /* Interleave the *$£%ù samples. */
400 Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
401 p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
405 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
406 p_out_buf->i_nb_bytes = i_bytes_per_block * 6;
409 /*****************************************************************************
410 * Destroy : deallocate data structures
411 *****************************************************************************/
412 static void Destroy( vlc_object_t *p_this )
414 aout_filter_t *p_filter = (aout_filter_t *)p_this;
415 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
417 a52_free( p_sys->p_liba52 );
421 /*****************************************************************************
423 *****************************************************************************/
424 static int OpenFilter( vlc_object_t *p_this )
426 filter_t *p_filter = (filter_t *)p_this;
430 if( p_filter->fmt_in.i_codec != VLC_FOURCC('a','5','2',' ') )
435 p_filter->fmt_out.audio.i_format =
437 p_filter->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
439 p_filter->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
441 p_filter->fmt_out.audio.i_bitspersample =
442 aout_BitsPerSample( p_filter->fmt_out.i_codec );
444 /* Allocate the memory needed to store the module's structure */
445 p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
448 msg_Err( p_filter, "out of memory" );
452 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
453 p_filter->fmt_in.audio, p_filter->fmt_out.audio );
455 p_filter->pf_audio_filter = Convert;
456 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
461 /*****************************************************************************
462 * CloseFilter : deallocate data structures
463 *****************************************************************************/
464 static void CloseFilter( vlc_object_t *p_this )
466 filter_t *p_filter = (filter_t *)p_this;
467 filter_sys_t *p_sys = p_filter->p_sys;
469 a52_free( p_sys->p_liba52 );
473 static block_t *Convert( filter_t *p_filter, block_t *p_block )
475 aout_filter_t aout_filter;
476 aout_buffer_t in_buf, out_buf;
480 if( !p_block || !p_block->i_samples )
482 if( p_block ) p_block->pf_release( p_block );
486 i_out_size = p_block->i_samples *
487 p_filter->fmt_out.audio.i_bitspersample *
488 p_filter->fmt_out.audio.i_channels / 8;
490 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
493 msg_Warn( p_filter, "can't get output buffer" );
494 p_block->pf_release( p_block );
498 p_out->i_samples = p_block->i_samples;
499 p_out->i_dts = p_block->i_dts;
500 p_out->i_pts = p_block->i_pts;
501 p_out->i_length = p_block->i_length;
503 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
504 aout_filter.input = p_filter->fmt_in.audio;
505 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
506 aout_filter.output = p_filter->fmt_out.audio;
507 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
509 in_buf.p_buffer = p_block->p_buffer;
510 in_buf.i_nb_bytes = p_block->i_buffer;
511 in_buf.i_nb_samples = p_block->i_samples;
512 out_buf.p_buffer = p_out->p_buffer;
513 out_buf.i_nb_bytes = p_out->i_buffer;
514 out_buf.i_nb_samples = p_out->i_samples;
516 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
518 p_out->i_buffer = out_buf.i_nb_bytes;
519 p_out->i_samples = out_buf.i_nb_samples;
521 p_block->pf_release( p_block );