1 /*****************************************************************************
2 * ac3_decoder_thread.c: ac3 decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
26 * - vérifier l'état de la fifo de sortie avant d'y stocker les samples
28 * - vlc_cond_signal() / vlc_cond_wait()
32 /*****************************************************************************
34 *****************************************************************************/
37 #include <unistd.h> /* getpid() */
39 #include <stdio.h> /* "intf_msg.h" */
40 #include <stdlib.h> /* malloc(), free() */
41 #include <sys/types.h> /* on BSD, uio.h needs types.h */
42 #include <sys/uio.h> /* "input.h" */
49 #include "debug.h" /* "input_netlist.h" */
51 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
53 #include "input.h" /* pes_packet_t */
54 #include "input_netlist.h" /* input_NetlistFreePES() */
55 #include "decoder_fifo.h" /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
57 #include "audio_output.h"
59 #include "ac3_decoder.h"
60 #include "ac3_decoder_thread.h"
62 #define AC3DEC_FRAME_SIZE (2*1536)
64 /*****************************************************************************
66 *****************************************************************************/
67 static int InitThread (ac3dec_thread_t * p_adec);
68 static void RunThread (ac3dec_thread_t * p_adec);
69 static void ErrorThread (ac3dec_thread_t * p_adec);
70 static void EndThread (ac3dec_thread_t * p_adec);
72 /*****************************************************************************
73 * ac3dec_CreateThread: creates an ac3 decoder thread
74 *****************************************************************************/
75 ac3dec_thread_t * ac3dec_CreateThread (input_thread_t * p_input)
77 ac3dec_thread_t * p_ac3dec;
79 intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread\n" );
81 /* Allocate the memory needed to store the thread's structure */
82 if ((p_ac3dec = (ac3dec_thread_t *)malloc (sizeof(ac3dec_thread_t))) == NULL) {
83 intf_ErrMsg ( "ac3dec error: not enough memory "
84 "for ac3dec_CreateThread() to create the new thread\n");
89 * Initialize the thread properties
92 p_ac3dec->b_error = 0;
95 * Initialize the input properties
97 /* Initialize the decoder fifo's data lock and conditional variable and set
98 * its buffer as empty */
99 vlc_mutex_init (&p_ac3dec->fifo.data_lock);
100 vlc_cond_init (&p_ac3dec->fifo.data_wait);
101 p_ac3dec->fifo.i_start = 0;
102 p_ac3dec->fifo.i_end = 0;
104 /* Initialize the ac3 decoder structures */
105 ac3_init (&p_ac3dec->ac3_decoder);
107 /* Initialize the bit stream structure */
108 p_ac3dec->p_input = p_input;
111 * Initialize the output properties
113 p_ac3dec->p_aout = p_input->p_aout;
114 p_ac3dec->p_aout_fifo = NULL;
116 /* Spawn the ac3 decoder thread */
117 if (vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec)) {
118 intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread\n" );
123 intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec);
127 /*****************************************************************************
128 * ac3dec_DestroyThread: destroys an ac3 decoder thread
129 *****************************************************************************/
130 void ac3dec_DestroyThread (ac3dec_thread_t * p_ac3dec)
132 intf_DbgMsg ("ac3dec debug: requesting termination of ac3 decoder thread %p\n", p_ac3dec);
134 /* Ask thread to kill itself */
137 /* Make sure the decoder thread leaves the GetByte() function */
138 vlc_mutex_lock (&(p_ac3dec->fifo.data_lock));
139 vlc_cond_signal (&(p_ac3dec->fifo.data_wait));
140 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
142 /* Waiting for the decoder thread to exit */
143 /* Remove this as soon as the "status" flag is implemented */
144 vlc_thread_join (p_ac3dec->thread_id);
147 /* Following functions are local */
149 /*****************************************************************************
150 * InitThread : initialize an ac3 decoder thread
151 *****************************************************************************/
152 static int InitThread (ac3dec_thread_t * p_ac3dec)
154 aout_fifo_t aout_fifo;
155 ac3_byte_stream_t * byte_stream;
157 intf_DbgMsg ("ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec);
159 /* Our first job is to initialize the bit stream structure with the
160 * beginning of the input stream */
161 vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
162 while (DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
163 if (p_ac3dec->b_die) {
164 vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
167 vlc_cond_wait (&p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock);
169 p_ac3dec->p_ts = DECODER_FIFO_START (p_ac3dec->fifo)->p_first_ts;
170 byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
171 byte_stream->p_byte =
172 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start;
174 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end;
175 byte_stream->info = p_ac3dec;
176 vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
178 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
179 aout_fifo.i_channels = 2;
180 aout_fifo.b_stereo = 1;
182 aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
184 /* Creating the audio output fifo */
185 if ((p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL) {
189 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec);
193 /*****************************************************************************
194 * RunThread : ac3 decoder thread
195 *****************************************************************************/
196 static void RunThread (ac3dec_thread_t * p_ac3dec)
200 intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid());
202 msleep (INPUT_PTS_DELAY);
204 /* Initializing the ac3 decoder thread */
205 if (InitThread (p_ac3dec)) /* XXX?? */ {
206 p_ac3dec->b_error = 1;
210 p_ac3dec->sync_ptr = 0;
212 /* ac3 decoder thread's main loop */
213 /* FIXME : do we have enough room to store the decoded frames ?? */
214 while ((!p_ac3dec->b_die) && (!p_ac3dec->b_error)) {
216 ac3_sync_info_t sync_info;
218 if (!sync) { /* have to find a synchro point */
220 ac3_byte_stream_t * p_byte_stream;
222 intf_DbgMsg ("ac3dec: sync\n");
224 p_byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
226 /* first read till next ac3 magic header */
228 ac3_byte_stream_next (p_byte_stream);
229 } while ((!p_ac3dec->sync_ptr) &&
230 (!p_ac3dec->b_die) &&
231 (!p_ac3dec->b_error));
232 /* skip the specified number of bytes */
234 ptr = p_ac3dec->sync_ptr;
235 while (--ptr && (!p_ac3dec->b_die) && (!p_ac3dec->b_error)) {
236 if (p_byte_stream->p_byte >= p_byte_stream->p_end) {
237 ac3_byte_stream_next (p_byte_stream);
239 p_byte_stream->p_byte++;
242 /* we are in sync now */
245 p_ac3dec->sync_ptr = 0;
248 if (DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts) {
249 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
250 DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
252 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
255 if (ac3_sync_frame (&p_ac3dec->ac3_decoder, &sync_info)) {
260 p_ac3dec->p_aout_fifo->l_rate = sync_info.sample_rate;
262 buffer = ((s16 *)p_ac3dec->p_aout_fifo->buffer) + (p_ac3dec->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
264 if (ac3_decode_frame (&p_ac3dec->ac3_decoder, buffer)) {
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);
277 /* If b_error is set, the ac3 decoder thread enters the error loop */
278 if (p_ac3dec->b_error) {
279 ErrorThread (p_ac3dec);
282 /* End of the ac3 decoder thread */
283 EndThread (p_ac3dec);
286 /*****************************************************************************
287 * ErrorThread : ac3 decoder's RunThread() error loop
288 *****************************************************************************/
289 static void ErrorThread (ac3dec_thread_t * p_ac3dec)
291 /* We take the lock, because we are going to read/write the start/end
292 * indexes of the decoder fifo */
293 vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
295 /* Wait until a `die' order is sent */
296 while (!p_ac3dec->b_die) {
297 /* Trash all received PES packets */
298 while (!DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
299 input_NetlistFreePES (p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo));
300 DECODER_FIFO_INCSTART (p_ac3dec->fifo);
303 /* Waiting for the input thread to put new PES packets in the fifo */
304 vlc_cond_wait (&p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock);
307 /* We can release the lock before leaving */
308 vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
311 /*****************************************************************************
312 * EndThread : ac3 decoder thread destruction
313 *****************************************************************************/
314 static void EndThread (ac3dec_thread_t * p_ac3dec)
316 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec);
318 /* If the audio output fifo was created, we destroy it */
319 if (p_ac3dec->p_aout_fifo != NULL) {
320 aout_DestroyFifo (p_ac3dec->p_aout_fifo);
322 /* Make sure the output thread leaves the NextFrame() function */
323 vlc_mutex_lock (&(p_ac3dec->p_aout_fifo->data_lock));
324 vlc_cond_signal (&(p_ac3dec->p_aout_fifo->data_wait));
325 vlc_mutex_unlock (&(p_ac3dec->p_aout_fifo->data_lock));
328 /* Destroy descriptor */
331 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec);
334 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream)
336 ac3dec_thread_t * p_ac3dec = p_byte_stream->info;
338 /* We are looking for the next TS packet that contains real data,
339 * and not just a PES header */
341 /* We were reading the last TS packet of this PES packet... It's
342 * time to jump to the next PES packet */
343 if (p_ac3dec->p_ts->p_next_ts == NULL) {
346 /* We are going to read/write the start and end indexes of the
347 * decoder fifo and to use the fifo's conditional variable,
348 * that's why we need to take the lock before */
349 vlc_mutex_lock (&p_ac3dec->fifo.data_lock);
351 /* Is the input thread dying ? */
352 if (p_ac3dec->p_input->b_die) {
353 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
357 /* We should increase the start index of the decoder fifo, but
358 * if we do this now, the input thread could overwrite the
359 * pointer to the current PES packet, and we weren't able to
360 * give it back to the netlist. That's why we free the PES
362 input_NetlistFreePES (p_ac3dec->p_input, DECODER_FIFO_START(p_ac3dec->fifo));
364 DECODER_FIFO_INCSTART (p_ac3dec->fifo);
366 while (DECODER_FIFO_ISEMPTY(p_ac3dec->fifo)) {
367 vlc_cond_wait (&p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock);
369 if (p_ac3dec->p_input->b_die) {
370 vlc_mutex_unlock (&(p_ac3dec->fifo.data_lock));
375 /* The next byte could be found in the next PES packet */
376 p_ac3dec->p_ts = DECODER_FIFO_START (p_ac3dec->fifo)->p_first_ts;
378 /* parse ac3 magic header */
379 ptr = p_ac3dec->p_ts->buffer [p_ac3dec->p_ts->i_payload_start+2];
381 ptr |= p_ac3dec->p_ts->buffer [p_ac3dec->p_ts->i_payload_start+3];
382 p_ac3dec->sync_ptr = ptr;
383 p_ac3dec->p_ts->i_payload_start += 4;
385 /* We can release the fifo's data lock */
386 vlc_mutex_unlock (&p_ac3dec->fifo.data_lock);
389 /* Perhaps the next TS packet of the current PES packet contains
390 * real data (ie its payload's size is greater than 0) */
392 p_ac3dec->p_ts = p_ac3dec->p_ts->p_next_ts;
394 } while (p_ac3dec->p_ts->i_payload_start == p_ac3dec->p_ts->i_payload_end);
395 p_byte_stream->p_byte =
396 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_start;
397 p_byte_stream->p_end =
398 p_ac3dec->p_ts->buffer + p_ac3dec->p_ts->i_payload_end;