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