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