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