]> git.sesse.net Git - vlc/blob - plugins/ac3_adec/ac3_adec.c
* Fixed a few crashes due to race conditions in the decoder spawning.
[vlc] / plugins / ac3_adec / ac3_adec.c
1 /*****************************************************************************
2  * ac3_adec.c: ac3 decoder module main file
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: ac3_adec.c,v 1.3 2001/11/15 17:39:12 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  * TODO :
26  *
27  * - vérifier l'état de la fifo de sortie avant d'y stocker les samples
28  *   décodés ;
29  * - vlc_cond_signal() / vlc_cond_wait()
30  *
31  */
32 #define MODULE_NAME ac3_adec
33 #include "modules_inner.h"
34
35 /*****************************************************************************
36  * Preamble
37  *****************************************************************************/
38 #include "defs.h"
39
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>                                              /* getpid() */
42 #endif
43
44 #include <stdlib.h>                                      /* malloc(), free() */
45 #include <string.h>                                              /* memset() */
46
47 #include "config.h"
48 #include "common.h"
49 #include "threads.h"
50 #include "mtime.h"
51 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
52
53 #include "audio_output.h"
54
55 #include "modules.h"
56 #include "modules_export.h"
57
58 #include "stream_control.h"
59 #include "input_ext-dec.h"
60 #include "input_ext-intf.h"                                /* MPEG?_AUDIO_ES */
61
62 #include "ac3_imdct.h"
63 #include "ac3_downmix.h"
64 #include "ac3_decoder.h"
65 #include "ac3_adec.h"
66
67 #define AC3DEC_FRAME_SIZE (2*1536) 
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int      ac3_adec_Probe      ( probedata_t * );
73 static int      ac3_adec_Run         ( decoder_config_t * );
74 static int      ac3_adec_Init        (ac3dec_thread_t * p_adec);
75 static void     ac3_adec_ErrorThread (ac3dec_thread_t * p_adec);
76 static void     ac3_adec_EndThread   (ac3dec_thread_t * p_adec);
77 static void     BitstreamCallback    ( bit_stream_t *p_bit_stream,
78                                               boolean_t b_new_pes );
79
80 /*****************************************************************************
81  * Capabilities
82  *****************************************************************************/
83 void _M( adec_getfunctions )( function_list_t * p_function_list )
84 {
85     p_function_list->pf_probe = ac3_adec_Probe;
86     p_function_list->functions.dec.pf_RunThread = ac3_adec_Run;
87 }
88
89 /*****************************************************************************
90  * Build configuration tree.
91  *****************************************************************************/
92 MODULE_CONFIG_START
93 ADD_WINDOW( "Configuration for ac3 decoder module" )
94     ADD_COMMENT( "Nothing to configure" )
95 MODULE_CONFIG_STOP
96
97 MODULE_INIT_START
98     p_module->i_capabilities = MODULE_CAPABILITY_DEC;
99     p_module->psz_longname = "Ac3 sofware decoder";
100 MODULE_INIT_STOP
101
102 MODULE_ACTIVATE_START
103     _M( adec_getfunctions )( &p_module->p_functions->dec );
104 MODULE_ACTIVATE_STOP
105
106 MODULE_DEACTIVATE_START
107 MODULE_DEACTIVATE_STOP
108
109
110 /*****************************************************************************
111  * ac3_adec_Probe: probe the decoder and return score
112  *****************************************************************************
113  * Tries to launch a decoder and return score so that the interface is able 
114  * to chose.
115  *****************************************************************************/
116 static int ac3_adec_Probe( probedata_t *p_data )
117 {
118     return ( p_data->i_type == AC3_AUDIO_ES ) ? 50 : 0;
119 }
120
121 /*****************************************************************************
122  * ac3_adec_Run: this function is called just after the thread is created
123  *****************************************************************************/
124 static int ac3_adec_Run ( decoder_config_t * p_config )
125 {
126     ac3dec_thread_t *   p_ac3thread;
127     int sync;
128
129     intf_DbgMsg( "ac3_adec debug: ac3_adec thread launched, initializing" );
130
131     /* Allocate the memory needed to store the thread's structure */
132     p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
133
134     if( p_ac3thread == NULL )
135     {
136         intf_ErrMsg ( "ac3_adec error: not enough memory "
137                       "for ac3_adec_Run() to allocate p_ac3thread" );
138         return( -1 );
139     }
140    
141     /*
142      * Initialize the thread properties
143      */
144     p_ac3thread->p_config = p_config;
145     if( ac3_adec_Init( p_ac3thread ) )
146     {
147         intf_ErrMsg( "ac3_adec error: could not initialize thread" );
148         free( p_ac3thread );
149         return( -1 );
150     }
151
152     sync = 0;
153     p_ac3thread->sync_ptr = 0;
154
155     /* ac3 decoder thread's main loop */
156     /* FIXME : do we have enough room to store the decoded frames ?? */
157     while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
158     {
159         s16 * buffer;
160         ac3_sync_info_t sync_info;
161         int ptr;
162
163         if (!sync) {
164             do {
165                 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
166             } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
167                     && (!p_ac3thread->p_fifo->b_error));
168             
169             ptr = p_ac3thread->sync_ptr;
170
171             while(ptr-- && (!p_ac3thread->p_fifo->b_die)
172                 && (!p_ac3thread->p_fifo->b_error))
173             {
174                 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
175             }
176                         
177             /* we are in sync now */
178             sync = 1;
179         }
180
181         if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
182         {
183             p_ac3thread->p_aout_fifo->date[
184                 p_ac3thread->p_aout_fifo->l_end_frame] =
185                 DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts;
186             DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts = 0;
187         } else {
188             p_ac3thread->p_aout_fifo->date[
189                 p_ac3thread->p_aout_fifo->l_end_frame] =
190                 LAST_MDATE;
191         }
192     
193         if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
194         {
195             sync = 0;
196             goto bad_frame;
197         }
198
199         p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
200
201         buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
202             (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
203
204         if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
205         {
206             sync = 0;
207             goto bad_frame;
208         }
209         
210         vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
211         p_ac3thread->p_aout_fifo->l_end_frame = 
212             (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
213         vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
214         vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
215
216         bad_frame:
217             RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
218     }
219
220     /* If b_error is set, the ac3 decoder thread enters the error loop */
221     if (p_ac3thread->p_fifo->b_error)
222     {
223         ac3_adec_ErrorThread (p_ac3thread);
224     }
225
226     /* End of the ac3 decoder thread */
227     ac3_adec_EndThread (p_ac3thread);
228
229     free( p_ac3thread );
230
231     return( 0 );
232 }
233
234
235 /*****************************************************************************
236  * ac3_adec_Init: initialize data before entering main loop
237  *****************************************************************************/
238 static int ac3_adec_Init( ac3dec_thread_t * p_ac3thread )
239 {
240     /*
241      * Thread properties 
242      */
243     p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
244     p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
245
246     /*
247      * Choose the best downmix module
248      */
249 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
250     DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
251
252     if( DOWNMIX.p_module == NULL )
253     {
254         intf_ErrMsg( "ac3dec error: no suitable downmix module" );
255         free( p_ac3thread->ac3_decoder );
256         return( -1 );
257     }
258
259 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
260     DOWNMIX.pf_downmix_3f_2r_to_2ch     = F.pf_downmix_3f_2r_to_2ch;
261     DOWNMIX.pf_downmix_2f_2r_to_2ch     = F.pf_downmix_2f_2r_to_2ch;
262     DOWNMIX.pf_downmix_3f_1r_to_2ch     = F.pf_downmix_3f_1r_to_2ch;
263     DOWNMIX.pf_downmix_2f_1r_to_2ch     = F.pf_downmix_2f_1r_to_2ch;
264     DOWNMIX.pf_downmix_3f_0r_to_2ch     = F.pf_downmix_3f_0r_to_2ch;
265     DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
266     DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
267 #undef F
268 #undef DOWNMIX
269
270     /*
271      * Choose the best IMDCT module
272      */
273     p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
274     
275 #define IMDCT p_ac3thread->ac3_decoder->imdct
276     IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
277
278     if( IMDCT->p_module == NULL )
279     {
280         intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
281         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
282         free( p_ac3thread->ac3_decoder->imdct );
283         free( p_ac3thread->ac3_decoder );
284         return( -1 );
285     }
286
287 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
288     IMDCT->pf_imdct_init    = F.pf_imdct_init;
289     IMDCT->pf_imdct_256     = F.pf_imdct_256;
290     IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
291     IMDCT->pf_imdct_512     = F.pf_imdct_512;
292     IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
293 #undef F
294
295     /* Initialize the ac3 decoder structures */
296 #define p_dec p_ac3thread->ac3_decoder
297 #if defined( __MINGW32__ )
298     p_dec->samples_back = memalign( 16, 6 * 256 * sizeof(float) + 15 );
299     p_dec->samples = (float *)
300                      (((unsigned long) p_dec->samples_back + 15 ) & ~0xFUL);
301 #else
302     p_dec->samples = memalign( 16, 6 * 256 * sizeof(float) );
303 #endif
304 #undef p_dec
305
306     IMDCT->buf    = memalign( 16, N/4 * sizeof(complex_t) );
307     IMDCT->delay  = memalign( 16, 6 * 256 * sizeof(float) );
308     IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
309     IMDCT->xcos1  = memalign( 16, N/4 * sizeof(float) );
310     IMDCT->xsin1  = memalign( 16, N/4 * sizeof(float) );
311     IMDCT->xcos2  = memalign( 16, N/8 * sizeof(float) );
312     IMDCT->xsin2  = memalign( 16, N/8 * sizeof(float) );
313     IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
314     IMDCT->w_1    = memalign( 16, 1  * sizeof(complex_t) );
315     IMDCT->w_2    = memalign( 16, 2  * sizeof(complex_t) );
316     IMDCT->w_4    = memalign( 16, 4  * sizeof(complex_t) );
317     IMDCT->w_8    = memalign( 16, 8  * sizeof(complex_t) );
318     IMDCT->w_16   = memalign( 16, 16 * sizeof(complex_t) );
319     IMDCT->w_32   = memalign( 16, 32 * sizeof(complex_t) );
320     IMDCT->w_64   = memalign( 16, 64 * sizeof(complex_t) );
321
322     ac3_init( p_ac3thread->ac3_decoder );
323
324     /*
325      * Initialize the output properties
326      */
327     p_ac3thread->p_aout_fifo = NULL;
328
329     intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized", 
330                   p_ac3thread );
331
332     /*
333      * Bit stream
334      */
335     p_ac3thread->p_config->pf_init_bit_stream(
336             &p_ac3thread->ac3_decoder->bit_stream,
337             p_ac3thread->p_config->p_decoder_fifo,
338             BitstreamCallback, (void *) p_ac3thread );
339     
340     /* Creating the audio output fifo */
341     p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
342                                                AC3DEC_FRAME_SIZE, NULL  );
343     if ( p_ac3thread->p_aout_fifo == NULL )
344     {
345         free( IMDCT->w_1 );
346         free( IMDCT->w_64 );
347         free( IMDCT->w_32 );
348         free( IMDCT->w_16 );
349         free( IMDCT->w_8 );
350         free( IMDCT->w_4 );
351         free( IMDCT->w_2 );
352         free( IMDCT->xcos_sin_sse );
353         free( IMDCT->xsin2 );
354         free( IMDCT->xcos2 );
355         free( IMDCT->xsin1 );
356         free( IMDCT->xcos1 );
357         free( IMDCT->delay1 );
358         free( IMDCT->delay );
359         free( IMDCT->buf );
360 #undef IMDCT
361
362 #if defined( __MINGW32__ )
363         free( p_ac3thread->ac3_decoder->samples_back );
364 #else
365         free( p_ac3thread->ac3_decoder->samples );
366 #endif
367
368         module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
369         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
370
371         free( p_ac3thread->ac3_decoder->imdct );
372         free( p_ac3thread->ac3_decoder );
373
374         return( -1 );
375     }
376
377     intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
378     
379     return( 0 );
380 }
381
382
383 /*****************************************************************************
384  * ac3_adec_ErrorThread : ac3 decoder's RunThread() error loop
385  *****************************************************************************/
386 static void ac3_adec_ErrorThread (ac3dec_thread_t * p_ac3thread)
387 {
388     /* We take the lock, because we are going to read/write the start/end
389      * indexes of the decoder fifo */
390     vlc_mutex_lock (&p_ac3thread->p_fifo->data_lock);
391
392     /* Wait until a `die' order is sent */
393     while (!p_ac3thread->p_fifo->b_die)
394     {
395         /* Trash all received PES packets */
396         while (!DECODER_FIFO_ISEMPTY(*p_ac3thread->p_fifo))
397         {
398             p_ac3thread->p_fifo->pf_delete_pes(
399                     p_ac3thread->p_fifo->p_packets_mgt,
400                     DECODER_FIFO_START(*p_ac3thread->p_fifo));
401             DECODER_FIFO_INCSTART (*p_ac3thread->p_fifo);
402         }
403
404         /* Waiting for the input thread to put new PES packets in the fifo */
405         vlc_cond_wait (&p_ac3thread->p_fifo->data_wait,
406                        &p_ac3thread->p_fifo->data_lock);
407     }
408
409     /* We can release the lock before leaving */
410     vlc_mutex_unlock (&p_ac3thread->p_fifo->data_lock);
411 }
412
413 /*****************************************************************************
414  * ac3_adec_EndThread : ac3 decoder thread destruction
415  *****************************************************************************/
416 static void ac3_adec_EndThread (ac3dec_thread_t * p_ac3thread)
417 {
418     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
419
420     /* If the audio output fifo was created, we destroy it */
421     if (p_ac3thread->p_aout_fifo != NULL)
422     {
423         aout_DestroyFifo (p_ac3thread->p_aout_fifo);
424
425         /* Make sure the output thread leaves the NextFrame() function */
426         vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
427         vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
428         vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
429     }
430
431     /* Free allocated structures */
432 #define IMDCT p_ac3thread->ac3_decoder->imdct
433     free( IMDCT->w_1 );
434     free( IMDCT->w_64 );
435     free( IMDCT->w_32 );
436     free( IMDCT->w_16 );
437     free( IMDCT->w_8 );
438     free( IMDCT->w_4 );
439     free( IMDCT->w_2 );
440     free( IMDCT->xcos_sin_sse );
441     free( IMDCT->xsin2 );
442     free( IMDCT->xcos2 );
443     free( IMDCT->xsin1 );
444     free( IMDCT->xcos1 );
445     free( IMDCT->delay1 );
446     free( IMDCT->delay );
447     free( IMDCT->buf );
448 #undef IMDCT
449
450 #if defined( __MINGW32__ )
451     free( p_ac3thread->ac3_decoder->samples_back );
452 #else
453     free( p_ac3thread->ac3_decoder->samples );
454 #endif
455
456     /* Unlock the modules */
457     module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
458     module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
459
460     /* Free what's left of the decoder */
461     free( p_ac3thread->ac3_decoder->imdct );
462     free( p_ac3thread->ac3_decoder );
463
464     intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread );
465 }
466
467 /*****************************************************************************
468  * BitstreamCallback: Import parameters from the new data/PES packet
469  *****************************************************************************
470  * This function is called by input's NextDataPacket.
471  *****************************************************************************/
472 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
473                                         boolean_t b_new_pes)
474 {
475
476     ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
477
478     if( b_new_pes )
479     {
480         int ptr;
481         
482         ptr = *(p_bit_stream->p_byte + 1);
483         ptr <<= 8;
484         ptr |= *(p_bit_stream->p_byte + 2);
485         p_ac3thread->sync_ptr = ptr;
486         p_bit_stream->p_byte += 3;                                                            
487     }
488 }
489