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