]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
2323aaaf724c36e5661a90397a8e9cd77244f6b3
[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.56 2001/07/31 21:13:30 gbazin Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          GaĆ«l Hendryckx <jimmy@via.ecp.fr>
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
55 /*
56  * Local prototypes
57  */
58 static void     RunThread           ( vdec_thread_t *p_vdec );
59
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 )
67 {
68     vdec_thread_t *     p_vdec;
69
70     intf_DbgMsg("vdec debug: creating video decoder thread");
71
72     /* Allocate the memory needed to store the thread's structure */
73     if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
74     {
75         intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
76         return( NULL );
77     }
78
79     /*
80      * Initialize the thread properties
81      */
82     p_vdec->b_die = 0;
83
84     /*
85      * Initialize the parser properties
86      */
87     p_vdec->p_pool = p_pool;
88
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) )
92     {
93         intf_ErrMsg("vdec error: can't spawn video decoder thread");
94         free( p_vdec );
95         return( NULL );
96     }
97
98     intf_DbgMsg("vdec debug: video decoder thread (%p) created", p_vdec);
99     return( p_vdec );
100 }
101
102 /*****************************************************************************
103  * vdec_DestroyThread: destroy a video decoder thread
104  *****************************************************************************/
105 void vdec_DestroyThread( vdec_thread_t *p_vdec )
106 {
107     intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p", p_vdec);
108
109     /* Ask thread to kill itself */
110     p_vdec->b_die = 1;
111
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 );
116
117     /* Waiting for the decoder thread to exit */
118     vlc_thread_join( p_vdec->thread_id );
119 }
120
121 /* following functions are local */
122
123 /*****************************************************************************
124  * vdec_InitThread: initialize video decoder thread
125  *****************************************************************************
126  * This function is called from RunThread and performs the second step of the
127  * initialization.
128  *****************************************************************************/
129 void vdec_InitThread( vdec_thread_t *p_vdec )
130 {
131     intf_DbgMsg("vdec debug: initializing video decoder thread %p", p_vdec);
132
133 #if !defined(SYS_BEOS)
134 #   if VDEC_NICE
135     /* Re-nice ourself - otherwise we would steal CPU time from the video
136      * output, which would make a poor display. */
137 #if !defined(WIN32)
138     if( nice(VDEC_NICE) == -1 )
139 #else
140     if( !SetThreadPriority( GetCurrentThread(),
141                             THREAD_PRIORITY_BELOW_NORMAL ) )
142 #endif
143     {
144         intf_WarnMsg( 2, "vpar warning : couldn't nice() (%s)",
145                       strerror(errno) );
146     }
147 #   endif
148 #endif
149
150     p_vdec->p_idct_data = NULL;
151
152     p_vdec->p_pool->pf_decode_init( p_vdec );
153     p_vdec->p_pool->pf_idct_init( p_vdec );
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  * RunThread: video decoder thread
179  *****************************************************************************
180  * Video decoder thread. This function does only return when the thread is
181  * terminated.
182  *****************************************************************************/
183 static void RunThread( vdec_thread_t *p_vdec )
184 {
185     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
186                 p_vdec, getpid());
187
188     vdec_InitThread( p_vdec );
189
190     /*
191      * Main loop
192      */
193     while( !p_vdec->b_die )
194     {
195         macroblock_t *          p_mb;
196
197         if( (p_mb = vpar_GetMacroblock( p_vdec->p_pool, &p_vdec->b_die )) != NULL )
198         {
199             p_vdec->p_pool->pf_vdec_decode( p_vdec, p_mb );
200
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 );
204         }
205     }
206
207     /* End of thread */
208     vdec_EndThread( p_vdec );
209 }
210