]> git.sesse.net Git - vlc/blob - plugins/mpeg_vdec/vpar_pool.c
* Added error checking in pthread wrapper ; as a result, intf_msg.h must
[vlc] / plugins / mpeg_vdec / vpar_pool.c
1 /*****************************************************************************
2  * vpar_pool.c : management of the pool of decoder threads
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: vpar_pool.c,v 1.2 2001/11/28 15:08:05 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 "intf_msg.h"
35 #include "threads.h"
36 #include "mtime.h"
37
38 #include "video.h"
39 #include "video_output.h"
40
41 #include "modules_export.h"
42 #include "stream_control.h"
43 #include "input_ext-dec.h"
44
45
46 #include "vdec_ext-plugins.h"
47 #include "vpar_pool.h"
48 #include "video_parser.h"
49 #include "video_decoder.h"
50
51 /*
52  * Local prototypes
53  */
54 static void WaitDummy( vdec_pool_t * p_pool );
55 static void WaitPool( vdec_pool_t * p_pool );
56 static void FreeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb );
57 static void FreeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb );
58 static macroblock_t * NewMacroblockDummy( vdec_pool_t * p_pool );
59 static macroblock_t * NewMacroblockPool( vdec_pool_t * p_pool );
60 static void DecodeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb );
61 static void DecodeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb );
62
63 /*****************************************************************************
64  * vpar_InitPool: Initializes the pool structure
65  *****************************************************************************/
66 void vpar_InitPool( vpar_thread_t * p_vpar )
67 {
68     int j;
69
70     /* Initialize mutex and cond. */
71     vlc_mutex_init( &p_vpar->pool.lock );
72     vlc_cond_init( &p_vpar->pool.wait_empty );
73     vlc_cond_init( &p_vpar->pool.wait_undecoded );
74
75     /* Spawn optional video decoder threads. */
76     p_vpar->pool.i_smp = 0;
77     p_vpar->pool.pp_vdec = NULL;
78     p_vpar->pool.p_macroblocks = NULL;
79     p_vpar->pool.pp_empty_macroblocks = NULL;
80     p_vpar->pool.pp_new_macroblocks = NULL;
81     p_vpar->pool.p_vpar = p_vpar;
82     vpar_SpawnPool( p_vpar );
83
84     /* Initialize fake video decoder structure (used when
85      * decoder == parser). */
86     if ( (p_vpar->pool.p_vdec =
87                 (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
88     {
89         intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
90         p_vpar->p_fifo->b_error = 1;
91         return;
92     }
93     p_vpar->pool.p_vdec->b_die = 0;
94     p_vpar->pool.p_vdec->p_pool = &p_vpar->pool;
95     vdec_InitThread( p_vpar->pool.p_vdec );
96
97     for( j = 0; j < 12; j++ )
98     {
99         p_vpar->pool.mb.p_idcts[j].pi_block =
100         memalign( 16, 64 * sizeof(dctelem_t) );
101     }
102 }
103
104 /*****************************************************************************
105  * vpar_SpawnPool: Create and cancel video decoder threads at any time
106  *****************************************************************************
107  * This function is called on startup, and everytime the user changes the
108  * number of threads to launch. Please note that *all* decoder threads must
109  * be idle during this operation, which only happens at the end of
110  * PictureHeader().
111  *****************************************************************************/
112 void vpar_SpawnPool( vpar_thread_t * p_vpar )
113 {
114     int                 i_new_smp;
115     stream_ctrl_t *     p_control;
116
117     p_control = p_vpar->p_config->p_stream_ctrl;
118     vlc_mutex_lock( &p_control->control_lock );
119     i_new_smp = p_control->i_smp;
120     vlc_mutex_unlock( &p_control->control_lock );
121
122     /* FIXME: No error check because I'm tired. Come back later... */
123
124     /* No need to lock p_vpar->pool, since decoders MUST be idle here. */
125     if( p_vpar->pool.i_smp != i_new_smp )
126     {
127         int i;
128
129         if( p_vpar->pool.i_smp > i_new_smp )
130         {
131             /* The user reduces the number of threads. */
132
133             for( i = p_vpar->pool.i_smp - 1; i >= i_new_smp; i-- )
134             {
135                 int j;
136
137                 vdec_DestroyThread( p_vpar->pool.pp_vdec[i] );
138
139                 for( j = 0; j < 12; j++ )
140                 {
141                     free( p_vpar->pool.p_macroblocks[i].p_idcts[j].pi_block );
142                 }
143             }
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         else
157         {
158             /* The user raises the number of threads. */
159
160             p_vpar->pool.pp_vdec = realloc( p_vpar->pool.pp_vdec,
161                                             i_new_smp * sizeof(vdec_thread_t *) );
162             p_vpar->pool.p_macroblocks = realloc( p_vpar->pool.p_macroblocks,
163                                             i_new_smp * sizeof(macroblock_t) );
164             p_vpar->pool.pp_empty_macroblocks = realloc( p_vpar->pool.pp_empty_macroblocks,
165                                             i_new_smp * sizeof(macroblock_t *) );
166             p_vpar->pool.i_index_empty = i_new_smp;
167             p_vpar->pool.pp_new_macroblocks = realloc( p_vpar->pool.pp_new_macroblocks,
168                                             i_new_smp * sizeof(macroblock_t *) );
169             p_vpar->pool.i_index_new = 0;
170
171             for( i = p_vpar->pool.i_smp; i < i_new_smp ; i++ )
172             {
173                 int j;
174
175                 for( j = 0; j < 12; j++ )
176                 {
177                     p_vpar->pool.p_macroblocks[i].p_idcts[j].pi_block =
178                         memalign( 16, 64 * sizeof(dctelem_t) );
179                 }
180
181                 p_vpar->pool.pp_vdec[i] = vdec_CreateThread( &p_vpar->pool );
182             }
183
184         }
185         for( i = 0; i < i_new_smp; i++ )
186         {
187             p_vpar->pool.pp_empty_macroblocks[i] =
188                                     &p_vpar->pool.p_macroblocks[i];
189         }
190         p_vpar->pool.i_smp = i_new_smp;
191     }
192
193     if( i_new_smp )
194     {
195         /* We have at least one decoder thread. */
196         p_vpar->pool.pf_wait_pool = WaitPool;
197         p_vpar->pool.pf_new_mb = NewMacroblockPool;
198         p_vpar->pool.pf_free_mb = FreeMacroblockPool;
199         p_vpar->pool.pf_decode_mb = DecodeMacroblockPool;
200     }
201     else
202     {
203         /* No decoder pool. */
204         p_vpar->pool.pf_wait_pool = WaitDummy;
205         p_vpar->pool.pf_new_mb = NewMacroblockDummy;
206         p_vpar->pool.pf_free_mb = FreeMacroblockDummy;
207         p_vpar->pool.pf_decode_mb = DecodeMacroblockDummy;
208     }
209 }
210
211 /*****************************************************************************
212  * vpar_EndPool: Releases the pool structure
213  *****************************************************************************/
214 void vpar_EndPool( vpar_thread_t * p_vpar )
215 {
216     int i;
217
218     for( i = 0; i < p_vpar->pool.i_smp; i++ )
219     {
220         int j;
221
222         vdec_DestroyThread( p_vpar->pool.pp_vdec[i] );
223
224         for( j = 0; j < 12; j++ )
225         {
226             free( p_vpar->pool.p_macroblocks[i].p_idcts[j].pi_block );
227         }
228     }
229
230     if( p_vpar->pool.i_smp )
231     {
232         free( p_vpar->pool.pp_vdec );
233         free( p_vpar->pool.p_macroblocks );
234         free( p_vpar->pool.pp_new_macroblocks );
235     }
236
237     /* Free fake video decoder (used when parser == decoder). */
238     vdec_EndThread( p_vpar->pool.p_vdec );
239
240     /* Destroy lock and cond. */
241     vlc_mutex_destroy( &p_vpar->pool.lock );
242     vlc_cond_destroy( &p_vpar->pool.wait_empty );
243     vlc_cond_destroy( &p_vpar->pool.wait_undecoded );
244 }
245
246 /*****************************************************************************
247  * WaitPool: Wait until all decoders are idle
248  *****************************************************************************/
249 static void WaitPool( vdec_pool_t * p_pool )
250 {
251     vlc_mutex_lock( &p_pool->lock );
252     while( p_pool->i_index_empty != p_pool->i_smp )
253     {
254         vlc_cond_wait( &p_pool->wait_empty, &p_pool->lock );
255     }
256     vlc_mutex_unlock( &p_pool->lock );
257 }
258
259 /*****************************************************************************
260  * WaitDummy: Placeholder used when parser == decoder
261  *****************************************************************************/
262 static void WaitDummy( vdec_pool_t * p_pool )
263 {
264 }
265
266 /*****************************************************************************
267  * NewMacroblockPool: Get an empty macroblock from the decoder pool
268  *****************************************************************************/
269 static macroblock_t * NewMacroblockPool( vdec_pool_t * p_pool )
270 {
271     macroblock_t *  p_mb;
272
273     vlc_mutex_lock( &p_pool->lock );
274     while( p_pool->i_index_empty == 0 )
275     {
276         vlc_cond_wait( &p_pool->wait_empty, &p_pool->lock );
277     }
278     p_mb = p_pool->pp_empty_macroblocks[ --p_pool->i_index_empty ];
279     vlc_mutex_unlock( &p_pool->lock );
280     return( p_mb );
281 }
282
283 /*****************************************************************************
284  * NewMacroblockDummy: Placeholder used when parser == decoder
285  *****************************************************************************/
286 static macroblock_t * NewMacroblockDummy( vdec_pool_t * p_pool )
287 {
288     return( &p_pool->mb );
289 }
290
291 /*****************************************************************************
292  * FreeMacroblockPool: Free a macroblock
293  *****************************************************************************/
294 static void FreeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb )
295 {
296     vlc_mutex_lock( &p_pool->lock );
297     p_pool->pp_empty_macroblocks[ p_pool->i_index_empty++ ] = p_mb;
298     vlc_cond_signal( &p_pool->wait_empty );
299     vlc_mutex_unlock( &p_pool->lock );
300 }
301
302 /*****************************************************************************
303  * FreeMacroblockDummy: Placeholder used when parser == decoder
304  *****************************************************************************/
305 static void FreeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb )
306 {
307 }
308
309 /*****************************************************************************
310  * DecodeMacroblockPool: Send a macroblock to a vdec thread
311  *****************************************************************************/
312 static void DecodeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb )
313 {
314     vlc_mutex_lock( &p_pool->lock );
315     /* The undecoded macroblock LIFO cannot be full, because
316      * #macroblocks == size of the LIFO */
317     p_pool->pp_new_macroblocks[ p_pool->i_index_new++ ] = p_mb;
318     vlc_cond_signal( &p_pool->wait_undecoded );
319     vlc_mutex_unlock( &p_pool->lock );
320 }
321
322 /*****************************************************************************
323  * DecodeMacroblockDummy: Placeholder used when parser == decoder
324  *****************************************************************************/
325 static void DecodeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb )
326 {
327     p_pool->pf_vdec_decode( p_pool->p_vdec, p_mb );
328 }
329