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