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