1 /*****************************************************************************
2 * ac3_decoder.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"
45 #include "ac3_parse.h"
46 #include "ac3_imdct.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 static int InitThread ( ac3dec_thread_t * p_adec );
52 static void RunThread ( ac3dec_thread_t * p_adec );
53 static void ErrorThread ( ac3dec_thread_t * p_adec );
54 static void EndThread ( ac3dec_thread_t * p_adec );
56 /*****************************************************************************
57 * ac3dec_CreateThread: creates an ac3 decoder thread
58 *****************************************************************************/
59 ac3dec_thread_t * ac3dec_CreateThread( input_thread_t * p_input )
61 ac3dec_thread_t * p_ac3dec;
63 intf_DbgMsg("ac3dec debug: creating ac3 decoder thread\n");
65 /* Allocate the memory needed to store the thread's structure */
66 if ( (p_ac3dec = (ac3dec_thread_t *)malloc( sizeof(ac3dec_thread_t) )) == NULL )
68 intf_ErrMsg("ac3dec error: not enough memory for ac3dec_CreateThread() to create the new thread\n");
73 * Initialize the thread properties
76 p_ac3dec->b_error = 0;
79 * Initialize the input properties
81 /* Initialize the decoder fifo's data lock and conditional variable and set
82 * its buffer as empty */
83 vlc_mutex_init( &p_ac3dec->fifo.data_lock );
84 vlc_cond_init( &p_ac3dec->fifo.data_wait );
85 p_ac3dec->fifo.i_start = 0;
86 p_ac3dec->fifo.i_end = 0;
87 /* Initialize the bit stream structure */
88 p_ac3dec->p_input = p_input;
89 p_ac3dec->ac3_decoder.bit_stream.buffer = 0;
90 p_ac3dec->ac3_decoder.bit_stream.i_available = 0;
93 * Initialize the output properties
95 p_ac3dec->p_aout = p_input->p_aout;
96 p_ac3dec->p_aout_fifo = NULL;
100 /* Spawn the ac3 decoder thread */
101 if ( vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec) )
103 intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread\n" );
108 intf_DbgMsg( "ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec );
112 /*****************************************************************************
113 * ac3dec_DestroyThread: destroys an ac3 decoder thread
114 *****************************************************************************/
115 void ac3dec_DestroyThread( ac3dec_thread_t * p_ac3dec )
117 intf_DbgMsg( "ac3dec debug: requesting termination of ac3 decoder thread %p\n", p_ac3dec );
119 /* Ask thread to kill itself */
122 /* Make sure the decoder thread leaves the GetByte() function */
123 vlc_mutex_lock( &(p_ac3dec->fifo.data_lock) );
124 vlc_cond_signal( &(p_ac3dec->fifo.data_wait) );
125 vlc_mutex_unlock( &(p_ac3dec->fifo.data_lock) );
127 /* Waiting for the decoder thread to exit */
128 /* Remove this as soon as the "status" flag is implemented */
129 vlc_thread_join( p_ac3dec->thread_id );
132 /* Following functions are local */
134 /*****************************************************************************
136 *****************************************************************************/
137 static __inline__ int decode_find_sync( ac3dec_thread_t * p_ac3dec )
139 while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
141 if (! (ac3_test_sync (&p_ac3dec->ac3_decoder)))
147 /*****************************************************************************
148 * InitThread : initialize an ac3 decoder thread
149 *****************************************************************************/
150 static int InitThread( ac3dec_thread_t * p_ac3dec )
152 aout_fifo_t aout_fifo;
154 intf_DbgMsg( "ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec );
156 /* Our first job is to initialize the bit stream structure with the
157 * beginning of the input stream */
158 vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
159 while ( DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
161 if ( p_ac3dec->b_die )
163 vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
166 vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
168 p_ac3dec->p_ts = DECODER_FIFO_START( p_ac3dec->fifo )->p_first_ts;
169 p_ac3dec->ac3_decoder.bit_stream.byte_stream.p_byte =
170 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start;
171 p_ac3dec->ac3_decoder.bit_stream.byte_stream.p_end =
172 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end;
173 p_ac3dec->ac3_decoder.bit_stream.byte_stream.info = p_ac3dec;
174 vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
176 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
177 aout_fifo.i_channels = 2;
178 aout_fifo.b_stereo = 1;
180 aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
182 /* Creating the audio output fifo */
183 if ( (p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL )
188 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec );
192 /*****************************************************************************
193 * RunThread : ac3 decoder thread
194 *****************************************************************************/
195 static void RunThread( ac3dec_thread_t * p_ac3dec )
197 intf_DbgMsg( "ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid() );
199 msleep( INPUT_PTS_DELAY );
201 /* Initializing the ac3 decoder thread */
202 if ( InitThread(p_ac3dec) ) /* XXX?? */
204 p_ac3dec->b_error = 1;
207 /* ac3 decoder thread's main loop */
208 /* FIXME : do we have enough room to store the decoded frames ?? */
209 while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
213 decode_find_sync( p_ac3dec );
215 if ( DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts )
217 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
218 DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
222 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
225 parse_syncinfo( &p_ac3dec->ac3_decoder );
226 switch ( p_ac3dec->ac3_decoder.syncinfo.fscod )
229 p_ac3dec->p_aout_fifo->l_rate = 48000;
233 p_ac3dec->p_aout_fifo->l_rate = 44100;
237 p_ac3dec->p_aout_fifo->l_rate = 32000;
241 fprintf( stderr, "ac3dec debug: invalid fscod\n" );
245 parse_bsi( &p_ac3dec->ac3_decoder );
247 for (i = 0; i < 6; i++)
251 buffer = ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ];
253 if (ac3_audio_block (&p_ac3dec->ac3_decoder, buffer))
257 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
258 vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
259 p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
260 vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
261 vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
264 parse_auxdata( &p_ac3dec->ac3_decoder );
268 /* If b_error is set, the ac3 decoder thread enters the error loop */
269 if ( p_ac3dec->b_error )
271 ErrorThread( p_ac3dec );
274 /* End of the ac3 decoder thread */
275 EndThread( p_ac3dec );
278 /*****************************************************************************
279 * ErrorThread : ac3 decoder's RunThread() error loop
280 *****************************************************************************/
281 static void ErrorThread( ac3dec_thread_t * p_ac3dec )
283 /* We take the lock, because we are going to read/write the start/end
284 * indexes of the decoder fifo */
285 vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
287 /* Wait until a `die' order is sent */
288 while( !p_ac3dec->b_die )
290 /* Trash all received PES packets */
291 while( !DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
293 input_NetlistFreePES( p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
294 DECODER_FIFO_INCSTART( p_ac3dec->fifo );
297 /* Waiting for the input thread to put new PES packets in the fifo */
298 vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
301 /* We can release the lock before leaving */
302 vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
305 /*****************************************************************************
306 * EndThread : ac3 decoder thread destruction
307 *****************************************************************************/
308 static void EndThread( ac3dec_thread_t * p_ac3dec )
310 intf_DbgMsg( "ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec );
312 /* If the audio output fifo was created, we destroy it */
313 if ( p_ac3dec->p_aout_fifo != NULL )
315 aout_DestroyFifo( p_ac3dec->p_aout_fifo );
317 /* Make sure the output thread leaves the NextFrame() function */
318 vlc_mutex_lock( &(p_ac3dec->p_aout_fifo->data_lock) );
319 vlc_cond_signal( &(p_ac3dec->p_aout_fifo->data_wait) );
320 vlc_mutex_unlock( &(p_ac3dec->p_aout_fifo->data_lock) );
323 /* Destroy descriptor */
326 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec );
329 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream)
331 ac3dec_thread_t * p_ac3dec = p_byte_stream->info;
333 /* We are looking for the next TS packet that contains real data,
334 * and not just a PES header */
336 /* We were reading the last TS packet of this PES packet... It's
337 * time to jump to the next PES packet */
338 if (p_ac3dec->p_ts->p_next_ts == NULL) {
339 /* We are going to read/write the start and end indexes of the
340 * decoder fifo and to use the fifo's conditional variable,
341 * that's why we need to take the lock before */
342 vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
344 /* Is the input thread dying ? */
345 if (p_ac3dec->p_input->b_die) {
346 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
350 /* We should increase the start index of the decoder fifo, but
351 * if we do this now, the input thread could overwrite the
352 * pointer to the current PES packet, and we weren't able to
353 * give it back to the netlist. That's why we free the PES
355 input_NetlistFreePES (p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
357 DECODER_FIFO_INCSTART (p_ac3dec->fifo);
359 while (DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
360 vlc_cond_wait (&p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
362 if (p_ac3dec->p_input->b_die) {
363 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
368 /* The next byte could be found in the next PES packet */
369 p_ac3dec->p_ts = DECODER_FIFO_START (p_ac3dec->fifo)->p_first_ts;
371 /* We can release the fifo's data lock */
372 vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
375 /* Perhaps the next TS packet of the current PES packet contains
376 * real data (ie its payload's size is greater than 0) */
378 p_ac3dec->p_ts = p_ac3dec->p_ts->p_next_ts;
380 } while (p_ac3dec->p_ts->i_payload_start == p_ac3dec->p_ts->i_payload_end);
381 p_byte_stream->p_byte =
382 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start;
383 p_byte_stream->p_end =
384 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end;