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-2009 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * NOTA BENE: this module requires the linking against a library which is
29 * known to require licensing under the GNU General Public License version 2
30 * (or later). Therefore, the result of compiling this module will normally
31 * be subject to the terms of that later license.
32 *****************************************************************************/
35 /*****************************************************************************
37 *****************************************************************************/
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
46 #include <stdint.h> /* int16_t .. */
48 #ifdef USE_A52DEC_TREE /* liba52 header file */
49 # include "include/a52.h"
51 # include "a52dec/a52.h"
55 #include <vlc_block.h>
56 #include <vlc_filter.h>
58 /*****************************************************************************
60 *****************************************************************************/
61 static int OpenFilter ( vlc_object_t * );
62 static void CloseFilter( vlc_object_t * );
63 static block_t *Convert( filter_t *, block_t * );
65 /* liba52 channel order */
66 static const uint32_t pi_channels_in[] =
67 { AOUT_CHAN_LFE, AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
68 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARCENTER, AOUT_CHAN_REARRIGHT, 0 };
70 /*****************************************************************************
72 *****************************************************************************/
75 a52_state_t * p_liba52; /* liba52 internal structure */
76 bool b_dynrng; /* see below */
77 int i_flags; /* liba52 flags, see a52dec/doc/liba52.txt */
79 int i_nb_channels; /* number of float32 per sample */
81 uint8_t pi_chan_table[AOUT_CHAN_MAX]; /* channel reordering */
84 /*****************************************************************************
86 *****************************************************************************/
87 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
88 #define DYNRNG_LONGTEXT N_( \
89 "Dynamic range compression makes the loud sounds softer, and the soft " \
90 "sounds louder, so you can more easily listen to the stream in a noisy " \
91 "environment without disturbing anyone. If you disable the dynamic range "\
92 "compression the playback will be more adapted to a movie theater or a " \
94 #define UPMIX_TEXT N_("Enable internal upmixing")
95 #define UPMIX_LONGTEXT N_( \
96 "Enable the internal upmixing algorithm (not recommended).")
99 set_shortname( "A/52" )
100 set_description( N_("ATSC A/52 (AC-3) audio decoder") )
101 set_category( CAT_INPUT )
102 set_subcategory( SUBCAT_INPUT_ACODEC )
103 add_bool( "a52-dynrng", true, DYNRNG_TEXT, DYNRNG_LONGTEXT, false )
104 add_bool( "a52-upmix", false, UPMIX_TEXT, UPMIX_LONGTEXT, true )
105 set_capability( "audio converter", 100 )
106 set_callbacks( OpenFilter, CloseFilter )
109 /*****************************************************************************
111 *****************************************************************************/
112 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
113 const audio_format_t *restrict input,
114 const audio_format_t *restrict output )
116 p_sys->b_dynrng = var_InheritBool( p_this, "a52-dynrng" );
117 p_sys->b_dontwarn = 0;
119 /* No upmixing: it's not necessary and some other filters may want to do
121 if ( aout_FormatNbChannels( output ) > aout_FormatNbChannels( input ) )
123 if ( ! var_InheritBool( p_this, "a52-upmix" ) )
129 /* We'll do our own downmixing, thanks. */
130 p_sys->i_nb_channels = aout_FormatNbChannels( output );
131 switch ( output->i_physical_channels & ~AOUT_CHAN_LFE )
133 case AOUT_CHAN_CENTER:
134 if ( (output->i_original_channels & AOUT_CHAN_CENTER)
135 || (output->i_original_channels
136 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
138 p_sys->i_flags = A52_MONO;
140 else if ( output->i_original_channels & AOUT_CHAN_LEFT )
142 p_sys->i_flags = A52_CHANNEL1;
146 p_sys->i_flags = A52_CHANNEL2;
150 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
151 if ( output->i_original_channels & AOUT_CHAN_DOLBYSTEREO )
153 p_sys->i_flags = A52_DOLBY;
155 else if ( input->i_original_channels == AOUT_CHAN_CENTER )
157 p_sys->i_flags = A52_MONO;
159 else if ( input->i_original_channels & AOUT_CHAN_DUALMONO )
161 p_sys->i_flags = A52_CHANNEL;
163 else if ( !(output->i_original_channels & AOUT_CHAN_RIGHT) )
165 p_sys->i_flags = A52_CHANNEL1;
167 else if ( !(output->i_original_channels & AOUT_CHAN_LEFT) )
169 p_sys->i_flags = A52_CHANNEL2;
173 p_sys->i_flags = A52_STEREO;
177 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
178 p_sys->i_flags = A52_3F;
181 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
182 p_sys->i_flags = A52_2F1R;
185 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
186 | AOUT_CHAN_REARCENTER:
187 p_sys->i_flags = A52_3F1R;
190 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
191 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
192 p_sys->i_flags = A52_2F2R;
195 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
196 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
197 p_sys->i_flags = A52_3F2R;
201 msg_Warn( p_this, "unknown sample format!" );
205 if ( output->i_physical_channels & AOUT_CHAN_LFE )
207 p_sys->i_flags |= A52_LFE;
209 p_sys->i_flags |= A52_ADJUST_LEVEL;
211 /* Initialize liba52 */
212 p_sys->p_liba52 = a52_init( 0 );
213 if( p_sys->p_liba52 == NULL )
215 msg_Err( p_this, "unable to initialize liba52" );
220 aout_CheckChannelReorder( pi_channels_in, NULL,
221 output->i_physical_channels,
222 p_sys->pi_chan_table );
227 /*****************************************************************************
228 * Interleave: helper function to interleave channels
229 *****************************************************************************/
230 static void Interleave( sample_t *restrict p_out, const sample_t *restrict p_in,
231 unsigned i_nb_channels, uint8_t *restrict pi_chan_table )
233 /* We do not only have to interleave, but also reorder the channels */
234 for( unsigned j = 0; j < i_nb_channels; j++ )
236 for( unsigned i = 0; i < 256; i++ )
239 p_out[i * i_nb_channels + pi_chan_table[j]] = ((uint32_t)p_in[j * 256 + i]) << 4;
241 p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
247 /*****************************************************************************
248 * Duplicate: helper function to duplicate a unique channel
249 *****************************************************************************/
250 static void Duplicate( sample_t *restrict p_out, const sample_t *restrict p_in )
252 for( unsigned i = 256; i--; )
255 sample_t s = ((uint32_t)*(p_in++)) << 4;
257 sample_t s = *(p_in++);
264 /*****************************************************************************
265 * Exchange: helper function to exchange left & right channels
266 *****************************************************************************/
267 static void Exchange( sample_t *restrict p_out, const sample_t *restrict p_in )
269 const sample_t *p_first = p_in + 256;
270 const sample_t *p_second = p_in;
272 for( unsigned i = 0; i < 256; i++ )
275 *p_out++ = ((uint32_t)*p_first++) << 4;
276 *p_out++ = ((uint32_t)*p_second++) << 4;
278 *p_out++ = *p_first++;
279 *p_out++ = *p_second++;
284 /*****************************************************************************
285 * Convert: decode an ATSC A/52 frame.
286 *****************************************************************************/
288 static block_t *Convert( filter_t *p_filter, block_t *p_in_buf )
290 filter_sys_t *p_sys = p_filter->p_sys;
292 sample_t i_sample_level = (1 << 24);
294 sample_t i_sample_level = 1;
296 int i_flags = p_sys->i_flags;
297 size_t i_bytes_per_block = 256 * p_sys->i_nb_channels * sizeof(sample_t);
299 block_t *p_out_buf = block_Alloc( 6 * i_bytes_per_block );
300 if( unlikely(p_out_buf == NULL) )
303 /* Do the actual decoding now. */
304 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
305 &i_flags, &i_sample_level, 0 );
307 if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
308 && !p_sys->b_dontwarn )
311 "liba52 couldn't do the requested downmix 0x%x->0x%x",
312 p_sys->i_flags & A52_CHANNEL_MASK,
313 i_flags & A52_CHANNEL_MASK );
315 p_sys->b_dontwarn = 1;
318 if( !p_sys->b_dynrng )
320 a52_dynrng( p_sys->p_liba52, NULL, NULL );
323 for( unsigned i = 0; i < 6; i++ )
325 sample_t * p_samples;
327 if( a52_block( p_sys->p_liba52 ) )
329 msg_Warn( p_filter, "a52_block failed for block %d", i );
332 p_samples = a52_samples( p_sys->p_liba52 );
334 if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
335 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
336 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
337 && (p_filter->fmt_out.audio.i_physical_channels
338 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
340 Duplicate( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
343 else if ( p_filter->fmt_out.audio.i_original_channels
344 & AOUT_CHAN_REVERSESTEREO )
346 Exchange( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
351 /* Interleave the *$£%ù samples. */
352 Interleave( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
353 p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
357 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
358 p_out_buf->i_dts = p_in_buf->i_dts;
359 p_out_buf->i_pts = p_in_buf->i_pts;
360 p_out_buf->i_length = p_in_buf->i_length;
362 block_Release( p_in_buf );
366 /*****************************************************************************
368 *****************************************************************************/
369 static int OpenFilter( vlc_object_t *p_this )
371 filter_t *p_filter = (filter_t *)p_this;
375 if( p_filter->fmt_in.i_codec != VLC_CODEC_A52 )
378 if( p_filter->fmt_out.audio.i_format != VLC_CODEC_S32N )
380 if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
384 /* Allocate the memory needed to store the module's structure */
385 p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
389 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
390 &p_filter->fmt_in.audio, &p_filter->fmt_out.audio );
392 p_filter->pf_audio_filter = Convert;
393 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
398 /*****************************************************************************
399 * CloseFilter : deallocate data structures
400 *****************************************************************************/
401 static void CloseFilter( vlc_object_t *p_this )
403 filter_t *p_filter = (filter_t *)p_this;
404 filter_sys_t *p_sys = p_filter->p_sys;
406 a52_free( p_sys->p_liba52 );