+ if( ac3_adec_Init( p_ac3thread ) )
+ {
+ intf_ErrMsg( "ac3_adec error : could not initialize thread" );
+ return( -1 );
+ }
+
+ sync = 0;
+ p_ac3thread->sync_ptr = 0;
+
+ /* ac3 decoder thread's main loop */
+ /* FIXME : do we have enough room to store the decoded frames ?? */
+ while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
+ {
+ s16 * buffer;
+ ac3_sync_info_t sync_info;
+ int ptr;
+
+ if (!sync) {
+ do {
+ GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
+ } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
+ && (!p_ac3thread->p_fifo->b_error));
+
+ ptr = p_ac3thread->sync_ptr;
+
+ while(ptr-- && (!p_ac3thread->p_fifo->b_die)
+ && (!p_ac3thread->p_fifo->b_error))
+ {
+ p_ac3thread->ac3_decoder->bit_stream.p_byte++;
+ }
+
+ /* we are in sync now */
+ sync = 1;
+ }
+
+ if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
+ {
+ p_ac3thread->p_aout_fifo->date[
+ p_ac3thread->p_aout_fifo->l_end_frame] =
+ DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts;
+ DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts = 0;
+ } else {
+ p_ac3thread->p_aout_fifo->date[
+ p_ac3thread->p_aout_fifo->l_end_frame] =
+ LAST_MDATE;
+ }
+
+ if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
+ {
+ sync = 0;
+ goto bad_frame;
+ }
+
+ p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
+
+ buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
+ (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
+
+ if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
+ {
+ sync = 0;
+ goto bad_frame;
+ }
+
+ vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
+ p_ac3thread->p_aout_fifo->l_end_frame =
+ (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
+ vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
+ vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
+
+ bad_frame:
+ RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
+ }
+
+ /* If b_error is set, the ac3 decoder thread enters the error loop */
+ if (p_ac3thread->p_fifo->b_error)
+ {
+ ac3_adec_ErrorThread (p_ac3thread);
+ }
+
+ /* End of the ac3 decoder thread */
+ ac3_adec_EndThread (p_ac3thread);
+
+ return( 0 );
+}
+
+
+/*****************************************************************************
+ * ac3_adec_Init: initialize data before entering main loop
+ *****************************************************************************/
+static int ac3_adec_Init( ac3dec_thread_t * p_ac3thread )
+{
+ /*
+ * Thread properties
+ */
+ p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;