]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
8160e854a4a3674f53eb0c1b7b8ea4646a94ddfb
[vlc] / src / video_output / vout_intf.c
1 /*****************************************************************************
2  * vout_intf.c : video output interface
3  *****************************************************************************
4  * Copyright (C) 2000-2007 the VideoLAN team
5  *
6  * Authors: Gildas Bazin <gbazin@videolan.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>                                                /* free() */
35 #include <sys/types.h>                                          /* opendir() */
36 #include <sys/stat.h>
37 #include <dirent.h>                                             /* opendir() */
38 #include <assert.h>
39 #include <time.h>                                           /* strftime */
40
41 #include <vlc_interface.h>
42 #include <vlc_block.h>
43 #include <vlc_playlist.h>
44
45 #include <vlc_vout.h>
46 #include <vlc_window.h>
47 #include <vlc_image.h>
48 #include <vlc_osd.h>
49 #include <vlc_charset.h>
50
51 #include <vlc_strings.h>
52 #include <vlc_charset.h>
53 #include "../libvlc.h"
54 #include "vout_internal.h"
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
60
61 /* Object variables callbacks */
62 static int ZoomCallback( vlc_object_t *, char const *,
63                          vlc_value_t, vlc_value_t, void * );
64 static int CropCallback( vlc_object_t *, char const *,
65                          vlc_value_t, vlc_value_t, void * );
66 static int AspectCallback( vlc_object_t *, char const *,
67                            vlc_value_t, vlc_value_t, void * );
68 static int ScalingCallback( vlc_object_t *, char const *,
69                             vlc_value_t, vlc_value_t, void * );
70 static int OnTopCallback( vlc_object_t *, char const *,
71                           vlc_value_t, vlc_value_t, void * );
72 static int FullscreenCallback( vlc_object_t *, char const *,
73                                vlc_value_t, vlc_value_t, void * );
74 static int SnapshotCallback( vlc_object_t *, char const *,
75                              vlc_value_t, vlc_value_t, void * );
76
77 static int TitleShowCallback( vlc_object_t *, char const *,
78                               vlc_value_t, vlc_value_t, void * );
79 static int TitleTimeoutCallback( vlc_object_t *, char const *,
80                                  vlc_value_t, vlc_value_t, void * );
81 static int TitlePositionCallback( vlc_object_t *, char const *,
82                                   vlc_value_t, vlc_value_t, void * );
83
84 /**
85  * Creates a video output window.
86  * On Unix systems, this is an X11 drawable (handle).
87  * On Windows, this is a Win32 window (handle).
88  * Video output plugins are supposed to called this function and display the
89  * video within the resulting window, while in windowed mode.
90  *
91  * @param p_vout video output thread to create a window for
92  * @param psz_cap VLC module capability (window system type)
93  * @param pi_x_hint pointer to store the recommended horizontal position [OUT]
94  * @param pi_y_hint pointer to store the recommended vertical position [OUT]
95  * @param pi_width_hint pointer to store the recommended width [OUT]
96  * @param pi_height_hint pointer to store the recommended height [OUT]
97  *
98  * @return a vout_window_t object, or NULL in case of failure.
99  * The window is released with vout_ReleaseWindow().
100  */
101 vout_window_t *vout_RequestWindow( vout_thread_t *p_vout, const char *psz_cap,
102                           int *pi_x_hint, int *pi_y_hint,
103                           unsigned int *pi_width_hint,
104                           unsigned int *pi_height_hint )
105 {
106     /* Get requested coordinates */
107     *pi_x_hint = var_GetInteger( p_vout, "video-x" );
108     *pi_y_hint = var_GetInteger( p_vout, "video-y" );
109
110     *pi_width_hint = p_vout->i_window_width;
111     *pi_height_hint = p_vout->i_window_height;
112
113     vout_window_t *wnd = vlc_custom_create (VLC_OBJECT(p_vout), sizeof (*wnd),
114                                             VLC_OBJECT_GENERIC, "window");
115     if (wnd == NULL)
116         return NULL;
117
118     wnd->vout = p_vout;
119     wnd->width = *pi_width_hint;
120     wnd->height = *pi_height_hint;
121     wnd->pos_x = *pi_x_hint;
122     wnd->pos_y = *pi_y_hint;
123     vlc_object_attach (wnd, p_vout);
124
125     wnd->module = module_need (wnd, psz_cap, NULL, false);
126     if (wnd->module == NULL)
127     {
128         msg_Dbg (wnd, "no \"%s\" window provider available", psz_cap);
129         vlc_object_release (wnd);
130         return NULL;
131     }
132     *pi_width_hint = wnd->width;
133     *pi_height_hint = wnd->height;
134     *pi_x_hint = wnd->pos_x;
135     *pi_y_hint = wnd->pos_y;
136     return wnd;
137 }
138
139 /**
140  * Releases a window handle obtained with vout_RequestWindow().
141  * @param p_vout video output thread that allocated the window
142  *               (if this is NULL; this fnction is a no-op).
143  */
144 void vout_ReleaseWindow( vout_window_t *wnd )
145 {
146     if (wnd == NULL)
147         return;
148
149     assert (wnd->module);
150     module_unneed (wnd, wnd->module);
151
152     vlc_object_release (wnd);
153 }
154
155 int vout_ControlWindow( vout_window_t *wnd, int i_query, va_list args )
156 {
157     if (wnd == NULL)
158         return VLC_EGENERIC;
159
160     assert (wnd->control);
161     return wnd->control (wnd, i_query, args);
162 }
163
164 /*****************************************************************************
165  * vout_IntfInit: called during the vout creation to initialise misc things.
166  *****************************************************************************/
167 static const struct
168 {
169     double f_value;
170     const char *psz_label;
171 } p_zoom_values[] = {
172     { 0.25, N_("1:4 Quarter") },
173     { 0.5, N_("1:2 Half") },
174     { 1, N_("1:1 Original") },
175     { 2, N_("2:1 Double") },
176     { 0, NULL } };
177
178 static const struct
179 {
180     const char *psz_value;
181     const char *psz_label;
182 } p_crop_values[] = {
183     { "", N_("Default") },
184     { "16:10", "16:10" },
185     { "16:9", "16:9" },
186     { "185:100", "1.85:1" },
187     { "221:100", "2.21:1" },
188     { "235:100", "2.35:1" },
189     { "239:100", "2.39:1" },
190     { "5:3", "5:3" },
191     { "4:3", "4:3" },
192     { "5:4", "5:4" },
193     { "1:1", "1:1" },
194     { NULL, NULL } };
195
196 static const struct
197 {
198     const char *psz_value;
199     const char *psz_label;
200 } p_aspect_ratio_values[] = {
201     { "", N_("Default") },
202     { "1:1", "1:1" },
203     { "4:3", "4:3" },
204     { "16:9", "16:9" },
205     { "16:10", "16:10" },
206     { "221:100", "2.21:1" },
207     { "5:4", "5:4" },
208     { NULL, NULL } };
209
210 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
211                              char *psz_list )
212 {
213     if( psz_list && *psz_list )
214     {
215         char *psz_cur = psz_list;
216         char *psz_next;
217         while( psz_cur && *psz_cur )
218         {
219             vlc_value_t val, text;
220             psz_next = strchr( psz_cur, ',' );
221             if( psz_next )
222             {
223                 *psz_next = '\0';
224                 psz_next++;
225             }
226             val.psz_string = psz_cur;
227             text.psz_string = psz_cur;
228             var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
229             psz_cur = psz_next;
230         }
231     }
232 }
233
234 void vout_IntfInit( vout_thread_t *p_vout )
235 {
236     vlc_value_t val, text, old_val;
237     bool b_force_par = false;
238     char *psz_buf;
239     int i;
240
241     /* Create a few object variables we'll need later on */
242     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
243     var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
244     var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
245     var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
246     var_Create( p_vout, "snapshot-sequential",
247                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
248     var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
249     var_SetInteger( p_vout, "snapshot-num", 1 );
250     var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
251     var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
252
253     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
254     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
255     p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
256
257     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
258     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
259
260     var_Create( p_vout, "mouse-hide-timeout",
261                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
262
263     p_vout->p->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
264     p_vout->p->i_title_timeout =
265         (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
266     p_vout->p->i_title_position =
267         var_CreateGetInteger( p_vout, "video-title-position" );
268     p_vout->p->psz_title =  NULL;
269
270     var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
271     var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
272     var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
273
274     /* Zoom object var */
275     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
276                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
277
278     text.psz_string = _("Zoom");
279     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
280
281     var_Get( p_vout, "zoom", &old_val );
282
283     for( i = 0; p_zoom_values[i].f_value; i++ )
284     {
285         if( old_val.f_float == p_zoom_values[i].f_value )
286             var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
287         val.f_float = p_zoom_values[i].f_value;
288         text.psz_string = _( p_zoom_values[i].psz_label );
289         var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
290     }
291
292     var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
293
294     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
295
296     /* Crop offset vars */
297     var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
298     var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
299     var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
300     var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
301
302     var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
303     var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
304     var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
305     var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
306
307     /* Crop object var */
308     var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
309                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
310
311     text.psz_string = _("Crop");
312     var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
313
314     val.psz_string = (char*)"";
315     var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
316
317     for( i = 0; p_crop_values[i].psz_value; i++ )
318     {
319         val.psz_string = (char*)p_crop_values[i].psz_value;
320         text.psz_string = _( p_crop_values[i].psz_label );
321         var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
322     }
323
324     /* update triggered every time the vout's crop parameters are changed */
325     var_Create( p_vout, "crop-update", VLC_VAR_VOID );
326
327     /* Add custom crop ratios */
328     psz_buf = config_GetPsz( p_vout, "custom-crop-ratios" );
329     AddCustomRatios( p_vout, "crop", psz_buf );
330     free( psz_buf );
331
332     var_AddCallback( p_vout, "crop", CropCallback, NULL );
333     var_Get( p_vout, "crop", &old_val );
334     if( old_val.psz_string && *old_val.psz_string )
335         var_TriggerCallback( p_vout, "crop" );
336     free( old_val.psz_string );
337
338     /* Monitor pixel aspect-ratio */
339     var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
340     var_Get( p_vout, "monitor-par", &val );
341     if( val.psz_string && *val.psz_string )
342     {
343         char *psz_parser = strchr( val.psz_string, ':' );
344         unsigned int i_aspect_num = 0, i_aspect_den = 0;
345         float i_aspect = 0;
346         if( psz_parser )
347         {
348             i_aspect_num = strtol( val.psz_string, 0, 10 );
349             i_aspect_den = strtol( ++psz_parser, 0, 10 );
350         }
351         else
352         {
353             i_aspect = atof( val.psz_string );
354             vlc_ureduce( &i_aspect_num, &i_aspect_den,
355                          i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
356         }
357         if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
358
359         p_vout->p->i_par_num = i_aspect_num;
360         p_vout->p->i_par_den = i_aspect_den;
361
362         vlc_ureduce( &p_vout->p->i_par_num, &p_vout->p->i_par_den,
363                      p_vout->p->i_par_num, p_vout->p->i_par_den, 0 );
364
365         msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
366                  p_vout->p->i_par_num, p_vout->p->i_par_den );
367         b_force_par = true;
368     }
369     free( val.psz_string );
370
371     /* Aspect-ratio object var */
372     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
373                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
374
375     text.psz_string = _("Aspect-ratio");
376     var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
377
378     val.psz_string = (char*)"";
379     var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
380
381     for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
382     {
383         val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
384         text.psz_string = _( p_aspect_ratio_values[i].psz_label );
385         var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
386     }
387
388     /* Add custom aspect ratios */
389     psz_buf = config_GetPsz( p_vout, "custom-aspect-ratios" );
390     AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
391     free( psz_buf );
392
393     var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
394     var_Get( p_vout, "aspect-ratio", &old_val );
395     if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
396         var_TriggerCallback( p_vout, "aspect-ratio" );
397     free( old_val.psz_string );
398
399     /* Add variables to manage scaling video */
400     var_Create( p_vout, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
401                 | VLC_VAR_ISCOMMAND );
402     text.psz_string = _("Autoscale video");
403     var_Change( p_vout, "autoscale", VLC_VAR_SETTEXT, &text, NULL );
404     var_AddCallback( p_vout, "autoscale", ScalingCallback, NULL );
405     p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
406
407     var_Create( p_vout, "scale", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT
408                 | VLC_VAR_ISCOMMAND );
409     text.psz_string = _("Scale factor");
410     var_Change( p_vout, "scale", VLC_VAR_SETTEXT, &text, NULL );
411     var_AddCallback( p_vout, "scale", ScalingCallback, NULL );
412     p_vout->i_zoom = (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
413
414     /* Initialize the dimensions of the video window */
415     InitWindowSize( p_vout, &p_vout->i_window_width,
416                     &p_vout->i_window_height );
417
418     /* Add a variable to indicate if the window should be on top of others */
419     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
420                 | VLC_VAR_ISCOMMAND );
421     text.psz_string = _("Always on top");
422     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
423     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
424
425     /* Add a variable to indicate whether we want window decoration or not */
426     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
427
428     /* Add a fullscreen variable */
429     if( var_CreateGetBoolCommand( p_vout, "fullscreen" ) )
430     {
431         /* user requested fullscreen */
432         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
433     }
434     text.psz_string = _("Fullscreen");
435     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
436     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
437
438     /* Add a snapshot variable */
439     var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
440     text.psz_string = _("Snapshot");
441     var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
442     var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
443
444     /* Mouse coordinates */
445     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
446     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
447     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
448     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
449     var_Create( p_vout, "mouse-clicked", VLC_VAR_BOOL );
450
451     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
452     var_SetBool( p_vout, "intf-change", true );
453 }
454
455 /*****************************************************************************
456  * vout_Snapshot: generates a snapshot.
457  *****************************************************************************/
458 /**
459  * This function will inject a subpicture into the vout with the provided
460  * picture
461  */
462 static int VoutSnapshotPip( vout_thread_t *p_vout, image_handler_t *p_image, picture_t *p_pic, const video_format_t *p_fmt_in )
463 {
464     video_format_t fmt_in = *p_fmt_in;
465     video_format_t fmt_out;
466     picture_t *p_pip;
467     subpicture_t *p_subpic;
468
469     /* */
470     memset( &fmt_out, 0, sizeof(fmt_out) );
471     fmt_out = fmt_in;
472     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
473
474     /* */
475     p_pip = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
476     if( !p_pip )
477         return VLC_EGENERIC;
478
479     p_subpic = subpicture_New();
480     if( p_subpic == NULL )
481     {
482          picture_Release( p_pip );
483          return VLC_EGENERIC;
484     }
485
486     p_subpic->i_channel = 0;
487     p_subpic->i_start = mdate();
488     p_subpic->i_stop = mdate() + 4000000;
489     p_subpic->b_ephemer = true;
490     p_subpic->b_fade = true;
491     p_subpic->i_original_picture_width = fmt_out.i_width * 4;
492     p_subpic->i_original_picture_height = fmt_out.i_height * 4;
493     fmt_out.i_aspect = 0;
494     fmt_out.i_sar_num =
495     fmt_out.i_sar_den = 0;
496
497     p_subpic->p_region = subpicture_region_New( &fmt_out );
498     if( p_subpic->p_region )
499     {
500         picture_Release( p_subpic->p_region->p_picture );
501         p_subpic->p_region->p_picture = p_pip;
502     }
503     else
504     {
505         picture_Release( p_pip );
506     }
507
508     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
509     return VLC_SUCCESS;
510 }
511 /**
512  * This function will return the default directory used for snapshots
513  */
514 static char *VoutSnapshotGetDefaultDirectory( void )
515 {
516     char *psz_path = NULL;
517 #if defined(__APPLE__) || defined(SYS_BEOS)
518
519     if( asprintf( &psz_path, "%s/Desktop",
520                   config_GetHomeDir() ) == -1 )
521         psz_path = NULL;
522
523 #elif defined(WIN32) && !defined(UNDER_CE)
524
525     /* Get the My Pictures folder path */
526     char *p_mypicturesdir = NULL;
527     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
528                                                LPWSTR );
529     #ifndef CSIDL_FLAG_CREATE
530     #   define CSIDL_FLAG_CREATE 0x8000
531     #endif
532     #ifndef CSIDL_MYPICTURES
533     #   define CSIDL_MYPICTURES 0x27
534     #endif
535     #ifndef SHGFP_TYPE_CURRENT
536     #   define SHGFP_TYPE_CURRENT 0
537     #endif
538
539     HINSTANCE shfolder_dll;
540     SHGETFOLDERPATH SHGetFolderPath ;
541
542     /* load the shfolder dll to retrieve SHGetFolderPath */
543     if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
544     {
545        wchar_t wdir[PATH_MAX];
546        SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
547                                                   _T("SHGetFolderPathW") );
548         if ((SHGetFolderPath != NULL )
549          && SUCCEEDED (SHGetFolderPath (NULL,
550                                        CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
551                                        NULL, SHGFP_TYPE_CURRENT,
552                                        wdir)))
553             p_mypicturesdir = FromWide (wdir);
554
555         FreeLibrary( shfolder_dll );
556     }
557
558     if( p_mypicturesdir == NULL )
559         psz_path = strdup( config_GetHomeDir() );
560     else
561         psz_path = p_mypicturesdir;
562
563 #else
564
565     /* XXX: This saves in the data directory. Shouldn't we try saving
566      *      to psz_homedir/Desktop or something nicer ? */
567     char *psz_datadir = config_GetUserDataDir();
568     if( psz_datadir )
569     {
570         if( asprintf( &psz_path, "%s", psz_datadir ) == -1 )
571             psz_path = NULL;
572         free( psz_datadir );
573     }
574
575 #endif
576
577     return psz_path;
578 }
579
580 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
581 {
582     image_handler_t *p_image = image_HandlerCreate( p_vout );
583     video_format_t fmt_in, fmt_out;
584     char *psz_filename = NULL;
585     vlc_value_t val, format;
586     DIR *path;
587     int i_ret;
588     bool b_embedded_snapshot;
589     void *p_obj;
590
591     /* */
592     val.psz_string = var_GetNonEmptyString( p_vout, "snapshot-path" );
593
594     /* Embedded snapshot : if snapshot-path == object:object_ptr */
595     if( val.psz_string && sscanf( val.psz_string, "object:%p", &p_obj ) > 0 )
596         b_embedded_snapshot = true;
597     else
598         b_embedded_snapshot = false;
599
600     /* */
601     memset( &fmt_in, 0, sizeof(video_format_t) );
602     fmt_in = p_vout->fmt_out;
603     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
604     {
605         fmt_in.i_sar_num =
606         fmt_in.i_sar_den = 1;
607     }
608
609     /* */
610     memset( &fmt_out, 0, sizeof(video_format_t) );
611     fmt_out.i_sar_num =
612     fmt_out.i_sar_den = 1;
613     fmt_out.i_chroma = b_embedded_snapshot ? VLC_FOURCC('p','n','g',' ') : 0;
614     fmt_out.i_width = var_GetInteger( p_vout, "snapshot-width" );
615     fmt_out.i_height = var_GetInteger( p_vout, "snapshot-height" );
616
617     if( b_embedded_snapshot &&
618         fmt_out.i_width == 0 && fmt_out.i_height == 0 )
619     {
620         /* If snapshot-width and/or snapshot height were not specified,
621            use a default snapshot width of 320 */
622         fmt_out.i_width = 320;
623     }
624
625     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
626     {
627         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width / fmt_in.i_width;
628         const int i_height = fmt_out.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
629         if( i_height > 0 )
630             fmt_out.i_height = i_height;
631     }
632     else
633     {
634         if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
635         {
636             fmt_out.i_width = fmt_in.i_width * fmt_out.i_height / fmt_in.i_height;
637         }
638         else
639         {
640             fmt_out.i_width = fmt_in.i_width;
641             fmt_out.i_height = fmt_in.i_height;
642         }
643         const int i_width = fmt_out.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
644         if( i_width > 0 )
645             fmt_out.i_width = i_width;
646     }
647
648     /* Embedded snapshot
649        create a snapshot_t* and store it in
650        object_ptr->p_private, then unlock and signal the
651        waiting object.
652      */
653     if( b_embedded_snapshot )
654     {
655         block_t *p_block;
656         snapshot_t *p_snapshot = p_obj;
657         size_t i_size;
658
659         vlc_mutex_lock( &p_snapshot->p_mutex );
660         p_snapshot->p_data = NULL;
661
662         /* Save the snapshot to a memory zone */
663         p_block = image_Write( p_image, p_pic, &fmt_in, &fmt_out );
664         if( !p_block )
665         {
666             msg_Err( p_vout, "Could not get snapshot" );
667             image_HandlerDelete( p_image );
668             vlc_cond_signal( &p_snapshot->p_condvar );
669             vlc_mutex_unlock( &p_snapshot->p_mutex );
670             return VLC_EGENERIC;
671         }
672
673         i_size = p_block->i_buffer;
674
675         p_snapshot->i_width = fmt_out.i_width;
676         p_snapshot->i_height = fmt_out.i_height;
677         p_snapshot->i_datasize = i_size;
678         p_snapshot->date = p_block->i_pts; /* FIXME ?? */
679         p_snapshot->p_data = malloc( i_size );
680         if( !p_snapshot->p_data )
681         {
682             block_Release( p_block );
683             image_HandlerDelete( p_image );
684             vlc_cond_signal( &p_snapshot->p_condvar );
685             vlc_mutex_unlock( &p_snapshot->p_mutex );
686             return VLC_ENOMEM;
687         }
688         memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
689
690         block_Release( p_block );
691
692         /* Unlock the object */
693         vlc_cond_signal( &p_snapshot->p_condvar );
694         vlc_mutex_unlock( &p_snapshot->p_mutex );
695
696         image_HandlerDelete( p_image );
697         return VLC_SUCCESS;
698     }
699
700     /* Get default directory if none provided */
701     if( !val.psz_string )
702         val.psz_string = VoutSnapshotGetDefaultDirectory( );
703     if( !val.psz_string )
704     {
705         msg_Err( p_vout, "no path specified for snapshots" );
706         image_HandlerDelete( p_image );
707         return VLC_EGENERIC;
708     }
709
710     /* Get snapshot format, default being "png" */
711     format.psz_string = var_GetNonEmptyString( p_vout, "snapshot-format" );
712     if( !format.psz_string )
713         format.psz_string = strdup( "png" );
714     if( !format.psz_string )
715     {
716         free( val.psz_string );
717         image_HandlerDelete( p_image );
718         return VLC_ENOMEM;
719     }
720
721     /*
722      * Did the user specify a directory? If not, path = NULL.
723      */
724     path = utf8_opendir ( (const char *)val.psz_string  );
725     if( path != NULL )
726     {
727         char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
728         if( psz_prefix == NULL )
729             psz_prefix = strdup( "vlcsnap-" );
730         else
731         {
732             char *psz_tmp = str_format( p_vout, psz_prefix );
733             filename_sanitize( psz_tmp );
734             free( psz_prefix );
735             psz_prefix = psz_tmp;
736         }
737
738         closedir( path );
739         if( var_GetBool( p_vout, "snapshot-sequential" ) == true )
740         {
741             int i_num = var_GetInteger( p_vout, "snapshot-num" );
742             struct stat st;
743
744             do
745             {
746                 free( psz_filename );
747                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
748                               val.psz_string, psz_prefix, i_num++,
749                               format.psz_string ) == -1 )
750                 {
751                     msg_Err( p_vout, "could not create snapshot" );
752                     image_HandlerDelete( p_image );
753                     return VLC_EGENERIC;
754                 }
755             }
756             while( utf8_stat( psz_filename, &st ) == 0 );
757
758             var_SetInteger( p_vout, "snapshot-num", i_num );
759         }
760         else
761         {
762             struct tm    curtime;
763             time_t        lcurtime ;
764             lcurtime = time( NULL ) ;
765             if ( ( localtime_r( &lcurtime, &curtime ) == NULL ) )
766             {
767                 msg_Warn( p_vout, "failed to get current time. Falling back to legacy snapshot naming" );
768                 /* failed to get current time. Fallback to old format */
769                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
770                           val.psz_string, psz_prefix,
771                           (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
772                           format.psz_string ) == -1 )
773                 {
774                     msg_Err( p_vout, "could not create snapshot" );
775                     image_HandlerDelete( p_image );
776                     return VLC_EGENERIC;
777                 }
778             }
779             else
780             {
781                 char psz_curtime[15] ;
782                 if( strftime( psz_curtime, 15, "%y%m%d-%H%M%S", &curtime ) == 0 )
783                 {
784                     msg_Warn( p_vout, "snapshot date string truncated" ) ;
785                 }
786                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%s%1u.%s",
787                       val.psz_string, psz_prefix, psz_curtime,
788                      /* suffix with the last decimal digit in 10s of seconds resolution */
789                      (unsigned int)(p_pic->date / (100*1000)) & 0xFF,
790                       format.psz_string ) == -1 )
791                 {
792                     msg_Err( p_vout, "could not create snapshot" );
793                     image_HandlerDelete( p_image );
794                     return VLC_EGENERIC;
795                 }
796             } //end if time() < 0
797         } //end snapshot sequential
798         free( psz_prefix );
799     }
800     else // The user specified a full path name (including file name)
801     {
802         psz_filename = str_format( p_vout, val.psz_string );
803         path_sanitize( psz_filename );
804     }
805
806     free( val.psz_string );
807     free( format.psz_string );
808
809     /* Save the snapshot */
810     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
811     if( i_ret != VLC_SUCCESS )
812     {
813         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
814         free( psz_filename );
815         image_HandlerDelete( p_image );
816         return VLC_EGENERIC;
817     }
818
819     /* */
820     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
821     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
822                      "%s", psz_filename );
823
824     /* Generate a media player event  - Right now just trigger a global libvlc var
825         CHECK: Could not find a more local object. The goal is to communicate
826         vout_thread with libvlc_media_player or its input_thread*/
827     val.psz_string =  psz_filename  ;
828
829     var_Set( p_vout->p_libvlc, "vout-snapshottaken", val );
830     /* var_Set duplicates data for transport so we can free*/
831     free( psz_filename );
832
833     /* */
834     if( var_GetBool( p_vout, "snapshot-preview" ) )
835     {
836         if( VoutSnapshotPip( p_vout, p_image, p_pic, &fmt_in ) )
837             msg_Warn( p_vout, "Failed to display snapshot" );
838     }
839     image_HandlerDelete( p_image );
840
841     return VLC_SUCCESS;
842 }
843
844 /*****************************************************************************
845  * Handle filters
846  *****************************************************************************/
847
848 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
849                         bool b_add, bool b_setconfig )
850 {
851     char *psz_parser;
852     char *psz_string = config_GetPsz( p_vout, "vout-filter" );
853
854     /* Todo : Use some generic chain manipulation functions */
855     if( !psz_string ) psz_string = strdup("");
856
857     psz_parser = strstr( psz_string, psz_name );
858     if( b_add )
859     {
860         if( !psz_parser )
861         {
862             psz_parser = psz_string;
863             if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
864                           psz_string, psz_name ) == -1 )
865             {
866                 free( psz_parser );
867                 return;
868             }
869             free( psz_parser );
870         }
871         else
872             return;
873     }
874     else
875     {
876         if( psz_parser )
877         {
878             memmove( psz_parser, psz_parser + strlen(psz_name) +
879                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
880                             strlen(psz_parser + strlen(psz_name)) + 1 );
881
882             /* Remove trailing : : */
883             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
884             {
885                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
886             }
887          }
888          else
889          {
890              free( psz_string );
891              return;
892          }
893     }
894     if( b_setconfig )
895         config_PutPsz( p_vout, "vout-filter", psz_string );
896
897     var_SetString( p_vout, "vout-filter", psz_string );
898     free( psz_string );
899 }
900
901 /*****************************************************************************
902  * InitWindowSize: find the initial dimensions the video window should have.
903  *****************************************************************************
904  * This function will check the "width", "height" and "zoom" config options and
905  * will calculate the size that the video window should have.
906  *****************************************************************************/
907 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
908                             unsigned *pi_height )
909 {
910     vlc_value_t val;
911     int i_width, i_height;
912     uint64_t ll_zoom;
913
914 #define FP_FACTOR 1000                             /* our fixed point factor */
915
916     var_Get( p_vout, "width", &val );
917     i_width = val.i_int;
918     var_Get( p_vout, "height", &val );
919     i_height = val.i_int;
920     var_Get( p_vout, "zoom", &val );
921     ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
922
923     if( i_width > 0 && i_height > 0)
924     {
925         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
926         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
927         goto initwsize_end;
928     }
929     else if( i_width > 0 )
930     {
931         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
932         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
933             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
934             FP_FACTOR / p_vout->fmt_in.i_visible_width );
935         goto initwsize_end;
936     }
937     else if( i_height > 0 )
938     {
939         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
940         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
941             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
942             FP_FACTOR / p_vout->fmt_in.i_visible_height );
943         goto initwsize_end;
944     }
945
946     if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
947         msg_Warn( p_vout, "fucked up aspect" );
948         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
949         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
950     }
951     else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
952     {
953         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
954             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
955         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
956             / FP_FACTOR );
957     }
958     else
959     {
960         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
961             / FP_FACTOR );
962         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
963             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
964     }
965
966 initwsize_end:
967     msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width,
968              p_vout->i_window_height );
969
970 #undef FP_FACTOR
971 }
972
973 /*****************************************************************************
974  * Object variables callbacks
975  *****************************************************************************/
976 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
977                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
978 {
979     vout_thread_t *p_vout = (vout_thread_t *)p_this;
980     (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
981     InitWindowSize( p_vout, &p_vout->i_window_width,
982                     &p_vout->i_window_height );
983     vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
984                   p_vout->i_window_height );
985     return VLC_SUCCESS;
986 }
987
988 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
989                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
990 {
991     vout_thread_t *p_vout = (vout_thread_t *)p_this;
992     int64_t i_aspect_num, i_aspect_den;
993     unsigned int i_width, i_height;
994
995     (void)oldval; (void)p_data;
996
997     /* Restore defaults */
998     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
999     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
1000     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
1001     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
1002
1003     if( !strcmp( psz_cmd, "crop" ) )
1004     {
1005         char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
1006         if( psz_parser )
1007         {
1008             /* We're using the 3:4 syntax */
1009             i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1010             if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
1011
1012             i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1013             if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
1014
1015             i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
1016                 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
1017             i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
1018                 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
1019
1020             if( i_width < p_vout->fmt_render.i_visible_width )
1021             {
1022                 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
1023                     (p_vout->fmt_render.i_visible_width - i_width) / 2;
1024                 p_vout->fmt_in.i_visible_width = i_width;
1025             }
1026             else
1027             {
1028                 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
1029                     (p_vout->fmt_render.i_visible_height - i_height) / 2;
1030                 p_vout->fmt_in.i_visible_height = i_height;
1031             }
1032         }
1033         else
1034         {
1035             psz_parser = strchr( newval.psz_string, 'x' );
1036             if( psz_parser )
1037             {
1038                 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
1039                 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
1040
1041                 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
1042                 if( psz_end != psz_parser ) goto crop_end;
1043
1044                 psz_parser = strchr( ++psz_end, '+' );
1045                 i_crop_height = strtol( psz_end, &psz_end, 10 );
1046                 if( psz_end != psz_parser ) goto crop_end;
1047
1048                 psz_parser = strchr( ++psz_end, '+' );
1049                 i_crop_left = strtol( psz_end, &psz_end, 10 );
1050                 if( psz_end != psz_parser ) goto crop_end;
1051
1052                 psz_end++;
1053                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1054                 if( *psz_end != '\0' ) goto crop_end;
1055
1056                 i_width = i_crop_width;
1057                 p_vout->fmt_in.i_visible_width = i_width;
1058
1059                 i_height = i_crop_height;
1060                 p_vout->fmt_in.i_visible_height = i_height;
1061
1062                 p_vout->fmt_in.i_x_offset = i_crop_left;
1063                 p_vout->fmt_in.i_y_offset = i_crop_top;
1064             }
1065             else
1066             {
1067                 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1068                 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1069
1070                 psz_parser = strchr( newval.psz_string, '+' );
1071                 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1072                 if( psz_end != psz_parser ) goto crop_end;
1073
1074                 psz_parser = strchr( ++psz_end, '+' );
1075                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1076                 if( psz_end != psz_parser ) goto crop_end;
1077
1078                 psz_parser = strchr( ++psz_end, '+' );
1079                 i_crop_right = strtol( psz_end, &psz_end, 10 );
1080                 if( psz_end != psz_parser ) goto crop_end;
1081
1082                 psz_end++;
1083                 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1084                 if( *psz_end != '\0' ) goto crop_end;
1085
1086                 i_width = p_vout->fmt_render.i_visible_width
1087                           - i_crop_left - i_crop_right;
1088                 p_vout->fmt_in.i_visible_width = i_width;
1089
1090                 i_height = p_vout->fmt_render.i_visible_height
1091                            - i_crop_top - i_crop_bottom;
1092                 p_vout->fmt_in.i_visible_height = i_height;
1093
1094                 p_vout->fmt_in.i_x_offset = i_crop_left;
1095                 p_vout->fmt_in.i_y_offset = i_crop_top;
1096             }
1097         }
1098     }
1099     else if( !strcmp( psz_cmd, "crop-top" )
1100           || !strcmp( psz_cmd, "crop-left" )
1101           || !strcmp( psz_cmd, "crop-bottom" )
1102           || !strcmp( psz_cmd, "crop-right" ) )
1103     {
1104         unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1105
1106         i_crop_top = var_GetInteger( p_vout, "crop-top" );
1107         i_crop_left = var_GetInteger( p_vout, "crop-left" );
1108         i_crop_right = var_GetInteger( p_vout, "crop-right" );
1109         i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1110
1111         i_width = p_vout->fmt_render.i_visible_width
1112                   - i_crop_left - i_crop_right;
1113         p_vout->fmt_in.i_visible_width = i_width;
1114
1115         i_height = p_vout->fmt_render.i_visible_height
1116                    - i_crop_top - i_crop_bottom;
1117         p_vout->fmt_in.i_visible_height = i_height;
1118
1119         p_vout->fmt_in.i_x_offset = i_crop_left;
1120         p_vout->fmt_in.i_y_offset = i_crop_top;
1121     }
1122
1123  crop_end:
1124     InitWindowSize( p_vout, &p_vout->i_window_width,
1125                     &p_vout->i_window_height );
1126
1127     p_vout->i_changes |= VOUT_CROP_CHANGE;
1128
1129     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1130              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1131              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1132              p_vout->fmt_in.i_visible_width,
1133              p_vout->fmt_in.i_visible_height );
1134
1135     var_SetVoid( p_vout, "crop-update" );
1136
1137     return VLC_SUCCESS;
1138 }
1139
1140 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1141                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1142 {
1143     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1144     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1145     vlc_value_t val;
1146
1147     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1148     (void)psz_cmd; (void)oldval; (void)p_data;
1149
1150     /* Restore defaults */
1151     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1152     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1153     p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1154     p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1155
1156     if( !psz_parser ) goto aspect_end;
1157
1158     i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1159     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1160
1161     i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1162     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1163
1164     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1165     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1166     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1167     p_vout->fmt_in.i_sar_num = i_sar_num;
1168     p_vout->fmt_in.i_sar_den = i_sar_den;
1169     p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1170     p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1171
1172  aspect_end:
1173     if( p_vout->p->i_par_num && p_vout->p->i_par_den )
1174     {
1175         p_vout->fmt_in.i_sar_num *= p_vout->p->i_par_den;
1176         p_vout->fmt_in.i_sar_den *= p_vout->p->i_par_num;
1177         p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1178             p_vout->p->i_par_den / p_vout->p->i_par_num;
1179         p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1180     }
1181
1182     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1183
1184     vlc_ureduce( &i_aspect_num, &i_aspect_den,
1185                  p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1186     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1187              i_aspect_num, i_aspect_den,
1188              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1189
1190     if( var_Get( p_vout, "crop", &val ) )
1191         return VLC_EGENERIC;
1192
1193     int i_ret = CropCallback( p_this, "crop", val, val, 0 );
1194     free( val.psz_string );
1195     return i_ret;
1196 }
1197
1198 static int ScalingCallback( vlc_object_t *p_this, char const *psz_cmd,
1199                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1200 {
1201     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1202     (void)oldval; (void)newval; (void)p_data;
1203
1204     vlc_mutex_lock( &p_vout->change_lock );
1205
1206     if( !strcmp( psz_cmd, "autoscale" ) )
1207     {
1208         p_vout->i_changes |= VOUT_SCALE_CHANGE;
1209     }
1210     else if( !strcmp( psz_cmd, "scale" ) )
1211     {
1212         p_vout->i_changes |= VOUT_ZOOM_CHANGE;
1213     }
1214
1215     vlc_mutex_unlock( &p_vout->change_lock );
1216
1217     return VLC_SUCCESS;
1218 }
1219
1220 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1221                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1222 {
1223     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1224     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
1225     (void)psz_cmd; (void)oldval; (void)p_data;
1226
1227     /* Modify libvlc as well because the vout might have to be restarted */
1228     var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
1229     var_Set( p_vout->p_libvlc, "video-on-top", newval );
1230
1231     return VLC_SUCCESS;
1232 }
1233
1234 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1235                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1236 {
1237     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1238     vlc_value_t val;
1239     (void)psz_cmd; (void)oldval; (void)p_data;
1240
1241     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1242
1243     /* Modify libvlc as well because the vout might have to be restarted */
1244     var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
1245     var_Set( p_vout->p_libvlc, "fullscreen", newval );
1246
1247     val.b_bool = true;
1248     var_Set( p_vout, "intf-change", val );
1249     return VLC_SUCCESS;
1250 }
1251
1252 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1253                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1254 {
1255     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1256
1257     /* FIXME: this is definitely not thread-safe */
1258     p_vout->p->b_snapshot = true;
1259     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1260     VLC_UNUSED(newval); VLC_UNUSED(p_data);
1261     return VLC_SUCCESS;
1262 }
1263
1264 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1265                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1266 {
1267     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1268     VLC_UNUSED(p_data);
1269     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1270     p_vout->p->b_title_show = newval.b_bool;
1271     return VLC_SUCCESS;
1272 }
1273
1274 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1275                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1276 {
1277     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1278     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1279     p_vout->p->i_title_timeout = (mtime_t) newval.i_int;
1280     return VLC_SUCCESS;
1281 }
1282
1283 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1284                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1285 {
1286     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1287     VLC_UNUSED(p_data);
1288     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1289     p_vout->p->i_title_position = newval.i_int;
1290     return VLC_SUCCESS;
1291 }