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