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_DecodeMacroblock( vdec_thread_t *p_vdec,
63 macroblock_t * p_mb );
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\n");
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\n");
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\n");
110 intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", 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\n", 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 )
157 intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
159 /* Initialize other properties */
162 p_vdec->c_idle_loops = 0;
163 p_vdec->c_decoded_pictures = 0;
164 p_vdec->c_decoded_i_pictures = 0;
165 p_vdec->c_decoded_p_pictures = 0;
166 p_vdec->c_decoded_b_pictures = 0;
169 /* Init crop table */
170 p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
171 for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
173 p_vdec->pi_crop[i_dummy] = 0;
175 for( ; i_dummy < 255; i_dummy ++ )
177 p_vdec->pi_crop[i_dummy] = i_dummy;
179 for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
181 p_vdec->pi_crop[i_dummy] = 255;
184 /* Mark thread as running and return */
185 intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
189 /*****************************************************************************
190 * ErrorThread: RunThread() error loop
191 *****************************************************************************
192 * This function is called when an error occured during thread main's loop. The
193 * thread can still receive feed, but must be ready to terminate as soon as
195 *****************************************************************************/
196 static void ErrorThread( vdec_thread_t *p_vdec )
200 /* Wait until a `die' order */
201 while( !p_vdec->b_die )
203 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
204 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
208 /*****************************************************************************
209 * EndThread: thread destruction
210 *****************************************************************************
211 * This function is called when the thread ends after a sucessful
213 *****************************************************************************/
214 static void EndThread( vdec_thread_t *p_vdec )
216 intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
219 /*****************************************************************************
220 * AddBlock : add a block
221 *****************************************************************************/
223 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
224 yuv_data_t * p_data, int i_incr )
228 for( i_y = 0; i_y < 8; i_y++ )
230 for( i_x = 0; i_x < 8; i_x++ )
232 *p_data = p_vdec->pi_crop[*p_data + *p_block++];
239 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
240 yuv_data_t * p_data, int i_incr )
243 "pxor %%mm7,%%mm7\n\t"
245 "movq (%0),%%mm1\n\t"
246 "movq %%mm1,%%mm2\n\t"
247 "punpckhbw %%mm7,%%mm1\n\t"
248 "punpcklbw %%mm7,%%mm2\n\t"
249 "paddw (%1),%%mm2\n\t"
250 "paddw 8(%1),%%mm1\n\t"
251 "packuswb %%mm1,%%mm2\n\t"
252 "movq %%mm2,(%0)\n\t"
255 "movq (%0),%%mm1\n\t"
256 "movq %%mm1,%%mm2\n\t"
257 "punpckhbw %%mm7,%%mm1\n\t"
258 "punpcklbw %%mm7,%%mm2\n\t"
259 "paddw 16(%1),%%mm2\n\t"
260 "paddw 24(%1),%%mm1\n\t"
261 "packuswb %%mm1,%%mm2\n\t"
262 "movq %%mm2,(%0)\n\t"
265 "movq (%0),%%mm1\n\t"
266 "movq %%mm1,%%mm2\n\t"
267 "punpckhbw %%mm7,%%mm1\n\t"
268 "punpcklbw %%mm7,%%mm2\n\t"
269 "paddw 32(%1),%%mm2\n\t"
270 "paddw 40(%1),%%mm1\n\t"
271 "packuswb %%mm1,%%mm2\n\t"
272 "movq %%mm2,(%0)\n\t"
275 "movq (%0),%%mm1\n\t"
276 "movq %%mm1,%%mm2\n\t"
277 "punpckhbw %%mm7,%%mm1\n\t"
278 "punpcklbw %%mm7,%%mm2\n\t"
279 "paddw 48(%1),%%mm2\n\t"
280 "paddw 56(%1),%%mm1\n\t"
281 "packuswb %%mm1,%%mm2\n\t"
282 "movq %%mm2,(%0)\n\t"
285 "movq (%0),%%mm1\n\t"
286 "movq %%mm1,%%mm2\n\t"
287 "punpckhbw %%mm7,%%mm1\n\t"
288 "punpcklbw %%mm7,%%mm2\n\t"
289 "paddw 64(%1),%%mm2\n\t"
290 "paddw 72(%1),%%mm1\n\t"
291 "packuswb %%mm1,%%mm2\n\t"
292 "movq %%mm2,(%0)\n\t"
295 "movq (%0),%%mm1\n\t"
296 "movq %%mm1,%%mm2\n\t"
297 "punpckhbw %%mm7,%%mm1\n\t"
298 "punpcklbw %%mm7,%%mm2\n\t"
299 "paddw 80(%1),%%mm2\n\t"
300 "paddw 88(%1),%%mm1\n\t"
301 "packuswb %%mm1,%%mm2\n\t"
302 "movq %%mm2,(%0)\n\t"
305 "movq (%0),%%mm1\n\t"
306 "movq %%mm1,%%mm2\n\t"
307 "punpckhbw %%mm7,%%mm1\n\t"
308 "punpcklbw %%mm7,%%mm2\n\t"
309 "paddw 96(%1),%%mm2\n\t"
310 "paddw 104(%1),%%mm1\n\t"
311 "packuswb %%mm1,%%mm2\n\t"
312 "movq %%mm2,(%0)\n\t"
315 "movq (%0),%%mm1\n\t"
316 "movq %%mm1,%%mm2\n\t"
317 "punpckhbw %%mm7,%%mm1\n\t"
318 "punpcklbw %%mm7,%%mm2\n\t"
319 "paddw 112(%1),%%mm2\n\t"
320 "paddw 120(%1),%%mm1\n\t"
321 "packuswb %%mm1,%%mm2\n\t"
322 "movq %%mm2,(%0)\n\t"
325 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
330 /*****************************************************************************
331 * CopyBlock : copy a block
332 *****************************************************************************/
334 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
335 yuv_data_t * p_data, int i_incr )
339 for( i_y = 0; i_y < 8; i_y++ )
341 for( i_x = 0; i_x < 8; i_x++ )
343 *p_data++ = p_vdec->pi_crop[*p_block++];
349 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
350 yuv_data_t * p_data, int i_incr )
353 "movq (%1),%%mm0\n\t"
354 "packuswb 8(%1),%%mm0\n\t"
355 "movq %%mm0,(%0)\n\t"
358 "movq 16(%1),%%mm0\n\t"
359 "packuswb 24(%1),%%mm0\n\t"
360 "movq %%mm0,(%0)\n\t"
363 "movq 32(%1),%%mm0\n\t"
364 "packuswb 40(%1),%%mm0\n\t"
365 "movq %%mm0,(%0)\n\t"
368 "movq 48(%1),%%mm0\n\t"
369 "packuswb 56(%1),%%mm0\n\t"
370 "movq %%mm0,(%0)\n\t"
373 "movq 64(%1),%%mm0\n\t"
374 "packuswb 72(%1),%%mm0\n\t"
375 "movq %%mm0,(%0)\n\t"
378 "movq 80(%1),%%mm0\n\t"
379 "packuswb 88(%1),%%mm0\n\t"
380 "movq %%mm0,(%0)\n\t"
383 "movq 96(%1),%%mm0\n\t"
384 "packuswb 104(%1),%%mm0\n\t"
385 "movq %%mm0,(%0)\n\t"
388 "movq 112(%1),%%mm0\n\t"
389 "packuswb 120(%1),%%mm0\n\t"
390 "movq %%mm0,(%0)\n\t"
392 :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
397 /*****************************************************************************
398 * vdec_DecodeMacroblock : decode a macroblock of a picture
399 *****************************************************************************/
400 #define DECODEBLOCKS( OPBLOCK ) \
404 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
407 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
409 if( p_mb->i_coded_block_pattern & i_mask ) \
412 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
414 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
415 p_mb->pi_sparse_pos[i_b] ); \
418 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
421 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
422 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
427 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; \
428 i_b++, i_mask >>= 1 ) \
430 if( p_mb->i_coded_block_pattern & i_mask ) \
433 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
435 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
436 p_mb->pi_sparse_pos[i_b] ); \
439 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
442 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
443 p_mb->p_data[i_b], p_mb->i_addb_c_stride ); \
449 static __inline__ void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
451 void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
454 if( !(p_mb->i_mb_type & MB_INTRA) )
457 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
459 if( p_mb->pf_motion == 0 )
461 intf_ErrMsg( "vdec error: pf_motion set to NULL\n" );
465 p_mb->pf_motion( p_mb );
468 DECODEBLOCKS( AddBlock )
472 DECODEBLOCKS( CopyBlock )
476 * Decoding is finished, release the macroblock and free
479 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
483 /*****************************************************************************
484 * RunThread: video decoder thread
485 *****************************************************************************
486 * Video decoder thread. This function does only return when the thread is
488 *****************************************************************************/
489 static void RunThread( vdec_thread_t *p_vdec )
491 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
495 * Initialize thread and free configuration
497 p_vdec->b_error = vdec_InitThread( p_vdec );
498 if( p_vdec->b_error )
505 * Main loop - it is not executed if an error occured during
508 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
512 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
514 vdec_DecodeMacroblock( p_vdec, p_mb );
521 if( p_vdec->b_error )
523 ErrorThread( p_vdec );