]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
* backport of [13046] , refs #416
[vlc] / src / video_output / vout_intf.c
1 /*****************************************************************************
2  * vout_intf.c : video output interface
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 #include "vlc_video.h"
33 #include "video_output.h"
34 #include "vlc_image.h"
35 #include "vlc_spu.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
41
42 /* Object variables callbacks */
43 static int ZoomCallback( vlc_object_t *, char const *,
44                          vlc_value_t, vlc_value_t, void * );
45 static int CropCallback( vlc_object_t *, char const *,
46                          vlc_value_t, vlc_value_t, void * );
47 static int AspectCallback( vlc_object_t *, char const *,
48                            vlc_value_t, vlc_value_t, void * );
49 static int OnTopCallback( vlc_object_t *, char const *,
50                           vlc_value_t, vlc_value_t, void * );
51 static int FullscreenCallback( vlc_object_t *, char const *,
52                                vlc_value_t, vlc_value_t, void * );
53 static int SnapshotCallback( vlc_object_t *, char const *,
54                              vlc_value_t, vlc_value_t, void * );
55
56 /*****************************************************************************
57  * vout_RequestWindow: Create/Get a video window if possible.
58  *****************************************************************************
59  * This function looks for the main interface and tries to request
60  * a new video window. If it fails then the vout will still need to create the
61  * window by itself.
62  *****************************************************************************/
63 void *vout_RequestWindow( vout_thread_t *p_vout,
64                           int *pi_x_hint, int *pi_y_hint,
65                           unsigned int *pi_width_hint,
66                           unsigned int *pi_height_hint )
67 {
68     intf_thread_t *p_intf = NULL;
69     vlc_list_t *p_list;
70     void *p_window;
71     vlc_value_t val;
72     int i;
73
74     /* Small kludge */
75     if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
76
77     /* Get requested coordinates */
78     var_Get( p_vout, "video-x", &val );
79     *pi_x_hint = val.i_int ;
80     var_Get( p_vout, "video-y", &val );
81     *pi_y_hint = val.i_int;
82
83     *pi_width_hint = p_vout->i_window_width;
84     *pi_height_hint = p_vout->i_window_height;
85
86     /* Check whether someone provided us with a window ID */
87     var_Get( p_vout->p_vlc, "drawable", &val );
88     if( val.i_int ) return (void *)val.i_int;
89
90     /* Find if the main interface supports embedding */
91     p_list = vlc_list_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
92     if( !p_list ) return NULL;
93
94     for( i = 0; i < p_list->i_count; i++ )
95     {
96         p_intf = (intf_thread_t *)p_list->p_values[i].p_object;
97         if( p_intf->b_block && p_intf->pf_request_window ) break;
98         p_intf = NULL;
99     }
100
101     if( !p_intf )
102     {
103         vlc_list_release( p_list );
104         return NULL;
105     }
106
107     vlc_object_yield( p_intf );
108     vlc_list_release( p_list );
109
110     p_window = p_intf->pf_request_window( p_intf, p_vout, pi_x_hint, pi_y_hint,
111                                           pi_width_hint, pi_height_hint );
112
113     if( !p_window ) vlc_object_release( p_intf );
114     else p_vout->p_parent_intf = p_intf;
115
116     return p_window;
117 }
118
119 void vout_ReleaseWindow( vout_thread_t *p_vout, void *p_window )
120 {
121     intf_thread_t *p_intf = p_vout->p_parent_intf;
122
123     if( !p_intf ) return;
124
125     vlc_mutex_lock( &p_intf->object_lock );
126     if( p_intf->b_dead )
127     {
128         vlc_mutex_unlock( &p_intf->object_lock );
129         return;
130     }
131
132     if( !p_intf->pf_release_window )
133     {
134         msg_Err( p_vout, "no pf_release_window");
135         vlc_mutex_unlock( &p_intf->object_lock );
136         vlc_object_release( p_intf );
137         return;
138     }
139
140     p_intf->pf_release_window( p_intf, p_window );
141
142     p_vout->p_parent_intf = NULL;
143     vlc_mutex_unlock( &p_intf->object_lock );
144     vlc_object_release( p_intf );
145 }
146
147 int vout_ControlWindow( vout_thread_t *p_vout, void *p_window,
148                         int i_query, va_list args )
149 {
150     intf_thread_t *p_intf = p_vout->p_parent_intf;
151     int i_ret;
152
153     if( !p_intf ) return VLC_EGENERIC;
154
155     vlc_mutex_lock( &p_intf->object_lock );
156     if( p_intf->b_dead )
157     {
158         vlc_mutex_unlock( &p_intf->object_lock );
159         return VLC_EGENERIC;
160     }
161
162     if( !p_intf->pf_control_window )
163     {
164         msg_Err( p_vout, "no pf_control_window");
165         vlc_mutex_unlock( &p_intf->object_lock );
166         return VLC_EGENERIC;
167     }
168
169     i_ret = p_intf->pf_control_window( p_intf, p_window, i_query, args );
170     vlc_mutex_unlock( &p_intf->object_lock );
171     return i_ret;
172 }
173
174 /*****************************************************************************
175  * vout_IntfInit: called during the vout creation to initialise misc things.
176  *****************************************************************************/
177 void vout_IntfInit( vout_thread_t *p_vout )
178 {
179     vlc_value_t val, text, old_val;
180     vlc_bool_t b_force_par = VLC_FALSE;
181
182     /* Create a few object variables we'll need later on */
183     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
184     var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
185     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
186     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
187     var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
188     var_Get( p_vout, "align", &val );
189     p_vout->i_alignment = val.i_int;
190
191     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
192     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
193
194     /* Zoom object var */
195     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
196                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
197
198     text.psz_string = _("Zoom");
199     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
200
201     var_Get( p_vout, "zoom", &old_val );
202     if( old_val.f_float == 0.25 ||
203         old_val.f_float == 0.5 ||
204         old_val.f_float == 1 ||
205         old_val.f_float == 2 )
206     {
207         var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
208     }
209
210     val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
211     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
212     val.f_float = 0.5; text.psz_string = _("1:2 Half");
213     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
214     val.f_float = 1; text.psz_string = _("1:1 Original");
215     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
216     val.f_float = 2; text.psz_string = _("2:1 Double");
217     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
218
219     var_Set( p_vout, "zoom", old_val );
220
221     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
222
223     /* Crop object var */
224     var_Create( p_vout, "crop", VLC_VAR_STRING |
225                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
226
227     text.psz_string = _("Crop");
228     var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
229
230     val.psz_string = "";
231     var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
232     val.psz_string = ""; text.psz_string = _("Default");
233     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
234     val.psz_string = "001:1"; text.psz_string = _("1:1");
235     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
236     val.psz_string = "004:3"; text.psz_string = _("4:3");
237     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
238     val.psz_string = "16:9"; text.psz_string = _("16:9");
239     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
240     val.psz_string = "221:100"; text.psz_string = _("221:100");
241     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
242
243     var_AddCallback( p_vout, "crop", CropCallback, NULL );
244     var_Get( p_vout, "crop", &old_val );
245     if( old_val.psz_string && *old_val.psz_string )
246         var_Change( p_vout, "crop", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
247     if( old_val.psz_string ) free( old_val.psz_string );
248
249     /* Monitor pixel aspect-ratio */
250     var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
251     var_Get( p_vout, "monitor-par", &val );
252     if( val.psz_string && *val.psz_string )
253     {
254         char *psz_parser = strchr( val.psz_string, ':' );
255         unsigned int i_aspect_num = 0, i_aspect_den = 0;
256         float i_aspect = 0;
257         if( psz_parser )
258         {
259             i_aspect_num = strtol( val.psz_string, 0, 0 );
260             i_aspect_den = strtol( ++psz_parser, 0, 0 );
261         }
262         else
263         {
264             i_aspect = atof( val.psz_string );
265             vlc_ureduce( &i_aspect_num, &i_aspect_den,
266                          i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
267         }
268         if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
269
270         p_vout->i_par_num = i_aspect_num;
271         p_vout->i_par_den = i_aspect_den;
272
273         vlc_ureduce( &p_vout->i_par_num, &p_vout->i_par_den,
274                      p_vout->i_par_num, p_vout->i_par_den, 0 );
275
276         msg_Dbg( p_vout, "monitor pixel aspect-ratio overriding: %i:%i",
277                  p_vout->i_par_num, p_vout->i_par_den );
278         b_force_par = VLC_TRUE;
279     }
280     if( val.psz_string ) free( val.psz_string );
281
282     /* Aspect-ratio object var */
283     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING |
284                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
285
286     text.psz_string = _("Aspect-ratio");
287     var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
288
289     val.psz_string = "";
290     var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
291     val.psz_string = ""; text.psz_string = _("Default");
292     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
293     val.psz_string = "001:1"; text.psz_string = _("1:1");
294     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
295     val.psz_string = "004:3"; text.psz_string = _("4:3");
296     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
297     val.psz_string = "16:9"; text.psz_string = _("16:9");
298     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
299     val.psz_string = "221:100"; text.psz_string = _("221:100");
300     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
301
302     var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
303     var_Get( p_vout, "aspect-ratio", &old_val );
304     if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
305         var_Change( p_vout, "aspect-ratio", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
306     if( old_val.psz_string ) free( old_val.psz_string );
307
308     /* Initialize the dimensions of the video window */
309     InitWindowSize( p_vout, &p_vout->i_window_width,
310                     &p_vout->i_window_height );
311     msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width, 
312              p_vout->i_window_height );
313
314     /* Add a variable to indicate if the window should be on top of others */
315     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
316     text.psz_string = _("Always on top");
317     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
318     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
319
320     /* Add a variable to indicate whether we want window decoration or not */
321     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
322
323     /* Add a fullscreen variable */
324     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
325     text.psz_string = _("Fullscreen");
326     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
327     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
328     if( val.b_bool )
329     {
330         /* user requested fullscreen */
331         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
332     }
333     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
334
335     /* Add a snapshot variable */
336     var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
337     text.psz_string = _("Snapshot");
338     var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
339     var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
340
341     /* Mouse coordinates */
342     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
343     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
344     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
345     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
346     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
347
348     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
349     val.b_bool = VLC_TRUE;
350     var_Set( p_vout, "intf-change", val );
351 }
352
353 /*****************************************************************************
354  * vout_Snapshot: generates a snapshot.
355  *****************************************************************************/
356 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
357 {
358     image_handler_t *p_image = image_HandlerCreate( p_vout );
359     video_format_t fmt_in = {0}, fmt_out = {0};
360     char *psz_filename;
361     subpicture_t *p_subpic;
362     picture_t *p_pif;
363     vlc_value_t val, format;
364     int i_ret;
365
366     var_Get( p_vout, "snapshot-path", &val );
367     if( val.psz_string && !*val.psz_string )
368     {
369         free( val.psz_string );
370         val.psz_string = 0;
371     }
372
373 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
374     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
375     {
376         asprintf( &val.psz_string, "%s/Desktop",
377                   p_vout->p_vlc->psz_homedir );
378     }
379
380 #elif defined(WIN32) && !defined(UNDER_CE)
381     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
382     {
383         /* Get the My Pictures folder path */
384
385         char *p_mypicturesdir = NULL;
386         typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
387                                                    LPSTR );
388         #ifndef CSIDL_FLAG_CREATE
389         #   define CSIDL_FLAG_CREATE 0x8000
390         #endif
391         #ifndef CSIDL_MYPICTURES
392         #   define CSIDL_MYPICTURES 0x27
393         #endif
394         #ifndef SHGFP_TYPE_CURRENT
395         #   define SHGFP_TYPE_CURRENT 0
396         #endif
397
398         HINSTANCE shfolder_dll;
399         SHGETFOLDERPATH SHGetFolderPath ;
400
401         /* load the shfolder dll to retrieve SHGetFolderPath */
402         if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
403         {
404             SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
405                                                       _T("SHGetFolderPathA") );
406             if( SHGetFolderPath != NULL )
407             {
408                 p_mypicturesdir = (char *)malloc( MAX_PATH );
409                 if( p_mypicturesdir ) 
410                 {
411
412                     if( S_OK != SHGetFolderPath( NULL,
413                                         CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
414                                         NULL, SHGFP_TYPE_CURRENT,
415                                         p_mypicturesdir ) )
416                     {
417                         free( p_mypicturesdir );
418                         p_mypicturesdir = NULL;
419                     }
420                 }
421             }
422             FreeLibrary( shfolder_dll );
423         }
424
425         if( p_mypicturesdir == NULL )
426         {
427             asprintf( &val.psz_string, "%s/" CONFIG_DIR,
428                       p_vout->p_vlc->psz_homedir );
429         }
430         else
431         {
432             asprintf( &val.psz_string, p_mypicturesdir );
433             free( p_mypicturesdir );
434         }
435     }
436
437 #else
438     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
439     {
440         asprintf( &val.psz_string, "%s/" CONFIG_DIR,
441                   p_vout->p_vlc->psz_homedir );
442     }
443 #endif
444
445     if( !val.psz_string )
446     {
447         msg_Err( p_vout, "no directory specified for snapshots" );
448         return VLC_EGENERIC;
449     }
450     var_Get( p_vout, "snapshot-format", &format );
451     if( !format.psz_string || !*format.psz_string )
452     {
453         if( format.psz_string ) free( format.psz_string );
454         format.psz_string = strdup( "png" );
455     }
456
457     asprintf( &psz_filename, "%s/vlcsnap-%u.%s", val.psz_string,
458               (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
459               format.psz_string );
460     free( val.psz_string );
461     free( format.psz_string );
462
463     /* Save the snapshot */
464     fmt_in = p_vout->fmt_in;
465     fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
466     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
467     if( i_ret != VLC_SUCCESS )
468     {
469         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
470         free( psz_filename );
471         image_HandlerDelete( p_image );
472         return VLC_EGENERIC;
473     }
474
475     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
476     free( psz_filename );
477
478     /* Inject a subpicture with the snapshot */
479     memset( &fmt_out, 0, sizeof(fmt_out) );
480     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
481     p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
482     image_HandlerDelete( p_image );
483     if( !p_pif ) return VLC_EGENERIC;
484
485     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
486     if( p_subpic == NULL )
487     {
488          p_pif->pf_release( p_pif );
489          return VLC_EGENERIC;
490     }
491
492     p_subpic->i_channel = 0;
493     p_subpic->i_start = mdate();
494     p_subpic->i_stop = mdate() + 4000000;
495     p_subpic->b_ephemer = VLC_TRUE;
496     p_subpic->b_fade = VLC_TRUE;
497     p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
498     p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
499
500     p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
501     vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pif );
502     p_pif->pf_release( p_pif );
503
504     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
505
506     return VLC_SUCCESS;
507 }
508
509 /*****************************************************************************
510  * vout_ControlDefault: default methods for video output control.
511  *****************************************************************************/
512 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
513 {
514     switch( i_query )
515     {
516     case VOUT_REPARENT:
517     case VOUT_CLOSE:
518         if( p_vout->p_parent_intf )
519         {
520             vlc_object_release( p_vout->p_parent_intf );
521             p_vout->p_parent_intf = NULL;
522         }
523         return VLC_SUCCESS;
524         break;
525
526     case VOUT_SNAPSHOT:
527         p_vout->b_snapshot = VLC_TRUE;
528         return VLC_SUCCESS;
529         break;
530
531     default:
532         msg_Dbg( p_vout, "control query not supported" );
533         return VLC_EGENERIC;
534     }
535 }
536
537 /*****************************************************************************
538  * InitWindowSize: find the initial dimensions the video window should have.
539  *****************************************************************************
540  * This function will check the "width", "height" and "zoom" config options and
541  * will calculate the size that the video window should have.
542  *****************************************************************************/
543 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
544                             unsigned *pi_height )
545 {
546     vlc_value_t val;
547     int i_width, i_height;
548     uint64_t ll_zoom;
549
550 #define FP_FACTOR 1000                             /* our fixed point factor */
551
552     var_Get( p_vout, "width", &val );
553     i_width = val.i_int;
554     var_Get( p_vout, "height", &val );
555     i_height = val.i_int;
556     var_Get( p_vout, "zoom", &val );
557     ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
558
559     if( i_width > 0 && i_height > 0)
560     {
561         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
562         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
563         return;
564     }
565     else if( i_width > 0 )
566     {
567         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
568         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
569             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
570             FP_FACTOR / p_vout->fmt_in.i_visible_width );
571         return;
572     }
573     else if( i_height > 0 )
574     {
575         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
576         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
577             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
578             FP_FACTOR / p_vout->fmt_in.i_visible_height );
579         return;
580     }
581
582     if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
583     {
584         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
585             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
586         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom 
587             / FP_FACTOR );
588     }
589     else
590     {
591         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom 
592             / FP_FACTOR );
593         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
594             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
595     }
596
597 #undef FP_FACTOR
598 }
599
600 /*****************************************************************************
601  * Object variables callbacks
602  *****************************************************************************/
603 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
604                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
605 {
606     vout_thread_t *p_vout = (vout_thread_t *)p_this;
607     vout_Control( p_vout, VOUT_SET_ZOOM, newval.f_float );
608     return VLC_SUCCESS;
609 }
610
611 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
612                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
613 {
614     vout_thread_t *p_vout = (vout_thread_t *)p_this;
615     int64_t i_aspect_num, i_aspect_den;
616     unsigned int i_width, i_height;
617
618     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
619
620     /* Restore defaults */
621     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
622     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
623     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
624     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
625
626     if( !psz_parser ) goto crop_end;
627
628     i_aspect_num = strtol( newval.psz_string, &psz_end, 0 );
629     if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
630
631     i_aspect_den = strtol( ++psz_parser, &psz_end, 0 );
632     if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
633
634     i_width = p_vout->fmt_in.i_sar_den * p_vout->fmt_render.i_visible_height *
635         i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
636     i_height = p_vout->fmt_render.i_visible_width * p_vout->fmt_in.i_sar_num *
637         i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
638
639     if( i_width < p_vout->fmt_render.i_visible_width )
640     {
641         p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
642             (p_vout->fmt_render.i_visible_width - i_width) / 2;
643         p_vout->fmt_in.i_visible_width = i_width;
644     }
645     else
646     {
647         p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
648             (p_vout->fmt_render.i_visible_height - i_height) / 2;
649         p_vout->fmt_in.i_visible_height = i_height;
650     }
651
652  crop_end:
653     InitWindowSize( p_vout, &p_vout->i_window_width,
654                     &p_vout->i_window_height );
655
656     p_vout->i_changes |= VOUT_CROP_CHANGE;
657
658     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
659              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
660              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
661              p_vout->fmt_in.i_visible_width,
662              p_vout->fmt_in.i_visible_height );
663
664     return VLC_SUCCESS;
665 }
666
667 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
668                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
669 {
670     vout_thread_t *p_vout = (vout_thread_t *)p_this;
671     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
672     vlc_value_t val;
673
674     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
675
676     /* Restore defaults */
677     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
678     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
679     p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
680     p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
681
682     if( !psz_parser ) goto aspect_end;
683
684     i_aspect_num = strtol( newval.psz_string, &psz_end, 0 );
685     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
686
687     i_aspect_den = strtol( ++psz_parser, &psz_end, 0 );
688     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
689
690     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
691     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
692     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
693     p_vout->fmt_in.i_sar_num = i_sar_num;
694     p_vout->fmt_in.i_sar_den = i_sar_den;
695     p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
696     p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
697
698  aspect_end:
699     if( p_vout->i_par_num && p_vout->i_par_den )
700     {
701         p_vout->fmt_in.i_sar_num *= p_vout->i_par_den;
702         p_vout->fmt_in.i_sar_den *= p_vout->i_par_num;
703         p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
704             p_vout->i_par_den / p_vout->i_par_num;
705         p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
706     }
707
708     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
709
710     vlc_ureduce( &i_aspect_num, &i_aspect_den,
711                  p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
712     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
713              i_aspect_num, i_aspect_den,
714              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
715
716     var_Get( p_vout, "crop", &val );
717     return CropCallback( p_this, 0, val, val, 0 );
718
719     return VLC_SUCCESS;
720 }
721
722 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
723                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
724 {
725     vout_thread_t *p_vout = (vout_thread_t *)p_this;
726     playlist_t *p_playlist;
727     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
728
729     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
730                                                  FIND_PARENT );
731     if( p_playlist )
732     {
733         /* Modify playlist as well because the vout might have to be restarted */
734         var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
735         var_Set( p_playlist, "video-on-top", newval );
736
737         vlc_object_release( p_playlist );
738     }
739     return VLC_SUCCESS;
740 }
741
742 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
743                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
744 {
745     vout_thread_t *p_vout = (vout_thread_t *)p_this;
746     playlist_t *p_playlist;
747     vlc_value_t val;
748
749     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
750
751     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
752                                                  FIND_PARENT );
753     if( p_playlist )
754     {
755         /* Modify playlist as well because the vout might have to be restarted */
756         var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
757         var_Set( p_playlist, "fullscreen", newval );
758
759         vlc_object_release( p_playlist );
760     }
761
762     /* Disable "always on top" in fullscreen mode */
763     var_Get( p_vout, "video-on-top", &val );
764     if( newval.b_bool && val.b_bool )
765     {
766         val.b_bool = VLC_FALSE;
767         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
768     }
769     else if( !newval.b_bool && val.b_bool )
770     {
771         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
772     }
773
774     val.b_bool = VLC_TRUE;
775     var_Set( p_vout, "intf-change", val );
776     return VLC_SUCCESS;
777 }
778
779 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
780                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
781 {
782     vout_thread_t *p_vout = (vout_thread_t *)p_this;
783     vout_Control( p_vout, VOUT_SNAPSHOT );
784     return VLC_SUCCESS;
785 }