]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
* Altivec IDCT and motion compensation, based on Paul Mackerras's mpeg2dec
[vlc] / src / video_decoder / video_decoder.c
1 /*****************************************************************************
2  * video_decoder.c : video decoder thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: video_decoder.c,v 1.58 2001/09/05 16:07:50 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 "threads.h"
41 #include "mtime.h"
42
43 #include "intf_msg.h"
44
45 #include "stream_control.h"
46 #include "input_ext-dec.h"
47
48 #include "video.h"
49 #include "video_output.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, boolean_t b_color )
187 {
188     int             i_xy_half;
189     yuv_data_t *    p_src1;
190     yuv_data_t *    p_src2;
191
192     i_xy_half = ((i_y_pred & 1) << 1) | (i_x_pred & 1);
193
194     p_src1 = pp_src[0] + i_src_offset
195                 + (i_x_pred >> 1) + (i_y_pred >> 1) * i_stride
196                 + b_second_half * (i_stride << 3);
197
198     p_pool->ppppf_motion[b_average][0][i_xy_half]
199             ( pp_dest[0] + i_dest_offset + b_second_half * (i_stride << 3),
200               p_src1, i_stride, i_height );
201
202     if( b_color )
203     {
204         /* Expanded at compile-time. */
205         i_x_pred /= 2;
206         i_y_pred /= 2;
207
208         i_xy_half = ((i_y_pred & 1) << 1) | (i_x_pred & 1);
209         i_stride >>= 1;
210         i_height >>= 1;
211         i_src_offset >>= 1;
212         i_src_offset += b_second_half * (i_stride << 2);
213         i_dest_offset >>= 1;
214         i_dest_offset += b_second_half * (i_stride << 2);
215
216         p_src1 = pp_src[1] + i_src_offset
217                     + (i_x_pred >> 1) + (i_y_pred >> 1) * i_stride;
218         p_src2 = pp_src[2] + i_src_offset
219                     + (i_x_pred >> 1) + (i_y_pred >> 1) * i_stride;
220
221         p_pool->ppppf_motion[b_average][1][i_xy_half]
222                 ( pp_dest[1] + i_dest_offset, p_src1, i_stride, i_height );
223         p_pool->ppppf_motion[b_average][1][i_xy_half]
224                 ( pp_dest[2] + i_dest_offset, p_src2, i_stride, i_height );
225     }
226 }
227
228
229 /*****************************************************************************
230  * DecodeMacroblock: decode a macroblock
231  *****************************************************************************/
232 #define DECLARE_DECODEMB( PSZ_NAME, B_COLOR )                               \
233 void PSZ_NAME ( vdec_thread_t *p_vdec, macroblock_t * p_mb )                \
234 {                                                                           \
235     int             i;                                                      \
236     idct_inner_t *  p_idct;                                                 \
237     vdec_pool_t *   p_pool = p_vdec->p_pool;                                \
238                                                                             \
239     if( !(p_mb->i_mb_modes & MB_INTRA) )                                    \
240     {                                                                       \
241         /*                                                                  \
242          * Motion Compensation (ISO/IEC 13818-2 section 7.6)                \
243          */                                                                 \
244         for( i = 0; i < p_mb->i_nb_motions; i++ )                           \
245         {                                                                   \
246             motion_inner_t *    p_motion = &p_mb->p_motions[i];             \
247             MotionBlock( p_pool, p_motion->b_average,                       \
248                          p_motion->i_x_pred, p_motion->i_y_pred,            \
249                          p_mb->pp_dest, p_motion->i_dest_offset,            \
250                          p_motion->pp_source, p_motion->i_src_offset,       \
251                          p_motion->i_stride, p_motion->i_height,            \
252                          p_motion->b_second_half, B_COLOR );                \
253         }                                                                   \
254                                                                             \
255         for( i = 0, p_idct = p_mb->p_idcts; i < 4 + 2 * B_COLOR;            \
256              i++, p_idct++ )                                                \
257         {                                                                   \
258             if( p_mb->i_coded_block_pattern & (1 << (5 - i)) )              \
259             {                                                               \
260                 /*                                                          \
261                  * Inverse DCT (ISO/IEC 13818-2 section Annex A) and        \
262                  * adding prediction and coefficient data (ISO/IEC          \
263                  * 13818-2 section 7.6.8)                                   \
264                  */                                                         \
265                 p_idct->pf_idct( p_idct->pi_block, p_idct->p_dct_data,      \
266                                  i < 4 ? p_mb->i_lum_dct_stride :           \
267                                          p_mb->i_chrom_dct_stride,          \
268                                  p_vdec->p_idct_data,                       \
269                                  p_idct->i_sparse_pos );                    \
270             }                                                               \
271         }                                                                   \
272     }                                                                       \
273     else                                                                    \
274     {                                                                       \
275         /* Intra macroblock */                                              \
276         for( i = 0, p_idct = p_mb->p_idcts; i < 4 + 2 * B_COLOR;            \
277              i++, p_idct++ )                                                \
278         {                                                                   \
279             p_idct->pf_idct( p_idct->pi_block, p_idct->p_dct_data,          \
280                              i < 4 ? p_mb->i_lum_dct_stride :               \
281                                      p_mb->i_chrom_dct_stride,              \
282                              p_vdec->p_idct_data,                           \
283                              p_idct->i_sparse_pos );                        \
284         }                                                                   \
285     }                                                                       \
286 }
287
288 DECLARE_DECODEMB( vdec_DecodeMacroblockC, 1 );
289 DECLARE_DECODEMB( vdec_DecodeMacroblockBW, 0 );
290
291 #undef DECLARE_DECODEMB
292
293 /*****************************************************************************
294  * RunThread: video decoder thread
295  *****************************************************************************
296  * Video decoder thread. This function does only return when the thread is
297  * terminated.
298  *****************************************************************************/
299 static void RunThread( vdec_thread_t *p_vdec )
300 {
301     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
302                 p_vdec, getpid());
303
304     vdec_InitThread( p_vdec );
305
306     /*
307      * Main loop
308      */
309     while( !p_vdec->b_die )
310     {
311         macroblock_t *          p_mb;
312
313         if( (p_mb = vpar_GetMacroblock( p_vdec->p_pool, &p_vdec->b_die )) != NULL )
314         {
315             p_vdec->p_pool->pf_vdec_decode( p_vdec, p_mb );
316
317             /* Decoding is finished, release the macroblock and free
318              * unneeded memory. */
319             p_vdec->p_pool->pf_free_mb( p_vdec->p_pool, p_mb );
320         }
321     }
322
323     /* End of thread */
324     vdec_EndThread( p_vdec );
325 }
326