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