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>
38 #include <stdint.h> /* int16_t .. */
43 #ifdef USE_A52DEC_TREE /* liba52 header file */
44 # include "include/a52.h"
46 # include "a52dec/a52.h"
50 #include <vlc_block.h>
51 #include <vlc_filter.h>
53 /*****************************************************************************
55 *****************************************************************************/
56 static int OpenFilter ( vlc_object_t * );
57 static void CloseFilter( vlc_object_t * );
58 static block_t *Convert( filter_t *, block_t * );
60 /* liba52 channel order */
61 static const uint32_t pi_channels_in[] =
62 { AOUT_CHAN_LFE, AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
63 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARCENTER, AOUT_CHAN_REARRIGHT, 0 };
65 /*****************************************************************************
67 *****************************************************************************/
70 a52_state_t * p_liba52; /* liba52 internal structure */
71 bool b_dynrng; /* see below */
72 int i_flags; /* liba52 flags, see a52dec/doc/liba52.txt */
74 int i_nb_channels; /* number of float32 per sample */
76 int pi_chan_table[AOUT_CHAN_MAX]; /* channel reordering */
79 /*****************************************************************************
81 *****************************************************************************/
82 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
83 #define DYNRNG_LONGTEXT N_( \
84 "Dynamic range compression makes the loud sounds softer, and the soft " \
85 "sounds louder, so you can more easily listen to the stream in a noisy " \
86 "environment without disturbing anyone. If you disable the dynamic range "\
87 "compression the playback will be more adapted to a movie theater or a " \
89 #define UPMIX_TEXT N_("Enable internal upmixing")
90 #define UPMIX_LONGTEXT N_( \
91 "Enable the internal upmixing algorithm (not recommended).")
94 set_shortname( "A/52" )
95 set_description( N_("ATSC A/52 (AC-3) audio decoder") )
96 set_category( CAT_INPUT )
97 set_subcategory( SUBCAT_INPUT_ACODEC )
98 add_bool( "a52-dynrng", true, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT, false )
99 add_bool( "a52-upmix", false, NULL, UPMIX_TEXT, UPMIX_LONGTEXT, true )
100 set_capability( "audio filter", 100 )
101 set_callbacks( OpenFilter, CloseFilter )
104 /*****************************************************************************
106 *****************************************************************************/
107 static int Open( vlc_object_t *p_this, filter_sys_t *p_sys,
108 audio_format_t input, audio_format_t output )
110 p_sys->b_dynrng = config_GetInt( p_this, "a52-dynrng" );
111 p_sys->b_dontwarn = 0;
113 /* No upmixing: it's not necessary and some other filters may want to do
115 if ( aout_FormatNbChannels( &output ) > aout_FormatNbChannels( &input ) )
117 if ( ! config_GetInt( p_this, "a52-upmix" ) )
123 /* We'll do our own downmixing, thanks. */
124 p_sys->i_nb_channels = aout_FormatNbChannels( &output );
125 switch ( (output.i_physical_channels & AOUT_CHAN_PHYSMASK)
128 case AOUT_CHAN_CENTER:
129 if ( (output.i_original_channels & AOUT_CHAN_CENTER)
130 || (output.i_original_channels
131 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
133 p_sys->i_flags = A52_MONO;
135 else if ( output.i_original_channels & AOUT_CHAN_LEFT )
137 p_sys->i_flags = A52_CHANNEL1;
141 p_sys->i_flags = A52_CHANNEL2;
145 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
146 if ( output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
148 p_sys->i_flags = A52_DOLBY;
150 else if ( input.i_original_channels == AOUT_CHAN_CENTER )
152 p_sys->i_flags = A52_MONO;
154 else if ( input.i_original_channels & AOUT_CHAN_DUALMONO )
156 p_sys->i_flags = A52_CHANNEL;
158 else if ( !(output.i_original_channels & AOUT_CHAN_RIGHT) )
160 p_sys->i_flags = A52_CHANNEL1;
162 else if ( !(output.i_original_channels & AOUT_CHAN_LEFT) )
164 p_sys->i_flags = A52_CHANNEL2;
168 p_sys->i_flags = A52_STEREO;
172 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
173 p_sys->i_flags = A52_3F;
176 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
177 p_sys->i_flags = A52_2F1R;
180 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
181 | AOUT_CHAN_REARCENTER:
182 p_sys->i_flags = A52_3F1R;
185 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
186 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
187 p_sys->i_flags = A52_2F2R;
190 case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
191 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
192 p_sys->i_flags = A52_3F2R;
196 msg_Warn( p_this, "unknown sample format!" );
200 if ( output.i_physical_channels & AOUT_CHAN_LFE )
202 p_sys->i_flags |= A52_LFE;
204 p_sys->i_flags |= A52_ADJUST_LEVEL;
206 /* Initialize liba52 */
207 p_sys->p_liba52 = a52_init( 0 );
208 if( p_sys->p_liba52 == NULL )
210 msg_Err( p_this, "unable to initialize liba52" );
215 aout_CheckChannelReorder( pi_channels_in, NULL,
216 output.i_physical_channels & AOUT_CHAN_PHYSMASK,
217 p_sys->i_nb_channels,
218 p_sys->pi_chan_table );
223 /*****************************************************************************
224 * Interleave: helper function to interleave channels
225 *****************************************************************************/
226 static void Interleave( sample_t * p_out, const sample_t * p_in,
227 int i_nb_channels, int *pi_chan_table )
229 /* We do not only have to interleave, but also reorder the channels */
232 for ( j = 0; j < i_nb_channels; j++ )
234 for ( i = 0; i < 256; i++ )
236 p_out[i * i_nb_channels + pi_chan_table[j]] = p_in[j * 256 + i];
241 /*****************************************************************************
242 * Duplicate: helper function to duplicate a unique channel
243 *****************************************************************************/
244 static void Duplicate( sample_t * p_out, const sample_t * p_in )
248 for ( i = 256; i--; )
256 /*****************************************************************************
257 * Exchange: helper function to exchange left & right channels
258 *****************************************************************************/
259 static void Exchange( sample_t * p_out, const sample_t * p_in )
262 const sample_t * p_first = p_in + 256;
263 const sample_t * p_second = p_in;
265 for ( i = 0; i < 256; i++ )
267 *p_out++ = *p_first++;
268 *p_out++ = *p_second++;
272 /*****************************************************************************
273 * DoWork: decode an ATSC A/52 frame.
274 *****************************************************************************/
275 static void DoWork( filter_t * p_filter,
276 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
278 filter_sys_t *p_sys = p_filter->p_sys;
280 sample_t i_sample_level = (1 << 24);
282 sample_t i_sample_level = 1;
284 int i_flags = p_sys->i_flags;
285 int i_bytes_per_block = 256 * p_sys->i_nb_channels
289 /* Do the actual decoding now. */
290 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
291 &i_flags, &i_sample_level, 0 );
293 if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK)
294 && !p_sys->b_dontwarn )
297 "liba52 couldn't do the requested downmix 0x%x->0x%x",
298 p_sys->i_flags & A52_CHANNEL_MASK,
299 i_flags & A52_CHANNEL_MASK );
301 p_sys->b_dontwarn = 1;
304 if( !p_sys->b_dynrng )
306 a52_dynrng( p_sys->p_liba52, NULL, NULL );
309 for ( i = 0; i < 6; i++ )
311 sample_t * p_samples;
313 if( a52_block( p_sys->p_liba52 ) )
315 msg_Warn( p_filter, "a52_block failed for block %d", i );
318 p_samples = a52_samples( p_sys->p_liba52 );
320 if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
321 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
322 || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
323 && (p_filter->fmt_out.audio.i_physical_channels
324 & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
326 Duplicate( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
329 else if ( p_filter->fmt_out.audio.i_original_channels
330 & AOUT_CHAN_REVERSESTEREO )
332 Exchange( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
337 /* Interleave the *$£%ù samples. */
338 Interleave( (sample_t *)(p_out_buf->p_buffer + i * i_bytes_per_block),
339 p_samples, p_sys->i_nb_channels, p_sys->pi_chan_table);
343 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
344 p_out_buf->i_buffer = i_bytes_per_block * 6;
347 /*****************************************************************************
349 *****************************************************************************/
350 static int OpenFilter( vlc_object_t *p_this )
352 filter_t *p_filter = (filter_t *)p_this;
356 if( p_filter->fmt_in.i_codec != VLC_CODEC_A52 ||
357 p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB ||
358 p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFL )
363 p_filter->fmt_out.audio.i_format =
365 p_filter->fmt_out.i_codec = VLC_CODEC_FI32;
367 p_filter->fmt_out.i_codec = VLC_CODEC_FL32;
369 p_filter->fmt_out.audio.i_bitspersample =
370 aout_BitsPerSample( p_filter->fmt_out.i_codec );
372 /* Allocate the memory needed to store the module's structure */
373 p_filter->p_sys = p_sys = malloc( sizeof(filter_sys_t) );
377 i_ret = Open( VLC_OBJECT(p_filter), p_sys,
378 p_filter->fmt_in.audio, p_filter->fmt_out.audio );
380 p_filter->pf_audio_filter = Convert;
381 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
386 /*****************************************************************************
387 * CloseFilter : deallocate data structures
388 *****************************************************************************/
389 static void CloseFilter( vlc_object_t *p_this )
391 filter_t *p_filter = (filter_t *)p_this;
392 filter_sys_t *p_sys = p_filter->p_sys;
394 a52_free( p_sys->p_liba52 );
398 static block_t *Convert( filter_t *p_filter, block_t *p_block )
400 if( !p_block || !p_block->i_nb_samples )
403 block_Release( p_block );
407 size_t i_out_size = p_block->i_nb_samples *
408 p_filter->fmt_out.audio.i_bitspersample *
409 p_filter->fmt_out.audio.i_channels / 8;
411 block_t *p_out = filter_NewAudioBuffer( p_filter, i_out_size );
414 msg_Warn( p_filter, "can't get output buffer" );
415 block_Release( p_block );
419 p_out->i_nb_samples = p_block->i_nb_samples;
420 p_out->i_dts = p_block->i_dts;
421 p_out->i_pts = p_block->i_pts;
422 p_out->i_length = p_block->i_length;
424 DoWork( p_filter, p_block, p_out );
426 block_Release( p_block );