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.1 2002/09/02 23:17:05 massiot 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 int i_nb_channels; /* number of float32 per sample */
72 /*****************************************************************************
74 *****************************************************************************/
75 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
76 #define DYNRNG_LONGTEXT N_( \
77 "Dynamic range compression makes the loud sounds softer, and the soft " \
78 "sounds louder, so you can more easily listen to the stream in a noisy " \
79 "environment without disturbing anyone. If you disable the dynamic range "\
80 "compression the playback will be more adapted to a movie theater or a " \
84 add_category_hint( N_("Miscellaneous"), NULL );
85 add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT );
86 set_description( _("ATSC A/52 aka AC-3 audio decoder module") );
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 != AOUT_FMT_A52
100 || p_filter->output.i_format != AOUT_FMT_FLOAT32 )
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" );
120 /* We'll do our own downmixing, thanks. */
121 p_sys->i_nb_channels = aout_FormatNbChannels( &p_filter->output );
122 switch ( p_filter->output.i_channels & AOUT_CHAN_MASK )
124 case AOUT_CHAN_CHANNEL: p_sys->i_flags = A52_CHANNEL; break;
125 case AOUT_CHAN_CHANNEL1: p_sys->i_flags = A52_CHANNEL1; break;
126 case AOUT_CHAN_CHANNEL2: p_sys->i_flags = A52_CHANNEL2; break;
127 case AOUT_CHAN_MONO: p_sys->i_flags = A52_MONO; break;
128 case AOUT_CHAN_STEREO: p_sys->i_flags = A52_STEREO; break;
129 case AOUT_CHAN_DOLBY: p_sys->i_flags = A52_DOLBY; break;
130 case AOUT_CHAN_3F: p_sys->i_flags = A52_3F; break;
131 case AOUT_CHAN_2F1R: p_sys->i_flags = A52_2F1R; break;
132 case AOUT_CHAN_3F1R: p_sys->i_flags = A52_3F1R; break;
133 case AOUT_CHAN_2F2R: p_sys->i_flags = A52_2F2R; break;
134 case AOUT_CHAN_3F2R: p_sys->i_flags = A52_3F2R; break;
136 msg_Err( p_filter, "unknow sample format !" );
140 if ( p_filter->output.i_channels & AOUT_CHAN_LFE )
142 p_sys->i_flags |= A52_LFE;
144 p_sys->i_flags |= A52_ADJUST_LEVEL;
146 /* Initialize liba52 */
147 p_sys->p_liba52 = a52_init( 0 );
148 if( p_sys->p_liba52 == NULL )
150 msg_Err( p_filter, "unable to initialize liba52" );
154 p_filter->pf_do_work = DoWork;
155 p_filter->b_in_place = 0;
160 /*****************************************************************************
161 * Interleave: helper function to interleave channels
162 *****************************************************************************/
163 static void Interleave( float * p_out, const float * p_in, int i_channels )
167 for ( j = 0; j < i_channels; j++ )
169 for ( i = 0; i < 256; i++ )
171 p_out[i * i_channels + j] = p_in[j * 256 + i];
176 /*****************************************************************************
177 * DoWork: decode an ATSC A/52 frame.
178 *****************************************************************************/
179 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
180 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
182 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
183 sample_t i_sample_level = 1;
184 int i_flags = p_sys->i_flags;
185 int i_bytes_per_block = 256 * p_sys->i_nb_channels
189 /* Do the actual decoding now. */
190 a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
191 &i_flags, &i_sample_level, 0 );
193 if ( i_flags != p_sys->i_flags )
196 "liba52 couldn't do the requested downmix 0x%x->0x%x",
197 p_sys->i_flags, i_flags );
198 memset( p_out_buf->p_buffer, 0, i_bytes_per_block * 6 );
202 if( !p_sys->b_dynrng )
204 a52_dynrng( p_filter->p_sys->p_liba52, NULL, NULL );
207 for ( i = 0; i < 6; i++ )
209 sample_t * p_samples;
211 if( a52_block( p_sys->p_liba52 ) )
213 msg_Warn( p_filter, "a52_block failed for block %d", i );
216 p_samples = a52_samples( p_sys->p_liba52 );
218 /* Interleave the *$£%ù samples. */
219 Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
220 p_samples, p_sys->i_nb_channels );
223 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
224 p_out_buf->i_nb_bytes = i_bytes_per_block * 6;
227 /*****************************************************************************
228 * Destroy : deallocate data structures
229 *****************************************************************************/
230 static void Destroy( vlc_object_t * _p_filter )
232 aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
233 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
235 a52_free( p_sys->p_liba52 );