1 /*****************************************************************************
2 * ac3_decoder_thread.c: ac3 decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: ac3_decoder_thread.c,v 1.30 2001/04/30 21:04:20 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 *****************************************************************************/
38 #include <unistd.h> /* getpid() */
40 #include <stdio.h> /* "intf_msg.h" */
41 #include <stdlib.h> /* malloc(), free() */
48 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
50 #include "stream_control.h"
51 #include "input_ext-dec.h"
53 #include "audio_output.h"
55 #include "ac3_decoder.h"
56 #include "ac3_decoder_thread.h"
58 #define AC3DEC_FRAME_SIZE (2*1536)
60 /*****************************************************************************
62 *****************************************************************************/
63 static int InitThread (ac3dec_thread_t * p_adec);
64 static void RunThread (ac3dec_thread_t * p_adec);
65 static void ErrorThread (ac3dec_thread_t * p_adec);
66 static void EndThread (ac3dec_thread_t * p_adec);
67 static void BitstreamCallback ( bit_stream_t *p_bit_stream,
68 boolean_t b_new_pes );
71 /*****************************************************************************
72 * ac3dec_CreateThread: creates an ac3 decoder thread
73 *****************************************************************************/
74 vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
76 ac3dec_thread_t * p_ac3dec_t;
78 intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread" );
80 /* Allocate the memory needed to store the thread's structure */
81 if((p_ac3dec_t = (ac3dec_thread_t *)malloc(sizeof(ac3dec_thread_t)))==NULL)
83 intf_ErrMsg ( "ac3dec error: not enough memory "
84 "for ac3dec_CreateThread() to create the new thread");
89 * Initialize the thread properties
91 p_ac3dec_t->p_config = p_config;
92 p_ac3dec_t->p_fifo = p_config->decoder_config.p_decoder_fifo;
94 /* Initialize the ac3 decoder structures */
95 ac3_init (&p_ac3dec_t->ac3_decoder);
98 * Initialize the output properties
100 p_ac3dec_t->p_aout = p_config->p_aout;
101 p_ac3dec_t->p_aout_fifo = NULL;
103 /* Spawn the ac3 decoder thread */
104 if (vlc_thread_create(&p_ac3dec_t->thread_id, "ac3 decoder",
105 (vlc_thread_func_t)RunThread, (void *)p_ac3dec_t))
107 intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread" );
112 intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created", p_ac3dec_t);
113 return p_ac3dec_t->thread_id;
116 /* Following functions are local */
118 /*****************************************************************************
119 * InitThread : initialize an ac3 decoder thread
120 *****************************************************************************/
121 static int InitThread (ac3dec_thread_t * p_ac3dec_t)
123 aout_fifo_t aout_fifo;
125 intf_DbgMsg("ac3dec debug: initializing ac3 decoder thread %p",p_ac3dec_t);
127 p_ac3dec_t->p_config->decoder_config.pf_init_bit_stream(
128 &p_ac3dec_t->ac3_decoder.bit_stream,
129 p_ac3dec_t->p_config->decoder_config.p_decoder_fifo,
130 BitstreamCallback, (void *) p_ac3dec_t );
133 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
134 aout_fifo.i_channels = 2;
135 aout_fifo.b_stereo = 1;
137 aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
139 /* Creating the audio output fifo */
140 if ((p_ac3dec_t->p_aout_fifo = aout_CreateFifo(p_ac3dec_t->p_aout, &aout_fifo)) == NULL)
145 intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3dec_t);
149 /*****************************************************************************
150 * RunThread : ac3 decoder thread
151 *****************************************************************************/
152 static void RunThread (ac3dec_thread_t * p_ac3dec_t)
156 intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3dec_t, getpid());
158 /* Initializing the ac3 decoder thread */
159 if (InitThread (p_ac3dec_t)) /* XXX?? */
161 p_ac3dec_t->p_fifo->b_error = 1;
165 p_ac3dec_t->sync_ptr = 0;
167 /* ac3 decoder thread's main loop */
168 /* FIXME : do we have enough room to store the decoded frames ?? */
169 while ((!p_ac3dec_t->p_fifo->b_die) && (!p_ac3dec_t->p_fifo->b_error))
172 ac3_sync_info_t sync_info;
177 GetBits(&p_ac3dec_t->ac3_decoder.bit_stream,8);
178 } while ((!p_ac3dec_t->sync_ptr) && (!p_ac3dec_t->p_fifo->b_die)
179 && (!p_ac3dec_t->p_fifo->b_error));
181 ptr = p_ac3dec_t->sync_ptr;
183 while(ptr-- && (!p_ac3dec_t->p_fifo->b_die)
184 && (!p_ac3dec_t->p_fifo->b_error))
186 p_ac3dec_t->ac3_decoder.bit_stream.p_byte++;
189 /* we are in sync now */
193 if (DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts)
195 p_ac3dec_t->p_aout_fifo->date[p_ac3dec_t->p_aout_fifo->l_end_frame] =
196 DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts;
197 DECODER_FIFO_START(*p_ac3dec_t->p_fifo)->i_pts = 0;
199 p_ac3dec_t->p_aout_fifo->date[p_ac3dec_t->p_aout_fifo->l_end_frame] =
203 if (ac3_sync_frame (&p_ac3dec_t->ac3_decoder, &sync_info))
209 p_ac3dec_t->p_aout_fifo->l_rate = sync_info.sample_rate;
211 buffer = ((s16 *)p_ac3dec_t->p_aout_fifo->buffer) +
212 (p_ac3dec_t->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
214 if (ac3_decode_frame (&p_ac3dec_t->ac3_decoder, buffer))
220 vlc_mutex_lock (&p_ac3dec_t->p_aout_fifo->data_lock);
221 p_ac3dec_t->p_aout_fifo->l_end_frame =
222 (p_ac3dec_t->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
223 vlc_cond_signal (&p_ac3dec_t->p_aout_fifo->data_wait);
224 vlc_mutex_unlock (&p_ac3dec_t->p_aout_fifo->data_lock);
227 RealignBits(&p_ac3dec_t->ac3_decoder.bit_stream);
230 /* If b_error is set, the ac3 decoder thread enters the error loop */
231 if (p_ac3dec_t->p_fifo->b_error)
233 ErrorThread (p_ac3dec_t);
236 /* End of the ac3 decoder thread */
237 EndThread (p_ac3dec_t);
240 /*****************************************************************************
241 * ErrorThread : ac3 decoder's RunThread() error loop
242 *****************************************************************************/
243 static void ErrorThread (ac3dec_thread_t * p_ac3dec_t)
245 /* We take the lock, because we are going to read/write the start/end
246 * indexes of the decoder fifo */
247 vlc_mutex_lock (&p_ac3dec_t->p_fifo->data_lock);
249 /* Wait until a `die' order is sent */
250 while (!p_ac3dec_t->p_fifo->b_die)
252 /* Trash all received PES packets */
253 while (!DECODER_FIFO_ISEMPTY(*p_ac3dec_t->p_fifo))
255 p_ac3dec_t->p_fifo->pf_delete_pes(p_ac3dec_t->p_fifo->p_packets_mgt,
256 DECODER_FIFO_START(*p_ac3dec_t->p_fifo));
257 DECODER_FIFO_INCSTART (*p_ac3dec_t->p_fifo);
260 /* Waiting for the input thread to put new PES packets in the fifo */
261 vlc_cond_wait (&p_ac3dec_t->p_fifo->data_wait,
262 &p_ac3dec_t->p_fifo->data_lock);
265 /* We can release the lock before leaving */
266 vlc_mutex_unlock (&p_ac3dec_t->p_fifo->data_lock);
269 /*****************************************************************************
270 * EndThread : ac3 decoder thread destruction
271 *****************************************************************************/
272 static void EndThread (ac3dec_thread_t * p_ac3dec_t)
274 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3dec_t);
276 /* If the audio output fifo was created, we destroy it */
277 if (p_ac3dec_t->p_aout_fifo != NULL)
279 aout_DestroyFifo (p_ac3dec_t->p_aout_fifo);
281 /* Make sure the output thread leaves the NextFrame() function */
282 vlc_mutex_lock (&(p_ac3dec_t->p_aout_fifo->data_lock));
283 vlc_cond_signal (&(p_ac3dec_t->p_aout_fifo->data_wait));
284 vlc_mutex_unlock (&(p_ac3dec_t->p_aout_fifo->data_lock));
288 /* Destroy descriptor */
289 free( p_ac3dec_t->p_config );
292 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3dec_t);
295 /*****************************************************************************
296 * BitstreamCallback: Import parameters from the new data/PES packet
297 *****************************************************************************
298 * This function is called by input's NextDataPacket.
299 *****************************************************************************/
300 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
304 ac3dec_thread_t *p_ac3dec_t=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
310 ptr = *(p_bit_stream->p_byte + 1);
312 ptr |= *(p_bit_stream->p_byte + 2);
313 p_ac3dec_t->sync_ptr = ptr;
314 p_bit_stream->p_byte += 3;