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 $
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 *****************************************************************************/
38 #include <unistd.h> /* getpid() */
40 #include <stdlib.h> /* malloc(), free() */
41 #include <string.h> /* memset() */
49 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
51 #include "stream_control.h"
52 #include "input_ext-dec.h"
54 #include "audio_output.h"
56 #include "ac3_imdct.h"
57 #include "ac3_downmix.h"
58 #include "ac3_decoder.h"
59 #include "ac3_decoder_thread.h"
61 #define AC3DEC_FRAME_SIZE (2*1536)
63 /*****************************************************************************
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 );
73 /*****************************************************************************
74 * ac3dec_CreateThread: creates an ac3 decoder thread
75 *****************************************************************************/
76 vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
78 ac3dec_thread_t * p_ac3thread;
80 intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread" );
82 /* Allocate the memory needed to store the thread's structure */
83 if((p_ac3thread = (ac3dec_thread_t *)malloc(sizeof(ac3dec_thread_t)))==NULL)
85 intf_ErrMsg ( "ac3dec error: not enough memory "
86 "for ac3dec_CreateThread() to create the new thread");
91 * Initialize the thread properties
93 p_ac3thread->p_config = p_config;
94 p_ac3thread->p_fifo = p_config->decoder_config.p_decoder_fifo;
97 * Choose the best downmix module
99 #define DOWNMIX p_ac3thread->ac3_decoder.downmix
100 DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
102 if( DOWNMIX.p_module == NULL )
104 intf_ErrMsg( "ac3dec error: no suitable downmix module" );
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;
121 * Choose the best IMDCT module
123 #define IMDCT p_ac3thread->ac3_decoder.imdct
124 IMDCT.p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
126 if( IMDCT.p_module == NULL )
128 intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
129 module_Unneed( p_ac3thread->ac3_decoder.downmix.p_module );
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;
143 /* Initialize the ac3 decoder structures */
144 ac3_init (&p_ac3thread->ac3_decoder);
147 * Initialize the output properties
149 p_ac3thread->p_aout_fifo = NULL;
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))
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 );
162 intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created", p_ac3thread);
163 return p_ac3thread->thread_id;
166 /* Following functions are local */
168 /*****************************************************************************
169 * InitThread : initialize an ac3 decoder thread
170 *****************************************************************************/
171 static int InitThread (ac3dec_thread_t * p_ac3thread)
173 intf_DbgMsg("ac3dec debug: initializing ac3 decoder thread %p",p_ac3thread);
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 );
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 )
188 intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
192 /*****************************************************************************
193 * RunThread : ac3 decoder thread
194 *****************************************************************************/
195 static void RunThread (ac3dec_thread_t * p_ac3thread)
199 intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3thread, getpid());
201 /* Initializing the ac3 decoder thread */
202 if (InitThread (p_ac3thread)) /* XXX?? */
204 p_ac3thread->p_fifo->b_error = 1;
208 p_ac3thread->sync_ptr = 0;
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))
215 ac3_sync_info_t sync_info;
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));
224 ptr = p_ac3thread->sync_ptr;
226 while(ptr-- && (!p_ac3thread->p_fifo->b_die)
227 && (!p_ac3thread->p_fifo->b_error))
229 p_ac3thread->ac3_decoder.bit_stream.p_byte++;
232 /* we are in sync now */
236 if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
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;
242 p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->l_end_frame] =
246 if (ac3_sync_frame (&p_ac3thread->ac3_decoder, &sync_info))
252 p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
254 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
255 (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
257 if (ac3_decode_frame (&p_ac3thread->ac3_decoder, buffer))
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);
270 RealignBits(&p_ac3thread->ac3_decoder.bit_stream);
273 /* If b_error is set, the ac3 decoder thread enters the error loop */
274 if (p_ac3thread->p_fifo->b_error)
276 ErrorThread (p_ac3thread);
279 /* End of the ac3 decoder thread */
280 EndThread (p_ac3thread);
283 /*****************************************************************************
284 * ErrorThread : ac3 decoder's RunThread() error loop
285 *****************************************************************************/
286 static void ErrorThread (ac3dec_thread_t * p_ac3thread)
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);
292 /* Wait until a `die' order is sent */
293 while (!p_ac3thread->p_fifo->b_die)
295 /* Trash all received PES packets */
296 while (!DECODER_FIFO_ISEMPTY(*p_ac3thread->p_fifo))
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);
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);
308 /* We can release the lock before leaving */
309 vlc_mutex_unlock (&p_ac3thread->p_fifo->data_lock);
312 /*****************************************************************************
313 * EndThread : ac3 decoder thread destruction
314 *****************************************************************************/
315 static void EndThread (ac3dec_thread_t * p_ac3thread)
317 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
319 /* If the audio output fifo was created, we destroy it */
320 if (p_ac3thread->p_aout_fifo != NULL)
322 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
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));
330 /* Unlock the modules */
331 module_Unneed( p_ac3thread->ac3_decoder.downmix.p_module );
332 module_Unneed( p_ac3thread->ac3_decoder.imdct.p_module );
334 /* Destroy descriptor */
335 free( p_ac3thread->p_config );
338 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread);
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,
350 ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
356 ptr = *(p_bit_stream->p_byte + 1);
358 ptr |= *(p_bit_stream->p_byte + 2);
359 p_ac3thread->sync_ptr = ptr;
360 p_bit_stream->p_byte += 3;