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