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 "stream_control.h"
54 #include "input_ext-dec.h"
56 #include "audio_output.h"
58 #include "ac3_decoder.h"
59 #include "ac3_decoder_thread.h"
61 #define AC3DEC_FRAME_SIZE (2*1536)
63 /*****************************************************************************
65 *****************************************************************************/
66 static int InitThread (ac3dec_thread_t * p_adec);
67 static void RunThread (ac3dec_thread_t * p_adec);
68 static void ErrorThread (ac3dec_thread_t * p_adec);
69 static void EndThread (ac3dec_thread_t * p_adec);
71 /*****************************************************************************
72 * ac3dec_CreateThread: creates an ac3 decoder thread
73 *****************************************************************************/
74 vlc_thread_t ac3dec_CreateThread( adec_config_t * p_config )
76 ac3dec_thread_t * p_ac3dec;
78 intf_DbgMsg( "ac3dec debug: creating ac3 decoder thread\n" );
80 /* Allocate the memory needed to store the thread's structure */
81 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;
93 p_ac3dec->p_config = p_config;
94 p_ac3dec->p_fifo = p_config->decoder_config.p_decoder_fifo;
96 /* Initialize the ac3 decoder structures */
97 ac3_init (&p_ac3dec->ac3_decoder);
100 * Initialize the output properties
102 p_ac3dec->p_aout = p_config->p_aout;
103 p_ac3dec->p_aout_fifo = NULL;
105 /* Spawn the ac3 decoder thread */
106 if (vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec))
108 intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread\n" );
113 intf_DbgMsg ("ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec);
114 return p_ac3dec->thread_id;
117 /* Following functions are local */
119 /*****************************************************************************
120 * InitThread : initialize an ac3 decoder thread
121 *****************************************************************************/
122 static int InitThread (ac3dec_thread_t * p_ac3dec)
124 aout_fifo_t aout_fifo;
125 ac3_byte_stream_t * byte_stream;
127 intf_DbgMsg ("ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec);
129 p_ac3dec->p_data = DECODER_FIFO_START(*p_ac3dec->p_fifo)->p_first;
130 byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
131 byte_stream->p_byte = p_ac3dec->p_data->p_payload_start;
132 byte_stream->p_end = p_ac3dec->p_data->p_payload_end;
133 byte_stream->info = p_ac3dec;
134 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
136 aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
137 aout_fifo.i_channels = 2;
138 aout_fifo.b_stereo = 1;
140 aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
142 /* Creating the audio output fifo */
143 if ((p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL)
148 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec);
152 /*****************************************************************************
153 * RunThread : ac3 decoder thread
154 *****************************************************************************/
155 static void RunThread (ac3dec_thread_t * p_ac3dec)
159 intf_DbgMsg ("ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid());
161 /* FIXME ! Qu'est-ce que c'est que ce bordel !?!?!?!? --Meuuh */
162 //msleep (INPUT_PTS_DELAY);
164 /* Initializing the ac3 decoder thread */
165 if (InitThread (p_ac3dec)) /* XXX?? */
167 p_ac3dec->b_error = 1;
171 p_ac3dec->sync_ptr = 0;
173 /* ac3 decoder thread's main loop */
174 /* FIXME : do we have enough room to store the decoded frames ?? */
175 while ((!p_ac3dec->b_die) && (!p_ac3dec->b_error))
178 ac3_sync_info_t sync_info;
180 if (!sync) { /* have to find a synchro point */
182 ac3_byte_stream_t * p_byte_stream;
184 intf_DbgMsg ("ac3dec: sync\n");
186 p_byte_stream = ac3_byte_stream (&p_ac3dec->ac3_decoder);
188 /* first read till next ac3 magic header */
191 ac3_byte_stream_next (p_byte_stream);
192 } while ((!p_ac3dec->sync_ptr) &&
193 (!p_ac3dec->b_die) &&
194 (!p_ac3dec->b_error));
195 /* skip the specified number of bytes */
197 if( p_ac3dec->b_die || p_ac3dec->b_error )
202 ptr = p_ac3dec->sync_ptr;
203 while (--ptr && (!p_ac3dec->b_die) && (!p_ac3dec->b_error))
205 if (p_byte_stream->p_byte >= p_byte_stream->p_end)
207 ac3_byte_stream_next (p_byte_stream);
209 p_byte_stream->p_byte++;
212 if( p_ac3dec->b_die || p_ac3dec->b_error )
217 /* we are in sync now */
219 p_ac3dec->sync_ptr = 0;
222 if (DECODER_FIFO_START(*p_ac3dec->p_fifo)->b_has_pts)
224 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(*p_ac3dec->p_fifo)->i_pts;
225 DECODER_FIFO_START(*p_ac3dec->p_fifo)->b_has_pts = 0;
229 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
232 if (ac3_sync_frame (&p_ac3dec->ac3_decoder, &sync_info))
238 p_ac3dec->p_aout_fifo->l_rate = sync_info.sample_rate;
240 buffer = ((s16 *)p_ac3dec->p_aout_fifo->buffer) + (p_ac3dec->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
242 if (ac3_decode_frame (&p_ac3dec->ac3_decoder, buffer))
248 vlc_mutex_lock (&p_ac3dec->p_aout_fifo->data_lock);
249 p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
250 vlc_cond_signal (&p_ac3dec->p_aout_fifo->data_wait);
251 vlc_mutex_unlock (&p_ac3dec->p_aout_fifo->data_lock);
256 /* If b_error is set, the ac3 decoder thread enters the error loop */
257 if (p_ac3dec->b_error)
259 ErrorThread (p_ac3dec);
262 /* End of the ac3 decoder thread */
263 EndThread (p_ac3dec);
266 /*****************************************************************************
267 * ErrorThread : ac3 decoder's RunThread() error loop
268 *****************************************************************************/
269 static void ErrorThread (ac3dec_thread_t * p_ac3dec)
271 /* We take the lock, because we are going to read/write the start/end
272 * indexes of the decoder fifo */
273 vlc_mutex_lock (&p_ac3dec->p_fifo->data_lock);
275 /* Wait until a `die' order is sent */
276 while (!p_ac3dec->b_die)
278 /* Trash all received PES packets */
279 while (!DECODER_FIFO_ISEMPTY(*p_ac3dec->p_fifo))
281 p_ac3dec->p_fifo->pf_delete_pes(p_ac3dec->p_fifo->p_packets_mgt,
282 DECODER_FIFO_START(*p_ac3dec->p_fifo));
283 DECODER_FIFO_INCSTART (*p_ac3dec->p_fifo);
286 /* Waiting for the input thread to put new PES packets in the fifo */
287 vlc_cond_wait (&p_ac3dec->p_fifo->data_wait,
288 &p_ac3dec->p_fifo->data_lock);
291 /* We can release the lock before leaving */
292 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
295 /*****************************************************************************
296 * EndThread : ac3 decoder thread destruction
297 *****************************************************************************/
298 static void EndThread (ac3dec_thread_t * p_ac3dec)
300 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec);
302 /* If the audio output fifo was created, we destroy it */
303 if (p_ac3dec->p_aout_fifo != NULL)
305 aout_DestroyFifo (p_ac3dec->p_aout_fifo);
307 /* Make sure the output thread leaves the NextFrame() function */
308 vlc_mutex_lock (&(p_ac3dec->p_aout_fifo->data_lock));
309 vlc_cond_signal (&(p_ac3dec->p_aout_fifo->data_wait));
310 vlc_mutex_unlock (&(p_ac3dec->p_aout_fifo->data_lock));
313 /* Destroy descriptor */
316 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec);
319 void ac3_byte_stream_next (ac3_byte_stream_t * p_byte_stream)
321 ac3dec_thread_t * p_ac3dec = p_byte_stream->info;
323 /* We are looking for the next TS packet that contains real data,
324 * and not just a PES header */
327 /* We were reading the last TS packet of this PES packet... It's
328 * time to jump to the next PES packet */
329 if (p_ac3dec->p_data->p_next == NULL)
333 /* We are going to read/write the start and end indexes of the
334 * decoder fifo and to use the fifo's conditional variable,
335 * that's why we need to take the lock before */
336 vlc_mutex_lock (&p_ac3dec->p_fifo->data_lock);
338 /* Is the input thread dying ? */
339 if (p_ac3dec->p_fifo->b_die)
341 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
345 /* We should increase the start index of the decoder fifo, but
346 * if we do this now, the input thread could overwrite the
347 * pointer to the current PES packet, and we weren't able to
348 * give it back to the netlist. That's why we free the PES
350 p_ac3dec->p_fifo->pf_delete_pes(p_ac3dec->p_fifo->p_packets_mgt,
351 DECODER_FIFO_START(*p_ac3dec->p_fifo));
353 DECODER_FIFO_INCSTART (*p_ac3dec->p_fifo);
355 while (DECODER_FIFO_ISEMPTY(*p_ac3dec->p_fifo))
357 vlc_cond_wait(&p_ac3dec->p_fifo->data_wait, &p_ac3dec->p_fifo->data_lock);
359 if (p_ac3dec->p_fifo->b_die)
361 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
366 /* The next byte could be found in the next PES packet */
367 p_ac3dec->p_data = DECODER_FIFO_START(*p_ac3dec->p_fifo)->p_first;
369 /* parse ac3 magic header */
370 ptr = *(p_ac3dec->p_data->p_payload_start + 2);
372 ptr |= *(p_ac3dec->p_data->p_payload_start + 3);
373 p_ac3dec->sync_ptr = ptr;
374 p_ac3dec->p_data->p_payload_start += 4;
376 /* We can release the fifo's data lock */
377 vlc_mutex_unlock (&p_ac3dec->p_fifo->data_lock);
379 /* Perhaps the next TS packet of the current PES packet contains
380 * real data (ie its payload's size is greater than 0) */
383 p_ac3dec->p_data = p_ac3dec->p_data->p_next;
385 } while (p_ac3dec->p_data->p_payload_start == p_ac3dec->p_data->p_payload_end);
386 p_byte_stream->p_byte = p_ac3dec->p_data->p_payload_start;
387 p_byte_stream->p_end = p_ac3dec->p_data->p_payload_end;