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 )
151 vout_window_t *wnd = p_vout->p_window;
156 assert (wnd->control);
157 return wnd->control (wnd, i_query, args);
160 /*****************************************************************************
161 * vout_IntfInit: called during the vout creation to initialise misc things.
162 *****************************************************************************/
166 const char *psz_label;
167 } p_zoom_values[] = {
168 { 0.25, N_("1:4 Quarter") },
169 { 0.5, N_("1:2 Half") },
170 { 1, N_("1:1 Original") },
171 { 2, N_("2:1 Double") },
176 const char *psz_value;
177 const char *psz_label;
178 } p_crop_values[] = {
179 { "", N_("Default") },
180 { "16:10", "16:10" },
182 { "185:100", "1.85:1" },
183 { "221:100", "2.21:1" },
184 { "235:100", "2.35:1" },
185 { "239:100", "2.39:1" },
194 const char *psz_value;
195 const char *psz_label;
196 } p_aspect_ratio_values[] = {
197 { "", N_("Default") },
201 { "16:10", "16:10" },
202 { "221:100", "2.21:1" },
206 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
209 if( psz_list && *psz_list )
211 char *psz_cur = psz_list;
213 while( psz_cur && *psz_cur )
215 vlc_value_t val, text;
216 psz_next = strchr( psz_cur, ',' );
222 val.psz_string = psz_cur;
223 text.psz_string = psz_cur;
224 var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
230 void vout_IntfInit( vout_thread_t *p_vout )
232 vlc_value_t val, text, old_val;
233 bool b_force_par = false;
237 /* Create a few object variables we'll need later on */
238 var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
239 var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
240 var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
241 var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
242 var_Create( p_vout, "snapshot-sequential",
243 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
244 var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
245 var_SetInteger( p_vout, "snapshot-num", 1 );
246 var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
247 var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
249 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
250 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
251 p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
253 var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
254 var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
256 var_Create( p_vout, "mouse-hide-timeout",
257 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
259 p_vout->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
260 p_vout->i_title_timeout =
261 (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
262 p_vout->i_title_position =
263 var_CreateGetInteger( p_vout, "video-title-position" );
265 var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
266 var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
267 var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
269 /* Zoom object var */
270 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
271 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
273 text.psz_string = _("Zoom");
274 var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
276 var_Get( p_vout, "zoom", &old_val );
278 for( i = 0; p_zoom_values[i].f_value; i++ )
280 if( old_val.f_float == p_zoom_values[i].f_value )
281 var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
282 val.f_float = p_zoom_values[i].f_value;
283 text.psz_string = _( p_zoom_values[i].psz_label );
284 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
287 var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
289 var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
291 /* Crop offset vars */
292 var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
293 var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
294 var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
295 var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
297 var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
298 var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
299 var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
300 var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
302 /* Crop object var */
303 var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
304 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
306 text.psz_string = _("Crop");
307 var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
309 val.psz_string = (char*)"";
310 var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
312 for( i = 0; p_crop_values[i].psz_value; i++ )
314 val.psz_string = (char*)p_crop_values[i].psz_value;
315 text.psz_string = _( p_crop_values[i].psz_label );
316 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
319 /* update triggered every time the vout's crop parameters are changed */
320 var_Create( p_vout, "crop-update", VLC_VAR_VOID );
322 /* Add custom crop ratios */
323 psz_buf = config_GetPsz( p_vout, "custom-crop-ratios" );
324 AddCustomRatios( p_vout, "crop", psz_buf );
327 var_AddCallback( p_vout, "crop", CropCallback, NULL );
328 var_Get( p_vout, "crop", &old_val );
329 if( old_val.psz_string && *old_val.psz_string )
330 var_Change( p_vout, "crop", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
331 free( old_val.psz_string );
333 /* Monitor pixel aspect-ratio */
334 var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
335 var_Get( p_vout, "monitor-par", &val );
336 if( val.psz_string && *val.psz_string )
338 char *psz_parser = strchr( val.psz_string, ':' );
339 unsigned int i_aspect_num = 0, i_aspect_den = 0;
343 i_aspect_num = strtol( val.psz_string, 0, 10 );
344 i_aspect_den = strtol( ++psz_parser, 0, 10 );
348 i_aspect = atof( val.psz_string );
349 vlc_ureduce( &i_aspect_num, &i_aspect_den,
350 i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
352 if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
354 p_vout->i_par_num = i_aspect_num;
355 p_vout->i_par_den = i_aspect_den;
357 vlc_ureduce( &p_vout->i_par_num, &p_vout->i_par_den,
358 p_vout->i_par_num, p_vout->i_par_den, 0 );
360 msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
361 p_vout->i_par_num, p_vout->i_par_den );
364 free( val.psz_string );
366 /* Aspect-ratio object var */
367 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
368 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
370 text.psz_string = _("Aspect-ratio");
371 var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
373 val.psz_string = (char*)"";
374 var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
376 for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
378 val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
379 text.psz_string = _( p_aspect_ratio_values[i].psz_label );
380 var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
383 /* Add custom aspect ratios */
384 psz_buf = config_GetPsz( p_vout, "custom-aspect-ratios" );
385 AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
388 var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
389 var_Get( p_vout, "aspect-ratio", &old_val );
390 if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
391 var_Change( p_vout, "aspect-ratio", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
392 free( old_val.psz_string );
394 /* Initialize the dimensions of the video window */
395 InitWindowSize( p_vout, &p_vout->i_window_width,
396 &p_vout->i_window_height );
398 /* Add a variable to indicate if the window should be on top of others */
399 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
400 | VLC_VAR_ISCOMMAND );
401 text.psz_string = _("Always on top");
402 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
403 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
405 /* Add a variable to indicate whether we want window decoration or not */
406 var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
408 /* Add a fullscreen variable */
409 if( var_CreateGetBoolCommand( p_vout, "fullscreen" ) )
411 /* user requested fullscreen */
412 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
414 text.psz_string = _("Fullscreen");
415 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
416 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
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 );
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 );
431 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
432 var_SetBool( p_vout, "intf-change", true );
435 /*****************************************************************************
436 * vout_Snapshot: generates a snapshot.
437 *****************************************************************************/
439 * This function will inject a subpicture into the vout with the provided
442 static int VoutSnapshotPip( vout_thread_t *p_vout, image_handler_t *p_image, picture_t *p_pic, const video_format_t *p_fmt_in )
444 video_format_t fmt_in = *p_fmt_in;
445 video_format_t fmt_out;
447 subpicture_t *p_subpic;
450 memset( &fmt_out, 0, sizeof(fmt_out) );
452 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
455 p_pip = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
459 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
460 if( p_subpic == NULL )
462 picture_Release( p_pip );
466 p_subpic->i_channel = 0;
467 p_subpic->i_start = mdate();
468 p_subpic->i_stop = mdate() + 4000000;
469 p_subpic->b_ephemer = true;
470 p_subpic->b_fade = true;
471 p_subpic->i_original_picture_width = fmt_out.i_width * 4;
472 p_subpic->i_original_picture_height = fmt_out.i_height * 4;
473 fmt_out.i_aspect = 0;
475 fmt_out.i_sar_den = 0;
477 p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
478 if( p_subpic->p_region )
479 vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pip );
480 picture_Release( p_pip );
482 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
486 * This function will return the default directory used for snapshots
488 static char *VoutSnapshotGetDefaultDirectory( void )
491 #if defined(__APPLE__) || defined(SYS_BEOS)
493 if( asprintf( &psz_path, "%s/Desktop",
494 config_GetHomeDir() ) == -1 )
497 #elif defined(WIN32) && !defined(UNDER_CE)
499 /* Get the My Pictures folder path */
500 char *p_mypicturesdir = NULL;
501 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
503 #ifndef CSIDL_FLAG_CREATE
504 # define CSIDL_FLAG_CREATE 0x8000
506 #ifndef CSIDL_MYPICTURES
507 # define CSIDL_MYPICTURES 0x27
509 #ifndef SHGFP_TYPE_CURRENT
510 # define SHGFP_TYPE_CURRENT 0
513 HINSTANCE shfolder_dll;
514 SHGETFOLDERPATH SHGetFolderPath ;
516 /* load the shfolder dll to retrieve SHGetFolderPath */
517 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
519 wchar_t wdir[PATH_MAX];
520 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
521 _T("SHGetFolderPathW") );
522 if ((SHGetFolderPath != NULL )
523 && SUCCEEDED (SHGetFolderPath (NULL,
524 CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
525 NULL, SHGFP_TYPE_CURRENT,
527 p_mypicturesdir = FromWide (wdir);
529 FreeLibrary( shfolder_dll );
532 if( p_mypicturesdir == NULL )
533 psz_path = strdup( config_GetHomeDir() );
535 psz_path = p_mypicturesdir;
539 /* XXX: This saves in the data directory. Shouldn't we try saving
540 * to psz_homedir/Desktop or something nicer ? */
541 char *psz_datadir = config_GetUserDataDir();
544 if( asprintf( &psz_path, "%s", psz_datadir ) == -1 )
554 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
556 image_handler_t *p_image = image_HandlerCreate( p_vout );
557 video_format_t fmt_in, fmt_out;
558 char *psz_filename = NULL;
559 vlc_value_t val, format;
562 bool b_embedded_snapshot;
563 uintmax_t i_id = (uintptr_t)NULL;
566 val.psz_string = var_GetNonEmptyString( p_vout, "snapshot-path" );
568 /* Embedded snapshot : if snapshot-path == object:object_ptr */
569 if( val.psz_string && sscanf( val.psz_string, "object:%ju", &i_id ) > 0 )
570 b_embedded_snapshot = true;
572 b_embedded_snapshot = false;
575 memset( &fmt_in, 0, sizeof(video_format_t) );
576 fmt_in = p_vout->fmt_in;
577 if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
580 fmt_in.i_sar_den = 1;
584 memset( &fmt_out, 0, sizeof(video_format_t) );
586 fmt_out.i_sar_den = 1;
587 fmt_out.i_chroma = b_embedded_snapshot ? VLC_FOURCC('p','n','g',' ') : 0;
588 fmt_out.i_width = var_GetInteger( p_vout, "snapshot-width" );
589 fmt_out.i_height = var_GetInteger( p_vout, "snapshot-height" );
591 if( b_embedded_snapshot &&
592 fmt_out.i_width == 0 && fmt_out.i_height == 0 )
594 /* If snapshot-width and/or snapshot height were not specified,
595 use a default snapshot width of 320 */
596 fmt_out.i_width = 320;
599 if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
601 fmt_out.i_height = fmt_in.i_height * fmt_out.i_width / fmt_in.i_width;
602 const int i_height = fmt_out.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
604 fmt_out.i_height = i_height;
608 if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
610 fmt_out.i_width = fmt_in.i_width * fmt_out.i_height / fmt_in.i_height;
614 fmt_out.i_width = fmt_in.i_width;
615 fmt_out.i_height = fmt_in.i_height;
617 const int i_width = fmt_out.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
619 fmt_out.i_width = i_width;
623 create a snapshot_t* and store it in
624 object_ptr->p_private, then unlock and signal the
627 if( b_embedded_snapshot )
629 vlc_object_t* p_dest = (vlc_object_t *)(uintptr_t)i_id;
631 snapshot_t *p_snapshot;
634 /* Object must be locked. We will unlock it once we get the
635 snapshot and written it to p_private */
636 p_dest->p_private = NULL;
638 /* Save the snapshot to a memory zone */
639 p_block = image_Write( p_image, p_pic, &fmt_in, &fmt_out );
642 msg_Err( p_vout, "Could not get snapshot" );
643 image_HandlerDelete( p_image );
644 vlc_object_signal( p_dest );
645 vlc_object_release( p_dest );
649 /* Copy the p_block data to a snapshot structure */
650 /* FIXME: get the timestamp */
651 p_snapshot = malloc( sizeof( snapshot_t ) );
654 block_Release( p_block );
655 image_HandlerDelete( p_image );
656 vlc_object_signal( p_dest );
657 vlc_object_release( p_dest );
661 i_size = p_block->i_buffer;
663 p_snapshot->i_width = fmt_out.i_width;
664 p_snapshot->i_height = fmt_out.i_height;
665 p_snapshot->i_datasize = i_size;
666 p_snapshot->date = p_block->i_pts; /* FIXME ?? */
667 p_snapshot->p_data = malloc( i_size );
668 if( !p_snapshot->p_data )
670 block_Release( p_block );
672 image_HandlerDelete( p_image );
673 vlc_object_signal( p_dest );
674 vlc_object_release( p_dest );
677 memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
679 p_dest->p_private = p_snapshot;
681 block_Release( p_block );
683 /* Unlock the object */
684 vlc_object_signal( p_dest );
685 vlc_object_release( p_dest );
687 image_HandlerDelete( p_image );
691 /* Get default directory if none provided */
692 if( !val.psz_string )
693 val.psz_string = VoutSnapshotGetDefaultDirectory( );
694 if( !val.psz_string )
696 msg_Err( p_vout, "no path specified for snapshots" );
697 image_HandlerDelete( p_image );
701 /* Get snapshot format, default being "png" */
702 format.psz_string = var_GetNonEmptyString( p_vout, "snapshot-format" );
703 if( !format.psz_string )
704 format.psz_string = strdup( "png" );
705 if( !format.psz_string )
707 free( val.psz_string );
708 image_HandlerDelete( p_image );
713 * Did the user specify a directory? If not, path = NULL.
715 path = utf8_opendir ( (const char *)val.psz_string );
718 char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
719 if( psz_prefix == NULL )
720 psz_prefix = strdup( "vlcsnap-" );
723 char *psz_tmp = str_format( p_vout, psz_prefix );
724 filename_sanitize( psz_tmp );
726 psz_prefix = psz_tmp;
730 if( var_GetBool( p_vout, "snapshot-sequential" ) == true )
732 int i_num = var_GetInteger( p_vout, "snapshot-num" );
736 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
737 val.psz_string, psz_prefix, i_num++,
738 format.psz_string ) == -1 )
740 msg_Err( p_vout, "could not create snapshot" );
741 image_HandlerDelete( p_image );
745 while( ( p_file = utf8_fopen( psz_filename, "r" ) ) && !fclose( p_file ) );
746 var_SetInteger( p_vout, "snapshot-num", i_num );
750 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
751 val.psz_string, psz_prefix,
752 (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
753 format.psz_string ) == -1 )
755 msg_Err( p_vout, "could not create snapshot" );
756 image_HandlerDelete( p_image );
763 else // The user specified a full path name (including file name)
765 psz_filename = str_format( p_vout, val.psz_string );
766 path_sanitize( psz_filename );
769 free( val.psz_string );
770 free( format.psz_string );
772 /* Save the snapshot */
773 i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
774 if( i_ret != VLC_SUCCESS )
776 msg_Err( p_vout, "could not create snapshot %s", psz_filename );
777 free( psz_filename );
778 image_HandlerDelete( p_image );
783 msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
784 vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
785 "%s", psz_filename );
786 free( psz_filename );
789 if( var_GetBool( p_vout, "snapshot-preview" ) )
791 if( VoutSnapshotPip( p_vout, p_image, p_pic, &fmt_in ) )
792 msg_Warn( p_vout, "Failed to display snapshot" );
794 image_HandlerDelete( p_image );
799 /*****************************************************************************
801 *****************************************************************************/
803 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
804 bool b_add, bool b_setconfig )
807 char *psz_string = config_GetPsz( p_vout, "vout-filter" );
809 /* Todo : Use some generic chain manipulation functions */
810 if( !psz_string ) psz_string = strdup("");
812 psz_parser = strstr( psz_string, psz_name );
817 psz_parser = psz_string;
818 if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
819 psz_string, psz_name ) == -1 )
833 memmove( psz_parser, psz_parser + strlen(psz_name) +
834 (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
835 strlen(psz_parser + strlen(psz_name)) + 1 );
837 /* Remove trailing : : */
838 if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
840 *(psz_string+strlen(psz_string ) -1 ) = '\0';
850 config_PutPsz( p_vout, "vout-filter", psz_string );
852 var_SetString( p_vout, "vout-filter", psz_string );
856 /*****************************************************************************
857 * vout_ControlDefault: default methods for video output control.
858 *****************************************************************************/
859 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
866 vout_ReleaseWindow( p_vout, NULL );
870 p_vout->b_snapshot = true;
874 msg_Dbg( p_vout, "control query not supported" );
879 /*****************************************************************************
880 * InitWindowSize: find the initial dimensions the video window should have.
881 *****************************************************************************
882 * This function will check the "width", "height" and "zoom" config options and
883 * will calculate the size that the video window should have.
884 *****************************************************************************/
885 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
886 unsigned *pi_height )
889 int i_width, i_height;
892 #define FP_FACTOR 1000 /* our fixed point factor */
894 var_Get( p_vout, "width", &val );
896 var_Get( p_vout, "height", &val );
897 i_height = val.i_int;
898 var_Get( p_vout, "zoom", &val );
899 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
901 if( i_width > 0 && i_height > 0)
903 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
904 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
907 else if( i_width > 0 )
909 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
910 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
911 p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
912 FP_FACTOR / p_vout->fmt_in.i_visible_width );
915 else if( i_height > 0 )
917 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
918 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
919 p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
920 FP_FACTOR / p_vout->fmt_in.i_visible_height );
924 if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
925 msg_Warn( p_vout, "fucked up aspect" );
926 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
927 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
929 else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
931 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
932 p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
933 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
938 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
940 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
941 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
945 msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width,
946 p_vout->i_window_height );
951 /*****************************************************************************
952 * Object variables callbacks
953 *****************************************************************************/
954 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
955 vlc_value_t oldval, vlc_value_t newval, void *p_data )
957 vout_thread_t *p_vout = (vout_thread_t *)p_this;
958 (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
959 InitWindowSize( p_vout, &p_vout->i_window_width,
960 &p_vout->i_window_height );
961 vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
962 p_vout->i_window_height );
966 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
967 vlc_value_t oldval, vlc_value_t newval, void *p_data )
969 vout_thread_t *p_vout = (vout_thread_t *)p_this;
970 int64_t i_aspect_num, i_aspect_den;
971 unsigned int i_width, i_height;
973 (void)oldval; (void)p_data;
975 /* Restore defaults */
976 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
977 p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
978 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
979 p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
981 if( !strcmp( psz_cmd, "crop" ) )
983 char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
986 /* We're using the 3:4 syntax */
987 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
988 if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
990 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
991 if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
993 i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
994 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
995 i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
996 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
998 if( i_width < p_vout->fmt_render.i_visible_width )
1000 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
1001 (p_vout->fmt_render.i_visible_width - i_width) / 2;
1002 p_vout->fmt_in.i_visible_width = i_width;
1006 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
1007 (p_vout->fmt_render.i_visible_height - i_height) / 2;
1008 p_vout->fmt_in.i_visible_height = i_height;
1013 psz_parser = strchr( newval.psz_string, 'x' );
1016 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
1017 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
1019 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
1020 if( psz_end != psz_parser ) goto crop_end;
1022 psz_parser = strchr( ++psz_end, '+' );
1023 i_crop_height = strtol( psz_end, &psz_end, 10 );
1024 if( psz_end != psz_parser ) goto crop_end;
1026 psz_parser = strchr( ++psz_end, '+' );
1027 i_crop_left = strtol( psz_end, &psz_end, 10 );
1028 if( psz_end != psz_parser ) goto crop_end;
1031 i_crop_top = strtol( psz_end, &psz_end, 10 );
1032 if( *psz_end != '\0' ) goto crop_end;
1034 i_width = i_crop_width;
1035 p_vout->fmt_in.i_visible_width = i_width;
1037 i_height = i_crop_height;
1038 p_vout->fmt_in.i_visible_height = i_height;
1040 p_vout->fmt_in.i_x_offset = i_crop_left;
1041 p_vout->fmt_in.i_y_offset = i_crop_top;
1045 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1046 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1048 psz_parser = strchr( newval.psz_string, '+' );
1049 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1050 if( psz_end != psz_parser ) goto crop_end;
1052 psz_parser = strchr( ++psz_end, '+' );
1053 i_crop_top = strtol( psz_end, &psz_end, 10 );
1054 if( psz_end != psz_parser ) goto crop_end;
1056 psz_parser = strchr( ++psz_end, '+' );
1057 i_crop_right = strtol( psz_end, &psz_end, 10 );
1058 if( psz_end != psz_parser ) goto crop_end;
1061 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1062 if( *psz_end != '\0' ) goto crop_end;
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 else if( !strcmp( psz_cmd, "crop-top" )
1078 || !strcmp( psz_cmd, "crop-left" )
1079 || !strcmp( psz_cmd, "crop-bottom" )
1080 || !strcmp( psz_cmd, "crop-right" ) )
1082 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1084 i_crop_top = var_GetInteger( p_vout, "crop-top" );
1085 i_crop_left = var_GetInteger( p_vout, "crop-left" );
1086 i_crop_right = var_GetInteger( p_vout, "crop-right" );
1087 i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1089 i_width = p_vout->fmt_render.i_visible_width
1090 - i_crop_left - i_crop_right;
1091 p_vout->fmt_in.i_visible_width = i_width;
1093 i_height = p_vout->fmt_render.i_visible_height
1094 - i_crop_top - i_crop_bottom;
1095 p_vout->fmt_in.i_visible_height = i_height;
1097 p_vout->fmt_in.i_x_offset = i_crop_left;
1098 p_vout->fmt_in.i_y_offset = i_crop_top;
1102 InitWindowSize( p_vout, &p_vout->i_window_width,
1103 &p_vout->i_window_height );
1105 p_vout->i_changes |= VOUT_CROP_CHANGE;
1107 msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1108 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1109 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1110 p_vout->fmt_in.i_visible_width,
1111 p_vout->fmt_in.i_visible_height );
1113 var_SetVoid( p_vout, "crop-update" );
1118 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1119 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1121 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1122 unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1125 char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1126 (void)psz_cmd; (void)oldval; (void)p_data;
1128 /* Restore defaults */
1129 p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1130 p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1131 p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1132 p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1134 if( !psz_parser ) goto aspect_end;
1136 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1137 if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1139 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1140 if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1142 i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1143 i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1144 vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1145 p_vout->fmt_in.i_sar_num = i_sar_num;
1146 p_vout->fmt_in.i_sar_den = i_sar_den;
1147 p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1148 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1151 if( p_vout->i_par_num && p_vout->i_par_den )
1153 p_vout->fmt_in.i_sar_num *= p_vout->i_par_den;
1154 p_vout->fmt_in.i_sar_den *= p_vout->i_par_num;
1155 p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1156 p_vout->i_par_den / p_vout->i_par_num;
1157 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1160 p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1162 vlc_ureduce( &i_aspect_num, &i_aspect_den,
1163 p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1164 msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1165 i_aspect_num, i_aspect_den,
1166 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1168 if( var_Get( p_vout, "crop", &val ) )
1169 return VLC_EGENERIC;
1171 int i_ret = CropCallback( p_this, "crop", val, val, 0 );
1172 free( val.psz_string );
1176 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1177 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1179 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1180 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
1181 (void)psz_cmd; (void)oldval; (void)p_data;
1183 /* Modify libvlc as well because the vout might have to be restarted */
1184 var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
1185 var_Set( p_vout->p_libvlc, "video-on-top", newval );
1190 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1191 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1193 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1195 (void)psz_cmd; (void)oldval; (void)p_data;
1197 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1199 /* Modify libvlc as well because the vout might have to be restarted */
1200 var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
1201 var_Set( p_vout->p_libvlc, "fullscreen", newval );
1204 var_Set( p_vout, "intf-change", val );
1208 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1209 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1211 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1212 VLC_UNUSED(newval); VLC_UNUSED(p_data);
1213 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1214 vout_Control( p_vout, VOUT_SNAPSHOT );
1218 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1219 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1221 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1223 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1224 p_vout->b_title_show = newval.b_bool;
1228 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1229 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1231 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1232 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1233 p_vout->i_title_timeout = (mtime_t) newval.i_int;
1237 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1238 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1240 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1242 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1243 p_vout->i_title_position = newval.i_int;