]> git.sesse.net Git - vlc/blob - src/video_output/video_output.c
FSF address change.
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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_filter_change )
178         {
179             /* We are not interested in this format, close this vout */
180             vlc_object_detach( p_vout );
181             vlc_object_release( p_vout );
182             vout_Destroy( p_vout );
183             p_vout = NULL;
184         }
185         else
186         {
187             /* This video output is cool! Hijack it. */
188             vlc_object_detach( p_vout );
189             spu_Attach( p_vout->p_spu, p_this, VLC_TRUE );
190             vlc_object_attach( p_vout, p_this );
191             vlc_object_release( p_vout );
192         }
193     }
194
195     if( !p_vout )
196     {
197         msg_Dbg( p_this, "no usable vout present, spawning one" );
198
199         p_vout = vout_Create( p_this, p_fmt );
200     }
201
202     return p_vout;
203 }
204
205 /*****************************************************************************
206  * vout_Create: creates a new video output thread
207  *****************************************************************************
208  * This function creates a new video output thread, and returns a pointer
209  * to its description. On error, it returns NULL.
210  *****************************************************************************/
211 vout_thread_t * __vout_Create( vlc_object_t *p_parent, video_format_t *p_fmt )
212 {
213     vout_thread_t  * p_vout;                            /* thread descriptor */
214     input_thread_t * p_input_thread;
215     int              i_index;                               /* loop variable */
216     char           * psz_plugin;
217     vlc_value_t      val, text;
218
219     unsigned int i_width = p_fmt->i_width;
220     unsigned int i_height = p_fmt->i_height;
221     vlc_fourcc_t i_chroma = p_fmt->i_chroma;
222     unsigned int i_aspect = p_fmt->i_aspect;
223
224     /* Allocate descriptor */
225     p_vout = vlc_object_create( p_parent, VLC_OBJECT_VOUT );
226     if( p_vout == NULL )
227     {
228         msg_Err( p_parent, "out of memory" );
229         return NULL;
230     }
231
232     stats_Create( p_vout, "displayed_pictures", VLC_VAR_INTEGER,
233                                                  STATS_COUNTER );
234     stats_Create( p_vout, "lost_pictures", VLC_VAR_INTEGER, STATS_COUNTER );
235
236     /* Initialize pictures - translation tables and functions
237      * will be initialized later in InitThread */
238     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++)
239     {
240         p_vout->p_picture[i_index].pf_lock = NULL;
241         p_vout->p_picture[i_index].pf_unlock = NULL;
242         p_vout->p_picture[i_index].i_status = FREE_PICTURE;
243         p_vout->p_picture[i_index].i_type   = EMPTY_PICTURE;
244         p_vout->p_picture[i_index].b_slow   = 0;
245     }
246
247     /* No images in the heap */
248     p_vout->i_heap_size = 0;
249
250     /* Initialize the rendering heap */
251     I_RENDERPICTURES = 0;
252
253     vlc_ureduce( &p_fmt->i_sar_num, &p_fmt->i_sar_den,
254                  p_fmt->i_sar_num, p_fmt->i_sar_den, 50000 );
255     p_vout->fmt_render        = *p_fmt;   /* FIXME palette */
256     p_vout->fmt_in            = *p_fmt;   /* FIXME palette */
257
258     p_vout->render.i_width    = i_width;
259     p_vout->render.i_height   = i_height;
260     p_vout->render.i_chroma   = i_chroma;
261     p_vout->render.i_aspect   = i_aspect;
262
263     p_vout->render.i_rmask    = 0;
264     p_vout->render.i_gmask    = 0;
265     p_vout->render.i_bmask    = 0;
266
267     p_vout->render.i_last_used_pic = -1;
268     p_vout->render.b_allow_modify_pics = 1;
269
270     /* Zero the output heap */
271     I_OUTPUTPICTURES = 0;
272     p_vout->output.i_width    = 0;
273     p_vout->output.i_height   = 0;
274     p_vout->output.i_chroma   = 0;
275     p_vout->output.i_aspect   = 0;
276
277     p_vout->output.i_rmask    = 0;
278     p_vout->output.i_gmask    = 0;
279     p_vout->output.i_bmask    = 0;
280
281     /* Initialize misc stuff */
282     p_vout->i_changes    = 0;
283     p_vout->f_gamma      = 0;
284     p_vout->b_grayscale  = 0;
285     p_vout->b_info       = 0;
286     p_vout->b_interface  = 0;
287     p_vout->b_scale      = 1;
288     p_vout->b_fullscreen = 0;
289     p_vout->i_alignment  = 0;
290     p_vout->render_time  = 10;
291     p_vout->c_fps_samples = 0;
292     p_vout->b_filter_change = 0;
293     p_vout->pf_control = 0;
294     p_vout->p_parent_intf = 0;
295     p_vout->i_par_num = p_vout->i_par_den = 1;
296
297     /* Initialize locks */
298     vlc_mutex_init( p_vout, &p_vout->picture_lock );
299     vlc_mutex_init( p_vout, &p_vout->change_lock );
300
301     /* Mouse coordinates */
302     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
303     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
304     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
305     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
306     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
307
308     /* Initialize subpicture unit */
309     p_vout->p_spu = spu_Create( p_vout );
310     spu_Attach( p_vout->p_spu, p_parent, VLC_TRUE );
311
312     /* Attach the new object now so we can use var inheritance below */
313     vlc_object_attach( p_vout, p_parent );
314
315     spu_Init( p_vout->p_spu );
316
317     /* Take care of some "interface/control" related initialisations */
318     vout_IntfInit( p_vout );
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             /* XXX: config_GetInt is slow, but this kind of frame dropping
794              * should not happen that often. */
795             if( !p_picture->b_force &&
796                 p_picture != p_last_picture &&
797                 display_date < current_date + p_vout->render_time &&
798                 config_GetInt( p_vout, "skip-frames" ) )
799             {
800                 /* Picture is late: it will be destroyed and the thread
801                  * will directly choose the next picture */
802                 vlc_mutex_lock( &p_vout->picture_lock );
803                 if( p_picture->i_refcount )
804                 {
805                     /* Pretend we displayed the picture, but don't destroy
806                      * it since the decoder might still need it. */
807                     p_picture->i_status = DISPLAYED_PICTURE;
808                 }
809                 else
810                 {
811                     /* Destroy the picture without displaying it */
812                     p_picture->i_status = DESTROYED_PICTURE;
813                     p_vout->i_heap_size--;
814                 }
815                 msg_Warn( p_vout, "late picture skipped ("I64Fd")",
816                                   current_date - display_date );
817                 stats_UpdateInteger( p_vout, "lost_pictures", 1 );
818                 vlc_mutex_unlock( &p_vout->picture_lock );
819
820                 continue;
821             }
822
823             if( display_date >
824                 current_date + p_vout->i_pts_delay + VOUT_BOGUS_DELAY )
825             {
826                 /* Picture is waaay too early: it will be destroyed */
827                 vlc_mutex_lock( &p_vout->picture_lock );
828                 if( p_picture->i_refcount )
829                 {
830                     /* Pretend we displayed the picture, but don't destroy
831                      * it since the decoder might still need it. */
832                     p_picture->i_status = DISPLAYED_PICTURE;
833                 }
834                 else
835                 {
836                     /* Destroy the picture without displaying it */
837                     p_picture->i_status = DESTROYED_PICTURE;
838                     p_vout->i_heap_size--;
839                 }
840                 stats_UpdateInteger( p_vout, "lost_pictures", 1 );
841                 msg_Warn( p_vout, "vout warning: early picture skipped "
842                           "("I64Fd")", display_date - current_date
843                           - p_vout->i_pts_delay );
844                 vlc_mutex_unlock( &p_vout->picture_lock );
845
846                 continue;
847             }
848
849             if( display_date > current_date + VOUT_DISPLAY_DELAY )
850             {
851                 /* A picture is ready to be rendered, but its rendering date
852                  * is far from the current one so the thread will perform an
853                  * empty loop as if no picture were found. The picture state
854                  * is unchanged */
855                 p_picture    = NULL;
856                 display_date = 0;
857             }
858             else if( p_picture == p_last_picture )
859             {
860                 /* We are asked to repeat the previous picture, but we first
861                  * wait for a couple of idle loops */
862                 if( i_idle_loops < 4 )
863                 {
864                     p_picture    = NULL;
865                     display_date = 0;
866                 }
867                 else
868                 {
869                     /* We set the display date to something high, otherwise
870                      * we'll have lots of problems with late pictures */
871                     display_date = current_date + p_vout->render_time;
872                 }
873             }
874         }
875
876         if( p_picture == NULL )
877         {
878             i_idle_loops++;
879         }
880
881         if( p_picture && p_vout->b_snapshot )
882         {
883             p_vout->b_snapshot = VLC_FALSE;
884             vout_Snapshot( p_vout, p_picture );
885         }
886
887         /*
888          * Check for subpictures to display
889          */
890         if( display_date > 0 )
891         {
892             p_subpic = spu_SortSubpictures( p_vout->p_spu, display_date );
893         }
894
895         /*
896          * Perform rendering
897          */
898         stats_UpdateInteger( p_vout, "displayed_pictures", 1 );
899         p_directbuffer = vout_RenderPicture( p_vout, p_picture, p_subpic );
900
901         /*
902          * Call the plugin-specific rendering method if there is one
903          */
904         if( p_picture != NULL && p_directbuffer != NULL && p_vout->pf_render )
905         {
906             /* Render the direct buffer returned by vout_RenderPicture */
907             p_vout->pf_render( p_vout, p_directbuffer );
908         }
909
910         /*
911          * Sleep, wake up
912          */
913         if( display_date != 0 && p_directbuffer != NULL )
914         {
915             mtime_t current_render_time = mdate() - current_date;
916             /* if render time is very large we don't include it in the mean */
917             if( current_render_time < p_vout->render_time +
918                 VOUT_DISPLAY_DELAY )
919             {
920                 /* Store render time using a sliding mean weighting to
921                  * current value in a 3 to 1 ratio*/
922                 p_vout->render_time *= 3;
923                 p_vout->render_time += current_render_time;
924                 p_vout->render_time >>= 2;
925             }
926         }
927
928         /* Give back change lock */
929         vlc_mutex_unlock( &p_vout->change_lock );
930
931         /* Sleep a while or until a given date */
932         if( display_date != 0 )
933         {
934             /* If there are filters in the chain, better give them the picture
935              * in advance */
936             if( !p_vout->psz_filter_chain || !*p_vout->psz_filter_chain )
937             {
938                 mwait( display_date - VOUT_MWAIT_TOLERANCE );
939             }
940         }
941         else
942         {
943             msleep( VOUT_IDLE_SLEEP );
944         }
945
946         /* On awakening, take back lock and send immediately picture
947          * to display. */
948         vlc_mutex_lock( &p_vout->change_lock );
949
950         /*
951          * Display the previously rendered picture
952          */
953         if( p_picture != NULL && p_directbuffer != NULL )
954         {
955             /* Display the direct buffer returned by vout_RenderPicture */
956             if( p_vout->pf_display )
957             {
958                 p_vout->pf_display( p_vout, p_directbuffer );
959             }
960
961             /* Tell the vout this was the last picture and that it does not
962              * need to be forced anymore. */
963             p_last_picture = p_picture;
964             p_last_picture->b_force = 0;
965         }
966
967         if( p_picture != NULL )
968         {
969             /* Reinitialize idle loop count */
970             i_idle_loops = 0;
971         }
972
973         /*
974          * Check events and manage thread
975          */
976         if( p_vout->pf_manage && p_vout->pf_manage( p_vout ) )
977         {
978             /* A fatal error occurred, and the thread must terminate
979              * immediately, without displaying anything - setting b_error to 1
980              * causes the immediate end of the main while() loop. */
981             p_vout->b_error = 1;
982         }
983
984         if( p_vout->i_changes & VOUT_SIZE_CHANGE )
985         {
986             /* this must only happen when the vout plugin is incapable of
987              * rescaling the picture itself. In this case we need to destroy
988              * the current picture buffers and recreate new ones with the right
989              * dimensions */
990             int i;
991
992             p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
993
994             p_vout->pf_end( p_vout );
995             for( i = 0; i < I_OUTPUTPICTURES; i++ )
996                  p_vout->p_picture[ i ].i_status = FREE_PICTURE;
997
998             I_OUTPUTPICTURES = 0;
999             if( p_vout->pf_init( p_vout ) )
1000             {
1001                 msg_Err( p_vout, "cannot resize display" );
1002                 /* FIXME: pf_end will be called again in EndThread() */
1003                 p_vout->b_error = 1;
1004             }
1005
1006             /* Need to reinitialise the chroma plugin */
1007             if( p_vout->chroma.p_module )
1008             {
1009                 if( p_vout->chroma.p_module->pf_deactivate )
1010                     p_vout->chroma.p_module->pf_deactivate( VLC_OBJECT(p_vout) );
1011                 p_vout->chroma.p_module->pf_activate( VLC_OBJECT(p_vout) );
1012             }
1013         }
1014
1015         if( p_vout->i_changes & VOUT_PICTURE_BUFFERS_CHANGE )
1016         {
1017             /* This happens when the picture buffers need to be recreated.
1018              * This is useful on multimonitor displays for instance.
1019              *
1020              * Warning: This only works when the vout creates only 1 picture
1021              * buffer!! */
1022             p_vout->i_changes &= ~VOUT_PICTURE_BUFFERS_CHANGE;
1023
1024             if( !p_vout->b_direct )
1025             {
1026                 module_Unneed( p_vout, p_vout->chroma.p_module );
1027             }
1028
1029             vlc_mutex_lock( &p_vout->picture_lock );
1030
1031             p_vout->pf_end( p_vout );
1032
1033             I_OUTPUTPICTURES = I_RENDERPICTURES = 0;
1034
1035             p_vout->b_error = InitThread( p_vout );
1036
1037             vlc_mutex_unlock( &p_vout->picture_lock );
1038         }
1039     }
1040
1041     /*
1042      * Error loop - wait until the thread destruction is requested
1043      */
1044     if( p_vout->b_error )
1045     {
1046         ErrorThread( p_vout );
1047     }
1048
1049     /* End of thread */
1050     EndThread( p_vout );
1051
1052     /* Destroy thread structures allocated by CreateThread */
1053     DestroyThread( p_vout );
1054 }
1055
1056 /*****************************************************************************
1057  * ErrorThread: RunThread() error loop
1058  *****************************************************************************
1059  * This function is called when an error occurred during thread main's loop.
1060  * The thread can still receive feed, but must be ready to terminate as soon
1061  * as possible.
1062  *****************************************************************************/
1063 static void ErrorThread( vout_thread_t *p_vout )
1064 {
1065     /* Wait until a `die' order */
1066     while( !p_vout->b_die )
1067     {
1068         /* Sleep a while */
1069         msleep( VOUT_IDLE_SLEEP );
1070     }
1071 }
1072
1073 /*****************************************************************************
1074  * EndThread: thread destruction
1075  *****************************************************************************
1076  * This function is called when the thread ends after a sucessful
1077  * initialization. It frees all resources allocated by InitThread.
1078  *****************************************************************************/
1079 static void EndThread( vout_thread_t *p_vout )
1080 {
1081     int     i_index;                                        /* index in heap */
1082
1083 #ifdef STATS
1084     {
1085         struct tms cpu_usage;
1086         times( &cpu_usage );
1087
1088         msg_Dbg( p_vout, "cpu usage (user: %d, system: %d)",
1089                  cpu_usage.tms_utime, cpu_usage.tms_stime );
1090     }
1091 #endif
1092
1093     if( !p_vout->b_direct )
1094     {
1095         module_Unneed( p_vout, p_vout->chroma.p_module );
1096     }
1097
1098     /* Destroy all remaining pictures */
1099     for( i_index = 0; i_index < 2 * VOUT_MAX_PICTURES + 1; i_index++ )
1100     {
1101         if ( p_vout->p_picture[i_index].i_type == MEMORY_PICTURE )
1102         {
1103             free( p_vout->p_picture[i_index].p_data_orig );
1104         }
1105     }
1106
1107     /* Destroy subpicture unit */
1108     spu_Attach( p_vout->p_spu, VLC_OBJECT(p_vout), VLC_FALSE );
1109     spu_Destroy( p_vout->p_spu );
1110
1111     /* Destroy translation tables */
1112     p_vout->pf_end( p_vout );
1113
1114     /* Release the change lock */
1115     vlc_mutex_unlock( &p_vout->change_lock );
1116 }
1117
1118 /*****************************************************************************
1119  * DestroyThread: thread destruction
1120  *****************************************************************************
1121  * This function is called when the thread ends. It frees all ressources
1122  * allocated by CreateThread. Status is available at this stage.
1123  *****************************************************************************/
1124 static void DestroyThread( vout_thread_t *p_vout )
1125 {
1126     /* Destroy the locks */
1127     vlc_mutex_destroy( &p_vout->picture_lock );
1128     vlc_mutex_destroy( &p_vout->change_lock );
1129
1130     /* Release the module */
1131     if( p_vout && p_vout->p_module )
1132     {
1133         module_Unneed( p_vout, p_vout->p_module );
1134     }
1135 }
1136
1137 /* following functions are local */
1138
1139 static int ReduceHeight( int i_ratio )
1140 {
1141     int i_dummy = VOUT_ASPECT_FACTOR;
1142     int i_pgcd  = 1;
1143
1144     if( !i_ratio )
1145     {
1146         return i_pgcd;
1147     }
1148
1149     /* VOUT_ASPECT_FACTOR is (2^7 * 3^3 * 5^3), we just check for 2, 3 and 5 */
1150     while( !(i_ratio & 1) && !(i_dummy & 1) )
1151     {
1152         i_ratio >>= 1;
1153         i_dummy >>= 1;
1154         i_pgcd  <<= 1;
1155     }
1156
1157     while( !(i_ratio % 3) && !(i_dummy % 3) )
1158     {
1159         i_ratio /= 3;
1160         i_dummy /= 3;
1161         i_pgcd  *= 3;
1162     }
1163
1164     while( !(i_ratio % 5) && !(i_dummy % 5) )
1165     {
1166         i_ratio /= 5;
1167         i_dummy /= 5;
1168         i_pgcd  *= 5;
1169     }
1170
1171     return i_pgcd;
1172 }
1173
1174 static void AspectRatio( int i_aspect, int *i_aspect_x, int *i_aspect_y )
1175 {
1176     unsigned int i_pgcd = ReduceHeight( i_aspect );
1177     *i_aspect_x = i_aspect / i_pgcd;
1178     *i_aspect_y = VOUT_ASPECT_FACTOR / i_pgcd;
1179 }
1180
1181 /*****************************************************************************
1182  * BinaryLog: computes the base 2 log of a binary value
1183  *****************************************************************************
1184  * This functions is used by MaskToShift, to get a bit index from a binary
1185  * value.
1186  *****************************************************************************/
1187 static int BinaryLog( uint32_t i )
1188 {
1189     int i_log = 0;
1190
1191     if( i == 0 ) return -31337;
1192
1193     if( i & 0xffff0000 ) i_log += 16;
1194     if( i & 0xff00ff00 ) i_log += 8;
1195     if( i & 0xf0f0f0f0 ) i_log += 4;
1196     if( i & 0xcccccccc ) i_log += 2;
1197     if( i & 0xaaaaaaaa ) i_log += 1;
1198
1199     return i_log;
1200 }
1201
1202 /*****************************************************************************
1203  * MaskToShift: transform a color mask into right and left shifts
1204  *****************************************************************************
1205  * This function is used for obtaining color shifts from masks.
1206  *****************************************************************************/
1207 static void MaskToShift( int *pi_left, int *pi_right, uint32_t i_mask )
1208 {
1209     uint32_t i_low, i_high;            /* lower hand higher bits of the mask */
1210
1211     if( !i_mask )
1212     {
1213         *pi_left = *pi_right = 0;
1214         return;
1215     }
1216
1217     /* Get bits */
1218     i_low =  i_mask & (- (int32_t)i_mask);          /* lower bit of the mask */
1219     i_high = i_mask + i_low;                       /* higher bit of the mask */
1220
1221     /* Transform bits into an index */
1222     i_low =  BinaryLog (i_low);
1223     i_high = BinaryLog (i_high);
1224
1225     /* Update pointers and return */
1226     *pi_left =   i_low;
1227     *pi_right = (8 - i_high + i_low);
1228 }
1229
1230 /*****************************************************************************
1231  * vout_VarCallback: generic callback for intf variables
1232  *****************************************************************************/
1233 int vout_VarCallback( vlc_object_t * p_this, const char * psz_variable,
1234                       vlc_value_t old_value, vlc_value_t new_value,
1235                       void * unused )
1236 {
1237     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1238     vlc_value_t val;
1239     val.b_bool = VLC_TRUE;
1240     var_Set( p_vout, "intf-change", val );
1241     return VLC_SUCCESS;
1242 }
1243
1244 /*****************************************************************************
1245  * Helper thread for object variables callbacks.
1246  * Only used to avoid deadlocks when using the video embedded mode.
1247  *****************************************************************************/
1248 typedef struct suxor_thread_t
1249 {
1250     VLC_COMMON_MEMBERS
1251     input_thread_t *p_input;
1252
1253 } suxor_thread_t;
1254
1255 static void SuxorRestartVideoES( suxor_thread_t *p_this )
1256 {
1257     vlc_value_t val;
1258
1259     vlc_thread_ready( p_this );
1260
1261     /* Now restart current video stream */
1262     var_Get( p_this->p_input, "video-es", &val );
1263     if( val.i_int >= 0 )
1264     {
1265         vlc_value_t val_es;
1266         val_es.i_int = -VIDEO_ES;
1267         var_Set( p_this->p_input, "video-es", val_es );
1268         var_Set( p_this->p_input, "video-es", val );
1269     }
1270
1271     vlc_object_release( p_this->p_input );
1272
1273 #ifdef WIN32
1274     CloseHandle( p_this->thread_id );
1275 #endif
1276
1277     vlc_object_destroy( p_this );
1278 }
1279
1280 /*****************************************************************************
1281  * object variables callbacks: a bunch of object variables are used by the
1282  * interfaces to interact with the vout.
1283  *****************************************************************************/
1284 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
1285                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1286 {
1287     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1288     input_thread_t *p_input;
1289     vlc_value_t val;
1290
1291     char *psz_mode = newval.psz_string;
1292     char *psz_filter, *psz_deinterlace = NULL;
1293
1294     var_Get( p_vout, "vout-filter", &val );
1295     psz_filter = val.psz_string;
1296     if( psz_filter ) psz_deinterlace = strstr( psz_filter, "deinterlace" );
1297
1298     if( !psz_mode || !*psz_mode )
1299     {
1300         if( psz_deinterlace )
1301         {
1302             char *psz_src = psz_deinterlace + sizeof("deinterlace") - 1;
1303             if( psz_src[0] == ':' ) psz_src++;
1304             memmove( psz_deinterlace, psz_src, strlen(psz_src) + 1 );
1305         }
1306     }
1307     else if( !psz_deinterlace )
1308     {
1309         psz_filter = realloc( psz_filter, strlen( psz_filter ) +
1310                               sizeof(":deinterlace") );
1311         if( psz_filter && *psz_filter ) strcat( psz_filter, ":" );
1312         strcat( psz_filter, "deinterlace" );
1313     }
1314
1315     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1316                                                  FIND_PARENT );
1317     if( !p_input ) return VLC_EGENERIC;
1318
1319     if( psz_mode && *psz_mode )
1320     {
1321         /* Modify input as well because the vout might have to be restarted */
1322         val.psz_string = psz_mode;
1323         var_Create( p_input, "deinterlace-mode", VLC_VAR_STRING );
1324         var_Set( p_input, "deinterlace-mode", val );
1325     }
1326     vlc_object_release( p_input );
1327
1328     val.b_bool = VLC_TRUE;
1329     var_Set( p_vout, "intf-change", val );
1330
1331     val.psz_string = psz_filter;
1332     var_Set( p_vout, "vout-filter", val );
1333     if( psz_filter ) free( psz_filter );
1334
1335     return VLC_SUCCESS;
1336 }
1337
1338 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1339                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1340 {
1341     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1342     input_thread_t *p_input;
1343     vlc_value_t val;
1344
1345     p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
1346                                                  FIND_PARENT );
1347     if (!p_input)
1348     {
1349         msg_Err( p_vout, "Input not found" );
1350         return( VLC_EGENERIC );
1351     }
1352
1353     val.b_bool = VLC_TRUE;
1354     var_Set( p_vout, "intf-change", val );
1355
1356     /* Modify input as well because the vout might have to be restarted */
1357     val.psz_string = newval.psz_string;
1358     var_Create( p_input, "vout-filter", VLC_VAR_STRING );
1359
1360     var_Set( p_input, "vout-filter", val );
1361
1362     /* Now restart current video stream */
1363     var_Get( p_input, "video-es", &val );
1364     if( val.i_int >= 0 )
1365     {
1366         suxor_thread_t *p_suxor =
1367             vlc_object_create( p_vout, sizeof(suxor_thread_t) );
1368         p_suxor->p_input = p_input;
1369         p_vout->b_filter_change = VLC_TRUE;
1370         vlc_object_yield( p_input );
1371         vlc_thread_create( p_suxor, "suxor", SuxorRestartVideoES,
1372                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1373     }
1374
1375     vlc_object_release( p_input );
1376
1377     return VLC_SUCCESS;
1378 }