]> git.sesse.net Git - vlc/blob - src/video_decoder/vpar_pool.c
d1ecf6575fdb00fca166c4781fe90bd159069890
[vlc] / src / video_decoder / vpar_pool.c
1 /*****************************************************************************
2  * vpar_pool.c : management of the pool of decoder threads
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: vpar_pool.c,v 1.1 2001/07/18 14:21:00 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <string.h>                                    /* memcpy(), memset() */
30 #include <stdlib.h>                                             /* realloc() */
31
32 #include "config.h"
33 #include "common.h"
34 #include "threads.h"
35 #include "mtime.h"
36
37 #include "intf_msg.h"
38
39 #include "stream_control.h"
40 #include "input_ext-dec.h"
41
42 #include "video.h"
43 #include "video_output.h"
44
45 #include "vdec_ext-plugins.h"
46 #include "vpar_pool.h"
47 #include "video_parser.h"
48 #include "video_decoder.h"
49
50 /*
51  * Local prototypes
52  */
53 static void WaitDummy( vdec_pool_t * p_pool );
54 static void WaitPool( vdec_pool_t * p_pool );
55 static void FreeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb );
56 static void FreeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb );
57 static macroblock_t * NewMacroblockDummy( vdec_pool_t * p_pool );
58 static macroblock_t * NewMacroblockPool( vdec_pool_t * p_pool );
59 static void DecodeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb );
60 static void DecodeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb );
61
62 /*****************************************************************************
63  * vpar_InitPool: Initializes the pool structure
64  *****************************************************************************/
65 void vpar_InitPool( vpar_thread_t * p_vpar )
66 {
67     /* Initialize mutex and cond. */
68     vlc_mutex_init( &p_vpar->pool.lock );
69     vlc_cond_init( &p_vpar->pool.wait_empty );
70     vlc_cond_init( &p_vpar->pool.wait_undecoded );
71
72     /* Spawn optional video decoder threads. */
73     p_vpar->pool.i_smp = 0;
74     p_vpar->pool.pp_vdec = NULL;
75     p_vpar->pool.p_macroblocks = NULL;
76     p_vpar->pool.pp_empty_macroblocks = NULL;
77     p_vpar->pool.pp_new_macroblocks = NULL;
78     vpar_SpawnPool( p_vpar );
79
80     /* Initialize fake video decoder structure (used when
81      * decoder == parser). */
82     if ( (p_vpar->pool.p_vdec =
83                 (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
84     {
85         intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
86         p_vpar->p_fifo->b_error = 1;
87         return;
88     }
89     p_vpar->pool.p_vdec->b_die = 0;
90     p_vpar->pool.p_vdec->p_pool = &p_vpar->pool;
91     vdec_InitThread( p_vpar->pool.p_vdec );
92 }
93
94 /*****************************************************************************
95  * vpar_SpawnPool: Create and cancel video decoder threads at any time
96  *****************************************************************************
97  * This function is called on startup, and everytime the user changes the
98  * number of threads to launch. Please note that *all* decoder threads must
99  * be idle during this operation, which only happens at the end of
100  * PictureHeader().
101  *****************************************************************************/
102 void vpar_SpawnPool( vpar_thread_t * p_vpar )
103 {
104     int                 i_new_smp;
105     boolean_t           b_grayscale;
106     stream_ctrl_t *     p_control;
107
108     p_control = p_vpar->p_config->decoder_config.p_stream_ctrl;
109     vlc_mutex_lock( &p_control->control_lock );
110     i_new_smp = p_control->i_smp;
111     b_grayscale = p_control->b_grayscale;
112     vlc_mutex_unlock( &p_control->control_lock );
113
114     /* FIXME: No error check because I'm tired. Come back later... */
115
116     /* No need to lock p_vpar->pool, since decoders MUST be idle here. */
117     if( p_vpar->pool.i_smp != i_new_smp )
118     {
119         int i;
120
121         if( p_vpar->pool.i_smp > i_new_smp )
122         {
123             /* The user reduces the number of threads. */
124
125             for( i = p_vpar->pool.i_smp - 1; i >= i_new_smp; i-- )
126             {
127                 vdec_DestroyThread( p_vpar->pool.pp_vdec[i] );
128             }
129
130             p_vpar->pool.pp_vdec = realloc( p_vpar->pool.pp_vdec,
131                                             i_new_smp * sizeof(vdec_thread_t *) );
132             p_vpar->pool.p_macroblocks = realloc( p_vpar->pool.p_macroblocks,
133                                             i_new_smp * sizeof(macroblock_t) );
134             p_vpar->pool.pp_empty_macroblocks = realloc( p_vpar->pool.pp_empty_macroblocks,
135                                             i_new_smp * sizeof(macroblock_t *) );
136             p_vpar->pool.i_index_empty = i_new_smp;
137             p_vpar->pool.pp_new_macroblocks = realloc( p_vpar->pool.pp_new_macroblocks,
138                                             i_new_smp * sizeof(macroblock_t *) );
139             p_vpar->pool.i_index_new = 0;
140         }
141         else
142         {
143             /* The user raises the number of threads. */
144
145             p_vpar->pool.pp_vdec = realloc( p_vpar->pool.pp_vdec,
146                                             i_new_smp * sizeof(vdec_thread_t *) );
147             p_vpar->pool.p_macroblocks = realloc( p_vpar->pool.p_macroblocks,
148                                             i_new_smp * sizeof(macroblock_t) );
149             p_vpar->pool.pp_empty_macroblocks = realloc( p_vpar->pool.pp_empty_macroblocks,
150                                             i_new_smp * sizeof(macroblock_t *) );
151             p_vpar->pool.i_index_empty = i_new_smp;
152             p_vpar->pool.pp_new_macroblocks = realloc( p_vpar->pool.pp_new_macroblocks,
153                                             i_new_smp * sizeof(macroblock_t *) );
154             p_vpar->pool.i_index_new = 0;
155
156             for( i = p_vpar->pool.i_smp; i < i_new_smp ; i++ )
157             {
158                 p_vpar->pool.pp_vdec[i] = vdec_CreateThread( &p_vpar->pool );
159             }
160
161         }
162         for( i = 0; i < i_new_smp; i++ )
163         {
164             p_vpar->pool.pp_empty_macroblocks[i] =
165                                     &p_vpar->pool.p_macroblocks[i];
166         }
167         p_vpar->pool.i_smp = i_new_smp;
168     }
169
170     if( i_new_smp )
171     {
172         /* We have at least one decoder thread. */
173         p_vpar->pool.pf_wait_pool = WaitPool;
174         p_vpar->pool.pf_new_mb = NewMacroblockPool;
175         p_vpar->pool.pf_free_mb = FreeMacroblockPool;
176         p_vpar->pool.pf_decode_mb = DecodeMacroblockPool;
177     }
178     else
179     {
180         /* No decoder pool. */
181         p_vpar->pool.pf_wait_pool = WaitDummy;
182         p_vpar->pool.pf_new_mb = NewMacroblockDummy;
183         p_vpar->pool.pf_free_mb = FreeMacroblockDummy;
184         p_vpar->pool.pf_decode_mb = DecodeMacroblockDummy;
185     }
186
187     if( !b_grayscale )
188     {
189         p_vpar->pool.pf_vdec_decode = p_vpar->pf_decode_mb_c;
190     }
191     else
192     {
193         p_vpar->pool.pf_vdec_decode = p_vpar->pf_decode_mb_bw;
194     }
195 }
196
197 /*****************************************************************************
198  * vpar_EndPool: Releases the pool structure
199  *****************************************************************************/
200 void vpar_EndPool( vpar_thread_t * p_vpar )
201 {
202     int i;
203
204     for( i = 0; i < p_vpar->pool.i_smp; i++ )
205     {
206         vdec_DestroyThread( p_vpar->pool.pp_vdec[i] );
207     }
208
209     if( p_vpar->pool.i_smp )
210     {
211         free( p_vpar->pool.pp_vdec );
212         free( p_vpar->pool.p_macroblocks );
213         free( p_vpar->pool.pp_new_macroblocks );
214     }
215
216     /* Free fake video decoder (used when parser == decoder). */
217     vdec_EndThread( p_vpar->pool.p_vdec );
218
219     /* Destroy lock and cond. */
220     vlc_mutex_destroy( &p_vpar->pool.lock );
221     vlc_cond_destroy( &p_vpar->pool.wait_empty );
222     vlc_cond_destroy( &p_vpar->pool.wait_undecoded );
223 }
224
225 /*****************************************************************************
226  * WaitPool: Wait until all decoders are idle
227  *****************************************************************************/
228 static void WaitPool( vdec_pool_t * p_pool )
229 {
230     vlc_mutex_lock( &p_pool->lock );
231     while( p_pool->i_index_empty != p_pool->i_smp )
232     {
233         vlc_cond_wait( &p_pool->wait_empty, &p_pool->lock );
234     }
235     vlc_mutex_unlock( &p_pool->lock );
236 }
237
238 /*****************************************************************************
239  * WaitDummy: Placeholder used when parser == decoder
240  *****************************************************************************/
241 static void WaitDummy( vdec_pool_t * p_pool )
242 {
243 }
244
245 /*****************************************************************************
246  * NewMacroblockPool: Get an empty macroblock from the decoder pool
247  *****************************************************************************/
248 static macroblock_t * NewMacroblockPool( vdec_pool_t * p_pool )
249 {
250     macroblock_t *  p_mb;
251
252     vlc_mutex_lock( &p_pool->lock );
253     while( p_pool->i_index_empty == 0 )
254     {
255         vlc_cond_wait( &p_pool->wait_empty, &p_pool->lock );
256     }
257     p_mb = p_pool->pp_empty_macroblocks[ --p_pool->i_index_empty ];
258     vlc_mutex_unlock( &p_pool->lock );
259     return( p_mb );
260 }
261
262 /*****************************************************************************
263  * NewMacroblockDummy: Placeholder used when parser == decoder
264  *****************************************************************************/
265 static macroblock_t * NewMacroblockDummy( vdec_pool_t * p_pool )
266 {
267     return( &p_pool->mb );
268 }
269
270 /*****************************************************************************
271  * FreeMacroblockPool: Free a macroblock
272  *****************************************************************************/
273 static void FreeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb )
274 {
275     vlc_mutex_lock( &p_pool->lock );
276     p_pool->pp_empty_macroblocks[ p_pool->i_index_empty++ ] = p_mb;
277     vlc_cond_signal( &p_pool->wait_empty );
278     vlc_mutex_unlock( &p_pool->lock );
279 }
280
281 /*****************************************************************************
282  * FreeMacroblockDummy: Placeholder used when parser == decoder
283  *****************************************************************************/
284 static void FreeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb )
285 {
286 }
287
288 /*****************************************************************************
289  * DecodeMacroblockPool: Send a macroblock to a vdec thread
290  *****************************************************************************/
291 static void DecodeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb )
292 {
293     vlc_mutex_lock( &p_pool->lock );
294     /* The undecoded macroblock LIFO cannot be full, because
295      * #macroblocks == size of the LIFO */
296     p_pool->pp_new_macroblocks[ p_pool->i_index_new++ ] = p_mb;
297     vlc_cond_signal( &p_pool->wait_undecoded );
298     vlc_mutex_unlock( &p_pool->lock );
299 }
300
301 /*****************************************************************************
302  * DecodeMacroblockDummy: Placeholder used when parser == decoder
303  *****************************************************************************/
304 static void DecodeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb )
305 {
306     p_pool->pf_vdec_decode( p_pool->p_vdec, p_mb );
307 }
308