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