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 $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <string.h> /* memcpy(), memset() */
30 #include <stdlib.h> /* realloc() */
39 #include "stream_control.h"
40 #include "input_ext-dec.h"
43 #include "video_output.h"
45 #include "vdec_ext-plugins.h"
46 #include "vpar_pool.h"
47 #include "video_parser.h"
48 #include "video_decoder.h"
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 );
62 /*****************************************************************************
63 * vpar_InitPool: Initializes the pool structure
64 *****************************************************************************/
65 void vpar_InitPool( vpar_thread_t * p_vpar )
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 );
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 );
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 )
85 intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread");
86 p_vpar->p_fifo->b_error = 1;
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 );
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
101 *****************************************************************************/
102 void vpar_SpawnPool( vpar_thread_t * p_vpar )
105 boolean_t b_grayscale;
106 stream_ctrl_t * p_control;
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 );
114 /* FIXME: No error check because I'm tired. Come back later... */
116 /* No need to lock p_vpar->pool, since decoders MUST be idle here. */
117 if( p_vpar->pool.i_smp != i_new_smp )
121 if( p_vpar->pool.i_smp > i_new_smp )
123 /* The user reduces the number of threads. */
125 for( i = p_vpar->pool.i_smp - 1; i >= i_new_smp; i-- )
127 vdec_DestroyThread( p_vpar->pool.pp_vdec[i] );
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;
143 /* The user raises the number of threads. */
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;
156 for( i = p_vpar->pool.i_smp; i < i_new_smp ; i++ )
158 p_vpar->pool.pp_vdec[i] = vdec_CreateThread( &p_vpar->pool );
162 for( i = 0; i < i_new_smp; i++ )
164 p_vpar->pool.pp_empty_macroblocks[i] =
165 &p_vpar->pool.p_macroblocks[i];
167 p_vpar->pool.i_smp = i_new_smp;
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;
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;
189 p_vpar->pool.pf_vdec_decode = p_vpar->pf_decode_mb_c;
193 p_vpar->pool.pf_vdec_decode = p_vpar->pf_decode_mb_bw;
197 /*****************************************************************************
198 * vpar_EndPool: Releases the pool structure
199 *****************************************************************************/
200 void vpar_EndPool( vpar_thread_t * p_vpar )
204 for( i = 0; i < p_vpar->pool.i_smp; i++ )
206 vdec_DestroyThread( p_vpar->pool.pp_vdec[i] );
209 if( p_vpar->pool.i_smp )
211 free( p_vpar->pool.pp_vdec );
212 free( p_vpar->pool.p_macroblocks );
213 free( p_vpar->pool.pp_new_macroblocks );
216 /* Free fake video decoder (used when parser == decoder). */
217 vdec_EndThread( p_vpar->pool.p_vdec );
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 );
225 /*****************************************************************************
226 * WaitPool: Wait until all decoders are idle
227 *****************************************************************************/
228 static void WaitPool( vdec_pool_t * p_pool )
230 vlc_mutex_lock( &p_pool->lock );
231 while( p_pool->i_index_empty != p_pool->i_smp )
233 vlc_cond_wait( &p_pool->wait_empty, &p_pool->lock );
235 vlc_mutex_unlock( &p_pool->lock );
238 /*****************************************************************************
239 * WaitDummy: Placeholder used when parser == decoder
240 *****************************************************************************/
241 static void WaitDummy( vdec_pool_t * p_pool )
245 /*****************************************************************************
246 * NewMacroblockPool: Get an empty macroblock from the decoder pool
247 *****************************************************************************/
248 static macroblock_t * NewMacroblockPool( vdec_pool_t * p_pool )
252 vlc_mutex_lock( &p_pool->lock );
253 while( p_pool->i_index_empty == 0 )
255 vlc_cond_wait( &p_pool->wait_empty, &p_pool->lock );
257 p_mb = p_pool->pp_empty_macroblocks[ --p_pool->i_index_empty ];
258 vlc_mutex_unlock( &p_pool->lock );
262 /*****************************************************************************
263 * NewMacroblockDummy: Placeholder used when parser == decoder
264 *****************************************************************************/
265 static macroblock_t * NewMacroblockDummy( vdec_pool_t * p_pool )
267 return( &p_pool->mb );
270 /*****************************************************************************
271 * FreeMacroblockPool: Free a macroblock
272 *****************************************************************************/
273 static void FreeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb )
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 );
281 /*****************************************************************************
282 * FreeMacroblockDummy: Placeholder used when parser == decoder
283 *****************************************************************************/
284 static void FreeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb )
288 /*****************************************************************************
289 * DecodeMacroblockPool: Send a macroblock to a vdec thread
290 *****************************************************************************/
291 static void DecodeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb )
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 );
301 /*****************************************************************************
302 * DecodeMacroblockDummy: Placeholder used when parser == decoder
303 *****************************************************************************/
304 static void DecodeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb )
306 p_pool->pf_vdec_decode( p_pool->p_vdec, p_mb );