* ./src/video_output/video_output.c: fixed the "picture has invalid status"
[vlc.git] / 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.11 2002/06/02 09:03:54 sam 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 <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include <vlc/decoder.h>
33
34 #include "vdec_ext-plugins.h"
35 #include "video_decoder.h"
36 #include "vpar_pool.h"
37 #include "video_parser.h"
38
39 /*
40  * Local prototypes
41  */
42 static void WaitDummy( vdec_pool_t * p_pool );
43 static void WaitPool( vdec_pool_t * p_pool );
44 static void FreeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb );
45 static void FreeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb );
46 static macroblock_t * NewMacroblockDummy( vdec_pool_t * p_pool );
47 static macroblock_t * NewMacroblockPool( vdec_pool_t * p_pool );
48 static void DecodeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb );
49 static void DecodeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb );
50
51 /*****************************************************************************
52  * vpar_InitPool: Initializes the pool structure
53  *****************************************************************************/
54 void vpar_InitPool( vpar_thread_t * p_vpar )
55 {
56     int j;
57
58     /* Initialize mutex and cond. */
59     vlc_mutex_init( p_vpar->p_fifo, &p_vpar->pool.lock );
60     vlc_cond_init( &p_vpar->pool.wait_empty );
61     vlc_cond_init( &p_vpar->pool.wait_undecoded );
62
63     /* Spawn optional video decoder threads. */
64     p_vpar->pool.i_smp = 0;
65     p_vpar->pool.pp_vdec = NULL;
66     p_vpar->pool.p_macroblocks = NULL;
67     p_vpar->pool.pp_empty_macroblocks = NULL;
68     p_vpar->pool.pp_new_macroblocks = NULL;
69     p_vpar->pool.p_vpar = p_vpar;
70     vpar_SpawnPool( p_vpar );
71
72     /* Initialize fake video decoder structure (used when
73      * decoder == parser). */
74     p_vpar->pool.p_vdec = vlc_object_create( p_vpar->p_fifo,
75                                              sizeof(vdec_thread_t) );
76     if ( p_vpar->pool.p_vdec == NULL )
77     {
78         msg_Err( p_vpar->p_fifo, "out of memory" );
79         p_vpar->p_fifo->b_error = 1;
80         return;
81     }
82     p_vpar->pool.p_vdec->p_pool = &p_vpar->pool;
83     vdec_InitThread( p_vpar->pool.p_vdec );
84
85     vlc_object_attach( p_vpar->pool.p_vdec, p_vpar->p_fifo );
86
87     for( j = 0; j < 12; j++ )
88     {
89         p_vpar->pool.mb.p_idcts[j].pi_block =
90             vlc_memalign( &p_vpar->pool.mb.p_idcts[j].pi_block_orig,
91                           16, 64 * sizeof(dctelem_t) );
92     }
93 }
94
95 /*****************************************************************************
96  * vpar_SpawnPool: Create and cancel video decoder threads at any time
97  *****************************************************************************
98  * This function is called on startup, and everytime the user changes the
99  * number of threads to launch. Please note that *all* decoder threads must
100  * be idle during this operation, which only happens at the end of
101  * PictureHeader().
102  *****************************************************************************/
103 void vpar_SpawnPool( vpar_thread_t * p_vpar )
104 {
105     int                 i_new_smp;
106     stream_ctrl_t *     p_control;
107
108     p_control = p_vpar->p_fifo->p_stream_ctrl;
109     vlc_mutex_lock( &p_control->control_lock );
110     i_new_smp = p_control->i_smp;
111     vlc_mutex_unlock( &p_control->control_lock );
112
113     /* FIXME: No error check because I'm tired. Come back later... */
114
115     /* No need to lock p_vpar->pool, since decoders MUST be idle here. */
116     if( p_vpar->pool.i_smp != i_new_smp )
117     {
118         int i;
119
120         if( p_vpar->pool.i_smp > i_new_smp )
121         {
122             /* The user reduces the number of threads. */
123
124             for( i = p_vpar->pool.i_smp - 1; i >= i_new_smp; i-- )
125             {
126                 int j;
127
128                 vlc_object_detach_all( p_vpar->pool.pp_vdec[i] );
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_orig );
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                         vlc_memalign( &p_vpar->pool.p_macroblocks[i].p_idcts[j].pi_block_orig,
171                                       16, 64 * sizeof(dctelem_t) );
172                 }
173
174                 p_vpar->pool.pp_vdec[i] = vdec_CreateThread( &p_vpar->pool );
175                 vlc_object_attach( p_vpar->pool.pp_vdec[i], p_vpar->p_fifo );
176             }
177
178         }
179         for( i = 0; i < i_new_smp; i++ )
180         {
181             p_vpar->pool.pp_empty_macroblocks[i] =
182                                     &p_vpar->pool.p_macroblocks[i];
183         }
184         p_vpar->pool.i_smp = i_new_smp;
185     }
186
187     if( i_new_smp )
188     {
189         /* We have at least one decoder thread. */
190         p_vpar->pool.pf_wait_pool = WaitPool;
191         p_vpar->pool.pf_new_mb = NewMacroblockPool;
192         p_vpar->pool.pf_free_mb = FreeMacroblockPool;
193         p_vpar->pool.pf_decode_mb = DecodeMacroblockPool;
194     }
195     else
196     {
197         /* No decoder pool. */
198         p_vpar->pool.pf_wait_pool = WaitDummy;
199         p_vpar->pool.pf_new_mb = NewMacroblockDummy;
200         p_vpar->pool.pf_free_mb = FreeMacroblockDummy;
201         p_vpar->pool.pf_decode_mb = DecodeMacroblockDummy;
202     }
203 }
204
205 /*****************************************************************************
206  * vpar_EndPool: Releases the pool structure
207  *****************************************************************************/
208 void vpar_EndPool( vpar_thread_t * p_vpar )
209 {
210     int i;
211
212     for( i = 0; i < 12; i++ )
213     {
214         free( p_vpar->pool.mb.p_idcts[i].pi_block_orig );
215     }
216
217     for( i = 0; i < p_vpar->pool.i_smp; i++ )
218     {
219         int j;
220
221         vlc_object_detach_all( p_vpar->pool.pp_vdec[i] );
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_orig );
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     vlc_object_detach_all( p_vpar->pool.p_vdec );
239     vdec_EndThread( p_vpar->pool.p_vdec );
240     vlc_object_destroy( p_vpar->pool.p_vdec );
241
242     /* Destroy lock and cond. */
243     vlc_mutex_destroy( &p_vpar->pool.lock );
244     vlc_cond_destroy( &p_vpar->pool.wait_empty );
245     vlc_cond_destroy( &p_vpar->pool.wait_undecoded );
246 }
247
248 /*****************************************************************************
249  * WaitPool: Wait until all decoders are idle
250  *****************************************************************************/
251 static void WaitPool( vdec_pool_t * p_pool )
252 {
253     vlc_mutex_lock( &p_pool->lock );
254     while( p_pool->i_index_empty != p_pool->i_smp )
255     {
256         vlc_cond_wait( &p_pool->wait_empty, &p_pool->lock );
257     }
258     vlc_mutex_unlock( &p_pool->lock );
259 }
260
261 /*****************************************************************************
262  * WaitDummy: Placeholder used when parser == decoder
263  *****************************************************************************/
264 static void WaitDummy( vdec_pool_t * p_pool )
265 {
266 }
267
268 /*****************************************************************************
269  * NewMacroblockPool: Get an empty macroblock from the decoder pool
270  *****************************************************************************/
271 static macroblock_t * NewMacroblockPool( vdec_pool_t * p_pool )
272 {
273     macroblock_t *  p_mb;
274
275     vlc_mutex_lock( &p_pool->lock );
276     while( p_pool->i_index_empty == 0 )
277     {
278         vlc_cond_wait( &p_pool->wait_empty, &p_pool->lock );
279     }
280     p_mb = p_pool->pp_empty_macroblocks[ --p_pool->i_index_empty ];
281     vlc_mutex_unlock( &p_pool->lock );
282     return( p_mb );
283 }
284
285 /*****************************************************************************
286  * NewMacroblockDummy: Placeholder used when parser == decoder
287  *****************************************************************************/
288 static macroblock_t * NewMacroblockDummy( vdec_pool_t * p_pool )
289 {
290     return( &p_pool->mb );
291 }
292
293 /*****************************************************************************
294  * FreeMacroblockPool: Free a macroblock
295  *****************************************************************************/
296 static void FreeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb )
297 {
298     vlc_mutex_lock( &p_pool->lock );
299     p_pool->pp_empty_macroblocks[ p_pool->i_index_empty++ ] = p_mb;
300     vlc_cond_signal( &p_pool->wait_empty );
301     vlc_mutex_unlock( &p_pool->lock );
302 }
303
304 /*****************************************************************************
305  * FreeMacroblockDummy: Placeholder used when parser == decoder
306  *****************************************************************************/
307 static void FreeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb )
308 {
309 }
310
311 /*****************************************************************************
312  * DecodeMacroblockPool: Send a macroblock to a vdec thread
313  *****************************************************************************/
314 static void DecodeMacroblockPool( vdec_pool_t * p_pool, macroblock_t * p_mb )
315 {
316     vlc_mutex_lock( &p_pool->lock );
317     /* The undecoded macroblock LIFO cannot be full, because
318      * #macroblocks == size of the LIFO */
319     p_pool->pp_new_macroblocks[ p_pool->i_index_new++ ] = p_mb;
320     vlc_cond_signal( &p_pool->wait_undecoded );
321     vlc_mutex_unlock( &p_pool->lock );
322 }
323
324 /*****************************************************************************
325  * DecodeMacroblockDummy: Placeholder used when parser == decoder
326  *****************************************************************************/
327 static void DecodeMacroblockDummy( vdec_pool_t * p_pool, macroblock_t * p_mb )
328 {
329     p_pool->pf_vdec_decode( p_pool->p_vdec, p_mb );
330 }
331