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 *****************************************************************************/
210 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
211 yuv_data_t * p_data, int i_incr )
215 for( i_y = 0; i_y < 8; i_y++ )
217 for( i_x = 0; i_x < 8; i_x++ )
219 *p_data = p_vdec->pi_crop[*p_data + *p_block++];
226 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
227 yuv_data_t * p_data, int i_incr )
230 "pxor %%mm7,%%mm7\n\t"
232 "movq (%0),%%mm1\n\t"
233 "movq %%mm1,%%mm2\n\t"
234 "punpckhbw %%mm7,%%mm1\n\t"
235 "punpcklbw %%mm7,%%mm2\n\t"
236 "paddw (%1),%%mm2\n\t"
237 "paddw 8(%1),%%mm1\n\t"
238 "packuswb %%mm1,%%mm2\n\t"
239 "movq %%mm2,(%0)\n\t"
242 "movq (%0),%%mm1\n\t"
243 "movq %%mm1,%%mm2\n\t"
244 "punpckhbw %%mm7,%%mm1\n\t"
245 "punpcklbw %%mm7,%%mm2\n\t"
246 "paddw 16(%1),%%mm2\n\t"
247 "paddw 24(%1),%%mm1\n\t"
248 "packuswb %%mm1,%%mm2\n\t"
249 "movq %%mm2,(%0)\n\t"
252 "movq (%0),%%mm1\n\t"
253 "movq %%mm1,%%mm2\n\t"
254 "punpckhbw %%mm7,%%mm1\n\t"
255 "punpcklbw %%mm7,%%mm2\n\t"
256 "paddw 32(%1),%%mm2\n\t"
257 "paddw 40(%1),%%mm1\n\t"
258 "packuswb %%mm1,%%mm2\n\t"
259 "movq %%mm2,(%0)\n\t"
262 "movq (%0),%%mm1\n\t"
263 "movq %%mm1,%%mm2\n\t"
264 "punpckhbw %%mm7,%%mm1\n\t"
265 "punpcklbw %%mm7,%%mm2\n\t"
266 "paddw 48(%1),%%mm2\n\t"
267 "paddw 56(%1),%%mm1\n\t"
268 "packuswb %%mm1,%%mm2\n\t"
269 "movq %%mm2,(%0)\n\t"
272 "movq (%0),%%mm1\n\t"
273 "movq %%mm1,%%mm2\n\t"
274 "punpckhbw %%mm7,%%mm1\n\t"
275 "punpcklbw %%mm7,%%mm2\n\t"
276 "paddw 64(%1),%%mm2\n\t"
277 "paddw 72(%1),%%mm1\n\t"
278 "packuswb %%mm1,%%mm2\n\t"
279 "movq %%mm2,(%0)\n\t"
282 "movq (%0),%%mm1\n\t"
283 "movq %%mm1,%%mm2\n\t"
284 "punpckhbw %%mm7,%%mm1\n\t"
285 "punpcklbw %%mm7,%%mm2\n\t"
286 "paddw 80(%1),%%mm2\n\t"
287 "paddw 88(%1),%%mm1\n\t"
288 "packuswb %%mm1,%%mm2\n\t"
289 "movq %%mm2,(%0)\n\t"
292 "movq (%0),%%mm1\n\t"
293 "movq %%mm1,%%mm2\n\t"
294 "punpckhbw %%mm7,%%mm1\n\t"
295 "punpcklbw %%mm7,%%mm2\n\t"
296 "paddw 96(%1),%%mm2\n\t"
297 "paddw 104(%1),%%mm1\n\t"
298 "packuswb %%mm1,%%mm2\n\t"
299 "movq %%mm2,(%0)\n\t"
302 "movq (%0),%%mm1\n\t"
303 "movq %%mm1,%%mm2\n\t"
304 "punpckhbw %%mm7,%%mm1\n\t"
305 "punpcklbw %%mm7,%%mm2\n\t"
306 "paddw 112(%1),%%mm2\n\t"
307 "paddw 120(%1),%%mm1\n\t"
308 "packuswb %%mm1,%%mm2\n\t"
309 "movq %%mm2,(%0)\n\t"
312 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
317 /*****************************************************************************
318 * CopyBlock : copy a block
319 *****************************************************************************/
321 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
322 yuv_data_t * p_data, int i_incr )
326 for( i_y = 0; i_y < 8; i_y++ )
328 for( i_x = 0; i_x < 8; i_x++ )
330 *p_data++ = p_vdec->pi_crop[*p_block++];
336 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
337 yuv_data_t * p_data, int i_incr )
340 "movq (%1),%%mm0\n\t"
341 "packuswb 8(%1),%%mm0\n\t"
342 "movq %%mm0,(%0)\n\t"
345 "movq 16(%1),%%mm0\n\t"
346 "packuswb 24(%1),%%mm0\n\t"
347 "movq %%mm0,(%0)\n\t"
350 "movq 32(%1),%%mm0\n\t"
351 "packuswb 40(%1),%%mm0\n\t"
352 "movq %%mm0,(%0)\n\t"
355 "movq 48(%1),%%mm0\n\t"
356 "packuswb 56(%1),%%mm0\n\t"
357 "movq %%mm0,(%0)\n\t"
360 "movq 64(%1),%%mm0\n\t"
361 "packuswb 72(%1),%%mm0\n\t"
362 "movq %%mm0,(%0)\n\t"
365 "movq 80(%1),%%mm0\n\t"
366 "packuswb 88(%1),%%mm0\n\t"
367 "movq %%mm0,(%0)\n\t"
370 "movq 96(%1),%%mm0\n\t"
371 "packuswb 104(%1),%%mm0\n\t"
372 "movq %%mm0,(%0)\n\t"
375 "movq 112(%1),%%mm0\n\t"
376 "packuswb 120(%1),%%mm0\n\t"
377 "movq %%mm0,(%0)\n\t"
379 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
384 /*****************************************************************************
385 * vdec_DecodeMacroblock : decode a macroblock of a picture
386 *****************************************************************************/
387 #define DECODEBLOCKS( OPBLOCK ) \
391 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
394 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
396 if( p_mb->i_coded_block_pattern & i_mask ) \
399 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
401 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
402 p_mb->pi_sparse_pos[i_b] ); \
405 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
408 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
409 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
414 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; \
415 i_b++, i_mask >>= 1 ) \
417 if( p_mb->i_coded_block_pattern & i_mask ) \
420 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
422 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
423 p_mb->pi_sparse_pos[i_b] ); \
426 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
429 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
430 p_mb->p_data[i_b], p_mb->i_addb_c_stride ); \
436 static __inline__ void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
438 void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
441 if( !(p_mb->i_mb_type & MB_INTRA) )
444 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
446 if( p_mb->pf_motion == 0 )
448 fprintf( stderr, "vdec error: pf_motion set to NULL\n" );
452 p_mb->pf_motion( p_mb );
455 DECODEBLOCKS( AddBlock )
459 DECODEBLOCKS( CopyBlock )
463 * Decoding is finished, release the macroblock and free
466 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
470 /*****************************************************************************
471 * RunThread: video decoder thread
472 *****************************************************************************
473 * Video decoder thread. This function does only return when the thread is
475 *****************************************************************************/
476 static void RunThread( vdec_thread_t *p_vdec )
478 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
482 * Initialize thread and free configuration
484 p_vdec->b_error = vdec_InitThread( p_vdec );
485 if( p_vdec->b_error )
492 * Main loop - it is not executed if an error occured during
495 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
499 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
501 vdec_DecodeMacroblock( p_vdec, p_mb );
508 if( p_vdec->b_error )
510 ErrorThread( p_vdec );