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 $
7 * Authors: Michel Lespinasse <walken@zoy.org>
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.
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.
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 *****************************************************************************/
27 * - vérifier l'état de la fifo de sortie avant d'y stocker les samples
29 * - vlc_cond_signal() / vlc_cond_wait()
33 /*****************************************************************************
35 *****************************************************************************/
39 #include <unistd.h> /* getpid() */
42 #include <stdlib.h> /* malloc(), free() */
43 #include <string.h> /* memset() */
51 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
53 #include "stream_control.h"
54 #include "input_ext-dec.h"
56 #include "audio_output.h"
58 #include "ac3_imdct.h"
59 #include "ac3_downmix.h"
60 #include "ac3_decoder.h"
61 #include "ac3_decoder_thread.h"
63 #define AC3DEC_FRAME_SIZE (2*1536)
65 /*****************************************************************************
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 );
75 /*****************************************************************************
76 * ac3dec_CreateThread: creates an ac3 decoder thread
77 *****************************************************************************/
78 vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
80 ac3dec_thread_t * p_ac3thread;
82 intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread" );
84 /* Allocate the memory needed to store the thread's structure */
85 p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
87 if(p_ac3thread == NULL)
89 intf_ErrMsg ( "ac3dec error: not enough memory "
90 "for ac3dec_CreateThread() to create the new thread");
95 * Initialize the thread properties
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));
102 * Choose the best downmix module
104 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
105 DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
107 if( DOWNMIX.p_module == NULL )
109 intf_ErrMsg( "ac3dec error: no suitable downmix module" );
110 free( p_ac3thread->ac3_decoder );
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;
127 * Choose the best IMDCT module
129 p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
131 #define IMDCT p_ac3thread->ac3_decoder->imdct
132 IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
134 if( IMDCT->p_module == NULL )
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 );
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;
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);
158 p_ac3thread->ac3_decoder->samples = memalign(16, 6 * 256 * sizeof(float));
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));
176 ac3_init (p_ac3thread->ac3_decoder);
179 * Initialize the output properties
181 p_ac3thread->p_aout_fifo = NULL;
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))
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 );
208 free( p_ac3thread->ac3_decoder->samples );
210 free( p_ac3thread->ac3_decoder->imdct );
211 free( p_ac3thread->ac3_decoder );
216 intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created", p_ac3thread);
217 return p_ac3thread->thread_id;
220 /* Following functions are local */
222 /*****************************************************************************
223 * InitThread : initialize an ac3 decoder thread
224 *****************************************************************************/
225 static int InitThread (ac3dec_thread_t * p_ac3thread)
227 intf_DbgMsg("ac3dec debug: initializing ac3 decoder thread %p",p_ac3thread);
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 );
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 )
242 intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
246 /*****************************************************************************
247 * RunThread : ac3 decoder thread
248 *****************************************************************************/
249 static void RunThread (ac3dec_thread_t * p_ac3thread)
253 intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3thread, getpid());
255 /* Initializing the ac3 decoder thread */
256 if (InitThread (p_ac3thread)) /* XXX?? */
258 p_ac3thread->p_fifo->b_error = 1;
262 p_ac3thread->sync_ptr = 0;
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))
269 ac3_sync_info_t sync_info;
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));
278 ptr = p_ac3thread->sync_ptr;
280 while(ptr-- && (!p_ac3thread->p_fifo->b_die)
281 && (!p_ac3thread->p_fifo->b_error))
283 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
286 /* we are in sync now */
290 if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
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;
296 p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->l_end_frame] =
300 if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
306 p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
308 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
309 (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
311 if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
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);
324 RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
327 /* If b_error is set, the ac3 decoder thread enters the error loop */
328 if (p_ac3thread->p_fifo->b_error)
330 ErrorThread (p_ac3thread);
333 /* End of the ac3 decoder thread */
334 EndThread (p_ac3thread);
337 /*****************************************************************************
338 * ErrorThread : ac3 decoder's RunThread() error loop
339 *****************************************************************************/
340 static void ErrorThread (ac3dec_thread_t * p_ac3thread)
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);
346 /* Wait until a `die' order is sent */
347 while (!p_ac3thread->p_fifo->b_die)
349 /* Trash all received PES packets */
350 while (!DECODER_FIFO_ISEMPTY(*p_ac3thread->p_fifo))
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);
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);
362 /* We can release the lock before leaving */
363 vlc_mutex_unlock (&p_ac3thread->p_fifo->data_lock);
366 /*****************************************************************************
367 * EndThread : ac3 decoder thread destruction
368 *****************************************************************************/
369 static void EndThread (ac3dec_thread_t * p_ac3thread)
371 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
373 /* If the audio output fifo was created, we destroy it */
374 if (p_ac3thread->p_aout_fifo != NULL)
376 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
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));
384 /* Unlock the modules */
385 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
386 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
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 );
407 free( p_ac3thread->ac3_decoder->samples );
409 free( p_ac3thread->ac3_decoder->imdct );
410 free( p_ac3thread->ac3_decoder );
411 free( p_ac3thread->p_config );
414 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread);
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,
426 ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
432 ptr = *(p_bit_stream->p_byte + 1);
434 ptr |= *(p_bit_stream->p_byte + 2);
435 p_ac3thread->sync_ptr = ptr;
436 p_bit_stream->p_byte += 3;