1 /*******************************************************************************
2 * video_decoder.c : video decoder thread
4 *******************************************************************************/
6 /* ?? passer en terminate/destroy avec les signaux supplémentaires */
8 /*******************************************************************************
10 *******************************************************************************/
18 #include <X11/extensions/XShm.h>
23 #include "vlc_thread.h"
26 #include "debug.h" /* ?? temporaire, requis par netlist.h */
29 #include "input_netlist.h"
30 #include "decoder_fifo.h"
32 #include "video_output.h"
33 #include "video_parser.h"
35 #include "undec_picture.h"
36 #include "video_fifo.h"
37 #include "video_decoder.h"
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 );
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:
56 *******************************************************************************/
57 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
59 vdec_thread_t * p_vdec;
61 intf_DbgMsg("vdec debug: creating video decoder thread\n");
63 /* Allocate the memory needed to store the thread's structure */
64 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
66 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
71 * Initialize the thread properties
77 * Initialize the parser properties
79 p_vdec->p_vpar = p_vpar;
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) )
85 intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
90 intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
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 */ )
103 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
105 /* Ask thread to kill itself */
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 );
113 /* following functions are local */
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 )
124 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
126 /* Initialize other properties */
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;
136 /* Mark thread as running and return */
137 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
141 /*******************************************************************************
142 * RunThread: video decoder thread
143 *******************************************************************************
144 * Video decoder thread. This function does only return when the thread is
146 *******************************************************************************/
147 static void RunThread( vdec_thread_t *p_vdec )
149 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
153 * Initialize thread and free configuration
155 p_vdec->b_error = InitThread( p_vdec );
156 if( p_vdec->b_error )
163 * Main loop - it is not executed if an error occured during
166 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
170 if( (p_mb = GetMacroblock( &p_vdec->p_vpar.vfifo )) != NULL )
172 DecodeMacroblock( p_vdec, p_mb );
179 if( p_vdec->b_error )
181 ErrorThread( p_vdec );
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
195 *******************************************************************************/
196 static void ErrorThread( vdec_thread_t *p_vdec )
198 undec_picture_t * p_undec_p;
200 /* Wait until a `die' order */
201 while( !p_vdec->b_die )
203 p_undec_p = GetPicture( p_vdec->p_vpar.vfifo );
204 DestroyPicture( p_vdec->p_vpar.vfifo, p_undec_p );
207 msleep( VDEC_IDLE_SLEEP );
211 /*******************************************************************************
212 * EndThread: thread destruction
213 *******************************************************************************
214 * This function is called when the thread ends after a sucessfull
216 *******************************************************************************/
217 static void EndThread( vdec_thread_t *p_vdec )
219 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
222 /*******************************************************************************
223 * DecodeMacroblock : decode a macroblock of a picture
224 *******************************************************************************/
225 static void DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
230 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
232 (*p_mb->pf_motion)( p_mb );
235 for( i_b = 0; i_b < 4; i_b++ )
238 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
240 (*p_mb->pf_idct[i_b])( p_mb, p_mb->ppi_blocks[i_b], p_mb->pi_sparse_pos[i_b] );
243 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
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 );
250 for( i_b = 4; i_b < 4 + 2*p_mb->i_chroma_nb_blocks; i_b++ )
253 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
255 (*p_mb->pf_idct[i_b])( p_mb, p_mb->ppi_blocks[i_b], p_mb->pi_sparse_pos[i_b] );
258 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
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 );
265 * Decoding is finished, release the macroblock and free
268 vpar_ReleaseMacroblock( &p_vdec->p_vpar.vfifo, p_mb );
271 /*******************************************************************************
272 * vdec_AddBlock : add a block
273 *******************************************************************************/
274 void vdec_AddBlock( elem_t * p_block, data_t * p_data, int i_incr )
278 for( i_y = 0; i_y < 8; i_y++ )
280 for( i_x = 0; i_x < 8; i_x++ )
282 /* ??? Need clip to be MPEG-2 compliant */
283 *p_data++ += *p_block++;
289 /*******************************************************************************
290 * vdec_CopyBlock : copy a block
291 *******************************************************************************/
292 void vdec_CopyBlock( elem_t * p_block, data_t * p_data, int i_incr )
296 for( i_y = 0; i_y < 8; i_y++ )
299 /* elem_t and data_t are the same */
300 memcopy( p_data, p_block, 8*sizeof(data_t) );
304 for( i_x = 0; i_x < 8; i_x++ )
306 /* ??? Need clip to be MPEG-2 compliant */
307 /* ??? Why does the reference decoder add 128 ??? */
308 *p_data++ = *p_block++;
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 )