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