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 union { uint32_t u; int32_t i; } spl;
241 spl.u = ((uint32_t)p_in[j * 256 + i]) << 4;
242 p_out[i * i_nb_channels + pi_chan_table[j]] = spl.i;
244 p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
250 /*****************************************************************************
251 * Duplicate: helper function to duplicate a unique channel
252 *****************************************************************************/
253 static void Duplicate( sample_t *restrict p_out, const sample_t *restrict p_in )
255 for( unsigned i = 256; i--; )
258 union { uint32_t u; int32_t i; } spl;
260 spl.u = ((uint32_t)*(p_in++)) << 4;
264 sample_t s = *(p_in++);
272 /*****************************************************************************
273 * Exchange: helper function to exchange left & right channels
274 *****************************************************************************/
275 static void Exchange( sample_t *restrict p_out, const sample_t *restrict p_in )
277 const sample_t *p_first = p_in + 256;
278 const sample_t *p_second = p_in;
280 for( unsigned i = 0; i < 256; i++ )
285 spl[0] = ((uint32_t)*p_first++) << 4;
286 spl[1] = ((uint32_t)*p_second++) << 4;
287 memcpy( p_out, spl, sizeof(spl) );
290 *p_out++ = *p_first++;
291 *p_out++ = *p_second++;
296 /*****************************************************************************
297 * Convert: decode an ATSC A/52 frame.
298 *****************************************************************************/
300 static block_t *Convert( filter_t *p_filter, block_t *p_in_buf )
302 filter_sys_t *p_sys = p_filter->p_sys;
304 sample_t i_sample_level = (1 << 24);
306 sample_t i_sample_level = 1;
308 int i_flags = p_sys->i_flags;
309 size_t i_bytes_per_block = 256 * p_sys->i_nb_channels * sizeof(sample_t);
311 block_t *p_out_buf = block_Alloc( 6 * i_bytes_per_block );
312 if( unlikely(p_out_buf == NULL) )
315 /* Do the actual decoding now. */
316 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
317 &i_flags, &i_sample_level, 0 );
319 if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
320 && !p_sys->b_dontwarn )
323 "liba52 couldn't do the requested downmix 0x%x->0x%x",
324 p_sys->i_flags & A52_CHANNEL_MASK,
325 i_flags & A52_CHANNEL_MASK );
327 p_sys->b_dontwarn = 1;
330 if( !p_sys->b_dynrng )
332 a52_dynrng( p_sys->p_liba52, NULL, NULL );
335 for( unsigned i = 0; i < 6; i++ )
337 sample_t * p_samples;
339 if( a52_block( p_sys->p_liba52 ) )
341 msg_Warn( p_filter, "a52_block failed for block %d", i );
344 p_samples = a52_samples( p_sys->p_liba52 );
346 if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
347 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
348 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
349 && (p_filter->fmt_out.audio.i_physical_channels
350 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
352 Duplicate( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
355 else if ( p_filter->fmt_out.audio.i_original_channels
356 & AOUT_CHAN_REVERSESTEREO )
358 Exchange( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
363 /* Interleave the *$£%ù samples. */
364 Interleave( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
365 p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
369 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
370 p_out_buf->i_dts = p_in_buf->i_dts;
371 p_out_buf->i_pts = p_in_buf->i_pts;
372 p_out_buf->i_length = p_in_buf->i_length;
374 block_Release( p_in_buf );
378 /*****************************************************************************
380 *****************************************************************************/
381 static int OpenFilter( vlc_object_t *p_this )
383 filter_t *p_filter = (filter_t *)p_this;
387 if( p_filter->fmt_in.i_codec != VLC_CODEC_A52 )
390 if( p_filter->fmt_out.audio.i_format != VLC_CODEC_S32N )
392 if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
396 /* Allocate the memory needed to store the module's structure */
397 p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
401 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
402 &p_filter->fmt_in.audio, &p_filter->fmt_out.audio );
404 p_filter->pf_audio_filter = Convert;
405 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
410 /*****************************************************************************
411 * CloseFilter : deallocate data structures
412 *****************************************************************************/
413 static void CloseFilter( vlc_object_t *p_this )
415 filter_t *p_filter = (filter_t *)p_this;
416 filter_sys_t *p_sys = p_filter->p_sys;
418 a52_free( p_sys->p_liba52 );