1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
7 * Gaël Hendryckx <jimmy@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
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 */
43 #include "decoder_fifo.h"
45 #include "video_output.h"
47 #include "vdec_idct.h"
48 #include "video_decoder.h"
49 #include "vdec_motion.h"
51 #include "vpar_blocks.h"
52 #include "vpar_headers.h"
53 #include "vpar_synchro.h"
54 #include "video_parser.h"
55 #include "video_fifo.h"
61 static int vdec_InitThread ( vdec_thread_t *p_vdec );
62 static void vdec_DecodeMacroblockC ( vdec_thread_t *p_vdec,
63 macroblock_t * p_mb );
64 static void vdec_DecodeMacroblockBW ( vdec_thread_t *p_vdec,
65 macroblock_t * p_mb );
67 static void RunThread ( vdec_thread_t *p_vdec );
68 static void ErrorThread ( vdec_thread_t *p_vdec );
69 static void EndThread ( vdec_thread_t *p_vdec );
71 /*****************************************************************************
72 * vdec_CreateThread: create a video decoder thread
73 *****************************************************************************
74 * This function creates a new video decoder thread, and returns a pointer
75 * to its description. On error, it returns NULL.
76 * Following configuration properties are used:
78 *****************************************************************************/
79 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
81 vdec_thread_t * p_vdec;
83 intf_DbgMsg("vdec debug: creating video decoder thread\n");
85 /* Allocate the memory needed to store the thread's structure */
86 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
88 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
93 * Initialize the thread properties
99 * Initialize the parser properties
101 p_vdec->p_vpar = p_vpar;
103 /* Spawn the video decoder thread */
104 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
105 (vlc_thread_func_t)RunThread, (void *)p_vdec) )
107 intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
112 intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
116 /*****************************************************************************
117 * vdec_DestroyThread: destroy a video decoder thread
118 *****************************************************************************
119 * Destroy and terminate thread. This function will return 0 if the thread could
120 * be destroyed, and non 0 else. The last case probably means that the thread
121 * was still active, and another try may succeed.
122 *****************************************************************************/
123 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
125 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
127 /* Ask thread to kill itself */
131 /* Make sure the decoder thread leaves the vpar_GetMacroblock() function */
132 vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
133 vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
134 vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
137 /* Waiting for the decoder thread to exit */
138 /* Remove this as soon as the "status" flag is implemented */
139 vlc_thread_join( p_vdec->thread_id );
142 /* following functions are local */
144 /*****************************************************************************
145 * vdec_InitThread: initialize video decoder thread
146 *****************************************************************************
147 * This function is called from RunThread and performs the second step of the
148 * initialization. It returns 0 on success. Note that the thread's flag are not
149 * modified inside this function.
150 *****************************************************************************/
152 static int vdec_InitThread( vdec_thread_t *p_vdec )
154 int vdec_InitThread( vdec_thread_t *p_vdec )
159 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
161 /* Initialize other properties */
164 p_vdec->c_idle_loops = 0;
165 p_vdec->c_decoded_pictures = 0;
166 p_vdec->c_decoded_i_pictures = 0;
167 p_vdec->c_decoded_p_pictures = 0;
168 p_vdec->c_decoded_b_pictures = 0;
171 /* Init crop table */
172 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
173 for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
175 p_vdec->pi_crop[i_dummy] = 0;
177 for( ; i_dummy < 255; i_dummy ++ )
179 p_vdec->pi_crop[i_dummy] = i_dummy;
181 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
183 p_vdec->pi_crop[i_dummy] = 255;
186 /* Mark thread as running and return */
187 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", 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)\n", 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 ); \
478 static __inline__ void vdec_DecodeMacroblockC ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
480 void vdec_DecodeMacroblockC ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
483 if( !(p_mb->i_mb_type & MB_INTRA) )
486 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
488 if( p_mb->pf_motion == 0 )
490 intf_ErrMsg( "vdec error: pf_motion set to NULL\n" );
494 p_mb->pf_motion( p_mb );
497 DECODEBLOCKSC( AddBlock )
501 DECODEBLOCKSC( CopyBlock )
505 * Decoding is finished, release the macroblock and free
508 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
512 static __inline__ void vdec_DecodeMacroblockBW ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
514 void vdec_DecodeMacroblockBW ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
517 if( !(p_mb->i_mb_type & MB_INTRA) )
520 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
522 if( p_mb->pf_motion == 0 )
524 intf_ErrMsg( "vdec error: pf_motion set to NULL\n" );
528 p_mb->pf_motion( p_mb );
531 DECODEBLOCKSBW( AddBlock )
535 DECODEBLOCKSBW( CopyBlock )
539 * Decoding is finished, release the macroblock and free
542 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
547 /*****************************************************************************
548 * RunThread: video decoder thread
549 *****************************************************************************
550 * Video decoder thread. This function does only return when the thread is
552 *****************************************************************************/
553 static void RunThread( vdec_thread_t *p_vdec )
555 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
559 * Initialize thread and free configuration
561 p_vdec->b_error = vdec_InitThread( p_vdec );
562 if( p_vdec->b_error )
569 * Main loop - it is not executed if an error occured during
572 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
576 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
578 p_vdec->p_vpar->p_vout->vdec_DecodeMacroblock ( p_vdec, p_mb );
585 if( p_vdec->b_error )
587 ErrorThread( p_vdec );