1 /*****************************************************************************
2 * vout_intf.c : video output interface
3 *****************************************************************************
4 * Copyright (C) 2000-2007 the VideoLAN team
6 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
34 #include <stdlib.h> /* free() */
35 #include <sys/types.h> /* opendir() */
36 #include <dirent.h> /* opendir() */
39 #include <vlc_interface.h>
40 #include <vlc_block.h>
41 #include <vlc_playlist.h>
44 #include <vlc_window.h>
45 #include <vlc_image.h>
47 #include <vlc_charset.h>
49 #include <vlc_strings.h>
50 #include <vlc_charset.h>
51 #include "../libvlc.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
58 /* Object variables callbacks */
59 static int ZoomCallback( vlc_object_t *, char const *,
60 vlc_value_t, vlc_value_t, void * );
61 static int CropCallback( vlc_object_t *, char const *,
62 vlc_value_t, vlc_value_t, void * );
63 static int AspectCallback( vlc_object_t *, char const *,
64 vlc_value_t, vlc_value_t, void * );
65 static int OnTopCallback( vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void * );
67 static int FullscreenCallback( vlc_object_t *, char const *,
68 vlc_value_t, vlc_value_t, void * );
69 static int SnapshotCallback( vlc_object_t *, char const *,
70 vlc_value_t, vlc_value_t, void * );
72 static int TitleShowCallback( vlc_object_t *, char const *,
73 vlc_value_t, vlc_value_t, void * );
74 static int TitleTimeoutCallback( vlc_object_t *, char const *,
75 vlc_value_t, vlc_value_t, void * );
76 static int TitlePositionCallback( vlc_object_t *, char const *,
77 vlc_value_t, vlc_value_t, void * );
79 /*****************************************************************************
80 * vout_RequestWindow: Create/Get a video window if possible.
81 *****************************************************************************
82 * This function looks for the main interface and tries to request
83 * a new video window. If it fails then the vout will still need to create the
85 *****************************************************************************/
86 void *vout_RequestWindow( vout_thread_t *p_vout,
87 int *pi_x_hint, int *pi_y_hint,
88 unsigned int *pi_width_hint,
89 unsigned int *pi_height_hint )
92 if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
94 /* Get requested coordinates */
95 *pi_x_hint = var_GetInteger( p_vout, "video-x" );
96 *pi_y_hint = var_GetInteger( p_vout, "video-y" );
98 *pi_width_hint = p_vout->i_window_width;
99 *pi_height_hint = p_vout->i_window_height;
101 /* Check whether someone provided us with a window ID */
102 int drawable = var_CreateGetInteger( p_vout, "drawable" );
103 if( drawable ) return (void *)(intptr_t)drawable;
105 vout_window_t *wnd = vlc_custom_create (VLC_OBJECT(p_vout), sizeof (*wnd),
106 VLC_OBJECT_GENERIC, "window");
111 wnd->width = *pi_width_hint;
112 wnd->height = *pi_height_hint;
113 wnd->pos_x = *pi_x_hint;
114 wnd->pos_y = *pi_y_hint;
115 vlc_object_attach (wnd, p_vout);
117 wnd->module = module_Need (wnd, "vout window", 0, 0);
118 if (wnd->module == NULL)
120 msg_Dbg (wnd, "no window provider available");
121 vlc_object_release (wnd);
124 p_vout->p_window = wnd;
125 *pi_width_hint = wnd->width;
126 *pi_height_hint = wnd->height;
127 *pi_x_hint = wnd->pos_x;
128 *pi_y_hint = wnd->pos_y;
132 void vout_ReleaseWindow( vout_thread_t *p_vout, void *dummy )
134 vout_window_t *wnd = p_vout->p_window;
138 p_vout->p_window = NULL;
140 assert (wnd->module);
141 module_Unneed (wnd, wnd->module);
143 vlc_object_release (wnd);
147 int vout_ControlWindow( vout_thread_t *p_vout, void *dummy,
148 int i_query, va_list args )
150 vout_window_t *wnd = p_vout->p_window;
155 assert (wnd->control);
156 return wnd->control (wnd, i_query, args);
159 /*****************************************************************************
160 * vout_IntfInit: called during the vout creation to initialise misc things.
161 *****************************************************************************/
165 const char *psz_label;
166 } p_zoom_values[] = {
167 { 0.25, N_("1:4 Quarter") },
168 { 0.5, N_("1:2 Half") },
169 { 1, N_("1:1 Original") },
170 { 2, N_("2:1 Double") },
175 const char *psz_value;
176 const char *psz_label;
177 } p_crop_values[] = {
178 { "", N_("Default") },
179 { "16:10", "16:10" },
181 { "185:100", "1.85:1" },
182 { "221:100", "2.21:1" },
183 { "235:100", "2.35:1" },
184 { "239:100", "2.39:1" },
193 const char *psz_value;
194 const char *psz_label;
195 } p_aspect_ratio_values[] = {
196 { "", N_("Default") },
200 { "16:10", "16:10" },
201 { "221:100", "2.21:1" },
205 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
208 if( psz_list && *psz_list )
210 char *psz_cur = psz_list;
212 while( psz_cur && *psz_cur )
214 vlc_value_t val, text;
215 psz_next = strchr( psz_cur, ',' );
221 val.psz_string = psz_cur;
222 text.psz_string = psz_cur;
223 var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
229 void vout_IntfInit( vout_thread_t *p_vout )
231 vlc_value_t val, text, old_val;
232 bool b_force_par = false;
236 /* Create a few object variables we'll need later on */
237 var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
238 var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
239 var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
240 var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
241 var_Create( p_vout, "snapshot-sequential",
242 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
243 var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
244 var_SetInteger( p_vout, "snapshot-num", 1 );
245 var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
246 var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
248 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
249 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
250 p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
252 var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
253 var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
255 var_Create( p_vout, "mouse-hide-timeout",
256 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
258 p_vout->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
259 p_vout->i_title_timeout =
260 (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
261 p_vout->i_title_position =
262 var_CreateGetInteger( p_vout, "video-title-position" );
264 var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
265 var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
266 var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
268 /* Zoom object var */
269 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
270 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
272 text.psz_string = _("Zoom");
273 var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
275 var_Get( p_vout, "zoom", &old_val );
277 for( i = 0; p_zoom_values[i].f_value; i++ )
279 if( old_val.f_float == p_zoom_values[i].f_value )
280 var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
281 val.f_float = p_zoom_values[i].f_value;
282 text.psz_string = _( p_zoom_values[i].psz_label );
283 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
286 var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
288 var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
290 /* Crop offset vars */
291 var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
292 var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
293 var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
294 var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
296 var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
297 var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
298 var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
299 var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
301 /* Crop object var */
302 var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
303 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
305 text.psz_string = _("Crop");
306 var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
308 val.psz_string = (char*)"";
309 var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
311 for( i = 0; p_crop_values[i].psz_value; i++ )
313 val.psz_string = (char*)p_crop_values[i].psz_value;
314 text.psz_string = _( p_crop_values[i].psz_label );
315 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
318 /* update triggered every time the vout's crop parameters are changed */
319 var_Create( p_vout, "crop-update", VLC_VAR_VOID );
321 /* Add custom crop ratios */
322 psz_buf = config_GetPsz( p_vout, "custom-crop-ratios" );
323 AddCustomRatios( p_vout, "crop", psz_buf );
326 var_AddCallback( p_vout, "crop", CropCallback, NULL );
327 var_Get( p_vout, "crop", &old_val );
328 if( old_val.psz_string && *old_val.psz_string )
329 var_Change( p_vout, "crop", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
330 free( old_val.psz_string );
332 /* Monitor pixel aspect-ratio */
333 var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
334 var_Get( p_vout, "monitor-par", &val );
335 if( val.psz_string && *val.psz_string )
337 char *psz_parser = strchr( val.psz_string, ':' );
338 unsigned int i_aspect_num = 0, i_aspect_den = 0;
342 i_aspect_num = strtol( val.psz_string, 0, 10 );
343 i_aspect_den = strtol( ++psz_parser, 0, 10 );
347 i_aspect = atof( val.psz_string );
348 vlc_ureduce( &i_aspect_num, &i_aspect_den,
349 i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
351 if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
353 p_vout->i_par_num = i_aspect_num;
354 p_vout->i_par_den = i_aspect_den;
356 vlc_ureduce( &p_vout->i_par_num, &p_vout->i_par_den,
357 p_vout->i_par_num, p_vout->i_par_den, 0 );
359 msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
360 p_vout->i_par_num, p_vout->i_par_den );
363 free( val.psz_string );
365 /* Aspect-ratio object var */
366 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
367 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
369 text.psz_string = _("Aspect-ratio");
370 var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
372 val.psz_string = (char*)"";
373 var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
375 for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
377 val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
378 text.psz_string = _( p_aspect_ratio_values[i].psz_label );
379 var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
382 /* Add custom aspect ratios */
383 psz_buf = config_GetPsz( p_vout, "custom-aspect-ratios" );
384 AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
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 free( old_val.psz_string );
393 /* Initialize the dimensions of the video window */
394 InitWindowSize( p_vout, &p_vout->i_window_width,
395 &p_vout->i_window_height );
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 | VLC_VAR_ISCOMMAND );
400 text.psz_string = _("Always on top");
401 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
402 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
404 /* Add a variable to indicate whether we want window decoration or not */
405 var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
407 /* Add a fullscreen variable */
408 if( var_CreateGetBoolCommand( p_vout, "fullscreen" ) )
410 /* user requested fullscreen */
411 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
413 text.psz_string = _("Fullscreen");
414 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
415 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
417 /* Add a snapshot variable */
418 var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
419 text.psz_string = _("Snapshot");
420 var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
421 var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
423 /* Mouse coordinates */
424 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
425 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
426 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
427 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
428 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
430 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
431 var_SetBool( p_vout, "intf-change", true );
434 /*****************************************************************************
435 * vout_Snapshot: generates a snapshot.
436 *****************************************************************************/
437 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
439 image_handler_t *p_image = image_HandlerCreate( p_vout );
440 video_format_t fmt_in, fmt_out;
441 char *psz_filename = NULL;
442 subpicture_t *p_subpic;
444 vlc_value_t val, format;
448 memset( &fmt_in, 0, sizeof(video_format_t) );
449 memset( &fmt_out, 0, sizeof(video_format_t) );
451 var_Get( p_vout, "snapshot-path", &val );
452 if( val.psz_string && !*val.psz_string )
454 free( val.psz_string );
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
463 if( val.psz_string && !strncmp( val.psz_string, "object:", 7 ) )
466 vlc_object_t* p_dest;
468 snapshot_t *p_snapshot;
471 /* Destination object-id is following object: */
472 i_id = atoi( &val.psz_string[7] );
473 p_dest = ( vlc_object_t* )vlc_object_get( i_id );
476 msg_Err( p_vout, "Cannot find calling object" );
477 image_HandlerDelete( p_image );
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;
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 fmt_out.i_chroma = VLC_FOURCC( 'p','n','g',' ' );
489 fmt_out.i_width = var_GetInteger( p_vout, "snapshot-width" );
490 fmt_out.i_height = var_GetInteger( p_vout, "snapshot-height" );
491 /* If snapshot-width and/or snapshot height were not specified,
492 use a default snapshot width of 320 */
493 if( fmt_out.i_width == 0 && fmt_out.i_height == 0 )
495 fmt_out.i_width = 320;
498 if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
500 fmt_out.i_width = (fmt_in.i_width * fmt_out.i_height) / fmt_in.i_height;
502 else if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
504 fmt_out.i_height = (fmt_in.i_height * fmt_out.i_width) / fmt_in.i_width;
508 fmt_out.i_width = fmt_in.i_width;
509 fmt_out.i_height = fmt_in.i_height;
512 p_block = ( block_t* ) image_Write( p_image, p_pic, &fmt_in, &fmt_out );
515 msg_Err( p_vout, "Could not get snapshot" );
516 image_HandlerDelete( p_image );
517 vlc_object_signal_maybe( VLC_OBJECT(p_dest) );
518 vlc_object_release( p_dest );
522 /* Copy the p_block data to a snapshot structure */
523 /* FIXME: get the timestamp */
524 p_snapshot = ( snapshot_t* ) malloc( sizeof( snapshot_t ) );
527 block_Release( p_block );
528 image_HandlerDelete( p_image );
529 vlc_object_signal_maybe( VLC_OBJECT(p_dest) );
530 vlc_object_release( p_dest );
534 i_size = p_block->i_buffer;
536 p_snapshot->i_width = fmt_out.i_width;
537 p_snapshot->i_height = fmt_out.i_height;
538 p_snapshot->i_datasize = i_size;
539 p_snapshot->date = p_block->i_pts; /* FIXME ?? */
540 p_snapshot->p_data = ( char* ) malloc( i_size );
541 if( !p_snapshot->p_data )
543 block_Release( p_block );
545 image_HandlerDelete( p_image );
546 vlc_object_signal_maybe( VLC_OBJECT(p_dest) );
547 vlc_object_release( p_dest );
550 memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
552 p_dest->p_private = p_snapshot;
554 block_Release( p_block );
556 /* Unlock the object */
557 vlc_object_signal_maybe( VLC_OBJECT(p_dest) );
558 vlc_object_release( p_dest );
560 image_HandlerDelete( p_image );
564 #if defined(__APPLE__) || defined(SYS_BEOS)
565 if( !val.psz_string )
567 if( asprintf( &val.psz_string, "%s/Desktop",
568 config_GetHomeDir() ) == -1 )
569 val.psz_string = NULL;
572 #elif defined(WIN32) && !defined(UNDER_CE)
573 if( !val.psz_string )
575 /* Get the My Pictures folder path */
577 char *p_mypicturesdir = NULL;
578 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
580 #ifndef CSIDL_FLAG_CREATE
581 # define CSIDL_FLAG_CREATE 0x8000
583 #ifndef CSIDL_MYPICTURES
584 # define CSIDL_MYPICTURES 0x27
586 #ifndef SHGFP_TYPE_CURRENT
587 # define SHGFP_TYPE_CURRENT 0
590 HINSTANCE shfolder_dll;
591 SHGETFOLDERPATH SHGetFolderPath ;
593 /* load the shfolder dll to retrieve SHGetFolderPath */
594 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
596 wchar_t wdir[PATH_MAX];
597 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
598 _T("SHGetFolderPathW") );
599 if ((SHGetFolderPath != NULL )
600 && SUCCEEDED (SHGetFolderPath (NULL,
601 CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
602 NULL, SHGFP_TYPE_CURRENT,
604 p_mypicturesdir = FromWide (wdir);
606 FreeLibrary( shfolder_dll );
609 if( p_mypicturesdir == NULL )
610 val.psz_string = strdup( config_GetHomeDir() );
612 val.psz_string = p_mypicturesdir;
616 /* XXX: This saves in the data directory. Shouldn't we try saving
617 * to psz_homedir/Desktop or something nicer ? */
618 char *psz_datadir = config_GetUserDataDir();
619 if( !val.psz_string && psz_datadir )
621 if( asprintf( &val.psz_string, "%s", psz_datadir ) == -1 )
622 val.psz_string = NULL;
627 if( !val.psz_string )
629 msg_Err( p_vout, "no path specified for snapshots" );
630 image_HandlerDelete( p_image );
633 var_Get( p_vout, "snapshot-format", &format );
634 if( !format.psz_string || !*format.psz_string )
636 free( format.psz_string );
637 format.psz_string = strdup( "png" );
641 * Did the user specify a directory? If not, path = NULL.
643 path = utf8_opendir ( (const char *)val.psz_string );
646 char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
647 if( psz_prefix == NULL )
648 psz_prefix = strdup( "vlcsnap-" );
651 char *psz_tmp = str_format( p_vout, psz_prefix );
652 filename_sanitize( psz_tmp );
654 psz_prefix = psz_tmp;
658 if( var_GetBool( p_vout, "snapshot-sequential" ) == true )
660 int i_num = var_GetInteger( p_vout, "snapshot-num" );
664 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
665 val.psz_string, psz_prefix, i_num++,
666 format.psz_string ) == -1 )
668 msg_Err( p_vout, "could not create snapshot" );
669 image_HandlerDelete( p_image );
673 while( ( p_file = utf8_fopen( psz_filename, "r" ) ) && !fclose( p_file ) );
674 var_SetInteger( p_vout, "snapshot-num", i_num );
678 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
679 val.psz_string, psz_prefix,
680 (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
681 format.psz_string ) == -1 )
683 msg_Err( p_vout, "could not create snapshot" );
684 image_HandlerDelete( p_image );
691 else // The user specified a full path name (including file name)
693 psz_filename = str_format( p_vout, val.psz_string );
694 path_sanitize( psz_filename );
697 free( val.psz_string );
698 free( format.psz_string );
700 fmt_out.i_width = var_GetInteger( p_vout, "snapshot-width" );
701 fmt_out.i_height = var_GetInteger( p_vout, "snapshot-height" );
703 fmt_in = p_vout->fmt_in;
705 if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
707 fmt_out.i_width = (fmt_in.i_width * fmt_out.i_height) / fmt_in.i_height;
709 else if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
711 fmt_out.i_height = (fmt_in.i_height * fmt_out.i_width) / fmt_in.i_width;
715 fmt_out.i_width = fmt_in.i_width;
716 fmt_out.i_height = fmt_in.i_height;
719 /* Save the snapshot */
720 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
721 i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
722 if( i_ret != VLC_SUCCESS )
724 msg_Err( p_vout, "could not create snapshot %s", psz_filename );
725 free( psz_filename );
726 image_HandlerDelete( p_image );
730 msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
731 vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
732 "%s", psz_filename );
733 free( psz_filename );
735 if( var_GetBool( p_vout, "snapshot-preview" ) )
737 /* Inject a subpicture with the snapshot */
738 memset( &fmt_out, 0, sizeof(fmt_out) );
739 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
740 p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
741 image_HandlerDelete( p_image );
742 if( !p_pif ) return VLC_EGENERIC;
744 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
745 if( p_subpic == NULL )
747 p_pif->pf_release( p_pif );
751 p_subpic->i_channel = 0;
752 p_subpic->i_start = mdate();
753 p_subpic->i_stop = mdate() + 4000000;
754 p_subpic->b_ephemer = true;
755 p_subpic->b_fade = true;
756 p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
757 p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
759 p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
760 vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture,
762 p_pif->pf_release( p_pif );
764 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
768 image_HandlerDelete( p_image );
774 /*****************************************************************************
776 *****************************************************************************/
778 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
779 bool b_add, bool b_setconfig )
782 char *psz_string = config_GetPsz( p_vout, "vout-filter" );
784 /* Todo : Use some generic chain manipulation functions */
785 if( !psz_string ) psz_string = strdup("");
787 psz_parser = strstr( psz_string, psz_name );
792 psz_parser = psz_string;
793 if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
794 psz_string, psz_name ) == -1 )
808 memmove( psz_parser, psz_parser + strlen(psz_name) +
809 (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
810 strlen(psz_parser + strlen(psz_name)) + 1 );
812 /* Remove trailing : : */
813 if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
815 *(psz_string+strlen(psz_string ) -1 ) = '\0';
825 config_PutPsz( p_vout, "vout-filter", psz_string );
827 var_SetString( p_vout, "vout-filter", psz_string );
831 /*****************************************************************************
832 * vout_ControlDefault: default methods for video output control.
833 *****************************************************************************/
834 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
841 vout_ReleaseWindow( p_vout, NULL );
845 p_vout->b_snapshot = true;
849 msg_Dbg( p_vout, "control query not supported" );
854 /*****************************************************************************
855 * InitWindowSize: find the initial dimensions the video window should have.
856 *****************************************************************************
857 * This function will check the "width", "height" and "zoom" config options and
858 * will calculate the size that the video window should have.
859 *****************************************************************************/
860 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
861 unsigned *pi_height )
864 int i_width, i_height;
867 #define FP_FACTOR 1000 /* our fixed point factor */
869 var_Get( p_vout, "width", &val );
871 var_Get( p_vout, "height", &val );
872 i_height = val.i_int;
873 var_Get( p_vout, "zoom", &val );
874 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
876 if( i_width > 0 && i_height > 0)
878 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
879 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
882 else if( i_width > 0 )
884 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
885 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
886 p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
887 FP_FACTOR / p_vout->fmt_in.i_visible_width );
890 else if( i_height > 0 )
892 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
893 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
894 p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
895 FP_FACTOR / p_vout->fmt_in.i_visible_height );
899 if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
900 msg_Warn( p_vout, "fucked up aspect" );
901 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
902 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
904 else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
906 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
907 p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
908 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
913 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
915 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
916 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
920 msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width,
921 p_vout->i_window_height );
926 /*****************************************************************************
927 * Object variables callbacks
928 *****************************************************************************/
929 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
930 vlc_value_t oldval, vlc_value_t newval, void *p_data )
932 vout_thread_t *p_vout = (vout_thread_t *)p_this;
933 (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
934 InitWindowSize( p_vout, &p_vout->i_window_width,
935 &p_vout->i_window_height );
936 vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
937 p_vout->i_window_height );
941 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
942 vlc_value_t oldval, vlc_value_t newval, void *p_data )
944 vout_thread_t *p_vout = (vout_thread_t *)p_this;
945 int64_t i_aspect_num, i_aspect_den;
946 unsigned int i_width, i_height;
948 (void)oldval; (void)p_data;
950 /* Restore defaults */
951 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
952 p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
953 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
954 p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
956 if( !strcmp( psz_cmd, "crop" ) )
958 char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
961 /* We're using the 3:4 syntax */
962 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
963 if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
965 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
966 if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
968 i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
969 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
970 i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
971 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
973 if( i_width < p_vout->fmt_render.i_visible_width )
975 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
976 (p_vout->fmt_render.i_visible_width - i_width) / 2;
977 p_vout->fmt_in.i_visible_width = i_width;
981 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
982 (p_vout->fmt_render.i_visible_height - i_height) / 2;
983 p_vout->fmt_in.i_visible_height = i_height;
988 psz_parser = strchr( newval.psz_string, 'x' );
991 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
992 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
994 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
995 if( psz_end != psz_parser ) goto crop_end;
997 psz_parser = strchr( ++psz_end, '+' );
998 i_crop_height = strtol( psz_end, &psz_end, 10 );
999 if( psz_end != psz_parser ) goto crop_end;
1001 psz_parser = strchr( ++psz_end, '+' );
1002 i_crop_left = strtol( psz_end, &psz_end, 10 );
1003 if( psz_end != psz_parser ) goto crop_end;
1006 i_crop_top = strtol( psz_end, &psz_end, 10 );
1007 if( *psz_end != '\0' ) goto crop_end;
1009 i_width = i_crop_width;
1010 p_vout->fmt_in.i_visible_width = i_width;
1012 i_height = i_crop_height;
1013 p_vout->fmt_in.i_visible_height = i_height;
1015 p_vout->fmt_in.i_x_offset = i_crop_left;
1016 p_vout->fmt_in.i_y_offset = i_crop_top;
1020 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1021 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1023 psz_parser = strchr( newval.psz_string, '+' );
1024 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1025 if( psz_end != psz_parser ) goto crop_end;
1027 psz_parser = strchr( ++psz_end, '+' );
1028 i_crop_top = strtol( psz_end, &psz_end, 10 );
1029 if( psz_end != psz_parser ) goto crop_end;
1031 psz_parser = strchr( ++psz_end, '+' );
1032 i_crop_right = strtol( psz_end, &psz_end, 10 );
1033 if( psz_end != psz_parser ) goto crop_end;
1036 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1037 if( *psz_end != '\0' ) goto crop_end;
1039 i_width = p_vout->fmt_render.i_visible_width
1040 - i_crop_left - i_crop_right;
1041 p_vout->fmt_in.i_visible_width = i_width;
1043 i_height = p_vout->fmt_render.i_visible_height
1044 - i_crop_top - i_crop_bottom;
1045 p_vout->fmt_in.i_visible_height = i_height;
1047 p_vout->fmt_in.i_x_offset = i_crop_left;
1048 p_vout->fmt_in.i_y_offset = i_crop_top;
1052 else if( !strcmp( psz_cmd, "crop-top" )
1053 || !strcmp( psz_cmd, "crop-left" )
1054 || !strcmp( psz_cmd, "crop-bottom" )
1055 || !strcmp( psz_cmd, "crop-right" ) )
1057 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1059 i_crop_top = var_GetInteger( p_vout, "crop-top" );
1060 i_crop_left = var_GetInteger( p_vout, "crop-left" );
1061 i_crop_right = var_GetInteger( p_vout, "crop-right" );
1062 i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1064 i_width = p_vout->fmt_render.i_visible_width
1065 - i_crop_left - i_crop_right;
1066 p_vout->fmt_in.i_visible_width = i_width;
1068 i_height = p_vout->fmt_render.i_visible_height
1069 - i_crop_top - i_crop_bottom;
1070 p_vout->fmt_in.i_visible_height = i_height;
1072 p_vout->fmt_in.i_x_offset = i_crop_left;
1073 p_vout->fmt_in.i_y_offset = i_crop_top;
1077 InitWindowSize( p_vout, &p_vout->i_window_width,
1078 &p_vout->i_window_height );
1080 p_vout->i_changes |= VOUT_CROP_CHANGE;
1082 msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1083 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1084 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1085 p_vout->fmt_in.i_visible_width,
1086 p_vout->fmt_in.i_visible_height );
1088 var_SetVoid( p_vout, "crop-update" );
1093 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1094 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1096 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1097 unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1100 char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1101 (void)psz_cmd; (void)oldval; (void)p_data;
1103 /* Restore defaults */
1104 p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1105 p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1106 p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1107 p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1109 if( !psz_parser ) goto aspect_end;
1111 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1112 if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1114 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1115 if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1117 i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1118 i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1119 vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1120 p_vout->fmt_in.i_sar_num = i_sar_num;
1121 p_vout->fmt_in.i_sar_den = i_sar_den;
1122 p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1123 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1126 if( p_vout->i_par_num && p_vout->i_par_den )
1128 p_vout->fmt_in.i_sar_num *= p_vout->i_par_den;
1129 p_vout->fmt_in.i_sar_den *= p_vout->i_par_num;
1130 p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1131 p_vout->i_par_den / p_vout->i_par_num;
1132 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1135 p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1137 vlc_ureduce( &i_aspect_num, &i_aspect_den,
1138 p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1139 msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1140 i_aspect_num, i_aspect_den,
1141 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1143 if( var_Get( p_vout, "crop", &val ) )
1144 return VLC_EGENERIC;
1146 int i_ret = CropCallback( p_this, "crop", val, val, 0 );
1147 free( val.psz_string );
1151 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1152 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1154 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1155 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
1156 (void)psz_cmd; (void)oldval; (void)p_data;
1158 /* Modify libvlc as well because the vout might have to be restarted */
1159 var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
1160 var_Set( p_vout->p_libvlc, "video-on-top", newval );
1165 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1166 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1168 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1170 (void)psz_cmd; (void)oldval; (void)p_data;
1172 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1174 /* Modify libvlc as well because the vout might have to be restarted */
1175 var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
1176 var_Set( p_vout->p_libvlc, "fullscreen", newval );
1179 var_Set( p_vout, "intf-change", val );
1183 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1184 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1186 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1187 VLC_UNUSED(newval); VLC_UNUSED(p_data);
1188 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1189 vout_Control( p_vout, VOUT_SNAPSHOT );
1193 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1194 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1196 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1198 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1199 p_vout->b_title_show = newval.b_bool;
1203 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1204 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1206 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1207 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1208 p_vout->i_title_timeout = (mtime_t) newval.i_int;
1212 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1213 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1215 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1217 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1218 p_vout->i_title_position = newval.i_int;