1 /*******************************************************************************
2 * video_decoder.c : video decoder thread
4 *******************************************************************************/
6 /* ?? passer en terminate/destroy avec les signaux supplémentaires */
8 /*******************************************************************************
10 *******************************************************************************/
18 #include <X11/extensions/XShm.h>
23 #include "vlc_thread.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 vlc_mutex_init( &p_vdec->fifo.data_lock );
77 vlc_cond_init( &p_vdec->fifo.data_wait );
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 ( vlc_thread_create(&p_vdec->thread_id, "video decoder", (vlc_thread_func)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 */
111 /* Make sure the decoder thread leaves the GetByte() function */
112 vlc_mutex_lock( &(p_vdec->fifo.data_lock) );
113 vlc_cond_signal( &(p_vdec->fifo.data_wait) );
114 vlc_mutex_unlock( &(p_vdec->fifo.data_lock) );
116 /* Waiting for the decoder thread to exit */
117 /* Remove this as soon as the "status" flag is implemented */
118 vlc_thread_join( p_vdec->thread_id );
121 /* following functions are local */
123 /*******************************************************************************
124 * CheckConfiguration: check vdec_CreateThread() configuration
125 *******************************************************************************
126 * Set default parameters where required. In DEBUG mode, check if configuration
128 *******************************************************************************/
130 static int CheckConfiguration( video_cfg_t *p_cfg )
138 /*******************************************************************************
139 * InitThread: initialize vdec output thread
140 *******************************************************************************
141 * This function is called from RunThread and performs the second step of the
142 * initialization. It returns 0 on success. Note that the thread's flag are not
143 * modified inside this function.
144 *******************************************************************************/
145 static int InitThread( vdec_thread_t *p_vdec )
148 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
150 /* Our first job is to initialize the bit stream structure with the
151 * beginning of the input stream */
152 vlc_mutex_lock( &p_vdec->fifo.data_lock );
153 while ( DECODER_FIFO_ISEMPTY(p_vdec->fifo) )
155 vlc_cond_wait( &p_vdec->fifo.data_wait, &p_vdec->fifo.data_lock );
157 p_vdec->bit_stream.p_ts = DECODER_FIFO_START( p_vdec->fifo )->p_first_ts;
158 p_vdec->bit_stream.i_byte = p_vdec->bit_stream.p_ts->i_payload_start;
159 vlc_mutex_unlock( &p_vdec->fifo.data_lock );
163 /* Create video stream */
164 p_vdec->i_stream = vout_CreateStream( p_vdec->p_vout );
165 if( p_vdec->i_stream < 0 ) /* error */
170 /* Initialize decoding data */
174 /* Initialize other properties */
177 p_vdec->c_idle_loops = 0;
178 p_vdec->c_pictures = 0;
179 p_vdec->c_i_pictures = 0;
180 p_vdec->c_p_pictures = 0;
181 p_vdec->c_b_pictures = 0;
182 p_vdec->c_decoded_pictures = 0;
183 p_vdec->c_decoded_i_pictures = 0;
184 p_vdec->c_decoded_p_pictures = 0;
185 p_vdec->c_decoded_b_pictures = 0;
188 /* Mark thread as running and return */
189 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
193 /*******************************************************************************
194 * RunThread: generic decoder thread
195 *******************************************************************************
196 * Generic decoder thread. This function does only returns when the thread is
198 *******************************************************************************/
199 static void RunThread( vdec_thread_t *p_vdec )
202 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n", p_vdec, getpid());
205 * Initialize thread and free configuration
207 p_vdec->b_error = InitThread( p_vdec );
208 if( p_vdec->b_error )
214 /* REMOVE ME !!!!! */
218 * Main loop - it is not executed if an error occured during
221 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
229 if( p_vdec->b_error )
231 ErrorThread( p_vdec );
239 /*******************************************************************************
240 * ErrorThread: RunThread() error loop
241 *******************************************************************************
242 * This function is called when an error occured during thread main's loop. The
243 * thread can still receive feed, but must be ready to terminate as soon as
245 *******************************************************************************/
246 static void ErrorThread( vdec_thread_t *p_vdec )
248 /* Wait until a `die' order */
249 while( !p_vdec->b_die )
251 /* We take the lock, because we are going to read/write the start/end
252 * indexes of the decoder fifo */
253 vlc_mutex_lock( &p_vdec->fifo.data_lock );
255 /* ?? trash all trashable PES packets */
256 while( !DECODER_FIFO_ISEMPTY(p_vdec->fifo) )
258 input_NetlistFreePES( p_vdec->bit_stream.p_input, DECODER_FIFO_START(p_vdec->fifo) );
259 DECODER_FIFO_INCSTART( p_vdec->fifo );
262 vlc_mutex_unlock( &p_vdec->fifo.data_lock );
264 msleep( VDEC_IDLE_SLEEP );
268 /*******************************************************************************
269 * EndThread: thread destruction
270 *******************************************************************************
271 * This function is called when the thread ends after a sucessfull
273 *******************************************************************************/
274 static void EndThread( vdec_thread_t *p_vdec )
276 intf_DbgMsg("vdec debug: destroying video decoder thread %p\n", p_vdec);
279 /* Check for remaining PES packets */
283 /* Destroy thread structures allocated by InitThread */
284 // vout_DestroyStream( p_vdec->p_vout, p_vdec->i_stream );
287 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);