1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_decoder.c,v 1.57 2001/08/22 17:21:45 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Michel Lespinasse <walken@zoy.org>
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 *****************************************************************************/
31 #include <unistd.h> /* getpid() */
34 #include <stdlib.h> /* free() */
35 #include <string.h> /* memcpy(), memset() */
36 #include <errno.h> /* errno */
45 #include "stream_control.h"
46 #include "input_ext-dec.h"
49 #include "video_output.h"
51 #include "vdec_ext-plugins.h"
52 #include "video_decoder.h"
53 #include "vpar_pool.h"
54 #include "video_parser.h"
59 static void RunThread ( vdec_thread_t *p_vdec );
61 /*****************************************************************************
62 * vdec_CreateThread: create a video decoder thread
63 *****************************************************************************
64 * This function creates a new video decoder thread, and returns a pointer
65 * to its description. On error, it returns NULL.
66 *****************************************************************************/
67 vdec_thread_t * vdec_CreateThread( vdec_pool_t * p_pool )
69 vdec_thread_t * p_vdec;
71 intf_DbgMsg("vdec debug: creating video decoder thread");
73 /* Allocate the memory needed to store the thread's structure */
74 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
76 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
81 * Initialize the thread properties
86 * Initialize the parser properties
88 p_vdec->p_pool = p_pool;
90 /* Spawn the video decoder thread */
91 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
92 (vlc_thread_func_t)RunThread, (void *)p_vdec) )
94 intf_ErrMsg("vdec error: can't spawn video decoder thread");
99 intf_DbgMsg("vdec debug: video decoder thread (%p) created", p_vdec);
103 /*****************************************************************************
104 * vdec_DestroyThread: destroy a video decoder thread
105 *****************************************************************************/
106 void vdec_DestroyThread( vdec_thread_t *p_vdec )
108 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p", p_vdec);
110 /* Ask thread to kill itself */
113 /* Make sure the decoder thread leaves the vpar_GetMacroblock() function */
114 vlc_mutex_lock( &p_vdec->p_pool->lock );
115 vlc_cond_broadcast( &p_vdec->p_pool->wait_undecoded );
116 vlc_mutex_unlock( &p_vdec->p_pool->lock );
118 /* Waiting for the decoder thread to exit */
119 vlc_thread_join( p_vdec->thread_id );
122 /* following functions are local */
124 /*****************************************************************************
125 * vdec_InitThread: initialize video decoder thread
126 *****************************************************************************
127 * This function is called from RunThread and performs the second step of the
129 *****************************************************************************/
130 void vdec_InitThread( vdec_thread_t * p_vdec )
132 intf_DbgMsg("vdec debug: initializing video decoder thread %p", p_vdec);
134 #if !defined(SYS_BEOS)
136 /* Re-nice ourself - otherwise we would steal CPU time from the video
137 * output, which would make a poor display. */
139 if( nice(VDEC_NICE) == -1 )
141 if( !SetThreadPriority( GetCurrentThread(),
142 THREAD_PRIORITY_BELOW_NORMAL ) )
145 intf_WarnMsg( 2, "vpar warning : couldn't nice() (%s)",
151 p_vdec->p_idct_data = NULL;
153 p_vdec->p_pool->pf_decode_init( p_vdec );
154 p_vdec->p_pool->pf_idct_init( &p_vdec->p_idct_data );
156 /* Mark thread as running and return */
157 intf_DbgMsg("vdec debug: InitThread(%p) succeeded", p_vdec);
160 /*****************************************************************************
161 * vdec_EndThread: thread destruction
162 *****************************************************************************
163 * This function is called when the thread ends after a sucessful
165 *****************************************************************************/
166 void vdec_EndThread( vdec_thread_t * p_vdec )
168 intf_DbgMsg("vdec debug: EndThread(%p)", p_vdec);
170 if( p_vdec->p_idct_data != NULL )
172 free( p_vdec->p_idct_data );
178 /*****************************************************************************
179 * MotionBlock: does one component of the motion compensation
180 *****************************************************************************/
181 static __inline__ void MotionBlock( vdec_pool_t * p_pool,
183 int i_x_pred, int i_y_pred,
184 yuv_data_t * pp_dest[3], int i_dest_offset,
185 yuv_data_t * pp_src[3], int i_src_offset,
186 int i_stride, int i_height,
187 boolean_t b_second_half, boolean_t b_color )
193 i_xy_half = ((i_y_pred & 1) << 1) | (i_x_pred & 1);
195 p_src1 = pp_src[0] + i_src_offset
196 + (i_x_pred >> 1) + (i_y_pred >> 1) * i_stride
197 + b_second_half * (i_stride << 3);
199 p_pool->ppppf_motion[b_average][0][i_xy_half]
200 ( pp_dest[0] + i_dest_offset + b_second_half * (i_stride << 3),
201 p_src1, i_stride, i_height );
205 /* Expanded at compile-time. */
209 i_xy_half = ((i_y_pred & 1) << 1) | (i_x_pred & 1);
213 i_src_offset += b_second_half * (i_stride << 2);
215 i_dest_offset += b_second_half * (i_stride << 2);
217 p_src1 = pp_src[1] + i_src_offset
218 + (i_x_pred >> 1) + (i_y_pred >> 1) * i_stride;
219 p_src2 = pp_src[2] + i_src_offset
220 + (i_x_pred >> 1) + (i_y_pred >> 1) * i_stride;
222 p_pool->ppppf_motion[b_average][1][i_xy_half]
223 ( pp_dest[1] + i_dest_offset, p_src1, i_stride, i_height );
224 p_pool->ppppf_motion[b_average][1][i_xy_half]
225 ( pp_dest[2] + i_dest_offset, p_src2, i_stride, i_height );
230 /*****************************************************************************
231 * DecodeMacroblock: decode a macroblock
232 *****************************************************************************/
233 #define DECLARE_DECODEMB( PSZ_NAME, B_COLOR ) \
234 void PSZ_NAME ( vdec_thread_t *p_vdec, macroblock_t * p_mb ) \
237 idct_inner_t * p_idct; \
238 vdec_pool_t * p_pool = p_vdec->p_pool; \
240 if( !(p_mb->i_mb_modes & MB_INTRA) ) \
243 * Motion Compensation (ISO/IEC 13818-2 section 7.6) \
245 for( i = 0; i < p_mb->i_nb_motions; i++ ) \
247 motion_inner_t * p_motion = &p_mb->p_motions[i]; \
248 MotionBlock( p_pool, p_motion->b_average, \
249 p_motion->i_x_pred, p_motion->i_y_pred, \
250 p_mb->pp_dest, p_motion->i_dest_offset, \
251 p_motion->pp_source, p_motion->i_src_offset, \
252 p_motion->i_stride, p_motion->i_height, \
253 p_motion->b_second_half, B_COLOR ); \
256 for( i = 0, p_idct = p_mb->p_idcts; i < 4 + 2 * B_COLOR; \
259 if( p_mb->i_coded_block_pattern & (1 << (5 - i)) ) \
262 * Inverse DCT (ISO/IEC 13818-2 section Annex A) \
264 p_idct->pf_idct( p_vdec->p_idct_data, p_idct->pi_block, \
265 p_idct->i_sparse_pos ); \
268 * Adding prediction and coefficient data (ISO/IEC \
269 * 13818-2 section 7.6.8) \
271 p_pool->pf_addblock( p_idct->pi_block, p_idct->p_dct_data, \
272 i < 4 ? p_mb->i_lum_dct_stride : \
273 p_mb->i_chrom_dct_stride ); \
279 /* Intra macroblock */ \
280 for( i = 0, p_idct = p_mb->p_idcts; i < 4 + 2 * B_COLOR; \
283 p_idct->pf_idct( p_vdec->p_idct_data, p_idct->pi_block, \
284 p_idct->i_sparse_pos ); \
285 p_pool->pf_copyblock( p_idct->pi_block, p_idct->p_dct_data, \
286 i < 4 ? p_mb->i_lum_dct_stride : \
287 p_mb->i_chrom_dct_stride ); \
292 DECLARE_DECODEMB( vdec_DecodeMacroblockC, 1 );
293 DECLARE_DECODEMB( vdec_DecodeMacroblockBW, 0 );
295 #undef DECLARE_DECODEMB
297 /*****************************************************************************
298 * RunThread: video decoder thread
299 *****************************************************************************
300 * Video decoder thread. This function does only return when the thread is
302 *****************************************************************************/
303 static void RunThread( vdec_thread_t *p_vdec )
305 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
308 vdec_InitThread( p_vdec );
313 while( !p_vdec->b_die )
317 if( (p_mb = vpar_GetMacroblock( p_vdec->p_pool, &p_vdec->b_die )) != NULL )
319 p_vdec->p_pool->pf_vdec_decode( p_vdec, p_mb );
321 /* Decoding is finished, release the macroblock and free
322 * unneeded memory. */
323 p_vdec->p_pool->pf_free_mb( p_vdec->p_pool, p_mb );
328 vdec_EndThread( p_vdec );