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