1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_decoder.c,v 1.48 2001/04/06 09:15:48 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 <unistd.h> /* getpid() */
32 #include <stdlib.h> /* free() */
33 #include <string.h> /* memcpy(), memset() */
34 #include <errno.h> /* errno */
43 #include "stream_control.h"
44 #include "input_ext-dec.h"
47 #include "video_output.h"
49 #include "vdec_motion.h"
50 #include "vdec_idct.h"
51 #include "video_decoder.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 (%2),%%mm2\n\t"
253 "paddw 8(%2),%%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(%2),%%mm2\n\t"
263 "paddw 24(%2),%%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(%2),%%mm2\n\t"
273 "paddw 40(%2),%%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(%2),%%mm2\n\t"
283 "paddw 56(%2),%%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(%2),%%mm2\n\t"
293 "paddw 72(%2),%%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(%2),%%mm2\n\t"
303 "paddw 88(%2),%%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(%2),%%mm2\n\t"
313 "paddw 104(%2),%%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(%2),%%mm2\n\t"
323 "paddw 120(%2),%%mm1\n\t"
324 "packuswb %%mm1,%%mm2\n\t"
325 "movq %%mm2,(%0)\n\t"
329 : "0" (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 (%2),%%mm0\n\t"
358 "packuswb 8(%2),%%mm0\n\t"
359 "movq %%mm0,(%0)\n\t"
362 "movq 16(%2),%%mm0\n\t"
363 "packuswb 24(%2),%%mm0\n\t"
364 "movq %%mm0,(%0)\n\t"
367 "movq 32(%2),%%mm0\n\t"
368 "packuswb 40(%2),%%mm0\n\t"
369 "movq %%mm0,(%0)\n\t"
372 "movq 48(%2),%%mm0\n\t"
373 "packuswb 56(%2),%%mm0\n\t"
374 "movq %%mm0,(%0)\n\t"
377 "movq 64(%2),%%mm0\n\t"
378 "packuswb 72(%2),%%mm0\n\t"
379 "movq %%mm0,(%0)\n\t"
382 "movq 80(%2),%%mm0\n\t"
383 "packuswb 88(%2),%%mm0\n\t"
384 "movq %%mm0,(%0)\n\t"
387 "movq 96(%2),%%mm0\n\t"
388 "packuswb 104(%2),%%mm0\n\t"
389 "movq %%mm0,(%0)\n\t"
392 "movq 112(%2),%%mm0\n\t"
393 "packuswb 120(%2),%%mm0\n\t"
394 "movq %%mm0,(%0)\n\t"
398 : "0" (p_data), "r" (p_block), "r" (i_incr + 8) );
403 /*****************************************************************************
404 * vdec_DecodeMacroblock : decode a macroblock of a picture
405 *****************************************************************************/
406 #define DECODEBLOCKSC( OPBLOCK ) \
410 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
413 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
415 if( p_mb->i_coded_block_pattern & i_mask ) \
418 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
420 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
421 p_mb->pi_sparse_pos[i_b] ); \
424 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
427 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
428 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
433 for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks; \
434 i_b++, i_mask >>= 1 ) \
436 if( p_mb->i_coded_block_pattern & i_mask ) \
439 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
441 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
442 p_mb->pi_sparse_pos[i_b] ); \
445 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
448 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
449 p_mb->p_data[i_b], p_mb->i_addb_c_stride ); \
454 #define DECODEBLOCKSBW( OPBLOCK ) \
458 i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks); \
461 for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 ) \
463 if( p_mb->i_coded_block_pattern & i_mask ) \
466 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
468 (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b], \
469 p_mb->pi_sparse_pos[i_b] ); \
472 * Adding prediction and coefficient data (ISO/IEC 13818-2 \
475 OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b], \
476 p_mb->p_data[i_b], p_mb->i_addb_l_stride ); \
481 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_WarnMsg( 2, "pf_motion set to NULL" );
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 );
511 void vdec_DecodeMacroblockBW ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
513 if( !(p_mb->i_mb_type & MB_INTRA) )
516 * Motion Compensation (ISO/IEC 13818-2 section 7.6)
518 if( p_mb->pf_motion == 0 )
520 intf_WarnMsg( 2, "pf_motion set to NULL" );
524 p_mb->pf_motion( p_mb );
527 DECODEBLOCKSBW( AddBlock )
531 DECODEBLOCKSBW( CopyBlock )
535 * Decoding is finished, release the macroblock and free
538 vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
543 /*****************************************************************************
544 * RunThread: video decoder thread
545 *****************************************************************************
546 * Video decoder thread. This function does only return when the thread is
548 *****************************************************************************/
549 static void RunThread( vdec_thread_t *p_vdec )
551 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
555 * Initialize thread and free configuration
557 p_vdec->b_error = vdec_InitThread( p_vdec );
558 if( p_vdec->b_error )
565 * Main loop - it is not executed if an error occured during
568 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
572 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
574 vdec_DecodeMacroblockC ( p_vdec, p_mb );
581 if( p_vdec->b_error )
583 ErrorThread( p_vdec );