1 /*******************************************************************************
2 * video_decoder.c : video decoder thread
4 *******************************************************************************/
6 /* ?? passer en terminate/destroy avec les signaux supplémentaires */
8 /*******************************************************************************
10 *******************************************************************************/
22 #include <X11/extensions/XShm.h>
27 #include "vlc_thread.h"
33 #include "input_netlist.h"
34 #include "decoder_fifo.h"
36 #include "video_output.h"
37 #include "video_decoder.h"*/
40 #include "vdec_idct.h"
41 #include "video_decoder.h"
42 #include "vdec_motion.h"
44 #include "vpar_blocks.h"
45 #include "vpar_headers.h"
46 #include "video_fifo.h"
47 #include "vpar_synchro.h"
48 #include "video_parser.h"
53 static int InitThread ( vdec_thread_t *p_vdec );
54 static void RunThread ( vdec_thread_t *p_vdec );
55 static void ErrorThread ( vdec_thread_t *p_vdec );
56 static void EndThread ( vdec_thread_t *p_vdec );
57 static void DecodeMacroblock ( vdec_thread_t *p_vdec,
58 macroblock_t * p_mb );
60 /*******************************************************************************
61 * vdec_CreateThread: create a video decoder thread
62 *******************************************************************************
63 * This function creates a new video decoder thread, and returns a pointer
64 * to its description. On error, it returns NULL.
65 * Following configuration properties are used:
67 *******************************************************************************/
68 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
70 vdec_thread_t * p_vdec;
72 intf_DbgMsg("vdec debug: creating video decoder thread\n");
74 /* Allocate the memory needed to store the thread's structure */
75 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
77 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
82 * Initialize the thread properties
88 * Initialize the parser properties
90 p_vdec->p_vpar = p_vpar;
92 /* Spawn the video decoder thread */
93 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
94 (vlc_thread_func)RunThread, (void *)p_vdec) )
96 intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
101 intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
105 /*******************************************************************************
106 * vdec_DestroyThread: destroy a video decoder thread
107 *******************************************************************************
108 * Destroy and terminate thread. This function will return 0 if the thread could
109 * be destroyed, and non 0 else. The last case probably means that the thread
110 * was still active, and another try may succeed.
111 *******************************************************************************/
112 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
114 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
116 /* Ask thread to kill itself */
119 /* Waiting for the decoder thread to exit */
120 /* Remove this as soon as the "status" flag is implemented */
121 vlc_thread_join( p_vdec->thread_id );
124 /* following functions are local */
126 /*******************************************************************************
127 * InitThread: initialize video decoder thread
128 *******************************************************************************
129 * This function is called from RunThread and performs the second step of the
130 * initialization. It returns 0 on success. Note that the thread's flag are not
131 * modified inside this function.
132 *******************************************************************************/
133 static int InitThread( vdec_thread_t *p_vdec )
135 #ifdef MPEG2_COMPLIANT
139 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
141 /* Initialize other properties */
144 p_vdec->c_idle_loops = 0;
145 p_vdec->c_decoded_pictures = 0;
146 p_vdec->c_decoded_i_pictures = 0;
147 p_vdec->c_decoded_p_pictures = 0;
148 p_vdec->c_decoded_b_pictures = 0;
151 #ifdef MPEG2_COMPLIANT
152 /* Init crop table */
153 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
154 for( i_dummy = -VDEC_CROPRANGE; i_dummy < -256; i_dummy ++ )
156 p_vdec->pi_crop[i_dummy] = -256;
158 for( ; i_dummy < 255; i_dummy ++ )
160 p_vdec->pi_crop[i_dummy] = i_dummy;
162 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
164 p_vdec->pi_crop[i_dummy] = 255;
168 /* Mark thread as running and return */
169 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
173 /*******************************************************************************
174 * RunThread: video decoder thread
175 *******************************************************************************
176 * Video decoder thread. This function does only return when the thread is
178 *******************************************************************************/
179 static void RunThread( vdec_thread_t *p_vdec )
181 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
185 * Initialize thread and free configuration
187 p_vdec->b_error = InitThread( p_vdec );
188 if( p_vdec->b_error )
195 * Main loop - it is not executed if an error occured during
198 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
202 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
204 DecodeMacroblock( p_vdec, p_mb );
211 if( p_vdec->b_error )
213 ErrorThread( p_vdec );
221 /*******************************************************************************
222 * ErrorThread: RunThread() error loop
223 *******************************************************************************
224 * This function is called when an error occured during thread main's loop. The
225 * thread can still receive feed, but must be ready to terminate as soon as
227 *******************************************************************************/
228 static void ErrorThread( vdec_thread_t *p_vdec )
232 /* Wait until a `die' order */
233 while( !p_vdec->b_die )
235 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
236 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
239 msleep( VDEC_IDLE_SLEEP );
243 /*******************************************************************************
244 * EndThread: thread destruction
245 *******************************************************************************
246 * This function is called when the thread ends after a sucessfull
248 *******************************************************************************/
249 static void EndThread( vdec_thread_t *p_vdec )
251 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
254 /*******************************************************************************
255 * DecodeMacroblock : decode a macroblock of a picture
256 *******************************************************************************/
257 static void DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
262 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
264 (*p_mb->pf_motion)( p_mb );
267 for( i_b = 0; i_b < 4; i_b++ )
270 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
272 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
273 p_mb->pi_sparse_pos[i_b] );
276 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
278 (p_mb->pf_addb[i_b])( p_mb->ppi_blocks[i_b],
279 p_mb->p_data[i_b], p_mb->i_l_stride );
283 for( i_b = 4; i_b < 4 + 2*p_mb->i_chroma_nb_blocks; i_b++ )
286 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
288 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
289 p_mb->pi_sparse_pos[i_b] );
292 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
294 (p_mb->pf_addb[i_b])( p_mb->ppi_blocks[i_b],
295 p_mb->p_data[i_b], p_mb->i_c_stride );
299 * Decoding is finished, release the macroblock and free
302 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
305 /*******************************************************************************
306 * vdec_AddBlock : add a block
307 *******************************************************************************/
308 void vdec_AddBlock( vdec_thread_t * p_vdec, elem_t * p_block, data_t * p_data,
313 for( i_y = 0; i_y < 8; i_y++ )
315 for( i_x = 0; i_x < 8; i_x++ )
317 #ifdef MPEG2_COMPLIANT
318 *p_data = p_vdec->pi_clip[*p_data + *p_block++];
321 *p_data++ += *p_block++;
328 /*******************************************************************************
329 * vdec_CopyBlock : copy a block
330 *******************************************************************************/
331 void vdec_CopyBlock( vdec_thread_t * p_vdec, elem_t * p_block, data_t * p_data,
336 for( i_y = 0; i_y < 8; i_y++ )
339 /* elem_t and data_t are the same */
340 memcopy( p_data, p_block, 8*sizeof(data_t) );
344 for( i_x = 0; i_x < 8; i_x++ )
346 /* ??? Need clip to be MPEG-2 compliant */
347 /* ??? Why does the reference decoder add 128 ??? */
348 *p_data++ = *p_block++;
355 /*******************************************************************************
356 * vdec_DummyBlock : dummy function that does nothing
357 *******************************************************************************/
358 void vdec_DummyBlock( vdec_thread_t * p_vdec, elem_t * p_block, data_t * p_data,