]> git.sesse.net Git - vlc/blob - plugins/a52/a52.c
b08c430dddab25b5c9c399e3c44b0504fbba9ccf
[vlc] / plugins / a52 / a52.c
1 /*****************************************************************************
2  * a52.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 VideoLAN
7  * $Id: a52.c,v 1.21 2002/07/23 00:39:16 sam Exp $
8  *
9  * Authors: Gildas Bazin <gbazin@netcourrier.com>
10  *      
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/aout.h>
31 #include <vlc/decoder.h>
32
33 #include <stdlib.h>                                      /* malloc(), free() */
34 #include <string.h>                                              /* strdup() */
35 #ifdef HAVE_STDINT_H
36 #   include <stdint.h>                                         /* int16_t .. */
37 #elif HAVE_INTTYPES_H
38 #   include <inttypes.h>                                       /* int16_t .. */
39 #endif
40
41 #ifdef USE_A52DEC_TREE                                 /* liba52 header file */
42 #   include "include/a52.h"
43 #else
44 #   include "a52dec/a52.h"
45 #endif
46
47 #include "a52.h"
48
49 #define AC3DEC_FRAME_SIZE 1536 
50
51 /*
52  * Global lock for accessing liba52 functions.
53  * Currently, liba52 isn't thread-safe. So to prevent two threads from
54  * using liba52 at the same time, we have to set up a global lock.
55  * I know static variables aren't a good idea in multi-threaded programs,
56  * but believe me, this is the way to go.
57  * --Meuuh 2002-07-19
58  */
59 static vlc_mutex_t a52_lock;
60 static vlc_bool_t  b_liba52_initialized = 0;
61
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int  decoder_Probe  ( vlc_fourcc_t * );
67 static int  decoder_Run    ( decoder_fifo_t * );
68 static int  DecodeFrame    ( a52_adec_thread_t * );
69 static int  InitThread     ( a52_adec_thread_t * );
70 static void EndThread      ( a52_adec_thread_t * );
71
72 static void               BitstreamCallback ( bit_stream_t *, vlc_bool_t );
73 static void               float2s16_2       ( float *, int16_t * );
74 static inline int16_t     convert   ( int32_t );
75
76 /*****************************************************************************
77  * Capabilities
78  *****************************************************************************/
79 void _M( adec_getfunctions )( function_list_t * p_function_list )
80 {
81     p_function_list->functions.dec.pf_probe = decoder_Probe;
82     p_function_list->functions.dec.pf_run   = decoder_Run;
83 }
84
85 /*****************************************************************************
86  * Build configuration structure.
87  *****************************************************************************/
88 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
89 #define DYNRNG_LONGTEXT N_( \
90     "Dynamic range compression makes the loud sounds softer, and the soft " \
91     "sounds louder, so you can more easily listen to the stream in a noisy " \
92     "environment without disturbing anyone. If you disable the dynamic range "\
93     "compression the playback will be more adapted to a movie theater or a " \
94     "listening room.")
95
96 MODULE_CONFIG_START
97 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
98 ADD_BOOL    ( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT )
99 MODULE_CONFIG_STOP
100
101 MODULE_INIT_START
102     SET_DESCRIPTION( _("a52 ATSC A/52 aka AC-3 audio decoder module") )
103     ADD_CAPABILITY( DECODER, 60 )
104 MODULE_INIT_STOP
105
106 MODULE_ACTIVATE_START
107     _M( adec_getfunctions )( &p_module->p_functions->dec );
108 MODULE_ACTIVATE_STOP
109
110 MODULE_DEACTIVATE_START
111 MODULE_DEACTIVATE_STOP
112
113 /*****************************************************************************
114  * decoder_Probe: probe the decoder and return score
115  *****************************************************************************
116  * Tries to launch a decoder and return score so that the interface is able
117  * to choose.
118  *****************************************************************************/
119 static int decoder_Probe( vlc_fourcc_t *pi_type )
120 {
121     return *pi_type == VLC_FOURCC('a','5','2',' ') ? 0 : -1;
122 }
123
124 /*****************************************************************************
125  * decoder_Run: this function is called just after the thread is created
126  *****************************************************************************/
127 static int decoder_Run ( decoder_fifo_t *p_fifo )
128 {
129     a52_adec_thread_t *p_a52_adec;
130
131     /* Allocate the memory needed to store the thread's structure */
132     p_a52_adec = (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
133     if (p_a52_adec == NULL)
134     {
135         msg_Err( p_fifo, "out of memory" );
136         DecoderError( p_fifo );
137         return( -1 );
138     }
139
140     /* FIXME */
141     p_a52_adec->i_channels = 2;
142
143     /*
144      * Initialize the thread properties
145      */
146     p_a52_adec->p_aout_fifo = NULL;
147     p_a52_adec->p_fifo = p_fifo;
148
149     if( InitThread( p_a52_adec ) )
150     {
151         msg_Err( p_a52_adec->p_fifo, "could not initialize thread" );
152         DecoderError( p_fifo );
153         free( p_a52_adec );
154         return( -1 );
155     }
156
157     /* liba52 decoder thread's main loop */
158     while( !p_a52_adec->p_fifo->b_die && !p_a52_adec->p_fifo->b_error )
159     {
160
161         /* look for sync word - should be 0x0b77 */
162         RealignBits(&p_a52_adec->bit_stream);
163         while( (ShowBits( &p_a52_adec->bit_stream, 16 ) ) != 0x0b77 && 
164                (!p_a52_adec->p_fifo->b_die) && (!p_a52_adec->p_fifo->b_error))
165         {
166             RemoveBits( &p_a52_adec->bit_stream, 8 );
167         }
168
169         /* get a52 frame header */
170         GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer, 7 );
171         if( p_a52_adec->p_fifo->b_die ) break;
172
173         /* check if frame is valid and get frame info */
174         vlc_mutex_lock( &a52_lock );
175         p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
176                                                &p_a52_adec->flags,
177                                                &p_a52_adec->sample_rate,
178                                                &p_a52_adec->bit_rate );
179         vlc_mutex_unlock( &a52_lock );
180
181         if( !p_a52_adec->frame_size )
182         {
183             msg_Warn( p_a52_adec->p_fifo, "a52_syncinfo failed" );
184             continue;
185         }
186
187         if( DecodeFrame( p_a52_adec ) && !p_a52_adec->p_fifo->b_die )
188         {
189             DecoderError( p_fifo );
190             free( p_a52_adec );
191             return( -1 );
192         }
193
194     }
195
196     /* If b_error is set, the decoder thread enters the error loop */
197     if( p_a52_adec->p_fifo->b_error )
198     {
199         DecoderError( p_a52_adec->p_fifo );
200     }
201
202     /* End of the liba52 decoder thread */
203     EndThread( p_a52_adec );
204
205     return( 0 );
206 }
207
208 /*****************************************************************************
209  * InitThread: initialize data before entering main loop
210  *****************************************************************************/
211 static int InitThread( a52_adec_thread_t * p_a52_adec )
212 {
213     /* Initialize the global lock */
214     vlc_mutex_lock( p_a52_adec->p_fifo->p_vlc->p_global_lock );
215     if ( !b_liba52_initialized )
216     {
217         vlc_mutex_init( p_a52_adec->p_fifo, &a52_lock );
218         b_liba52_initialized = 1;
219     }
220     vlc_mutex_unlock( p_a52_adec->p_fifo->p_vlc->p_global_lock );
221
222     /* Initialize liba52 */
223     vlc_mutex_lock( &a52_lock );
224     p_a52_adec->p_a52_state = a52_init( 0 );
225     vlc_mutex_unlock( &a52_lock );
226     if( p_a52_adec->p_a52_state == NULL )
227     {
228         msg_Err( p_a52_adec->p_fifo, "unable to initialize liba52" );
229         return -1;
230     }
231
232     p_a52_adec->b_dynrng = config_GetInt( p_a52_adec->p_fifo, "a52-dynrng" );
233
234     /* Init the BitStream */
235     InitBitstream( &p_a52_adec->bit_stream,
236                    p_a52_adec->p_fifo,
237                    BitstreamCallback, NULL );
238
239     return( 0 );
240 }
241
242 /*****************************************************************************
243  * DecodeFrame: decodes an ATSC A/52 frame.
244  *****************************************************************************/
245 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
246 {
247     sample_t sample_level = 1;
248     byte_t   *p_buffer;
249     int i;
250
251     if( ( p_a52_adec->p_aout_fifo != NULL ) &&
252         ( p_a52_adec->p_aout_fifo->i_rate != p_a52_adec->sample_rate ) )
253     {
254         /* Make sure the output thread leaves the NextFrame() function */
255         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
256         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
257         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
258         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
259
260         p_a52_adec->p_aout_fifo = NULL;
261     }
262
263     /* Creating the audio output fifo if not created yet */
264     if( p_a52_adec->p_aout_fifo == NULL )
265     {
266         p_a52_adec->p_aout_fifo = aout_CreateFifo( p_a52_adec->p_fifo,
267                                     AOUT_FIFO_PCM, p_a52_adec->i_channels,
268                                     p_a52_adec->sample_rate,
269                                     AC3DEC_FRAME_SIZE * p_a52_adec->i_channels,
270                                     NULL );
271
272         if ( p_a52_adec->p_aout_fifo == NULL )
273         { 
274             return( -1 );
275         }
276     }
277
278     /* Set the Presentation Time Stamp */
279     CurrentPTS( &p_a52_adec->bit_stream,
280                 &p_a52_adec->p_aout_fifo->date[
281                     p_a52_adec->p_aout_fifo->i_end_frame],
282                 NULL );
283
284     if( !p_a52_adec->p_aout_fifo->date[
285             p_a52_adec->p_aout_fifo->i_end_frame] )
286     {
287         p_a52_adec->p_aout_fifo->date[
288             p_a52_adec->p_aout_fifo->i_end_frame] = LAST_MDATE;
289     }
290
291
292
293     p_buffer = ((byte_t *)p_a52_adec->p_aout_fifo->buffer) +
294         ( p_a52_adec->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE *
295           p_a52_adec->i_channels * sizeof(s16) );
296
297     /* FIXME */
298     p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
299
300     /* Get the complete frame */
301     GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer + 7,
302               p_a52_adec->frame_size - 7 );
303     if( p_a52_adec->p_fifo->b_die ) return( -1 );
304
305     /* do the actual decoding now */
306     vlc_mutex_lock( &a52_lock );
307     a52_frame( p_a52_adec->p_a52_state, p_a52_adec->p_frame_buffer,
308                &p_a52_adec->flags, &sample_level, 384 );
309
310     if( !p_a52_adec->b_dynrng )
311         a52_dynrng( p_a52_adec->p_a52_state, NULL, NULL );
312
313     for( i = 0; i < 6; i++ )
314     {
315         if( a52_block( p_a52_adec->p_a52_state ) )
316         {
317             msg_Warn( p_a52_adec->p_fifo, "a52_block failed for block %i", i );
318         }
319
320         float2s16_2( a52_samples( p_a52_adec->p_a52_state ),
321                      ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
322     }
323     vlc_mutex_unlock( &a52_lock );
324
325
326     vlc_mutex_lock( &p_a52_adec->p_aout_fifo->data_lock );
327     p_a52_adec->p_aout_fifo->i_end_frame = 
328       (p_a52_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
329     vlc_cond_signal (&p_a52_adec->p_aout_fifo->data_wait);
330     vlc_mutex_unlock (&p_a52_adec->p_aout_fifo->data_lock);
331
332     return 0;
333 }
334
335 /*****************************************************************************
336  * EndThread : liba52 decoder thread destruction
337  *****************************************************************************/
338 static void EndThread (a52_adec_thread_t *p_a52_adec)
339 {
340     /* If the audio output fifo was created, we destroy it */
341     if (p_a52_adec->p_aout_fifo != NULL)
342     {
343         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
344
345         /* Make sure the output thread leaves the NextFrame() function */
346         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
347         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
348         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
349     }
350
351     vlc_mutex_lock( &a52_lock );
352     a52_free( p_a52_adec->p_a52_state );
353     vlc_mutex_unlock( &a52_lock );
354     free( p_a52_adec );
355
356 }
357
358 /*****************************************************************************
359  * float2s16_2 : converts floats to ints using a trick based on the IEEE
360  *               floating-point format
361  *****************************************************************************/
362 static inline int16_t convert (int32_t i)
363 {
364     if (i > 0x43c07fff)
365         return 32767;
366     else if (i < 0x43bf8000)
367         return -32768;
368     else
369         return i - 0x43c00000;
370 }
371
372 static void float2s16_2 (float * _f, int16_t * s16)
373 {
374     int i;
375     int32_t * f = (int32_t *) _f;
376
377     for (i = 0; i < 256; i++) {
378       s16[2*i] = convert (f[i]);
379         s16[2*i+1] = convert (f[i+256]);
380     }
381 }
382
383 /*****************************************************************************
384  * BitstreamCallback: Import parameters from the new data/PES packet
385  *****************************************************************************
386  * This function is called by input's NextDataPacket.
387  *****************************************************************************/
388 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
389                                 vlc_bool_t b_new_pes )
390 {
391     if( b_new_pes )
392     {
393         /* Drop special AC3 header */
394 /*        p_bit_stream->p_byte += 3; */
395     }
396 }