]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
Remove uneeded asprintf.
[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, picture_t *p_pic )
463 {
464     subpicture_t *p_subpic = subpicture_NewFromPicture( VLC_OBJECT(p_vout),
465                                                         p_pic, VLC_CODEC_YUVA );
466     if( !p_subpic )
467         return VLC_EGENERIC;
468
469     /* FIXME DEFAULT_CHAN is not good (used by the text) but
470      * hardcoded 0 doesn't seem right */
471     p_subpic->i_channel = 0;
472     p_subpic->i_start = mdate();
473     p_subpic->i_stop  = p_subpic->i_start + 4000000;
474     p_subpic->b_ephemer = true;
475     p_subpic->b_fade = true;
476
477     /* Reduce the picture to 1/4^2 of the screen */
478     p_subpic->i_original_picture_width  *= 4;
479     p_subpic->i_original_picture_height *= 4;
480
481     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
482     return VLC_SUCCESS;
483 }
484 /**
485  * This function will return the default directory used for snapshots
486  */
487 static char *VoutSnapshotGetDefaultDirectory( void )
488 {
489     char *psz_path = NULL;
490 #if defined(__APPLE__) || defined(SYS_BEOS)
491
492     if( asprintf( &psz_path, "%s/Desktop",
493                   config_GetHomeDir() ) == -1 )
494         psz_path = NULL;
495
496 #elif defined(WIN32) && !defined(UNDER_CE)
497
498     /* Get the My Pictures folder path */
499     char *p_mypicturesdir = NULL;
500     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
501                                                LPWSTR );
502     #ifndef CSIDL_FLAG_CREATE
503     #   define CSIDL_FLAG_CREATE 0x8000
504     #endif
505     #ifndef CSIDL_MYPICTURES
506     #   define CSIDL_MYPICTURES 0x27
507     #endif
508     #ifndef SHGFP_TYPE_CURRENT
509     #   define SHGFP_TYPE_CURRENT 0
510     #endif
511
512     HINSTANCE shfolder_dll;
513     SHGETFOLDERPATH SHGetFolderPath ;
514
515     /* load the shfolder dll to retrieve SHGetFolderPath */
516     if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
517     {
518        wchar_t wdir[PATH_MAX];
519        SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
520                                                   _T("SHGetFolderPathW") );
521         if ((SHGetFolderPath != NULL )
522          && SUCCEEDED (SHGetFolderPath (NULL,
523                                        CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
524                                        NULL, SHGFP_TYPE_CURRENT,
525                                        wdir)))
526             p_mypicturesdir = FromWide (wdir);
527
528         FreeLibrary( shfolder_dll );
529     }
530
531     if( p_mypicturesdir == NULL )
532         psz_path = strdup( config_GetHomeDir() );
533     else
534         psz_path = p_mypicturesdir;
535
536 #else
537
538     /* XXX: This saves in the data directory. Shouldn't we try saving
539      *      to psz_homedir/Desktop or something nicer ? */
540     psz_path = config_GetUserDataDir();
541
542 #endif
543
544     return psz_path;
545 }
546 /**
547  * This function will save a video snapshot to a file
548  */
549 static int VoutWriteSnapshot( vout_thread_t *p_vout, char **ppsz_filename,
550                               const block_t *p_image,
551                               const char *psz_path,
552                               const char *psz_format,
553                               const char *psz_prefix_fmt )
554 {
555     /* */
556     char *psz_filename;
557     DIR *p_path = utf8_opendir( psz_path );
558     if( p_path != NULL )
559     {
560         /* The use specified a directory path */
561         closedir( p_path );
562
563         /* */
564         char *psz_prefix = NULL;
565         if( psz_prefix_fmt )
566             psz_prefix = str_format( p_vout, psz_prefix_fmt );
567         if( !psz_prefix )
568         {
569             psz_prefix = strdup( "vlcsnap-" );
570             if( !psz_prefix )
571                 goto error;
572         }
573
574         if( var_GetBool( p_vout, "snapshot-sequential" ) )
575         {
576             int i_num = var_GetInteger( p_vout, "snapshot-num" );
577             for( ; ; i_num++ )
578             {
579                 struct stat st;
580
581                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
582                               psz_path, psz_prefix, i_num++, psz_format ) < 0 )
583                 {
584                     free( psz_prefix );
585                     goto error;
586                 }
587                 if( utf8_stat( psz_filename, &st ) )
588                     break;
589                 free( psz_filename );
590             }
591
592             var_SetInteger( p_vout, "snapshot-num", i_num );
593         }
594         else
595         {
596             struct tm    curtime;
597             time_t       lcurtime = time( NULL ) ;
598
599             if( !localtime_r( &lcurtime, &curtime ) )
600             {
601                 const unsigned int i_id = (p_image->i_pts / 100000) & 0xFFFFFF;
602
603                 msg_Warn( p_vout, "failed to get current time. Falling back to legacy snapshot naming" );
604
605                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
606                               psz_path, psz_prefix, i_id, psz_format ) < 0 )
607                     psz_filename = NULL;
608             }
609             else
610             {
611                 /* suffix with the last decimal digit in 10s of seconds resolution
612                  * FIXME gni ? */
613                 const int i_id = (p_image->i_pts / (100*1000)) & 0xFF;
614                 char psz_curtime[128];
615
616                 if( !strftime( psz_curtime, sizeof(psz_curtime), "%Y-%m-%d-%Hh%Mm%Ss", &curtime ) )
617                     strcpy( psz_curtime, "error" );
618
619                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%s%1u.%s",
620                               psz_path, psz_prefix, psz_curtime, i_id, psz_format ) < 0 )
621                     psz_filename = NULL;
622             }
623         }
624         free( psz_prefix );
625     }
626     else
627     {
628         /* The user specified a full path name (including file name) */
629         psz_filename = str_format( p_vout, psz_path );
630         path_sanitize( psz_filename );
631     }
632
633     if( !psz_filename )
634         goto error;
635
636     /* Save the snapshot */
637     FILE *p_file = utf8_fopen( psz_filename, "wb" );
638     if( !p_file )
639     {
640         msg_Err( p_vout, "Failed to open '%s'", psz_filename );
641         free( psz_filename );
642         goto error;
643     }
644     if( fwrite( p_image->p_buffer, p_image->i_buffer, 1, p_file ) != 1 )
645     {
646         msg_Err( p_vout, "Failed to write to '%s'", psz_filename );
647         fclose( p_file );
648         free( psz_filename );
649         goto error;
650     }
651     fclose( p_file );
652
653     /* */
654     if( ppsz_filename )
655         *ppsz_filename = psz_filename;
656     else
657         free( psz_filename );
658
659     return VLC_SUCCESS;
660
661 error:
662     msg_Err( p_vout, "could not save snapshot" );
663     return VLC_EGENERIC;
664 }
665
666 /**
667  * This function will display the name and a PIP of the provided snapshot
668  */
669 static void VoutOsdSnapshot( vout_thread_t *p_vout, picture_t *p_pic, const char *psz_filename )
670 {
671     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
672     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN, "%s", psz_filename );
673
674     if( var_GetBool( p_vout, "snapshot-preview" ) )
675     {
676         if( VoutSnapshotPip( p_vout, p_pic ) )
677             msg_Warn( p_vout, "Failed to display snapshot" );
678     }
679 }
680
681 /* */
682 int vout_GetSnapshot( vout_thread_t *p_vout,
683                       block_t **pp_image, picture_t **pp_picture,
684                       video_format_t *p_fmt,
685                       const char *psz_format, mtime_t i_timeout )
686 {
687     vout_thread_sys_t *p_sys = p_vout->p;
688
689     vlc_mutex_lock( &p_sys->snapshot.lock );
690     p_sys->snapshot.i_request++;
691
692     const mtime_t i_deadline = mdate() + i_timeout;
693     while( p_sys->snapshot.b_available && !p_sys->snapshot.p_picture )
694     {
695         if( vlc_cond_timedwait( &p_sys->snapshot.wait, &p_sys->snapshot.lock,
696                                 i_deadline ) )
697             break;
698     }
699
700     picture_t *p_picture = p_sys->snapshot.p_picture;
701     if( p_picture )
702         p_sys->snapshot.p_picture = p_picture->p_next;
703     else if( p_sys->snapshot.i_request > 0 )
704         p_sys->snapshot.i_request--;
705     vlc_mutex_unlock( &p_sys->snapshot.lock );
706
707     if( !p_picture )
708     {
709         msg_Err( p_vout, "Failed to grab a snapshot" );
710         return VLC_EGENERIC;
711     }
712
713     if( pp_image )
714     {
715         vlc_fourcc_t i_format = VLC_CODEC_PNG;
716         if( psz_format && image_Type2Fourcc( psz_format ) )
717             i_format = image_Type2Fourcc( psz_format );
718
719         const int i_override_width  = var_GetInteger( p_vout, "snapshot-width" );
720         const int i_override_height = var_GetInteger( p_vout, "snapshot-height" );
721
722         if( picture_Export( VLC_OBJECT(p_vout), pp_image, p_fmt,
723                             p_picture, i_format, i_override_width, i_override_height ) )
724         {
725             msg_Err( p_vout, "Failed to convert image for snapshot" );
726             picture_Release( p_picture );
727             return VLC_EGENERIC;
728         }
729     }
730     if( pp_picture )
731         *pp_picture = p_picture;
732     else
733         picture_Release( p_picture );
734     return VLC_SUCCESS;
735 }
736
737 /**
738  * This function will handle a snapshot request
739  */
740 static void VoutSaveSnapshot( vout_thread_t *p_vout )
741 {
742     char *psz_path = var_GetNonEmptyString( p_vout, "snapshot-path" );
743     char *psz_format = var_GetNonEmptyString( p_vout, "snapshot-format" );
744     char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
745
746     /* */
747     picture_t *p_picture;
748     block_t *p_image;
749     video_format_t fmt;
750
751     /* 500ms timeout
752      * XXX it will cause trouble with low fps video (< 2fps) */
753     if( vout_GetSnapshot( p_vout, &p_image, &p_picture, &fmt, psz_format, 500*1000 ) )
754     {
755         p_picture = NULL;
756         p_image = NULL;
757         goto exit;
758     }
759
760     if( !psz_path )
761     {
762         psz_path = VoutSnapshotGetDefaultDirectory();
763         if( !psz_path )
764         {
765             msg_Err( p_vout, "no path specified for snapshots" );
766             goto exit;
767         }
768     }
769
770     char *psz_filename;
771     if( VoutWriteSnapshot( p_vout, &psz_filename,
772                            p_image,
773                            psz_path, psz_format, psz_prefix ) )
774         goto exit;
775
776     VoutOsdSnapshot( p_vout, p_picture, psz_filename );
777
778     /* Generate a media player event  - Right now just trigger a global libvlc var
779         CHECK: Could not find a more local object. The goal is to communicate
780         vout_thread with libvlc_media_player or its input_thread */
781     var_SetString( p_vout->p_libvlc, "vout-snapshottaken", psz_filename );
782     free( psz_filename );
783
784 exit:
785     if( p_image )
786         block_Release( p_image );
787     if( p_picture )
788         picture_Release( p_picture );
789     free( psz_prefix );
790     free( psz_format );
791     free( psz_path );
792 }
793
794 /*****************************************************************************
795  * Handle filters
796  *****************************************************************************/
797
798 void vout_EnableFilter( vout_thread_t *p_vout, const char *psz_name,
799                         bool b_add, bool b_setconfig )
800 {
801     char *psz_parser;
802     char *psz_string;
803     const char *psz_filter_type;
804
805     /* FIXME temporary hack */
806     const char *psz_module_name = psz_name;
807     if( !strcmp( psz_name, "magnify" ) ||
808         !strcmp( psz_name, "puzzle" ) ||
809         !strcmp( psz_name, "logo" ) ||
810         !strcmp( psz_name, "wall" ) ||
811         !strcmp( psz_name, "clone" ) )
812         psz_module_name = "video_filter_wrapper";
813
814     module_t *p_obj = module_find( psz_module_name );
815     if( !p_obj )
816     {
817         msg_Err( p_vout, "Unable to find filter module \"%s\".", psz_name );
818         return;
819     }
820
821     if( module_provides( p_obj, "video filter" ) )
822     {
823         psz_filter_type = "vout-filter";
824     }
825     else if( module_provides( p_obj, "video filter2" ) )
826     {
827         psz_filter_type = "video-filter";
828     }
829     else if( module_provides( p_obj, "sub filter" ) )
830     {
831         psz_filter_type = "sub-filter";
832     }
833     else
834     {
835         module_release( p_obj );
836         msg_Err( p_vout, "Unknown video filter type." );
837         return;
838     }
839     module_release( p_obj );
840
841     if( !strcmp( psz_filter_type, "sub-filter") )
842         psz_string = var_GetString( vout_GetSpu( p_vout ), psz_filter_type );
843     else
844         psz_string = var_GetString( p_vout, psz_filter_type );
845
846     /* Todo : Use some generic chain manipulation functions */
847     if( !psz_string ) psz_string = strdup("");
848
849     psz_parser = strstr( psz_string, psz_name );
850     if( b_add )
851     {
852         if( !psz_parser )
853         {
854             psz_parser = psz_string;
855             if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
856                           psz_string, psz_name ) == -1 )
857             {
858                 free( psz_parser );
859                 return;
860             }
861             free( psz_parser );
862         }
863         else
864             return;
865     }
866     else
867     {
868         if( psz_parser )
869         {
870             memmove( psz_parser, psz_parser + strlen(psz_name) +
871                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
872                             strlen(psz_parser + strlen(psz_name)) + 1 );
873
874             /* Remove trailing : : */
875             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
876             {
877                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
878             }
879          }
880          else
881          {
882              free( psz_string );
883              return;
884          }
885     }
886
887     if( b_setconfig )
888     {
889         if( !strcmp( psz_filter_type, "sub-filter") )
890             config_PutPsz( vout_GetSpu( p_vout ), psz_filter_type, psz_string );
891         else
892             config_PutPsz( p_vout, psz_filter_type, psz_string );
893     }
894
895     if( !strcmp( psz_filter_type, "sub-filter") )
896         var_SetString( vout_GetSpu( p_vout ), psz_filter_type, psz_string );
897     else
898         var_SetString( p_vout, psz_filter_type, psz_string );
899
900     free( psz_string );
901 }
902
903 /*****************************************************************************
904  * InitWindowSize: find the initial dimensions the video window should have.
905  *****************************************************************************
906  * This function will check the "width", "height" and "zoom" config options and
907  * will calculate the size that the video window should have.
908  *****************************************************************************/
909 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
910                             unsigned *pi_height )
911 {
912 #define FP_FACTOR 1000                             /* our fixed point factor */
913
914     int i_width = var_GetInteger( p_vout, "width" );
915     int i_height = var_GetInteger( p_vout, "height" );
916     float f_zoom = var_GetFloat( p_vout, "zoom" );
917     uint64_t ll_zoom = (uint64_t)( FP_FACTOR * f_zoom );
918
919     if( i_width > 0 && i_height > 0)
920     {
921         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
922         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
923     }
924     else if( i_width > 0 )
925     {
926         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
927         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
928             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
929             FP_FACTOR / p_vout->fmt_in.i_visible_width );
930     }
931     else if( i_height > 0 )
932     {
933         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
934         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
935             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
936             FP_FACTOR / p_vout->fmt_in.i_visible_height );
937     }
938     else if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 )
939     {
940         msg_Warn( p_vout, "aspect ratio screwed up" );
941         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
942         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
943     }
944     else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
945     {
946         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
947             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
948         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
949             / FP_FACTOR );
950     }
951     else
952     {
953         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
954             / FP_FACTOR );
955         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
956             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
957     }
958
959     msg_Dbg( p_vout, "window size: %ux%u", *pi_width, *pi_height );
960
961 #undef FP_FACTOR
962 }
963
964 /*****************************************************************************
965  * Object variables callbacks
966  *****************************************************************************/
967 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
968                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
969 {
970     vout_thread_t *p_vout = (vout_thread_t *)p_this;
971     (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
972     InitWindowSize( p_vout, &p_vout->i_window_width,
973                     &p_vout->i_window_height );
974     vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
975                   p_vout->i_window_height );
976     return VLC_SUCCESS;
977 }
978
979 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
980                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
981 {
982     vout_thread_t *p_vout = (vout_thread_t *)p_this;
983     int64_t i_aspect_num, i_aspect_den;
984     unsigned int i_width, i_height;
985
986     (void)oldval; (void)p_data;
987
988     /* Restore defaults */
989     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
990     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
991     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
992     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
993
994     if( !strcmp( psz_cmd, "crop" ) )
995     {
996         char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
997         if( psz_parser )
998         {
999             /* We're using the 3:4 syntax */
1000             i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1001             if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
1002
1003             i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1004             if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
1005
1006             i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
1007                 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
1008             i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
1009                 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
1010
1011             if( i_width < p_vout->fmt_render.i_visible_width )
1012             {
1013                 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
1014                     (p_vout->fmt_render.i_visible_width - i_width) / 2;
1015                 p_vout->fmt_in.i_visible_width = i_width;
1016             }
1017             else
1018             {
1019                 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
1020                     (p_vout->fmt_render.i_visible_height - i_height) / 2;
1021                 p_vout->fmt_in.i_visible_height = i_height;
1022             }
1023         }
1024         else
1025         {
1026             psz_parser = strchr( newval.psz_string, 'x' );
1027             if( psz_parser )
1028             {
1029                 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
1030                 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
1031
1032                 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
1033                 if( psz_end != psz_parser ) goto crop_end;
1034
1035                 psz_parser = strchr( ++psz_end, '+' );
1036                 i_crop_height = strtol( psz_end, &psz_end, 10 );
1037                 if( psz_end != psz_parser ) goto crop_end;
1038
1039                 psz_parser = strchr( ++psz_end, '+' );
1040                 i_crop_left = strtol( psz_end, &psz_end, 10 );
1041                 if( psz_end != psz_parser ) goto crop_end;
1042
1043                 psz_end++;
1044                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1045                 if( *psz_end != '\0' ) goto crop_end;
1046
1047                 if( i_crop_top + i_crop_height >= p_vout->fmt_render.i_visible_height ||
1048                     i_crop_left + i_crop_width >= p_vout->fmt_render.i_visible_width )
1049                 {
1050                     msg_Err( p_vout, "Unable to crop over picture boundaries");
1051                     return VLC_EGENERIC;
1052                 }
1053
1054                 i_width = i_crop_width;
1055                 p_vout->fmt_in.i_visible_width = i_width;
1056
1057                 i_height = i_crop_height;
1058                 p_vout->fmt_in.i_visible_height = i_height;
1059
1060                 p_vout->fmt_in.i_x_offset = i_crop_left;
1061                 p_vout->fmt_in.i_y_offset = i_crop_top;
1062             }
1063             else
1064             {
1065                 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1066                 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1067
1068                 psz_parser = strchr( newval.psz_string, '+' );
1069                 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1070                 if( psz_end != psz_parser ) goto crop_end;
1071
1072                 psz_parser = strchr( ++psz_end, '+' );
1073                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1074                 if( psz_end != psz_parser ) goto crop_end;
1075
1076                 psz_parser = strchr( ++psz_end, '+' );
1077                 i_crop_right = strtol( psz_end, &psz_end, 10 );
1078                 if( psz_end != psz_parser ) goto crop_end;
1079
1080                 psz_end++;
1081                 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1082                 if( *psz_end != '\0' ) goto crop_end;
1083
1084                 if( i_crop_top + i_crop_bottom >= p_vout->fmt_render.i_visible_height ||
1085                     i_crop_right + i_crop_left >= p_vout->fmt_render.i_visible_width )
1086                 {
1087                     msg_Err( p_vout, "Unable to crop over picture boundaries" );
1088                     return VLC_EGENERIC;
1089                 }
1090
1091                 i_width = p_vout->fmt_render.i_visible_width
1092                           - i_crop_left - i_crop_right;
1093                 p_vout->fmt_in.i_visible_width = i_width;
1094
1095                 i_height = p_vout->fmt_render.i_visible_height
1096                            - i_crop_top - i_crop_bottom;
1097                 p_vout->fmt_in.i_visible_height = i_height;
1098
1099                 p_vout->fmt_in.i_x_offset = i_crop_left;
1100                 p_vout->fmt_in.i_y_offset = i_crop_top;
1101             }
1102         }
1103     }
1104     else if( !strcmp( psz_cmd, "crop-top" )
1105           || !strcmp( psz_cmd, "crop-left" )
1106           || !strcmp( psz_cmd, "crop-bottom" )
1107           || !strcmp( psz_cmd, "crop-right" ) )
1108     {
1109         unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1110
1111         i_crop_top = var_GetInteger( p_vout, "crop-top" );
1112         i_crop_left = var_GetInteger( p_vout, "crop-left" );
1113         i_crop_right = var_GetInteger( p_vout, "crop-right" );
1114         i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1115
1116         if( i_crop_top + i_crop_bottom >= p_vout->fmt_render.i_visible_height ||
1117             i_crop_right + i_crop_left >= p_vout->fmt_render.i_visible_width )
1118         {
1119             msg_Err( p_vout, "Unable to crop over picture boundaries" );
1120             return VLC_EGENERIC;
1121         }
1122
1123         i_width = p_vout->fmt_render.i_visible_width
1124                   - i_crop_left - i_crop_right;
1125         p_vout->fmt_in.i_visible_width = i_width;
1126
1127         i_height = p_vout->fmt_render.i_visible_height
1128                    - i_crop_top - i_crop_bottom;
1129         p_vout->fmt_in.i_visible_height = i_height;
1130
1131         p_vout->fmt_in.i_x_offset = i_crop_left;
1132         p_vout->fmt_in.i_y_offset = i_crop_top;
1133     }
1134
1135  crop_end:
1136     InitWindowSize( p_vout, &p_vout->i_window_width,
1137                     &p_vout->i_window_height );
1138
1139     p_vout->i_changes |= VOUT_CROP_CHANGE;
1140
1141     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1142              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1143              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1144              p_vout->fmt_in.i_visible_width,
1145              p_vout->fmt_in.i_visible_height );
1146
1147     var_SetVoid( p_vout, "crop-update" );
1148
1149     return VLC_SUCCESS;
1150 }
1151
1152 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1153                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1154 {
1155     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1156     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1157     vlc_value_t val;
1158
1159     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1160     (void)psz_cmd; (void)oldval; (void)p_data;
1161
1162     /* Restore defaults */
1163     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1164     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1165     p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1166     p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1167
1168     if( !psz_parser ) goto aspect_end;
1169
1170     i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1171     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1172
1173     i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1174     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1175
1176     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1177     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1178     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1179     p_vout->fmt_in.i_sar_num = i_sar_num;
1180     p_vout->fmt_in.i_sar_den = i_sar_den;
1181     p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1182     p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1183
1184  aspect_end:
1185     if( p_vout->p->i_par_num && p_vout->p->i_par_den )
1186     {
1187         p_vout->fmt_in.i_sar_num *= p_vout->p->i_par_den;
1188         p_vout->fmt_in.i_sar_den *= p_vout->p->i_par_num;
1189         p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1190             p_vout->p->i_par_den / p_vout->p->i_par_num;
1191         p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1192     }
1193
1194     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1195
1196     vlc_ureduce( &i_aspect_num, &i_aspect_den,
1197                  p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1198     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1199              i_aspect_num, i_aspect_den,
1200              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1201
1202     if( var_Get( p_vout, "crop", &val ) )
1203         return VLC_EGENERIC;
1204
1205     int i_ret = CropCallback( p_this, "crop", val, val, 0 );
1206     free( val.psz_string );
1207     return i_ret;
1208 }
1209
1210 static int ScalingCallback( vlc_object_t *p_this, char const *psz_cmd,
1211                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1212 {
1213     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1214     (void)oldval; (void)newval; (void)p_data;
1215
1216     vlc_mutex_lock( &p_vout->change_lock );
1217
1218     if( !strcmp( psz_cmd, "autoscale" ) )
1219     {
1220         p_vout->i_changes |= VOUT_SCALE_CHANGE;
1221     }
1222     else if( !strcmp( psz_cmd, "scale" ) )
1223     {
1224         p_vout->i_changes |= VOUT_ZOOM_CHANGE;
1225     }
1226
1227     vlc_mutex_unlock( &p_vout->change_lock );
1228
1229     return VLC_SUCCESS;
1230 }
1231
1232 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1233                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1234 {
1235     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1236
1237     vlc_mutex_lock( &p_vout->change_lock );
1238     p_vout->i_changes |= VOUT_ON_TOP_CHANGE;
1239     p_vout->b_on_top = newval.b_bool;
1240     vlc_mutex_unlock( &p_vout->change_lock );
1241
1242     /* Modify libvlc as well because the vout might have to be restarted */
1243     var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
1244     var_Set( p_vout->p_libvlc, "video-on-top", newval );
1245
1246     (void)psz_cmd; (void)oldval; (void)p_data;
1247     return VLC_SUCCESS;
1248 }
1249
1250 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1251                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1252 {
1253     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1254     vlc_value_t val;
1255     (void)psz_cmd; (void)oldval; (void)p_data;
1256
1257     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1258
1259     /* Modify libvlc as well because the vout might have to be restarted */
1260     var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
1261     var_Set( p_vout->p_libvlc, "fullscreen", newval );
1262
1263     val.b_bool = true;
1264     var_Set( p_vout, "intf-change", val );
1265     return VLC_SUCCESS;
1266 }
1267
1268 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1269                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1270 {
1271     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1272     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1273     VLC_UNUSED(newval); VLC_UNUSED(p_data);
1274
1275     VoutSaveSnapshot( p_vout );
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 }