1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_decoder.c,v 1.1 2001/11/13 12:09:18 henri 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 "video_output.h"
47 #include "modules_export.h"
48 #include "stream_control.h"
49 #include "input_ext-dec.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_idct_init( &p_vdec->p_idct_data );
155 /* Mark thread as running and return */
156 intf_DbgMsg("vdec debug: InitThread(%p) succeeded", p_vdec);
159 /*****************************************************************************
160 * vdec_EndThread: thread destruction
161 *****************************************************************************
162 * This function is called when the thread ends after a sucessful
164 *****************************************************************************/
165 void vdec_EndThread( vdec_thread_t * p_vdec )
167 intf_DbgMsg("vdec debug: EndThread(%p)", p_vdec);
169 if( p_vdec->p_idct_data != NULL )
171 free( p_vdec->p_idct_data );
177 /*****************************************************************************
178 * MotionBlock: does one component of the motion compensation
179 *****************************************************************************/
180 static __inline__ void MotionBlock( vdec_pool_t * p_pool,
182 int i_x_pred, int i_y_pred,
183 yuv_data_t * pp_dest[3], int i_dest_offset,
184 yuv_data_t * pp_src[3], int i_src_offset,
185 int i_stride, int i_height,
186 boolean_t b_second_half,
187 int i_chroma_format )
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 );
203 if( i_chroma_format != CHROMA_NONE )
205 /* Expanded at compile-time. */
206 if( i_chroma_format != CHROMA_444 )
213 if( i_chroma_format == CHROMA_420 )
219 i_xy_half = ((i_y_pred & 1) << 1) | (i_x_pred & 1);
221 i_src_offset += b_second_half * (i_stride << 3);
222 i_dest_offset += b_second_half * (i_stride << 3);
224 p_src1 = pp_src[1] + i_src_offset
225 + (i_x_pred >> 1) + (i_y_pred >> 1) * i_stride;
226 p_src2 = pp_src[2] + i_src_offset
227 + (i_x_pred >> 1) + (i_y_pred >> 1) * i_stride;
229 p_pool->ppppf_motion[b_average][(i_chroma_format != CHROMA_444)]
231 ( pp_dest[1] + i_dest_offset, p_src1, i_stride, i_height );
232 p_pool->ppppf_motion[b_average][(i_chroma_format != CHROMA_444)]
234 ( pp_dest[2] + i_dest_offset, p_src2, i_stride, i_height );
239 /*****************************************************************************
240 * DecodeMacroblock: decode a macroblock
241 *****************************************************************************/
242 #define DECODE_INTRA_BLOCK( i_b, p_dest, I_CHROMA ) \
243 p_idct = &p_mb->p_idcts[i_b]; \
244 p_idct->pf_idct( p_idct->pi_block, p_dest, \
245 i_b < 4 ? i_lum_dct_stride : \
246 I_CHROMA == CHROMA_420 ? \
247 p_vpar->picture.i_field_width >> 1 : \
248 i_chrom_dct_stride, \
249 p_vdec->p_idct_data, p_idct->i_sparse_pos );
251 #define DECODE_NONINTRA_BLOCK( i_b, p_dest, I_CHROMA ) \
252 if( p_mb->i_coded_block_pattern & (1 << (11 - (i_b))) ) \
254 DECODE_INTRA_BLOCK( i_b, p_dest, I_CHROMA ); \
257 #define DECLARE_DECODEMB( PSZ_NAME, I_CHROMA ) \
258 void PSZ_NAME ( vdec_thread_t *p_vdec, macroblock_t * p_mb ) \
260 int i, i_lum_dct_offset, i_lum_dct_stride; \
261 /* This is to keep the compiler happy with CHROMA_420 and CHROMA_NONE */\
262 int i_chrom_dct_offset __attribute__((unused)); \
263 int i_chrom_dct_stride __attribute__((unused)); \
264 idct_inner_t * p_idct; \
265 vdec_pool_t * p_pool = p_vdec->p_pool; \
266 vpar_thread_t * p_vpar = p_pool->p_vpar; \
268 if( p_mb->i_mb_modes & DCT_TYPE_INTERLACED ) \
270 i_lum_dct_offset = p_vpar->picture.i_field_width; \
271 i_lum_dct_stride = p_vpar->picture.i_field_width * 2; \
272 if( I_CHROMA == CHROMA_422 ) \
274 i_chrom_dct_offset = p_vpar->picture.i_field_width >> 1; \
275 i_chrom_dct_stride = p_vpar->picture.i_field_width; \
277 else if( I_CHROMA == CHROMA_444 ) \
279 i_chrom_dct_offset = p_vpar->picture.i_field_width; \
280 i_chrom_dct_stride = p_vpar->picture.i_field_width * 2; \
285 i_lum_dct_offset = p_vpar->picture.i_field_width * 8; \
286 i_lum_dct_stride = p_vpar->picture.i_field_width; \
287 if( I_CHROMA == CHROMA_422 ) \
289 i_chrom_dct_offset = p_vpar->picture.i_field_width * 4; \
290 i_chrom_dct_stride = p_vpar->picture.i_field_width >> 1; \
292 else if( I_CHROMA == CHROMA_444 ) \
294 i_chrom_dct_offset = p_vpar->picture.i_field_width * 8; \
295 i_chrom_dct_stride = p_vpar->picture.i_field_width; \
299 if( !(p_mb->i_mb_modes & MB_INTRA) ) \
302 * Motion Compensation (ISO/IEC 13818-2 section 7.6) \
304 for( i = 0; i < p_mb->i_nb_motions; i++ ) \
306 motion_inner_t * p_motion = &p_mb->p_motions[i]; \
307 MotionBlock( p_pool, p_motion->b_average, \
308 p_motion->i_x_pred, p_motion->i_y_pred, \
309 p_mb->pp_dest, p_motion->i_dest_offset, \
310 p_motion->pp_source, p_motion->i_src_offset, \
311 p_motion->i_stride, p_motion->i_height, \
312 p_motion->b_second_half, I_CHROMA ); \
316 * Inverse DCT (ISO/IEC 13818-2 section Annex A) and \
317 * adding prediction and coefficient data (ISO/IEC \
318 * 13818-2 section 7.6.8) \
320 DECODE_NONINTRA_BLOCK( 0, p_mb->p_y_data, I_CHROMA ); \
321 DECODE_NONINTRA_BLOCK( 1, p_mb->p_y_data + 8, I_CHROMA ); \
322 DECODE_NONINTRA_BLOCK( 2, p_mb->p_y_data + i_lum_dct_offset, \
324 DECODE_NONINTRA_BLOCK( 3, p_mb->p_y_data + i_lum_dct_offset + 8, \
326 if( I_CHROMA != CHROMA_NONE ) \
328 DECODE_NONINTRA_BLOCK( 4, p_mb->p_u_data, I_CHROMA ); \
329 DECODE_NONINTRA_BLOCK( 5, p_mb->p_v_data, I_CHROMA ); \
330 if( I_CHROMA != CHROMA_420 ) \
332 DECODE_NONINTRA_BLOCK( 6, p_mb->p_u_data \
333 + i_chrom_dct_offset, I_CHROMA );\
334 DECODE_NONINTRA_BLOCK( 7, p_mb->p_v_data \
335 + i_chrom_dct_offset, I_CHROMA );\
336 if( I_CHROMA == CHROMA_444 ) \
338 DECODE_NONINTRA_BLOCK( 8, p_mb->p_u_data + 8, \
340 DECODE_NONINTRA_BLOCK( 9, p_mb->p_v_data + 8, \
342 DECODE_NONINTRA_BLOCK( 10, p_mb->p_u_data + 8 \
343 + i_chrom_dct_offset, I_CHROMA );\
344 DECODE_NONINTRA_BLOCK( 11, p_mb->p_v_data + 8 \
345 + i_chrom_dct_offset, I_CHROMA );\
352 /* Intra macroblock */ \
353 DECODE_INTRA_BLOCK( 0, p_mb->p_y_data, I_CHROMA ); \
354 DECODE_INTRA_BLOCK( 1, p_mb->p_y_data + 8, I_CHROMA ); \
355 DECODE_INTRA_BLOCK( 2, p_mb->p_y_data + i_lum_dct_offset, \
357 DECODE_INTRA_BLOCK( 3, p_mb->p_y_data + i_lum_dct_offset + 8, \
359 if( I_CHROMA != CHROMA_NONE ) \
361 DECODE_INTRA_BLOCK( 4, p_mb->p_u_data, I_CHROMA ); \
362 DECODE_INTRA_BLOCK( 5, p_mb->p_v_data, I_CHROMA ); \
363 if( I_CHROMA != CHROMA_420 ) \
365 DECODE_INTRA_BLOCK( 6, p_mb->p_u_data \
366 + i_chrom_dct_offset, I_CHROMA ); \
367 DECODE_INTRA_BLOCK( 7, p_mb->p_v_data \
368 + i_chrom_dct_offset, I_CHROMA ); \
369 if( I_CHROMA == CHROMA_444 ) \
371 DECODE_INTRA_BLOCK( 8, p_mb->p_u_data + 8, I_CHROMA ); \
372 DECODE_INTRA_BLOCK( 9, p_mb->p_v_data + 8, I_CHROMA ); \
373 DECODE_INTRA_BLOCK( 10, p_mb->p_u_data + 8 \
374 + i_chrom_dct_offset, I_CHROMA );\
375 DECODE_INTRA_BLOCK( 11, p_mb->p_v_data + 8 \
376 + i_chrom_dct_offset, I_CHROMA );\
383 DECLARE_DECODEMB( vdec_DecodeMacroblockBW, CHROMA_NONE );
384 DECLARE_DECODEMB( vdec_DecodeMacroblock420, CHROMA_420 );
385 DECLARE_DECODEMB( vdec_DecodeMacroblock422, CHROMA_422 );
386 DECLARE_DECODEMB( vdec_DecodeMacroblock444, CHROMA_444 );
388 #undef DECLARE_DECODEMB
390 /*****************************************************************************
391 * RunThread: video decoder thread
392 *****************************************************************************
393 * Video decoder thread. This function does only return when the thread is
395 *****************************************************************************/
396 static void RunThread( vdec_thread_t *p_vdec )
398 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
401 vdec_InitThread( p_vdec );
406 while( !p_vdec->b_die )
410 if( (p_mb = vpar_GetMacroblock( p_vdec->p_pool, &p_vdec->b_die )) != NULL )
412 p_vdec->p_pool->pf_vdec_decode( p_vdec, p_mb );
414 /* Decoding is finished, release the macroblock and free
415 * unneeded memory. */
416 p_vdec->p_pool->pf_free_mb( p_vdec->p_pool, p_mb );
421 vdec_EndThread( p_vdec );