]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
Ajout des fichiers de la synchro un peu partout.
[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
34 #include "vdec_idct.h"
35 #include "video_decoder.h"
36 #include "vdec_motion.h"
37
38 #include "vpar_blocks.h"
39 #include "vpar_headers.h"
40 #include "video_fifo.h"
41 #include "vpar_synchro.h"
42 #include "video_parser.h"
43
44 /*
45  * Local prototypes
46  */
47 static int      InitThread          ( vdec_thread_t *p_vdec );
48 static void     RunThread           ( vdec_thread_t *p_vdec );
49 static void     ErrorThread         ( vdec_thread_t *p_vdec );
50 static void     EndThread           ( vdec_thread_t *p_vdec );
51 static void     DecodeMacroblock    ( vdec_thread_t *p_vdec,
52                                       macroblock_t * p_mb );
53
54 /*******************************************************************************
55  * vdec_CreateThread: create a video decoder thread
56  *******************************************************************************
57  * This function creates a new video decoder thread, and returns a pointer
58  * to its description. On error, it returns NULL.
59  * Following configuration properties are used:
60  * ??
61  *******************************************************************************/
62 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
63 {
64     vdec_thread_t *     p_vdec;
65
66     intf_DbgMsg("vdec debug: creating video decoder thread\n");
67
68     /* Allocate the memory needed to store the thread's structure */
69     if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
70     {
71         intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
72         return( NULL );
73     }
74
75     /*
76      * Initialize the thread properties
77      */
78     p_vdec->b_die = 0;
79     p_vdec->b_error = 0;
80
81     /*
82      * Initialize the parser properties
83      */
84     p_vdec->p_vpar = p_vpar;
85
86     /* Spawn the video decoder thread */
87     if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
88          (vlc_thread_func)RunThread, (void *)p_vdec) )
89     {
90         intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
91         free( p_vdec );
92         return( NULL );
93     }
94
95     intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
96     return( p_vdec );
97 }
98
99 /*******************************************************************************
100  * vdec_DestroyThread: destroy a video decoder thread
101  *******************************************************************************
102  * Destroy and terminate thread. This function will return 0 if the thread could
103  * be destroyed, and non 0 else. The last case probably means that the thread
104  * was still active, and another try may succeed.
105  *******************************************************************************/
106 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
107 {
108     intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
109
110     /* Ask thread to kill itself */
111     p_vdec->b_die = 1;
112
113     /* Waiting for the decoder thread to exit */
114     /* Remove this as soon as the "status" flag is implemented */
115     vlc_thread_join( p_vdec->thread_id );
116 }
117
118 /* following functions are local */
119
120 /*******************************************************************************
121  * InitThread: initialize video decoder thread
122  *******************************************************************************
123  * This function is called from RunThread and performs the second step of the
124  * initialization. It returns 0 on success. Note that the thread's flag are not
125  * modified inside this function.
126  *******************************************************************************/
127 static int InitThread( vdec_thread_t *p_vdec )
128 {
129     intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
130
131     /* Initialize other properties */
132 #ifdef STATS
133     p_vdec->c_loops = 0;    
134     p_vdec->c_idle_loops = 0;
135     p_vdec->c_decoded_pictures = 0;
136     p_vdec->c_decoded_i_pictures = 0;
137     p_vdec->c_decoded_p_pictures = 0;
138     p_vdec->c_decoded_b_pictures = 0;
139 #endif
140
141     /* Mark thread as running and return */
142     intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);    
143     return( 0 );    
144 }
145
146 /*******************************************************************************
147  * RunThread: video decoder thread
148  *******************************************************************************
149  * Video decoder thread. This function does only return when the thread is
150  * terminated. 
151  *******************************************************************************/
152 static void RunThread( vdec_thread_t *p_vdec )
153 {
154     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
155                 p_vdec, getpid());
156
157     /* 
158      * Initialize thread and free configuration 
159      */
160     p_vdec->b_error = InitThread( p_vdec );
161     if( p_vdec->b_error )
162     {
163         return;
164     }
165     p_vdec->b_run = 1;
166
167     /*
168      * Main loop - it is not executed if an error occured during
169      * initialization
170      */
171     while( (!p_vdec->b_die) && (!p_vdec->b_error) )
172     {
173         macroblock_t *          p_mb;
174         
175         if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
176         {
177             DecodeMacroblock( p_vdec, p_mb );
178         }
179     } 
180
181     /*
182      * Error loop
183      */
184     if( p_vdec->b_error )
185     {
186         ErrorThread( p_vdec );        
187     }
188
189     /* End of thread */
190     EndThread( p_vdec );
191     p_vdec->b_run = 0;
192 }
193
194 /*******************************************************************************
195  * ErrorThread: RunThread() error loop
196  *******************************************************************************
197  * This function is called when an error occured during thread main's loop. The
198  * thread can still receive feed, but must be ready to terminate as soon as
199  * possible.
200  *******************************************************************************/
201 static void ErrorThread( vdec_thread_t *p_vdec )
202 {
203     macroblock_t *       p_mb;
204
205     /* Wait until a `die' order */
206     while( !p_vdec->b_die )
207     {
208         p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
209         vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
210
211         /* Sleep a while */
212         msleep( VDEC_IDLE_SLEEP );                
213     }
214 }
215
216 /*******************************************************************************
217  * EndThread: thread destruction
218  *******************************************************************************
219  * This function is called when the thread ends after a sucessfull 
220  * initialization.
221  *******************************************************************************/
222 static void EndThread( vdec_thread_t *p_vdec )
223 {
224     intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
225 }
226
227 /*******************************************************************************
228  * DecodeMacroblock : decode a macroblock of a picture
229  *******************************************************************************/
230 static void DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
231 {
232     int             i_b;
233
234     /*
235      * Motion Compensation (ISO/IEC 13818-2 section 7.6)
236      */
237     (*p_mb->pf_motion)( p_mb );
238
239     /* luminance */
240     for( i_b = 0; i_b < 4; i_b++ )
241     {
242         /*
243          * Inverse DCT (ISO/IEC 13818-2 section Annex A)
244          */
245         (p_mb->pf_idct[i_b])( p_mb->ppi_blocks[i_b], p_mb->pi_sparse_pos[i_b] );
246
247         /*
248          * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
249          */
250         (p_mb->pf_addb[i_b])( p_mb->ppi_blocks[i_b],
251                                p_mb->p_data[i_b], p_mb->i_lum_incr );
252     }
253
254     /* chrominance */
255     for( i_b = 4; i_b < 4 + 2*p_mb->i_chroma_nb_blocks; i_b++ )
256     {
257         /*
258          * Inverse DCT (ISO/IEC 13818-2 section Annex A)
259          */
260         (p_mb->pf_idct[i_b])( p_mb->ppi_blocks[i_b], p_mb->pi_sparse_pos[i_b] );
261
262         /*
263          * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
264          */
265         (p_mb->pf_addb[i_b])( p_mb->ppi_blocks[i_b],
266                                p_mb->p_data[i_b], p_mb->i_chroma_incr );
267     }
268
269     /*
270      * Decoding is finished, release the macroblock and free
271      * unneeded memory.
272      */
273     vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
274 }
275
276 /*******************************************************************************
277  * vdec_AddBlock : add a block
278  *******************************************************************************/
279 void vdec_AddBlock( elem_t * p_block, data_t * p_data, int i_incr )
280 {
281     int i_x, i_y;
282     
283     for( i_y = 0; i_y < 8; i_y++ )
284     {
285         for( i_x = 0; i_x < 8; i_x++ )
286         {
287             /* ??? Need clip to be MPEG-2 compliant */
288             *p_data++ += *p_block++;
289         }
290         p_data += i_incr;
291     }
292 }
293
294 /*******************************************************************************
295  * vdec_CopyBlock : copy a block
296  *******************************************************************************/
297 void vdec_CopyBlock( elem_t * p_block, data_t * p_data, int i_incr )
298 {
299     int i_x, i_y;
300     
301     for( i_y = 0; i_y < 8; i_y++ )
302     {
303 #ifndef VDEC_DFT
304         /* elem_t and data_t are the same */
305         memcopy( p_data, p_block, 8*sizeof(data_t) );
306         p_data += i_incr+8;
307         p_block += 8;
308 #else
309         for( i_x = 0; i_x < 8; i_x++ )
310         {
311             /* ??? Need clip to be MPEG-2 compliant */
312             /* ??? Why does the reference decoder add 128 ??? */
313             *p_data++ = *p_block++;
314         }
315         p_data += i_incr;
316 #endif
317     }
318 }
319
320 /*******************************************************************************
321  * vdec_DummyBlock : dummy function that does nothing
322  *******************************************************************************/
323 void vdec_DummyBlock( elem_t * p_block, data_t * p_data, int i_incr )
324 {
325 }