]> git.sesse.net Git - vlc/blob - plugins/a52/a52.c
* compilation fix (include stdint.h)
[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.2 2002/02/13 21:54:44 gbazin 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 <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>                                              /* strdup() */
31 #include <stdint.h>
32
33 #include <videolan/vlc.h>
34
35 #include "audio_output.h"
36
37 #include "stream_control.h"
38 #include "input_ext-dec.h"
39
40 #include "debug.h"
41
42 #include <a52dec/a52.h>                                /* liba52 header file */
43 #include "a52.h"
44
45 #define AC3DEC_FRAME_SIZE 1536 
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int  decoder_Probe  ( probedata_t * );
51 static int  decoder_Run    ( decoder_config_t * );
52 static int  DecodeFrame    ( a52_adec_thread_t * );
53 static int  InitThread     ( a52_adec_thread_t * );
54 static void EndThread      ( a52_adec_thread_t * );
55
56 static void               BitstreamCallback ( bit_stream_t *, boolean_t );
57 static void               float2s16_2       ( float *, int16_t * );
58 static __inline__ int16_t convert   ( int32_t );
59
60 /*****************************************************************************
61  * Capabilities
62  *****************************************************************************/
63 void _M( adec_getfunctions )( function_list_t * p_function_list )
64 {
65     p_function_list->pf_probe = decoder_Probe;
66     p_function_list->functions.dec.pf_run = decoder_Run;
67 }
68
69 /*****************************************************************************
70  * Build configuration tree.
71  *****************************************************************************/
72 MODULE_CONFIG_START
73 MODULE_CONFIG_STOP
74
75 MODULE_INIT_START
76     SET_DESCRIPTION( "a52 ATSC A/52 aka AC-3 audio decoder plugin" )
77     ADD_CAPABILITY( DECODER, 40 )
78     ADD_SHORTCUT( "a52" )
79 MODULE_INIT_STOP
80
81 MODULE_ACTIVATE_START
82     _M( adec_getfunctions )( &p_module->p_functions->dec );
83 MODULE_ACTIVATE_STOP
84
85 MODULE_DEACTIVATE_START
86 MODULE_DEACTIVATE_STOP
87
88 /*****************************************************************************
89  * decoder_Probe: probe the decoder and return score
90  *****************************************************************************
91  * Tries to launch a decoder and return score so that the interface is able
92  * to choose.
93  *****************************************************************************/
94 static int decoder_Probe( probedata_t *p_data )
95 {
96     return ( p_data->i_type == AC3_AUDIO_ES );
97 }
98
99 /*****************************************************************************
100  * decoder_Run: this function is called just after the thread is created
101  *****************************************************************************/
102 static int decoder_Run ( decoder_config_t *p_config )
103 {
104     a52_adec_thread_t *p_a52_adec;
105
106     /* Allocate the memory needed to store the thread's structure */
107     p_a52_adec =
108         (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
109     if (p_a52_adec == NULL)
110     {
111         intf_ErrMsg ( "a52 error: not enough memory "
112                       "for decoder_Run() to allocate p_a52_adec" );
113         DecoderError( p_config->p_decoder_fifo );
114         return( -1 );
115     }
116
117     /* FIXME */
118     p_a52_adec->i_channels = 2;
119
120     /*
121      * Initialize the thread properties
122      */
123     p_a52_adec->p_aout_fifo = NULL;
124     p_a52_adec->p_config = p_config;
125     p_a52_adec->p_fifo = p_a52_adec->p_config->p_decoder_fifo;
126     if( InitThread( p_a52_adec ) )
127     {
128         intf_ErrMsg( "a52 error: could not initialize thread" );
129         DecoderError( p_config->p_decoder_fifo );
130         free( p_a52_adec );
131         return( -1 );
132     }
133
134     /* liba52 decoder thread's main loop */
135     while( !p_a52_adec->p_fifo->b_die && !p_a52_adec->p_fifo->b_error )
136     {
137
138         /* look for sync word - should be 0x0b77 */
139         RealignBits(&p_a52_adec->bit_stream);
140         while( (ShowBits( &p_a52_adec->bit_stream, 16 ) ) != 0x0b77 && 
141                (!p_a52_adec->p_fifo->b_die) && (!p_a52_adec->p_fifo->b_error))
142         {
143             RemoveBits( &p_a52_adec->bit_stream, 8 );
144         }
145
146         /* get a52 frame header */
147         GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer, 7 );
148
149         /* check if frame is valid and get frame info */
150         p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
151                                                &p_a52_adec->flags,
152                                                &p_a52_adec->sample_rate,
153                                                &p_a52_adec->bit_rate );
154
155         if( !p_a52_adec->frame_size )
156         {
157             intf_WarnMsg( 3, "a52: a52_syncinfo failed" );
158             continue;
159         }
160
161         if( DecodeFrame( p_a52_adec ) )
162         {
163             DecoderError( p_config->p_decoder_fifo );
164             free( p_a52_adec );
165             return( -1 );
166         }
167
168     }
169
170     /* If b_error is set, the decoder thread enters the error loop */
171     if( p_a52_adec->p_fifo->b_error )
172     {
173         DecoderError( p_a52_adec->p_fifo );
174     }
175
176     /* End of the liba52 decoder thread */
177     EndThread( p_a52_adec );
178
179     return( 0 );
180 }
181
182 /*****************************************************************************
183  * InitThread: initialize data before entering main loop
184  *****************************************************************************/
185 static int InitThread( a52_adec_thread_t * p_a52_adec )
186 {
187     intf_WarnMsg( 3, "a52: InitThread" );
188
189     /* Initialize liba52 */
190     p_a52_adec->p_decoded_samples = a52_init( 0 );
191     if( p_a52_adec->p_decoded_samples == NULL )
192     {
193         intf_ErrMsg ( "a52 error: InitThread() unable to initialize "
194                       "liba52" );
195         return -1;
196     }
197
198     /* Init the BitStream */
199     InitBitstream( &p_a52_adec->bit_stream,
200                    p_a52_adec->p_fifo,
201                    BitstreamCallback, NULL );
202
203     return( 0 );
204 }
205
206 /*****************************************************************************
207  * DecodeFrame: decodes an ATSC A/52 frame.
208  *****************************************************************************/
209 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
210 {
211     sample_t sample_level = 1;
212     byte_t   *p_buffer;
213     int i;
214
215     if( ( p_a52_adec->p_aout_fifo != NULL ) &&
216         ( p_a52_adec->p_aout_fifo->l_rate != p_a52_adec->sample_rate ) )
217     {
218         /* Make sure the output thread leaves the NextFrame() function */
219         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
220         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
221         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
222         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
223
224         p_a52_adec->p_aout_fifo = NULL;
225     }
226
227     /* Creating the audio output fifo if not created yet */
228     if (p_a52_adec->p_aout_fifo == NULL )
229     {
230         p_a52_adec->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 
231                                     p_a52_adec->i_channels,
232                                     p_a52_adec->sample_rate, 0,
233                                     AC3DEC_FRAME_SIZE * p_a52_adec->i_channels,
234                                     NULL );
235
236         if ( p_a52_adec->p_aout_fifo == NULL )
237         { 
238             return( -1 );
239         }
240     }
241
242     /* Set the Presentation Time Stamp */
243     CurrentPTS( &p_a52_adec->bit_stream,
244                 &p_a52_adec->p_aout_fifo->date[
245                     p_a52_adec->p_aout_fifo->l_end_frame],
246                 NULL );
247
248     if( !p_a52_adec->p_aout_fifo->date[
249             p_a52_adec->p_aout_fifo->l_end_frame] )
250     {
251         p_a52_adec->p_aout_fifo->date[
252             p_a52_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
253     }
254
255
256
257     p_buffer = ((byte_t *)p_a52_adec->p_aout_fifo->buffer) +
258         ( p_a52_adec->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE *
259           p_a52_adec->i_channels * sizeof(s16) );
260
261     /* FIXME */
262     p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
263
264     /* Get the complete frame */
265     GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer + 7,
266               p_a52_adec->frame_size - 7 );
267
268     /* do the actual decoding now */
269     a52_frame( &p_a52_adec->a52_state, p_a52_adec->p_frame_buffer,
270                &p_a52_adec->flags, &sample_level, 384 );
271
272     for( i = 0; i < 6; i++ )
273     {
274         if( a52_block(&p_a52_adec->a52_state, p_a52_adec->p_decoded_samples) )
275             intf_WarnMsg( 5, "a52: a52_block failed for block %i", i );
276
277         float2s16_2( p_a52_adec->p_decoded_samples,
278                      ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
279     }
280
281
282     vlc_mutex_lock( &p_a52_adec->p_aout_fifo->data_lock );
283     p_a52_adec->p_aout_fifo->l_end_frame = 
284       (p_a52_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
285     vlc_cond_signal (&p_a52_adec->p_aout_fifo->data_wait);
286     vlc_mutex_unlock (&p_a52_adec->p_aout_fifo->data_lock);
287
288     return 0;
289 }
290
291 /*****************************************************************************
292  * EndThread : liba52 decoder thread destruction
293  *****************************************************************************/
294 static void EndThread (a52_adec_thread_t *p_a52_adec)
295 {
296     intf_WarnMsg ( 3, "a52: EndThread" );
297
298     /* If the audio output fifo was created, we destroy it */
299     if (p_a52_adec->p_aout_fifo != NULL)
300     {
301         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
302
303         /* Make sure the output thread leaves the NextFrame() function */
304         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
305         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
306         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
307     }
308
309     free( p_a52_adec );
310
311 }
312
313 /*****************************************************************************
314  * float2s16_2 : converts floats to ints using a trick based on the IEEE
315  *               floating-point format
316  *****************************************************************************/
317 static __inline__ int16_t convert (int32_t i)
318 {
319     if (i > 0x43c07fff)
320         return 32767;
321     else if (i < 0x43bf8000)
322         return -32768;
323     else
324         return i - 0x43c00000;
325 }
326
327 static void float2s16_2 (float * _f, int16_t * s16)
328 {
329     int i;
330     int32_t * f = (int32_t *) _f;
331
332     for (i = 0; i < 256; i++) {
333       s16[2*i] = convert (f[i]);
334         s16[2*i+1] = convert (f[i+256]);
335     }
336 }
337
338 /*****************************************************************************
339  * BitstreamCallback: Import parameters from the new data/PES packet
340  *****************************************************************************
341  * This function is called by input's NextDataPacket.
342  *****************************************************************************/
343 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
344                                 boolean_t b_new_pes )
345 {
346     if( b_new_pes )
347     {
348         /* Drop special AC3 header */
349         p_bit_stream->p_byte += 3;
350     }
351 }