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() */
40 #include <vlc_interface.h>
41 #include <vlc_block.h>
42 #include <vlc_playlist.h>
45 #include <vlc_window.h>
46 #include <vlc_image.h>
48 #include <vlc_charset.h>
50 #include <vlc_strings.h>
51 #include <vlc_charset.h>
52 #include "../libvlc.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
59 /* Object variables callbacks */
60 static int ZoomCallback( vlc_object_t *, char const *,
61 vlc_value_t, vlc_value_t, void * );
62 static int CropCallback( vlc_object_t *, char const *,
63 vlc_value_t, vlc_value_t, void * );
64 static int AspectCallback( vlc_object_t *, char const *,
65 vlc_value_t, vlc_value_t, void * );
66 static int OnTopCallback( vlc_object_t *, char const *,
67 vlc_value_t, vlc_value_t, void * );
68 static int FullscreenCallback( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
70 static int SnapshotCallback( vlc_object_t *, char const *,
71 vlc_value_t, vlc_value_t, void * );
73 static int TitleShowCallback( vlc_object_t *, char const *,
74 vlc_value_t, vlc_value_t, void * );
75 static int TitleTimeoutCallback( vlc_object_t *, char const *,
76 vlc_value_t, vlc_value_t, void * );
77 static int TitlePositionCallback( vlc_object_t *, char const *,
78 vlc_value_t, vlc_value_t, void * );
80 /*****************************************************************************
81 * vout_RequestWindow: Create/Get a video window if possible.
82 *****************************************************************************
83 * This function looks for the main interface and tries to request
84 * a new video window. If it fails then the vout will still need to create the
86 *****************************************************************************/
87 void *vout_RequestWindow( vout_thread_t *p_vout,
88 int *pi_x_hint, int *pi_y_hint,
89 unsigned int *pi_width_hint,
90 unsigned int *pi_height_hint )
93 if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
95 /* Get requested coordinates */
96 *pi_x_hint = var_GetInteger( p_vout, "video-x" );
97 *pi_y_hint = var_GetInteger( p_vout, "video-y" );
99 *pi_width_hint = p_vout->i_window_width;
100 *pi_height_hint = p_vout->i_window_height;
102 /* Check whether someone provided us with a window ID */
103 int drawable = var_CreateGetInteger( p_vout, "drawable" );
104 if( drawable ) return (void *)(intptr_t)drawable;
106 vout_window_t *wnd = vlc_custom_create (VLC_OBJECT(p_vout), sizeof (*wnd),
107 VLC_OBJECT_GENERIC, "window");
112 wnd->width = *pi_width_hint;
113 wnd->height = *pi_height_hint;
114 wnd->pos_x = *pi_x_hint;
115 wnd->pos_y = *pi_y_hint;
116 vlc_object_attach (wnd, p_vout);
118 wnd->module = module_Need (wnd, "vout window", 0, 0);
119 if (wnd->module == NULL)
121 msg_Dbg (wnd, "no window provider available");
122 vlc_object_release (wnd);
125 p_vout->p_window = wnd;
126 *pi_width_hint = wnd->width;
127 *pi_height_hint = wnd->height;
128 *pi_x_hint = wnd->pos_x;
129 *pi_y_hint = wnd->pos_y;
133 void vout_ReleaseWindow( vout_thread_t *p_vout, void *dummy )
135 vout_window_t *wnd = p_vout->p_window;
139 p_vout->p_window = NULL;
141 assert (wnd->module);
142 module_Unneed (wnd, wnd->module);
144 vlc_object_release (wnd);
148 int vout_ControlWindow( vout_thread_t *p_vout, void *dummy,
149 int i_query, va_list args )
152 vout_window_t *wnd = p_vout->p_window;
157 assert (wnd->control);
158 return wnd->control (wnd, i_query, args);
161 /*****************************************************************************
162 * vout_IntfInit: called during the vout creation to initialise misc things.
163 *****************************************************************************/
167 const char *psz_label;
168 } p_zoom_values[] = {
169 { 0.25, N_("1:4 Quarter") },
170 { 0.5, N_("1:2 Half") },
171 { 1, N_("1:1 Original") },
172 { 2, N_("2:1 Double") },
177 const char *psz_value;
178 const char *psz_label;
179 } p_crop_values[] = {
180 { "", N_("Default") },
181 { "16:10", "16:10" },
183 { "185:100", "1.85:1" },
184 { "221:100", "2.21:1" },
185 { "235:100", "2.35:1" },
186 { "239:100", "2.39:1" },
195 const char *psz_value;
196 const char *psz_label;
197 } p_aspect_ratio_values[] = {
198 { "", N_("Default") },
202 { "16:10", "16:10" },
203 { "221:100", "2.21:1" },
207 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
210 if( psz_list && *psz_list )
212 char *psz_cur = psz_list;
214 while( psz_cur && *psz_cur )
216 vlc_value_t val, text;
217 psz_next = strchr( psz_cur, ',' );
223 val.psz_string = psz_cur;
224 text.psz_string = psz_cur;
225 var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
231 void vout_IntfInit( vout_thread_t *p_vout )
233 vlc_value_t val, text, old_val;
234 bool b_force_par = false;
238 /* Create a few object variables we'll need later on */
239 var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
240 var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
241 var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
242 var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
243 var_Create( p_vout, "snapshot-sequential",
244 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
245 var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
246 var_SetInteger( p_vout, "snapshot-num", 1 );
247 var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
248 var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
250 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
251 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
252 p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
254 var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
255 var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
257 var_Create( p_vout, "mouse-hide-timeout",
258 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
260 p_vout->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
261 p_vout->i_title_timeout =
262 (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
263 p_vout->i_title_position =
264 var_CreateGetInteger( p_vout, "video-title-position" );
266 var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
267 var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
268 var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
270 /* Zoom object var */
271 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
272 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
274 text.psz_string = _("Zoom");
275 var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
277 var_Get( p_vout, "zoom", &old_val );
279 for( i = 0; p_zoom_values[i].f_value; i++ )
281 if( old_val.f_float == p_zoom_values[i].f_value )
282 var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
283 val.f_float = p_zoom_values[i].f_value;
284 text.psz_string = _( p_zoom_values[i].psz_label );
285 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
288 var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
290 var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
292 /* Crop offset vars */
293 var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
294 var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
295 var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
296 var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
298 var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
299 var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
300 var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
301 var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
303 /* Crop object var */
304 var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
305 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
307 text.psz_string = _("Crop");
308 var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
310 val.psz_string = (char*)"";
311 var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
313 for( i = 0; p_crop_values[i].psz_value; i++ )
315 val.psz_string = (char*)p_crop_values[i].psz_value;
316 text.psz_string = _( p_crop_values[i].psz_label );
317 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
320 /* update triggered every time the vout's crop parameters are changed */
321 var_Create( p_vout, "crop-update", VLC_VAR_VOID );
323 /* Add custom crop ratios */
324 psz_buf = config_GetPsz( p_vout, "custom-crop-ratios" );
325 AddCustomRatios( p_vout, "crop", psz_buf );
328 var_AddCallback( p_vout, "crop", CropCallback, NULL );
329 var_Get( p_vout, "crop", &old_val );
330 if( old_val.psz_string && *old_val.psz_string )
331 var_Change( p_vout, "crop", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
332 free( old_val.psz_string );
334 /* Monitor pixel aspect-ratio */
335 var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
336 var_Get( p_vout, "monitor-par", &val );
337 if( val.psz_string && *val.psz_string )
339 char *psz_parser = strchr( val.psz_string, ':' );
340 unsigned int i_aspect_num = 0, i_aspect_den = 0;
344 i_aspect_num = strtol( val.psz_string, 0, 10 );
345 i_aspect_den = strtol( ++psz_parser, 0, 10 );
349 i_aspect = atof( val.psz_string );
350 vlc_ureduce( &i_aspect_num, &i_aspect_den,
351 i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
353 if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
355 p_vout->i_par_num = i_aspect_num;
356 p_vout->i_par_den = i_aspect_den;
358 vlc_ureduce( &p_vout->i_par_num, &p_vout->i_par_den,
359 p_vout->i_par_num, p_vout->i_par_den, 0 );
361 msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
362 p_vout->i_par_num, p_vout->i_par_den );
365 free( val.psz_string );
367 /* Aspect-ratio object var */
368 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
369 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
371 text.psz_string = _("Aspect-ratio");
372 var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
374 val.psz_string = (char*)"";
375 var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
377 for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
379 val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
380 text.psz_string = _( p_aspect_ratio_values[i].psz_label );
381 var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
384 /* Add custom aspect ratios */
385 psz_buf = config_GetPsz( p_vout, "custom-aspect-ratios" );
386 AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
389 var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
390 var_Get( p_vout, "aspect-ratio", &old_val );
391 if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
392 var_Change( p_vout, "aspect-ratio", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
393 free( old_val.psz_string );
395 /* Initialize the dimensions of the video window */
396 InitWindowSize( p_vout, &p_vout->i_window_width,
397 &p_vout->i_window_height );
399 /* Add a variable to indicate if the window should be on top of others */
400 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
401 | VLC_VAR_ISCOMMAND );
402 text.psz_string = _("Always on top");
403 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
404 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
406 /* Add a variable to indicate whether we want window decoration or not */
407 var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
409 /* Add a fullscreen variable */
410 if( var_CreateGetBoolCommand( p_vout, "fullscreen" ) )
412 /* user requested fullscreen */
413 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
415 text.psz_string = _("Fullscreen");
416 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
417 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
419 /* Add a snapshot variable */
420 var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
421 text.psz_string = _("Snapshot");
422 var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
423 var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
425 /* Mouse coordinates */
426 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
427 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
428 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
429 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
430 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
432 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
433 var_SetBool( p_vout, "intf-change", true );
436 /*****************************************************************************
437 * vout_Snapshot: generates a snapshot.
438 *****************************************************************************/
440 * This function will inject a subpicture into the vout with the provided
443 static int VoutSnapshotPip( vout_thread_t *p_vout, image_handler_t *p_image, picture_t *p_pic, const video_format_t *p_fmt_in )
445 video_format_t fmt_in = *p_fmt_in;
446 video_format_t fmt_out;
448 subpicture_t *p_subpic;
451 memset( &fmt_out, 0, sizeof(fmt_out) );
453 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
456 p_pip = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
460 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
461 if( p_subpic == NULL )
463 picture_Release( p_pip );
467 p_subpic->i_channel = 0;
468 p_subpic->i_start = mdate();
469 p_subpic->i_stop = mdate() + 4000000;
470 p_subpic->b_ephemer = true;
471 p_subpic->b_fade = true;
472 p_subpic->i_original_picture_width = fmt_out.i_width * 4;
473 p_subpic->i_original_picture_height = fmt_out.i_height * 4;
474 fmt_out.i_aspect = 0;
476 fmt_out.i_sar_den = 0;
478 p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
479 if( p_subpic->p_region )
480 vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pip );
481 picture_Release( p_pip );
483 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
487 * This function will return the default directory used for snapshots
489 static char *VoutSnapshotGetDefaultDirectory( void )
492 #if defined(__APPLE__) || defined(SYS_BEOS)
494 if( asprintf( &psz_path, "%s/Desktop",
495 config_GetHomeDir() ) == -1 )
498 #elif defined(WIN32) && !defined(UNDER_CE)
500 /* Get the My Pictures folder path */
501 char *p_mypicturesdir = NULL;
502 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
504 #ifndef CSIDL_FLAG_CREATE
505 # define CSIDL_FLAG_CREATE 0x8000
507 #ifndef CSIDL_MYPICTURES
508 # define CSIDL_MYPICTURES 0x27
510 #ifndef SHGFP_TYPE_CURRENT
511 # define SHGFP_TYPE_CURRENT 0
514 HINSTANCE shfolder_dll;
515 SHGETFOLDERPATH SHGetFolderPath ;
517 /* load the shfolder dll to retrieve SHGetFolderPath */
518 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
520 wchar_t wdir[PATH_MAX];
521 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
522 _T("SHGetFolderPathW") );
523 if ((SHGetFolderPath != NULL )
524 && SUCCEEDED (SHGetFolderPath (NULL,
525 CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
526 NULL, SHGFP_TYPE_CURRENT,
528 p_mypicturesdir = FromWide (wdir);
530 FreeLibrary( shfolder_dll );
533 if( p_mypicturesdir == NULL )
534 psz_path = strdup( config_GetHomeDir() );
536 psz_path = p_mypicturesdir;
540 /* XXX: This saves in the data directory. Shouldn't we try saving
541 * to psz_homedir/Desktop or something nicer ? */
542 char *psz_datadir = config_GetUserDataDir();
545 if( asprintf( &psz_path, "%s", psz_datadir ) == -1 )
555 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
557 image_handler_t *p_image = image_HandlerCreate( p_vout );
558 video_format_t fmt_in, fmt_out;
559 char *psz_filename = NULL;
560 vlc_value_t val, format;
563 bool b_embedded_snapshot;
564 uintmax_t i_id = (uintptr_t)NULL;
567 val.psz_string = var_GetNonEmptyString( p_vout, "snapshot-path" );
569 /* Embedded snapshot : if snapshot-path == object:object_ptr */
570 if( val.psz_string && sscanf( val.psz_string, "object:%ju", &i_id ) > 0 )
571 b_embedded_snapshot = true;
573 b_embedded_snapshot = false;
576 memset( &fmt_in, 0, sizeof(video_format_t) );
577 fmt_in = p_vout->fmt_out;
578 if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
581 fmt_in.i_sar_den = 1;
585 memset( &fmt_out, 0, sizeof(video_format_t) );
587 fmt_out.i_sar_den = 1;
588 fmt_out.i_chroma = b_embedded_snapshot ? VLC_FOURCC('p','n','g',' ') : 0;
589 fmt_out.i_width = var_GetInteger( p_vout, "snapshot-width" );
590 fmt_out.i_height = var_GetInteger( p_vout, "snapshot-height" );
592 if( b_embedded_snapshot &&
593 fmt_out.i_width == 0 && fmt_out.i_height == 0 )
595 /* If snapshot-width and/or snapshot height were not specified,
596 use a default snapshot width of 320 */
597 fmt_out.i_width = 320;
600 if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
602 fmt_out.i_height = fmt_in.i_height * fmt_out.i_width / fmt_in.i_width;
603 const int i_height = fmt_out.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
605 fmt_out.i_height = i_height;
609 if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
611 fmt_out.i_width = fmt_in.i_width * fmt_out.i_height / fmt_in.i_height;
615 fmt_out.i_width = fmt_in.i_width;
616 fmt_out.i_height = fmt_in.i_height;
618 const int i_width = fmt_out.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
620 fmt_out.i_width = i_width;
624 create a snapshot_t* and store it in
625 object_ptr->p_private, then unlock and signal the
628 if( b_embedded_snapshot )
630 vlc_object_t* p_dest = (vlc_object_t *)(uintptr_t)i_id;
632 snapshot_t *p_snapshot;
635 /* Object must be locked. We will unlock it once we get the
636 snapshot and written it to p_private */
637 p_dest->p_private = NULL;
639 /* Save the snapshot to a memory zone */
640 p_block = image_Write( p_image, p_pic, &fmt_in, &fmt_out );
643 msg_Err( p_vout, "Could not get snapshot" );
644 image_HandlerDelete( p_image );
645 vlc_object_signal_unlocked( p_dest );
646 vlc_object_release( p_dest );
650 /* Copy the p_block data to a snapshot structure */
651 /* FIXME: get the timestamp */
652 p_snapshot = malloc( sizeof( snapshot_t ) );
655 block_Release( p_block );
656 image_HandlerDelete( p_image );
657 vlc_object_signal_unlocked( p_dest );
658 vlc_object_release( p_dest );
662 i_size = p_block->i_buffer;
664 p_snapshot->i_width = fmt_out.i_width;
665 p_snapshot->i_height = fmt_out.i_height;
666 p_snapshot->i_datasize = i_size;
667 p_snapshot->date = p_block->i_pts; /* FIXME ?? */
668 p_snapshot->p_data = malloc( i_size );
669 if( !p_snapshot->p_data )
671 block_Release( p_block );
673 image_HandlerDelete( p_image );
674 vlc_object_signal_unlocked( p_dest );
675 vlc_object_release( p_dest );
678 memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
680 p_dest->p_private = p_snapshot;
682 block_Release( p_block );
684 /* Unlock the object */
685 vlc_object_signal_unlocked( p_dest );
686 vlc_object_release( p_dest );
688 image_HandlerDelete( p_image );
692 /* Get default directory if none provided */
693 if( !val.psz_string )
694 val.psz_string = VoutSnapshotGetDefaultDirectory( );
695 if( !val.psz_string )
697 msg_Err( p_vout, "no path specified for snapshots" );
698 image_HandlerDelete( p_image );
702 /* Get snapshot format, default being "png" */
703 format.psz_string = var_GetNonEmptyString( p_vout, "snapshot-format" );
704 if( !format.psz_string )
705 format.psz_string = strdup( "png" );
706 if( !format.psz_string )
708 free( val.psz_string );
709 image_HandlerDelete( p_image );
714 * Did the user specify a directory? If not, path = NULL.
716 path = utf8_opendir ( (const char *)val.psz_string );
719 char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
720 if( psz_prefix == NULL )
721 psz_prefix = strdup( "vlcsnap-" );
724 char *psz_tmp = str_format( p_vout, psz_prefix );
725 filename_sanitize( psz_tmp );
727 psz_prefix = psz_tmp;
731 if( var_GetBool( p_vout, "snapshot-sequential" ) == true )
733 int i_num = var_GetInteger( p_vout, "snapshot-num" );
738 free( psz_filename );
739 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
740 val.psz_string, psz_prefix, i_num++,
741 format.psz_string ) == -1 )
743 msg_Err( p_vout, "could not create snapshot" );
744 image_HandlerDelete( p_image );
748 while( utf8_stat( psz_filename, &st ) == 0 );
750 var_SetInteger( p_vout, "snapshot-num", i_num );
754 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
755 val.psz_string, psz_prefix,
756 (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
757 format.psz_string ) == -1 )
759 msg_Err( p_vout, "could not create snapshot" );
760 image_HandlerDelete( p_image );
767 else // The user specified a full path name (including file name)
769 psz_filename = str_format( p_vout, val.psz_string );
770 path_sanitize( psz_filename );
773 free( val.psz_string );
774 free( format.psz_string );
776 /* Save the snapshot */
777 i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
778 if( i_ret != VLC_SUCCESS )
780 msg_Err( p_vout, "could not create snapshot %s", psz_filename );
781 free( psz_filename );
782 image_HandlerDelete( p_image );
787 msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
788 vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
789 "%s", psz_filename );
790 free( psz_filename );
793 if( var_GetBool( p_vout, "snapshot-preview" ) )
795 if( VoutSnapshotPip( p_vout, p_image, p_pic, &fmt_in ) )
796 msg_Warn( p_vout, "Failed to display snapshot" );
798 image_HandlerDelete( p_image );
803 /*****************************************************************************
805 *****************************************************************************/
807 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
808 bool b_add, bool b_setconfig )
811 char *psz_string = config_GetPsz( p_vout, "vout-filter" );
813 /* Todo : Use some generic chain manipulation functions */
814 if( !psz_string ) psz_string = strdup("");
816 psz_parser = strstr( psz_string, psz_name );
821 psz_parser = psz_string;
822 if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
823 psz_string, psz_name ) == -1 )
837 memmove( psz_parser, psz_parser + strlen(psz_name) +
838 (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
839 strlen(psz_parser + strlen(psz_name)) + 1 );
841 /* Remove trailing : : */
842 if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
844 *(psz_string+strlen(psz_string ) -1 ) = '\0';
854 config_PutPsz( p_vout, "vout-filter", psz_string );
856 var_SetString( p_vout, "vout-filter", psz_string );
860 /*****************************************************************************
861 * vout_ControlDefault: default methods for video output control.
862 *****************************************************************************/
863 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
870 vout_ReleaseWindow( p_vout, NULL );
874 p_vout->b_snapshot = true;
878 msg_Dbg( p_vout, "control query not supported" );
883 /*****************************************************************************
884 * InitWindowSize: find the initial dimensions the video window should have.
885 *****************************************************************************
886 * This function will check the "width", "height" and "zoom" config options and
887 * will calculate the size that the video window should have.
888 *****************************************************************************/
889 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
890 unsigned *pi_height )
893 int i_width, i_height;
896 #define FP_FACTOR 1000 /* our fixed point factor */
898 var_Get( p_vout, "width", &val );
900 var_Get( p_vout, "height", &val );
901 i_height = val.i_int;
902 var_Get( p_vout, "zoom", &val );
903 ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
905 if( i_width > 0 && i_height > 0)
907 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
908 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
911 else if( i_width > 0 )
913 *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
914 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
915 p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
916 FP_FACTOR / p_vout->fmt_in.i_visible_width );
919 else if( i_height > 0 )
921 *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
922 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
923 p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
924 FP_FACTOR / p_vout->fmt_in.i_visible_height );
928 if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
929 msg_Warn( p_vout, "fucked up aspect" );
930 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
931 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
933 else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
935 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
936 p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
937 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
942 *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
944 *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
945 p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
949 msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width,
950 p_vout->i_window_height );
955 /*****************************************************************************
956 * Object variables callbacks
957 *****************************************************************************/
958 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
959 vlc_value_t oldval, vlc_value_t newval, void *p_data )
961 vout_thread_t *p_vout = (vout_thread_t *)p_this;
962 (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
963 InitWindowSize( p_vout, &p_vout->i_window_width,
964 &p_vout->i_window_height );
965 vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
966 p_vout->i_window_height );
970 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
971 vlc_value_t oldval, vlc_value_t newval, void *p_data )
973 vout_thread_t *p_vout = (vout_thread_t *)p_this;
974 int64_t i_aspect_num, i_aspect_den;
975 unsigned int i_width, i_height;
977 (void)oldval; (void)p_data;
979 /* Restore defaults */
980 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
981 p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
982 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
983 p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
985 if( !strcmp( psz_cmd, "crop" ) )
987 char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
990 /* We're using the 3:4 syntax */
991 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
992 if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
994 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
995 if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
997 i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
998 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
999 i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
1000 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
1002 if( i_width < p_vout->fmt_render.i_visible_width )
1004 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
1005 (p_vout->fmt_render.i_visible_width - i_width) / 2;
1006 p_vout->fmt_in.i_visible_width = i_width;
1010 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
1011 (p_vout->fmt_render.i_visible_height - i_height) / 2;
1012 p_vout->fmt_in.i_visible_height = i_height;
1017 psz_parser = strchr( newval.psz_string, 'x' );
1020 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
1021 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
1023 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
1024 if( psz_end != psz_parser ) goto crop_end;
1026 psz_parser = strchr( ++psz_end, '+' );
1027 i_crop_height = strtol( psz_end, &psz_end, 10 );
1028 if( psz_end != psz_parser ) goto crop_end;
1030 psz_parser = strchr( ++psz_end, '+' );
1031 i_crop_left = strtol( psz_end, &psz_end, 10 );
1032 if( psz_end != psz_parser ) goto crop_end;
1035 i_crop_top = strtol( psz_end, &psz_end, 10 );
1036 if( *psz_end != '\0' ) goto crop_end;
1038 i_width = i_crop_width;
1039 p_vout->fmt_in.i_visible_width = i_width;
1041 i_height = i_crop_height;
1042 p_vout->fmt_in.i_visible_height = i_height;
1044 p_vout->fmt_in.i_x_offset = i_crop_left;
1045 p_vout->fmt_in.i_y_offset = i_crop_top;
1049 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1050 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1052 psz_parser = strchr( newval.psz_string, '+' );
1053 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1054 if( psz_end != psz_parser ) goto crop_end;
1056 psz_parser = strchr( ++psz_end, '+' );
1057 i_crop_top = strtol( psz_end, &psz_end, 10 );
1058 if( psz_end != psz_parser ) goto crop_end;
1060 psz_parser = strchr( ++psz_end, '+' );
1061 i_crop_right = strtol( psz_end, &psz_end, 10 );
1062 if( psz_end != psz_parser ) goto crop_end;
1065 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1066 if( *psz_end != '\0' ) goto crop_end;
1068 i_width = p_vout->fmt_render.i_visible_width
1069 - i_crop_left - i_crop_right;
1070 p_vout->fmt_in.i_visible_width = i_width;
1072 i_height = p_vout->fmt_render.i_visible_height
1073 - i_crop_top - i_crop_bottom;
1074 p_vout->fmt_in.i_visible_height = i_height;
1076 p_vout->fmt_in.i_x_offset = i_crop_left;
1077 p_vout->fmt_in.i_y_offset = i_crop_top;
1081 else if( !strcmp( psz_cmd, "crop-top" )
1082 || !strcmp( psz_cmd, "crop-left" )
1083 || !strcmp( psz_cmd, "crop-bottom" )
1084 || !strcmp( psz_cmd, "crop-right" ) )
1086 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1088 i_crop_top = var_GetInteger( p_vout, "crop-top" );
1089 i_crop_left = var_GetInteger( p_vout, "crop-left" );
1090 i_crop_right = var_GetInteger( p_vout, "crop-right" );
1091 i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1093 i_width = p_vout->fmt_render.i_visible_width
1094 - i_crop_left - i_crop_right;
1095 p_vout->fmt_in.i_visible_width = i_width;
1097 i_height = p_vout->fmt_render.i_visible_height
1098 - i_crop_top - i_crop_bottom;
1099 p_vout->fmt_in.i_visible_height = i_height;
1101 p_vout->fmt_in.i_x_offset = i_crop_left;
1102 p_vout->fmt_in.i_y_offset = i_crop_top;
1106 InitWindowSize( p_vout, &p_vout->i_window_width,
1107 &p_vout->i_window_height );
1109 p_vout->i_changes |= VOUT_CROP_CHANGE;
1111 msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1112 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1113 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1114 p_vout->fmt_in.i_visible_width,
1115 p_vout->fmt_in.i_visible_height );
1117 var_SetVoid( p_vout, "crop-update" );
1122 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1123 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1125 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1126 unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1129 char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1130 (void)psz_cmd; (void)oldval; (void)p_data;
1132 /* Restore defaults */
1133 p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1134 p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1135 p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1136 p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1138 if( !psz_parser ) goto aspect_end;
1140 i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1141 if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1143 i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1144 if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1146 i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1147 i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1148 vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1149 p_vout->fmt_in.i_sar_num = i_sar_num;
1150 p_vout->fmt_in.i_sar_den = i_sar_den;
1151 p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1152 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1155 if( p_vout->i_par_num && p_vout->i_par_den )
1157 p_vout->fmt_in.i_sar_num *= p_vout->i_par_den;
1158 p_vout->fmt_in.i_sar_den *= p_vout->i_par_num;
1159 p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1160 p_vout->i_par_den / p_vout->i_par_num;
1161 p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1164 p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1166 vlc_ureduce( &i_aspect_num, &i_aspect_den,
1167 p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1168 msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1169 i_aspect_num, i_aspect_den,
1170 p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1172 if( var_Get( p_vout, "crop", &val ) )
1173 return VLC_EGENERIC;
1175 int i_ret = CropCallback( p_this, "crop", val, val, 0 );
1176 free( val.psz_string );
1180 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1181 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1183 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1184 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
1185 (void)psz_cmd; (void)oldval; (void)p_data;
1187 /* Modify libvlc as well because the vout might have to be restarted */
1188 var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
1189 var_Set( p_vout->p_libvlc, "video-on-top", newval );
1194 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1195 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1197 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1199 (void)psz_cmd; (void)oldval; (void)p_data;
1201 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1203 /* Modify libvlc as well because the vout might have to be restarted */
1204 var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
1205 var_Set( p_vout->p_libvlc, "fullscreen", newval );
1208 var_Set( p_vout, "intf-change", val );
1212 static int SnapshotCallback( 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);
1216 VLC_UNUSED(newval); VLC_UNUSED(p_data);
1217 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1218 vout_Control( p_vout, VOUT_SNAPSHOT );
1222 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1223 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1225 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1227 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1228 p_vout->b_title_show = newval.b_bool;
1232 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1233 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1235 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1236 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1237 p_vout->i_title_timeout = (mtime_t) newval.i_int;
1241 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1242 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1244 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1246 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1247 p_vout->i_title_position = newval.i_int;