1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_decoder.c,v 1.43 2000/12/23 03:10:59 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gaƫl Hendryckx <jimmy@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <stdlib.h> /* free() */
31 #include <unistd.h> /* getpid() */
32 #include <sys/types.h> /* on BSD, uio.h needs types.h */
33 #include <sys/uio.h> /* for input.h */
34 #include <errno.h> /* errno */
44 #include "stream_control.h"
45 #include "input_ext-dec.h"
48 #include "video_output.h"
50 #include "vdec_idct.h"
51 #include "video_decoder.h"
52 #include "vdec_motion.h"
54 #include "vpar_blocks.h"
55 #include "vpar_headers.h"
56 #include "vpar_synchro.h"
57 #include "video_parser.h"
58 #include "video_fifo.h"
64 static int vdec_InitThread ( vdec_thread_t *p_vdec );
66 static void RunThread ( vdec_thread_t *p_vdec );
67 static void ErrorThread ( vdec_thread_t *p_vdec );
68 static void EndThread ( vdec_thread_t *p_vdec );
70 /*****************************************************************************
71 * vdec_CreateThread: create a video decoder thread
72 *****************************************************************************
73 * This function creates a new video decoder thread, and returns a pointer
74 * to its description. On error, it returns NULL.
75 * Following configuration properties are used:
77 *****************************************************************************/
78 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
80 vdec_thread_t * p_vdec;
82 intf_DbgMsg("vdec debug: creating video decoder thread");
84 /* Allocate the memory needed to store the thread's structure */
85 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
87 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
92 * Initialize the thread properties
98 * Initialize the parser properties
100 p_vdec->p_vpar = p_vpar;
102 /* Spawn the video decoder thread */
103 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
104 (vlc_thread_func_t)RunThread, (void *)p_vdec) )
106 intf_ErrMsg("vdec error: can't spawn video decoder thread");
111 intf_DbgMsg("vdec debug: video decoder thread (%p) created", p_vdec);
115 /*****************************************************************************
116 * vdec_DestroyThread: destroy a video decoder thread
117 *****************************************************************************
118 * Destroy and terminate thread. This function will return 0 if the thread could
119 * be destroyed, and non 0 else. The last case probably means that the thread
120 * was still active, and another try may succeed.
121 *****************************************************************************/
122 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
124 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p", p_vdec);
126 /* Ask thread to kill itself */
130 /* Make sure the decoder thread leaves the vpar_GetMacroblock() function */
131 vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
132 vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
133 vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
136 /* Waiting for the decoder thread to exit */
137 /* Remove this as soon as the "status" flag is implemented */
138 vlc_thread_join( p_vdec->thread_id );
141 /* following functions are local */
143 /*****************************************************************************
144 * vdec_InitThread: initialize video decoder thread
145 *****************************************************************************
146 * This function is called from RunThread and performs the second step of the
147 * initialization. It returns 0 on success. Note that the thread's flag are not
148 * modified inside this function.
149 *****************************************************************************/
151 static int vdec_InitThread( vdec_thread_t *p_vdec )
153 int vdec_InitThread( vdec_thread_t *p_vdec )
160 intf_DbgMsg("vdec debug: initializing video decoder thread %p", p_vdec);
163 /* Init crop table */
164 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
165 for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
167 p_vdec->pi_crop[i_dummy] = 0;
169 for( ; i_dummy < 255; i_dummy ++ )
171 p_vdec->pi_crop[i_dummy] = i_dummy;
173 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
175 p_vdec->pi_crop[i_dummy] = 255;
180 /* Re-nice ourself */
181 if( nice(VDEC_NICE) == -1 )
183 intf_WarnMsg( 2, "vdec warning : couldn't nice() (%s)",
188 /* Mark thread as running and return */
189 intf_DbgMsg("vdec debug: InitThread(%p) succeeded", 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 );
212 /*****************************************************************************
213 * EndThread: thread destruction
214 *****************************************************************************
215 * This function is called when the thread ends after a sucessful
217 *****************************************************************************/
218 static void EndThread( vdec_thread_t *p_vdec )
220 intf_DbgMsg("vdec debug: EndThread(%p)", p_vdec);
223 /*****************************************************************************
224 * AddBlock : add a block
225 *****************************************************************************/
227 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
228 yuv_data_t * p_data, int i_incr )
232 for( i_y = 0; i_y < 8; i_y++ )
234 for( i_x = 0; i_x < 8; i_x++ )
236 *p_data = p_vdec->pi_crop[*p_data + *p_block++];
243 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
244 yuv_data_t * p_data, int i_incr )
247 "pxor %%mm7,%%mm7\n\t"
249 "movq (%0),%%mm1\n\t"
250 "movq %%mm1,%%mm2\n\t"
251 "punpckhbw %%mm7,%%mm1\n\t"
252 "punpcklbw %%mm7,%%mm2\n\t"
253 "paddw (%1),%%mm2\n\t"
254 "paddw 8(%1),%%mm1\n\t"
255 "packuswb %%mm1,%%mm2\n\t"
256 "movq %%mm2,(%0)\n\t"
259 "movq (%0),%%mm1\n\t"
260 "movq %%mm1,%%mm2\n\t"
261 "punpckhbw %%mm7,%%mm1\n\t"
262 "punpcklbw %%mm7,%%mm2\n\t"
263 "paddw 16(%1),%%mm2\n\t"
264 "paddw 24(%1),%%mm1\n\t"
265 "packuswb %%mm1,%%mm2\n\t"
266 "movq %%mm2,(%0)\n\t"
269 "movq (%0),%%mm1\n\t"
270 "movq %%mm1,%%mm2\n\t"
271 "punpckhbw %%mm7,%%mm1\n\t"
272 "punpcklbw %%mm7,%%mm2\n\t"
273 "paddw 32(%1),%%mm2\n\t"
274 "paddw 40(%1),%%mm1\n\t"
275 "packuswb %%mm1,%%mm2\n\t"
276 "movq %%mm2,(%0)\n\t"
279 "movq (%0),%%mm1\n\t"
280 "movq %%mm1,%%mm2\n\t"
281 "punpckhbw %%mm7,%%mm1\n\t"
282 "punpcklbw %%mm7,%%mm2\n\t"
283 "paddw 48(%1),%%mm2\n\t"
284 "paddw 56(%1),%%mm1\n\t"
285 "packuswb %%mm1,%%mm2\n\t"
286 "movq %%mm2,(%0)\n\t"
289 "movq (%0),%%mm1\n\t"
290 "movq %%mm1,%%mm2\n\t"
291 "punpckhbw %%mm7,%%mm1\n\t"
292 "punpcklbw %%mm7,%%mm2\n\t"
293 "paddw 64(%1),%%mm2\n\t"
294 "paddw 72(%1),%%mm1\n\t"
295 "packuswb %%mm1,%%mm2\n\t"
296 "movq %%mm2,(%0)\n\t"
299 "movq (%0),%%mm1\n\t"
300 "movq %%mm1,%%mm2\n\t"
301 "punpckhbw %%mm7,%%mm1\n\t"
302 "punpcklbw %%mm7,%%mm2\n\t"
303 "paddw 80(%1),%%mm2\n\t"
304 "paddw 88(%1),%%mm1\n\t"
305 "packuswb %%mm1,%%mm2\n\t"
306 "movq %%mm2,(%0)\n\t"
309 "movq (%0),%%mm1\n\t"
310 "movq %%mm1,%%mm2\n\t"
311 "punpckhbw %%mm7,%%mm1\n\t"
312 "punpcklbw %%mm7,%%mm2\n\t"
313 "paddw 96(%1),%%mm2\n\t"
314 "paddw 104(%1),%%mm1\n\t"
315 "packuswb %%mm1,%%mm2\n\t"
316 "movq %%mm2,(%0)\n\t"
319 "movq (%0),%%mm1\n\t"
320 "movq %%mm1,%%mm2\n\t"
321 "punpckhbw %%mm7,%%mm1\n\t"
322 "punpcklbw %%mm7,%%mm2\n\t"
323 "paddw 112(%1),%%mm2\n\t"
324 "paddw 120(%1),%%mm1\n\t"
325 "packuswb %%mm1,%%mm2\n\t"
326 "movq %%mm2,(%0)\n\t"
329 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
334 /*****************************************************************************
335 * CopyBlock : copy a block
336 *****************************************************************************/
338 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
339 yuv_data_t * p_data, int i_incr )
343 for( i_y = 0; i_y < 8; i_y++ )
345 for( i_x = 0; i_x < 8; i_x++ )
347 *p_data++ = p_vdec->pi_crop[*p_block++];
353 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
354 yuv_data_t * p_data, int i_incr )
357 "movq (%1),%%mm0\n\t"
358 "packuswb 8(%1),%%mm0\n\t"
359 "movq %%mm0,(%0)\n\t"
362 "movq 16(%1),%%mm0\n\t"
363 "packuswb 24(%1),%%mm0\n\t"
364 "movq %%mm0,(%0)\n\t"
367 "movq 32(%1),%%mm0\n\t"
368 "packuswb 40(%1),%%mm0\n\t"
369 "movq %%mm0,(%0)\n\t"
372 "movq 48(%1),%%mm0\n\t"
373 "packuswb 56(%1),%%mm0\n\t"
374 "movq %%mm0,(%0)\n\t"
377 "movq 64(%1),%%mm0\n\t"
378 "packuswb 72(%1),%%mm0\n\t"
379 "movq %%mm0,(%0)\n\t"
382 "movq 80(%1),%%mm0\n\t"
383 "packuswb 88(%1),%%mm0\n\t"
384 "movq %%mm0,(%0)\n\t"
387 "movq 96(%1),%%mm0\n\t"
388 "packuswb 104(%1),%%mm0\n\t"
389 "movq %%mm0,(%0)\n\t"
392 "movq 112(%1),%%mm0\n\t"
393 "packuswb 120(%1),%%mm0\n\t"
394 "movq %%mm0,(%0)\n\t"
396 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
401 /*****************************************************************************
402 * vdec_DecodeMacroblock : decode a macroblock of a picture
403 *****************************************************************************/
404 #define DECODEBLOCKSC( OPBLOCK ) \
408 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
411 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
413 if( p_mb->i_coded_block_pattern & i_mask ) \
416 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
418 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
419 p_mb->pi_sparse_pos[i_b] ); \
422 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
425 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
426 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
431 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; \
432 i_b++, i_mask >>= 1 ) \
434 if( p_mb->i_coded_block_pattern & i_mask ) \
437 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
439 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
440 p_mb->pi_sparse_pos[i_b] ); \
443 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
446 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
447 p_mb->p_data[i_b], p_mb->i_addb_c_stride ); \
452 #define DECODEBLOCKSBW( OPBLOCK ) \
456 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
459 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
461 if( p_mb->i_coded_block_pattern & i_mask ) \
464 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
466 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
467 p_mb->pi_sparse_pos[i_b] ); \
470 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
473 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
474 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
479 void vdec_DecodeMacroblockC ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
481 if( !(p_mb->i_mb_type & MB_INTRA) )
484 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
486 if( p_mb->pf_motion == 0 )
488 intf_ErrMsg( "vdec error: pf_motion set to NULL" );
492 p_mb->pf_motion( p_mb );
495 DECODEBLOCKSC( AddBlock )
499 DECODEBLOCKSC( CopyBlock )
503 * Decoding is finished, release the macroblock and free
506 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
509 void vdec_DecodeMacroblockBW ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
511 if( !(p_mb->i_mb_type & MB_INTRA) )
514 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
516 if( p_mb->pf_motion == 0 )
518 intf_ErrMsg( "vdec error: pf_motion set to NULL" );
522 p_mb->pf_motion( p_mb );
525 DECODEBLOCKSBW( AddBlock )
529 DECODEBLOCKSBW( CopyBlock )
533 * Decoding is finished, release the macroblock and free
536 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
541 /*****************************************************************************
542 * RunThread: video decoder thread
543 *****************************************************************************
544 * Video decoder thread. This function does only return when the thread is
546 *****************************************************************************/
547 static void RunThread( vdec_thread_t *p_vdec )
549 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
553 * Initialize thread and free configuration
555 p_vdec->b_error = vdec_InitThread( p_vdec );
556 if( p_vdec->b_error )
563 * Main loop - it is not executed if an error occured during
566 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
570 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
572 vdec_DecodeMacroblockC ( p_vdec, p_mb );
579 if( p_vdec->b_error )
581 ErrorThread( p_vdec );