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