1 /*****************************************************************************
2 * video_decoder.c : video decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_decoder.c,v 1.56 2001/07/31 21:13:30 gbazin 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 /*****************************************************************************
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"
58 static void RunThread ( vdec_thread_t *p_vdec );
60 /*****************************************************************************
61 * vdec_CreateThread: create a video decoder thread
62 *****************************************************************************
63 * This function creates a new video decoder thread, and returns a pointer
64 * to its description. On error, it returns NULL.
65 *****************************************************************************/
66 vdec_thread_t * vdec_CreateThread( vdec_pool_t * p_pool )
68 vdec_thread_t * p_vdec;
70 intf_DbgMsg("vdec debug: creating video decoder thread");
72 /* Allocate the memory needed to store the thread's structure */
73 if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
75 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
80 * Initialize the thread properties
85 * Initialize the parser properties
87 p_vdec->p_pool = p_pool;
89 /* Spawn the video decoder thread */
90 if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
91 (vlc_thread_func_t)RunThread, (void *)p_vdec) )
93 intf_ErrMsg("vdec error: can't spawn video decoder thread");
98 intf_DbgMsg("vdec debug: video decoder thread (%p) created", p_vdec);
102 /*****************************************************************************
103 * vdec_DestroyThread: destroy a video decoder thread
104 *****************************************************************************/
105 void vdec_DestroyThread( vdec_thread_t *p_vdec )
107 intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p", p_vdec);
109 /* Ask thread to kill itself */
112 /* Make sure the decoder thread leaves the vpar_GetMacroblock() function */
113 vlc_mutex_lock( &p_vdec->p_pool->lock );
114 vlc_cond_broadcast( &p_vdec->p_pool->wait_undecoded );
115 vlc_mutex_unlock( &p_vdec->p_pool->lock );
117 /* Waiting for the decoder thread to exit */
118 vlc_thread_join( p_vdec->thread_id );
121 /* following functions are local */
123 /*****************************************************************************
124 * vdec_InitThread: initialize video decoder thread
125 *****************************************************************************
126 * This function is called from RunThread and performs the second step of the
128 *****************************************************************************/
129 void vdec_InitThread( vdec_thread_t *p_vdec )
131 intf_DbgMsg("vdec debug: initializing video decoder thread %p", p_vdec);
133 #if !defined(SYS_BEOS)
135 /* Re-nice ourself - otherwise we would steal CPU time from the video
136 * output, which would make a poor display. */
138 if( nice(VDEC_NICE) == -1 )
140 if( !SetThreadPriority( GetCurrentThread(),
141 THREAD_PRIORITY_BELOW_NORMAL ) )
144 intf_WarnMsg( 2, "vpar warning : couldn't nice() (%s)",
150 p_vdec->p_idct_data = NULL;
152 p_vdec->p_pool->pf_decode_init( p_vdec );
153 p_vdec->p_pool->pf_idct_init( p_vdec );
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 * RunThread: video decoder thread
179 *****************************************************************************
180 * Video decoder thread. This function does only return when the thread is
182 *****************************************************************************/
183 static void RunThread( vdec_thread_t *p_vdec )
185 intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
188 vdec_InitThread( p_vdec );
193 while( !p_vdec->b_die )
197 if( (p_mb = vpar_GetMacroblock( p_vdec->p_pool, &p_vdec->b_die )) != NULL )
199 p_vdec->p_pool->pf_vdec_decode( p_vdec, p_mb );
201 /* Decoding is finished, release the macroblock and free
202 * unneeded memory. */
203 p_vdec->p_pool->pf_free_mb( p_vdec->p_pool, p_mb );
208 vdec_EndThread( p_vdec );