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