1 /*****************************************************************************
2 * ac3_decoder_thread.c: ac3 decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: ac3_decoder_thread.c,v 1.23 2001/01/05 18:46:44 massiot Exp $
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() */
49 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
51 #include "stream_control.h"
52 #include "input_ext-dec.h"
54 #include "audio_output.h"
56 #include "ac3_decoder.h"
57 #include "ac3_decoder_thread.h"
59 #define AC3DEC_FRAME_SIZE (2*1536)
61 /*****************************************************************************
63 *****************************************************************************/
64 static int InitThread (ac3dec_thread_t * p_adec);
65 static void RunThread (ac3dec_thread_t * p_adec);
66 static void ErrorThread (ac3dec_thread_t * p_adec);
67 static void EndThread (ac3dec_thread_t * p_adec);
69 /*****************************************************************************
70 * ac3dec_CreateThread: creates an ac3 decoder thread
71 *****************************************************************************/
72 vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
74 ac3dec_thread_t * p_ac3dec;
76 intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread" );
78 /* Allocate the memory needed to store the thread's structure */
79 if ((p_ac3dec = (ac3dec_thread_t *)malloc (sizeof(ac3dec_thread_t))) == NULL)
81 intf_ErrMsg ( "ac3dec error: not enough memory "
82 "for ac3dec_CreateThread() to create the new thread");
87 * Initialize the thread properties
89 p_ac3dec->p_config = p_config;
90 p_ac3dec->p_fifo = p_config->decoder_config.p_decoder_fifo;
92 /* Initialize the ac3 decoder structures */
93 ac3_init (&p_ac3dec->ac3_decoder);
96 * Initialize the output properties
98 p_ac3dec->p_aout = p_config->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" );
109 intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created", p_ac3dec);
110 return p_ac3dec->thread_id;
113 /* Following functions are local */
115 /*****************************************************************************
116 * InitThread : initialize an ac3 decoder thread
117 *****************************************************************************/
118 static int InitThread (ac3dec_thread_t * p_ac3dec)
120 aout_fifo_t aout_fifo;
121 ac3_byte_stream_t * byte_stream;
123 intf_DbgMsg ("ac3dec debug: initializing ac3 decoder thread %p", p_ac3dec);
125 /* Get the first data packet. */
126 vlc_mutex_lock( &p_ac3dec->p_fifo->data_lock );
127 while ( DECODER_FIFO_ISEMPTY( *p_ac3dec->p_fifo ) )
129 if ( p_ac3dec->p_fifo->b_die )
131 vlc_mutex_unlock( &p_ac3dec->p_fifo->data_lock );
134 vlc_cond_wait( &p_ac3dec->p_fifo->data_wait, &p_ac3dec->p_fifo->data_lock );
136 p_ac3dec->p_data = DECODER_FIFO_START(*p_ac3dec->p_fifo)->p_first;
137 byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
138 byte_stream->p_byte = p_ac3dec->p_data->p_payload_start;
139 byte_stream->p_end = p_ac3dec->p_data->p_payload_end;
140 byte_stream->info = p_ac3dec;
141 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
143 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
144 aout_fifo.i_channels = 2;
145 aout_fifo.b_stereo = 1;
147 aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
149 /* Creating the audio output fifo */
150 if ((p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL)
155 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p initialized", p_ac3dec);
159 /*****************************************************************************
160 * RunThread : ac3 decoder thread
161 *****************************************************************************/
162 static void RunThread (ac3dec_thread_t * p_ac3dec)
166 intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)", p_ac3dec, getpid());
168 /* FIXME ! Qu'est-ce que c'est que ce bordel !?!?!?!? --Meuuh */
169 //msleep (INPUT_PTS_DELAY);
171 /* Initializing the ac3 decoder thread */
172 if (InitThread (p_ac3dec)) /* XXX?? */
174 p_ac3dec->p_fifo->b_error = 1;
178 p_ac3dec->sync_ptr = 0;
180 /* ac3 decoder thread's main loop */
181 /* FIXME : do we have enough room to store the decoded frames ?? */
182 while ((!p_ac3dec->p_fifo->b_die) && (!p_ac3dec->p_fifo->b_error))
185 ac3_sync_info_t sync_info;
187 if (!sync) { /* have to find a synchro point */
189 ac3_byte_stream_t * p_byte_stream;
191 intf_DbgMsg ("ac3dec: sync");
193 p_byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
195 /* first read till next ac3 magic header */
198 ac3_byte_stream_next (p_byte_stream);
199 } while ((!p_ac3dec->sync_ptr) &&
200 (!p_ac3dec->p_fifo->b_die) &&
201 (!p_ac3dec->p_fifo->b_error));
202 /* skip the specified number of bytes */
204 if( p_ac3dec->p_fifo->b_die || p_ac3dec->p_fifo->b_error )
209 ptr = p_ac3dec->sync_ptr;
210 while (--ptr && (!p_ac3dec->p_fifo->b_die) && (!p_ac3dec->p_fifo->b_error))
212 if (p_byte_stream->p_byte >= p_byte_stream->p_end)
214 ac3_byte_stream_next (p_byte_stream);
216 p_byte_stream->p_byte++;
219 if( p_ac3dec->p_fifo->b_die || p_ac3dec->p_fifo->b_error )
224 /* we are in sync now */
226 p_ac3dec->sync_ptr = 0;
229 if (DECODER_FIFO_START(*p_ac3dec->p_fifo)->i_pts)
231 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(*p_ac3dec->p_fifo)->i_pts;
232 DECODER_FIFO_START(*p_ac3dec->p_fifo)->i_pts = 0;
236 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
239 if (ac3_sync_frame (&p_ac3dec->ac3_decoder, &sync_info))
245 p_ac3dec->p_aout_fifo->l_rate = sync_info.sample_rate;
247 buffer = ((s16 *)p_ac3dec->p_aout_fifo->buffer) + (p_ac3dec->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
249 if (ac3_decode_frame (&p_ac3dec->ac3_decoder, buffer))
255 vlc_mutex_lock (&p_ac3dec->p_aout_fifo->data_lock);
256 p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
257 vlc_cond_signal (&p_ac3dec->p_aout_fifo->data_wait);
258 vlc_mutex_unlock (&p_ac3dec->p_aout_fifo->data_lock);
263 /* If b_error is set, the ac3 decoder thread enters the error loop */
264 if (p_ac3dec->p_fifo->b_error)
266 ErrorThread (p_ac3dec);
269 /* End of the ac3 decoder thread */
270 EndThread (p_ac3dec);
273 /*****************************************************************************
274 * ErrorThread : ac3 decoder's RunThread() error loop
275 *****************************************************************************/
276 static void ErrorThread (ac3dec_thread_t * p_ac3dec)
278 /* We take the lock, because we are going to read/write the start/end
279 * indexes of the decoder fifo */
280 vlc_mutex_lock (&p_ac3dec->p_fifo->data_lock);
282 /* Wait until a `die' order is sent */
283 while (!p_ac3dec->p_fifo->b_die)
285 /* Trash all received PES packets */
286 while (!DECODER_FIFO_ISEMPTY(*p_ac3dec->p_fifo))
288 p_ac3dec->p_fifo->pf_delete_pes(p_ac3dec->p_fifo->p_packets_mgt,
289 DECODER_FIFO_START(*p_ac3dec->p_fifo));
290 DECODER_FIFO_INCSTART (*p_ac3dec->p_fifo);
293 /* Waiting for the input thread to put new PES packets in the fifo */
294 vlc_cond_wait (&p_ac3dec->p_fifo->data_wait,
295 &p_ac3dec->p_fifo->data_lock);
298 /* We can release the lock before leaving */
299 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
302 /*****************************************************************************
303 * EndThread : ac3 decoder thread destruction
304 *****************************************************************************/
305 static void EndThread (ac3dec_thread_t * p_ac3dec)
307 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3dec);
309 /* If the audio output fifo was created, we destroy it */
310 if (p_ac3dec->p_aout_fifo != NULL)
312 aout_DestroyFifo (p_ac3dec->p_aout_fifo);
314 /* Make sure the output thread leaves the NextFrame() function */
315 vlc_mutex_lock (&(p_ac3dec->p_aout_fifo->data_lock));
316 vlc_cond_signal (&(p_ac3dec->p_aout_fifo->data_wait));
317 vlc_mutex_unlock (&(p_ac3dec->p_aout_fifo->data_lock));
320 /* Destroy descriptor */
321 free( p_ac3dec->p_config );
324 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3dec);
327 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream)
329 ac3dec_thread_t * p_ac3dec = p_byte_stream->info;
331 /* We are looking for the next TS packet that contains real data,
332 * and not just a PES header */
335 /* We were reading the last TS packet of this PES packet... It's
336 * time to jump to the next PES packet */
337 if (p_ac3dec->p_data->p_next == NULL)
341 /* We are going to read/write the start and end indexes of the
342 * decoder fifo and to use the fifo's conditional variable,
343 * that's why we need to take the lock before */
344 vlc_mutex_lock (&p_ac3dec->p_fifo->data_lock);
346 /* Is the input thread dying ? */
347 if (p_ac3dec->p_fifo->b_die)
349 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
353 /* We should increase the start index of the decoder fifo, but
354 * if we do this now, the input thread could overwrite the
355 * pointer to the current PES packet, and we weren't able to
356 * give it back to the netlist. That's why we free the PES
358 p_ac3dec->p_fifo->pf_delete_pes(p_ac3dec->p_fifo->p_packets_mgt,
359 DECODER_FIFO_START(*p_ac3dec->p_fifo));
361 DECODER_FIFO_INCSTART (*p_ac3dec->p_fifo);
363 while (DECODER_FIFO_ISEMPTY(*p_ac3dec->p_fifo))
365 vlc_cond_wait(&p_ac3dec->p_fifo->data_wait, &p_ac3dec->p_fifo->data_lock);
367 if (p_ac3dec->p_fifo->b_die)
369 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
374 /* The next byte could be found in the next PES packet */
375 p_ac3dec->p_data = DECODER_FIFO_START(*p_ac3dec->p_fifo)->p_first;
377 /* parse ac3 magic header */
378 ptr = *(p_ac3dec->p_data->p_payload_start + 1);
380 ptr |= *(p_ac3dec->p_data->p_payload_start + 2);
381 p_ac3dec->sync_ptr = ptr;
382 p_ac3dec->p_data->p_payload_start += 3;
384 /* We can release the fifo's data lock */
385 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
387 /* Perhaps the next TS packet of the current PES packet contains
388 * real data (ie its payload's size is greater than 0) */
391 p_ac3dec->p_data = p_ac3dec->p_data->p_next;
393 } while (p_ac3dec->p_data->p_payload_start == p_ac3dec->p_data->p_payload_end);
394 p_byte_stream->p_byte = p_ac3dec->p_data->p_payload_start;
395 p_byte_stream->p_end = p_ac3dec->p_data->p_payload_end;