]> git.sesse.net Git - vlc/blob - video_decoder.c
355ea210e7c21a8450b94c196bbfbe0d12a9d7a4
[vlc] / video_decoder.c
1 /*****************************************************************************
2  * video_decoder.c : video decoder thread
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: video_decoder.c,v 1.2 2001/11/28 15:08:05 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Michel Lespinasse <walken@zoy.org>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>                                              /* getpid() */
32 #endif
33
34 #include <stdlib.h>                                                /* free() */
35 #include <string.h>                                    /* memcpy(), memset() */
36 #include <errno.h>                                                  /* errno */
37
38 #include "config.h"
39 #include "common.h"
40 #include "intf_msg.h"
41 #include "threads.h"
42 #include "mtime.h"
43
44 #include "video.h"
45 #include "video_output.h"
46
47 #include "modules_export.h"
48 #include "stream_control.h"
49 #include "input_ext-dec.h"
50
51 #include "vdec_ext-plugins.h"
52 #include "video_decoder.h"
53 #include "vpar_pool.h"
54 #include "video_parser.h"
55
56 /*
57  * Local prototypes
58  */
59 static void     RunThread           ( vdec_thread_t *p_vdec );
60
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 )
68 {
69     vdec_thread_t *     p_vdec;
70
71     intf_DbgMsg("vdec debug: creating video decoder thread");
72
73     /* Allocate the memory needed to store the thread's structure */
74     if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
75     {
76         intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
77         return( NULL );
78     }
79
80     /*
81      * Initialize the thread properties
82      */
83     p_vdec->b_die = 0;
84
85     /*
86      * Initialize the parser properties
87      */
88     p_vdec->p_pool = p_pool;
89
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) )
93     {
94         intf_ErrMsg("vdec error: can't spawn video decoder thread");
95         free( p_vdec );
96         return( NULL );
97     }
98
99     intf_DbgMsg("vdec debug: video decoder thread (%p) created", p_vdec);
100     return( p_vdec );
101 }
102
103 /*****************************************************************************
104  * vdec_DestroyThread: destroy a video decoder thread
105  *****************************************************************************/
106 void vdec_DestroyThread( vdec_thread_t *p_vdec )
107 {
108     intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p", p_vdec);
109
110     /* Ask thread to kill itself */
111     p_vdec->b_die = 1;
112
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 );
117
118     /* Waiting for the decoder thread to exit */
119     vlc_thread_join( p_vdec->thread_id );
120 }
121
122 /* following functions are local */
123
124 /*****************************************************************************
125  * vdec_InitThread: initialize video decoder thread
126  *****************************************************************************
127  * This function is called from RunThread and performs the second step of the
128  * initialization.
129  *****************************************************************************/
130 void vdec_InitThread( vdec_thread_t * p_vdec )
131 {
132     intf_DbgMsg("vdec debug: initializing video decoder thread %p", p_vdec);
133
134 #if !defined(SYS_BEOS)
135 #   if VDEC_NICE
136     /* Re-nice ourself - otherwise we would steal CPU time from the video
137      * output, which would make a poor display. */
138 #       if !defined(WIN32)
139     if( nice(VDEC_NICE) == -1 )
140 #       else
141     if( !SetThreadPriority( GetCurrentThread(),
142                             THREAD_PRIORITY_BELOW_NORMAL ) )
143 #       endif
144     {
145         intf_WarnMsg( 2, "vpar warning : couldn't nice() (%s)",
146                       strerror(errno) );
147     }
148 #   endif
149 #endif
150
151     p_vdec->p_idct_data = NULL;
152
153     p_vdec->p_pool->pf_idct_init( &p_vdec->p_idct_data );
154
155     /* Mark thread as running and return */
156     intf_DbgMsg("vdec debug: InitThread(%p) succeeded", p_vdec);
157 }
158
159 /*****************************************************************************
160  * vdec_EndThread: thread destruction
161  *****************************************************************************
162  * This function is called when the thread ends after a sucessful
163  * initialization.
164  *****************************************************************************/
165 void vdec_EndThread( vdec_thread_t * p_vdec )
166 {
167     intf_DbgMsg("vdec debug: EndThread(%p)", p_vdec);
168
169     if( p_vdec->p_idct_data != NULL )
170     {
171         free( p_vdec->p_idct_data );
172     }
173
174     free( p_vdec );
175 }
176
177 /*****************************************************************************
178  * MotionBlock: does one component of the motion compensation
179  *****************************************************************************/
180 static __inline__ void MotionBlock( vdec_pool_t * p_pool,
181                                     boolean_t b_average,
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 )
188 {
189     int             i_xy_half;
190     yuv_data_t *    p_src1;
191     yuv_data_t *    p_src2;
192
193     i_xy_half = ((i_y_pred & 1) << 1) | (i_x_pred & 1);
194
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);
198
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 );
202
203     if( i_chroma_format != CHROMA_NONE )
204     {
205         /* Expanded at compile-time. */
206         if( i_chroma_format != CHROMA_444 )
207         {
208             i_x_pred /= 2;
209             i_stride >>= 1;
210             i_src_offset >>= 1;
211             i_dest_offset >>= 1;
212         }
213         if( i_chroma_format == CHROMA_420 )
214         {
215             i_y_pred /= 2;
216             i_height >>= 1;
217         }
218
219         i_xy_half = ((i_y_pred & 1) << 1) | (i_x_pred & 1);
220
221         i_src_offset += b_second_half * (i_stride << 3);
222         i_dest_offset += b_second_half * (i_stride << 3);
223
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;
228
229         p_pool->ppppf_motion[b_average][(i_chroma_format != CHROMA_444)]
230                             [i_xy_half]
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)]
233                             [i_xy_half]
234                 ( pp_dest[2] + i_dest_offset, p_src2, i_stride, i_height );
235     }
236 }
237
238
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 ); 
250
251 #define DECODE_NONINTRA_BLOCK( i_b, p_dest, I_CHROMA )                      \
252     if( p_mb->i_coded_block_pattern & (1 << (11 - (i_b))) )                 \
253     {                                                                       \
254         DECODE_INTRA_BLOCK( i_b, p_dest, I_CHROMA );                        \
255     }
256     
257 #define DECLARE_DECODEMB( PSZ_NAME, I_CHROMA )                              \
258 void PSZ_NAME ( vdec_thread_t *p_vdec, macroblock_t * p_mb )                \
259 {                                                                           \
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;                                \
267                                                                             \
268     if( p_mb->i_mb_modes & DCT_TYPE_INTERLACED )                            \
269     {                                                                       \
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 )                                        \
273         {                                                                   \
274             i_chrom_dct_offset = p_vpar->picture.i_field_width >> 1;        \
275             i_chrom_dct_stride = p_vpar->picture.i_field_width;             \
276         }                                                                   \
277         else if( I_CHROMA == CHROMA_444 )                                   \
278         {                                                                   \
279             i_chrom_dct_offset = p_vpar->picture.i_field_width;             \
280             i_chrom_dct_stride = p_vpar->picture.i_field_width * 2;         \
281         }                                                                   \
282     }                                                                       \
283     else                                                                    \
284     {                                                                       \
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 )                                        \
288         {                                                                   \
289             i_chrom_dct_offset = p_vpar->picture.i_field_width * 4;         \
290             i_chrom_dct_stride = p_vpar->picture.i_field_width >> 1;        \
291         }                                                                   \
292         else if( I_CHROMA == CHROMA_444 )                                   \
293         {                                                                   \
294             i_chrom_dct_offset = p_vpar->picture.i_field_width * 8;         \
295             i_chrom_dct_stride = p_vpar->picture.i_field_width;             \
296         }                                                                   \
297     }                                                                       \
298                                                                             \
299     if( !(p_mb->i_mb_modes & MB_INTRA) )                                    \
300     {                                                                       \
301         /*                                                                  \
302          * Motion Compensation (ISO/IEC 13818-2 section 7.6)                \
303          */                                                                 \
304         for( i = 0; i < p_mb->i_nb_motions; i++ )                           \
305         {                                                                   \
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 );               \
313         }                                                                   \
314                                                                             \
315         /*                                                                  \
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)                                           \
319          */                                                                 \
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,        \
323                                I_CHROMA );                                  \
324         DECODE_NONINTRA_BLOCK( 3, p_mb->p_y_data + i_lum_dct_offset + 8,    \
325                                I_CHROMA );                                  \
326         if( I_CHROMA != CHROMA_NONE )                                       \
327         {                                                                   \
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 )                                    \
331             {                                                               \
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 )                                \
337                 {                                                           \
338                     DECODE_NONINTRA_BLOCK( 8, p_mb->p_u_data + 8,           \
339                                            I_CHROMA );                      \
340                     DECODE_NONINTRA_BLOCK( 9, p_mb->p_v_data + 8,           \
341                                            I_CHROMA );                      \
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 );\
346                 }                                                           \
347             }                                                               \
348         }                                                                   \
349     }                                                                       \
350     else                                                                    \
351     {                                                                       \
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,           \
356                             I_CHROMA );                                     \
357         DECODE_INTRA_BLOCK( 3, p_mb->p_y_data + i_lum_dct_offset + 8,       \
358                             I_CHROMA );                                     \
359         if( I_CHROMA != CHROMA_NONE )                                       \
360         {                                                                   \
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 )                                    \
364             {                                                               \
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 )                                \
370                 {                                                           \
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 );\
377                 }                                                           \
378             }                                                               \
379         }                                                                   \
380     }                                                                       \
381 }
382
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 );
387
388 #undef DECLARE_DECODEMB
389
390 /*****************************************************************************
391  * RunThread: video decoder thread
392  *****************************************************************************
393  * Video decoder thread. This function does only return when the thread is
394  * terminated.
395  *****************************************************************************/
396 static void RunThread( vdec_thread_t *p_vdec )
397 {
398     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
399                 p_vdec, getpid());
400
401     vdec_InitThread( p_vdec );
402
403     /*
404      * Main loop
405      */
406     while( !p_vdec->b_die )
407     {
408         macroblock_t *          p_mb;
409
410         if( (p_mb = vpar_GetMacroblock( p_vdec->p_pool, &p_vdec->b_die )) != NULL )
411         {
412             p_vdec->p_pool->pf_vdec_decode( p_vdec, p_mb );
413
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 );
417         }
418     }
419
420     /* End of thread */
421     vdec_EndThread( p_vdec );
422 }
423