]> git.sesse.net Git - vlc/blob - plugins/a52/a52.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[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.22 2002/07/31 20:56:50 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  OpenDecoder    ( vlc_object_t * );
67 static int  RunDecoder     ( 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  * Module descriptor
78  *****************************************************************************/
79 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
80 #define DYNRNG_LONGTEXT N_( \
81     "Dynamic range compression makes the loud sounds softer, and the soft " \
82     "sounds louder, so you can more easily listen to the stream in a noisy " \
83     "environment without disturbing anyone. If you disable the dynamic range "\
84     "compression the playback will be more adapted to a movie theater or a " \
85     "listening room.")
86
87 vlc_module_begin();
88     add_category_hint( N_("Miscellaneous"), NULL );
89     add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT );
90     set_description( _("a52 ATSC A/52 aka AC-3 audio decoder module") );
91     set_capability( "decoder", 60 );
92     set_callbacks( OpenDecoder, NULL );
93 vlc_module_end();
94
95 /*****************************************************************************
96  * OpenDecoder: probe the decoder and return score
97  *****************************************************************************
98  * Tries to launch a decoder and return score so that the interface is able
99  * to choose.
100  *****************************************************************************/
101 static int OpenDecoder( vlc_object_t *p_this )
102 {
103     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
104     
105     if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ') )
106     {   
107         return VLC_EGENERIC;
108     }
109
110     p_fifo->pf_run = RunDecoder;
111     return VLC_SUCCESS;
112 }
113
114 /*****************************************************************************
115  * RunDecoder: this function is called just after the thread is created
116  *****************************************************************************/
117 static int RunDecoder( decoder_fifo_t *p_fifo )
118 {
119     a52_adec_thread_t *p_a52_adec;
120
121     /* Allocate the memory needed to store the thread's structure */
122     p_a52_adec = (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
123     if (p_a52_adec == NULL)
124     {
125         msg_Err( p_fifo, "out of memory" );
126         DecoderError( p_fifo );
127         return( -1 );
128     }
129
130     /* FIXME */
131     p_a52_adec->i_channels = 2;
132
133     /*
134      * Initialize the thread properties
135      */
136     p_a52_adec->p_aout_fifo = NULL;
137     p_a52_adec->p_fifo = p_fifo;
138
139     if( InitThread( p_a52_adec ) )
140     {
141         msg_Err( p_a52_adec->p_fifo, "could not initialize thread" );
142         DecoderError( p_fifo );
143         free( p_a52_adec );
144         return( -1 );
145     }
146
147     /* liba52 decoder thread's main loop */
148     while( !p_a52_adec->p_fifo->b_die && !p_a52_adec->p_fifo->b_error )
149     {
150
151         /* look for sync word - should be 0x0b77 */
152         RealignBits(&p_a52_adec->bit_stream);
153         while( (ShowBits( &p_a52_adec->bit_stream, 16 ) ) != 0x0b77 && 
154                (!p_a52_adec->p_fifo->b_die) && (!p_a52_adec->p_fifo->b_error))
155         {
156             RemoveBits( &p_a52_adec->bit_stream, 8 );
157         }
158
159         /* get a52 frame header */
160         GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer, 7 );
161         if( p_a52_adec->p_fifo->b_die ) break;
162
163         /* check if frame is valid and get frame info */
164         vlc_mutex_lock( &a52_lock );
165         p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
166                                                &p_a52_adec->flags,
167                                                &p_a52_adec->sample_rate,
168                                                &p_a52_adec->bit_rate );
169         vlc_mutex_unlock( &a52_lock );
170
171         if( !p_a52_adec->frame_size )
172         {
173             msg_Warn( p_a52_adec->p_fifo, "a52_syncinfo failed" );
174             continue;
175         }
176
177         if( DecodeFrame( p_a52_adec ) && !p_a52_adec->p_fifo->b_die )
178         {
179             DecoderError( p_fifo );
180             free( p_a52_adec );
181             return( -1 );
182         }
183
184     }
185
186     /* If b_error is set, the decoder thread enters the error loop */
187     if( p_a52_adec->p_fifo->b_error )
188     {
189         DecoderError( p_a52_adec->p_fifo );
190     }
191
192     /* End of the liba52 decoder thread */
193     EndThread( p_a52_adec );
194
195     return( 0 );
196 }
197
198 /*****************************************************************************
199  * InitThread: initialize data before entering main loop
200  *****************************************************************************/
201 static int InitThread( a52_adec_thread_t * p_a52_adec )
202 {
203     /* Initialize the global lock */
204     vlc_mutex_lock( p_a52_adec->p_fifo->p_vlc->p_global_lock );
205     if ( !b_liba52_initialized )
206     {
207         vlc_mutex_init( p_a52_adec->p_fifo, &a52_lock );
208         b_liba52_initialized = 1;
209     }
210     vlc_mutex_unlock( p_a52_adec->p_fifo->p_vlc->p_global_lock );
211
212     /* Initialize liba52 */
213     vlc_mutex_lock( &a52_lock );
214     p_a52_adec->p_a52_state = a52_init( 0 );
215     vlc_mutex_unlock( &a52_lock );
216     if( p_a52_adec->p_a52_state == NULL )
217     {
218         msg_Err( p_a52_adec->p_fifo, "unable to initialize liba52" );
219         return -1;
220     }
221
222     p_a52_adec->b_dynrng = config_GetInt( p_a52_adec->p_fifo, "a52-dynrng" );
223
224     /* Init the BitStream */
225     InitBitstream( &p_a52_adec->bit_stream,
226                    p_a52_adec->p_fifo,
227                    BitstreamCallback, NULL );
228
229     return( 0 );
230 }
231
232 /*****************************************************************************
233  * DecodeFrame: decodes an ATSC A/52 frame.
234  *****************************************************************************/
235 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
236 {
237     sample_t sample_level = 1;
238     byte_t   *p_buffer;
239     int i;
240
241     if( ( p_a52_adec->p_aout_fifo != NULL ) &&
242         ( p_a52_adec->p_aout_fifo->i_rate != p_a52_adec->sample_rate ) )
243     {
244         /* Make sure the output thread leaves the NextFrame() function */
245         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
246         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
247         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
248         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
249
250         p_a52_adec->p_aout_fifo = NULL;
251     }
252
253     /* Creating the audio output fifo if not created yet */
254     if( p_a52_adec->p_aout_fifo == NULL )
255     {
256         p_a52_adec->p_aout_fifo = aout_CreateFifo( p_a52_adec->p_fifo,
257                                     AOUT_FIFO_PCM, p_a52_adec->i_channels,
258                                     p_a52_adec->sample_rate,
259                                     AC3DEC_FRAME_SIZE * p_a52_adec->i_channels,
260                                     NULL );
261
262         if ( p_a52_adec->p_aout_fifo == NULL )
263         { 
264             return( -1 );
265         }
266     }
267
268     /* Set the Presentation Time Stamp */
269     CurrentPTS( &p_a52_adec->bit_stream,
270                 &p_a52_adec->p_aout_fifo->date[
271                     p_a52_adec->p_aout_fifo->i_end_frame],
272                 NULL );
273
274     if( !p_a52_adec->p_aout_fifo->date[
275             p_a52_adec->p_aout_fifo->i_end_frame] )
276     {
277         p_a52_adec->p_aout_fifo->date[
278             p_a52_adec->p_aout_fifo->i_end_frame] = LAST_MDATE;
279     }
280
281
282
283     p_buffer = ((byte_t *)p_a52_adec->p_aout_fifo->buffer) +
284         ( p_a52_adec->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE *
285           p_a52_adec->i_channels * sizeof(s16) );
286
287     /* FIXME */
288     p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
289
290     /* Get the complete frame */
291     GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer + 7,
292               p_a52_adec->frame_size - 7 );
293     if( p_a52_adec->p_fifo->b_die ) return( -1 );
294
295     /* do the actual decoding now */
296     vlc_mutex_lock( &a52_lock );
297     a52_frame( p_a52_adec->p_a52_state, p_a52_adec->p_frame_buffer,
298                &p_a52_adec->flags, &sample_level, 384 );
299
300     if( !p_a52_adec->b_dynrng )
301         a52_dynrng( p_a52_adec->p_a52_state, NULL, NULL );
302
303     for( i = 0; i < 6; i++ )
304     {
305         if( a52_block( p_a52_adec->p_a52_state ) )
306         {
307             msg_Warn( p_a52_adec->p_fifo, "a52_block failed for block %i", i );
308         }
309
310         float2s16_2( a52_samples( p_a52_adec->p_a52_state ),
311                      ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
312     }
313     vlc_mutex_unlock( &a52_lock );
314
315
316     vlc_mutex_lock( &p_a52_adec->p_aout_fifo->data_lock );
317     p_a52_adec->p_aout_fifo->i_end_frame = 
318       (p_a52_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
319     vlc_cond_signal (&p_a52_adec->p_aout_fifo->data_wait);
320     vlc_mutex_unlock (&p_a52_adec->p_aout_fifo->data_lock);
321
322     return 0;
323 }
324
325 /*****************************************************************************
326  * EndThread : liba52 decoder thread destruction
327  *****************************************************************************/
328 static void EndThread (a52_adec_thread_t *p_a52_adec)
329 {
330     /* If the audio output fifo was created, we destroy it */
331     if (p_a52_adec->p_aout_fifo != NULL)
332     {
333         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
334
335         /* Make sure the output thread leaves the NextFrame() function */
336         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
337         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
338         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
339     }
340
341     vlc_mutex_lock( &a52_lock );
342     a52_free( p_a52_adec->p_a52_state );
343     vlc_mutex_unlock( &a52_lock );
344     free( p_a52_adec );
345
346 }
347
348 /*****************************************************************************
349  * float2s16_2 : converts floats to ints using a trick based on the IEEE
350  *               floating-point format
351  *****************************************************************************/
352 static inline int16_t convert (int32_t i)
353 {
354     if (i > 0x43c07fff)
355         return 32767;
356     else if (i < 0x43bf8000)
357         return -32768;
358     else
359         return i - 0x43c00000;
360 }
361
362 static void float2s16_2 (float * _f, int16_t * s16)
363 {
364     int i;
365     int32_t * f = (int32_t *) _f;
366
367     for (i = 0; i < 256; i++) {
368       s16[2*i] = convert (f[i]);
369         s16[2*i+1] = convert (f[i+256]);
370     }
371 }
372
373 /*****************************************************************************
374  * BitstreamCallback: Import parameters from the new data/PES packet
375  *****************************************************************************
376  * This function is called by input's NextDataPacket.
377  *****************************************************************************/
378 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
379                                 vlc_bool_t b_new_pes )
380 {
381     if( b_new_pes )
382     {
383         /* Drop special AC3 header */
384 /*        p_bit_stream->p_byte += 3; */
385     }
386 }