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 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>
37 #include <stdint.h> /* int16_t .. */
39 #ifdef USE_A52DEC_TREE /* liba52 header file */
40 # include "include/a52.h"
42 # include "a52dec/a52.h"
46 #include <vlc_block.h>
47 #include <vlc_filter.h>
49 /*****************************************************************************
51 *****************************************************************************/
52 static int OpenFilter ( vlc_object_t * );
53 static void CloseFilter( vlc_object_t * );
54 static block_t *Convert( filter_t *, block_t * );
56 /* liba52 channel order */
57 static const uint32_t pi_channels_in[] =
58 { AOUT_CHAN_LFE, AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
59 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARCENTER, AOUT_CHAN_REARRIGHT, 0 };
61 /*****************************************************************************
63 *****************************************************************************/
66 a52_state_t * p_liba52; /* liba52 internal structure */
67 bool b_dynrng; /* see below */
68 int i_flags; /* liba52 flags, see a52dec/doc/liba52.txt */
70 int i_nb_channels; /* number of float32 per sample */
72 int pi_chan_table[AOUT_CHAN_MAX]; /* channel reordering */
75 /*****************************************************************************
77 *****************************************************************************/
78 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
79 #define DYNRNG_LONGTEXT N_( \
80 "Dynamic range compression makes the loud sounds softer, and the soft " \
81 "sounds louder, so you can more easily listen to the stream in a noisy " \
82 "environment without disturbing anyone. If you disable the dynamic range "\
83 "compression the playback will be more adapted to a movie theater or a " \
85 #define UPMIX_TEXT N_("Enable internal upmixing")
86 #define UPMIX_LONGTEXT N_( \
87 "Enable the internal upmixing algorithm (not recommended).")
90 set_shortname( "A/52" )
91 set_description( N_("ATSC A/52 (AC-3) audio decoder") )
92 set_category( CAT_INPUT )
93 set_subcategory( SUBCAT_INPUT_ACODEC )
94 add_bool( "a52-dynrng", true, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, false )
95 add_bool( "a52-upmix", false, NULL, UPMIX_TEXT, UPMIX_LONGTEXT, true )
96 set_capability( "audio filter2", 100 )
97 set_callbacks( OpenFilter, CloseFilter )
100 /*****************************************************************************
102 *****************************************************************************/
103 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
104 audio_format_t input, audio_format_t output )
106 p_sys->b_dynrng = config_GetInt( p_this, "a52-dynrng" );
107 p_sys->b_dontwarn = 0;
109 /* No upmixing: it's not necessary and some other filters may want to do
111 if ( aout_FormatNbChannels( &output ) > aout_FormatNbChannels( &input ) )
113 if ( ! config_GetInt( p_this, "a52-upmix" ) )
119 /* We'll do our own downmixing, thanks. */
120 p_sys->i_nb_channels = aout_FormatNbChannels( &output );
121 switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
124 case AOUT_CHAN_CENTER:
125 if ( (output.i_original_channels & AOUT_CHAN_CENTER)
126 || (output.i_original_channels
127 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
129 p_sys->i_flags = A52_MONO;
131 else if ( output.i_original_channels & AOUT_CHAN_LEFT )
133 p_sys->i_flags = A52_CHANNEL1;
137 p_sys->i_flags = A52_CHANNEL2;
141 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
142 if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
144 p_sys->i_flags = A52_DOLBY;
146 else if ( input.i_original_channels == AOUT_CHAN_CENTER )
148 p_sys->i_flags = A52_MONO;
150 else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
152 p_sys->i_flags = A52_CHANNEL;
154 else if ( !(output.i_original_channels & AOUT_CHAN_RIGHT) )
156 p_sys->i_flags = A52_CHANNEL1;
158 else if ( !(output.i_original_channels & AOUT_CHAN_LEFT) )
160 p_sys->i_flags = A52_CHANNEL2;
164 p_sys->i_flags = A52_STEREO;
168 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
169 p_sys->i_flags = A52_3F;
172 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
173 p_sys->i_flags = A52_2F1R;
176 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
177 | AOUT_CHAN_REARCENTER:
178 p_sys->i_flags = A52_3F1R;
181 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
182 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
183 p_sys->i_flags = A52_2F2R;
186 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
187 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
188 p_sys->i_flags = A52_3F2R;
192 msg_Warn( p_this, "unknown sample format!" );
196 if ( output.i_physical_channels & AOUT_CHAN_LFE )
198 p_sys->i_flags |= A52_LFE;
200 p_sys->i_flags |= A52_ADJUST_LEVEL;
202 /* Initialize liba52 */
203 p_sys->p_liba52 = a52_init( 0 );
204 if( p_sys->p_liba52 == NULL )
206 msg_Err( p_this, "unable to initialize liba52" );
211 aout_CheckChannelReorder( pi_channels_in, NULL,
212 output.i_physical_channels & AOUT_CHAN_PHYSMASK,
213 p_sys->i_nb_channels,
214 p_sys->pi_chan_table );
219 /*****************************************************************************
220 * Interleave: helper function to interleave channels
221 *****************************************************************************/
222 static void Interleave( sample_t * p_out, const sample_t * p_in,
223 int i_nb_channels, int *pi_chan_table )
225 /* We do not only have to interleave, but also reorder the channels */
228 for ( j = 0; j < i_nb_channels; j++ )
230 for ( i = 0; i < 256; i++ )
232 p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
237 /*****************************************************************************
238 * Duplicate: helper function to duplicate a unique channel
239 *****************************************************************************/
240 static void Duplicate( sample_t * p_out, const sample_t * p_in )
244 for ( i = 256; i--; )
252 /*****************************************************************************
253 * Exchange: helper function to exchange left & right channels
254 *****************************************************************************/
255 static void Exchange( sample_t * p_out, const sample_t * p_in )
258 const sample_t * p_first = p_in + 256;
259 const sample_t * p_second = p_in;
261 for ( i = 0; i < 256; i++ )
263 *p_out++ = *p_first++;
264 *p_out++ = *p_second++;
268 /*****************************************************************************
269 * DoWork: decode an ATSC A/52 frame.
270 *****************************************************************************/
271 static void DoWork( filter_t * p_filter,
272 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
274 filter_sys_t *p_sys = p_filter->p_sys;
276 sample_t i_sample_level = (1 << 24);
278 sample_t i_sample_level = 1;
280 int i_flags = p_sys->i_flags;
281 int i_bytes_per_block = 256 * p_sys->i_nb_channels
285 /* Do the actual decoding now. */
286 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
287 &i_flags, &i_sample_level, 0 );
289 if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
290 && !p_sys->b_dontwarn )
293 "liba52 couldn't do the requested downmix 0x%x->0x%x",
294 p_sys->i_flags & A52_CHANNEL_MASK,
295 i_flags & A52_CHANNEL_MASK );
297 p_sys->b_dontwarn = 1;
300 if( !p_sys->b_dynrng )
302 a52_dynrng( p_sys->p_liba52, NULL, NULL );
305 for ( i = 0; i < 6; i++ )
307 sample_t * p_samples;
309 if( a52_block( p_sys->p_liba52 ) )
311 msg_Warn( p_filter, "a52_block failed for block %d", i );
314 p_samples = a52_samples( p_sys->p_liba52 );
316 if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
317 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
318 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
319 && (p_filter->fmt_out.audio.i_physical_channels
320 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
322 Duplicate( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
325 else if ( p_filter->fmt_out.audio.i_original_channels
326 & AOUT_CHAN_REVERSESTEREO )
328 Exchange( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
333 /* Interleave the *$£%ù samples. */
334 Interleave( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
335 p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
339 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
340 p_out_buf->i_buffer = i_bytes_per_block * 6;
343 /*****************************************************************************
345 *****************************************************************************/
346 static int OpenFilter( vlc_object_t *p_this )
348 filter_t *p_filter = (filter_t *)p_this;
352 if( p_filter->fmt_in.i_codec != VLC_CODEC_A52 )
357 p_filter->fmt_out.audio.i_format =
359 p_filter->fmt_out.i_codec = VLC_CODEC_FI32;
361 p_filter->fmt_out.i_codec = VLC_CODEC_FL32;
363 p_filter->fmt_out.audio.i_bitspersample =
364 aout_BitsPerSample( p_filter->fmt_out.i_codec );
366 /* Allocate the memory needed to store the module's structure */
367 p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
371 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
372 p_filter->fmt_in.audio, p_filter->fmt_out.audio );
374 p_filter->pf_audio_filter = Convert;
375 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
380 /*****************************************************************************
381 * CloseFilter : deallocate data structures
382 *****************************************************************************/
383 static void CloseFilter( vlc_object_t *p_this )
385 filter_t *p_filter = (filter_t *)p_this;
386 filter_sys_t *p_sys = p_filter->p_sys;
388 a52_free( p_sys->p_liba52 );
392 static block_t *Convert( filter_t *p_filter, block_t *p_block )
394 if( !p_block || !p_block->i_nb_samples )
397 block_Release( p_block );
401 size_t i_out_size = p_block->i_nb_samples *
402 p_filter->fmt_out.audio.i_bitspersample *
403 p_filter->fmt_out.audio.i_channels / 8;
405 block_t *p_out = filter_NewAudioBuffer( p_filter, i_out_size );
408 msg_Warn( p_filter, "can't get output buffer" );
409 block_Release( p_block );
413 p_out->i_nb_samples = p_block->i_nb_samples;
414 p_out->i_dts = p_block->i_dts;
415 p_out->i_pts = p_block->i_pts;
416 p_out->i_length = p_block->i_length;
418 DoWork( p_filter, p_block, p_out );
420 block_Release( p_block );