]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
* src/video_output/video_output.c: fixed potential segfault.
[vlc] / src / video_output / video_output.c
1 /*****************************************************************************
2  * video_output.c : video output thread
3  * This module describes the programming interface for video output threads.
4  * It includes functions allowing to open a new thread, send pictures to a
5  * thread, and destroy a previously oppened video output thread.
6  *****************************************************************************
7  * Copyright (C) 2000-2001 VideoLAN
8  * $Id: video_output.c,v 1.231 2003/07/29 22:20:53 gbazin Exp $
9  *
10  * Authors: Vincent Seguin <seguin@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                                /* free() */
31
32 #include <vlc/vlc.h>
33
34 #ifdef HAVE_SYS_TIMES_H
35 #   include <sys/times.h>
36 #endif
37
38 #include "vlc_video.h"
39 #include "video_output.h"
40 #include <vlc/input.h>                 /* for input_thread_t and i_pts_delay */
41
42 #if defined( SYS_DARWIN )
43 #include "darwin_specific.h"
44 #endif
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int      InitThread        ( vout_thread_t * );
50 static void     RunThread         ( vout_thread_t * );
51 static void     ErrorThread       ( vout_thread_t * );
52 static void     EndThread         ( vout_thread_t * );
53 static void     DestroyThread     ( vout_thread_t * );
54
55 static int      ReduceHeight      ( int );
56 static int      BinaryLog         ( uint32_t );
57 static void     MaskToShift       ( int *, int *, uint32_t );
58 static void     InitWindowSize    ( vout_thread_t *, int *, int * );
59
60 /* Object variables callbacks */
61 static int FullscreenCallback( vlc_object_t *, char const *,
62                                vlc_value_t, vlc_value_t, void * );
63 static int DeinterlaceCallback( vlc_object_t *, char const *,
64                                 vlc_value_t, vlc_value_t, void * );
65
66 /*****************************************************************************
67  * vout_Request: find a video output thread, create one, or destroy one.
68  *****************************************************************************
69  * This function looks for a video output thread matching the current
70  * properties. If not found, it spawns a new one.
71  *****************************************************************************/
72 vout_thread_t * __vout_Request ( vlc_object_t *p_this, vout_thread_t *p_vout,
73                                  unsigned int i_width, unsigned int i_height,
74                                  vlc_fourcc_t i_chroma, unsigned int i_aspect )
75 {
76     if( !i_width || !i_height || !i_chroma )
77     {
78         /* Reattach video output to input before bailing out */
79         if( p_vout )
80         {
81             vlc_object_t *p_input;
82
83             p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
84
85             if( p_input )
86             {
87                 vlc_object_detach( p_vout );
88                 vlc_object_attach( p_vout, p_input );
89
90                 vlc_object_release( p_input );
91             }
92             else
93             {
94                 vlc_object_detach( p_vout );
95                 /* vlc_object_release( p_vout ); */
96                 vout_Destroy( p_vout );
97             }
98         }
99         return NULL;
100     }
101
102     /* If a video output was provided, lock it, otherwise look for one. */
103     if( p_vout )
104     {
105         vlc_object_yield( p_vout );
106     }
107     else
108     {
109         p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_CHILD );
110
111         if( !p_vout )
112         {
113             vlc_object_t *p_input;
114
115             p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
116             if( p_input )
117             {
118                 p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT,
119                                           FIND_CHILD );
120                 /* only first children of p_input for unused vout */
121                 if( p_vout && p_vout->p_parent != p_input )
122                 {
123                     vlc_object_release( p_vout );
124                     p_vout = NULL;
125                 }
126                 vlc_object_release( p_input );
127             }
128         }
129     }
130
131     /* If we now have a video output, check it has the right properties */
132     if( p_vout )
133     {
134         char *psz_filter_chain;
135
136         /* We don't directly check for the "filter" variable for obvious
137          * performance reasons. */
138         if( p_vout->b_filter_change )
139         {
140             psz_filter_chain = config_GetPsz( p_this, "filter" );
141
142             if( psz_filter_chain && !*psz_filter_chain )
143             {
144                 free( psz_filter_chain );
145                 psz_filter_chain = NULL;
146             }
147             if( p_vout->psz_filter_chain && !*p_vout->psz_filter_chain )
148             {
149                 free( p_vout->psz_filter_chain );
150                 p_vout->psz_filter_chain = NULL;
151             }
152
153             if( ( !psz_filter_chain && !p_vout->psz_filter_chain ) ||
154                 ( psz_filter_chain && p_vout->psz_filter_chain &&
155                   !strcmp( psz_filter_chain, p_vout->psz_filter_chain ) ) )
156             {
157                 p_vout->b_filter_change = VLC_FALSE;
158             }
159
160             if( psz_filter_chain ) free( psz_filter_chain );
161         }
162
163         if( ( p_vout->render.i_width != i_width ) ||
164             ( p_vout->render.i_height != i_height ) ||
165             ( p_vout->render.i_chroma != i_chroma ) ||
166             ( p_vout->render.i_aspect != i_aspect
167                     && !p_vout->b_override_aspect ) ||
168             p_vout->b_filter_change )
169         {
170             /* We are not interested in this format, close this vout */
171             vlc_object_detach( p_vout );
172             vlc_object_release( p_vout );
173             vout_Destroy( p_vout );
174             p_vout = NULL;
175         }
176         else
177         {
178             /* This video output is cool! Hijack it. */
179             vlc_object_detach( p_vout );
180             vlc_object_attach( p_vout, p_this );
181             vlc_object_release( p_vout );
182         }
183     }
184
185     if( !p_vout )
186     {
187         msg_Dbg( p_this, "no usable vout present, spawning one" );
188
189         p_vout = vout_Create( p_this, i_width, i_height, i_chroma, i_aspect );
190     }
191
192     return p_vout;
193 }
194
195 /*****************************************************************************
196  * vout_Create: creates a new video output thread
197  *****************************************************************************
198  * This function creates a new video output thread, and returns a pointer
199  * to its description. On error, it returns NULL.
200  *****************************************************************************/
201 vout_thread_t * __vout_Create( vlc_object_t *p_parent,
202                                unsigned int i_width, unsigned int i_height,
203                                vlc_fourcc_t i_chroma, unsigned int i_aspect )
204 {
205     vout_thread_t  * p_vout;                            /* thread descriptor */
206     input_thread_t * p_input_thread;
207     int              i_index;                               /* loop variable */
208     char           * psz_plugin;
209     vlc_value_t      val, text;
210
211     /* Allocate descriptor */
212     p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
213     if( p_vout == NULL )
214     {
215         msg_Err( p_parent, "out of memory" );
216         return NULL;
217     }
218
219     /* Initialize pictures and subpictures - translation tables and functions
220      * will be initialized later in InitThread */
221     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++)
222     {
223         p_vout->p_picture[i_index].pf_lock = NULL;
224         p_vout->p_picture[i_index].pf_unlock = NULL;
225         p_vout->p_picture[i_index].i_status = FREE_PICTURE;
226         p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
227     }
228
229     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++)
230     {
231         p_vout->p_subpicture[i_index].i_status = FREE_SUBPICTURE;
232         p_vout->p_subpicture[i_index].i_type   = EMPTY_SUBPICTURE;
233     }
234
235     /* No images in the heap */
236     p_vout->i_heap_size = 0;
237
238     /* Initialize the rendering heap */
239     I_RENDERPICTURES = 0;
240     p_vout->render.i_width    = i_width;
241     p_vout->render.i_height   = i_height;
242     p_vout->render.i_chroma   = i_chroma;
243     p_vout->render.i_aspect   = i_aspect;
244
245     p_vout->render.i_rmask    = 0;
246     p_vout->render.i_gmask    = 0;
247     p_vout->render.i_bmask    = 0;
248
249     p_vout->render.i_last_used_pic = -1;
250     p_vout->render.b_allow_modify_pics = 1;
251
252     /* Zero the output heap */
253     I_OUTPUTPICTURES = 0;
254     p_vout->output.i_width    = 0;
255     p_vout->output.i_height   = 0;
256     p_vout->output.i_chroma   = 0;
257     p_vout->output.i_aspect   = 0;
258
259     p_vout->output.i_rmask    = 0;
260     p_vout->output.i_gmask    = 0;
261     p_vout->output.i_bmask    = 0;
262
263     /* Initialize misc stuff */
264     p_vout->i_changes    = 0;
265     p_vout->f_gamma      = 0;
266     p_vout->b_grayscale  = 0;
267     p_vout->b_info       = 0;
268     p_vout->b_interface  = 0;
269     p_vout->b_scale      = 1;
270     p_vout->b_fullscreen = 0;
271     p_vout->render_time  = 10;
272     p_vout->c_fps_samples = 0;
273     p_vout->b_filter_change = 0;
274
275     /* Mouse coordinates */
276     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
277     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
278     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
279     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
280     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
281     var_Create( p_vout, "key-pressed", VLC_VAR_STRING );
282
283     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
284     val.b_bool = VLC_TRUE;
285     var_Set( p_vout, "intf-change", val );
286
287     /* Initialize locks */
288     vlc_mutex_init( p_vout, &p_vout->picture_lock );
289     vlc_mutex_init( p_vout, &p_vout->subpicture_lock );
290     vlc_mutex_init( p_vout, &p_vout->change_lock );
291
292     /* Attach the new object now so we can use var inheritance below */
293     vlc_object_attach( p_vout, p_parent );
294
295     /* Create a few object variables we'll need later on */
296     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
297     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
298     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
299     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
300
301     p_vout->b_override_aspect = VLC_FALSE;
302
303     /* If the parent is not a VOUT object, that means we are at the start of
304      * the video output pipe */
305     if( p_parent->i_object_type != VLC_OBJECT_VOUT )
306     {
307         var_Get( p_vout, "aspect-ratio", &val );
308
309         /* Check whether the user tried to override aspect ratio */
310         if( val.psz_string )
311         {
312             unsigned int i_new_aspect = i_aspect;
313             char *psz_parser = strchr( val.psz_string, ':' );
314
315             if( psz_parser )
316             {
317                 *psz_parser++ = '\0';
318                 i_new_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
319                                                       / atoi( psz_parser );
320             }
321             else
322             {
323                 i_new_aspect = i_width * VOUT_ASPECT_FACTOR
324                                        * atof( val.psz_string )
325                                        / i_height;
326             }
327
328             free( val.psz_string );
329
330             if( i_new_aspect && i_new_aspect != i_aspect )
331             {
332                 int i_pgcd = ReduceHeight( i_new_aspect );
333
334                 msg_Dbg( p_vout, "overriding source aspect ratio to %i:%i",
335                          i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
336
337                 p_vout->render.i_aspect = i_new_aspect;
338
339                 p_vout->b_override_aspect = VLC_TRUE;
340             }
341         }
342
343         /* Look for the default filter configuration */
344         var_Create( p_vout, "filter", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
345         var_Get( p_vout, "filter", &val );
346         p_vout->psz_filter_chain = val.psz_string;
347     }
348     else
349     {
350         /* continue the parent's filter chain */
351         char *psz_end;
352
353         psz_end = strchr( ((vout_thread_t *)p_parent)->psz_filter_chain, ':' );
354         if( psz_end && *(psz_end+1) )
355             p_vout->psz_filter_chain = strdup( psz_end+1 );
356         else p_vout->psz_filter_chain = NULL;
357     }
358
359     /* Choose the video output module */
360     if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
361     {
362         var_Create( p_vout, "vout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
363         var_Get( p_vout, "vout", &val );
364         psz_plugin = val.psz_string;
365     }
366     else
367     {
368         /* the filter chain is a string list of filters separated by double
369          * colons */
370         char *psz_end;
371
372         psz_end = strchr( p_vout->psz_filter_chain, ':' );
373         if( psz_end )
374             psz_plugin = strndup( p_vout->psz_filter_chain,
375                                   psz_end - p_vout->psz_filter_chain );
376         else psz_plugin = strdup( p_vout->psz_filter_chain );
377     }
378
379     /* Initialize the dimensions of the video window */
380     InitWindowSize( p_vout, &p_vout->i_window_width,
381                     &p_vout->i_window_height );
382
383     /* Create the vout thread */
384     p_vout->p_module = module_Need( p_vout,
385                            ( p_vout->psz_filter_chain &&
386                                *p_vout->psz_filter_chain ) ?
387                            "video filter" : "video output",
388                            psz_plugin );
389
390     if( psz_plugin ) free( psz_plugin );
391     if( p_vout->p_module == NULL )
392     {
393         msg_Err( p_vout, "no suitable vout module" );
394         vlc_object_destroy( p_vout );
395         return NULL;
396     }
397
398     p_vout->p_text_renderer_module = module_Need( p_vout, "text renderer", 
399                                                   NULL );
400     if( p_vout->p_text_renderer_module == NULL )
401     {
402         msg_Warn( p_vout, "no suitable text renderer module" );
403         p_vout->pf_add_string = NULL;
404     }
405
406     /* Create a few object variables for interface interaction */
407     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
408     text.psz_string = _("Fullscreen");
409     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
410     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
411     if( val.b_bool )
412     {
413         /* user requested fullscreen */
414         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
415     }
416     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
417
418     var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
419     text.psz_string = _("Deinterlace");
420     var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
421     val.psz_string = ""; text.psz_string = _("Disable");
422     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
423     val.psz_string = "discard"; text.psz_string = _("Discard");
424     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
425     val.psz_string = "blend"; text.psz_string = _("Blend");
426     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
427     val.psz_string = "mean"; text.psz_string = _("Mean");
428     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
429     val.psz_string = "bob"; text.psz_string = _("Bob");
430     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
431     val.psz_string = "linear"; text.psz_string = _("Linear");
432     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
433     if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
434     {
435         var_Set( p_vout, "deinterlace", val );
436     }
437     var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
438
439     /* Calculate delay created by internal caching */
440     p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
441                                            VLC_OBJECT_INPUT, FIND_ANYWHERE );
442     if( p_input_thread )
443     {
444         p_vout->i_pts_delay = p_input_thread->i_pts_delay;
445         vlc_object_release( p_input_thread );
446     }
447     else
448     {
449         p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
450     }
451
452     if( vlc_thread_create( p_vout, "video output", RunThread,
453                            VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
454     {
455         msg_Err( p_vout, "out of memory" );
456         module_Unneed( p_vout, p_vout->p_module );
457         vlc_object_destroy( p_vout );
458         return NULL;
459     }
460
461     return p_vout;
462 }
463
464 /*****************************************************************************
465  * vout_Destroy: destroys a previously created video output
466  *****************************************************************************
467  * Destroy a terminated thread.
468  * The function will request a destruction of the specified thread. If pi_error
469  * is NULL, it will return once the thread is destroyed. Else, it will be
470  * update using one of the THREAD_* constants.
471  *****************************************************************************/
472 void vout_Destroy( vout_thread_t *p_vout )
473
474     vlc_object_t *p_playlist;
475     
476     /* Request thread destruction */
477     p_vout->b_die = VLC_TRUE;
478     vlc_thread_join( p_vout );
479
480     var_Destroy( p_vout, "intf-change" );
481
482     p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST, 
483                                   FIND_ANYWHERE );
484
485     /* Free structure */
486     vlc_object_destroy( p_vout );
487
488     /* If it was the last vout, tell the interface to show up */
489     if( p_playlist != NULL )
490     {
491         vout_thread_t *p_another_vout = vlc_object_find( p_playlist, 
492                                             VLC_OBJECT_VOUT, FIND_ANYWHERE );
493         if( p_another_vout == NULL )
494         {
495             vlc_value_t val;
496             val.b_bool = VLC_TRUE;
497             var_Set( p_playlist, "intf-show", val );
498         }
499         else
500         {
501             vlc_object_release( p_another_vout );
502         }
503         vlc_object_release( p_playlist );
504     }
505 }
506
507 /*****************************************************************************
508  * InitThread: initialize video output thread
509  *****************************************************************************
510  * This function is called from RunThread and performs the second step of the
511  * initialization. It returns 0 on success. Note that the thread's flag are not
512  * modified inside this function.
513  *****************************************************************************/
514 static int InitThread( vout_thread_t *p_vout )
515 {
516     int i, i_pgcd;
517
518     vlc_mutex_lock( &p_vout->change_lock );
519
520 #ifdef STATS
521     p_vout->c_loops = 0;
522 #endif
523
524     /* Initialize output method, it allocates direct buffers for us */
525     if( p_vout->pf_init( p_vout ) )
526     {
527         vlc_mutex_unlock( &p_vout->change_lock );
528         return VLC_EGENERIC;
529     }
530
531     if( !I_OUTPUTPICTURES )
532     {
533         msg_Err( p_vout, "plugin was unable to allocate at least "
534                          "one direct buffer" );
535         p_vout->pf_end( p_vout );
536         vlc_mutex_unlock( &p_vout->change_lock );
537         return VLC_EGENERIC;
538     }
539
540     if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
541     {
542         msg_Err( p_vout, "plugin allocated too many direct buffers, "
543                          "our internal buffers must have overflown." );
544         p_vout->pf_end( p_vout );
545         vlc_mutex_unlock( &p_vout->change_lock );
546         return VLC_EGENERIC;
547     }
548
549     msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
550
551 #if 0
552     if( !p_vout->psz_filter_chain )
553     {
554         char *psz_aspect = config_GetPsz( p_vout, "pixel-ratio" );
555
556         if( psz_aspect )
557         {
558             int i_new_aspect = p_vout->output.i_width * VOUT_ASPECT_FACTOR
559                                                       * atof( psz_aspect )
560                                                       / p_vout->output.i_height;
561             free( psz_aspect );
562
563             if( i_new_aspect && i_new_aspect != p_vout->output.i_aspect )
564             {
565                 int i_pgcd = ReduceHeight( i_new_aspect );
566                 msg_Dbg( p_vout, "output ratio forced to %i:%i\n",
567                          i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
568                 p_vout->output.i_aspect = i_new_aspect;
569             }
570         }
571     }
572 #endif
573
574     i_pgcd = ReduceHeight( p_vout->render.i_aspect );
575     msg_Dbg( p_vout,
576              "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
577              p_vout->render.i_width, p_vout->render.i_height,
578              p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
579              p_vout->render.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
580
581     i_pgcd = ReduceHeight( p_vout->output.i_aspect );
582     msg_Dbg( p_vout,
583              "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
584              p_vout->output.i_width, p_vout->output.i_height,
585              p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
586              p_vout->output.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
587
588     /* Calculate shifts from system-updated masks */
589     MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
590                  p_vout->output.i_rmask );
591     MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
592                  p_vout->output.i_gmask );
593     MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
594                  p_vout->output.i_bmask );
595
596     /* Check whether we managed to create direct buffers similar to
597      * the render buffers, ie same size and chroma */
598     if( ( p_vout->output.i_width == p_vout->render.i_width )
599      && ( p_vout->output.i_height == p_vout->render.i_height )
600      && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
601     {
602         /* Cool ! We have direct buffers, we can ask the decoder to
603          * directly decode into them ! Map the first render buffers to
604          * the first direct buffers, but keep the first direct buffer
605          * for memcpy operations */
606         p_vout->b_direct = 1;
607
608         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
609         {
610             if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
611                 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
612                 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
613             {
614                 /* We have enough direct buffers so there's no need to
615                  * try to use system memory buffers. */
616                 break;
617             }
618             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
619             I_RENDERPICTURES++;
620         }
621
622         msg_Dbg( p_vout, "direct render, mapping "
623                  "render pictures 0-%i to system pictures 1-%i",
624                  VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
625     }
626     else
627     {
628         /* Rats... Something is wrong here, we could not find an output
629          * plugin able to directly render what we decode. See if we can
630          * find a chroma plugin to do the conversion */
631         p_vout->b_direct = 0;
632
633         /* Choose the best module */
634         p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL );
635
636         if( p_vout->chroma.p_module == NULL )
637         {
638             msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
639                      (char*)&p_vout->render.i_chroma,
640                      (char*)&p_vout->output.i_chroma );
641             p_vout->pf_end( p_vout );
642             vlc_mutex_unlock( &p_vout->change_lock );
643             return VLC_EGENERIC;
644         }
645
646         msg_Dbg( p_vout, "indirect render, mapping "
647                  "render pictures 0-%i to system pictures %i-%i",
648                  VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
649                  I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
650
651         /* Append render buffers after the direct buffers */
652         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
653         {
654             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
655             I_RENDERPICTURES++;
656
657             /* Check if we have enough render pictures */
658             if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
659                 break;
660         }
661     }
662
663     /* Link pictures back to their heap */
664     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
665     {
666         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
667     }
668
669     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
670     {
671         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
672     }
673
674 /* XXX XXX mark thread ready */
675     return VLC_SUCCESS;
676 }
677
678 /*****************************************************************************
679  * RunThread: video output thread
680  *****************************************************************************
681  * Video output thread. This function does only returns when the thread is
682  * terminated. It handles the pictures arriving in the video heap and the
683  * display device events.
684  *****************************************************************************/
685 static void RunThread( vout_thread_t *p_vout)
686 {
687     int             i_index;                                /* index in heap */
688     int             i_idle_loops = 0;  /* loops without displaying a picture */
689     mtime_t         current_date;                            /* current date */
690     mtime_t         display_date;                            /* display date */
691
692     picture_t *     p_picture;                            /* picture pointer */
693     picture_t *     p_last_picture = NULL;                   /* last picture */
694     picture_t *     p_directbuffer;              /* direct buffer to display */
695
696     subpicture_t *  p_subpic;                          /* subpicture pointer */
697
698     /*
699      * Initialize thread
700      */
701     p_vout->b_error = InitThread( p_vout );
702
703     /* signal the creation of the vout */
704     vlc_thread_ready( p_vout );
705
706     if( p_vout->b_error )
707     {
708         /* Destroy thread structures allocated by Create and InitThread */
709         DestroyThread( p_vout );
710         return;
711     }
712
713     /*
714      * Main loop - it is not executed if an error occured during
715      * initialization
716      */
717     while( (!p_vout->b_die) && (!p_vout->b_error) )
718     {
719         /* Initialize loop variables */
720         p_picture = NULL;
721         display_date = 0;
722         current_date = mdate();
723
724 #if 0
725         p_vout->c_loops++;
726         if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
727         {
728             msg_Dbg( p_vout, "picture heap: %d/%d",
729                      I_RENDERPICTURES, p_vout->i_heap_size );
730         }
731 #endif
732
733         /*
734          * Find the picture to display (the one with the earliest date).
735          * This operation does not need lock, since only READY_PICTUREs
736          * are handled. */
737         for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
738         {
739             if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
740                 && ( (p_picture == NULL) ||
741                      (PP_RENDERPICTURE[i_index]->date < display_date) ) )
742             {
743                 p_picture = PP_RENDERPICTURE[i_index];
744                 display_date = p_picture->date;
745             }
746         }
747
748         if( p_picture )
749         {
750             /* If we met the last picture, parse again to see whether there is
751              * a more appropriate one. */
752             if( p_picture == p_last_picture )
753             {
754                 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
755                 {
756                     if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
757                         && (PP_RENDERPICTURE[i_index] != p_last_picture)
758                         && ((p_picture == p_last_picture) ||
759                             (PP_RENDERPICTURE[i_index]->date < display_date)) )
760                     {
761                         p_picture = PP_RENDERPICTURE[i_index];
762                         display_date = p_picture->date;
763                     }
764                 }
765             }
766
767             /* If we found better than the last picture, destroy it */
768             if( p_last_picture && p_picture != p_last_picture )
769             {
770                 vlc_mutex_lock( &p_vout->picture_lock );
771                 if( p_last_picture->i_refcount )
772                 {
773                     p_last_picture->i_status = DISPLAYED_PICTURE;
774                 }
775                 else
776                 {
777                     p_last_picture->i_status = DESTROYED_PICTURE;
778                     p_vout->i_heap_size--;
779                 }
780                 vlc_mutex_unlock( &p_vout->picture_lock );
781                 p_last_picture = NULL;
782             }
783
784             /* Compute FPS rate */
785             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
786                 = display_date;
787
788             if( !p_picture->b_force &&
789                 p_picture != p_last_picture &&
790                 display_date < current_date + p_vout->render_time )
791             {
792                 /* Picture is late: it will be destroyed and the thread
793                  * will directly choose the next picture */
794                 vlc_mutex_lock( &p_vout->picture_lock );
795                 if( p_picture->i_refcount )
796                 {
797                     /* Pretend we displayed the picture, but don't destroy
798                      * it since the decoder might still need it. */
799                     p_picture->i_status = DISPLAYED_PICTURE;
800                 }
801                 else
802                 {
803                     /* Destroy the picture without displaying it */
804                     p_picture->i_status = DESTROYED_PICTURE;
805                     p_vout->i_heap_size--;
806                 }
807                 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
808                                   current_date - display_date );
809                 vlc_mutex_unlock( &p_vout->picture_lock );
810
811                 continue;
812             }
813
814             if( display_date >
815                 current_date + p_vout->i_pts_delay +  VOUT_BOGUS_DELAY )
816             {
817                 /* Picture is waaay too early: it will be destroyed */
818                 vlc_mutex_lock( &p_vout->picture_lock );
819                 if( p_picture->i_refcount )
820                 {
821                     /* Pretend we displayed the picture, but don't destroy
822                      * it since the decoder might still need it. */
823                     p_picture->i_status = DISPLAYED_PICTURE;
824                 }
825                 else
826                 {
827                     /* Destroy the picture without displaying it */
828                     p_picture->i_status = DESTROYED_PICTURE;
829                     p_vout->i_heap_size--;
830                 }
831                 msg_Warn( p_vout, "vout warning: early picture skipped "
832                           "("I64Fd")", display_date - current_date
833                           - p_vout->i_pts_delay );
834                 vlc_mutex_unlock( &p_vout->picture_lock );
835
836                 continue;
837             }
838
839             if( display_date > current_date + VOUT_DISPLAY_DELAY )
840             {
841                 /* A picture is ready to be rendered, but its rendering date
842                  * is far from the current one so the thread will perform an
843                  * empty loop as if no picture were found. The picture state
844                  * is unchanged */
845                 p_picture    = NULL;
846                 display_date = 0;
847             }
848             else if( p_picture == p_last_picture )
849             {
850                 /* We are asked to repeat the previous picture, but we first
851                  * wait for a couple of idle loops */
852                 if( i_idle_loops < 4 )
853                 {
854                     p_picture    = NULL;
855                     display_date = 0;
856                 }
857                 else
858                 {
859                     /* We set the display date to something high, otherwise
860                      * we'll have lots of problems with late pictures */
861                     display_date = current_date + p_vout->render_time;
862                 }
863             }
864         }
865
866         if( p_picture == NULL )
867         {
868             i_idle_loops++;
869         }
870
871         /*
872          * Check for subpictures to display
873          */
874         p_subpic = vout_SortSubPictures( p_vout, display_date );
875
876         /*
877          * Perform rendering
878          */
879         p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
880
881         /*
882          * Call the plugin-specific rendering method if there is one
883          */
884         if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
885         {
886             /* Render the direct buffer returned by vout_RenderPicture */
887             p_vout->pf_render( p_vout, p_directbuffer );
888         }
889
890         /*
891          * Sleep, wake up
892          */
893         if( display_date != 0 && p_directbuffer != NULL )
894         {
895             /* Store render time using a sliding mean */
896             p_vout->render_time += mdate() - current_date;
897             p_vout->render_time >>= 1;
898         }
899
900         /* Give back change lock */
901         vlc_mutex_unlock( &p_vout->change_lock );
902
903         /* Sleep a while or until a given date */
904         if( display_date != 0 )
905         {
906             mwait( display_date - VOUT_MWAIT_TOLERANCE );
907         }
908         else
909         {
910             msleep( VOUT_IDLE_SLEEP );
911         }
912
913         /* On awakening, take back lock and send immediately picture
914          * to display. */
915         vlc_mutex_lock( &p_vout->change_lock );
916
917         /*
918          * Display the previously rendered picture
919          */
920         if( p_picture != NULL && p_directbuffer != NULL )
921         {
922             /* Display the direct buffer returned by vout_RenderPicture */
923             if( p_vout->pf_display )
924             {
925                 p_vout->pf_display( p_vout, p_directbuffer );
926             }
927
928             /* Reinitialize idle loop count */
929             i_idle_loops = 0;
930
931             /* Tell the vout this was the last picture and that it does not
932              * need to be forced anymore. */
933             p_last_picture = p_picture;
934             p_last_picture->b_force = 0;
935         }
936
937         /*
938          * Check events and manage thread
939          */
940         if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
941         {
942             /* A fatal error occured, and the thread must terminate
943              * immediately, without displaying anything - setting b_error to 1
944              * causes the immediate end of the main while() loop. */
945             p_vout->b_error = 1;
946         }
947
948         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
949         {
950             /* this must only happen when the vout plugin is incapable of
951              * rescaling the picture itself. In this case we need to destroy
952              * the current picture buffers and recreate new ones with the right
953              * dimensions */
954             int i;
955
956             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
957
958             p_vout->pf_end( p_vout );
959             for( i = 0; i < I_OUTPUTPICTURES; i++ )
960                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
961
962             I_OUTPUTPICTURES = 0;
963             if( p_vout->pf_init( p_vout ) )
964             {
965                 msg_Err( p_vout, "cannot resize display" );
966                 /* FIXME: pf_end will be called again in EndThread() */
967                 p_vout->b_error = 1;
968             }
969
970             /* Need to reinitialise the chroma plugin */
971             if( p_vout->chroma.p_module->pf_deactivate )
972                 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
973             p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
974         }
975     }
976
977     /*
978      * Error loop - wait until the thread destruction is requested
979      */
980     if( p_vout->b_error )
981     {
982         ErrorThread( p_vout );
983     }
984
985     /* End of thread */
986     EndThread( p_vout );
987
988     /* Destroy thread structures allocated by CreateThread */
989     DestroyThread( p_vout );
990 }
991
992 /*****************************************************************************
993  * ErrorThread: RunThread() error loop
994  *****************************************************************************
995  * This function is called when an error occured during thread main's loop. The
996  * thread can still receive feed, but must be ready to terminate as soon as
997  * possible.
998  *****************************************************************************/
999 static void ErrorThread( vout_thread_t *p_vout )
1000 {
1001     /* Wait until a `die' order */
1002     while( !p_vout->b_die )
1003     {
1004         /* Sleep a while */
1005         msleep( VOUT_IDLE_SLEEP );
1006     }
1007 }
1008
1009 /*****************************************************************************
1010  * EndThread: thread destruction
1011  *****************************************************************************
1012  * This function is called when the thread ends after a sucessful
1013  * initialization. It frees all ressources allocated by InitThread.
1014  *****************************************************************************/
1015 static void EndThread( vout_thread_t *p_vout )
1016 {
1017     int     i_index;                                        /* index in heap */
1018
1019 #ifdef STATS
1020     {
1021         struct tms cpu_usage;
1022         times( &cpu_usage );
1023
1024         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1025                  cpu_usage.tms_utime, cpu_usage.tms_stime );
1026     }
1027 #endif
1028
1029     if( !p_vout->b_direct )
1030     {
1031         module_Unneed( p_vout, p_vout->chroma.p_module );
1032     }
1033     
1034     /* Destroy all remaining pictures */
1035     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
1036     {
1037         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1038         {
1039             free( p_vout->p_picture[i_index].p_data_orig );
1040         }
1041     }
1042
1043     /* Destroy all remaining subpictures */
1044     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1045     {
1046         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1047         {
1048             vout_DestroySubPicture( p_vout,
1049                                     &p_vout->p_subpicture[i_index] );
1050         }
1051     }
1052
1053     if( p_vout->p_text_renderer_module )
1054         module_Unneed( p_vout, p_vout->p_text_renderer_module );
1055     
1056     /* Destroy translation tables */
1057     p_vout->pf_end( p_vout );
1058
1059     /* Release the change lock */
1060     vlc_mutex_unlock( &p_vout->change_lock );
1061 }
1062
1063 /*****************************************************************************
1064  * DestroyThread: thread destruction
1065  *****************************************************************************
1066  * This function is called when the thread ends. It frees all ressources
1067  * allocated by CreateThread. Status is available at this stage.
1068  *****************************************************************************/
1069 static void DestroyThread( vout_thread_t *p_vout )
1070 {
1071     /* Destroy the locks */
1072     vlc_mutex_destroy( &p_vout->picture_lock );
1073     vlc_mutex_destroy( &p_vout->subpicture_lock );
1074     vlc_mutex_destroy( &p_vout->change_lock );
1075
1076     /* Release the module */
1077     module_Unneed( p_vout, p_vout->p_module );
1078 }
1079
1080 /* following functions are local */
1081
1082 static int ReduceHeight( int i_ratio )
1083 {
1084     int i_dummy = VOUT_ASPECT_FACTOR;
1085     int i_pgcd  = 1;
1086  
1087     if( !i_ratio )
1088     {
1089         return i_pgcd;
1090     }
1091
1092     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1093     while( !(i_ratio & 1) && !(i_dummy & 1) )
1094     {
1095         i_ratio >>= 1;
1096         i_dummy >>= 1;
1097         i_pgcd  <<= 1;
1098     }
1099
1100     while( !(i_ratio % 3) && !(i_dummy % 3) )
1101     {
1102         i_ratio /= 3;
1103         i_dummy /= 3;
1104         i_pgcd  *= 3;
1105     }
1106
1107     while( !(i_ratio % 5) && !(i_dummy % 5) )
1108     {
1109         i_ratio /= 5;
1110         i_dummy /= 5;
1111         i_pgcd  *= 5;
1112     }
1113
1114     return i_pgcd;
1115 }
1116
1117 /*****************************************************************************
1118  * BinaryLog: computes the base 2 log of a binary value
1119  *****************************************************************************
1120  * This functions is used by MaskToShift, to get a bit index from a binary
1121  * value.
1122  *****************************************************************************/
1123 static int BinaryLog( uint32_t i )
1124 {
1125     int i_log = 0;
1126
1127     if( i == 0 ) return -31337;
1128
1129     if( i & 0xffff0000 ) i_log += 16;
1130     if( i & 0xff00ff00 ) i_log += 8;
1131     if( i & 0xf0f0f0f0 ) i_log += 4;
1132     if( i & 0xcccccccc ) i_log += 2;
1133     if( i & 0xaaaaaaaa ) i_log += 1;
1134
1135     return i_log;
1136 }
1137
1138 /*****************************************************************************
1139  * MaskToShift: transform a color mask into right and left shifts
1140  *****************************************************************************
1141  * This function is used for obtaining color shifts from masks.
1142  *****************************************************************************/
1143 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1144 {
1145     uint32_t i_low, i_high;            /* lower hand higher bits of the mask */
1146
1147     if( !i_mask )
1148     {
1149         *pi_left = *pi_right = 0;
1150         return;
1151     }
1152
1153     /* Get bits */
1154     i_low =  i_mask & (- (int32_t)i_mask);          /* lower bit of the mask */
1155     i_high = i_mask + i_low;                       /* higher bit of the mask */
1156
1157     /* Transform bits into an index */
1158     i_low =  BinaryLog (i_low);
1159     i_high = BinaryLog (i_high);
1160
1161     /* Update pointers and return */
1162     *pi_left =   i_low;
1163     *pi_right = (8 - i_high + i_low);
1164 }
1165
1166 /*****************************************************************************
1167  * InitWindowSize: find the initial dimensions the video window should have.
1168  *****************************************************************************
1169  * This function will check the "width", "height" and "zoom" config options and
1170  * will calculate the size that the video window should have.
1171  *****************************************************************************/
1172 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
1173                             int *pi_height )
1174 {
1175     vlc_value_t val;
1176     int i_width, i_height;
1177     uint64_t ll_zoom;
1178
1179 #define FP_FACTOR 1000                             /* our fixed point factor */
1180
1181     var_Get( p_vout, "width", &val );
1182     i_width = val.i_int;
1183     var_Get( p_vout, "height", &val );
1184     i_height = val.i_int;
1185     var_Get( p_vout, "zoom", &val );
1186     ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
1187
1188     if( i_width > 0 && i_height > 0)
1189     {
1190         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1191         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1192         return;
1193     }
1194     else if( i_width > 0 )
1195     {
1196         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1197         *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1198                             p_vout->render.i_aspect / FP_FACTOR );
1199         return;
1200     }
1201     else if( i_height > 0 )
1202     {
1203         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1204         *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1205                            VOUT_ASPECT_FACTOR / FP_FACTOR );
1206         return;
1207     }
1208
1209     if( p_vout->render.i_height * p_vout->render.i_aspect
1210         >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1211     {
1212         *pi_width = (int)( p_vout->render.i_height * ll_zoom
1213           * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1214         *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1215     }
1216     else
1217     {
1218         *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1219         *pi_height = (int)( p_vout->render.i_width * ll_zoom
1220           * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1221     }
1222
1223 #undef FP_FACTOR
1224 }
1225
1226 /*****************************************************************************
1227  * vout_VarCallback: generic callback for intf variables
1228  *****************************************************************************/
1229 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1230                       vlc_value_t old_value, vlc_value_t new_value,
1231                       void * unused )
1232 {
1233     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1234     vlc_value_t val;
1235     val.b_bool = VLC_TRUE;
1236     var_Set( p_vout, "intf-change", val );
1237     return VLC_SUCCESS;
1238 }
1239
1240 /*****************************************************************************
1241  * object variables callbacks: a bunch of object variables are used by the
1242  * interfaces to interact with the vout.
1243  *****************************************************************************/
1244 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1245                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1246 {
1247     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1248     input_thread_t *p_input;
1249     vlc_value_t val;
1250
1251     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1252
1253     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1254                                                  FIND_PARENT );
1255     if( p_input )
1256     {
1257         /* Modify input as well because the vout might have to be restarted */
1258         var_Create( p_input, "fullscreen", VLC_VAR_BOOL );
1259         var_Set( p_input, "fullscreen", newval );
1260
1261         vlc_object_release( p_input );
1262     }
1263
1264     val.b_bool = VLC_TRUE;
1265     var_Set( p_vout, "intf-change", val );
1266     return VLC_SUCCESS;
1267 }
1268
1269 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1270                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1271 {
1272     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1273     input_thread_t *p_input;
1274     vlc_value_t val;
1275
1276     char *psz_mode = newval.psz_string;
1277     char *psz_filter;
1278     unsigned int  i;
1279
1280     psz_filter = config_GetPsz( p_vout, "filter" );
1281
1282     if( !psz_mode || !*psz_mode )
1283     {
1284         config_PutPsz( p_vout, "filter", "" );
1285     }
1286     else
1287     {
1288         if( !psz_filter || !*psz_filter )
1289         {
1290             config_PutPsz( p_vout, "filter", "deinterlace" );
1291         }
1292         else
1293         {
1294             if( strstr( psz_filter, "deinterlace" ) == NULL )
1295             {
1296                 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
1297                 strcat( psz_filter, ",deinterlace" );
1298             }
1299             config_PutPsz( p_vout, "filter", psz_filter );
1300         }
1301     }
1302
1303     if( psz_filter ) free( psz_filter );
1304
1305
1306     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1307                                                  FIND_PARENT );
1308     if( !p_input ) return VLC_EGENERIC;
1309
1310     if( psz_mode && *psz_mode )
1311     {
1312         val.psz_string = psz_mode;
1313         var_Set( p_vout, "deinterlace-mode", val );
1314         /* Modify input as well because the vout might have to be restarted */
1315         var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1316         var_Set( p_input, "deinterlace-mode", val );
1317     }
1318
1319     /* now restart all video streams */
1320     vlc_mutex_lock( &p_input->stream.stream_lock );
1321
1322     p_vout->b_filter_change = VLC_TRUE;
1323
1324 #define ES p_input->stream.pp_es[i]
1325
1326     for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1327     {
1328         if( ( ES->i_cat == VIDEO_ES ) && ES->p_decoder_fifo != NULL )
1329         {
1330             input_UnselectES( p_input, ES );
1331             input_SelectES( p_input, ES );
1332         }
1333 #undef ES
1334     }
1335     vlc_mutex_unlock( &p_input->stream.stream_lock );
1336
1337     vlc_object_release( p_input );
1338
1339     val.b_bool = VLC_TRUE;
1340     var_Set( p_vout, "intf-change", val );
1341     return VLC_SUCCESS;
1342 }