1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_decoder.c,v 1.44 2001/01/05 18:46:44 massiot 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 <errno.h> /* errno */
42 #include "stream_control.h"
43 #include "input_ext-dec.h"
46 #include "video_output.h"
48 #include "vdec_idct.h"
49 #include "video_decoder.h"
50 #include "vdec_motion.h"
52 #include "vpar_blocks.h"
53 #include "vpar_headers.h"
54 #include "vpar_synchro.h"
55 #include "video_parser.h"
56 #include "video_fifo.h"
62 static int vdec_InitThread ( vdec_thread_t *p_vdec );
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");
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");
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");
109 intf_DbgMsg("vdec debug: video decoder thread (%p) created", 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", 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 )
158 intf_DbgMsg("vdec debug: initializing video decoder thread %p", p_vdec);
161 /* Init crop table */
162 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
163 for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
165 p_vdec->pi_crop[i_dummy] = 0;
167 for( ; i_dummy < 255; i_dummy ++ )
169 p_vdec->pi_crop[i_dummy] = i_dummy;
171 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
173 p_vdec->pi_crop[i_dummy] = 255;
178 /* Re-nice ourself */
179 if( nice(VDEC_NICE) == -1 )
181 intf_WarnMsg( 2, "vdec warning : couldn't nice() (%s)",
186 /* Mark thread as running and return */
187 intf_DbgMsg("vdec debug: InitThread(%p) succeeded", p_vdec);
191 /*****************************************************************************
192 * ErrorThread: RunThread() error loop
193 *****************************************************************************
194 * This function is called when an error occured during thread main's loop. The
195 * thread can still receive feed, but must be ready to terminate as soon as
197 *****************************************************************************/
198 static void ErrorThread( vdec_thread_t *p_vdec )
202 /* Wait until a `die' order */
203 while( !p_vdec->b_die )
205 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
206 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
210 /*****************************************************************************
211 * EndThread: thread destruction
212 *****************************************************************************
213 * This function is called when the thread ends after a sucessful
215 *****************************************************************************/
216 static void EndThread( vdec_thread_t *p_vdec )
218 intf_DbgMsg("vdec debug: EndThread(%p)", p_vdec);
221 /*****************************************************************************
222 * AddBlock : add a block
223 *****************************************************************************/
225 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
226 yuv_data_t * p_data, int i_incr )
230 for( i_y = 0; i_y < 8; i_y++ )
232 for( i_x = 0; i_x < 8; i_x++ )
234 *p_data = p_vdec->pi_crop[*p_data + *p_block++];
241 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
242 yuv_data_t * p_data, int i_incr )
245 "pxor %%mm7,%%mm7\n\t"
247 "movq (%0),%%mm1\n\t"
248 "movq %%mm1,%%mm2\n\t"
249 "punpckhbw %%mm7,%%mm1\n\t"
250 "punpcklbw %%mm7,%%mm2\n\t"
251 "paddw (%1),%%mm2\n\t"
252 "paddw 8(%1),%%mm1\n\t"
253 "packuswb %%mm1,%%mm2\n\t"
254 "movq %%mm2,(%0)\n\t"
257 "movq (%0),%%mm1\n\t"
258 "movq %%mm1,%%mm2\n\t"
259 "punpckhbw %%mm7,%%mm1\n\t"
260 "punpcklbw %%mm7,%%mm2\n\t"
261 "paddw 16(%1),%%mm2\n\t"
262 "paddw 24(%1),%%mm1\n\t"
263 "packuswb %%mm1,%%mm2\n\t"
264 "movq %%mm2,(%0)\n\t"
267 "movq (%0),%%mm1\n\t"
268 "movq %%mm1,%%mm2\n\t"
269 "punpckhbw %%mm7,%%mm1\n\t"
270 "punpcklbw %%mm7,%%mm2\n\t"
271 "paddw 32(%1),%%mm2\n\t"
272 "paddw 40(%1),%%mm1\n\t"
273 "packuswb %%mm1,%%mm2\n\t"
274 "movq %%mm2,(%0)\n\t"
277 "movq (%0),%%mm1\n\t"
278 "movq %%mm1,%%mm2\n\t"
279 "punpckhbw %%mm7,%%mm1\n\t"
280 "punpcklbw %%mm7,%%mm2\n\t"
281 "paddw 48(%1),%%mm2\n\t"
282 "paddw 56(%1),%%mm1\n\t"
283 "packuswb %%mm1,%%mm2\n\t"
284 "movq %%mm2,(%0)\n\t"
287 "movq (%0),%%mm1\n\t"
288 "movq %%mm1,%%mm2\n\t"
289 "punpckhbw %%mm7,%%mm1\n\t"
290 "punpcklbw %%mm7,%%mm2\n\t"
291 "paddw 64(%1),%%mm2\n\t"
292 "paddw 72(%1),%%mm1\n\t"
293 "packuswb %%mm1,%%mm2\n\t"
294 "movq %%mm2,(%0)\n\t"
297 "movq (%0),%%mm1\n\t"
298 "movq %%mm1,%%mm2\n\t"
299 "punpckhbw %%mm7,%%mm1\n\t"
300 "punpcklbw %%mm7,%%mm2\n\t"
301 "paddw 80(%1),%%mm2\n\t"
302 "paddw 88(%1),%%mm1\n\t"
303 "packuswb %%mm1,%%mm2\n\t"
304 "movq %%mm2,(%0)\n\t"
307 "movq (%0),%%mm1\n\t"
308 "movq %%mm1,%%mm2\n\t"
309 "punpckhbw %%mm7,%%mm1\n\t"
310 "punpcklbw %%mm7,%%mm2\n\t"
311 "paddw 96(%1),%%mm2\n\t"
312 "paddw 104(%1),%%mm1\n\t"
313 "packuswb %%mm1,%%mm2\n\t"
314 "movq %%mm2,(%0)\n\t"
317 "movq (%0),%%mm1\n\t"
318 "movq %%mm1,%%mm2\n\t"
319 "punpckhbw %%mm7,%%mm1\n\t"
320 "punpcklbw %%mm7,%%mm2\n\t"
321 "paddw 112(%1),%%mm2\n\t"
322 "paddw 120(%1),%%mm1\n\t"
323 "packuswb %%mm1,%%mm2\n\t"
324 "movq %%mm2,(%0)\n\t"
327 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
332 /*****************************************************************************
333 * CopyBlock : copy a block
334 *****************************************************************************/
336 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
337 yuv_data_t * p_data, int i_incr )
341 for( i_y = 0; i_y < 8; i_y++ )
343 for( i_x = 0; i_x < 8; i_x++ )
345 *p_data++ = p_vdec->pi_crop[*p_block++];
351 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
352 yuv_data_t * p_data, int i_incr )
355 "movq (%1),%%mm0\n\t"
356 "packuswb 8(%1),%%mm0\n\t"
357 "movq %%mm0,(%0)\n\t"
360 "movq 16(%1),%%mm0\n\t"
361 "packuswb 24(%1),%%mm0\n\t"
362 "movq %%mm0,(%0)\n\t"
365 "movq 32(%1),%%mm0\n\t"
366 "packuswb 40(%1),%%mm0\n\t"
367 "movq %%mm0,(%0)\n\t"
370 "movq 48(%1),%%mm0\n\t"
371 "packuswb 56(%1),%%mm0\n\t"
372 "movq %%mm0,(%0)\n\t"
375 "movq 64(%1),%%mm0\n\t"
376 "packuswb 72(%1),%%mm0\n\t"
377 "movq %%mm0,(%0)\n\t"
380 "movq 80(%1),%%mm0\n\t"
381 "packuswb 88(%1),%%mm0\n\t"
382 "movq %%mm0,(%0)\n\t"
385 "movq 96(%1),%%mm0\n\t"
386 "packuswb 104(%1),%%mm0\n\t"
387 "movq %%mm0,(%0)\n\t"
390 "movq 112(%1),%%mm0\n\t"
391 "packuswb 120(%1),%%mm0\n\t"
392 "movq %%mm0,(%0)\n\t"
394 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
399 /*****************************************************************************
400 * vdec_DecodeMacroblock : decode a macroblock of a picture
401 *****************************************************************************/
402 #define DECODEBLOCKSC( OPBLOCK ) \
406 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
409 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
411 if( p_mb->i_coded_block_pattern & i_mask ) \
414 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
416 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
417 p_mb->pi_sparse_pos[i_b] ); \
420 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
423 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
424 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
429 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; \
430 i_b++, i_mask >>= 1 ) \
432 if( p_mb->i_coded_block_pattern & i_mask ) \
435 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
437 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
438 p_mb->pi_sparse_pos[i_b] ); \
441 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
444 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
445 p_mb->p_data[i_b], p_mb->i_addb_c_stride ); \
450 #define DECODEBLOCKSBW( OPBLOCK ) \
454 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
457 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
459 if( p_mb->i_coded_block_pattern & i_mask ) \
462 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
464 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
465 p_mb->pi_sparse_pos[i_b] ); \
468 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
471 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
472 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
477 void vdec_DecodeMacroblockC ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
479 if( !(p_mb->i_mb_type & MB_INTRA) )
482 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
484 if( p_mb->pf_motion == 0 )
486 intf_ErrMsg( "vdec error: pf_motion set to NULL" );
490 p_mb->pf_motion( p_mb );
493 DECODEBLOCKSC( AddBlock )
497 DECODEBLOCKSC( CopyBlock )
501 * Decoding is finished, release the macroblock and free
504 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
507 void vdec_DecodeMacroblockBW ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
509 if( !(p_mb->i_mb_type & MB_INTRA) )
512 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
514 if( p_mb->pf_motion == 0 )
516 intf_ErrMsg( "vdec error: pf_motion set to NULL" );
520 p_mb->pf_motion( p_mb );
523 DECODEBLOCKSBW( AddBlock )
527 DECODEBLOCKSBW( CopyBlock )
531 * Decoding is finished, release the macroblock and free
534 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
539 /*****************************************************************************
540 * RunThread: video decoder thread
541 *****************************************************************************
542 * Video decoder thread. This function does only return when the thread is
544 *****************************************************************************/
545 static void RunThread( vdec_thread_t *p_vdec )
547 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
551 * Initialize thread and free configuration
553 p_vdec->b_error = vdec_InitThread( p_vdec );
554 if( p_vdec->b_error )
561 * Main loop - it is not executed if an error occured during
564 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
568 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
570 vdec_DecodeMacroblockC ( p_vdec, p_mb );
577 if( p_vdec->b_error )
579 ErrorThread( p_vdec );