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->ac3_decoder.bit_stream.p_input = p_input;
89 p_ac3dec->ac3_decoder.bit_stream.p_decoder_fifo = &p_ac3dec->fifo;
90 p_ac3dec->ac3_decoder.bit_stream.fifo.buffer = 0;
91 p_ac3dec->ac3_decoder.bit_stream.fifo.i_available = 0;
94 * Initialize the output properties
96 p_ac3dec->p_aout = p_input->p_aout;
97 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 /*****************************************************************************
137 *****************************************************************************/
138 static __inline__ int decode_find_sync( ac3dec_thread_t * p_ac3dec )
140 while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
142 NeedBits( &(p_ac3dec->ac3_decoder.bit_stream), 16 );
143 if ( (p_ac3dec->ac3_decoder.bit_stream.fifo.buffer >> (32 - 16)) == 0x0b77 )
145 DumpBits( &(p_ac3dec->ac3_decoder.bit_stream), 16 );
146 p_ac3dec->ac3_decoder.total_bits_read = 16;
149 DumpBits( &(p_ac3dec->ac3_decoder.bit_stream), 1 ); /* XXX */
154 /*****************************************************************************
155 * InitThread : initialize an ac3 decoder thread
156 *****************************************************************************/
157 static int InitThread( ac3dec_thread_t * p_ac3dec )
159 aout_fifo_t aout_fifo;
161 intf_DbgMsg( "ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec );
163 /* Our first job is to initialize the bit stream structure with the
164 * beginning of the input stream */
165 vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
166 while ( DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
168 if ( p_ac3dec->b_die )
170 vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
173 vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
175 p_ac3dec->ac3_decoder.bit_stream.p_ts = DECODER_FIFO_START( p_ac3dec->fifo )->p_first_ts;
176 p_ac3dec->ac3_decoder.bit_stream.p_byte = p_ac3dec->ac3_decoder.bit_stream.p_ts->buffer + p_ac3dec->ac3_decoder.bit_stream.p_ts->i_payload_start;
177 p_ac3dec->ac3_decoder.bit_stream.p_end = p_ac3dec->ac3_decoder.bit_stream.p_ts->buffer + p_ac3dec->ac3_decoder.bit_stream.p_ts->i_payload_end;
178 vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
180 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
181 aout_fifo.i_channels = 2;
182 aout_fifo.b_stereo = 1;
184 aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
186 /* Creating the audio output fifo */
187 if ( (p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL )
192 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec );
196 /*****************************************************************************
197 * RunThread : ac3 decoder thread
198 *****************************************************************************/
199 static void RunThread( ac3dec_thread_t * p_ac3dec )
201 intf_DbgMsg( "ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid() );
203 msleep( INPUT_PTS_DELAY );
205 /* Initializing the ac3 decoder thread */
206 if ( InitThread(p_ac3dec) ) /* XXX */
208 p_ac3dec->b_error = 1;
211 /* ac3 decoder thread's main loop */
212 /* FIXME : do we have enough room to store the decoded frames ? */
213 while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
217 p_ac3dec->ac3_decoder.b_invalid = 0;
219 decode_find_sync( p_ac3dec );
221 if ( DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts )
223 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
224 DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
228 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
231 parse_syncinfo( &p_ac3dec->ac3_decoder );
232 switch ( p_ac3dec->ac3_decoder.syncinfo.fscod )
235 p_ac3dec->p_aout_fifo->l_rate = 48000;
239 p_ac3dec->p_aout_fifo->l_rate = 44100;
243 p_ac3dec->p_aout_fifo->l_rate = 32000;
247 fprintf( stderr, "ac3dec debug: invalid fscod\n" );
248 p_ac3dec->ac3_decoder.b_invalid = 1;
251 if ( p_ac3dec->ac3_decoder.b_invalid ) /* XXX */
256 parse_bsi( &p_ac3dec->ac3_decoder );
258 for (i = 0; i < 6; i++)
262 buffer = ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ];
264 if (ac3_audio_block (&p_ac3dec->ac3_decoder, buffer))
268 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
269 vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
270 p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
271 vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
272 vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
275 parse_auxdata( &p_ac3dec->ac3_decoder );
279 /* If b_error is set, the ac3 decoder thread enters the error loop */
280 if ( p_ac3dec->b_error )
282 ErrorThread( p_ac3dec );
285 /* End of the ac3 decoder thread */
286 EndThread( p_ac3dec );
289 /*****************************************************************************
290 * ErrorThread : ac3 decoder's RunThread() error loop
291 *****************************************************************************/
292 static void ErrorThread( ac3dec_thread_t * p_ac3dec )
294 /* We take the lock, because we are going to read/write the start/end
295 * indexes of the decoder fifo */
296 vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
298 /* Wait until a `die' order is sent */
299 while( !p_ac3dec->b_die )
301 /* Trash all received PES packets */
302 while( !DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
304 input_NetlistFreePES( p_ac3dec->ac3_decoder.bit_stream.p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
305 DECODER_FIFO_INCSTART( p_ac3dec->fifo );
308 /* Waiting for the input thread to put new PES packets in the fifo */
309 vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
312 /* We can release the lock before leaving */
313 vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
316 /*****************************************************************************
317 * EndThread : ac3 decoder thread destruction
318 *****************************************************************************/
319 static void EndThread( ac3dec_thread_t * p_ac3dec )
321 intf_DbgMsg( "ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec );
323 /* If the audio output fifo was created, we destroy it */
324 if ( p_ac3dec->p_aout_fifo != NULL )
326 aout_DestroyFifo( p_ac3dec->p_aout_fifo );
328 /* Make sure the output thread leaves the NextFrame() function */
329 vlc_mutex_lock( &(p_ac3dec->p_aout_fifo->data_lock) );
330 vlc_cond_signal( &(p_ac3dec->p_aout_fifo->data_wait) );
331 vlc_mutex_unlock( &(p_ac3dec->p_aout_fifo->data_lock) );
334 /* Destroy descriptor */
337 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec );