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"
34 #include "vdec_idct.h"
35 #include "video_decoder.h"
36 #include "vdec_motion.h"
38 #include "vpar_blocks.h"
39 #include "vpar_headers.h"
40 #include "video_fifo.h"
41 #include "video_parser.h"
46 static int InitThread ( vdec_thread_t *p_vdec );
47 static void RunThread ( vdec_thread_t *p_vdec );
48 static void ErrorThread ( vdec_thread_t *p_vdec );
49 static void EndThread ( vdec_thread_t *p_vdec );
50 static void DecodeMacroblock ( vdec_thread_t *p_vdec,
51 macroblock_t * p_mb );
53 /*******************************************************************************
54 * vdec_CreateThread: create a video decoder thread
55 *******************************************************************************
56 * This function creates a new video decoder thread, and returns a pointer
57 * to its description. On error, it returns NULL.
58 * Following configuration properties are used:
60 *******************************************************************************/
61 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
63 vdec_thread_t * p_vdec;
65 intf_DbgMsg("vdec debug: creating video decoder thread\n");
67 /* Allocate the memory needed to store the thread's structure */
68 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
70 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
75 * Initialize the thread properties
81 * Initialize the parser properties
83 p_vdec->p_vpar = p_vpar;
85 /* Spawn the video decoder thread */
86 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
87 (vlc_thread_func)RunThread, (void *)p_vdec) )
89 intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
94 intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
98 /*******************************************************************************
99 * vdec_DestroyThread: destroy a video decoder thread
100 *******************************************************************************
101 * Destroy and terminate thread. This function will return 0 if the thread could
102 * be destroyed, and non 0 else. The last case probably means that the thread
103 * was still active, and another try may succeed.
104 *******************************************************************************/
105 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
107 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
109 /* Ask thread to kill itself */
112 /* Waiting for the decoder thread to exit */
113 /* Remove this as soon as the "status" flag is implemented */
114 vlc_thread_join( p_vdec->thread_id );
117 /* following functions are local */
119 /*******************************************************************************
120 * InitThread: initialize video decoder thread
121 *******************************************************************************
122 * This function is called from RunThread and performs the second step of the
123 * initialization. It returns 0 on success. Note that the thread's flag are not
124 * modified inside this function.
125 *******************************************************************************/
126 static int InitThread( vdec_thread_t *p_vdec )
128 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
130 /* Initialize other properties */
133 p_vdec->c_idle_loops = 0;
134 p_vdec->c_decoded_pictures = 0;
135 p_vdec->c_decoded_i_pictures = 0;
136 p_vdec->c_decoded_p_pictures = 0;
137 p_vdec->c_decoded_b_pictures = 0;
140 /* Mark thread as running and return */
141 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
145 /*******************************************************************************
146 * RunThread: video decoder thread
147 *******************************************************************************
148 * Video decoder thread. This function does only return when the thread is
150 *******************************************************************************/
151 static void RunThread( vdec_thread_t *p_vdec )
153 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
157 * Initialize thread and free configuration
159 p_vdec->b_error = InitThread( p_vdec );
160 if( p_vdec->b_error )
167 * Main loop - it is not executed if an error occured during
170 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
174 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
176 DecodeMacroblock( p_vdec, p_mb );
183 if( p_vdec->b_error )
185 ErrorThread( p_vdec );
193 /*******************************************************************************
194 * ErrorThread: RunThread() error loop
195 *******************************************************************************
196 * This function is called when an error occured during thread main's loop. The
197 * thread can still receive feed, but must be ready to terminate as soon as
199 *******************************************************************************/
200 static void ErrorThread( vdec_thread_t *p_vdec )
204 /* Wait until a `die' order */
205 while( !p_vdec->b_die )
207 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
208 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
211 msleep( VDEC_IDLE_SLEEP );
215 /*******************************************************************************
216 * EndThread: thread destruction
217 *******************************************************************************
218 * This function is called when the thread ends after a sucessfull
220 *******************************************************************************/
221 static void EndThread( vdec_thread_t *p_vdec )
223 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
226 /*******************************************************************************
227 * DecodeMacroblock : decode a macroblock of a picture
228 *******************************************************************************/
229 static void DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
234 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
236 (*p_mb->pf_motion)( p_mb );
239 for( i_b = 0; i_b < 4; i_b++ )
242 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
244 (p_mb->pf_idct[i_b])( p_mb->ppi_blocks[i_b], p_mb->pi_sparse_pos[i_b] );
247 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
249 (p_mb->pf_addb[i_b])( p_mb->ppi_blocks[i_b],
250 p_mb->p_data[i_b], p_mb->i_lum_incr );
254 for( i_b = 4; i_b < 4 + 2*p_mb->i_chroma_nb_blocks; i_b++ )
257 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
259 (p_mb->pf_idct[i_b])( p_mb->ppi_blocks[i_b], p_mb->pi_sparse_pos[i_b] );
262 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
264 (p_mb->pf_addb[i_b])( p_mb->ppi_blocks[i_b],
265 p_mb->p_data[i_b], p_mb->i_chroma_incr );
269 * Decoding is finished, release the macroblock and free
272 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
275 /*******************************************************************************
276 * vdec_AddBlock : add a block
277 *******************************************************************************/
278 void vdec_AddBlock( elem_t * p_block, data_t * p_data, int i_incr )
282 for( i_y = 0; i_y < 8; i_y++ )
284 for( i_x = 0; i_x < 8; i_x++ )
286 /* ??? Need clip to be MPEG-2 compliant */
287 *p_data++ += *p_block++;
293 /*******************************************************************************
294 * vdec_CopyBlock : copy a block
295 *******************************************************************************/
296 void vdec_CopyBlock( elem_t * p_block, data_t * p_data, int i_incr )
300 for( i_y = 0; i_y < 8; i_y++ )
303 /* elem_t and data_t are the same */
304 memcopy( p_data, p_block, 8*sizeof(data_t) );
308 for( i_x = 0; i_x < 8; i_x++ )
310 /* ??? Need clip to be MPEG-2 compliant */
311 /* ??? Why does the reference decoder add 128 ??? */
312 *p_data++ = *p_block++;
319 /*******************************************************************************
320 * vdec_DummyBlock : dummy function that does nothing
321 *******************************************************************************/
322 void vdec_DummyBlock( elem_t * p_block, data_t * p_data, int i_incr )