]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
dcfb063b0a0bc132bd68c9772fadfdaf599c7ced
[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 OnTopCallback( vlc_object_t *, char const *,
69                           vlc_value_t, vlc_value_t, void * );
70 static int FullscreenCallback( vlc_object_t *, char const *,
71                                vlc_value_t, vlc_value_t, void * );
72 static int SnapshotCallback( vlc_object_t *, char const *,
73                              vlc_value_t, vlc_value_t, void * );
74
75 static int TitleShowCallback( vlc_object_t *, char const *,
76                               vlc_value_t, vlc_value_t, void * );
77 static int TitleTimeoutCallback( vlc_object_t *, char const *,
78                                  vlc_value_t, vlc_value_t, void * );
79 static int TitlePositionCallback( vlc_object_t *, char const *,
80                                   vlc_value_t, vlc_value_t, void * );
81
82 /**
83  * Creates a video output window.
84  * On Unix systems, this returns an X11 drawable handle.
85  * On Windows, this returns a Win32 window handle.
86  * Video output plugins are supposed to called this function and display the
87  * video within the resulting window, while in windowed mode.
88  *
89  * Note that a given video output thread must not allocate more than one
90  * such window at a time.
91  *
92  * @param p_vout video output thread to create a window for
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 platform-specific window handle, or NULL in case of failure.
99  * In case of success, the handle is released with vout_ReleaseWindow().
100  */
101 void *vout_RequestWindow( vout_thread_t *p_vout,
102                           int *pi_x_hint, int *pi_y_hint,
103                           unsigned int *pi_width_hint,
104                           unsigned int *pi_height_hint )
105 {
106     assert (p_vout->p_window == NULL);
107
108     /* Get requested coordinates */
109     *pi_x_hint = var_GetInteger( p_vout, "video-x" );
110     *pi_y_hint = var_GetInteger( p_vout, "video-y" );
111
112     *pi_width_hint = p_vout->i_window_width;
113     *pi_height_hint = p_vout->i_window_height;
114
115     /* Check whether someone provided us with a window ID */
116     int drawable = var_CreateGetInteger( p_vout, "drawable" );
117     if( drawable ) return (void *)(intptr_t)drawable;
118
119     vout_window_t *wnd = vlc_custom_create (VLC_OBJECT(p_vout), sizeof (*wnd),
120                                             VLC_OBJECT_GENERIC, "window");
121     if (wnd == NULL)
122         return NULL;
123
124     wnd->vout = p_vout;
125     wnd->width = *pi_width_hint;
126     wnd->height = *pi_height_hint;
127     wnd->pos_x = *pi_x_hint;
128     wnd->pos_y = *pi_y_hint;
129     vlc_object_attach (wnd, p_vout);
130
131     wnd->module = module_need (wnd, "vout_window", NULL, false);
132     if (wnd->module == NULL)
133     {
134         msg_Dbg (wnd, "no window provider available");
135         vlc_object_release (wnd);
136         return NULL;
137     }
138     p_vout->p_window = wnd;
139     *pi_width_hint = wnd->width;
140     *pi_height_hint = wnd->height;
141     *pi_x_hint = wnd->pos_x;
142     *pi_y_hint = wnd->pos_y;
143     return wnd->handle;
144 }
145
146 /**
147  * Releases a window handle obtained with vout_RequestWindow().
148  * @param p_vout video output thread that allocated the window
149  * @param dummy unused legacy paramater
150  */
151 void vout_ReleaseWindow( vout_thread_t *p_vout, void *dummy )
152 {
153     vout_window_t *wnd = p_vout->p_window;
154
155     if (wnd == NULL)
156         return;
157     p_vout->p_window = NULL;
158
159     assert (wnd->module);
160     module_unneed (wnd, wnd->module);
161
162     vlc_object_release (wnd);
163     (void)dummy;
164 }
165
166 int vout_ControlWindow( vout_thread_t *p_vout, void *dummy,
167                         int i_query, va_list args )
168 {
169     (void)dummy;
170     vout_window_t *wnd = p_vout->p_window;
171
172     if (wnd == NULL)
173         return VLC_EGENERIC;
174
175     assert (wnd->control);
176     return wnd->control (wnd, i_query, args);
177 }
178
179 /*****************************************************************************
180  * vout_IntfInit: called during the vout creation to initialise misc things.
181  *****************************************************************************/
182 static const struct
183 {
184     double f_value;
185     const char *psz_label;
186 } p_zoom_values[] = {
187     { 0.25, N_("1:4 Quarter") },
188     { 0.5, N_("1:2 Half") },
189     { 1, N_("1:1 Original") },
190     { 2, N_("2:1 Double") },
191     { 0, NULL } };
192
193 static const struct
194 {
195     const char *psz_value;
196     const char *psz_label;
197 } p_crop_values[] = {
198     { "", N_("Default") },
199     { "16:10", "16:10" },
200     { "16:9", "16:9" },
201     { "185:100", "1.85:1" },
202     { "221:100", "2.21:1" },
203     { "235:100", "2.35:1" },
204     { "239:100", "2.39:1" },
205     { "5:3", "5:3" },
206     { "4:3", "4:3" },
207     { "5:4", "5:4" },
208     { "1:1", "1:1" },
209     { NULL, NULL } };
210
211 static const struct
212 {
213     const char *psz_value;
214     const char *psz_label;
215 } p_aspect_ratio_values[] = {
216     { "", N_("Default") },
217     { "1:1", "1:1" },
218     { "4:3", "4:3" },
219     { "16:9", "16:9" },
220     { "16:10", "16:10" },
221     { "221:100", "2.21:1" },
222     { "5:4", "5:4" },
223     { NULL, NULL } };
224
225 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
226                              char *psz_list )
227 {
228     if( psz_list && *psz_list )
229     {
230         char *psz_cur = psz_list;
231         char *psz_next;
232         while( psz_cur && *psz_cur )
233         {
234             vlc_value_t val, text;
235             psz_next = strchr( psz_cur, ',' );
236             if( psz_next )
237             {
238                 *psz_next = '\0';
239                 psz_next++;
240             }
241             val.psz_string = psz_cur;
242             text.psz_string = psz_cur;
243             var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
244             psz_cur = psz_next;
245         }
246     }
247 }
248
249 void vout_IntfInit( vout_thread_t *p_vout )
250 {
251     vlc_value_t val, text, old_val;
252     bool b_force_par = false;
253     char *psz_buf;
254     int i;
255
256     /* Create a few object variables we'll need later on */
257     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
258     var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
259     var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
260     var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
261     var_Create( p_vout, "snapshot-sequential",
262                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
263     var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
264     var_SetInteger( p_vout, "snapshot-num", 1 );
265     var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
266     var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
267
268     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
269     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
270     p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
271
272     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
273     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
274
275     var_Create( p_vout, "mouse-hide-timeout",
276                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
277
278     p_vout->p->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
279     p_vout->p->i_title_timeout =
280         (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
281     p_vout->p->i_title_position =
282         var_CreateGetInteger( p_vout, "video-title-position" );
283     p_vout->p->psz_title =  NULL;
284
285     var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
286     var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
287     var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
288
289     /* Zoom object var */
290     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
291                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
292
293     text.psz_string = _("Zoom");
294     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
295
296     var_Get( p_vout, "zoom", &old_val );
297
298     for( i = 0; p_zoom_values[i].f_value; i++ )
299     {
300         if( old_val.f_float == p_zoom_values[i].f_value )
301             var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
302         val.f_float = p_zoom_values[i].f_value;
303         text.psz_string = _( p_zoom_values[i].psz_label );
304         var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
305     }
306
307     var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
308
309     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
310
311     /* Crop offset vars */
312     var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
313     var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
314     var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
315     var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
316
317     var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
318     var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
319     var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
320     var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
321
322     /* Crop object var */
323     var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
324                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
325
326     text.psz_string = _("Crop");
327     var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
328
329     val.psz_string = (char*)"";
330     var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
331
332     for( i = 0; p_crop_values[i].psz_value; i++ )
333     {
334         val.psz_string = (char*)p_crop_values[i].psz_value;
335         text.psz_string = _( p_crop_values[i].psz_label );
336         var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
337     }
338
339     /* update triggered every time the vout's crop parameters are changed */
340     var_Create( p_vout, "crop-update", VLC_VAR_VOID );
341
342     /* Add custom crop ratios */
343     psz_buf = config_GetPsz( p_vout, "custom-crop-ratios" );
344     AddCustomRatios( p_vout, "crop", psz_buf );
345     free( psz_buf );
346
347     var_AddCallback( p_vout, "crop", CropCallback, NULL );
348     var_Get( p_vout, "crop", &old_val );
349     if( old_val.psz_string && *old_val.psz_string )
350         var_TriggerCallback( p_vout, "crop" );
351     free( old_val.psz_string );
352
353     /* Monitor pixel aspect-ratio */
354     var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
355     var_Get( p_vout, "monitor-par", &val );
356     if( val.psz_string && *val.psz_string )
357     {
358         char *psz_parser = strchr( val.psz_string, ':' );
359         unsigned int i_aspect_num = 0, i_aspect_den = 0;
360         float i_aspect = 0;
361         if( psz_parser )
362         {
363             i_aspect_num = strtol( val.psz_string, 0, 10 );
364             i_aspect_den = strtol( ++psz_parser, 0, 10 );
365         }
366         else
367         {
368             i_aspect = atof( val.psz_string );
369             vlc_ureduce( &i_aspect_num, &i_aspect_den,
370                          i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
371         }
372         if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
373
374         p_vout->p->i_par_num = i_aspect_num;
375         p_vout->p->i_par_den = i_aspect_den;
376
377         vlc_ureduce( &p_vout->p->i_par_num, &p_vout->p->i_par_den,
378                      p_vout->p->i_par_num, p_vout->p->i_par_den, 0 );
379
380         msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
381                  p_vout->p->i_par_num, p_vout->p->i_par_den );
382         b_force_par = true;
383     }
384     free( val.psz_string );
385
386     /* Aspect-ratio object var */
387     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
388                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
389
390     text.psz_string = _("Aspect-ratio");
391     var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
392
393     val.psz_string = (char*)"";
394     var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
395
396     for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
397     {
398         val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
399         text.psz_string = _( p_aspect_ratio_values[i].psz_label );
400         var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
401     }
402
403     /* Add custom aspect ratios */
404     psz_buf = config_GetPsz( p_vout, "custom-aspect-ratios" );
405     AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
406     free( psz_buf );
407
408     var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
409     var_Get( p_vout, "aspect-ratio", &old_val );
410     if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
411         var_TriggerCallback( p_vout, "aspect-ratio" );
412     free( old_val.psz_string );
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_INTEGER );
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         vlc_object_t* p_dest = p_obj;
656         block_t *p_block;
657         snapshot_t *p_snapshot;
658         size_t i_size;
659
660         vlc_object_lock( p_dest );
661         p_dest->p_private = NULL;
662
663         /* Save the snapshot to a memory zone */
664         p_block = image_Write( p_image, p_pic, &fmt_in, &fmt_out );
665         if( !p_block )
666         {
667             msg_Err( p_vout, "Could not get snapshot" );
668             image_HandlerDelete( p_image );
669             vlc_object_signal_unlocked( p_dest );
670             vlc_object_unlock( p_dest );
671             return VLC_EGENERIC;
672         }
673
674         /* Copy the p_block data to a snapshot structure */
675         /* FIXME: get the timestamp */
676         p_snapshot = malloc( sizeof( snapshot_t ) );
677         if( !p_snapshot )
678         {
679             block_Release( p_block );
680             image_HandlerDelete( p_image );
681             vlc_object_signal_unlocked( p_dest );
682             vlc_object_unlock( p_dest );
683             return VLC_ENOMEM;
684         }
685
686         i_size = p_block->i_buffer;
687
688         p_snapshot->i_width = fmt_out.i_width;
689         p_snapshot->i_height = fmt_out.i_height;
690         p_snapshot->i_datasize = i_size;
691         p_snapshot->date = p_block->i_pts; /* FIXME ?? */
692         p_snapshot->p_data = malloc( i_size );
693         if( !p_snapshot->p_data )
694         {
695             block_Release( p_block );
696             free( p_snapshot );
697             image_HandlerDelete( p_image );
698             vlc_object_signal_unlocked( p_dest );
699             vlc_object_unlock( p_dest );
700             return VLC_ENOMEM;
701         }
702         memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
703
704         p_dest->p_private = p_snapshot;
705
706         block_Release( p_block );
707
708         /* Unlock the object */
709         vlc_object_signal_unlocked( p_dest );
710         vlc_object_unlock( p_dest );
711
712         image_HandlerDelete( p_image );
713         return VLC_SUCCESS;
714     }
715
716     /* Get default directory if none provided */
717     if( !val.psz_string )
718         val.psz_string = VoutSnapshotGetDefaultDirectory( );
719     if( !val.psz_string )
720     {
721         msg_Err( p_vout, "no path specified for snapshots" );
722         image_HandlerDelete( p_image );
723         return VLC_EGENERIC;
724     }
725
726     /* Get snapshot format, default being "png" */
727     format.psz_string = var_GetNonEmptyString( p_vout, "snapshot-format" );
728     if( !format.psz_string )
729         format.psz_string = strdup( "png" );
730     if( !format.psz_string )
731     {
732         free( val.psz_string );
733         image_HandlerDelete( p_image );
734         return VLC_ENOMEM;
735     }
736
737     /*
738      * Did the user specify a directory? If not, path = NULL.
739      */
740     path = utf8_opendir ( (const char *)val.psz_string  );
741     if( path != NULL )
742     {
743         char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
744         if( psz_prefix == NULL )
745             psz_prefix = strdup( "vlcsnap-" );
746         else
747         {
748             char *psz_tmp = str_format( p_vout, psz_prefix );
749             filename_sanitize( psz_tmp );
750             free( psz_prefix );
751             psz_prefix = psz_tmp;
752         }
753
754         closedir( path );
755         if( var_GetBool( p_vout, "snapshot-sequential" ) == true )
756         {
757             int i_num = var_GetInteger( p_vout, "snapshot-num" );
758             struct stat st;
759
760             do
761             {
762                 free( psz_filename );
763                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
764                               val.psz_string, psz_prefix, i_num++,
765                               format.psz_string ) == -1 )
766                 {
767                     msg_Err( p_vout, "could not create snapshot" );
768                     image_HandlerDelete( p_image );
769                     return VLC_EGENERIC;
770                 }
771             }
772             while( utf8_stat( psz_filename, &st ) == 0 );
773
774             var_SetInteger( p_vout, "snapshot-num", i_num );
775         }
776         else
777         {
778             struct tm    curtime;
779             time_t        lcurtime ;
780             lcurtime = time( NULL ) ;
781             if ( ( localtime_r( &lcurtime, &curtime ) == NULL ) )
782             {
783                 msg_Warn( p_vout, "failed to get current time. Falling back to legacy snapshot naming" );
784                 /* failed to get current time. Fallback to old format */
785                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
786                           val.psz_string, psz_prefix,
787                           (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
788                           format.psz_string ) == -1 )
789                 {
790                     msg_Err( p_vout, "could not create snapshot" );
791                     image_HandlerDelete( p_image );
792                     return VLC_EGENERIC;
793                 }
794             }
795             else
796             {
797                 char psz_curtime[15] ;
798                 if( strftime( psz_curtime, 15, "%y%m%d-%H%M%S", &curtime ) == 0 )
799                 {
800                     msg_Warn( p_vout, "snapshot date string truncated" ) ;
801                 }
802                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%s%1u.%s",
803                       val.psz_string, psz_prefix, psz_curtime,
804                      /* suffix with the last decimal digit in 10s of seconds resolution */
805                      (unsigned int)(p_pic->date / (100*1000)) & 0xFF,
806                       format.psz_string ) == -1 )
807                 {
808                     msg_Err( p_vout, "could not create snapshot" );
809                     image_HandlerDelete( p_image );
810                     return VLC_EGENERIC;
811                 }
812             } //end if time() < 0
813         } //end snapshot sequential
814         free( psz_prefix );
815     }
816     else // The user specified a full path name (including file name)
817     {
818         psz_filename = str_format( p_vout, val.psz_string );
819         path_sanitize( psz_filename );
820     }
821
822     free( val.psz_string );
823     free( format.psz_string );
824
825     /* Save the snapshot */
826     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
827     if( i_ret != VLC_SUCCESS )
828     {
829         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
830         free( psz_filename );
831         image_HandlerDelete( p_image );
832         return VLC_EGENERIC;
833     }
834
835     /* */
836     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
837     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
838                      "%s", psz_filename );
839
840     /* Generate a media player event  - Right now just trigger a global libvlc var
841         CHECK: Could not find a more local object. The goal is to communicate
842         vout_thread with libvlc_media_player or its input_thread*/
843     val.psz_string =  psz_filename  ;
844
845     var_Set( p_vout->p_libvlc, "vout-snapshottaken", val );
846     /* var_Set duplicates data for transport so we can free*/
847     free( psz_filename );
848
849     /* */
850     if( var_GetBool( p_vout, "snapshot-preview" ) )
851     {
852         if( VoutSnapshotPip( p_vout, p_image, p_pic, &fmt_in ) )
853             msg_Warn( p_vout, "Failed to display snapshot" );
854     }
855     image_HandlerDelete( p_image );
856
857     return VLC_SUCCESS;
858 }
859
860 /*****************************************************************************
861  * Handle filters
862  *****************************************************************************/
863
864 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
865                         bool b_add, bool b_setconfig )
866 {
867     char *psz_parser;
868     char *psz_string = config_GetPsz( p_vout, "vout-filter" );
869
870     /* Todo : Use some generic chain manipulation functions */
871     if( !psz_string ) psz_string = strdup("");
872
873     psz_parser = strstr( psz_string, psz_name );
874     if( b_add )
875     {
876         if( !psz_parser )
877         {
878             psz_parser = psz_string;
879             if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
880                           psz_string, psz_name ) == -1 )
881             {
882                 free( psz_parser );
883                 return;
884             }
885             free( psz_parser );
886         }
887         else
888             return;
889     }
890     else
891     {
892         if( psz_parser )
893         {
894             memmove( psz_parser, psz_parser + strlen(psz_name) +
895                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
896                             strlen(psz_parser + strlen(psz_name)) + 1 );
897
898             /* Remove trailing : : */
899             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
900             {
901                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
902             }
903          }
904          else
905          {
906              free( psz_string );
907              return;
908          }
909     }
910     if( b_setconfig )
911         config_PutPsz( p_vout, "vout-filter", psz_string );
912
913     var_SetString( p_vout, "vout-filter", psz_string );
914     free( psz_string );
915 }
916
917 /*****************************************************************************
918  * vout_ControlDefault: default methods for video output control.
919  *****************************************************************************/
920 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
921 {
922     (void)args;
923     switch( i_query )
924     {
925     case VOUT_REPARENT:
926     case VOUT_CLOSE:
927         vout_ReleaseWindow( p_vout, NULL );
928         return VLC_SUCCESS;
929
930     case VOUT_SNAPSHOT:
931         p_vout->p->b_snapshot = true;
932         return VLC_SUCCESS;
933
934     default:
935         msg_Dbg( p_vout, "control query not supported" );
936     }
937     return VLC_EGENERIC;
938 }
939
940 /*****************************************************************************
941  * InitWindowSize: find the initial dimensions the video window should have.
942  *****************************************************************************
943  * This function will check the "width", "height" and "zoom" config options and
944  * will calculate the size that the video window should have.
945  *****************************************************************************/
946 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
947                             unsigned *pi_height )
948 {
949     vlc_value_t val;
950     int i_width, i_height;
951     uint64_t ll_zoom;
952
953 #define FP_FACTOR 1000                             /* our fixed point factor */
954
955     var_Get( p_vout, "width", &val );
956     i_width = val.i_int;
957     var_Get( p_vout, "height", &val );
958     i_height = val.i_int;
959     var_Get( p_vout, "zoom", &val );
960     ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
961
962     if( i_width > 0 && i_height > 0)
963     {
964         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
965         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
966         goto initwsize_end;
967     }
968     else if( i_width > 0 )
969     {
970         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
971         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
972             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
973             FP_FACTOR / p_vout->fmt_in.i_visible_width );
974         goto initwsize_end;
975     }
976     else if( i_height > 0 )
977     {
978         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
979         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
980             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
981             FP_FACTOR / p_vout->fmt_in.i_visible_height );
982         goto initwsize_end;
983     }
984
985     if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
986         msg_Warn( p_vout, "fucked up aspect" );
987         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
988         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
989     }
990     else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
991     {
992         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
993             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
994         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
995             / FP_FACTOR );
996     }
997     else
998     {
999         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
1000             / FP_FACTOR );
1001         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
1002             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
1003     }
1004
1005 initwsize_end:
1006     msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width,
1007              p_vout->i_window_height );
1008
1009 #undef FP_FACTOR
1010 }
1011
1012 /*****************************************************************************
1013  * Object variables callbacks
1014  *****************************************************************************/
1015 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
1016                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1017 {
1018     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1019     (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
1020     InitWindowSize( p_vout, &p_vout->i_window_width,
1021                     &p_vout->i_window_height );
1022     vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
1023                   p_vout->i_window_height );
1024     return VLC_SUCCESS;
1025 }
1026
1027 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
1028                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1029 {
1030     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1031     int64_t i_aspect_num, i_aspect_den;
1032     unsigned int i_width, i_height;
1033
1034     (void)oldval; (void)p_data;
1035
1036     /* Restore defaults */
1037     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
1038     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
1039     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
1040     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
1041
1042     if( !strcmp( psz_cmd, "crop" ) )
1043     {
1044         char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
1045         if( psz_parser )
1046         {
1047             /* We're using the 3:4 syntax */
1048             i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1049             if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
1050
1051             i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1052             if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
1053
1054             i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
1055                 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
1056             i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
1057                 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
1058
1059             if( i_width < p_vout->fmt_render.i_visible_width )
1060             {
1061                 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
1062                     (p_vout->fmt_render.i_visible_width - i_width) / 2;
1063                 p_vout->fmt_in.i_visible_width = i_width;
1064             }
1065             else
1066             {
1067                 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
1068                     (p_vout->fmt_render.i_visible_height - i_height) / 2;
1069                 p_vout->fmt_in.i_visible_height = i_height;
1070             }
1071         }
1072         else
1073         {
1074             psz_parser = strchr( newval.psz_string, 'x' );
1075             if( psz_parser )
1076             {
1077                 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
1078                 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
1079
1080                 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
1081                 if( psz_end != psz_parser ) goto crop_end;
1082
1083                 psz_parser = strchr( ++psz_end, '+' );
1084                 i_crop_height = strtol( psz_end, &psz_end, 10 );
1085                 if( psz_end != psz_parser ) goto crop_end;
1086
1087                 psz_parser = strchr( ++psz_end, '+' );
1088                 i_crop_left = strtol( psz_end, &psz_end, 10 );
1089                 if( psz_end != psz_parser ) goto crop_end;
1090
1091                 psz_end++;
1092                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1093                 if( *psz_end != '\0' ) goto crop_end;
1094
1095                 i_width = i_crop_width;
1096                 p_vout->fmt_in.i_visible_width = i_width;
1097
1098                 i_height = i_crop_height;
1099                 p_vout->fmt_in.i_visible_height = i_height;
1100
1101                 p_vout->fmt_in.i_x_offset = i_crop_left;
1102                 p_vout->fmt_in.i_y_offset = i_crop_top;
1103             }
1104             else
1105             {
1106                 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1107                 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1108
1109                 psz_parser = strchr( newval.psz_string, '+' );
1110                 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1111                 if( psz_end != psz_parser ) goto crop_end;
1112
1113                 psz_parser = strchr( ++psz_end, '+' );
1114                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1115                 if( psz_end != psz_parser ) goto crop_end;
1116
1117                 psz_parser = strchr( ++psz_end, '+' );
1118                 i_crop_right = strtol( psz_end, &psz_end, 10 );
1119                 if( psz_end != psz_parser ) goto crop_end;
1120
1121                 psz_end++;
1122                 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1123                 if( *psz_end != '\0' ) goto crop_end;
1124
1125                 i_width = p_vout->fmt_render.i_visible_width
1126                           - i_crop_left - i_crop_right;
1127                 p_vout->fmt_in.i_visible_width = i_width;
1128
1129                 i_height = p_vout->fmt_render.i_visible_height
1130                            - i_crop_top - i_crop_bottom;
1131                 p_vout->fmt_in.i_visible_height = i_height;
1132
1133                 p_vout->fmt_in.i_x_offset = i_crop_left;
1134                 p_vout->fmt_in.i_y_offset = i_crop_top;
1135             }
1136         }
1137     }
1138     else if( !strcmp( psz_cmd, "crop-top" )
1139           || !strcmp( psz_cmd, "crop-left" )
1140           || !strcmp( psz_cmd, "crop-bottom" )
1141           || !strcmp( psz_cmd, "crop-right" ) )
1142     {
1143         unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1144
1145         i_crop_top = var_GetInteger( p_vout, "crop-top" );
1146         i_crop_left = var_GetInteger( p_vout, "crop-left" );
1147         i_crop_right = var_GetInteger( p_vout, "crop-right" );
1148         i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1149
1150         i_width = p_vout->fmt_render.i_visible_width
1151                   - i_crop_left - i_crop_right;
1152         p_vout->fmt_in.i_visible_width = i_width;
1153
1154         i_height = p_vout->fmt_render.i_visible_height
1155                    - i_crop_top - i_crop_bottom;
1156         p_vout->fmt_in.i_visible_height = i_height;
1157
1158         p_vout->fmt_in.i_x_offset = i_crop_left;
1159         p_vout->fmt_in.i_y_offset = i_crop_top;
1160     }
1161
1162  crop_end:
1163     InitWindowSize( p_vout, &p_vout->i_window_width,
1164                     &p_vout->i_window_height );
1165
1166     p_vout->i_changes |= VOUT_CROP_CHANGE;
1167
1168     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1169              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1170              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1171              p_vout->fmt_in.i_visible_width,
1172              p_vout->fmt_in.i_visible_height );
1173
1174     var_SetVoid( p_vout, "crop-update" );
1175
1176     return VLC_SUCCESS;
1177 }
1178
1179 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1180                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1181 {
1182     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1183     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1184     vlc_value_t val;
1185
1186     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1187     (void)psz_cmd; (void)oldval; (void)p_data;
1188
1189     /* Restore defaults */
1190     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1191     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1192     p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1193     p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1194
1195     if( !psz_parser ) goto aspect_end;
1196
1197     i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1198     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1199
1200     i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1201     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1202
1203     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1204     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1205     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1206     p_vout->fmt_in.i_sar_num = i_sar_num;
1207     p_vout->fmt_in.i_sar_den = i_sar_den;
1208     p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1209     p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1210
1211  aspect_end:
1212     if( p_vout->p->i_par_num && p_vout->p->i_par_den )
1213     {
1214         p_vout->fmt_in.i_sar_num *= p_vout->p->i_par_den;
1215         p_vout->fmt_in.i_sar_den *= p_vout->p->i_par_num;
1216         p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1217             p_vout->p->i_par_den / p_vout->p->i_par_num;
1218         p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1219     }
1220
1221     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1222
1223     vlc_ureduce( &i_aspect_num, &i_aspect_den,
1224                  p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1225     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1226              i_aspect_num, i_aspect_den,
1227              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1228
1229     if( var_Get( p_vout, "crop", &val ) )
1230         return VLC_EGENERIC;
1231
1232     int i_ret = CropCallback( p_this, "crop", val, val, 0 );
1233     free( val.psz_string );
1234     return i_ret;
1235 }
1236
1237 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1238                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1239 {
1240     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1241     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
1242     (void)psz_cmd; (void)oldval; (void)p_data;
1243
1244     /* Modify libvlc as well because the vout might have to be restarted */
1245     var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
1246     var_Set( p_vout->p_libvlc, "video-on-top", newval );
1247
1248     return VLC_SUCCESS;
1249 }
1250
1251 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1252                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1253 {
1254     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1255     vlc_value_t val;
1256     (void)psz_cmd; (void)oldval; (void)p_data;
1257
1258     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1259
1260     /* Modify libvlc as well because the vout might have to be restarted */
1261     var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
1262     var_Set( p_vout->p_libvlc, "fullscreen", newval );
1263
1264     val.b_bool = true;
1265     var_Set( p_vout, "intf-change", val );
1266     return VLC_SUCCESS;
1267 }
1268
1269 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1270                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1271 {
1272     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1273     VLC_UNUSED(newval); VLC_UNUSED(p_data);
1274     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1275     vout_Control( p_vout, VOUT_SNAPSHOT );
1276     return VLC_SUCCESS;
1277 }
1278
1279 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1280                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1281 {
1282     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1283     VLC_UNUSED(p_data);
1284     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1285     p_vout->p->b_title_show = newval.b_bool;
1286     return VLC_SUCCESS;
1287 }
1288
1289 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1290                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1291 {
1292     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1293     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1294     p_vout->p->i_title_timeout = (mtime_t) newval.i_int;
1295     return VLC_SUCCESS;
1296 }
1297
1298 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1299                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1300 {
1301     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1302     VLC_UNUSED(p_data);
1303     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1304     p_vout->p->i_title_position = newval.i_int;
1305     return VLC_SUCCESS;
1306 }