]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/a52tofloat32.c
Partial fix of the OS X audio output (thanks Heiko!).
[vlc] / modules / audio_filter / converter / a52tofloat32.c
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.7 2002/11/18 23:00:41 massiot Exp $
8  *
9  * Authors: Gildas Bazin <gbazin@netcourrier.com>
10  *          Christophe Massiot <massiot@via.ecp.fr>
11  *      
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.
16  * 
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <vlc/vlc.h>
31
32 #include <stdlib.h>                                      /* malloc(), free() */
33 #include <string.h>                                              /* strdup() */
34 #ifdef HAVE_STDINT_H
35 #   include <stdint.h>                                         /* int16_t .. */
36 #elif HAVE_INTTYPES_H
37 #   include <inttypes.h>                                       /* int16_t .. */
38 #endif
39
40 #ifdef HAVE_UNISTD_H
41 #   include <unistd.h>
42 #endif
43
44 #ifdef USE_A52DEC_TREE                                 /* liba52 header file */
45 #   include "include/a52.h"
46 #else
47 #   include "a52dec/a52.h"
48 #endif
49
50 #include "audio_output.h"
51 #include "aout_internal.h"
52
53 /*****************************************************************************
54  * Local prototypes
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 *,  
59                         aout_buffer_t * );
60
61 /*****************************************************************************
62  * Local structures
63  *****************************************************************************/
64 struct aout_filter_sys_t
65 {
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 */
70 };
71
72 /*****************************************************************************
73  * Module descriptor
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 " \
81     "listening room.")
82
83 vlc_module_begin();
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 );
89 vlc_module_end();
90
91 /*****************************************************************************
92  * Create: 
93  *****************************************************************************/
94 static int Create( vlc_object_t * _p_filter )
95 {
96     aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
97     struct aout_filter_sys_t * p_sys;
98
99     if ( p_filter->input.i_format != VLC_FOURCC('a','5','2',' ')
100           || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
101     {
102         return -1;
103     }
104
105     if ( p_filter->input.i_rate != p_filter->output.i_rate )
106     {
107         return -1;
108     }
109
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) );
112     if( p_sys == NULL )
113     {
114         msg_Err( p_filter, "out of memory" );
115         return -1;
116     }
117
118     p_sys->b_dynrng = config_GetInt( p_filter, "a52-dynrng" );
119
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_physical_channels & AOUT_CHAN_PHYSMASK)
123               & ~AOUT_CHAN_LFE )
124     {
125     case AOUT_CHAN_CENTER:
126         if ( (p_filter->output.i_original_channels & AOUT_CHAN_CENTER)
127               || (p_filter->output.i_original_channels
128                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
129         {
130             p_sys->i_flags = A52_MONO;
131         }
132         else if ( p_filter->output.i_original_channels & AOUT_CHAN_LEFT )
133         {
134             p_sys->i_flags = A52_CHANNEL1;
135         }
136         else
137         {
138             p_sys->i_flags = A52_CHANNEL2;
139         }
140         break;
141
142     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT:
143         if ( p_filter->output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
144         {
145             p_sys->i_flags = A52_DOLBY;
146         }
147         else if ( p_filter->input.i_original_channels & AOUT_CHAN_DUALMONO )
148         {
149             p_sys->i_flags = A52_CHANNEL;
150         }
151         else if ( !(p_filter->output.i_original_channels & AOUT_CHAN_RIGHT) )
152         {
153             p_sys->i_flags = A52_CHANNEL1;
154         }
155         else if ( !(p_filter->output.i_original_channels & AOUT_CHAN_LEFT) )
156         {
157             p_sys->i_flags = A52_CHANNEL2;
158         }
159         else
160         {
161             p_sys->i_flags = A52_STEREO;
162         }
163         break;
164
165     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER:
166         p_sys->i_flags = A52_3F;
167         break;
168
169     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARCENTER:
170         p_sys->i_flags = A52_2F1R;
171         break;
172
173     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
174           | AOUT_CHAN_REARCENTER:
175         p_sys->i_flags = A52_3F1R;
176         break;
177
178     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
179           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
180         p_sys->i_flags = A52_2F2R;
181         break;
182
183     case AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
184           | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT:
185         p_sys->i_flags = A52_3F2R;
186         break;
187
188     default:
189         msg_Err( p_filter, "unknow sample format !" );
190         free( p_sys );
191         return -1;
192     }
193     if ( p_filter->output.i_physical_channels & AOUT_CHAN_LFE )
194     {
195         p_sys->i_flags |= A52_LFE;
196     }
197     p_sys->i_flags |= A52_ADJUST_LEVEL;
198
199     /* Initialize liba52 */
200     p_sys->p_liba52 = a52_init( 0 );
201     if( p_sys->p_liba52 == NULL )
202     {
203         msg_Err( p_filter, "unable to initialize liba52" );
204         return -1;
205     }
206
207     p_filter->pf_do_work = DoWork;
208     p_filter->b_in_place = 0;
209
210     return 0;
211 }
212
213 /*****************************************************************************
214  * Interleave: helper function to interleave channels
215  *****************************************************************************/
216 static void Interleave( float * p_out, const float * p_in, int i_nb_channels )
217 {
218     int i, j;
219
220     for ( j = 0; j < i_nb_channels; j++ )
221     {
222         for ( i = 0; i < 256; i++ )
223         {
224             p_out[i * i_nb_channels + j] = p_in[j * 256 + i];
225         }
226     }
227 }
228
229 /*****************************************************************************
230  * Duplicate: helper function to duplicate a unique channel
231  *****************************************************************************/
232 static void Duplicate( float * p_out, const float * p_in )
233 {
234     int i;
235
236     for ( i = 256; i--; )
237     {
238         *p_out++ = *p_in;
239         *p_out++ = *p_in;
240         p_in++;
241     }
242 }
243
244 /*****************************************************************************
245  * DoWork: decode an ATSC A/52 frame.
246  *****************************************************************************/
247 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
248                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
249 {
250     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
251     sample_t        i_sample_level = 1;
252     int             i_flags = p_sys->i_flags;
253     int             i_bytes_per_block = 256 * p_sys->i_nb_channels
254                       * sizeof(float);
255     int             i;
256
257     /* Do the actual decoding now. */
258     a52_frame( p_sys->p_liba52, p_in_buf->p_buffer,
259                &i_flags, &i_sample_level, 0 );
260
261     if ( (i_flags & A52_CHANNEL_MASK) != (p_sys->i_flags & A52_CHANNEL_MASK) )
262     {
263         msg_Err( p_filter,
264                  "liba52 couldn't do the requested downmix 0x%x->0x%x",
265                  p_sys->i_flags  & A52_CHANNEL_MASK,
266                  i_flags & A52_CHANNEL_MASK );
267
268         /* We do not know if the output has the same number of channels
269          * than the input, so die quietly... */
270         memset( p_out_buf->p_buffer, 0, i_bytes_per_block * 6 );
271         p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
272         p_out_buf->i_nb_bytes = i_bytes_per_block * 6;
273         return;
274     }
275
276     if( !p_sys->b_dynrng )
277     {
278         a52_dynrng( p_filter->p_sys->p_liba52, NULL, NULL );
279     }
280
281     for ( i = 0; i < 6; i++ )
282     {
283         sample_t * p_samples;
284
285         if( a52_block( p_sys->p_liba52 ) )
286         {
287             msg_Warn( p_filter, "a52_block failed for block %d", i );
288         }
289
290         p_samples = a52_samples( p_sys->p_liba52 );
291
292         if ( ((p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL1
293                || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_CHANNEL2
294                || (p_sys->i_flags & A52_CHANNEL_MASK) == A52_MONO)
295               && (p_filter->output.i_physical_channels 
296                    & (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
297         {
298             Duplicate( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
299                        p_samples );
300         }
301         else
302         {
303             /* Interleave the *$£%ù samples. */
304             Interleave( (float *)(p_out_buf->p_buffer + i * i_bytes_per_block),
305                         p_samples, p_sys->i_nb_channels );
306         }
307     }
308
309     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
310     p_out_buf->i_nb_bytes = i_bytes_per_block * 6;
311 }
312
313 /*****************************************************************************
314  * Destroy : deallocate data structures
315  *****************************************************************************/
316 static void Destroy( vlc_object_t * _p_filter )
317 {
318     aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
319     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
320
321     a52_free( p_sys->p_liba52 );
322     free( p_sys );
323 }
324