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