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: a52tofloat32.c,v 1.17 2004/01/25 18:53:06 gbazin Exp $
9 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <stdlib.h> /* malloc(), free() */
33 #include <string.h> /* strdup() */
35 # include <stdint.h> /* int16_t .. */
37 # include <inttypes.h> /* int16_t .. */
44 #ifdef USE_A52DEC_TREE /* liba52 header file */
45 # include "include/a52.h"
47 # include "a52dec/a52.h"
50 #include "audio_output.h"
51 #include "aout_internal.h"
53 /*****************************************************************************
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 *,
61 /*****************************************************************************
63 *****************************************************************************/
64 struct aout_filter_sys_t
66 a52_state_t * p_liba52; /* liba52 internal structure */
67 vlc_bool_t b_dynrng; /* see below */
68 int i_flags; /* liba52 flags, see a52dec/doc/liba52.txt */
69 vlc_bool_t b_dontwarn;
70 int i_nb_channels; /* number of float32 per sample */
73 /*****************************************************************************
75 *****************************************************************************/
76 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
77 #define DYNRNG_LONGTEXT N_( \
78 "Dynamic range compression makes the loud sounds softer, and the soft " \
79 "sounds louder, so you can more easily listen to the stream in a noisy " \
80 "environment without disturbing anyone. If you disable the dynamic range "\
81 "compression the playback will be more adapted to a movie theater or a " \
85 set_description( _("ATSC A/52 aka AC-3 audio decoder") );
86 add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, VLC_FALSE );
87 set_capability( "audio filter", 100 );
88 set_callbacks( Create, Destroy );
91 /*****************************************************************************
93 *****************************************************************************/
94 static int Create( vlc_object_t * _p_filter )
96 aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
97 struct aout_filter_sys_t * p_sys;
99 if ( p_filter->input.i_format != VLC_FOURCC('a','5','2',' ')
100 || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
105 if ( p_filter->input.i_rate != p_filter->output.i_rate )
110 /* Allocate the memory needed to store the module's structure */
111 p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
114 msg_Err( p_filter, "out of memory" );
118 p_sys->b_dynrng = config_GetInt( p_filter, "a52-dynrng" );
119 p_sys->b_dontwarn = 0;
121 /* We'll do our own downmixing, thanks. */
122 p_sys->i_nb_channels = aout_FormatNbChannels( &p_filter->output );
123 switch ( (p_filter->output.i_physical_channels & AOUT_CHAN_PHYSMASK)
126 case AOUT_CHAN_CENTER:
127 if ( (p_filter->output.i_original_channels & AOUT_CHAN_CENTER)
128 || (p_filter->output.i_original_channels
129 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
131 p_sys->i_flags = A52_MONO;
133 else if ( p_filter->output.i_original_channels & AOUT_CHAN_LEFT )
135 p_sys->i_flags = A52_CHANNEL1;
139 p_sys->i_flags = A52_CHANNEL2;
143 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
144 if ( p_filter->output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
146 p_sys->i_flags = A52_DOLBY;
148 else if ( p_filter->input.i_original_channels == AOUT_CHAN_CENTER )
150 p_sys->i_flags = A52_MONO;
152 else if ( p_filter->input.i_original_channels & AOUT_CHAN_DUALMONO )
154 p_sys->i_flags = A52_CHANNEL;
156 else if ( !(p_filter->output.i_original_channels & AOUT_CHAN_RIGHT) )
158 p_sys->i_flags = A52_CHANNEL1;
160 else if ( !(p_filter->output.i_original_channels & AOUT_CHAN_LEFT) )
162 p_sys->i_flags = A52_CHANNEL2;
166 p_sys->i_flags = A52_STEREO;
170 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
171 p_sys->i_flags = A52_3F;
174 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
175 p_sys->i_flags = A52_2F1R;
178 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
179 | AOUT_CHAN_REARCENTER:
180 p_sys->i_flags = A52_3F1R;
183 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
184 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
185 p_sys->i_flags = A52_2F2R;
188 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
189 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
190 p_sys->i_flags = A52_3F2R;
194 msg_Warn( p_filter, "unknown sample format!" );
198 if ( p_filter->output.i_physical_channels & AOUT_CHAN_LFE )
200 p_sys->i_flags |= A52_LFE;
202 p_sys->i_flags |= A52_ADJUST_LEVEL;
204 /* Initialize liba52 */
205 p_sys->p_liba52 = a52_init( 0 );
206 if( p_sys->p_liba52 == NULL )
208 msg_Err( p_filter, "unable to initialize liba52" );
212 p_filter->pf_do_work = DoWork;
213 p_filter->b_in_place = 0;
218 /*****************************************************************************
219 * Interleave: helper function to interleave channels
220 *****************************************************************************/
221 static void Interleave( float * p_out, const float * p_in, int i_nb_channels )
223 /* We do not only have to interleave, but also reorder the channels
224 * Channel reordering according to number of output channels of libA52
225 * The reordering needs to be different for different channel configurations
226 * (3F2R, 1F2R etc), so this is only temporary.
227 * The WG-4 order is appropriate for stereo, quadrophonia, and 5.1 surround.
230 * channel liba52 order WG-4 order
238 * The liba52 moves channels to the front if there are unused spaces, so
239 * there is no gap between channels. The translation table says which
240 * channel of the new stream is taken from which original channel [use
241 * the new channel as the array index, use the number you get from the
242 * array to address the original channel].
245 static const int translation[7][6] =
246 {{ 0, 0, 0, 0, 0, 0 }, /* 0 channels (rarely used) */
247 { 0, 0, 0, 0, 0, 0 }, /* 1 ch */
248 { 0, 1, 0, 0, 0, 0 }, /* 2 */
249 { 1, 2, 0, 0, 0, 0 }, /* 3 */
250 { 1, 3, 2, 0, 0, 0 }, /* 4 */
251 { 1, 3, 4, 2, 0, 0 }, /* 5 */
252 { 1, 3, 4, 5, 2, 0 }}; /* 6 */
255 for ( j = 0; j < i_nb_channels; j++ )
257 for ( i = 0; i < 256; i++ )
259 p_out[i * i_nb_channels + j] = p_in[translation[i_nb_channels][j]
265 /*****************************************************************************
266 * Duplicate: helper function to duplicate a unique channel
267 *****************************************************************************/
268 static void Duplicate( float * p_out, const float * p_in )
272 for ( i = 256; i--; )
280 /*****************************************************************************
281 * Exchange: helper function to exchange left & right channels
282 *****************************************************************************/
283 static void Exchange( float * p_out, const float * p_in )
286 const float * p_first = p_in + 256;
287 const float * p_second = p_in;
289 for ( i = 0; i < 256; i++ )
291 *p_out++ = *p_first++;
292 *p_out++ = *p_second++;
296 /*****************************************************************************
297 * DoWork: decode an ATSC A/52 frame.
298 *****************************************************************************/
299 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
300 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
302 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
303 sample_t i_sample_level = 1;
304 int i_flags = p_sys->i_flags;
305 int i_bytes_per_block = 256 * p_sys->i_nb_channels
309 /* Do the actual decoding now. */
310 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
311 &i_flags, &i_sample_level, 0 );
313 if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
314 && !p_sys->b_dontwarn )
317 "liba52 couldn't do the requested downmix 0x%x->0x%x",
318 p_sys->i_flags & A52_CHANNEL_MASK,
319 i_flags & A52_CHANNEL_MASK );
321 p_sys->b_dontwarn = 1;
324 if( !p_sys->b_dynrng )
326 a52_dynrng( p_filter->p_sys->p_liba52, NULL, NULL );
329 for ( i = 0; i < 6; i++ )
331 sample_t * p_samples;
333 if( a52_block( p_sys->p_liba52 ) )
335 msg_Warn( p_filter, "a52_block failed for block %d", i );
338 p_samples = a52_samples( p_sys->p_liba52 );
340 if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
341 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
342 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
343 && (p_filter->output.i_physical_channels
344 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
346 Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
349 else if ( p_filter->output.i_original_channels
350 & AOUT_CHAN_REVERSESTEREO )
352 Exchange( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
357 /* Interleave the *$£%ù samples. */
358 Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
359 p_samples, p_sys->i_nb_channels );
363 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
364 p_out_buf->i_nb_bytes = i_bytes_per_block * 6;
367 /*****************************************************************************
368 * Destroy : deallocate data structures
369 *****************************************************************************/
370 static void Destroy( vlc_object_t * _p_filter )
372 aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
373 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
375 a52_free( p_sys->p_liba52 );