]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
Les I marchent !!!!!!!!!!!!!!!!!!!!!!!!
[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 "vlc.h"
12
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <sys/uio.h>
19 #include <X11/Xlib.h>
20 #include <X11/extensions/XShm.h>
21
22 #include "config.h"
23 #include "common.h"
24 #include "mtime.h"
25 #include "vlc_thread.h"
26
27 #include "intf_msg.h"
28 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
29
30 #include "input.h"
31 #include "input_netlist.h"
32 #include "decoder_fifo.h"
33 #include "video.h"
34 #include "video_output.h"
35
36 #include "vdec_idct.h"
37 #include "video_decoder.h"
38 #include "vdec_motion.h"
39
40 #include "vpar_blocks.h"
41 #include "vpar_headers.h"
42 #include "video_fifo.h"
43 #include "vpar_synchro.h"
44 #include "video_parser.h"
45
46 /*
47  * Local prototypes
48  */
49 static int      InitThread          ( vdec_thread_t *p_vdec );
50 static void     RunThread           ( vdec_thread_t *p_vdec );
51 static void     ErrorThread         ( vdec_thread_t *p_vdec );
52 static void     EndThread           ( vdec_thread_t *p_vdec );
53 static void     DecodeMacroblock    ( vdec_thread_t *p_vdec,
54                                       macroblock_t * p_mb );
55
56 /*******************************************************************************
57  * vdec_CreateThread: create a video decoder thread
58  *******************************************************************************
59  * This function creates a new video decoder thread, and returns a pointer
60  * to its description. On error, it returns NULL.
61  * Following configuration properties are used:
62  * ??
63  *******************************************************************************/
64 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
65 {
66     vdec_thread_t *     p_vdec;
67
68     intf_DbgMsg("vdec debug: creating video decoder thread\n");
69
70     /* Allocate the memory needed to store the thread's structure */
71     if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
72     {
73         intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
74         return( NULL );
75     }
76
77     /*
78      * Initialize the thread properties
79      */
80     p_vdec->b_die = 0;
81     p_vdec->b_error = 0;
82
83     /*
84      * Initialize the parser properties
85      */
86     p_vdec->p_vpar = p_vpar;
87
88     /* Spawn the video decoder thread */
89     if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
90          (vlc_thread_func_t)RunThread, (void *)p_vdec) )
91     {
92         intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
93         free( p_vdec );
94         return( NULL );
95     }
96
97     intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
98     return( p_vdec );
99 }
100
101 /*******************************************************************************
102  * vdec_DestroyThread: destroy a video decoder thread
103  *******************************************************************************
104  * Destroy and terminate thread. This function will return 0 if the thread could
105  * be destroyed, and non 0 else. The last case probably means that the thread
106  * was still active, and another try may succeed.
107  *******************************************************************************/
108 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
109 {
110     intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
111
112     /* Ask thread to kill itself */
113     p_vdec->b_die = 1;
114
115     /* Waiting for the decoder thread to exit */
116     /* Remove this as soon as the "status" flag is implemented */
117     vlc_thread_join( p_vdec->thread_id );
118 }
119
120 /* following functions are local */
121
122 /*******************************************************************************
123  * InitThread: initialize video decoder thread
124  *******************************************************************************
125  * This function is called from RunThread and performs the second step of the
126  * initialization. It returns 0 on success. Note that the thread's flag are not
127  * modified inside this function.
128  *******************************************************************************/
129 static int InitThread( vdec_thread_t *p_vdec )
130 {
131 //#ifdef MPEG2_COMPLIANT
132     int i_dummy;
133 //#endif
134
135     intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
136
137     /* Initialize other properties */
138 #ifdef STATS
139     p_vdec->c_loops = 0;    
140     p_vdec->c_idle_loops = 0;
141     p_vdec->c_decoded_pictures = 0;
142     p_vdec->c_decoded_i_pictures = 0;
143     p_vdec->c_decoded_p_pictures = 0;
144     p_vdec->c_decoded_b_pictures = 0;
145 #endif
146
147 //#ifdef MPEG2_COMPLIANT
148     /* Init crop table */
149     p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
150     for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
151     {
152         p_vdec->pi_crop[i_dummy] = 0;
153     }
154     for( ; i_dummy < 255; i_dummy ++ )
155     {
156         p_vdec->pi_crop[i_dummy] = i_dummy;
157     }
158     for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
159     {
160         p_vdec->pi_crop[i_dummy] = 255;
161     }
162 //#endif
163
164     /* Mark thread as running and return */
165     intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);    
166     return( 0 );    
167 }
168
169 /*******************************************************************************
170  * RunThread: video decoder thread
171  *******************************************************************************
172  * Video decoder thread. This function does only return when the thread is
173  * terminated. 
174  *******************************************************************************/
175 static void RunThread( vdec_thread_t *p_vdec )
176 {
177     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
178                 p_vdec, getpid());
179
180     /* 
181      * Initialize thread and free configuration 
182      */
183     p_vdec->b_error = InitThread( p_vdec );
184     if( p_vdec->b_error )
185     {
186         return;
187     }
188     p_vdec->b_run = 1;
189
190     /*
191      * Main loop - it is not executed if an error occured during
192      * initialization
193      */
194     while( (!p_vdec->b_die) && (!p_vdec->b_error) )
195     {
196         macroblock_t *          p_mb;
197         
198         if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
199         {
200             DecodeMacroblock( p_vdec, p_mb );
201         }
202     } 
203
204     /*
205      * Error loop
206      */
207     if( p_vdec->b_error )
208     {
209         ErrorThread( p_vdec );        
210     }
211
212     /* End of thread */
213     EndThread( p_vdec );
214     p_vdec->b_run = 0;
215 }
216
217 /*******************************************************************************
218  * ErrorThread: RunThread() error loop
219  *******************************************************************************
220  * This function is called when an error occured during thread main's loop. The
221  * thread can still receive feed, but must be ready to terminate as soon as
222  * possible.
223  *******************************************************************************/
224 static void ErrorThread( vdec_thread_t *p_vdec )
225 {
226     macroblock_t *       p_mb;
227
228     /* Wait until a `die' order */
229     while( !p_vdec->b_die )
230     {
231         p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
232         vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
233     }
234 }
235
236 /*******************************************************************************
237  * EndThread: thread destruction
238  *******************************************************************************
239  * This function is called when the thread ends after a sucessfull 
240  * initialization.
241  *******************************************************************************/
242 static void EndThread( vdec_thread_t *p_vdec )
243 {
244     intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
245 }
246
247 /*******************************************************************************
248  * DecodeMacroblock : decode a macroblock of a picture
249  *******************************************************************************/
250 static void DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
251 {
252     int             i_b;
253
254     /*
255      * Motion Compensation (ISO/IEC 13818-2 section 7.6)
256      */
257     (*p_mb->pf_motion)( p_mb );
258 //#if 0
259     /* luminance */
260     for( i_b = 0; i_b <4; i_b++ )
261     {
262         /*
263          * Inverse DCT (ISO/IEC 13818-2 section Annex A)
264          */
265         (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
266                               p_mb->pi_sparse_pos[i_b] );
267
268 //        if( (i_b & 2) )
269 /*        {
270 int i, j;
271 elem_t meuh[64];
272
273 for( i = 0; i < 8; i++ )
274 {
275     for( j = 0; j < 8; j++ )
276     {
277         meuh[8*j + (7-i)] = p_mb->ppi_blocks[i_b][j + 8*i];
278     }
279 }
280 memcpy(p_mb->ppi_blocks[i_b], meuh, 128);
281         }
282         */
283         /*
284          * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
285          */
286         (p_mb->pf_addb[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
287                               p_mb->p_data[i_b], p_mb->i_addb_l_stride );
288     }
289 //#endif
290 //#if 0
291     /* chrominance */
292     for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; i_b++ )
293     {
294         /*
295          * Inverse DCT (ISO/IEC 13818-2 section Annex A)
296          */
297         (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
298                               p_mb->pi_sparse_pos[i_b] );
299 /*
300         {
301 int i, j;
302 elem_t meuh[64];
303
304 for( i = 0; i < 8; i++ )
305 {
306     for( j = 0; j < 8; j++ )
307     {
308         meuh[8*j + (7-i)] = p_mb->ppi_blocks[i_b][j + 8*i];
309     }
310 }
311 memcpy(p_mb->ppi_blocks[i_b], meuh, 128);
312         }
313 */ 
314         
315         /*
316          * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
317          */
318         (p_mb->pf_addb[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
319                               p_mb->p_data[i_b], p_mb->i_addb_c_stride );
320     }
321 //#endif
322     /*
323      * Decoding is finished, release the macroblock and free
324      * unneeded memory.
325      */
326     vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
327 }
328
329 /*******************************************************************************
330  * vdec_AddBlock : add a block
331  *******************************************************************************/
332 void vdec_AddBlock( vdec_thread_t * p_vdec, elem_t * p_block, yuv_data_t * p_data, int i_incr )
333 {
334     int i_x, i_y;
335     
336     for( i_y = 0; i_y < 8; i_y++ )
337     {
338         for( i_x = 0; i_x < 8; i_x++ )
339         {
340 #ifdef MPEG2_COMPLIANT
341             *p_data = p_vdec->pi_crop[*p_data + *p_block++];
342             p_data++;
343 #else
344             *p_data++ += *p_block++;
345 #endif
346         }
347         p_data += i_incr;
348     }
349 }
350
351 /*******************************************************************************
352  * vdec_CopyBlock : copy a block
353  *******************************************************************************/
354 void vdec_CopyBlock( vdec_thread_t * p_vdec, elem_t * p_block, yuv_data_t * p_data, int i_incr )
355 {
356     int i_y;
357 //fprintf(stderr, "%d ", i_incr );
358
359     for( i_y = 0; i_y < 8; i_y++ )
360     {
361 #if 0
362         /* elem_t and yuv_data_t are the same */
363         memcpy( p_data, p_block, 8*sizeof(yuv_data_t) );
364         p_data += i_incr+8;
365         p_block += 8;
366 #else
367         int i_x;
368
369         for( i_x = 0; i_x < 8; i_x++ )
370         {
371             /* ??? Need clip to be MPEG-2 compliant */
372             /* ??? Why does the reference decoder add 128 ??? */
373             *p_data++ = p_vdec->pi_crop[*p_block++];
374         }
375         p_data += i_incr;
376 #endif
377     }
378 }
379
380 /*******************************************************************************
381  * vdec_DummyBlock : dummy function that does nothing
382  *******************************************************************************/
383 void vdec_DummyBlock( vdec_thread_t * p_vdec, elem_t * p_block, yuv_data_t * p_data, int i_incr )
384 {
385 }