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