1 /*******************************************************************************
2 * video_decoder.c : video decoder thread
4 *******************************************************************************/
6 /* ?? passer en terminate/destroy avec les signaux supplémentaires */
8 /*******************************************************************************
10 *******************************************************************************/
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 "vpar_synchro.h"
42 #include "video_parser.h"
47 static int InitThread ( vdec_thread_t *p_vdec );
48 static void RunThread ( vdec_thread_t *p_vdec );
49 static void ErrorThread ( vdec_thread_t *p_vdec );
50 static void EndThread ( vdec_thread_t *p_vdec );
51 static void DecodeMacroblock ( vdec_thread_t *p_vdec,
52 macroblock_t * p_mb );
54 /*******************************************************************************
55 * vdec_CreateThread: create a video decoder thread
56 *******************************************************************************
57 * This function creates a new video decoder thread, and returns a pointer
58 * to its description. On error, it returns NULL.
59 * Following configuration properties are used:
61 *******************************************************************************/
62 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
64 vdec_thread_t * p_vdec;
66 intf_DbgMsg("vdec debug: creating video decoder thread\n");
68 /* Allocate the memory needed to store the thread's structure */
69 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
71 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
76 * Initialize the thread properties
82 * Initialize the parser properties
84 p_vdec->p_vpar = p_vpar;
86 /* Spawn the video decoder thread */
87 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
88 (vlc_thread_func_t)RunThread, (void *)p_vdec) )
90 intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
95 intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
99 /*******************************************************************************
100 * vdec_DestroyThread: destroy a video decoder thread
101 *******************************************************************************
102 * Destroy and terminate thread. This function will return 0 if the thread could
103 * be destroyed, and non 0 else. The last case probably means that the thread
104 * was still active, and another try may succeed.
105 *******************************************************************************/
106 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
108 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
110 /* Ask thread to kill itself */
112 /* Make sure the parser thread leaves the GetByte() function */
113 vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
114 vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
115 vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
118 /* Waiting for the decoder thread to exit */
119 /* Remove this as soon as the "status" flag is implemented */
120 vlc_thread_join( p_vdec->thread_id );
123 /* following functions are local */
125 /*******************************************************************************
126 * InitThread: initialize video decoder thread
127 *******************************************************************************
128 * This function is called from RunThread and performs the second step of the
129 * initialization. It returns 0 on success. Note that the thread's flag are not
130 * modified inside this function.
131 *******************************************************************************/
132 static int InitThread( vdec_thread_t *p_vdec )
136 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
138 /* Initialize other properties */
141 p_vdec->c_idle_loops = 0;
142 p_vdec->c_decoded_pictures = 0;
143 p_vdec->c_decoded_i_pictures = 0;
144 p_vdec->c_decoded_p_pictures = 0;
145 p_vdec->c_decoded_b_pictures = 0;
148 /* Init crop table */
149 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
150 for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
152 p_vdec->pi_crop[i_dummy] = 0;
154 for( ; i_dummy < 255; i_dummy ++ )
156 p_vdec->pi_crop[i_dummy] = i_dummy;
158 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
160 p_vdec->pi_crop[i_dummy] = 255;
163 /* Mark thread as running and return */
164 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
168 /*******************************************************************************
169 * RunThread: video decoder thread
170 *******************************************************************************
171 * Video decoder thread. This function does only return when the thread is
173 *******************************************************************************/
174 static void RunThread( vdec_thread_t *p_vdec )
176 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
180 * Initialize thread and free configuration
182 p_vdec->b_error = InitThread( p_vdec );
183 if( p_vdec->b_error )
190 * Main loop - it is not executed if an error occured during
193 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
197 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
199 DecodeMacroblock( p_vdec, p_mb );
206 if( p_vdec->b_error )
208 ErrorThread( p_vdec );
216 /*******************************************************************************
217 * ErrorThread: RunThread() error loop
218 *******************************************************************************
219 * This function is called when an error occured during thread main's loop. The
220 * thread can still receive feed, but must be ready to terminate as soon as
222 *******************************************************************************/
223 static void ErrorThread( vdec_thread_t *p_vdec )
227 /* Wait until a `die' order */
228 while( !p_vdec->b_die )
230 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
231 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
235 /*******************************************************************************
236 * EndThread: thread destruction
237 *******************************************************************************
238 * This function is called when the thread ends after a sucessfull
240 *******************************************************************************/
241 static void EndThread( vdec_thread_t *p_vdec )
243 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
246 /*******************************************************************************
247 * DecodeMacroblock : decode a macroblock of a picture
248 *******************************************************************************/
249 static void DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
254 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
256 (*p_mb->pf_motion)( p_mb );
259 for( i_b = 0; i_b < 4; i_b++ )
262 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
264 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
265 p_mb->pi_sparse_pos[i_b] );
268 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
270 (p_mb->pf_addb[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
271 p_mb->p_data[i_b], p_mb->i_addb_l_stride );
275 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; i_b++ )
278 * Inverse DCT (ISO/IEC 13818-2 section Annex A)
280 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
281 p_mb->pi_sparse_pos[i_b] );
284 * Adding prediction and coefficient data (ISO/IEC 13818-2 section 7.6.8)
286 (p_mb->pf_addb[i_b])( p_vdec, p_mb->ppi_blocks[i_b],
287 p_mb->p_data[i_b], p_mb->i_addb_c_stride );
291 * Decoding is finished, release the macroblock and free
294 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
297 /*******************************************************************************
298 * vdec_AddBlock : add a block
299 *******************************************************************************/
300 void vdec_AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block, yuv_data_t * p_data, int i_incr )
304 for( i_y = 0; i_y < 8; i_y++ )
306 for( i_x = 0; i_x < 8; i_x++ )
308 *p_data = p_vdec->pi_crop[*p_data + *p_block++];
315 /*******************************************************************************
316 * vdec_CopyBlock : copy a block
317 *******************************************************************************/
318 void vdec_CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block, yuv_data_t * p_data, int i_incr )
322 for( i_y = 0; i_y < 8; i_y++ )
326 for( i_x = 0; i_x < 8; i_x++ )
328 /* ??? Why does the reference decoder add 128 ??? */
329 *p_data++ = p_vdec->pi_crop[*p_block++];
335 /*******************************************************************************
336 * vdec_DummyBlock : dummy function that does nothing
337 *******************************************************************************/
338 void vdec_DummyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block, yuv_data_t * p_data, int i_incr )