]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
video_decoder : ajout de la crop table dans AddBlock ;
[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 #ifdef MPEG2_COMPLIANT
130     int i_dummy;
131 #endif
132
133     intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
134
135     /* Initialize other properties */
136 #ifdef STATS
137     p_vdec->c_loops = 0;    
138     p_vdec->c_idle_loops = 0;
139     p_vdec->c_decoded_pictures = 0;
140     p_vdec->c_decoded_i_pictures = 0;
141     p_vdec->c_decoded_p_pictures = 0;
142     p_vdec->c_decoded_b_pictures = 0;
143 #endif
144
145 #ifdef MPEG2_COMPLIANT
146     /* Init crop table */
147     p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
148     for( i_dummy = -VDEC_CROPRANGE; i_dummy < -256; i_dummy ++ )
149     {
150         p_vdec->pi_crop[i_dummy] = -256;
151     }
152     for( ; i_dummy < 255; i_dummy ++ )
153     {
154         p_vdec->pi_crop[i_dummy] = i_dummy;
155     }
156     for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
157     {
158         p_vdec->pi_crop[i_dummy] = 255;
159     }
160 #endif
161
162     /* Mark thread as running and return */
163     intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);    
164     return( 0 );    
165 }
166
167 /*******************************************************************************
168  * RunThread: video decoder thread
169  *******************************************************************************
170  * Video decoder thread. This function does only return when the thread is
171  * terminated. 
172  *******************************************************************************/
173 static void RunThread( vdec_thread_t *p_vdec )
174 {
175     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
176                 p_vdec, getpid());
177
178     /* 
179      * Initialize thread and free configuration 
180      */
181     p_vdec->b_error = InitThread( p_vdec );
182     if( p_vdec->b_error )
183     {
184         return;
185     }
186     p_vdec->b_run = 1;
187
188     /*
189      * Main loop - it is not executed if an error occured during
190      * initialization
191      */
192     while( (!p_vdec->b_die) && (!p_vdec->b_error) )
193     {
194         macroblock_t *          p_mb;
195         
196         if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
197         {
198             DecodeMacroblock( p_vdec, p_mb );
199         }
200     } 
201
202     /*
203      * Error loop
204      */
205     if( p_vdec->b_error )
206     {
207         ErrorThread( p_vdec );        
208     }
209
210     /* End of thread */
211     EndThread( p_vdec );
212     p_vdec->b_run = 0;
213 }
214
215 /*******************************************************************************
216  * ErrorThread: RunThread() error loop
217  *******************************************************************************
218  * This function is called when an error occured during thread main's loop. The
219  * thread can still receive feed, but must be ready to terminate as soon as
220  * possible.
221  *******************************************************************************/
222 static void ErrorThread( vdec_thread_t *p_vdec )
223 {
224     macroblock_t *       p_mb;
225
226     /* Wait until a `die' order */
227     while( !p_vdec->b_die )
228     {
229         p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
230         vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
231
232         /* Sleep a while */
233         msleep( VDEC_IDLE_SLEEP );                
234     }
235 }
236
237 /*******************************************************************************
238  * EndThread: thread destruction
239  *******************************************************************************
240  * This function is called when the thread ends after a sucessfull 
241  * initialization.
242  *******************************************************************************/
243 static void EndThread( vdec_thread_t *p_vdec )
244 {
245     intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
246 }
247
248 /*******************************************************************************
249  * DecodeMacroblock : decode a macroblock of a picture
250  *******************************************************************************/
251 static void DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
252 {
253     int             i_b;
254
255     /*
256      * Motion Compensation (ISO/IEC 13818-2 section 7.6)
257      */
258     (*p_mb->pf_motion)( p_mb );
259
260     /* luminance */
261     for( i_b = 0; i_b < 4; i_b++ )
262     {
263         /*
264          * Inverse DCT (ISO/IEC 13818-2 section Annex A)
265          */
266         (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
267                               p_mb->pi_sparse_pos[i_b] );
268
269         /*
270          * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
271          */
272         (p_mb->pf_addb[i_b])( p_mb->ppi_blocks[i_b],
273                                p_mb->p_data[i_b], p_mb->i_l_stride );
274     }
275
276     /* chrominance */
277     for( i_b = 4; i_b < 4 + 2*p_mb->i_chroma_nb_blocks; i_b++ )
278     {
279         /*
280          * Inverse DCT (ISO/IEC 13818-2 section Annex A)
281          */
282         (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
283                               p_mb->pi_sparse_pos[i_b] );
284
285         /*
286          * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
287          */
288         (p_mb->pf_addb[i_b])( p_mb->ppi_blocks[i_b],
289                                p_mb->p_data[i_b], p_mb->i_c_stride );
290     }
291
292     /*
293      * Decoding is finished, release the macroblock and free
294      * unneeded memory.
295      */
296     vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
297 }
298
299 /*******************************************************************************
300  * vdec_AddBlock : add a block
301  *******************************************************************************/
302 void vdec_AddBlock( vdec_thread_t * p_vdec, elem_t * p_block, data_t * p_data,
303                     int i_incr )
304 {
305     int i_x, i_y;
306     
307     for( i_y = 0; i_y < 8; i_y++ )
308     {
309         for( i_x = 0; i_x < 8; i_x++ )
310         {
311 #ifdef MPEG2_COMPLIANT
312             *p_data = p_vdec->pi_clip[*p_data + *p_block++];
313             p_data++;
314 #else
315             *p_data++ += *p_block++;
316 #endif
317         }
318         p_data += i_incr;
319     }
320 }
321
322 /*******************************************************************************
323  * vdec_CopyBlock : copy a block
324  *******************************************************************************/
325 void vdec_CopyBlock( vdec_thread_t * p_vdec, elem_t * p_block, data_t * p_data,
326                      int i_incr )
327 {
328     int i_x, i_y;
329     
330     for( i_y = 0; i_y < 8; i_y++ )
331     {
332 #ifndef VDEC_DFT
333         /* elem_t and data_t are the same */
334         memcopy( p_data, p_block, 8*sizeof(data_t) );
335         p_data += i_incr+8;
336         p_block += 8;
337 #else
338         for( i_x = 0; i_x < 8; i_x++ )
339         {
340             /* ??? Need clip to be MPEG-2 compliant */
341             /* ??? Why does the reference decoder add 128 ??? */
342             *p_data++ = *p_block++;
343         }
344         p_data += i_incr;
345 #endif
346     }
347 }
348
349 /*******************************************************************************
350  * vdec_DummyBlock : dummy function that does nothing
351  *******************************************************************************/
352 void vdec_DummyBlock( vdec_thread_t * p_vdec, elem_t * p_block, data_t * p_data,
353                       int i_incr )
354 {
355 }