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