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() */
37 #include <dirent.h> /* opendir() */
39 #include <time.h> /* strftime */
41 #include <vlc_interface.h>
42 #include <vlc_block.h>
43 #include <vlc_playlist.h>
46 #include <vlc_window.h>
47 #include <vlc_image.h>
49 #include <vlc_charset.h>
51 #include <vlc_strings.h>
52 #include <vlc_charset.h>
53 #include "../libvlc.h"
54 #include "vout_internal.h"
56 /*****************************************************************************
58 *****************************************************************************/
59 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
61 /* Object variables callbacks */
62 static int ZoomCallback( vlc_object_t *, char const *,
63 vlc_value_t, vlc_value_t, void * );
64 static int CropCallback( vlc_object_t *, char const *,
65 vlc_value_t, vlc_value_t, void * );
66 static int AspectCallback( vlc_object_t *, char const *,
67 vlc_value_t, vlc_value_t, void * );
68 static int ScalingCallback( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
70 static int OnTopCallback( vlc_object_t *, char const *,
71 vlc_value_t, vlc_value_t, void * );
72 static int FullscreenCallback( vlc_object_t *, char const *,
73 vlc_value_t, vlc_value_t, void * );
74 static int SnapshotCallback( vlc_object_t *, char const *,
75 vlc_value_t, vlc_value_t, void * );
77 static int TitleShowCallback( vlc_object_t *, char const *,
78 vlc_value_t, vlc_value_t, void * );
79 static int TitleTimeoutCallback( vlc_object_t *, char const *,
80 vlc_value_t, vlc_value_t, void * );
81 static int TitlePositionCallback( vlc_object_t *, char const *,
82 vlc_value_t, vlc_value_t, void * );
85 * Creates a video output window.
86 * On Unix systems, this is an X11 drawable (handle).
87 * On Windows, this is a Win32 window (handle).
88 * Video output plugins are supposed to called this function and display the
89 * video within the resulting window, while in windowed mode.
91 * @param p_vout video output thread to create a window for
92 * @param psz_cap VLC module capability (window system type)
93 * @param pi_x_hint pointer to store the recommended horizontal position [OUT]
94 * @param pi_y_hint pointer to store the recommended vertical position [OUT]
95 * @param pi_width_hint pointer to store the recommended width [OUT]
96 * @param pi_height_hint pointer to store the recommended height [OUT]
98 * @return a vout_window_t object, or NULL in case of failure.
99 * The window is released with vout_ReleaseWindow().
101 vout_window_t *vout_RequestWindow( vout_thread_t *p_vout, const char *psz_cap,
102 int *pi_x_hint, int *pi_y_hint,
103 unsigned int *pi_width_hint,
104 unsigned int *pi_height_hint )
106 /* Get requested coordinates */
107 *pi_x_hint = var_GetInteger( p_vout, "video-x" );
108 *pi_y_hint = var_GetInteger( p_vout, "video-y" );
110 *pi_width_hint = p_vout->i_window_width;
111 *pi_height_hint = p_vout->i_window_height;
113 /* Check whether someone provided us with a window ID */
114 int drawable = var_CreateGetInteger( p_vout, "drawable" );
115 if( drawable ) return (void *)(intptr_t)drawable;
117 vout_window_t *wnd = vlc_custom_create (VLC_OBJECT(p_vout), sizeof (*wnd),
118 VLC_OBJECT_GENERIC, "window");
123 wnd->width = *pi_width_hint;
124 wnd->height = *pi_height_hint;
125 wnd->pos_x = *pi_x_hint;
126 wnd->pos_y = *pi_y_hint;
127 vlc_object_attach (wnd, p_vout);
129 wnd->module = module_need (wnd, psz_cap, NULL, false);
130 if (wnd->module == NULL)
132 msg_Dbg (wnd, "no \"%s\" window provider available", psz_cap);
133 vlc_object_release (wnd);
136 *pi_width_hint = wnd->width;
137 *pi_height_hint = wnd->height;
138 *pi_x_hint = wnd->pos_x;
139 *pi_y_hint = wnd->pos_y;
144 * Releases a window handle obtained with vout_RequestWindow().
145 * @param p_vout video output thread that allocated the window
146 * (if this is NULL; this fnction is a no-op).
148 void vout_ReleaseWindow( vout_window_t *wnd )
153 assert (wnd->module);
154 module_unneed (wnd, wnd->module);
156 vlc_object_release (wnd);
159 int vout_ControlWindow( vout_window_t *wnd, int i_query, va_list args )
164 assert (wnd->control);
165 return wnd->control (wnd, i_query, args);
168 /*****************************************************************************
169 * vout_IntfInit: called during the vout creation to initialise misc things.
170 *****************************************************************************/
174 const char *psz_label;
175 } p_zoom_values[] = {
176 { 0.25, N_("1:4 Quarter") },
177 { 0.5, N_("1:2 Half") },
178 { 1, N_("1:1 Original") },
179 { 2, N_("2:1 Double") },
184 const char *psz_value;
185 const char *psz_label;
186 } p_crop_values[] = {
187 { "", N_("Default") },
188 { "16:10", "16:10" },
190 { "185:100", "1.85:1" },
191 { "221:100", "2.21:1" },
192 { "235:100", "2.35:1" },
193 { "239:100", "2.39:1" },
202 const char *psz_value;
203 const char *psz_label;
204 } p_aspect_ratio_values[] = {
205 { "", N_("Default") },
209 { "16:10", "16:10" },
210 { "221:100", "2.21:1" },
214 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
217 if( psz_list && *psz_list )
219 char *psz_cur = psz_list;
221 while( psz_cur && *psz_cur )
223 vlc_value_t val, text;
224 psz_next = strchr( psz_cur, ',' );
230 val.psz_string = psz_cur;
231 text.psz_string = psz_cur;
232 var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
238 void vout_IntfInit( vout_thread_t *p_vout )
240 vlc_value_t val, text, old_val;
241 bool b_force_par = false;
245 /* Create a few object variables we'll need later on */
246 var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
247 var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
248 var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
249 var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
250 var_Create( p_vout, "snapshot-sequential",
251 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
252 var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
253 var_SetInteger( p_vout, "snapshot-num", 1 );
254 var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
255 var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
257 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
258 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
259 p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
261 var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
262 var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
264 var_Create( p_vout, "mouse-hide-timeout",
265 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
267 p_vout->p->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
268 p_vout->p->i_title_timeout =
269 (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
270 p_vout->p->i_title_position =
271 var_CreateGetInteger( p_vout, "video-title-position" );
272 p_vout->p->psz_title = NULL;
274 var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
275 var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
276 var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
278 /* Zoom object var */
279 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
280 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
282 text.psz_string = _("Zoom");
283 var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
285 var_Get( p_vout, "zoom", &old_val );
287 for( i = 0; p_zoom_values[i].f_value; i++ )
289 if( old_val.f_float == p_zoom_values[i].f_value )
290 var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
291 val.f_float = p_zoom_values[i].f_value;
292 text.psz_string = _( p_zoom_values[i].psz_label );
293 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
296 var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
298 var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
300 /* Crop offset vars */
301 var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
302 var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
303 var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
304 var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
306 var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
307 var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
308 var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
309 var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
311 /* Crop object var */
312 var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
313 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
315 text.psz_string = _("Crop");
316 var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
318 val.psz_string = (char*)"";
319 var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
321 for( i = 0; p_crop_values[i].psz_value; i++ )
323 val.psz_string = (char*)p_crop_values[i].psz_value;
324 text.psz_string = _( p_crop_values[i].psz_label );
325 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
328 /* update triggered every time the vout's crop parameters are changed */
329 var_Create( p_vout, "crop-update", VLC_VAR_VOID );
331 /* Add custom crop ratios */
332 psz_buf = config_GetPsz( p_vout, "custom-crop-ratios" );
333 AddCustomRatios( p_vout, "crop", psz_buf );
336 var_AddCallback( p_vout, "crop", CropCallback, NULL );
337 var_Get( p_vout, "crop", &old_val );
338 if( old_val.psz_string && *old_val.psz_string )
339 var_TriggerCallback( p_vout, "crop" );
340 free( old_val.psz_string );
342 /* Monitor pixel aspect-ratio */
343 var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
344 var_Get( p_vout, "monitor-par", &val );
345 if( val.psz_string && *val.psz_string )
347 char *psz_parser = strchr( val.psz_string, ':' );
348 unsigned int i_aspect_num = 0, i_aspect_den = 0;
352 i_aspect_num = strtol( val.psz_string, 0, 10 );
353 i_aspect_den = strtol( ++psz_parser, 0, 10 );
357 i_aspect = atof( val.psz_string );
358 vlc_ureduce( &i_aspect_num, &i_aspect_den,
359 i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
361 if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
363 p_vout->p->i_par_num = i_aspect_num;
364 p_vout->p->i_par_den = i_aspect_den;
366 vlc_ureduce( &p_vout->p->i_par_num, &p_vout->p->i_par_den,
367 p_vout->p->i_par_num, p_vout->p->i_par_den, 0 );
369 msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
370 p_vout->p->i_par_num, p_vout->p->i_par_den );
373 free( val.psz_string );
375 /* Aspect-ratio object var */
376 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
377 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
379 text.psz_string = _("Aspect-ratio");
380 var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
382 val.psz_string = (char*)"";
383 var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
385 for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
387 val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
388 text.psz_string = _( p_aspect_ratio_values[i].psz_label );
389 var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
392 /* Add custom aspect ratios */
393 psz_buf = config_GetPsz( p_vout, "custom-aspect-ratios" );
394 AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
397 var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
398 var_Get( p_vout, "aspect-ratio", &old_val );
399 if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
400 var_TriggerCallback( p_vout, "aspect-ratio" );
401 free( old_val.psz_string );
403 /* Add variables to manage scaling video */
404 var_Create( p_vout, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
405 | VLC_VAR_ISCOMMAND );
406 text.psz_string = _("Autoscale video");
407 var_Change( p_vout, "autoscale", VLC_VAR_SETTEXT, &text, NULL );
408 var_AddCallback( p_vout, "autoscale", ScalingCallback, NULL );
409 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
411 var_Create( p_vout, "scale", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT
412 | VLC_VAR_ISCOMMAND );
413 text.psz_string = _("Scale factor");
414 var_Change( p_vout, "scale", VLC_VAR_SETTEXT, &text, NULL );
415 var_AddCallback( p_vout, "scale", ScalingCallback, NULL );
416 p_vout->i_zoom = (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
418 /* Initialize the dimensions of the video window */
419 InitWindowSize( p_vout, &p_vout->i_window_width,
420 &p_vout->i_window_height );
422 /* Add a variable to indicate if the window should be on top of others */
423 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
424 | VLC_VAR_ISCOMMAND );
425 text.psz_string = _("Always on top");
426 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
427 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
429 /* Add a variable to indicate whether we want window decoration or not */
430 var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
432 /* Add a fullscreen variable */
433 if( var_CreateGetBoolCommand( p_vout, "fullscreen" ) )
435 /* user requested fullscreen */
436 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
438 text.psz_string = _("Fullscreen");
439 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
440 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
442 /* Add a snapshot variable */
443 var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
444 text.psz_string = _("Snapshot");
445 var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
446 var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
448 /* Mouse coordinates */
449 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
450 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
451 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
452 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
453 var_Create( p_vout, "mouse-clicked", VLC_VAR_BOOL );
455 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
456 var_SetBool( p_vout, "intf-change", true );
459 /*****************************************************************************
460 * vout_Snapshot: generates a snapshot.
461 *****************************************************************************/
463 * This function will inject a subpicture into the vout with the provided
466 static int VoutSnapshotPip( vout_thread_t *p_vout, image_handler_t *p_image, picture_t *p_pic, const video_format_t *p_fmt_in )
468 video_format_t fmt_in = *p_fmt_in;
469 video_format_t fmt_out;
471 subpicture_t *p_subpic;
474 memset( &fmt_out, 0, sizeof(fmt_out) );
476 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
479 p_pip = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
483 p_subpic = subpicture_New();
484 if( p_subpic == NULL )
486 picture_Release( p_pip );
490 p_subpic->i_channel = 0;
491 p_subpic->i_start = mdate();
492 p_subpic->i_stop = mdate() + 4000000;
493 p_subpic->b_ephemer = true;
494 p_subpic->b_fade = true;
495 p_subpic->i_original_picture_width = fmt_out.i_width * 4;
496 p_subpic->i_original_picture_height = fmt_out.i_height * 4;
497 fmt_out.i_aspect = 0;
499 fmt_out.i_sar_den = 0;
501 p_subpic->p_region = subpicture_region_New( &fmt_out );
502 if( p_subpic->p_region )
504 picture_Release( p_subpic->p_region->p_picture );
505 p_subpic->p_region->p_picture = p_pip;
509 picture_Release( p_pip );
512 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
516 * This function will return the default directory used for snapshots
518 static char *VoutSnapshotGetDefaultDirectory( void )
520 char *psz_path = NULL;
521 #if defined(__APPLE__) || defined(SYS_BEOS)
523 if( asprintf( &psz_path, "%s/Desktop",
524 config_GetHomeDir() ) == -1 )
527 #elif defined(WIN32) && !defined(UNDER_CE)
529 /* Get the My Pictures folder path */
530 char *p_mypicturesdir = NULL;
531 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
533 #ifndef CSIDL_FLAG_CREATE
534 # define CSIDL_FLAG_CREATE 0x8000
536 #ifndef CSIDL_MYPICTURES
537 # define CSIDL_MYPICTURES 0x27
539 #ifndef SHGFP_TYPE_CURRENT
540 # define SHGFP_TYPE_CURRENT 0
543 HINSTANCE shfolder_dll;
544 SHGETFOLDERPATH SHGetFolderPath ;
546 /* load the shfolder dll to retrieve SHGetFolderPath */
547 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
549 wchar_t wdir[PATH_MAX];
550 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
551 _T("SHGetFolderPathW") );
552 if ((SHGetFolderPath != NULL )
553 && SUCCEEDED (SHGetFolderPath (NULL,
554 CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
555 NULL, SHGFP_TYPE_CURRENT,
557 p_mypicturesdir = FromWide (wdir);
559 FreeLibrary( shfolder_dll );
562 if( p_mypicturesdir == NULL )
563 psz_path = strdup( config_GetHomeDir() );
565 psz_path = p_mypicturesdir;
569 /* XXX: This saves in the data directory. Shouldn't we try saving
570 * to psz_homedir/Desktop or something nicer ? */
571 char *psz_datadir = config_GetUserDataDir();
574 if( asprintf( &psz_path, "%s", psz_datadir ) == -1 )
584 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
586 image_handler_t *p_image = image_HandlerCreate( p_vout );
587 video_format_t fmt_in, fmt_out;
588 char *psz_filename = NULL;
589 vlc_value_t val, format;
592 bool b_embedded_snapshot;
596 val.psz_string = var_GetNonEmptyString( p_vout, "snapshot-path" );
598 /* Embedded snapshot : if snapshot-path == object:object_ptr */
599 if( val.psz_string && sscanf( val.psz_string, "object:%p", &p_obj ) > 0 )
600 b_embedded_snapshot = true;
602 b_embedded_snapshot = false;
605 memset( &fmt_in, 0, sizeof(video_format_t) );
606 fmt_in = p_vout->fmt_out;
607 if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
610 fmt_in.i_sar_den = 1;
614 memset( &fmt_out, 0, sizeof(video_format_t) );
616 fmt_out.i_sar_den = 1;
617 fmt_out.i_chroma = b_embedded_snapshot ? VLC_FOURCC('p','n','g',' ') : 0;
618 fmt_out.i_width = var_GetInteger( p_vout, "snapshot-width" );
619 fmt_out.i_height = var_GetInteger( p_vout, "snapshot-height" );
621 if( b_embedded_snapshot &&
622 fmt_out.i_width == 0 && fmt_out.i_height == 0 )
624 /* If snapshot-width and/or snapshot height were not specified,
625 use a default snapshot width of 320 */
626 fmt_out.i_width = 320;
629 if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
631 fmt_out.i_height = fmt_in.i_height * fmt_out.i_width / fmt_in.i_width;
632 const int i_height = fmt_out.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
634 fmt_out.i_height = i_height;
638 if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
640 fmt_out.i_width = fmt_in.i_width * fmt_out.i_height / fmt_in.i_height;
644 fmt_out.i_width = fmt_in.i_width;
645 fmt_out.i_height = fmt_in.i_height;
647 const int i_width = fmt_out.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
649 fmt_out.i_width = i_width;
653 create a snapshot_t* and store it in
654 object_ptr->p_private, then unlock and signal the
657 if( b_embedded_snapshot )
660 snapshot_t *p_snapshot = p_obj;
663 vlc_mutex_lock( &p_snapshot->p_mutex );
664 p_snapshot->p_data = NULL;
666 /* Save the snapshot to a memory zone */
667 p_block = image_Write( p_image, p_pic, &fmt_in, &fmt_out );
670 msg_Err( p_vout, "Could not get snapshot" );
671 image_HandlerDelete( p_image );
672 vlc_cond_signal( &p_snapshot->p_condvar );
673 vlc_mutex_unlock( &p_snapshot->p_mutex );
677 i_size = p_block->i_buffer;
679 p_snapshot->i_width = fmt_out.i_width;
680 p_snapshot->i_height = fmt_out.i_height;
681 p_snapshot->i_datasize = i_size;
682 p_snapshot->date = p_block->i_pts; /* FIXME ?? */
683 p_snapshot->p_data = malloc( i_size );
684 if( !p_snapshot->p_data )
686 block_Release( p_block );
687 image_HandlerDelete( p_image );
688 vlc_cond_signal( &p_snapshot->p_condvar );
689 vlc_mutex_unlock( &p_snapshot->p_mutex );
692 memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
694 block_Release( p_block );
696 /* Unlock the object */
697 vlc_cond_signal( &p_snapshot->p_condvar );
698 vlc_mutex_unlock( &p_snapshot->p_mutex );
700 image_HandlerDelete( p_image );
704 /* Get default directory if none provided */
705 if( !val.psz_string )
706 val.psz_string = VoutSnapshotGetDefaultDirectory( );
707 if( !val.psz_string )
709 msg_Err( p_vout, "no path specified for snapshots" );
710 image_HandlerDelete( p_image );
714 /* Get snapshot format, default being "png" */
715 format.psz_string = var_GetNonEmptyString( p_vout, "snapshot-format" );
716 if( !format.psz_string )
717 format.psz_string = strdup( "png" );
718 if( !format.psz_string )
720 free( val.psz_string );
721 image_HandlerDelete( p_image );
726 * Did the user specify a directory? If not, path = NULL.
728 path = utf8_opendir ( (const char *)val.psz_string );
731 char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
732 if( psz_prefix == NULL )
733 psz_prefix = strdup( "vlcsnap-" );
736 char *psz_tmp = str_format( p_vout, psz_prefix );
737 filename_sanitize( psz_tmp );
739 psz_prefix = psz_tmp;
743 if( var_GetBool( p_vout, "snapshot-sequential" ) == true )
745 int i_num = var_GetInteger( p_vout, "snapshot-num" );
750 free( psz_filename );
751 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
752 val.psz_string, psz_prefix, i_num++,
753 format.psz_string ) == -1 )
755 msg_Err( p_vout, "could not create snapshot" );
756 image_HandlerDelete( p_image );
760 while( utf8_stat( psz_filename, &st ) == 0 );
762 var_SetInteger( p_vout, "snapshot-num", i_num );
768 lcurtime = time( NULL ) ;
769 if ( ( localtime_r( &lcurtime, &curtime ) == NULL ) )
771 msg_Warn( p_vout, "failed to get current time. Falling back to legacy snapshot naming" );
772 /* failed to get current time. Fallback to old format */
773 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
774 val.psz_string, psz_prefix,
775 (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
776 format.psz_string ) == -1 )
778 msg_Err( p_vout, "could not create snapshot" );
779 image_HandlerDelete( p_image );
785 char psz_curtime[15] ;
786 if( strftime( psz_curtime, 15, "%y%m%d-%H%M%S", &curtime ) == 0 )
788 msg_Warn( p_vout, "snapshot date string truncated" ) ;
790 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%s%1u.%s",
791 val.psz_string, psz_prefix, psz_curtime,
792 /* suffix with the last decimal digit in 10s of seconds resolution */
793 (unsigned int)(p_pic->date / (100*1000)) & 0xFF,
794 format.psz_string ) == -1 )
796 msg_Err( p_vout, "could not create snapshot" );
797 image_HandlerDelete( p_image );
800 } //end if time() < 0
801 } //end snapshot sequential
804 else // The user specified a full path name (including file name)
806 psz_filename = str_format( p_vout, val.psz_string );
807 path_sanitize( psz_filename );
810 free( val.psz_string );
811 free( format.psz_string );
813 /* Save the snapshot */
814 i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
815 if( i_ret != VLC_SUCCESS )
817 msg_Err( p_vout, "could not create snapshot %s", psz_filename );
818 free( psz_filename );
819 image_HandlerDelete( p_image );
824 msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
825 vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
826 "%s", psz_filename );
828 /* Generate a media player event - Right now just trigger a global libvlc var
829 CHECK: Could not find a more local object. The goal is to communicate
830 vout_thread with libvlc_media_player or its input_thread*/
831 val.psz_string = psz_filename ;
833 var_Set( p_vout->p_libvlc, "vout-snapshottaken", val );
834 /* var_Set duplicates data for transport so we can free*/
835 free( psz_filename );
838 if( var_GetBool( p_vout, "snapshot-preview" ) )
840 if( VoutSnapshotPip( p_vout, p_image, p_pic, &fmt_in ) )
841 msg_Warn( p_vout, "Failed to display snapshot" );
843 image_HandlerDelete( p_image );
848 /*****************************************************************************
850 *****************************************************************************/
852 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
853 bool b_add, bool b_setconfig )
856 char *psz_string = config_GetPsz( p_vout, "vout-filter" );
858 /* Todo : Use some generic chain manipulation functions */
859 if( !psz_string ) psz_string = strdup("");
861 psz_parser = strstr( psz_string, psz_name );
866 psz_parser = psz_string;
867 if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
868 psz_string, psz_name ) == -1 )
882 memmove( psz_parser, psz_parser + strlen(psz_name) +
883 (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
884 strlen(psz_parser + strlen(psz_name)) + 1 );
886 /* Remove trailing : : */
887 if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
889 *(psz_string+strlen(psz_string ) -1 ) = '\0';
899 config_PutPsz( p_vout, "vout-filter", psz_string );
901 var_SetString( p_vout, "vout-filter", psz_string );
905 /*****************************************************************************
906 * vout_ControlDefault: default methods for video output control.
907 *****************************************************************************/
908 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
914 p_vout->p->b_snapshot = true;
918 msg_Dbg( p_vout, "control query not supported" );
923 /*****************************************************************************
924 * InitWindowSize: find the initial dimensions the video window should have.
925 *****************************************************************************
926 * This function will check the "width", "height" and "zoom" config options and
927 * will calculate the size that the video window should have.
928 *****************************************************************************/
929 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
930 unsigned *pi_height )
933 int i_width, i_height;
936 #define FP_FACTOR 1000 /* our fixed point factor */
938 var_Get( p_vout, "width", &val );
940 var_Get( p_vout, "height", &val );
941 i_height = val.i_int;
942 var_Get( p_vout, "zoom", &val );
943 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
945 if( i_width > 0 && i_height > 0)
947 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
948 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
951 else if( i_width > 0 )
953 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
954 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
955 p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
956 FP_FACTOR / p_vout->fmt_in.i_visible_width );
959 else if( i_height > 0 )
961 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
962 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
963 p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
964 FP_FACTOR / p_vout->fmt_in.i_visible_height );
968 if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
969 msg_Warn( p_vout, "fucked up aspect" );
970 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
971 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
973 else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
975 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
976 p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
977 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
982 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
984 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
985 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
989 msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width,
990 p_vout->i_window_height );
995 /*****************************************************************************
996 * Object variables callbacks
997 *****************************************************************************/
998 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
999 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1001 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1002 (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
1003 InitWindowSize( p_vout, &p_vout->i_window_width,
1004 &p_vout->i_window_height );
1005 vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
1006 p_vout->i_window_height );
1010 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
1011 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1013 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1014 int64_t i_aspect_num, i_aspect_den;
1015 unsigned int i_width, i_height;
1017 (void)oldval; (void)p_data;
1019 /* Restore defaults */
1020 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
1021 p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
1022 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
1023 p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
1025 if( !strcmp( psz_cmd, "crop" ) )
1027 char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
1030 /* We're using the 3:4 syntax */
1031 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1032 if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
1034 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1035 if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
1037 i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
1038 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
1039 i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
1040 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
1042 if( i_width < p_vout->fmt_render.i_visible_width )
1044 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
1045 (p_vout->fmt_render.i_visible_width - i_width) / 2;
1046 p_vout->fmt_in.i_visible_width = i_width;
1050 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
1051 (p_vout->fmt_render.i_visible_height - i_height) / 2;
1052 p_vout->fmt_in.i_visible_height = i_height;
1057 psz_parser = strchr( newval.psz_string, 'x' );
1060 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
1061 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
1063 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
1064 if( psz_end != psz_parser ) goto crop_end;
1066 psz_parser = strchr( ++psz_end, '+' );
1067 i_crop_height = strtol( psz_end, &psz_end, 10 );
1068 if( psz_end != psz_parser ) goto crop_end;
1070 psz_parser = strchr( ++psz_end, '+' );
1071 i_crop_left = strtol( psz_end, &psz_end, 10 );
1072 if( psz_end != psz_parser ) goto crop_end;
1075 i_crop_top = strtol( psz_end, &psz_end, 10 );
1076 if( *psz_end != '\0' ) goto crop_end;
1078 i_width = i_crop_width;
1079 p_vout->fmt_in.i_visible_width = i_width;
1081 i_height = i_crop_height;
1082 p_vout->fmt_in.i_visible_height = i_height;
1084 p_vout->fmt_in.i_x_offset = i_crop_left;
1085 p_vout->fmt_in.i_y_offset = i_crop_top;
1089 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1090 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1092 psz_parser = strchr( newval.psz_string, '+' );
1093 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1094 if( psz_end != psz_parser ) goto crop_end;
1096 psz_parser = strchr( ++psz_end, '+' );
1097 i_crop_top = strtol( psz_end, &psz_end, 10 );
1098 if( psz_end != psz_parser ) goto crop_end;
1100 psz_parser = strchr( ++psz_end, '+' );
1101 i_crop_right = strtol( psz_end, &psz_end, 10 );
1102 if( psz_end != psz_parser ) goto crop_end;
1105 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1106 if( *psz_end != '\0' ) goto crop_end;
1108 i_width = p_vout->fmt_render.i_visible_width
1109 - i_crop_left - i_crop_right;
1110 p_vout->fmt_in.i_visible_width = i_width;
1112 i_height = p_vout->fmt_render.i_visible_height
1113 - i_crop_top - i_crop_bottom;
1114 p_vout->fmt_in.i_visible_height = i_height;
1116 p_vout->fmt_in.i_x_offset = i_crop_left;
1117 p_vout->fmt_in.i_y_offset = i_crop_top;
1121 else if( !strcmp( psz_cmd, "crop-top" )
1122 || !strcmp( psz_cmd, "crop-left" )
1123 || !strcmp( psz_cmd, "crop-bottom" )
1124 || !strcmp( psz_cmd, "crop-right" ) )
1126 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1128 i_crop_top = var_GetInteger( p_vout, "crop-top" );
1129 i_crop_left = var_GetInteger( p_vout, "crop-left" );
1130 i_crop_right = var_GetInteger( p_vout, "crop-right" );
1131 i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1133 i_width = p_vout->fmt_render.i_visible_width
1134 - i_crop_left - i_crop_right;
1135 p_vout->fmt_in.i_visible_width = i_width;
1137 i_height = p_vout->fmt_render.i_visible_height
1138 - i_crop_top - i_crop_bottom;
1139 p_vout->fmt_in.i_visible_height = i_height;
1141 p_vout->fmt_in.i_x_offset = i_crop_left;
1142 p_vout->fmt_in.i_y_offset = i_crop_top;
1146 InitWindowSize( p_vout, &p_vout->i_window_width,
1147 &p_vout->i_window_height );
1149 p_vout->i_changes |= VOUT_CROP_CHANGE;
1151 msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1152 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1153 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1154 p_vout->fmt_in.i_visible_width,
1155 p_vout->fmt_in.i_visible_height );
1157 var_SetVoid( p_vout, "crop-update" );
1162 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1163 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1165 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1166 unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1169 char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1170 (void)psz_cmd; (void)oldval; (void)p_data;
1172 /* Restore defaults */
1173 p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1174 p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1175 p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1176 p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1178 if( !psz_parser ) goto aspect_end;
1180 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1181 if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1183 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1184 if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1186 i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1187 i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1188 vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1189 p_vout->fmt_in.i_sar_num = i_sar_num;
1190 p_vout->fmt_in.i_sar_den = i_sar_den;
1191 p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1192 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1195 if( p_vout->p->i_par_num && p_vout->p->i_par_den )
1197 p_vout->fmt_in.i_sar_num *= p_vout->p->i_par_den;
1198 p_vout->fmt_in.i_sar_den *= p_vout->p->i_par_num;
1199 p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1200 p_vout->p->i_par_den / p_vout->p->i_par_num;
1201 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1204 p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1206 vlc_ureduce( &i_aspect_num, &i_aspect_den,
1207 p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1208 msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1209 i_aspect_num, i_aspect_den,
1210 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1212 if( var_Get( p_vout, "crop", &val ) )
1213 return VLC_EGENERIC;
1215 int i_ret = CropCallback( p_this, "crop", val, val, 0 );
1216 free( val.psz_string );
1220 static int ScalingCallback( vlc_object_t *p_this, char const *psz_cmd,
1221 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1223 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1224 (void)oldval; (void)newval; (void)p_data;
1226 vlc_mutex_lock( &p_vout->change_lock );
1228 if( !strcmp( psz_cmd, "autoscale" ) )
1230 p_vout->i_changes |= VOUT_SCALE_CHANGE;
1232 else if( !strcmp( psz_cmd, "scale" ) )
1234 p_vout->i_changes |= VOUT_ZOOM_CHANGE;
1237 vlc_mutex_unlock( &p_vout->change_lock );
1242 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1243 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1245 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1246 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
1247 (void)psz_cmd; (void)oldval; (void)p_data;
1249 /* Modify libvlc as well because the vout might have to be restarted */
1250 var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
1251 var_Set( p_vout->p_libvlc, "video-on-top", newval );
1256 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1257 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1259 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1261 (void)psz_cmd; (void)oldval; (void)p_data;
1263 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1265 /* Modify libvlc as well because the vout might have to be restarted */
1266 var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
1267 var_Set( p_vout->p_libvlc, "fullscreen", newval );
1270 var_Set( p_vout, "intf-change", val );
1274 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1275 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1277 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1278 VLC_UNUSED(newval); VLC_UNUSED(p_data);
1279 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1280 vout_Control( p_vout, VOUT_SNAPSHOT );
1284 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1285 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1287 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1289 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1290 p_vout->p->b_title_show = newval.b_bool;
1294 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1295 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1297 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1298 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1299 p_vout->p->i_title_timeout = (mtime_t) newval.i_int;
1303 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1304 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1306 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1308 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1309 p_vout->p->i_title_position = newval.i_int;