* Applied patch from Jon Lech Johansen <jon-vl@nanocrew.net> to compile
[vlc.git] / src / video_decoder / video_decoder.c
blobb30440ecd86235270b24214d9e6d44b7db5f2642
1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_decoder.c,v 1.51 2001/05/31 01:37:08 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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #include "defs.h"
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h> /* getpid() */
32 #endif
34 #include <stdlib.h> /* free() */
35 #include <string.h> /* memcpy(), memset() */
36 #include <errno.h> /* errno */
38 #include "config.h"
39 #include "common.h"
40 #include "threads.h"
41 #include "mtime.h"
43 #include "intf_msg.h"
45 #include "stream_control.h"
46 #include "input_ext-dec.h"
48 #include "video.h"
49 #include "video_output.h"
51 #include "vdec_motion.h"
52 #include "video_decoder.h"
54 #include "vpar_blocks.h"
55 #include "vpar_headers.h"
56 #include "vpar_synchro.h"
57 #include "video_parser.h"
58 #include "video_fifo.h"
61 * Local prototypes
63 #ifdef VDEC_SMP
64 static int vdec_InitThread ( vdec_thread_t *p_vdec );
65 #endif
66 static void RunThread ( vdec_thread_t *p_vdec );
67 static void ErrorThread ( vdec_thread_t *p_vdec );
68 static void EndThread ( vdec_thread_t *p_vdec );
70 /*****************************************************************************
71 * vdec_CreateThread: create a video decoder thread
72 *****************************************************************************
73 * This function creates a new video decoder thread, and returns a pointer
74 * to its description. On error, it returns NULL.
75 * Following configuration properties are used:
76 * XXX??
77 *****************************************************************************/
78 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
80 vdec_thread_t * p_vdec;
82 intf_DbgMsg("vdec debug: creating video decoder thread");
84 /* Allocate the memory needed to store the thread's structure */
85 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
87 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
88 return( NULL );
92 * Initialize the thread properties
94 p_vdec->b_die = 0;
95 p_vdec->b_error = 0;
98 * Initialize the parser properties
100 p_vdec->p_vpar = p_vpar;
102 /* Spawn the video decoder thread */
103 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
104 (vlc_thread_func_t)RunThread, (void *)p_vdec) )
106 intf_ErrMsg("vdec error: can't spawn video decoder thread");
107 free( p_vdec );
108 return( NULL );
111 intf_DbgMsg("vdec debug: video decoder thread (%p) created", p_vdec);
112 return( p_vdec );
115 /*****************************************************************************
116 * vdec_DestroyThread: destroy a video decoder thread
117 *****************************************************************************
118 * Destroy and terminate thread. This function will return 0 if the thread could
119 * be destroyed, and non 0 else. The last case probably means that the thread
120 * was still active, and another try may succeed.
121 *****************************************************************************/
122 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
124 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p", p_vdec);
126 /* Ask thread to kill itself */
127 p_vdec->b_die = 1;
129 #ifdef VDEC_SMP
130 /* Make sure the decoder thread leaves the vpar_GetMacroblock() function */
131 vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
132 vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
133 vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
134 #endif
136 /* Waiting for the decoder thread to exit */
137 /* Remove this as soon as the "status" flag is implemented */
138 vlc_thread_join( p_vdec->thread_id );
141 /* following functions are local */
143 /*****************************************************************************
144 * vdec_InitThread: initialize video decoder thread
145 *****************************************************************************
146 * This function is called from RunThread and performs the second step of the
147 * initialization. It returns 0 on success. Note that the thread's flag are not
148 * modified inside this function.
149 *****************************************************************************/
150 #ifdef VDEC_SMP
151 static int vdec_InitThread( vdec_thread_t *p_vdec )
152 #else
153 int vdec_InitThread( vdec_thread_t *p_vdec )
154 #endif
156 intf_DbgMsg("vdec debug: initializing video decoder thread %p", p_vdec);
158 p_vdec->pf_decode_init = p_vdec->p_vpar->pf_decode_init;
159 p_vdec->pf_decode_mb_c = p_vdec->p_vpar->pf_decode_mb_c;
160 p_vdec->pf_decode_mb_bw = p_vdec->p_vpar->pf_decode_mb_bw;
162 p_vdec->pf_decode_init( p_vdec );
164 #ifdef VDEC_SMP
165 /* Re-nice ourself */
166 if( nice(VDEC_NICE) == -1 )
168 intf_WarnMsg( 2, "vdec warning : couldn't nice() (%s)",
169 strerror(errno) );
171 #endif
173 /* Mark thread as running and return */
174 intf_DbgMsg("vdec debug: InitThread(%p) succeeded", p_vdec);
175 return( 0 );
178 /*****************************************************************************
179 * ErrorThread: RunThread() error loop
180 *****************************************************************************
181 * This function is called when an error occured during thread main's loop. The
182 * thread can still receive feed, but must be ready to terminate as soon as
183 * possible.
184 *****************************************************************************/
185 static void ErrorThread( vdec_thread_t *p_vdec )
187 macroblock_t * p_mb;
189 /* Wait until a `die' order */
190 while( !p_vdec->b_die )
192 p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
193 vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
197 /*****************************************************************************
198 * EndThread: thread destruction
199 *****************************************************************************
200 * This function is called when the thread ends after a sucessful
201 * initialization.
202 *****************************************************************************/
203 static void EndThread( vdec_thread_t *p_vdec )
205 intf_DbgMsg("vdec debug: EndThread(%p)", p_vdec);
208 /*****************************************************************************
209 * RunThread: video decoder thread
210 *****************************************************************************
211 * Video decoder thread. This function does only return when the thread is
212 * terminated.
213 *****************************************************************************/
214 static void RunThread( vdec_thread_t *p_vdec )
216 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
217 p_vdec, getpid());
220 * Initialize thread and free configuration
222 p_vdec->b_error = vdec_InitThread( p_vdec );
223 if( p_vdec->b_error )
225 return;
227 p_vdec->b_run = 1;
230 * Main loop - it is not executed if an error occured during
231 * initialization
233 while( (!p_vdec->b_die) && (!p_vdec->b_error) )
235 macroblock_t * p_mb;
237 if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
239 p_vdec->pf_decode_mb_c( p_vdec, p_mb );
244 * Error loop
246 if( p_vdec->b_error )
248 ErrorThread( p_vdec );
251 /* End of thread */
252 EndThread( p_vdec );
253 p_vdec->b_run = 0;