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 "vpar_synchro.h"
41 #include "video_parser.h"
42 #include "video_fifo.h"
48 static int vdec_InitThread ( vdec_thread_t *p_vdec );
49 static void vdec_DecodeMacroblock( vdec_thread_t *p_vdec,
50 macroblock_t * p_mb );
52 static void RunThread ( vdec_thread_t *p_vdec );
53 static void ErrorThread ( vdec_thread_t *p_vdec );
54 static void EndThread ( vdec_thread_t *p_vdec );
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 */
116 /* Make sure the decoder thread leaves the vpar_GetMacroblock() function */
117 vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
118 vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
119 vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
122 /* Waiting for the decoder thread to exit */
123 /* Remove this as soon as the "status" flag is implemented */
124 vlc_thread_join( p_vdec->thread_id );
127 /* following functions are local */
129 /*******************************************************************************
130 * vdec_InitThread: initialize video decoder thread
131 *******************************************************************************
132 * This function is called from RunThread and performs the second step of the
133 * initialization. It returns 0 on success. Note that the thread's flag are not
134 * modified inside this function.
135 *******************************************************************************/
137 static int vdec_InitThread( vdec_thread_t *p_vdec )
139 int vdec_InitThread( vdec_thread_t *p_vdec )
144 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
146 /* Initialize other properties */
149 p_vdec->c_idle_loops = 0;
150 p_vdec->c_decoded_pictures = 0;
151 p_vdec->c_decoded_i_pictures = 0;
152 p_vdec->c_decoded_p_pictures = 0;
153 p_vdec->c_decoded_b_pictures = 0;
156 /* Init crop table */
157 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
158 for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
160 p_vdec->pi_crop[i_dummy] = 0;
162 for( ; i_dummy < 255; i_dummy ++ )
164 p_vdec->pi_crop[i_dummy] = i_dummy;
166 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
168 p_vdec->pi_crop[i_dummy] = 255;
171 /* Mark thread as running and return */
172 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
176 /*******************************************************************************
177 * ErrorThread: RunThread() error loop
178 *******************************************************************************
179 * This function is called when an error occured during thread main's loop. The
180 * thread can still receive feed, but must be ready to terminate as soon as
182 *******************************************************************************/
183 static void ErrorThread( vdec_thread_t *p_vdec )
187 /* Wait until a `die' order */
188 while( !p_vdec->b_die )
190 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
191 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
195 /*******************************************************************************
196 * EndThread: thread destruction
197 *******************************************************************************
198 * This function is called when the thread ends after a sucessfull
200 *******************************************************************************/
201 static void EndThread( vdec_thread_t *p_vdec )
203 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
206 /*******************************************************************************
207 * AddBlock : add a block
208 *******************************************************************************/
209 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
210 yuv_data_t * p_data, int i_incr )
214 for( i_y = 0; i_y < 8; i_y++ )
216 for( i_x = 0; i_x < 8; i_x++ )
218 *p_data = p_vdec->pi_crop[*p_data + *p_block++];
225 /*******************************************************************************
226 * CopyBlock : copy a block
227 *******************************************************************************/
228 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
229 yuv_data_t * p_data, int i_incr )
233 for( i_y = 0; i_y < 8; i_y++ )
235 for( i_x = 0; i_x < 8; i_x++ )
237 *p_data++ = p_vdec->pi_crop[*p_block++];
243 /*******************************************************************************
244 * vdec_DecodeMacroblock : decode a macroblock of a picture
245 *******************************************************************************/
246 #define DECODEBLOCKS( OPBLOCK ) \
250 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
253 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
255 if( p_mb->i_coded_block_pattern & i_mask ) \
258 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
260 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
261 p_mb->pi_sparse_pos[i_b] ); \
264 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
267 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
268 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
273 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; \
274 i_b++, i_mask >>= 1 ) \
276 if( p_mb->i_coded_block_pattern & i_mask ) \
279 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
281 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
282 p_mb->pi_sparse_pos[i_b] ); \
285 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
288 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
289 p_mb->p_data[i_b], p_mb->i_addb_c_stride ); \
295 static __inline__ void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
297 void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
300 if( !(p_mb->i_mb_type & MB_INTRA) )
303 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
305 p_mb->pf_motion( p_mb );
307 DECODEBLOCKS( AddBlock )
311 DECODEBLOCKS( CopyBlock )
315 * Decoding is finished, release the macroblock and free
318 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
322 /*******************************************************************************
323 * RunThread: video decoder thread
324 *******************************************************************************
325 * Video decoder thread. This function does only return when the thread is
327 *******************************************************************************/
328 static void RunThread( vdec_thread_t *p_vdec )
330 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
334 * Initialize thread and free configuration
336 p_vdec->b_error = vdec_InitThread( p_vdec );
337 if( p_vdec->b_error )
344 * Main loop - it is not executed if an error occured during
347 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
351 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
353 vdec_DecodeMacroblock( p_vdec, p_mb );
360 if( p_vdec->b_error )
362 ErrorThread( p_vdec );