1 /*******************************************************************************
2 * video_decoder.c : video decoder thread
4 *******************************************************************************/
6 /* ?? passer en terminate/destroy avec les signaux supplémentaires */
8 /*******************************************************************************
10 *******************************************************************************/
20 #include <X11/extensions/XShm.h>
25 #include "vlc_thread.h"
28 #include "debug.h" /* ?? temporaire, requis par netlist.h */
31 #include "input_netlist.h"
32 #include "decoder_fifo.h"
34 #include "video_output.h"
36 #include "vdec_idct.h"
37 #include "video_decoder.h"
38 #include "vdec_motion.h"
40 #include "vpar_blocks.h"
41 #include "vpar_headers.h"
42 #include "video_fifo.h"
43 #include "vpar_synchro.h"
44 #include "video_parser.h"
49 static int InitThread ( vdec_thread_t *p_vdec );
50 static void RunThread ( vdec_thread_t *p_vdec );
51 static void ErrorThread ( vdec_thread_t *p_vdec );
52 static void EndThread ( vdec_thread_t *p_vdec );
53 static void DecodeMacroblock ( vdec_thread_t *p_vdec,
54 macroblock_t * p_mb );
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:
63 *******************************************************************************/
64 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
66 vdec_thread_t * p_vdec;
68 intf_DbgMsg("vdec debug: creating video decoder thread\n");
70 /* Allocate the memory needed to store the thread's structure */
71 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
73 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
78 * Initialize the thread properties
84 * Initialize the parser properties
86 p_vdec->p_vpar = p_vpar;
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) )
92 intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
97 intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
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 */ )
110 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
112 /* Ask thread to kill itself */
114 /* Make sure the parser thread leaves the GetByte() function */
115 vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
116 vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
117 vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
120 /* Waiting for the decoder thread to exit */
121 /* Remove this as soon as the "status" flag is implemented */
122 vlc_thread_join( p_vdec->thread_id );
125 /* following functions are local */
127 /*******************************************************************************
128 * InitThread: initialize video decoder thread
129 *******************************************************************************
130 * This function is called from RunThread and performs the second step of the
131 * initialization. It returns 0 on success. Note that the thread's flag are not
132 * modified inside this function.
133 *******************************************************************************/
134 static int InitThread( vdec_thread_t *p_vdec )
138 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
140 /* Initialize other properties */
143 p_vdec->c_idle_loops = 0;
144 p_vdec->c_decoded_pictures = 0;
145 p_vdec->c_decoded_i_pictures = 0;
146 p_vdec->c_decoded_p_pictures = 0;
147 p_vdec->c_decoded_b_pictures = 0;
150 /* Init crop table */
151 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
152 for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
154 p_vdec->pi_crop[i_dummy] = 0;
156 for( ; i_dummy < 255; i_dummy ++ )
158 p_vdec->pi_crop[i_dummy] = i_dummy;
160 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
162 p_vdec->pi_crop[i_dummy] = 255;
165 /* Mark thread as running and return */
166 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
170 /*******************************************************************************
171 * RunThread: video decoder thread
172 *******************************************************************************
173 * Video decoder thread. This function does only return when the thread is
175 *******************************************************************************/
176 static void RunThread( vdec_thread_t *p_vdec )
178 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
182 * Initialize thread and free configuration
184 p_vdec->b_error = InitThread( p_vdec );
185 if( p_vdec->b_error )
192 * Main loop - it is not executed if an error occured during
195 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
199 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
201 DecodeMacroblock( p_vdec, p_mb );
208 if( p_vdec->b_error )
210 ErrorThread( p_vdec );
218 /*******************************************************************************
219 * ErrorThread: RunThread() error loop
220 *******************************************************************************
221 * This function is called when an error occured during thread main's loop. The
222 * thread can still receive feed, but must be ready to terminate as soon as
224 *******************************************************************************/
225 static void ErrorThread( vdec_thread_t *p_vdec )
229 /* Wait until a `die' order */
230 while( !p_vdec->b_die )
232 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
233 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
237 /*******************************************************************************
238 * EndThread: thread destruction
239 *******************************************************************************
240 * This function is called when the thread ends after a sucessfull
242 *******************************************************************************/
243 static void EndThread( vdec_thread_t *p_vdec )
245 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
248 /*******************************************************************************
249 * DecodeMacroblock : decode a macroblock of a picture
250 *******************************************************************************/
251 static void DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
256 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
258 (*p_mb->pf_motion)( p_mb );
261 for( i_b = 0; i_b < 4; i_b++ )
264 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
266 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
267 p_mb->pi_sparse_pos[i_b] );
270 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
272 (p_mb->pf_addb[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
273 p_mb->p_data[i_b], p_mb->i_addb_l_stride );
277 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; i_b++ )
280 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
282 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
283 p_mb->pi_sparse_pos[i_b] );
286 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
288 (p_mb->pf_addb[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
289 p_mb->p_data[i_b], p_mb->i_addb_c_stride );
293 * Decoding is finished, release the macroblock and free
296 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
299 /*******************************************************************************
300 * vdec_AddBlock : add a block
301 *******************************************************************************/
302 void vdec_AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block, yuv_data_t * p_data, int i_incr )
306 for( i_y = 0; i_y < 8; i_y++ )
308 for( i_x = 0; i_x < 8; i_x++ )
310 *p_data = p_vdec->pi_crop[*p_data + *p_block++];
317 /*******************************************************************************
318 * vdec_CopyBlock : copy a block
319 *******************************************************************************/
320 void vdec_CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block, yuv_data_t * p_data, int i_incr )
323 //fprintf(stderr, "%d ", i_incr );
325 for( i_y = 0; i_y < 8; i_y++ )
328 /* dctelem_t and yuv_data_t are the same */
329 memcpy( p_data, p_block, 8*sizeof(yuv_data_t) );
335 for( i_x = 0; i_x < 8; i_x++ )
337 /* ??? Need clip to be MPEG-2 compliant */
338 /* ??? Why does the reference decoder add 128 ??? */
339 *p_data++ = p_vdec->pi_crop[*p_block++];
346 /*******************************************************************************
347 * vdec_DummyBlock : dummy function that does nothing
348 *******************************************************************************/
349 void vdec_DummyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block, yuv_data_t * p_data, int i_incr )