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