]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
vout_Snapshot(): hability to specify width & height when the snapshot isn't taken...
[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     fmt_out.i_width = var_GetInteger( p_vout, "snapshot-width" );
709     fmt_out.i_height = var_GetInteger( p_vout, "snapshot-height" );
710
711     if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
712     {
713         float f = (float)p_vout->fmt_in.i_height / fmt_out.i_height;
714         fmt_out.i_width = p_vout->fmt_in.i_width / f;
715     }
716     else if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
717     {
718         float f = (float)p_vout->fmt_in.i_width / fmt_out.i_width;
719         fmt_out.i_height = p_vout->fmt_in.i_height / f;
720     }
721
722     /* Save the snapshot */
723     fmt_in = p_vout->fmt_in;
724     fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
725     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
726     if( i_ret != VLC_SUCCESS )
727     {
728         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
729         free( psz_filename );
730         image_HandlerDelete( p_image );
731         return VLC_EGENERIC;
732     }
733
734     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
735     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
736                      "%s", psz_filename );
737     free( psz_filename );
738
739     if( var_GetBool( p_vout, "snapshot-preview" ) )
740     {
741         /* Inject a subpicture with the snapshot */
742         memset( &fmt_out, 0, sizeof(fmt_out) );
743         fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
744         p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
745         image_HandlerDelete( p_image );
746         if( !p_pif ) return VLC_EGENERIC;
747
748         p_subpic = spu_CreateSubpicture( p_vout->p_spu );
749         if( p_subpic == NULL )
750         {
751              p_pif->pf_release( p_pif );
752              return VLC_EGENERIC;
753         }
754
755         p_subpic->i_channel = 0;
756         p_subpic->i_start = mdate();
757         p_subpic->i_stop = mdate() + 4000000;
758         p_subpic->b_ephemer = VLC_TRUE;
759         p_subpic->b_fade = VLC_TRUE;
760         p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
761         p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
762
763         p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
764         vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture,
765                           p_pif );
766         p_pif->pf_release( p_pif );
767
768         spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
769     }
770     else
771     {
772         image_HandlerDelete( p_image );
773     }
774
775     return VLC_SUCCESS;
776 }
777
778 /*****************************************************************************
779  * Handle filters
780  *****************************************************************************/
781
782 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
783                         vlc_bool_t b_add, vlc_bool_t b_setconfig )
784 {
785     char *psz_parser;
786     char *psz_string = config_GetPsz( p_vout, "vout-filter" );
787
788     /* Todo : Use some generic chain manipulation functions */
789     if( !psz_string ) psz_string = strdup("");
790
791     psz_parser = strstr( psz_string, psz_name );
792     if( b_add )
793     {
794         if( !psz_parser )
795         {
796             psz_parser = psz_string;
797             asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
798                             psz_string, psz_name );
799             free( psz_parser );
800         }
801         else
802             return;
803     }
804     else
805     {
806         if( psz_parser )
807         {
808             memmove( psz_parser, psz_parser + strlen(psz_name) +
809                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
810                             strlen(psz_parser + strlen(psz_name)) + 1 );
811
812             /* Remove trailing : : */
813             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
814             {
815                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
816             }
817          }
818          else
819          {
820              free( psz_string );
821              return;
822          }
823     }
824     if( b_setconfig )
825         config_PutPsz( p_vout, "vout-filter", psz_string );
826
827     var_SetString( p_vout, "vout-filter", psz_string );
828     free( psz_string );
829 }
830
831 /*****************************************************************************
832  * vout_ControlDefault: default methods for video output control.
833  *****************************************************************************/
834 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
835 {
836     (void)args;
837     switch( i_query )
838     {
839     case VOUT_REPARENT:
840     case VOUT_CLOSE:
841         if( p_vout->p_parent_intf )
842         {
843             vlc_object_release( p_vout->p_parent_intf );
844             p_vout->p_parent_intf = NULL;
845         }
846         return VLC_SUCCESS;
847         break;
848
849     case VOUT_SNAPSHOT:
850         p_vout->b_snapshot = VLC_TRUE;
851         return VLC_SUCCESS;
852         break;
853
854     default:
855         msg_Dbg( p_vout, "control query not supported" );
856         return VLC_EGENERIC;
857     }
858 }
859
860 /*****************************************************************************
861  * InitWindowSize: find the initial dimensions the video window should have.
862  *****************************************************************************
863  * This function will check the "width", "height" and "zoom" config options and
864  * will calculate the size that the video window should have.
865  *****************************************************************************/
866 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
867                             unsigned *pi_height )
868 {
869     vlc_value_t val;
870     int i_width, i_height;
871     uint64_t ll_zoom;
872
873 #define FP_FACTOR 1000                             /* our fixed point factor */
874
875     var_Get( p_vout, "width", &val );
876     i_width = val.i_int;
877     var_Get( p_vout, "height", &val );
878     i_height = val.i_int;
879     var_Get( p_vout, "zoom", &val );
880     ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
881
882     if( i_width > 0 && i_height > 0)
883     {
884         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
885         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
886         goto initwsize_end;
887     }
888     else if( i_width > 0 )
889     {
890         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
891         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
892             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
893             FP_FACTOR / p_vout->fmt_in.i_visible_width );
894         goto initwsize_end;
895     }
896     else if( i_height > 0 )
897     {
898         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
899         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
900             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
901             FP_FACTOR / p_vout->fmt_in.i_visible_height );
902         goto initwsize_end;
903     }
904
905     if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
906         msg_Warn( p_vout, "fucked up aspect" );
907         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
908         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
909     }
910     else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
911     {
912         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
913             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
914         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
915             / FP_FACTOR );
916     }
917     else
918     {
919         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
920             / FP_FACTOR );
921         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
922             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
923     }
924
925 initwsize_end:
926     msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width,
927              p_vout->i_window_height );
928
929 #undef FP_FACTOR
930 }
931
932 /*****************************************************************************
933  * Object variables callbacks
934  *****************************************************************************/
935 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
936                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
937 {
938     vout_thread_t *p_vout = (vout_thread_t *)p_this;
939     (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
940     InitWindowSize( p_vout, &p_vout->i_window_width,
941                     &p_vout->i_window_height );
942     vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
943                   p_vout->i_window_height );
944     return VLC_SUCCESS;
945 }
946
947 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
948                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
949 {
950     vout_thread_t *p_vout = (vout_thread_t *)p_this;
951     int64_t i_aspect_num, i_aspect_den;
952     unsigned int i_width, i_height;
953
954     (void)oldval; (void)p_data;
955
956     /* Restore defaults */
957     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
958     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
959     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
960     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
961
962     if( !strcmp( psz_cmd, "crop" ) )
963     {
964         char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
965         if( psz_parser )
966         {
967             /* We're using the 3:4 syntax */
968             i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
969             if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
970
971             i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
972             if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
973
974             i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
975                 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
976             i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
977                 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
978
979             if( i_width < p_vout->fmt_render.i_visible_width )
980             {
981                 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
982                     (p_vout->fmt_render.i_visible_width - i_width) / 2;
983                 p_vout->fmt_in.i_visible_width = i_width;
984             }
985             else
986             {
987                 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
988                     (p_vout->fmt_render.i_visible_height - i_height) / 2;
989                 p_vout->fmt_in.i_visible_height = i_height;
990             }
991         }
992         else
993         {
994             psz_parser = strchr( newval.psz_string, 'x' );
995             if( psz_parser )
996             {
997                 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
998                 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
999
1000                 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
1001                 if( psz_end != psz_parser ) goto crop_end;
1002
1003                 psz_parser = strchr( ++psz_end, '+' );
1004                 i_crop_height = strtol( psz_end, &psz_end, 10 );
1005                 if( psz_end != psz_parser ) goto crop_end;
1006
1007                 psz_parser = strchr( ++psz_end, '+' );
1008                 i_crop_left = strtol( psz_end, &psz_end, 10 );
1009                 if( psz_end != psz_parser ) goto crop_end;
1010
1011                 psz_end++;
1012                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1013                 if( *psz_end != '\0' ) goto crop_end;
1014
1015                 i_width = i_crop_width;
1016                 p_vout->fmt_in.i_visible_width = i_width;
1017
1018                 i_height = i_crop_height;
1019                 p_vout->fmt_in.i_visible_height = i_height;
1020
1021                 p_vout->fmt_in.i_x_offset = i_crop_left;
1022                 p_vout->fmt_in.i_y_offset = i_crop_top;
1023             }
1024             else
1025             {
1026                 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1027                 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1028
1029                 psz_parser = strchr( newval.psz_string, '+' );
1030                 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1031                 if( psz_end != psz_parser ) goto crop_end;
1032
1033                 psz_parser = strchr( ++psz_end, '+' );
1034                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1035                 if( psz_end != psz_parser ) goto crop_end;
1036
1037                 psz_parser = strchr( ++psz_end, '+' );
1038                 i_crop_right = strtol( psz_end, &psz_end, 10 );
1039                 if( psz_end != psz_parser ) goto crop_end;
1040
1041                 psz_end++;
1042                 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1043                 if( *psz_end != '\0' ) goto crop_end;
1044
1045                 i_width = p_vout->fmt_render.i_visible_width
1046                           - i_crop_left - i_crop_right;
1047                 p_vout->fmt_in.i_visible_width = i_width;
1048
1049                 i_height = p_vout->fmt_render.i_visible_height
1050                            - i_crop_top - i_crop_bottom;
1051                 p_vout->fmt_in.i_visible_height = i_height;
1052
1053                 p_vout->fmt_in.i_x_offset = i_crop_left;
1054                 p_vout->fmt_in.i_y_offset = i_crop_top;
1055             }
1056         }
1057     }
1058     else if( !strcmp( psz_cmd, "crop-top" )
1059           || !strcmp( psz_cmd, "crop-left" )
1060           || !strcmp( psz_cmd, "crop-bottom" )
1061           || !strcmp( psz_cmd, "crop-right" ) )
1062     {
1063         unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1064
1065         i_crop_top = var_GetInteger( p_vout, "crop-top" );
1066         i_crop_left = var_GetInteger( p_vout, "crop-left" );
1067         i_crop_right = var_GetInteger( p_vout, "crop-right" );
1068         i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1069
1070         i_width = p_vout->fmt_render.i_visible_width
1071                   - i_crop_left - i_crop_right;
1072         p_vout->fmt_in.i_visible_width = i_width;
1073
1074         i_height = p_vout->fmt_render.i_visible_height
1075                    - i_crop_top - i_crop_bottom;
1076         p_vout->fmt_in.i_visible_height = i_height;
1077
1078         p_vout->fmt_in.i_x_offset = i_crop_left;
1079         p_vout->fmt_in.i_y_offset = i_crop_top;
1080     }
1081
1082  crop_end:
1083     InitWindowSize( p_vout, &p_vout->i_window_width,
1084                     &p_vout->i_window_height );
1085
1086     p_vout->i_changes |= VOUT_CROP_CHANGE;
1087
1088     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1089              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1090              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1091              p_vout->fmt_in.i_visible_width,
1092              p_vout->fmt_in.i_visible_height );
1093
1094     return VLC_SUCCESS;
1095 }
1096
1097 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1098                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1099 {
1100     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1101     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1102     vlc_value_t val;
1103
1104     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1105     (void)psz_cmd; (void)oldval; (void)p_data;
1106
1107     /* Restore defaults */
1108     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1109     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1110     p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1111     p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1112
1113     if( !psz_parser ) goto aspect_end;
1114
1115     i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1116     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1117
1118     i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1119     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1120
1121     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1122     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1123     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1124     p_vout->fmt_in.i_sar_num = i_sar_num;
1125     p_vout->fmt_in.i_sar_den = i_sar_den;
1126     p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1127     p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1128
1129  aspect_end:
1130     if( p_vout->i_par_num && p_vout->i_par_den )
1131     {
1132         p_vout->fmt_in.i_sar_num *= p_vout->i_par_den;
1133         p_vout->fmt_in.i_sar_den *= p_vout->i_par_num;
1134         p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1135             p_vout->i_par_den / p_vout->i_par_num;
1136         p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1137     }
1138
1139     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1140
1141     vlc_ureduce( &i_aspect_num, &i_aspect_den,
1142                  p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1143     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1144              i_aspect_num, i_aspect_den,
1145              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1146
1147     var_Get( p_vout, "crop", &val );
1148     return CropCallback( p_this, "crop", val, val, 0 );
1149
1150     return VLC_SUCCESS;
1151 }
1152
1153 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1154                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1155 {
1156     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1157     playlist_t *p_playlist = pl_Yield( p_this );
1158     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
1159     (void)psz_cmd; (void)oldval; (void)p_data;
1160
1161     /* Modify playlist as well because the vout might have to be restarted */
1162     var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
1163     var_Set( p_playlist, "video-on-top", newval );
1164
1165     pl_Release( p_this );
1166     return VLC_SUCCESS;
1167 }
1168
1169 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1170                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1171 {
1172     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1173     vlc_value_t val;
1174     playlist_t *p_playlist = pl_Yield( p_this );
1175     (void)psz_cmd; (void)oldval; (void)p_data;
1176
1177     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1178
1179     /* Modify playlist as well because the vout might have to be restarted */
1180     var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
1181     var_Set( p_playlist, "fullscreen", newval );
1182     pl_Release( p_playlist );
1183
1184     val.b_bool = VLC_TRUE;
1185     var_Set( p_vout, "intf-change", val );
1186     return VLC_SUCCESS;
1187 }
1188
1189 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1190                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1191 {
1192     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1193     vout_Control( p_vout, VOUT_SNAPSHOT );
1194     (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
1195     return VLC_SUCCESS;
1196 }
1197
1198 static int TitleCallback( vlc_object_t *p_this, char const *psz_cmd,
1199                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1200 {
1201     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1202
1203     if( !strncmp( psz_cmd, "video-title-show", 16 ) )
1204         p_vout->b_title_show = newval.b_bool;
1205     else if( !strncmp( psz_cmd, "video-title-timeout", 19 ) )
1206         p_vout->i_title_timeout = (mtime_t) newval.i_int;
1207     else if( !strncmp( psz_cmd, "video-title-position", 20 ) )
1208         p_vout->i_title_position = newval.i_int;
1209     return VLC_SUCCESS;
1210 }