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