]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
Fin du remplacement des pthread + ajout du frame rate dans display.c.
[vlc] / src / video_decoder / video_decoder.c
1 /*******************************************************************************
2  * video_decoder.c : video decoder thread
3  * (c)1999 VideoLAN
4  *******************************************************************************/
5
6 /* ?? passer en terminate/destroy avec les signaux supplĂ©mentaires */
7
8 /*******************************************************************************
9  * Preamble
10  *******************************************************************************/
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <sys/uio.h>
17 #include <X11/Xlib.h>
18 #include <X11/extensions/XShm.h>
19
20 #include "config.h"
21 #include "common.h"
22 #include "mtime.h"
23 #include "vlc_thread.h"
24
25 #include "intf_msg.h"
26 #include "debug.h"                      /* ?? temporaire, requis par netlist.h */
27
28 #include "input.h"
29 #include "input_netlist.h"
30 #include "decoder_fifo.h"
31 #include "video.h"
32 #include "video_output.h"
33 #include "video_decoder.h"
34
35 /*
36  * Local prototypes
37  */
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 );
43
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:
50  * ??
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 */ )
54 {
55     vdec_thread_t *     p_vdec;
56
57     intf_DbgMsg("vdec debug: creating video decoder thread\n");
58
59     /* Allocate the memory needed to store the thread's structure */
60     if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
61     {
62         intf_ErrMsg("adec error: not enough memory for vdec_CreateThread() to create the new thread\n");
63         return( NULL );
64     }
65
66     /*
67      * Initialize the thread properties
68      */
69     p_vdec->b_die = 0;
70     p_vdec->b_error = 0;
71
72     /*
73      * Initialize the input properties
74      */
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;
85
86     /* Spawn the video decoder thread */
87     if ( vlc_thread_create(&p_vdec->thread_id, "video decoder", (vlc_thread_func)RunThread, (void *)p_vdec) )
88     {
89         intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
90         free( p_vdec );
91         return( NULL );
92     }
93
94     intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
95     return( p_vdec );
96 }
97
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 */ )
106 {
107     intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
108
109     /* Ask thread to kill itself */
110     p_vdec->b_die = 1;
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) );
115
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 );
119 }
120
121 /* following functions are local */
122
123 /*******************************************************************************
124  * CheckConfiguration: check vdec_CreateThread() configuration
125  *******************************************************************************
126  * Set default parameters where required. In DEBUG mode, check if configuration
127  * is valid.
128  *******************************************************************************/
129 #if 0
130 static int CheckConfiguration( video_cfg_t *p_cfg )
131 {
132     /* ?? */
133
134     return( 0 );
135 }
136 #endif
137
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 )
146 {
147
148     intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
149
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) )
154     {
155         vlc_cond_wait( &p_vdec->fifo.data_wait, &p_vdec->fifo.data_lock );
156     }
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 );
160
161 #if 0
162     /* ?? */
163     /* Create video stream */
164     p_vdec->i_stream =  vout_CreateStream( p_vdec->p_vout );
165     if( p_vdec->i_stream < 0 )                                        /* error */
166     {
167         return( 1 );        
168     }
169     
170     /* Initialize decoding data */    
171     /* ?? */
172 #endif
173
174     /* Initialize other properties */
175 #ifdef STATS
176     p_vdec->c_loops = 0;    
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;
186 #endif
187
188     /* Mark thread as running and return */
189     intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);    
190     return( 0 );    
191 }
192
193 /*******************************************************************************
194  * RunThread: generic decoder thread
195  *******************************************************************************
196  * Generic decoder thread. This function does only returns when the thread is
197  * terminated. 
198  *******************************************************************************/
199 static void RunThread( vdec_thread_t *p_vdec )
200 {
201
202     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n", p_vdec, getpid());
203
204     /* 
205      * Initialize thread and free configuration 
206      */
207     p_vdec->b_error = InitThread( p_vdec );
208     if( p_vdec->b_error )
209     {
210         return;
211     }
212     p_vdec->b_run = 1;
213
214 /* REMOVE ME !!!!! */
215 p_vdec->b_error = 1;
216
217     /*
218      * Main loop - it is not executed if an error occured during
219      * initialization
220      */
221     while( (!p_vdec->b_die) && (!p_vdec->b_error) )
222     {
223         /* ?? */
224     } 
225
226     /*
227      * Error loop
228      */
229     if( p_vdec->b_error )
230     {
231         ErrorThread( p_vdec );        
232     }
233
234     /* End of thread */
235     EndThread( p_vdec );
236     p_vdec->b_run = 0;
237 }
238
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
244  * possible.
245  *******************************************************************************/
246 static void ErrorThread( vdec_thread_t *p_vdec )
247 {
248     /* Wait until a `die' order */
249     while( !p_vdec->b_die )
250     {
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 );
254
255         /* ?? trash all trashable PES packets */
256         while( !DECODER_FIFO_ISEMPTY(p_vdec->fifo) )
257         {
258             input_NetlistFreePES( p_vdec->bit_stream.p_input, DECODER_FIFO_START(p_vdec->fifo) );
259             DECODER_FIFO_INCSTART( p_vdec->fifo );
260         }
261
262         vlc_mutex_unlock( &p_vdec->fifo.data_lock );
263         /* Sleep a while */
264         msleep( VDEC_IDLE_SLEEP );                
265     }
266 }
267
268 /*******************************************************************************
269  * EndThread: thread destruction
270  *******************************************************************************
271  * This function is called when the thread ends after a sucessfull 
272  * initialization.
273  *******************************************************************************/
274 static void EndThread( vdec_thread_t *p_vdec )
275 {
276     intf_DbgMsg("vdec debug: destroying video decoder thread %p\n", p_vdec);
277
278 #ifdef DEBUG
279     /* Check for remaining PES packets */
280     /* ?? */
281 #endif
282
283     /* Destroy thread structures allocated by InitThread */
284 //    vout_DestroyStream( p_vdec->p_vout, p_vdec->i_stream );
285     /* ?? */
286
287     intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
288 }