]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
* Optimisation de vdec_motion.c et video_decoder.c ;
[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
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 "vpar_synchro.h"
41 #include "video_parser.h"
42 #include "video_fifo.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_t)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     /* Make sure the parser thread leaves the GetByte() function */
113     vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
114     vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
115     vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
116
117
118     /* Waiting for the decoder thread to exit */
119     /* Remove this as soon as the "status" flag is implemented */
120     vlc_thread_join( p_vdec->thread_id );
121 }
122
123 /* following functions are local */
124
125 /*******************************************************************************
126  * InitThread: initialize video decoder thread
127  *******************************************************************************
128  * This function is called from RunThread and performs the second step of the
129  * initialization. It returns 0 on success. Note that the thread's flag are not
130  * modified inside this function.
131  *******************************************************************************/
132 static int InitThread( vdec_thread_t *p_vdec )
133 {
134     int i_dummy;
135
136     intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
137
138     /* Initialize other properties */
139 #ifdef STATS
140     p_vdec->c_loops = 0;    
141     p_vdec->c_idle_loops = 0;
142     p_vdec->c_decoded_pictures = 0;
143     p_vdec->c_decoded_i_pictures = 0;
144     p_vdec->c_decoded_p_pictures = 0;
145     p_vdec->c_decoded_b_pictures = 0;
146 #endif
147
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
163     /* Mark thread as running and return */
164     intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);    
165     return( 0 );    
166 }
167
168 /*******************************************************************************
169  * ErrorThread: RunThread() error loop
170  *******************************************************************************
171  * This function is called when an error occured during thread main's loop. The
172  * thread can still receive feed, but must be ready to terminate as soon as
173  * possible.
174  *******************************************************************************/
175 static void ErrorThread( vdec_thread_t *p_vdec )
176 {
177     macroblock_t *       p_mb;
178
179     /* Wait until a `die' order */
180     while( !p_vdec->b_die )
181     {
182         p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
183         vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
184     }
185 }
186
187 /*******************************************************************************
188  * EndThread: thread destruction
189  *******************************************************************************
190  * This function is called when the thread ends after a sucessfull 
191  * initialization.
192  *******************************************************************************/
193 static void EndThread( vdec_thread_t *p_vdec )
194 {
195     intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
196 }
197
198 /*******************************************************************************
199  * AddBlock : add a block
200  *******************************************************************************/
201 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
202                                  yuv_data_t * p_data, int i_incr )
203 {
204     int i_x, i_y;
205  
206     for( i_y = 0; i_y < 8; i_y++ )
207     {
208         for( i_x = 0; i_x < 8; i_x++ )
209         {
210             *p_data = p_vdec->pi_crop[*p_data + *p_block++];
211             p_data++;
212         }
213         p_data += i_incr;
214     }
215 }
216
217 /*******************************************************************************
218  * CopyBlock : copy a block
219  *******************************************************************************/
220 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
221                                   yuv_data_t * p_data, int i_incr )
222 {
223     int i_x, i_y;
224
225     for( i_y = 0; i_y < 8; i_y++ )
226     {
227         for( i_x = 0; i_x < 8; i_x++ )
228         {
229             *p_data++ = p_vdec->pi_crop[*p_block++];
230         }
231         p_data += i_incr;
232     }
233 }
234
235 /*******************************************************************************
236  * DecodeMacroblock : decode a macroblock of a picture
237  *******************************************************************************/
238 #define DECODEBLOCKS( OPBLOCK )                                         \
239 {                                                                       \
240     int             i_b, i_mask;                                        \
241                                                                         \
242     i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks);                       \
243                                                                         \
244     /* luminance */                                                     \
245     for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 )                        \
246     {                                                                   \
247         if( p_mb->i_coded_block_pattern & i_mask )                      \
248         {                                                               \
249             /*                                                          \
250              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
251              */                                                         \
252             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
253                                   p_mb->pi_sparse_pos[i_b] );           \
254                                                                         \
255             /*                                                          \
256              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
257              * section 7.6.8)                                           \
258              */                                                         \
259             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
260                      p_mb->p_data[i_b], p_mb->i_addb_l_stride );        \
261         }                                                               \
262     }                                                                   \
263                                                                         \
264     /* chrominance */                                                   \
265     for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks;                   \
266          i_b++, i_mask >>= 1 )                                          \
267     {                                                                   \
268         if( p_mb->i_coded_block_pattern & i_mask )                      \
269         {                                                               \
270             /*                                                          \
271              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
272              */                                                         \
273             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
274                                   p_mb->pi_sparse_pos[i_b] );           \
275                                                                         \
276             /*                                                          \
277              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
278              * section 7.6.8)                                           \
279              */                                                         \
280             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
281                      p_mb->p_data[i_b], p_mb->i_addb_c_stride );        \
282         }                                                               \
283     }                                                                   \
284 }
285
286 static __inline__ void DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
287 {
288     if( !(p_mb->i_mb_type & MB_INTRA) )
289     {
290         /*
291          * Motion Compensation (ISO/IEC 13818-2 section 7.6)
292          */
293         p_mb->pf_motion( p_mb );
294
295         DECODEBLOCKS( AddBlock )
296     }
297     else
298     {
299         DECODEBLOCKS( CopyBlock )
300     }
301
302     /*
303      * Decoding is finished, release the macroblock and free
304      * unneeded memory.
305      */
306     vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
307 }
308
309
310 /*******************************************************************************
311  * RunThread: video decoder thread
312  *******************************************************************************
313  * Video decoder thread. This function does only return when the thread is
314  * terminated. 
315  *******************************************************************************/
316 static void RunThread( vdec_thread_t *p_vdec )
317 {
318     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
319                 p_vdec, getpid());
320
321     /* 
322      * Initialize thread and free configuration 
323      */
324     p_vdec->b_error = InitThread( p_vdec );
325     if( p_vdec->b_error )
326     {
327         return;
328     }
329     p_vdec->b_run = 1;
330
331     /*
332      * Main loop - it is not executed if an error occured during
333      * initialization
334      */
335     while( (!p_vdec->b_die) && (!p_vdec->b_error) )
336     {
337         macroblock_t *          p_mb;
338
339         if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
340         {
341             DecodeMacroblock( p_vdec, p_mb );
342         }
343     } 
344
345     /*
346      * Error loop
347      */
348     if( p_vdec->b_error )
349     {
350         ErrorThread( p_vdec );
351     }
352
353     /* End of thread */
354     EndThread( p_vdec );
355     p_vdec->b_run = 0;
356 }