1 /*******************************************************************************
2 * video_decoder.c : video decoder thread
4 *******************************************************************************/
6 /* ?? passer en terminate/destroy avec les signaux supplémentaires */
8 /*******************************************************************************
10 *******************************************************************************/
19 #include <X11/extensions/XShm.h>
26 #include "debug.h" /* ?? temporaire, requis par netlist.h */
29 #include "input_netlist.h"
30 #include "decoder_fifo.h"
32 #include "video_output.h"
33 #include "video_decoder.h"
38 //static int CheckConfiguration ( video_cfg_t *p_cfg );
39 static int InitThread ( vdec_thread_t *p_vdec );
40 static void RunThread ( vdec_thread_t *p_vdec );
41 static void ErrorThread ( vdec_thread_t *p_vdec );
42 static void EndThread ( vdec_thread_t *p_vdec );
44 /*******************************************************************************
45 * vdec_CreateThread: create a generic decoder thread
46 *******************************************************************************
47 * This function creates a new video decoder thread, and returns a pointer
48 * to its description. On error, it returns NULL.
49 * Following configuration properties are used:
51 *******************************************************************************/
52 vdec_thread_t * vdec_CreateThread( /* video_cfg_t *p_cfg, */ input_thread_t *p_input /*,
53 vout_thread_t *p_vout, int *pi_status */ )
55 vdec_thread_t * p_vdec;
57 intf_DbgMsg("vdec debug: creating video decoder thread\n");
59 /* Allocate the memory needed to store the thread's structure */
60 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
62 intf_ErrMsg("adec error: not enough memory for vdec_CreateThread() to create the new thread\n");
67 * Initialize the thread properties
73 * Initialize the input properties
75 /* Initialize the decoder fifo's data lock and conditional variable and set * its buffer as empty */
76 pthread_mutex_init( &p_vdec->fifo.data_lock, NULL );
77 pthread_cond_init( &p_vdec->fifo.data_wait, NULL );
78 p_vdec->fifo.i_start = 0;
79 p_vdec->fifo.i_end = 0;
80 /* Initialize the bit stream structure */
81 p_vdec->bit_stream.p_input = p_input;
82 p_vdec->bit_stream.p_decoder_fifo = &p_vdec->fifo;
83 p_vdec->bit_stream.fifo.buffer = 0;
84 p_vdec->bit_stream.fifo.i_available = 0;
86 /* Spawn the video decoder thread */
87 if ( pthread_create(&p_vdec->thread_id, NULL, (void *)RunThread, (void *)p_vdec) )
89 intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
94 intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
98 /*******************************************************************************
99 * vdec_DestroyThread: destroy a generic decoder thread
100 *******************************************************************************
101 * Destroy a terminated thread. This function will return 0 if the thread could
102 * be destroyed, and non 0 else. The last case probably means that the thread
103 * was still active, and another try may succeed.
104 *******************************************************************************/
105 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
107 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
109 /* Ask thread to kill itself */
112 /* Remove this as soon as the "status" flag is implemented */
113 pthread_join( p_vdec->thread_id, NULL ); /* wait until it's done */
116 /* following functions are local */
118 /*******************************************************************************
119 * CheckConfiguration: check vdec_CreateThread() configuration
120 *******************************************************************************
121 * Set default parameters where required. In DEBUG mode, check if configuration
123 *******************************************************************************/
125 static int CheckConfiguration( video_cfg_t *p_cfg )
133 /*******************************************************************************
134 * InitThread: initialize vdec output thread
135 *******************************************************************************
136 * This function is called from RunThread and performs the second step of the
137 * initialization. It returns 0 on success. Note that the thread's flag are not
138 * modified inside this function.
139 *******************************************************************************/
140 static int InitThread( vdec_thread_t *p_vdec )
143 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
145 /* Our first job is to initialize the bit stream structure with the
146 * beginning of the input stream */
147 pthread_mutex_lock( &p_vdec->fifo.data_lock );
148 while ( DECODER_FIFO_ISEMPTY(p_vdec->fifo) )
150 pthread_cond_wait( &p_vdec->fifo.data_wait, &p_vdec->fifo.data_lock );
152 p_vdec->bit_stream.p_ts = DECODER_FIFO_START( p_vdec->fifo )->p_first_ts;
153 p_vdec->bit_stream.i_byte = p_vdec->bit_stream.p_ts->i_payload_start;
154 pthread_mutex_unlock( &p_vdec->fifo.data_lock );
158 /* Create video stream */
159 p_vdec->i_stream = vout_CreateStream( p_vdec->p_vout );
160 if( p_vdec->i_stream < 0 ) /* error */
165 /* Initialize decoding data */
169 /* Initialize other properties */
172 p_vdec->c_idle_loops = 0;
173 p_vdec->c_pictures = 0;
174 p_vdec->c_i_pictures = 0;
175 p_vdec->c_p_pictures = 0;
176 p_vdec->c_b_pictures = 0;
177 p_vdec->c_decoded_pictures = 0;
178 p_vdec->c_decoded_i_pictures = 0;
179 p_vdec->c_decoded_p_pictures = 0;
180 p_vdec->c_decoded_b_pictures = 0;
183 /* Mark thread as running and return */
184 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
188 /*******************************************************************************
189 * RunThread: generic decoder thread
190 *******************************************************************************
191 * Generic decoder thread. This function does only returns when the thread is
193 *******************************************************************************/
194 static void RunThread( vdec_thread_t *p_vdec )
197 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n", p_vdec, getpid());
200 * Initialize thread and free configuration
202 p_vdec->b_error = InitThread( p_vdec );
203 if( p_vdec->b_error )
209 /* REMOVE ME !!!!! */
213 * Main loop - it is not executed if an error occured during
216 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
224 if( p_vdec->b_error )
226 ErrorThread( p_vdec );
234 /*******************************************************************************
235 * ErrorThread: RunThread() error loop
236 *******************************************************************************
237 * This function is called when an error occured during thread main's loop. The
238 * thread can still receive feed, but must be ready to terminate as soon as
240 *******************************************************************************/
241 static void ErrorThread( vdec_thread_t *p_vdec )
243 /* Wait until a `die' order */
244 while( !p_vdec->b_die )
246 /* We take the lock, because we are going to read/write the start/end
247 * indexes of the decoder fifo */
248 pthread_mutex_lock( &p_vdec->fifo.data_lock );
250 /* ?? trash all trashable PES packets */
251 while( !DECODER_FIFO_ISEMPTY(p_vdec->fifo) )
253 input_NetlistFreePES( p_vdec->bit_stream.p_input, DECODER_FIFO_START(p_vdec->fifo) );
254 DECODER_FIFO_INCSTART( p_vdec->fifo );
257 pthread_mutex_unlock( &p_vdec->fifo.data_lock );
259 msleep( VDEC_IDLE_SLEEP );
263 /*******************************************************************************
264 * EndThread: thread destruction
265 *******************************************************************************
266 * This function is called when the thread ends after a sucessfull
268 *******************************************************************************/
269 static void EndThread( vdec_thread_t *p_vdec )
271 intf_DbgMsg("vdec debug: destroying video decoder thread %p\n", p_vdec);
274 /* Check for remaining PES packets */
278 /* Destroy thread structures allocated by InitThread */
279 // vout_DestroyStream( p_vdec->p_vout, p_vdec->i_stream );
282 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);