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