1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_decoder.c,v 1.42 2000/12/22 13:04:45 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 */
43 #include "stream_control.h"
44 #include "input_ext-dec.h"
47 #include "video_output.h"
49 #include "vdec_idct.h"
50 #include "video_decoder.h"
51 #include "vdec_motion.h"
53 #include "vpar_blocks.h"
54 #include "vpar_headers.h"
55 #include "vpar_synchro.h"
56 #include "video_parser.h"
57 #include "video_fifo.h"
63 static int vdec_InitThread ( vdec_thread_t *p_vdec );
65 static void RunThread ( vdec_thread_t *p_vdec );
66 static void ErrorThread ( vdec_thread_t *p_vdec );
67 static void EndThread ( vdec_thread_t *p_vdec );
69 /*****************************************************************************
70 * vdec_CreateThread: create a video decoder thread
71 *****************************************************************************
72 * This function creates a new video decoder thread, and returns a pointer
73 * to its description. On error, it returns NULL.
74 * Following configuration properties are used:
76 *****************************************************************************/
77 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
79 vdec_thread_t * p_vdec;
81 intf_DbgMsg("vdec debug: creating video decoder thread");
83 /* Allocate the memory needed to store the thread's structure */
84 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
86 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
91 * Initialize the thread properties
97 * Initialize the parser properties
99 p_vdec->p_vpar = p_vpar;
101 /* Spawn the video decoder thread */
102 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
103 (vlc_thread_func_t)RunThread, (void *)p_vdec) )
105 intf_ErrMsg("vdec error: can't spawn video decoder thread");
110 intf_DbgMsg("vdec debug: video decoder thread (%p) created", p_vdec);
114 /*****************************************************************************
115 * vdec_DestroyThread: destroy a video decoder thread
116 *****************************************************************************
117 * Destroy and terminate thread. This function will return 0 if the thread could
118 * be destroyed, and non 0 else. The last case probably means that the thread
119 * was still active, and another try may succeed.
120 *****************************************************************************/
121 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
123 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p", p_vdec);
125 /* Ask thread to kill itself */
129 /* Make sure the decoder thread leaves the vpar_GetMacroblock() function */
130 vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
131 vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
132 vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
135 /* Waiting for the decoder thread to exit */
136 /* Remove this as soon as the "status" flag is implemented */
137 vlc_thread_join( p_vdec->thread_id );
140 /* following functions are local */
142 /*****************************************************************************
143 * vdec_InitThread: initialize video decoder thread
144 *****************************************************************************
145 * This function is called from RunThread and performs the second step of the
146 * initialization. It returns 0 on success. Note that the thread's flag are not
147 * modified inside this function.
148 *****************************************************************************/
150 static int vdec_InitThread( vdec_thread_t *p_vdec )
152 int vdec_InitThread( vdec_thread_t *p_vdec )
159 intf_DbgMsg("vdec debug: initializing video decoder thread %p", p_vdec);
162 /* Init crop table */
163 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
164 for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
166 p_vdec->pi_crop[i_dummy] = 0;
168 for( ; i_dummy < 255; i_dummy ++ )
170 p_vdec->pi_crop[i_dummy] = i_dummy;
172 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
174 p_vdec->pi_crop[i_dummy] = 255;
179 /* Re-nice ourself */
180 if( nice(VDEC_NICE) == -1 )
182 intf_WarnMsg( 2, "vdec warning : couldn't nice() (%s)",
187 /* Mark thread as running and return */
188 intf_DbgMsg("vdec debug: InitThread(%p) succeeded", p_vdec);
192 /*****************************************************************************
193 * ErrorThread: RunThread() error loop
194 *****************************************************************************
195 * This function is called when an error occured during thread main's loop. The
196 * thread can still receive feed, but must be ready to terminate as soon as
198 *****************************************************************************/
199 static void ErrorThread( vdec_thread_t *p_vdec )
203 /* Wait until a `die' order */
204 while( !p_vdec->b_die )
206 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
207 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
211 /*****************************************************************************
212 * EndThread: thread destruction
213 *****************************************************************************
214 * This function is called when the thread ends after a sucessful
216 *****************************************************************************/
217 static void EndThread( vdec_thread_t *p_vdec )
219 intf_DbgMsg("vdec debug: EndThread(%p)", p_vdec);
222 /*****************************************************************************
223 * AddBlock : add a block
224 *****************************************************************************/
226 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
227 yuv_data_t * p_data, int i_incr )
231 for( i_y = 0; i_y < 8; i_y++ )
233 for( i_x = 0; i_x < 8; i_x++ )
235 *p_data = p_vdec->pi_crop[*p_data + *p_block++];
242 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
243 yuv_data_t * p_data, int i_incr )
246 "pxor %%mm7,%%mm7\n\t"
248 "movq (%0),%%mm1\n\t"
249 "movq %%mm1,%%mm2\n\t"
250 "punpckhbw %%mm7,%%mm1\n\t"
251 "punpcklbw %%mm7,%%mm2\n\t"
252 "paddw (%1),%%mm2\n\t"
253 "paddw 8(%1),%%mm1\n\t"
254 "packuswb %%mm1,%%mm2\n\t"
255 "movq %%mm2,(%0)\n\t"
258 "movq (%0),%%mm1\n\t"
259 "movq %%mm1,%%mm2\n\t"
260 "punpckhbw %%mm7,%%mm1\n\t"
261 "punpcklbw %%mm7,%%mm2\n\t"
262 "paddw 16(%1),%%mm2\n\t"
263 "paddw 24(%1),%%mm1\n\t"
264 "packuswb %%mm1,%%mm2\n\t"
265 "movq %%mm2,(%0)\n\t"
268 "movq (%0),%%mm1\n\t"
269 "movq %%mm1,%%mm2\n\t"
270 "punpckhbw %%mm7,%%mm1\n\t"
271 "punpcklbw %%mm7,%%mm2\n\t"
272 "paddw 32(%1),%%mm2\n\t"
273 "paddw 40(%1),%%mm1\n\t"
274 "packuswb %%mm1,%%mm2\n\t"
275 "movq %%mm2,(%0)\n\t"
278 "movq (%0),%%mm1\n\t"
279 "movq %%mm1,%%mm2\n\t"
280 "punpckhbw %%mm7,%%mm1\n\t"
281 "punpcklbw %%mm7,%%mm2\n\t"
282 "paddw 48(%1),%%mm2\n\t"
283 "paddw 56(%1),%%mm1\n\t"
284 "packuswb %%mm1,%%mm2\n\t"
285 "movq %%mm2,(%0)\n\t"
288 "movq (%0),%%mm1\n\t"
289 "movq %%mm1,%%mm2\n\t"
290 "punpckhbw %%mm7,%%mm1\n\t"
291 "punpcklbw %%mm7,%%mm2\n\t"
292 "paddw 64(%1),%%mm2\n\t"
293 "paddw 72(%1),%%mm1\n\t"
294 "packuswb %%mm1,%%mm2\n\t"
295 "movq %%mm2,(%0)\n\t"
298 "movq (%0),%%mm1\n\t"
299 "movq %%mm1,%%mm2\n\t"
300 "punpckhbw %%mm7,%%mm1\n\t"
301 "punpcklbw %%mm7,%%mm2\n\t"
302 "paddw 80(%1),%%mm2\n\t"
303 "paddw 88(%1),%%mm1\n\t"
304 "packuswb %%mm1,%%mm2\n\t"
305 "movq %%mm2,(%0)\n\t"
308 "movq (%0),%%mm1\n\t"
309 "movq %%mm1,%%mm2\n\t"
310 "punpckhbw %%mm7,%%mm1\n\t"
311 "punpcklbw %%mm7,%%mm2\n\t"
312 "paddw 96(%1),%%mm2\n\t"
313 "paddw 104(%1),%%mm1\n\t"
314 "packuswb %%mm1,%%mm2\n\t"
315 "movq %%mm2,(%0)\n\t"
318 "movq (%0),%%mm1\n\t"
319 "movq %%mm1,%%mm2\n\t"
320 "punpckhbw %%mm7,%%mm1\n\t"
321 "punpcklbw %%mm7,%%mm2\n\t"
322 "paddw 112(%1),%%mm2\n\t"
323 "paddw 120(%1),%%mm1\n\t"
324 "packuswb %%mm1,%%mm2\n\t"
325 "movq %%mm2,(%0)\n\t"
328 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
333 /*****************************************************************************
334 * CopyBlock : copy a block
335 *****************************************************************************/
337 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
338 yuv_data_t * p_data, int i_incr )
342 for( i_y = 0; i_y < 8; i_y++ )
344 for( i_x = 0; i_x < 8; i_x++ )
346 *p_data++ = p_vdec->pi_crop[*p_block++];
352 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
353 yuv_data_t * p_data, int i_incr )
356 "movq (%1),%%mm0\n\t"
357 "packuswb 8(%1),%%mm0\n\t"
358 "movq %%mm0,(%0)\n\t"
361 "movq 16(%1),%%mm0\n\t"
362 "packuswb 24(%1),%%mm0\n\t"
363 "movq %%mm0,(%0)\n\t"
366 "movq 32(%1),%%mm0\n\t"
367 "packuswb 40(%1),%%mm0\n\t"
368 "movq %%mm0,(%0)\n\t"
371 "movq 48(%1),%%mm0\n\t"
372 "packuswb 56(%1),%%mm0\n\t"
373 "movq %%mm0,(%0)\n\t"
376 "movq 64(%1),%%mm0\n\t"
377 "packuswb 72(%1),%%mm0\n\t"
378 "movq %%mm0,(%0)\n\t"
381 "movq 80(%1),%%mm0\n\t"
382 "packuswb 88(%1),%%mm0\n\t"
383 "movq %%mm0,(%0)\n\t"
386 "movq 96(%1),%%mm0\n\t"
387 "packuswb 104(%1),%%mm0\n\t"
388 "movq %%mm0,(%0)\n\t"
391 "movq 112(%1),%%mm0\n\t"
392 "packuswb 120(%1),%%mm0\n\t"
393 "movq %%mm0,(%0)\n\t"
395 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
400 /*****************************************************************************
401 * vdec_DecodeMacroblock : decode a macroblock of a picture
402 *****************************************************************************/
403 #define DECODEBLOCKSC( OPBLOCK ) \
407 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
410 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
412 if( p_mb->i_coded_block_pattern & i_mask ) \
415 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
417 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
418 p_mb->pi_sparse_pos[i_b] ); \
421 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
424 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
425 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
430 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; \
431 i_b++, i_mask >>= 1 ) \
433 if( p_mb->i_coded_block_pattern & i_mask ) \
436 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
438 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
439 p_mb->pi_sparse_pos[i_b] ); \
442 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
445 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
446 p_mb->p_data[i_b], p_mb->i_addb_c_stride ); \
451 #define DECODEBLOCKSBW( OPBLOCK ) \
455 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
458 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
460 if( p_mb->i_coded_block_pattern & i_mask ) \
463 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
465 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
466 p_mb->pi_sparse_pos[i_b] ); \
469 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
472 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
473 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
478 void vdec_DecodeMacroblockC ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
480 if( !(p_mb->i_mb_type & MB_INTRA) )
483 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
485 if( p_mb->pf_motion == 0 )
487 intf_ErrMsg( "vdec error: pf_motion set to NULL" );
491 p_mb->pf_motion( p_mb );
494 DECODEBLOCKSC( AddBlock )
498 DECODEBLOCKSC( CopyBlock )
502 * Decoding is finished, release the macroblock and free
505 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
508 void vdec_DecodeMacroblockBW ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
510 if( !(p_mb->i_mb_type & MB_INTRA) )
513 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
515 if( p_mb->pf_motion == 0 )
517 intf_ErrMsg( "vdec error: pf_motion set to NULL" );
521 p_mb->pf_motion( p_mb );
524 DECODEBLOCKSBW( AddBlock )
528 DECODEBLOCKSBW( CopyBlock )
532 * Decoding is finished, release the macroblock and free
535 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
540 /*****************************************************************************
541 * RunThread: video decoder thread
542 *****************************************************************************
543 * Video decoder thread. This function does only return when the thread is
545 *****************************************************************************/
546 static void RunThread( vdec_thread_t *p_vdec )
548 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
552 * Initialize thread and free configuration
554 p_vdec->b_error = vdec_InitThread( p_vdec );
555 if( p_vdec->b_error )
562 * Main loop - it is not executed if an error occured during
565 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
569 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
571 vdec_DecodeMacroblockC ( p_vdec, p_mb );
578 if( p_vdec->b_error )
580 ErrorThread( p_vdec );