]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder_thread.c
* Closing Debian bug #119369 which was fixed a while ago.
[vlc] / src / ac3_decoder / ac3_decoder_thread.c
1 /*****************************************************************************
2  * ac3_decoder_thread.c: ac3 decoder thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: ac3_decoder_thread.c,v 1.39 2001/10/31 11:55:53 reno 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
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #include "defs.h"
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>                                              /* getpid() */
40 #endif
41
42 #include <stdlib.h>                                      /* malloc(), free() */
43 #include <string.h>                                              /* memset() */
44
45 #include "config.h"
46 #include "common.h"
47 #include "threads.h"
48 #include "mtime.h"
49 #include "modules.h"
50
51 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
52
53 #include "stream_control.h"
54 #include "input_ext-dec.h"
55
56 #include "audio_output.h"
57
58 #include "ac3_imdct.h"
59 #include "ac3_downmix.h"
60 #include "ac3_decoder.h"
61 #include "ac3_decoder_thread.h"
62
63 #define AC3DEC_FRAME_SIZE (2*1536) 
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int      InitThread              (ac3dec_thread_t * p_adec);
69 static void     RunThread               (ac3dec_thread_t * p_adec);
70 static void     ErrorThread             (ac3dec_thread_t * p_adec);
71 static void     EndThread               (ac3dec_thread_t * p_adec);
72 static void     BitstreamCallback       ( bit_stream_t *p_bit_stream,
73                                               boolean_t b_new_pes );
74
75 /*****************************************************************************
76  * ac3dec_CreateThread: creates an ac3 decoder thread
77  *****************************************************************************/
78 vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
79 {
80     ac3dec_thread_t *   p_ac3thread;
81
82     intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread" );
83
84     /* Allocate the memory needed to store the thread's structure */
85     p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
86
87     if(p_ac3thread == NULL)
88     {
89         intf_ErrMsg ( "ac3dec error: not enough memory "
90                       "for ac3dec_CreateThread() to create the new thread");
91         return 0;
92     }
93    
94     /*
95      * Initialize the thread properties
96      */
97     p_ac3thread->p_config = p_config;
98     p_ac3thread->p_fifo = p_config->decoder_config.p_decoder_fifo;
99     p_ac3thread->ac3_decoder = memalign(16, sizeof(ac3dec_t));
100
101     /*
102      * Choose the best downmix module
103      */
104 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
105     DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
106
107     if( DOWNMIX.p_module == NULL )
108     {
109         intf_ErrMsg( "ac3dec error: no suitable downmix module" );
110         free( p_ac3thread->ac3_decoder );
111         free( p_ac3thread );
112         return( 0 );
113     }
114
115 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
116     DOWNMIX.pf_downmix_3f_2r_to_2ch     = F.pf_downmix_3f_2r_to_2ch;
117     DOWNMIX.pf_downmix_2f_2r_to_2ch     = F.pf_downmix_2f_2r_to_2ch;
118     DOWNMIX.pf_downmix_3f_1r_to_2ch     = F.pf_downmix_3f_1r_to_2ch;
119     DOWNMIX.pf_downmix_2f_1r_to_2ch     = F.pf_downmix_2f_1r_to_2ch;
120     DOWNMIX.pf_downmix_3f_0r_to_2ch     = F.pf_downmix_3f_0r_to_2ch;
121     DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
122     DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
123 #undef F
124 #undef DOWNMIX
125
126     /*
127      * Choose the best IMDCT module
128      */
129     p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
130     
131 #define IMDCT p_ac3thread->ac3_decoder->imdct
132     IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
133
134     if( IMDCT->p_module == NULL )
135     {
136         intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
137         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
138         free( p_ac3thread->ac3_decoder->imdct );
139         free( p_ac3thread->ac3_decoder );
140         free( p_ac3thread );
141         return( 0 );
142     }
143
144 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
145     IMDCT->pf_imdct_init    = F.pf_imdct_init;
146     IMDCT->pf_imdct_256     = F.pf_imdct_256;
147     IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
148     IMDCT->pf_imdct_512     = F.pf_imdct_512;
149     IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
150 #undef F
151 #undef IMDCT
152
153     /* Initialize the ac3 decoder structures */
154 #if defined( __MINGW32__ )
155     p_ac3thread->ac3_decoder->samples_back = memalign(16, 6 * 256 * sizeof(float) + 15);
156     p_ac3thread->ac3_decoder->samples = (float *) (((unsigned long) p_ac3thread->ac3_decoder->samples_back+15) & ~0xFUL);
157 #else
158     p_ac3thread->ac3_decoder->samples = memalign(16, 6 * 256 * sizeof(float));
159 #endif
160     p_ac3thread->ac3_decoder->imdct->buf = memalign(16, N/4 * sizeof(complex_t));
161     p_ac3thread->ac3_decoder->imdct->delay = memalign(16, 6 * 256 * sizeof(float));
162     p_ac3thread->ac3_decoder->imdct->delay1 = memalign(16, 6 * 256 * sizeof(float));
163     p_ac3thread->ac3_decoder->imdct->xcos1 = memalign(16, N/4 * sizeof(float));
164     p_ac3thread->ac3_decoder->imdct->xsin1 = memalign(16, N/4 * sizeof(float));
165     p_ac3thread->ac3_decoder->imdct->xcos2 = memalign(16, N/8 * sizeof(float));
166     p_ac3thread->ac3_decoder->imdct->xsin2 = memalign(16, N/8 * sizeof(float));
167     p_ac3thread->ac3_decoder->imdct->xcos_sin_sse = memalign(16, 128 * 4 * sizeof(float));
168     p_ac3thread->ac3_decoder->imdct->w_2 = memalign(16, 2 * sizeof(complex_t));
169     p_ac3thread->ac3_decoder->imdct->w_4 = memalign(16, 4 * sizeof(complex_t));
170     p_ac3thread->ac3_decoder->imdct->w_8 = memalign(16, 8 * sizeof(complex_t));
171     p_ac3thread->ac3_decoder->imdct->w_16 = memalign(16, 16 * sizeof(complex_t));
172     p_ac3thread->ac3_decoder->imdct->w_32 = memalign(16, 32 * sizeof(complex_t));
173     p_ac3thread->ac3_decoder->imdct->w_64 = memalign(16, 64 * sizeof(complex_t));
174     p_ac3thread->ac3_decoder->imdct->w_1 = memalign(16, sizeof(complex_t));
175
176     ac3_init (p_ac3thread->ac3_decoder);
177
178     /*
179      * Initialize the output properties
180      */
181     p_ac3thread->p_aout_fifo = NULL;
182
183     /* Spawn the ac3 decoder thread */
184     if (vlc_thread_create(&p_ac3thread->thread_id, "ac3 decoder", 
185                 (vlc_thread_func_t)RunThread, (void *)p_ac3thread))
186     {
187         intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread" );
188         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
189         module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
190         free( p_ac3thread->ac3_decoder->imdct->w_1 );
191         free( p_ac3thread->ac3_decoder->imdct->w_64 );
192         free( p_ac3thread->ac3_decoder->imdct->w_32 );
193         free( p_ac3thread->ac3_decoder->imdct->w_16 );
194         free( p_ac3thread->ac3_decoder->imdct->w_8 );
195         free( p_ac3thread->ac3_decoder->imdct->w_4 );
196         free( p_ac3thread->ac3_decoder->imdct->w_2 );
197         free( p_ac3thread->ac3_decoder->imdct->xcos_sin_sse );
198         free( p_ac3thread->ac3_decoder->imdct->xsin2 );
199         free( p_ac3thread->ac3_decoder->imdct->xcos2 );
200         free( p_ac3thread->ac3_decoder->imdct->xsin1 );
201         free( p_ac3thread->ac3_decoder->imdct->xcos1 );
202         free( p_ac3thread->ac3_decoder->imdct->delay1 );
203         free( p_ac3thread->ac3_decoder->imdct->delay );
204         free( p_ac3thread->ac3_decoder->imdct->buf );
205 #if defined( __MINGW32__ )
206         free( p_ac3thread->ac3_decoder->samples_back );
207 #else
208         free( p_ac3thread->ac3_decoder->samples );
209 #endif
210         free( p_ac3thread->ac3_decoder->imdct );
211         free( p_ac3thread->ac3_decoder );
212         free( p_ac3thread );
213         return 0;
214     }
215
216     intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created", p_ac3thread);
217     return p_ac3thread->thread_id;
218 }
219
220 /* Following functions are local */
221
222 /*****************************************************************************
223  * InitThread : initialize an ac3 decoder thread
224  *****************************************************************************/
225 static int InitThread (ac3dec_thread_t * p_ac3thread)
226 {
227     intf_DbgMsg("ac3dec debug: initializing ac3 decoder thread %p",p_ac3thread);
228
229     p_ac3thread->p_config->decoder_config.pf_init_bit_stream(
230             &p_ac3thread->ac3_decoder->bit_stream,
231             p_ac3thread->p_config->decoder_config.p_decoder_fifo,
232             BitstreamCallback, (void *) p_ac3thread );
233
234     /* Creating the audio output fifo */
235     p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
236                                                AC3DEC_FRAME_SIZE, NULL  );
237     if ( p_ac3thread->p_aout_fifo == NULL )
238     {
239         return -1;
240     }
241
242     intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
243     return 0;
244 }
245
246 /*****************************************************************************
247  * RunThread : ac3 decoder thread
248  *****************************************************************************/
249 static void RunThread (ac3dec_thread_t * p_ac3thread)
250 {
251     int sync;
252
253     intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3thread, getpid());
254
255     /* Initializing the ac3 decoder thread */
256     if (InitThread (p_ac3thread)) /* XXX?? */
257     {
258         p_ac3thread->p_fifo->b_error = 1;
259     }
260
261     sync = 0;
262     p_ac3thread->sync_ptr = 0;
263
264     /* ac3 decoder thread's main loop */
265     /* FIXME : do we have enough room to store the decoded frames ?? */
266     while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
267     {
268         s16 * buffer;
269         ac3_sync_info_t sync_info;
270         int ptr;
271
272         if (!sync) {
273             do {
274                 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
275             } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
276                     && (!p_ac3thread->p_fifo->b_error));
277             
278             ptr = p_ac3thread->sync_ptr;
279
280             while(ptr-- && (!p_ac3thread->p_fifo->b_die)
281                 && (!p_ac3thread->p_fifo->b_error))
282             {
283                 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
284             }
285                         
286             /* we are in sync now */
287             sync = 1;
288         }
289
290         if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
291         {
292             p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->l_end_frame] =
293                 DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts;
294             DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts = 0;
295         } else {
296             p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->l_end_frame] =
297                 LAST_MDATE;
298         }
299     
300         if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
301         {
302             sync = 0;
303             goto bad_frame;
304         }
305
306         p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
307
308         buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
309             (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
310
311         if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
312         {
313             sync = 0;
314             goto bad_frame;
315         }
316         
317         vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
318         p_ac3thread->p_aout_fifo->l_end_frame = 
319             (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
320         vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
321         vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
322
323         bad_frame:
324             RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
325     }
326
327     /* If b_error is set, the ac3 decoder thread enters the error loop */
328     if (p_ac3thread->p_fifo->b_error)
329     {
330         ErrorThread (p_ac3thread);
331     }
332
333     /* End of the ac3 decoder thread */
334     EndThread (p_ac3thread);
335 }
336
337 /*****************************************************************************
338  * ErrorThread : ac3 decoder's RunThread() error loop
339  *****************************************************************************/
340 static void ErrorThread (ac3dec_thread_t * p_ac3thread)
341 {
342     /* We take the lock, because we are going to read/write the start/end
343      * indexes of the decoder fifo */
344     vlc_mutex_lock (&p_ac3thread->p_fifo->data_lock);
345
346     /* Wait until a `die' order is sent */
347     while (!p_ac3thread->p_fifo->b_die)
348     {
349         /* Trash all received PES packets */
350         while (!DECODER_FIFO_ISEMPTY(*p_ac3thread->p_fifo))
351         {
352             p_ac3thread->p_fifo->pf_delete_pes(p_ac3thread->p_fifo->p_packets_mgt,
353                     DECODER_FIFO_START(*p_ac3thread->p_fifo));
354             DECODER_FIFO_INCSTART (*p_ac3thread->p_fifo);
355         }
356
357         /* Waiting for the input thread to put new PES packets in the fifo */
358         vlc_cond_wait (&p_ac3thread->p_fifo->data_wait,
359                        &p_ac3thread->p_fifo->data_lock);
360     }
361
362     /* We can release the lock before leaving */
363     vlc_mutex_unlock (&p_ac3thread->p_fifo->data_lock);
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     /* Unlock the modules */
385     module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
386     module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
387
388     /* Destroy descriptor */
389     free( p_ac3thread->ac3_decoder->imdct->w_1 );
390     free( p_ac3thread->ac3_decoder->imdct->w_64 );
391     free( p_ac3thread->ac3_decoder->imdct->w_32 );
392     free( p_ac3thread->ac3_decoder->imdct->w_16 );
393     free( p_ac3thread->ac3_decoder->imdct->w_8 );
394     free( p_ac3thread->ac3_decoder->imdct->w_4 );
395     free( p_ac3thread->ac3_decoder->imdct->w_2 );
396     free( p_ac3thread->ac3_decoder->imdct->xcos_sin_sse );
397     free( p_ac3thread->ac3_decoder->imdct->xsin2 );
398     free( p_ac3thread->ac3_decoder->imdct->xcos2 );
399     free( p_ac3thread->ac3_decoder->imdct->xsin1 );
400     free( p_ac3thread->ac3_decoder->imdct->xcos1 );
401     free( p_ac3thread->ac3_decoder->imdct->delay1 );
402     free( p_ac3thread->ac3_decoder->imdct->delay );
403     free( p_ac3thread->ac3_decoder->imdct->buf );
404 #if defined( __MINGW32__ )
405     free( p_ac3thread->ac3_decoder->samples_back );
406 #else
407     free( p_ac3thread->ac3_decoder->samples );
408 #endif
409     free( p_ac3thread->ac3_decoder->imdct );
410     free( p_ac3thread->ac3_decoder );
411     free( p_ac3thread->p_config );
412     free( p_ac3thread );
413
414     intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread);
415 }
416
417 /*****************************************************************************
418 * BitstreamCallback: Import parameters from the new data/PES packet
419 *****************************************************************************
420 * This function is called by input's NextDataPacket.
421 *****************************************************************************/
422 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
423                                         boolean_t b_new_pes)
424 {
425
426     ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
427
428     if( b_new_pes )
429     {
430         int ptr;
431         
432         ptr = *(p_bit_stream->p_byte + 1);
433         ptr <<= 8;
434         ptr |= *(p_bit_stream->p_byte + 2);
435         p_ac3thread->sync_ptr = ptr;
436         p_bit_stream->p_byte += 3;                                                            
437     }
438 }
439