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