1e52142e464527770090c3a3b8d46c7013fbb7eb
[vlc.git] / plugins / ac3_adec / ac3_adec.c
1 /*****************************************************************************
2  * ac3_adec.c: ac3 decoder module main file
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: ac3_adec.c,v 1.12 2001/12/31 04:53:33 sam Exp $
6  *
7  * Authors: Michel Lespinasse <walken@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                              /* memset() */
29
30 #include <videolan/vlc.h>
31
32 #ifdef HAVE_UNISTD_H
33 #   include <unistd.h>                                           /* getpid() */
34 #endif
35
36 #include "audio_output.h"
37
38 #include "stream_control.h"
39 #include "input_ext-dec.h"
40 #include "input_ext-intf.h"                                /* MPEG?_AUDIO_ES */
41
42 #include "ac3_imdct.h"
43 #include "ac3_downmix.h"
44 #include "ac3_decoder.h"
45 #include "ac3_adec.h"
46
47 #define AC3DEC_FRAME_SIZE (2*1536) 
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  decoder_Probe     ( probedata_t * );
53 static int  decoder_Run       ( decoder_config_t * );
54 static int  InitThread        ( ac3dec_thread_t * p_adec );
55 static void EndThread         ( ac3dec_thread_t * p_adec );
56 static void BitstreamCallback ( bit_stream_t *p_bit_stream,
57                                 boolean_t b_new_pes );
58
59 /*****************************************************************************
60  * Capabilities
61  *****************************************************************************/
62 void _M( adec_getfunctions )( function_list_t * p_function_list )
63 {
64     p_function_list->pf_probe = decoder_Probe;
65     p_function_list->functions.dec.pf_run = decoder_Run;
66 }
67
68 /*****************************************************************************
69  * Build configuration tree.
70  *****************************************************************************/
71 MODULE_CONFIG_START
72 MODULE_CONFIG_STOP
73
74 MODULE_INIT_START
75     SET_DESCRIPTION( "software AC3 decoder" )
76     ADD_CAPABILITY( DECODER, 50 )
77 MODULE_INIT_STOP
78
79 MODULE_ACTIVATE_START
80     _M( adec_getfunctions )( &p_module->p_functions->dec );
81 MODULE_ACTIVATE_STOP
82
83 MODULE_DEACTIVATE_START
84 MODULE_DEACTIVATE_STOP
85
86
87 /*****************************************************************************
88  * decoder_Probe: probe the decoder and return score
89  *****************************************************************************
90  * Tries to launch a decoder and return score so that the interface is able 
91  * to chose.
92  *****************************************************************************/
93 static int decoder_Probe( probedata_t *p_data )
94 {
95     return ( p_data->i_type == AC3_AUDIO_ES ) ? 50 : 0;
96 }
97
98 /*****************************************************************************
99  * decoder_Run: this function is called just after the thread is created
100  *****************************************************************************/
101 static int decoder_Run ( decoder_config_t * p_config )
102 {
103     ac3dec_thread_t *   p_ac3thread;
104     int sync;
105
106     intf_DbgMsg( "ac3_adec debug: ac3_adec thread launched, initializing" );
107
108     /* Allocate the memory needed to store the thread's structure */
109     p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
110
111     if( p_ac3thread == NULL )
112     {
113         intf_ErrMsg ( "ac3_adec error: not enough memory "
114                       "for decoder_Run() to allocate p_ac3thread" );
115         DecoderError( p_config->p_decoder_fifo );
116         return( -1 );
117     }
118    
119     /*
120      * Initialize the thread properties
121      */
122     p_ac3thread->p_config = p_config;
123     if( InitThread( p_ac3thread ) )
124     {
125         intf_ErrMsg( "ac3_adec error: could not initialize thread" );
126         DecoderError( p_config->p_decoder_fifo );
127         free( p_ac3thread );
128         return( -1 );
129     }
130
131     sync = 0;
132     p_ac3thread->sync_ptr = 0;
133
134     /* ac3 decoder thread's main loop */
135     /* FIXME : do we have enough room to store the decoded frames ?? */
136     while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
137     {
138         s16 * buffer;
139         ac3_sync_info_t sync_info;
140         int ptr;
141
142         if (!sync) {
143             do {
144                 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
145             } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
146                     && (!p_ac3thread->p_fifo->b_error));
147             
148             ptr = p_ac3thread->sync_ptr;
149
150             while(ptr-- && (!p_ac3thread->p_fifo->b_die)
151                 && (!p_ac3thread->p_fifo->b_error))
152             {
153                 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
154             }
155                         
156             /* we are in sync now */
157             sync = 1;
158         }
159
160         if (p_ac3thread->p_fifo->p_first->i_pts)
161         {
162             p_ac3thread->p_aout_fifo->date[
163                 p_ac3thread->p_aout_fifo->l_end_frame] =
164                 p_ac3thread->p_fifo->p_first->i_pts;
165             p_ac3thread->p_fifo->p_first->i_pts = 0;
166         } else {
167             p_ac3thread->p_aout_fifo->date[
168                 p_ac3thread->p_aout_fifo->l_end_frame] =
169                 LAST_MDATE;
170         }
171     
172         if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
173         {
174             sync = 0;
175             goto bad_frame;
176         }
177
178         p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
179
180         buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
181             (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
182
183         if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
184         {
185             sync = 0;
186             goto bad_frame;
187         }
188         
189         vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
190         p_ac3thread->p_aout_fifo->l_end_frame = 
191             (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
192         vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
193         vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
194
195         bad_frame:
196             RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
197     }
198
199     /* If b_error is set, the ac3 decoder thread enters the error loop */
200     if (p_ac3thread->p_fifo->b_error)
201     {
202         DecoderError( p_ac3thread->p_fifo );
203     }
204
205     /* End of the ac3 decoder thread */
206     EndThread (p_ac3thread);
207
208     free( p_ac3thread );
209
210     return( 0 );
211 }
212
213
214 /*****************************************************************************
215  * InitThread: initialize data before entering main loop
216  *****************************************************************************/
217 static int InitThread( ac3dec_thread_t * p_ac3thread )
218 {
219     /*
220      * Thread properties 
221      */
222     p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
223     p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
224
225     /*
226      * Choose the best downmix module
227      */
228 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
229     DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX,
230                                main_GetPszVariable( DOWNMIX_METHOD_VAR, NULL ),
231                                NULL );
232
233     if( DOWNMIX.p_module == NULL )
234     {
235         intf_ErrMsg( "ac3dec error: no suitable downmix module" );
236         free( p_ac3thread->ac3_decoder );
237         return( -1 );
238     }
239
240 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
241     DOWNMIX.pf_downmix_3f_2r_to_2ch     = F.pf_downmix_3f_2r_to_2ch;
242     DOWNMIX.pf_downmix_2f_2r_to_2ch     = F.pf_downmix_2f_2r_to_2ch;
243     DOWNMIX.pf_downmix_3f_1r_to_2ch     = F.pf_downmix_3f_1r_to_2ch;
244     DOWNMIX.pf_downmix_2f_1r_to_2ch     = F.pf_downmix_2f_1r_to_2ch;
245     DOWNMIX.pf_downmix_3f_0r_to_2ch     = F.pf_downmix_3f_0r_to_2ch;
246     DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
247     DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
248 #undef F
249 #undef DOWNMIX
250
251     /*
252      * Choose the best IMDCT module
253      */
254     p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
255     
256 #define IMDCT p_ac3thread->ac3_decoder->imdct
257     IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT,
258                                main_GetPszVariable( IMDCT_METHOD_VAR, NULL ),
259                                NULL );
260
261     if( IMDCT->p_module == NULL )
262     {
263         intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
264         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
265         free( p_ac3thread->ac3_decoder->imdct );
266         free( p_ac3thread->ac3_decoder );
267         return( -1 );
268     }
269
270 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
271     IMDCT->pf_imdct_init    = F.pf_imdct_init;
272     IMDCT->pf_imdct_256     = F.pf_imdct_256;
273     IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
274     IMDCT->pf_imdct_512     = F.pf_imdct_512;
275     IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
276 #undef F
277
278     /* Initialize the ac3 decoder structures */
279 #define p_dec p_ac3thread->ac3_decoder
280 #if defined( __MINGW32__ )
281     p_dec->samples_back = memalign( 16, 6 * 256 * sizeof(float) + 15 );
282     p_dec->samples = (float *)
283                      (((unsigned long) p_dec->samples_back + 15 ) & ~0xFUL);
284 #else
285     p_dec->samples = memalign( 16, 6 * 256 * sizeof(float) );
286 #endif
287 #undef p_dec
288
289     IMDCT->buf    = memalign( 16, N/4 * sizeof(complex_t) );
290     IMDCT->delay  = memalign( 16, 6 * 256 * sizeof(float) );
291     IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
292     IMDCT->xcos1  = memalign( 16, N/4 * sizeof(float) );
293     IMDCT->xsin1  = memalign( 16, N/4 * sizeof(float) );
294     IMDCT->xcos2  = memalign( 16, N/8 * sizeof(float) );
295     IMDCT->xsin2  = memalign( 16, N/8 * sizeof(float) );
296     IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
297     IMDCT->w_1    = memalign( 16, 1  * sizeof(complex_t) );
298     IMDCT->w_2    = memalign( 16, 2  * sizeof(complex_t) );
299     IMDCT->w_4    = memalign( 16, 4  * sizeof(complex_t) );
300     IMDCT->w_8    = memalign( 16, 8  * sizeof(complex_t) );
301     IMDCT->w_16   = memalign( 16, 16 * sizeof(complex_t) );
302     IMDCT->w_32   = memalign( 16, 32 * sizeof(complex_t) );
303     IMDCT->w_64   = memalign( 16, 64 * sizeof(complex_t) );
304
305     ac3_init( p_ac3thread->ac3_decoder );
306
307     /*
308      * Initialize the output properties
309      */
310     p_ac3thread->p_aout_fifo = NULL;
311
312     intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized", 
313                   p_ac3thread );
314
315     /* Creating the audio output fifo */
316     p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
317                                                 AC3DEC_FRAME_SIZE, NULL  );
318     if ( p_ac3thread->p_aout_fifo == NULL )
319     {
320         free( IMDCT->w_1 );
321         free( IMDCT->w_64 );
322         free( IMDCT->w_32 );
323         free( IMDCT->w_16 );
324         free( IMDCT->w_8 );
325         free( IMDCT->w_4 );
326         free( IMDCT->w_2 );
327         free( IMDCT->xcos_sin_sse );
328         free( IMDCT->xsin2 );
329         free( IMDCT->xcos2 );
330         free( IMDCT->xsin1 );
331         free( IMDCT->xcos1 );
332         free( IMDCT->delay1 );
333         free( IMDCT->delay );
334         free( IMDCT->buf );
335 #undef IMDCT
336
337 #if defined( __MINGW32__ )
338         free( p_ac3thread->ac3_decoder->samples_back );
339 #else
340         free( p_ac3thread->ac3_decoder->samples );
341 #endif
342
343         module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
344         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
345
346         free( p_ac3thread->ac3_decoder->imdct );
347         free( p_ac3thread->ac3_decoder );
348
349         return( -1 );
350     }
351
352     /*
353      * Bit stream
354      */
355     p_ac3thread->p_config->pf_init_bit_stream(
356             &p_ac3thread->ac3_decoder->bit_stream,
357             p_ac3thread->p_config->p_decoder_fifo,
358             BitstreamCallback, (void *) p_ac3thread );
359     
360     intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
361     
362     return( 0 );
363 }
364
365
366 /*****************************************************************************
367  * EndThread : ac3 decoder thread destruction
368  *****************************************************************************/
369 static void EndThread (ac3dec_thread_t * p_ac3thread)
370 {
371     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
372
373     /* If the audio output fifo was created, we destroy it */
374     if (p_ac3thread->p_aout_fifo != NULL)
375     {
376         aout_DestroyFifo (p_ac3thread->p_aout_fifo);
377
378         /* Make sure the output thread leaves the NextFrame() function */
379         vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
380         vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
381         vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
382     }
383
384     /* Free allocated structures */
385 #define IMDCT p_ac3thread->ac3_decoder->imdct
386     free( IMDCT->w_1 );
387     free( IMDCT->w_64 );
388     free( IMDCT->w_32 );
389     free( IMDCT->w_16 );
390     free( IMDCT->w_8 );
391     free( IMDCT->w_4 );
392     free( IMDCT->w_2 );
393     free( IMDCT->xcos_sin_sse );
394     free( IMDCT->xsin2 );
395     free( IMDCT->xcos2 );
396     free( IMDCT->xsin1 );
397     free( IMDCT->xcos1 );
398     free( IMDCT->delay1 );
399     free( IMDCT->delay );
400     free( IMDCT->buf );
401 #undef IMDCT
402
403 #if defined( __MINGW32__ )
404     free( p_ac3thread->ac3_decoder->samples_back );
405 #else
406     free( p_ac3thread->ac3_decoder->samples );
407 #endif
408
409     /* Unlock the modules */
410     module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
411     module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
412
413     /* Free what's left of the decoder */
414     free( p_ac3thread->ac3_decoder->imdct );
415     free( p_ac3thread->ac3_decoder );
416
417     intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread );
418 }
419
420 /*****************************************************************************
421  * BitstreamCallback: Import parameters from the new data/PES packet
422  *****************************************************************************
423  * This function is called by input's NextDataPacket.
424  *****************************************************************************/
425 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
426                                         boolean_t b_new_pes)
427 {
428
429     ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
430
431     if( b_new_pes )
432     {
433         int ptr;
434         
435         ptr = *(p_bit_stream->p_byte + 1);
436         ptr <<= 8;
437         ptr |= *(p_bit_stream->p_byte + 2);
438         p_ac3thread->sync_ptr = ptr;
439         p_bit_stream->p_byte += 3;                                                            
440     }
441 }
442