]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
* Le video_decoder a desormais une structure qui tourne (copier-coller de
[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 <pthread.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <sys/uio.h>
18 #include <X11/Xlib.h>
19 #include <X11/extensions/XShm.h>
20
21 #include "config.h"
22 #include "common.h"
23 #include "mtime.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     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;
85
86     /* Spawn the video decoder thread */
87     if ( pthread_create(&p_vdec->thread_id, NULL, (void *)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
112     /* Remove this as soon as the "status" flag is implemented */
113     pthread_join( p_vdec->thread_id, NULL );         /* wait until it's done */
114 }
115
116 /* following functions are local */
117
118 /*******************************************************************************
119  * CheckConfiguration: check vdec_CreateThread() configuration
120  *******************************************************************************
121  * Set default parameters where required. In DEBUG mode, check if configuration
122  * is valid.
123  *******************************************************************************/
124 #if 0
125 static int CheckConfiguration( video_cfg_t *p_cfg )
126 {
127     /* ?? */
128
129     return( 0 );
130 }
131 #endif
132
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 )
141 {
142
143     intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
144
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) )
149     {
150         pthread_cond_wait( &p_vdec->fifo.data_wait, &p_vdec->fifo.data_lock );
151     }
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 );
155
156 #if 0
157     /* ?? */
158     /* Create video stream */
159     p_vdec->i_stream =  vout_CreateStream( p_vdec->p_vout );
160     if( p_vdec->i_stream < 0 )                                        /* error */
161     {
162         return( 1 );        
163     }
164     
165     /* Initialize decoding data */    
166     /* ?? */
167 #endif
168
169     /* Initialize other properties */
170 #ifdef STATS
171     p_vdec->c_loops = 0;    
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;
181 #endif
182
183     /* Mark thread as running and return */
184     intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);    
185     return( 0 );    
186 }
187
188 /*******************************************************************************
189  * RunThread: generic decoder thread
190  *******************************************************************************
191  * Generic decoder thread. This function does only returns when the thread is
192  * terminated. 
193  *******************************************************************************/
194 static void RunThread( vdec_thread_t *p_vdec )
195 {
196
197     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n", p_vdec, getpid());
198
199     /* 
200      * Initialize thread and free configuration 
201      */
202     p_vdec->b_error = InitThread( p_vdec );
203     if( p_vdec->b_error )
204     {
205         return;
206     }
207     p_vdec->b_run = 1;
208
209 /* REMOVE ME !!!!! */
210 p_vdec->b_error = 1;
211
212     /*
213      * Main loop - it is not executed if an error occured during
214      * initialization
215      */
216     while( (!p_vdec->b_die) && (!p_vdec->b_error) )
217     {
218         /* ?? */
219     } 
220
221     /*
222      * Error loop
223      */
224     if( p_vdec->b_error )
225     {
226         ErrorThread( p_vdec );        
227     }
228
229     /* End of thread */
230     EndThread( p_vdec );
231     p_vdec->b_run = 0;
232 }
233
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
239  * possible.
240  *******************************************************************************/
241 static void ErrorThread( vdec_thread_t *p_vdec )
242 {
243     /* Wait until a `die' order */
244     while( !p_vdec->b_die )
245     {
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 );
249
250         /* ?? trash all trashable PES packets */
251         while( !DECODER_FIFO_ISEMPTY(p_vdec->fifo) )
252         {
253             input_NetlistFreePES( p_vdec->bit_stream.p_input, DECODER_FIFO_START(p_vdec->fifo) );
254             DECODER_FIFO_INCSTART( p_vdec->fifo );
255         }
256
257         pthread_mutex_unlock( &p_vdec->fifo.data_lock );
258         /* Sleep a while */
259         msleep( VDEC_IDLE_SLEEP );                
260     }
261 }
262
263 /*******************************************************************************
264  * EndThread: thread destruction
265  *******************************************************************************
266  * This function is called when the thread ends after a sucessfull 
267  * initialization.
268  *******************************************************************************/
269 static void EndThread( vdec_thread_t *p_vdec )
270 {
271     intf_DbgMsg("vdec debug: destroying video decoder thread %p\n", p_vdec);
272
273 #ifdef DEBUG
274     /* Check for remaining PES packets */
275     /* ?? */
276 #endif
277
278     /* Destroy thread structures allocated by InitThread */
279 //    vout_DestroyStream( p_vdec->p_vout, p_vdec->i_stream );
280     /* ?? */
281
282     intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
283 }
284
285