1 /*****************************************************************************
2 * ac3_decoder_thread.c: ac3 decoder thread
4 *****************************************************************************/
9 * - vérifier l'état de la fifo de sortie avant d'y stocker les samples
11 * - vlc_cond_signal() / vlc_cond_wait()
15 /*****************************************************************************
17 *****************************************************************************/
18 #include <sys/types.h>
22 #include <unistd.h> /* getpid() */
24 #include <stdio.h> /* "intf_msg.h" */
25 #include <stdlib.h> /* malloc(), free() */
26 #include <sys/soundcard.h> /* "audio_output.h" */
27 #include <sys/uio.h> /* "input.h" */
32 #include "vlc_thread.h"
33 #include "debug.h" /* "input_netlist.h" */
35 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
37 #include "input.h" /* pes_packet_t */
38 #include "input_netlist.h" /* input_NetlistFreePES() */
39 #include "decoder_fifo.h" /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
41 #include "audio_output.h"
43 #include "ac3_decoder.h"
44 #include "ac3_decoder_thread.h"
46 #define AC3DEC_FRAME_SIZE (2*1536)
47 typedef s16 ac3dec_frame_t[ AC3DEC_FRAME_SIZE ];
49 /*****************************************************************************
51 *****************************************************************************/
52 static int InitThread ( ac3dec_thread_t * p_adec );
53 static void RunThread ( ac3dec_thread_t * p_adec );
54 static void ErrorThread ( ac3dec_thread_t * p_adec );
55 static void EndThread ( ac3dec_thread_t * p_adec );
57 /*****************************************************************************
58 * ac3dec_CreateThread: creates an ac3 decoder thread
59 *****************************************************************************/
60 ac3dec_thread_t * ac3dec_CreateThread( input_thread_t * p_input )
62 ac3dec_thread_t * p_ac3dec;
64 intf_DbgMsg("ac3dec debug: creating ac3 decoder thread\n");
66 /* Allocate the memory needed to store the thread's structure */
67 if ( (p_ac3dec = (ac3dec_thread_t *)malloc( sizeof(ac3dec_thread_t) )) == NULL )
69 intf_ErrMsg("ac3dec error: not enough memory for ac3dec_CreateThread() to create the new thread\n");
74 * Initialize the thread properties
77 p_ac3dec->b_error = 0;
80 * Initialize the input properties
82 /* Initialize the decoder fifo's data lock and conditional variable and set
83 * its buffer as empty */
84 vlc_mutex_init( &p_ac3dec->fifo.data_lock );
85 vlc_cond_init( &p_ac3dec->fifo.data_wait );
86 p_ac3dec->fifo.i_start = 0;
87 p_ac3dec->fifo.i_end = 0;
89 /* Initialize the ac3 decoder structures */
90 ac3_init (&p_ac3dec->ac3_decoder);
92 /* Initialize the bit stream structure */
93 p_ac3dec->p_input = p_input;
96 * Initialize the output properties
98 p_ac3dec->p_aout = p_input->p_aout;
99 p_ac3dec->p_aout_fifo = NULL;
101 /* Spawn the ac3 decoder thread */
102 if ( vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec) )
104 intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread\n" );
109 intf_DbgMsg( "ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec );
113 /*****************************************************************************
114 * ac3dec_DestroyThread: destroys an ac3 decoder thread
115 *****************************************************************************/
116 void ac3dec_DestroyThread( ac3dec_thread_t * p_ac3dec )
118 intf_DbgMsg( "ac3dec debug: requesting termination of ac3 decoder thread %p\n", p_ac3dec );
120 /* Ask thread to kill itself */
123 /* Make sure the decoder thread leaves the GetByte() function */
124 vlc_mutex_lock( &(p_ac3dec->fifo.data_lock) );
125 vlc_cond_signal( &(p_ac3dec->fifo.data_wait) );
126 vlc_mutex_unlock( &(p_ac3dec->fifo.data_lock) );
128 /* Waiting for the decoder thread to exit */
129 /* Remove this as soon as the "status" flag is implemented */
130 vlc_thread_join( p_ac3dec->thread_id );
133 /* Following functions are local */
135 /*****************************************************************************
136 * InitThread : initialize an ac3 decoder thread
137 *****************************************************************************/
138 static int InitThread( ac3dec_thread_t * p_ac3dec )
140 aout_fifo_t aout_fifo;
141 ac3_byte_stream_t * byte_stream;
143 intf_DbgMsg( "ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec );
145 /* Our first job is to initialize the bit stream structure with the
146 * beginning of the input stream */
147 vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
148 while ( DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
150 if ( p_ac3dec->b_die )
152 vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
155 vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
157 p_ac3dec->p_ts = DECODER_FIFO_START( p_ac3dec->fifo )->p_first_ts;
158 byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
159 byte_stream->p_byte =
160 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start;
162 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end;
163 byte_stream->info = p_ac3dec;
164 vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
166 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
167 aout_fifo.i_channels = 2;
168 aout_fifo.b_stereo = 1;
170 aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
172 /* Creating the audio output fifo */
173 if ( (p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL )
178 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec );
182 /*****************************************************************************
183 * RunThread : ac3 decoder thread
184 *****************************************************************************/
185 static void RunThread( ac3dec_thread_t * p_ac3dec )
189 intf_DbgMsg( "ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid() );
191 msleep( INPUT_PTS_DELAY );
193 /* Initializing the ac3 decoder thread */
194 if ( InitThread(p_ac3dec) ) /* XXX?? */
196 p_ac3dec->b_error = 1;
200 p_ac3dec->sync_ptr = 0;
202 /* ac3 decoder thread's main loop */
203 /* FIXME : do we have enough room to store the decoded frames ?? */
204 while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
207 ac3_sync_info_t sync_info;
209 if (!sync) { /* have to find a synchro point */
212 ac3_byte_stream_t * p_byte_stream;
214 p_byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
216 /* first read till next ac3 magic header */
218 ac3_byte_stream_next (p_byte_stream);
219 } while ((!p_ac3dec->sync_ptr) &&
220 (!p_ac3dec->b_die) &&
221 (!p_ac3dec->b_error));
222 /* skip the specified number of bytes */
224 ptr = p_ac3dec->sync_ptr;
225 while (--ptr && (!p_ac3dec->b_die) && (!p_ac3dec->b_error)) {
226 if (p_byte_stream->p_byte >= p_byte_stream->p_end) {
227 ac3_byte_stream_next (p_byte_stream);
229 p_byte_stream->p_byte++;
232 /* we are in sync now */
235 p_ac3dec->sync_ptr = 0;
238 if ( DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts )
240 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
241 DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
245 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
248 if (ac3_sync_frame (&p_ac3dec->ac3_decoder, &sync_info)) {
253 p_ac3dec->p_aout_fifo->l_rate = sync_info.sample_rate;
255 buffer = ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ];
257 if (ac3_decode_frame (&p_ac3dec->ac3_decoder, buffer)) {
262 vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
263 p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
264 vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
265 vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
270 /* If b_error is set, the ac3 decoder thread enters the error loop */
271 if ( p_ac3dec->b_error )
273 ErrorThread( p_ac3dec );
276 /* End of the ac3 decoder thread */
277 EndThread( p_ac3dec );
280 /*****************************************************************************
281 * ErrorThread : ac3 decoder's RunThread() error loop
282 *****************************************************************************/
283 static void ErrorThread( ac3dec_thread_t * p_ac3dec )
285 /* We take the lock, because we are going to read/write the start/end
286 * indexes of the decoder fifo */
287 vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
289 /* Wait until a `die' order is sent */
290 while( !p_ac3dec->b_die )
292 /* Trash all received PES packets */
293 while( !DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
295 input_NetlistFreePES( p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
296 DECODER_FIFO_INCSTART( p_ac3dec->fifo );
299 /* Waiting for the input thread to put new PES packets in the fifo */
300 vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
303 /* We can release the lock before leaving */
304 vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
307 /*****************************************************************************
308 * EndThread : ac3 decoder thread destruction
309 *****************************************************************************/
310 static void EndThread( ac3dec_thread_t * p_ac3dec )
312 intf_DbgMsg( "ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec );
314 /* If the audio output fifo was created, we destroy it */
315 if ( p_ac3dec->p_aout_fifo != NULL )
317 aout_DestroyFifo( p_ac3dec->p_aout_fifo );
319 /* Make sure the output thread leaves the NextFrame() function */
320 vlc_mutex_lock( &(p_ac3dec->p_aout_fifo->data_lock) );
321 vlc_cond_signal( &(p_ac3dec->p_aout_fifo->data_wait) );
322 vlc_mutex_unlock( &(p_ac3dec->p_aout_fifo->data_lock) );
325 /* Destroy descriptor */
328 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec );
331 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream)
333 ac3dec_thread_t * p_ac3dec = p_byte_stream->info;
335 /* We are looking for the next TS packet that contains real data,
336 * and not just a PES header */
338 /* We were reading the last TS packet of this PES packet... It's
339 * time to jump to the next PES packet */
340 if (p_ac3dec->p_ts->p_next_ts == NULL) {
343 /* We are going to read/write the start and end indexes of the
344 * decoder fifo and to use the fifo's conditional variable,
345 * that's why we need to take the lock before */
346 vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
348 /* Is the input thread dying ? */
349 if (p_ac3dec->p_input->b_die) {
350 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
354 /* We should increase the start index of the decoder fifo, but
355 * if we do this now, the input thread could overwrite the
356 * pointer to the current PES packet, and we weren't able to
357 * give it back to the netlist. That's why we free the PES
359 input_NetlistFreePES (p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
361 DECODER_FIFO_INCSTART (p_ac3dec->fifo);
363 while (DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
364 vlc_cond_wait (&p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
366 if (p_ac3dec->p_input->b_die) {
367 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
372 /* The next byte could be found in the next PES packet */
373 p_ac3dec->p_ts = DECODER_FIFO_START (p_ac3dec->fifo)->p_first_ts;
375 /* parse ac3 magic header */
376 ptr = p_ac3dec->p_ts->buffer [p_ac3dec->p_ts->i_payload_start+2];
378 ptr |= p_ac3dec->p_ts->buffer [p_ac3dec->p_ts->i_payload_start+3];
379 p_ac3dec->sync_ptr = ptr;
380 p_ac3dec->p_ts->i_payload_start += 4;
382 /* We can release the fifo's data lock */
383 vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
386 /* Perhaps the next TS packet of the current PES packet contains
387 * real data (ie its payload's size is greater than 0) */
389 p_ac3dec->p_ts = p_ac3dec->p_ts->p_next_ts;
391 } while (p_ac3dec->p_ts->i_payload_start == p_ac3dec->p_ts->i_payload_end);
392 p_byte_stream->p_byte =
393 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start;
394 p_byte_stream->p_end =
395 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end;