1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *****************************************************************************/
24 /* FIXME: passer en terminate/destroy avec les signaux supplémentaires ?? */
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* free() */
30 #include <unistd.h> /* getpid() */
31 #include <sys/types.h> /* on BSD, uio.h needs types.h */
32 #include <sys/uio.h> /* for input.h */
42 #include "decoder_fifo.h"
44 #include "video_output.h"
46 #include "vdec_idct.h"
47 #include "video_decoder.h"
48 #include "vdec_motion.h"
50 #include "vpar_blocks.h"
51 #include "vpar_headers.h"
52 #include "vpar_synchro.h"
53 #include "video_parser.h"
54 #include "video_fifo.h"
60 static int vdec_InitThread ( vdec_thread_t *p_vdec );
61 static void vdec_DecodeMacroblock( vdec_thread_t *p_vdec,
62 macroblock_t * p_mb );
64 static void RunThread ( vdec_thread_t *p_vdec );
65 static void ErrorThread ( vdec_thread_t *p_vdec );
66 static void EndThread ( vdec_thread_t *p_vdec );
68 /*****************************************************************************
69 * vdec_CreateThread: create a video decoder thread
70 *****************************************************************************
71 * This function creates a new video decoder thread, and returns a pointer
72 * to its description. On error, it returns NULL.
73 * Following configuration properties are used:
75 *****************************************************************************/
76 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
78 vdec_thread_t * p_vdec;
80 intf_DbgMsg("vdec debug: creating video decoder thread\n");
82 /* Allocate the memory needed to store the thread's structure */
83 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
85 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
90 * Initialize the thread properties
96 * Initialize the parser properties
98 p_vdec->p_vpar = p_vpar;
100 /* Spawn the video decoder thread */
101 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
102 (vlc_thread_func_t)RunThread, (void *)p_vdec) )
104 intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
109 intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
113 /*****************************************************************************
114 * vdec_DestroyThread: destroy a video decoder thread
115 *****************************************************************************
116 * Destroy and terminate thread. This function will return 0 if the thread could
117 * be destroyed, and non 0 else. The last case probably means that the thread
118 * was still active, and another try may succeed.
119 *****************************************************************************/
120 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
122 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
124 /* Ask thread to kill itself */
128 /* Make sure the decoder thread leaves the vpar_GetMacroblock() function */
129 vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
130 vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
131 vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
134 /* Waiting for the decoder thread to exit */
135 /* Remove this as soon as the "status" flag is implemented */
136 vlc_thread_join( p_vdec->thread_id );
139 /* following functions are local */
141 /*****************************************************************************
142 * vdec_InitThread: initialize video decoder thread
143 *****************************************************************************
144 * This function is called from RunThread and performs the second step of the
145 * initialization. It returns 0 on success. Note that the thread's flag are not
146 * modified inside this function.
147 *****************************************************************************/
149 static int vdec_InitThread( vdec_thread_t *p_vdec )
151 int vdec_InitThread( vdec_thread_t *p_vdec )
156 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
158 /* Initialize other properties */
161 p_vdec->c_idle_loops = 0;
162 p_vdec->c_decoded_pictures = 0;
163 p_vdec->c_decoded_i_pictures = 0;
164 p_vdec->c_decoded_p_pictures = 0;
165 p_vdec->c_decoded_b_pictures = 0;
168 /* Init crop table */
169 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
170 for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
172 p_vdec->pi_crop[i_dummy] = 0;
174 for( ; i_dummy < 255; i_dummy ++ )
176 p_vdec->pi_crop[i_dummy] = i_dummy;
178 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
180 p_vdec->pi_crop[i_dummy] = 255;
183 /* Mark thread as running and return */
184 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
188 /*****************************************************************************
189 * ErrorThread: RunThread() error loop
190 *****************************************************************************
191 * This function is called when an error occured during thread main's loop. The
192 * thread can still receive feed, but must be ready to terminate as soon as
194 *****************************************************************************/
195 static void ErrorThread( vdec_thread_t *p_vdec )
199 /* Wait until a `die' order */
200 while( !p_vdec->b_die )
202 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
203 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
207 /*****************************************************************************
208 * EndThread: thread destruction
209 *****************************************************************************
210 * This function is called when the thread ends after a sucessfull
212 *****************************************************************************/
213 static void EndThread( vdec_thread_t *p_vdec )
215 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
218 /*****************************************************************************
219 * AddBlock : add a block
220 *****************************************************************************/
222 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
223 yuv_data_t * p_data, int i_incr )
227 for( i_y = 0; i_y < 8; i_y++ )
229 for( i_x = 0; i_x < 8; i_x++ )
231 *p_data = p_vdec->pi_crop[*p_data + *p_block++];
238 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
239 yuv_data_t * p_data, int i_incr )
242 "pxor %%mm7,%%mm7\n\t"
244 "movq (%0),%%mm1\n\t"
245 "movq %%mm1,%%mm2\n\t"
246 "punpckhbw %%mm7,%%mm1\n\t"
247 "punpcklbw %%mm7,%%mm2\n\t"
248 "paddw (%1),%%mm2\n\t"
249 "paddw 8(%1),%%mm1\n\t"
250 "packuswb %%mm1,%%mm2\n\t"
251 "movq %%mm2,(%0)\n\t"
254 "movq (%0),%%mm1\n\t"
255 "movq %%mm1,%%mm2\n\t"
256 "punpckhbw %%mm7,%%mm1\n\t"
257 "punpcklbw %%mm7,%%mm2\n\t"
258 "paddw 16(%1),%%mm2\n\t"
259 "paddw 24(%1),%%mm1\n\t"
260 "packuswb %%mm1,%%mm2\n\t"
261 "movq %%mm2,(%0)\n\t"
264 "movq (%0),%%mm1\n\t"
265 "movq %%mm1,%%mm2\n\t"
266 "punpckhbw %%mm7,%%mm1\n\t"
267 "punpcklbw %%mm7,%%mm2\n\t"
268 "paddw 32(%1),%%mm2\n\t"
269 "paddw 40(%1),%%mm1\n\t"
270 "packuswb %%mm1,%%mm2\n\t"
271 "movq %%mm2,(%0)\n\t"
274 "movq (%0),%%mm1\n\t"
275 "movq %%mm1,%%mm2\n\t"
276 "punpckhbw %%mm7,%%mm1\n\t"
277 "punpcklbw %%mm7,%%mm2\n\t"
278 "paddw 48(%1),%%mm2\n\t"
279 "paddw 56(%1),%%mm1\n\t"
280 "packuswb %%mm1,%%mm2\n\t"
281 "movq %%mm2,(%0)\n\t"
284 "movq (%0),%%mm1\n\t"
285 "movq %%mm1,%%mm2\n\t"
286 "punpckhbw %%mm7,%%mm1\n\t"
287 "punpcklbw %%mm7,%%mm2\n\t"
288 "paddw 64(%1),%%mm2\n\t"
289 "paddw 72(%1),%%mm1\n\t"
290 "packuswb %%mm1,%%mm2\n\t"
291 "movq %%mm2,(%0)\n\t"
294 "movq (%0),%%mm1\n\t"
295 "movq %%mm1,%%mm2\n\t"
296 "punpckhbw %%mm7,%%mm1\n\t"
297 "punpcklbw %%mm7,%%mm2\n\t"
298 "paddw 80(%1),%%mm2\n\t"
299 "paddw 88(%1),%%mm1\n\t"
300 "packuswb %%mm1,%%mm2\n\t"
301 "movq %%mm2,(%0)\n\t"
304 "movq (%0),%%mm1\n\t"
305 "movq %%mm1,%%mm2\n\t"
306 "punpckhbw %%mm7,%%mm1\n\t"
307 "punpcklbw %%mm7,%%mm2\n\t"
308 "paddw 96(%1),%%mm2\n\t"
309 "paddw 104(%1),%%mm1\n\t"
310 "packuswb %%mm1,%%mm2\n\t"
311 "movq %%mm2,(%0)\n\t"
314 "movq (%0),%%mm1\n\t"
315 "movq %%mm1,%%mm2\n\t"
316 "punpckhbw %%mm7,%%mm1\n\t"
317 "punpcklbw %%mm7,%%mm2\n\t"
318 "paddw 112(%1),%%mm2\n\t"
319 "paddw 120(%1),%%mm1\n\t"
320 "packuswb %%mm1,%%mm2\n\t"
321 "movq %%mm2,(%0)\n\t"
324 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
329 /*****************************************************************************
330 * CopyBlock : copy a block
331 *****************************************************************************/
333 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
334 yuv_data_t * p_data, int i_incr )
338 for( i_y = 0; i_y < 8; i_y++ )
340 for( i_x = 0; i_x < 8; i_x++ )
342 *p_data++ = p_vdec->pi_crop[*p_block++];
348 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
349 yuv_data_t * p_data, int i_incr )
352 "movq (%1),%%mm0\n\t"
353 "packuswb 8(%1),%%mm0\n\t"
354 "movq %%mm0,(%0)\n\t"
357 "movq 16(%1),%%mm0\n\t"
358 "packuswb 24(%1),%%mm0\n\t"
359 "movq %%mm0,(%0)\n\t"
362 "movq 32(%1),%%mm0\n\t"
363 "packuswb 40(%1),%%mm0\n\t"
364 "movq %%mm0,(%0)\n\t"
367 "movq 48(%1),%%mm0\n\t"
368 "packuswb 56(%1),%%mm0\n\t"
369 "movq %%mm0,(%0)\n\t"
372 "movq 64(%1),%%mm0\n\t"
373 "packuswb 72(%1),%%mm0\n\t"
374 "movq %%mm0,(%0)\n\t"
377 "movq 80(%1),%%mm0\n\t"
378 "packuswb 88(%1),%%mm0\n\t"
379 "movq %%mm0,(%0)\n\t"
382 "movq 96(%1),%%mm0\n\t"
383 "packuswb 104(%1),%%mm0\n\t"
384 "movq %%mm0,(%0)\n\t"
387 "movq 112(%1),%%mm0\n\t"
388 "packuswb 120(%1),%%mm0\n\t"
389 "movq %%mm0,(%0)\n\t"
391 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
396 /*****************************************************************************
397 * vdec_DecodeMacroblock : decode a macroblock of a picture
398 *****************************************************************************/
399 #define DECODEBLOCKS( OPBLOCK ) \
403 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
406 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
408 if( p_mb->i_coded_block_pattern & i_mask ) \
411 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
413 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
414 p_mb->pi_sparse_pos[i_b] ); \
417 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
420 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
421 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
426 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; \
427 i_b++, i_mask >>= 1 ) \
429 if( p_mb->i_coded_block_pattern & i_mask ) \
432 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
434 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
435 p_mb->pi_sparse_pos[i_b] ); \
438 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
441 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
442 p_mb->p_data[i_b], p_mb->i_addb_c_stride ); \
448 static __inline__ void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
450 void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
453 if( !(p_mb->i_mb_type & MB_INTRA) )
456 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
458 if( p_mb->pf_motion == 0 )
460 intf_ErrMsg( "vdec error: pf_motion set to NULL\n" );
464 p_mb->pf_motion( p_mb );
467 DECODEBLOCKS( AddBlock )
471 DECODEBLOCKS( CopyBlock )
475 * Decoding is finished, release the macroblock and free
478 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
482 /*****************************************************************************
483 * RunThread: video decoder thread
484 *****************************************************************************
485 * Video decoder thread. This function does only return when the thread is
487 *****************************************************************************/
488 static void RunThread( vdec_thread_t *p_vdec )
490 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
494 * Initialize thread and free configuration
496 p_vdec->b_error = vdec_InitThread( p_vdec );
497 if( p_vdec->b_error )
504 * Main loop - it is not executed if an error occured during
507 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
511 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
513 vdec_DecodeMacroblock( p_vdec, p_mb );
520 if( p_vdec->b_error )
522 ErrorThread( p_vdec );