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