]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
880891bfbb0767ea9a6ee53a50101fe471f31fe6
[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.230 2003/07/23 22:01:25 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     p_vout->p_text_renderer_module = module_Need( p_vout, "text renderer", 
398                                                   NULL );
399     if( p_vout->p_text_renderer_module == NULL )
400     {
401         msg_Warn( p_vout, "no suitable text renderer module" );
402         p_vout->pf_add_string = NULL;
403     }
404     /* Create a few object variables for interface interaction */
405     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
406     text.psz_string = _("Fullscreen");
407     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
408     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
409     if( val.b_bool )
410     {
411         /* user requested fullscreen */
412         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
413     }
414     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
415
416     var_Create( p_vout, "deinterlace", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
417     text.psz_string = _("Deinterlace");
418     var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
419     val.psz_string = ""; text.psz_string = _("Disable");
420     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
421     val.psz_string = "discard"; text.psz_string = _("Discard");
422     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
423     val.psz_string = "blend"; text.psz_string = _("Blend");
424     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
425     val.psz_string = "mean"; text.psz_string = _("Mean");
426     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
427     val.psz_string = "bob"; text.psz_string = _("Bob");
428     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
429     val.psz_string = "linear"; text.psz_string = _("Linear");
430     var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
431     if( var_Get( p_vout, "deinterlace-mode", &val ) == VLC_SUCCESS )
432     {
433         var_Set( p_vout, "deinterlace", val );
434     }
435     var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
436
437     /* Calculate delay created by internal caching */
438     p_input_thread = (input_thread_t *)vlc_object_find( p_vout,
439                                            VLC_OBJECT_INPUT, FIND_ANYWHERE );
440     if( p_input_thread )
441     {
442         p_vout->i_pts_delay = p_input_thread->i_pts_delay;
443         vlc_object_release( p_input_thread );
444     }
445     else
446     {
447         p_vout->i_pts_delay = DEFAULT_PTS_DELAY;
448     }
449
450     if( vlc_thread_create( p_vout, "video output", RunThread,
451                            VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
452     {
453         msg_Err( p_vout, "out of memory" );
454         module_Unneed( p_vout, p_vout->p_module );
455         vlc_object_destroy( p_vout );
456         return NULL;
457     }
458
459     return p_vout;
460 }
461
462 /*****************************************************************************
463  * vout_Destroy: destroys a previously created video output
464  *****************************************************************************
465  * Destroy a terminated thread.
466  * The function will request a destruction of the specified thread. If pi_error
467  * is NULL, it will return once the thread is destroyed. Else, it will be
468  * update using one of the THREAD_* constants.
469  *****************************************************************************/
470 void vout_Destroy( vout_thread_t *p_vout )
471
472     vlc_object_t *p_playlist;
473     
474     /* Request thread destruction */
475     p_vout->b_die = VLC_TRUE;
476     vlc_thread_join( p_vout );
477
478     var_Destroy( p_vout, "intf-change" );
479
480     p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST, 
481                                   FIND_ANYWHERE );
482
483     /* Free structure */
484     vlc_object_destroy( p_vout );
485
486     /* If it was the last vout, tell the interface to show up */
487     if( p_playlist != NULL )
488     {
489         vout_thread_t *p_another_vout = vlc_object_find( p_playlist, 
490                                             VLC_OBJECT_VOUT, FIND_ANYWHERE );
491         if( p_another_vout == NULL )
492         {
493             vlc_value_t val;
494             val.b_bool = VLC_TRUE;
495             var_Set( p_playlist, "intf-show", val );
496         }
497         else
498         {
499             vlc_object_release( p_another_vout );
500         }
501         vlc_object_release( p_playlist );
502     }
503 }
504
505 /*****************************************************************************
506  * InitThread: initialize video output thread
507  *****************************************************************************
508  * This function is called from RunThread and performs the second step of the
509  * initialization. It returns 0 on success. Note that the thread's flag are not
510  * modified inside this function.
511  *****************************************************************************/
512 static int InitThread( vout_thread_t *p_vout )
513 {
514     int i, i_pgcd;
515
516     vlc_mutex_lock( &p_vout->change_lock );
517
518 #ifdef STATS
519     p_vout->c_loops = 0;
520 #endif
521
522     /* Initialize output method, it allocates direct buffers for us */
523     if( p_vout->pf_init( p_vout ) )
524     {
525         vlc_mutex_unlock( &p_vout->change_lock );
526         return VLC_EGENERIC;
527     }
528
529     if( !I_OUTPUTPICTURES )
530     {
531         msg_Err( p_vout, "plugin was unable to allocate at least "
532                          "one direct buffer" );
533         p_vout->pf_end( p_vout );
534         vlc_mutex_unlock( &p_vout->change_lock );
535         return VLC_EGENERIC;
536     }
537
538     if( I_OUTPUTPICTURES > VOUT_MAX_PICTURES )
539     {
540         msg_Err( p_vout, "plugin allocated too many direct buffers, "
541                          "our internal buffers must have overflown." );
542         p_vout->pf_end( p_vout );
543         vlc_mutex_unlock( &p_vout->change_lock );
544         return VLC_EGENERIC;
545     }
546
547     msg_Dbg( p_vout, "got %i direct buffer(s)", I_OUTPUTPICTURES );
548
549 #if 0
550     if( !p_vout->psz_filter_chain )
551     {
552         char *psz_aspect = config_GetPsz( p_vout, "pixel-ratio" );
553
554         if( psz_aspect )
555         {
556             int i_new_aspect = p_vout->output.i_width * VOUT_ASPECT_FACTOR
557                                                       * atof( psz_aspect )
558                                                       / p_vout->output.i_height;
559             free( psz_aspect );
560
561             if( i_new_aspect && i_new_aspect != p_vout->output.i_aspect )
562             {
563                 int i_pgcd = ReduceHeight( i_new_aspect );
564                 msg_Dbg( p_vout, "output ratio forced to %i:%i\n",
565                          i_new_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
566                 p_vout->output.i_aspect = i_new_aspect;
567             }
568         }
569     }
570 #endif
571
572     i_pgcd = ReduceHeight( p_vout->render.i_aspect );
573     msg_Dbg( p_vout,
574              "picture in %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
575              p_vout->render.i_width, p_vout->render.i_height,
576              p_vout->render.i_chroma, (char*)&p_vout->render.i_chroma,
577              p_vout->render.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
578
579     i_pgcd = ReduceHeight( p_vout->output.i_aspect );
580     msg_Dbg( p_vout,
581              "picture out %ix%i, chroma 0x%.8x (%4.4s), aspect ratio %i:%i",
582              p_vout->output.i_width, p_vout->output.i_height,
583              p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma,
584              p_vout->output.i_aspect / i_pgcd, VOUT_ASPECT_FACTOR / i_pgcd );
585
586     /* Calculate shifts from system-updated masks */
587     MaskToShift( &p_vout->output.i_lrshift, &p_vout->output.i_rrshift,
588                  p_vout->output.i_rmask );
589     MaskToShift( &p_vout->output.i_lgshift, &p_vout->output.i_rgshift,
590                  p_vout->output.i_gmask );
591     MaskToShift( &p_vout->output.i_lbshift, &p_vout->output.i_rbshift,
592                  p_vout->output.i_bmask );
593
594     /* Check whether we managed to create direct buffers similar to
595      * the render buffers, ie same size and chroma */
596     if( ( p_vout->output.i_width == p_vout->render.i_width )
597      && ( p_vout->output.i_height == p_vout->render.i_height )
598      && ( vout_ChromaCmp( p_vout->output.i_chroma, p_vout->render.i_chroma ) ) )
599     {
600         /* Cool ! We have direct buffers, we can ask the decoder to
601          * directly decode into them ! Map the first render buffers to
602          * the first direct buffers, but keep the first direct buffer
603          * for memcpy operations */
604         p_vout->b_direct = 1;
605
606         for( i = 1; i < VOUT_MAX_PICTURES; i++ )
607         {
608             if( p_vout->p_picture[ i ].i_type != DIRECT_PICTURE &&
609                 I_RENDERPICTURES >= VOUT_MIN_DIRECT_PICTURES - 1 &&
610                 p_vout->p_picture[ i - 1 ].i_type == DIRECT_PICTURE )
611             {
612                 /* We have enough direct buffers so there's no need to
613                  * try to use system memory buffers. */
614                 break;
615             }
616             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
617             I_RENDERPICTURES++;
618         }
619
620         msg_Dbg( p_vout, "direct render, mapping "
621                  "render pictures 0-%i to system pictures 1-%i",
622                  VOUT_MAX_PICTURES - 2, VOUT_MAX_PICTURES - 1 );
623     }
624     else
625     {
626         /* Rats... Something is wrong here, we could not find an output
627          * plugin able to directly render what we decode. See if we can
628          * find a chroma plugin to do the conversion */
629         p_vout->b_direct = 0;
630
631         /* Choose the best module */
632         p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL );
633
634         if( p_vout->chroma.p_module == NULL )
635         {
636             msg_Err( p_vout, "no chroma module for %4.4s to %4.4s",
637                      (char*)&p_vout->render.i_chroma,
638                      (char*)&p_vout->output.i_chroma );
639             p_vout->pf_end( p_vout );
640             vlc_mutex_unlock( &p_vout->change_lock );
641             return VLC_EGENERIC;
642         }
643
644         msg_Dbg( p_vout, "indirect render, mapping "
645                  "render pictures 0-%i to system pictures %i-%i",
646                  VOUT_MAX_PICTURES - 1, I_OUTPUTPICTURES,
647                  I_OUTPUTPICTURES + VOUT_MAX_PICTURES - 1 );
648
649         /* Append render buffers after the direct buffers */
650         for( i = I_OUTPUTPICTURES; i < 2 * VOUT_MAX_PICTURES; i++ )
651         {
652             PP_RENDERPICTURE[ I_RENDERPICTURES ] = &p_vout->p_picture[ i ];
653             I_RENDERPICTURES++;
654
655             /* Check if we have enough render pictures */
656             if( I_RENDERPICTURES == VOUT_MAX_PICTURES )
657                 break;
658         }
659     }
660
661     /* Link pictures back to their heap */
662     for( i = 0 ; i < I_RENDERPICTURES ; i++ )
663     {
664         PP_RENDERPICTURE[ i ]->p_heap = &p_vout->render;
665     }
666
667     for( i = 0 ; i < I_OUTPUTPICTURES ; i++ )
668     {
669         PP_OUTPUTPICTURE[ i ]->p_heap = &p_vout->output;
670     }
671
672 /* XXX XXX mark thread ready */
673     return VLC_SUCCESS;
674 }
675
676 /*****************************************************************************
677  * RunThread: video output thread
678  *****************************************************************************
679  * Video output thread. This function does only returns when the thread is
680  * terminated. It handles the pictures arriving in the video heap and the
681  * display device events.
682  *****************************************************************************/
683 static void RunThread( vout_thread_t *p_vout)
684 {
685     int             i_index;                                /* index in heap */
686     int             i_idle_loops = 0;  /* loops without displaying a picture */
687     mtime_t         current_date;                            /* current date */
688     mtime_t         display_date;                            /* display date */
689
690     picture_t *     p_picture;                            /* picture pointer */
691     picture_t *     p_last_picture = NULL;                   /* last picture */
692     picture_t *     p_directbuffer;              /* direct buffer to display */
693
694     subpicture_t *  p_subpic;                          /* subpicture pointer */
695
696     /*
697      * Initialize thread
698      */
699     p_vout->b_error = InitThread( p_vout );
700
701     /* signal the creation of the vout */
702     vlc_thread_ready( p_vout );
703
704     if( p_vout->b_error )
705     {
706         /* Destroy thread structures allocated by Create and InitThread */
707         DestroyThread( p_vout );
708         return;
709     }
710
711     /*
712      * Main loop - it is not executed if an error occured during
713      * initialization
714      */
715     while( (!p_vout->b_die) && (!p_vout->b_error) )
716     {
717         /* Initialize loop variables */
718         p_picture = NULL;
719         display_date = 0;
720         current_date = mdate();
721
722 #if 0
723         p_vout->c_loops++;
724         if( !(p_vout->c_loops % VOUT_STATS_NB_LOOPS) )
725         {
726             msg_Dbg( p_vout, "picture heap: %d/%d",
727                      I_RENDERPICTURES, p_vout->i_heap_size );
728         }
729 #endif
730
731         /*
732          * Find the picture to display (the one with the earliest date).
733          * This operation does not need lock, since only READY_PICTUREs
734          * are handled. */
735         for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
736         {
737             if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
738                 && ( (p_picture == NULL) ||
739                      (PP_RENDERPICTURE[i_index]->date < display_date) ) )
740             {
741                 p_picture = PP_RENDERPICTURE[i_index];
742                 display_date = p_picture->date;
743             }
744         }
745
746         if( p_picture )
747         {
748             /* If we met the last picture, parse again to see whether there is
749              * a more appropriate one. */
750             if( p_picture == p_last_picture )
751             {
752                 for( i_index = 0; i_index < I_RENDERPICTURES; i_index++ )
753                 {
754                     if( (PP_RENDERPICTURE[i_index]->i_status == READY_PICTURE)
755                         && (PP_RENDERPICTURE[i_index] != p_last_picture)
756                         && ((p_picture == p_last_picture) ||
757                             (PP_RENDERPICTURE[i_index]->date < display_date)) )
758                     {
759                         p_picture = PP_RENDERPICTURE[i_index];
760                         display_date = p_picture->date;
761                     }
762                 }
763             }
764
765             /* If we found better than the last picture, destroy it */
766             if( p_last_picture && p_picture != p_last_picture )
767             {
768                 vlc_mutex_lock( &p_vout->picture_lock );
769                 if( p_last_picture->i_refcount )
770                 {
771                     p_last_picture->i_status = DISPLAYED_PICTURE;
772                 }
773                 else
774                 {
775                     p_last_picture->i_status = DESTROYED_PICTURE;
776                     p_vout->i_heap_size--;
777                 }
778                 vlc_mutex_unlock( &p_vout->picture_lock );
779                 p_last_picture = NULL;
780             }
781
782             /* Compute FPS rate */
783             p_vout->p_fps_sample[ p_vout->c_fps_samples++ % VOUT_FPS_SAMPLES ]
784                 = display_date;
785
786             if( !p_picture->b_force &&
787                 p_picture != p_last_picture &&
788                 display_date < current_date + p_vout->render_time )
789             {
790                 /* Picture is late: it will be destroyed and the thread
791                  * will directly choose the next picture */
792                 vlc_mutex_lock( &p_vout->picture_lock );
793                 if( p_picture->i_refcount )
794                 {
795                     /* Pretend we displayed the picture, but don't destroy
796                      * it since the decoder might still need it. */
797                     p_picture->i_status = DISPLAYED_PICTURE;
798                 }
799                 else
800                 {
801                     /* Destroy the picture without displaying it */
802                     p_picture->i_status = DESTROYED_PICTURE;
803                     p_vout->i_heap_size--;
804                 }
805                 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
806                                   current_date - display_date );
807                 vlc_mutex_unlock( &p_vout->picture_lock );
808
809                 continue;
810             }
811
812             if( display_date >
813                 current_date + p_vout->i_pts_delay +  VOUT_BOGUS_DELAY )
814             {
815                 /* Picture is waaay too early: it will be destroyed */
816                 vlc_mutex_lock( &p_vout->picture_lock );
817                 if( p_picture->i_refcount )
818                 {
819                     /* Pretend we displayed the picture, but don't destroy
820                      * it since the decoder might still need it. */
821                     p_picture->i_status = DISPLAYED_PICTURE;
822                 }
823                 else
824                 {
825                     /* Destroy the picture without displaying it */
826                     p_picture->i_status = DESTROYED_PICTURE;
827                     p_vout->i_heap_size--;
828                 }
829                 msg_Warn( p_vout, "vout warning: early picture skipped "
830                           "("I64Fd")", display_date - current_date
831                           - p_vout->i_pts_delay );
832                 vlc_mutex_unlock( &p_vout->picture_lock );
833
834                 continue;
835             }
836
837             if( display_date > current_date + VOUT_DISPLAY_DELAY )
838             {
839                 /* A picture is ready to be rendered, but its rendering date
840                  * is far from the current one so the thread will perform an
841                  * empty loop as if no picture were found. The picture state
842                  * is unchanged */
843                 p_picture    = NULL;
844                 display_date = 0;
845             }
846             else if( p_picture == p_last_picture )
847             {
848                 /* We are asked to repeat the previous picture, but we first
849                  * wait for a couple of idle loops */
850                 if( i_idle_loops < 4 )
851                 {
852                     p_picture    = NULL;
853                     display_date = 0;
854                 }
855                 else
856                 {
857                     /* We set the display date to something high, otherwise
858                      * we'll have lots of problems with late pictures */
859                     display_date = current_date + p_vout->render_time;
860                 }
861             }
862         }
863
864         if( p_picture == NULL )
865         {
866             i_idle_loops++;
867         }
868
869         /*
870          * Check for subpictures to display
871          */
872         p_subpic = vout_SortSubPictures( p_vout, display_date );
873
874         /*
875          * Perform rendering
876          */
877         p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
878
879         /*
880          * Call the plugin-specific rendering method if there is one
881          */
882         if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
883         {
884             /* Render the direct buffer returned by vout_RenderPicture */
885             p_vout->pf_render( p_vout, p_directbuffer );
886         }
887
888         /*
889          * Sleep, wake up
890          */
891         if( display_date != 0 && p_directbuffer != NULL )
892         {
893             /* Store render time using a sliding mean */
894             p_vout->render_time += mdate() - current_date;
895             p_vout->render_time >>= 1;
896         }
897
898         /* Give back change lock */
899         vlc_mutex_unlock( &p_vout->change_lock );
900
901         /* Sleep a while or until a given date */
902         if( display_date != 0 )
903         {
904             mwait( display_date - VOUT_MWAIT_TOLERANCE );
905         }
906         else
907         {
908             msleep( VOUT_IDLE_SLEEP );
909         }
910
911         /* On awakening, take back lock and send immediately picture
912          * to display. */
913         vlc_mutex_lock( &p_vout->change_lock );
914
915         /*
916          * Display the previously rendered picture
917          */
918         if( p_picture != NULL && p_directbuffer != NULL )
919         {
920             /* Display the direct buffer returned by vout_RenderPicture */
921             if( p_vout->pf_display )
922             {
923                 p_vout->pf_display( p_vout, p_directbuffer );
924             }
925
926             /* Reinitialize idle loop count */
927             i_idle_loops = 0;
928
929             /* Tell the vout this was the last picture and that it does not
930              * need to be forced anymore. */
931             p_last_picture = p_picture;
932             p_last_picture->b_force = 0;
933         }
934
935         /*
936          * Check events and manage thread
937          */
938         if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
939         {
940             /* A fatal error occured, and the thread must terminate
941              * immediately, without displaying anything - setting b_error to 1
942              * causes the immediate end of the main while() loop. */
943             p_vout->b_error = 1;
944         }
945
946         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
947         {
948             /* this must only happen when the vout plugin is incapable of
949              * rescaling the picture itself. In this case we need to destroy
950              * the current picture buffers and recreate new ones with the right
951              * dimensions */
952             int i;
953
954             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
955
956             p_vout->pf_end( p_vout );
957             for( i = 0; i < I_OUTPUTPICTURES; i++ )
958                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
959
960             I_OUTPUTPICTURES = 0;
961             if( p_vout->pf_init( p_vout ) )
962             {
963                 msg_Err( p_vout, "cannot resize display" );
964                 /* FIXME: pf_end will be called again in EndThread() */
965                 p_vout->b_error = 1;
966             }
967
968             /* Need to reinitialise the chroma plugin */
969             if( p_vout->chroma.p_module->pf_deactivate )
970                 p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
971             p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
972         }
973     }
974
975     /*
976      * Error loop - wait until the thread destruction is requested
977      */
978     if( p_vout->b_error )
979     {
980         ErrorThread( p_vout );
981     }
982
983     /* End of thread */
984     EndThread( p_vout );
985
986     /* Destroy thread structures allocated by CreateThread */
987     DestroyThread( p_vout );
988 }
989
990 /*****************************************************************************
991  * ErrorThread: RunThread() error loop
992  *****************************************************************************
993  * This function is called when an error occured during thread main's loop. The
994  * thread can still receive feed, but must be ready to terminate as soon as
995  * possible.
996  *****************************************************************************/
997 static void ErrorThread( vout_thread_t *p_vout )
998 {
999     /* Wait until a `die' order */
1000     while( !p_vout->b_die )
1001     {
1002         /* Sleep a while */
1003         msleep( VOUT_IDLE_SLEEP );
1004     }
1005 }
1006
1007 /*****************************************************************************
1008  * EndThread: thread destruction
1009  *****************************************************************************
1010  * This function is called when the thread ends after a sucessful
1011  * initialization. It frees all ressources allocated by InitThread.
1012  *****************************************************************************/
1013 static void EndThread( vout_thread_t *p_vout )
1014 {
1015     int     i_index;                                        /* index in heap */
1016
1017 #ifdef STATS
1018     {
1019         struct tms cpu_usage;
1020         times( &cpu_usage );
1021
1022         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1023                  cpu_usage.tms_utime, cpu_usage.tms_stime );
1024     }
1025 #endif
1026
1027     if( !p_vout->b_direct )
1028     {
1029         module_Unneed( p_vout, p_vout->chroma.p_module );
1030     }
1031     
1032     /* Destroy all remaining pictures */
1033     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES; i_index++ )
1034     {
1035         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1036         {
1037             free( p_vout->p_picture[i_index].p_data_orig );
1038         }
1039     }
1040
1041     /* Destroy all remaining subpictures */
1042     for( i_index = 0; i_index < VOUT_MAX_SUBPICTURES; i_index++ )
1043     {
1044         if( p_vout->p_subpicture[i_index].i_status != FREE_SUBPICTURE )
1045         {
1046             vout_DestroySubPicture( p_vout,
1047                                     &p_vout->p_subpicture[i_index] );
1048         }
1049     }
1050
1051     module_Unneed( p_vout, p_vout->p_text_renderer_module );
1052     
1053     /* Destroy translation tables */
1054     p_vout->pf_end( p_vout );
1055
1056     /* Release the change lock */
1057     vlc_mutex_unlock( &p_vout->change_lock );
1058 }
1059
1060 /*****************************************************************************
1061  * DestroyThread: thread destruction
1062  *****************************************************************************
1063  * This function is called when the thread ends. It frees all ressources
1064  * allocated by CreateThread. Status is available at this stage.
1065  *****************************************************************************/
1066 static void DestroyThread( vout_thread_t *p_vout )
1067 {
1068     /* Destroy the locks */
1069     vlc_mutex_destroy( &p_vout->picture_lock );
1070     vlc_mutex_destroy( &p_vout->subpicture_lock );
1071     vlc_mutex_destroy( &p_vout->change_lock );
1072
1073     /* Release the module */
1074     module_Unneed( p_vout, p_vout->p_module );
1075 }
1076
1077 /* following functions are local */
1078
1079 static int ReduceHeight( int i_ratio )
1080 {
1081     int i_dummy = VOUT_ASPECT_FACTOR;
1082     int i_pgcd  = 1;
1083  
1084     if( !i_ratio )
1085     {
1086         return i_pgcd;
1087     }
1088
1089     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1090     while( !(i_ratio & 1) && !(i_dummy & 1) )
1091     {
1092         i_ratio >>= 1;
1093         i_dummy >>= 1;
1094         i_pgcd  <<= 1;
1095     }
1096
1097     while( !(i_ratio % 3) && !(i_dummy % 3) )
1098     {
1099         i_ratio /= 3;
1100         i_dummy /= 3;
1101         i_pgcd  *= 3;
1102     }
1103
1104     while( !(i_ratio % 5) && !(i_dummy % 5) )
1105     {
1106         i_ratio /= 5;
1107         i_dummy /= 5;
1108         i_pgcd  *= 5;
1109     }
1110
1111     return i_pgcd;
1112 }
1113
1114 /*****************************************************************************
1115  * BinaryLog: computes the base 2 log of a binary value
1116  *****************************************************************************
1117  * This functions is used by MaskToShift, to get a bit index from a binary
1118  * value.
1119  *****************************************************************************/
1120 static int BinaryLog( uint32_t i )
1121 {
1122     int i_log = 0;
1123
1124     if( i == 0 ) return -31337;
1125
1126     if( i & 0xffff0000 ) i_log += 16;
1127     if( i & 0xff00ff00 ) i_log += 8;
1128     if( i & 0xf0f0f0f0 ) i_log += 4;
1129     if( i & 0xcccccccc ) i_log += 2;
1130     if( i & 0xaaaaaaaa ) i_log += 1;
1131
1132     return i_log;
1133 }
1134
1135 /*****************************************************************************
1136  * MaskToShift: transform a color mask into right and left shifts
1137  *****************************************************************************
1138  * This function is used for obtaining color shifts from masks.
1139  *****************************************************************************/
1140 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1141 {
1142     uint32_t i_low, i_high;            /* lower hand higher bits of the mask */
1143
1144     if( !i_mask )
1145     {
1146         *pi_left = *pi_right = 0;
1147         return;
1148     }
1149
1150     /* Get bits */
1151     i_low =  i_mask & (- (int32_t)i_mask);          /* lower bit of the mask */
1152     i_high = i_mask + i_low;                       /* higher bit of the mask */
1153
1154     /* Transform bits into an index */
1155     i_low =  BinaryLog (i_low);
1156     i_high = BinaryLog (i_high);
1157
1158     /* Update pointers and return */
1159     *pi_left =   i_low;
1160     *pi_right = (8 - i_high + i_low);
1161 }
1162
1163 /*****************************************************************************
1164  * InitWindowSize: find the initial dimensions the video window should have.
1165  *****************************************************************************
1166  * This function will check the "width", "height" and "zoom" config options and
1167  * will calculate the size that the video window should have.
1168  *****************************************************************************/
1169 static void InitWindowSize( vout_thread_t *p_vout, int *pi_width,
1170                             int *pi_height )
1171 {
1172     vlc_value_t val;
1173     int i_width, i_height;
1174     uint64_t ll_zoom;
1175
1176 #define FP_FACTOR 1000                             /* our fixed point factor */
1177
1178     var_Get( p_vout, "width", &val );
1179     i_width = val.i_int;
1180     var_Get( p_vout, "height", &val );
1181     i_height = val.i_int;
1182     var_Get( p_vout, "zoom", &val );
1183     ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
1184
1185     if( i_width > 0 && i_height > 0)
1186     {
1187         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1188         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1189         return;
1190     }
1191     else if( i_width > 0 )
1192     {
1193         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
1194         *pi_height = (int)( i_width * ll_zoom * VOUT_ASPECT_FACTOR /
1195                             p_vout->render.i_aspect / FP_FACTOR );
1196         return;
1197     }
1198     else if( i_height > 0 )
1199     {
1200         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
1201         *pi_width = (int)( i_height * ll_zoom * p_vout->render.i_aspect /
1202                            VOUT_ASPECT_FACTOR / FP_FACTOR );
1203         return;
1204     }
1205
1206     if( p_vout->render.i_height * p_vout->render.i_aspect
1207         >= p_vout->render.i_width * VOUT_ASPECT_FACTOR )
1208     {
1209         *pi_width = (int)( p_vout->render.i_height * ll_zoom
1210           * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR / FP_FACTOR );
1211         *pi_height = (int)( p_vout->render.i_height * ll_zoom / FP_FACTOR );
1212     }
1213     else
1214     {
1215         *pi_width = (int)( p_vout->render.i_width * ll_zoom / FP_FACTOR );
1216         *pi_height = (int)( p_vout->render.i_width * ll_zoom
1217           * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect / FP_FACTOR );
1218     }
1219
1220 #undef FP_FACTOR
1221 }
1222
1223 /*****************************************************************************
1224  * vout_VarCallback: generic callback for intf variables
1225  *****************************************************************************/
1226 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1227                       vlc_value_t old_value, vlc_value_t new_value,
1228                       void * unused )
1229 {
1230     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1231     vlc_value_t val;
1232     val.b_bool = VLC_TRUE;
1233     var_Set( p_vout, "intf-change", val );
1234     return VLC_SUCCESS;
1235 }
1236
1237 /*****************************************************************************
1238  * object variables callbacks: a bunch of object variables are used by the
1239  * interfaces to interact with the vout.
1240  *****************************************************************************/
1241 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1242                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1243 {
1244     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1245     input_thread_t *p_input;
1246     vlc_value_t val;
1247
1248     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1249
1250     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1251                                                  FIND_PARENT );
1252     if( p_input )
1253     {
1254         /* Modify input as well because the vout might have to be restarted */
1255         var_Create( p_input, "fullscreen", VLC_VAR_BOOL );
1256         var_Set( p_input, "fullscreen", newval );
1257
1258         vlc_object_release( p_input );
1259     }
1260
1261     val.b_bool = VLC_TRUE;
1262     var_Set( p_vout, "intf-change", val );
1263     return VLC_SUCCESS;
1264 }
1265
1266 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1267                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1268 {
1269     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1270     input_thread_t *p_input;
1271     vlc_value_t val;
1272
1273     char *psz_mode = newval.psz_string;
1274     char *psz_filter;
1275     unsigned int  i;
1276
1277     psz_filter = config_GetPsz( p_vout, "filter" );
1278
1279     if( !psz_mode || !*psz_mode )
1280     {
1281         config_PutPsz( p_vout, "filter", "" );
1282     }
1283     else
1284     {
1285         if( !psz_filter || !*psz_filter )
1286         {
1287             config_PutPsz( p_vout, "filter", "deinterlace" );
1288         }
1289         else
1290         {
1291             if( strstr( psz_filter, "deinterlace" ) == NULL )
1292             {
1293                 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
1294                 strcat( psz_filter, ",deinterlace" );
1295             }
1296             config_PutPsz( p_vout, "filter", psz_filter );
1297         }
1298     }
1299
1300     if( psz_filter ) free( psz_filter );
1301
1302
1303     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1304                                                  FIND_PARENT );
1305     if( !p_input ) return VLC_EGENERIC;
1306
1307     if( psz_mode && *psz_mode )
1308     {
1309         val.psz_string = psz_mode;
1310         var_Set( p_vout, "deinterlace-mode", val );
1311         /* Modify input as well because the vout might have to be restarted */
1312         var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1313         var_Set( p_input, "deinterlace-mode", val );
1314     }
1315
1316     /* now restart all video streams */
1317     vlc_mutex_lock( &p_input->stream.stream_lock );
1318
1319     p_vout->b_filter_change = VLC_TRUE;
1320
1321 #define ES p_input->stream.pp_es[i]
1322
1323     for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1324     {
1325         if( ( ES->i_cat == VIDEO_ES ) && ES->p_decoder_fifo != NULL )
1326         {
1327             input_UnselectES( p_input, ES );
1328             input_SelectES( p_input, ES );
1329         }
1330 #undef ES
1331     }
1332     vlc_mutex_unlock( &p_input->stream.stream_lock );
1333
1334     vlc_object_release( p_input );
1335
1336     val.b_bool = VLC_TRUE;
1337     var_Set( p_vout, "intf-change", val );
1338     return VLC_SUCCESS;
1339 }