]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
Remove useless <sys/stat.h> includes
[vlc] / src / video_output / vout_intf.c
1 /*****************************************************************************
2  * vout_intf.c : video output interface
3  *****************************************************************************
4  * Copyright (C) 2000-2007 the VideoLAN team
5  *
6  * Authors: Gildas Bazin <gbazin@videolan.org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>                                                /* free() */
35 #include <sys/types.h>                                          /* opendir() */
36 #include <dirent.h>                                             /* opendir() */
37 #include <assert.h>
38 #include <time.h>                                           /* strftime */
39
40 #include <vlc_interface.h>
41 #include <vlc_block.h>
42 #include <vlc_playlist.h>
43
44 #include <vlc_vout.h>
45 #include <vlc_image.h>
46 #include <vlc_osd.h>
47 #include <vlc_charset.h>
48
49 #include <vlc_strings.h>
50 #include <vlc_charset.h>
51 #include "../libvlc.h"
52 #include "vout_internal.h"
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
58
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 ScalingCallback( vlc_object_t *, char const *,
67                             vlc_value_t, vlc_value_t, void * );
68 static int OnTopCallback( vlc_object_t *, char const *,
69                           vlc_value_t, vlc_value_t, void * );
70 static int FullscreenCallback( vlc_object_t *, char const *,
71                                vlc_value_t, vlc_value_t, void * );
72 static int SnapshotCallback( vlc_object_t *, char const *,
73                              vlc_value_t, vlc_value_t, void * );
74
75 static int TitleShowCallback( vlc_object_t *, char const *,
76                               vlc_value_t, vlc_value_t, void * );
77 static int TitleTimeoutCallback( vlc_object_t *, char const *,
78                                  vlc_value_t, vlc_value_t, void * );
79 static int TitlePositionCallback( vlc_object_t *, char const *,
80                                   vlc_value_t, vlc_value_t, void * );
81
82 /*****************************************************************************
83  * vout_IntfInit: called during the vout creation to initialise misc things.
84  *****************************************************************************/
85 static const struct
86 {
87     double f_value;
88     const char *psz_label;
89 } p_zoom_values[] = {
90     { 0.25, N_("1:4 Quarter") },
91     { 0.5, N_("1:2 Half") },
92     { 1, N_("1:1 Original") },
93     { 2, N_("2:1 Double") },
94     { 0, NULL } };
95
96 static const struct
97 {
98     const char *psz_value;
99     const char *psz_label;
100 } p_crop_values[] = {
101     { "", N_("Default") },
102     { "16:10", "16:10" },
103     { "16:9", "16:9" },
104     { "185:100", "1.85:1" },
105     { "221:100", "2.21:1" },
106     { "235:100", "2.35:1" },
107     { "239:100", "2.39:1" },
108     { "5:3", "5:3" },
109     { "4:3", "4:3" },
110     { "5:4", "5:4" },
111     { "1:1", "1:1" },
112     { NULL, NULL } };
113
114 static const struct
115 {
116     const char *psz_value;
117     const char *psz_label;
118 } p_aspect_ratio_values[] = {
119     { "", N_("Default") },
120     { "1:1", "1:1" },
121     { "4:3", "4:3" },
122     { "16:9", "16:9" },
123     { "16:10", "16:10" },
124     { "221:100", "2.21:1" },
125     { "5:4", "5:4" },
126     { NULL, NULL } };
127
128 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
129                              char *psz_list )
130 {
131     assert( psz_list );
132
133     char *psz_cur = psz_list;
134     char *psz_next;
135     while( psz_cur && *psz_cur )
136     {
137         vlc_value_t val, text;
138         psz_next = strchr( psz_cur, ',' );
139         if( psz_next )
140         {
141             *psz_next = '\0';
142             psz_next++;
143         }
144         val.psz_string = psz_cur;
145         text.psz_string = psz_cur;
146         var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
147         psz_cur = psz_next;
148     }
149 }
150
151 void vout_IntfInit( vout_thread_t *p_vout )
152 {
153     vlc_value_t val, text, old_val;
154     bool b_force_par = false;
155     char *psz_buf;
156     int i;
157
158     /* Create a few object variables we'll need later on */
159     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
160     var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
161     var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
162     var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
163     var_Create( p_vout, "snapshot-sequential",
164                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
165     var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
166     var_SetInteger( p_vout, "snapshot-num", 1 );
167     var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
168     var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
169
170     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
171     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
172     p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
173
174     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
175     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
176
177     var_Create( p_vout, "mouse-hide-timeout",
178                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179
180     p_vout->p->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
181     p_vout->p->i_title_timeout =
182         (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
183     p_vout->p->i_title_position =
184         var_CreateGetInteger( p_vout, "video-title-position" );
185     p_vout->p->psz_title =  NULL;
186
187     var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
188     var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
189     var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
190
191     /* Zoom object var */
192     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
193                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
194
195     text.psz_string = _("Zoom");
196     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
197
198     var_Get( p_vout, "zoom", &old_val );
199
200     for( i = 0; p_zoom_values[i].f_value; i++ )
201     {
202         if( old_val.f_float == p_zoom_values[i].f_value )
203             var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
204         val.f_float = p_zoom_values[i].f_value;
205         text.psz_string = _( p_zoom_values[i].psz_label );
206         var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
207     }
208
209     var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
210
211     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
212
213     /* Crop offset vars */
214     var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
215     var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
216     var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
217     var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
218
219     var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
220     var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
221     var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
222     var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
223
224     /* Crop object var */
225     var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
226                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
227
228     text.psz_string = _("Crop");
229     var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
230
231     val.psz_string = (char*)"";
232     var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
233
234     for( i = 0; p_crop_values[i].psz_value; i++ )
235     {
236         val.psz_string = (char*)p_crop_values[i].psz_value;
237         text.psz_string = _( p_crop_values[i].psz_label );
238         var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
239     }
240
241     /* update triggered every time the vout's crop parameters are changed */
242     var_Create( p_vout, "crop-update", VLC_VAR_VOID );
243
244     /* Add custom crop ratios */
245     psz_buf = var_CreateGetNonEmptyString( p_vout, "custom-crop-ratios" );
246     if( psz_buf )
247     {
248         AddCustomRatios( p_vout, "crop", psz_buf );
249         free( psz_buf );
250     }
251
252     var_AddCallback( p_vout, "crop", CropCallback, NULL );
253     var_Get( p_vout, "crop", &old_val );
254     if( old_val.psz_string && *old_val.psz_string )
255         var_TriggerCallback( p_vout, "crop" );
256     free( old_val.psz_string );
257
258     /* Monitor pixel aspect-ratio */
259     var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
260     var_Get( p_vout, "monitor-par", &val );
261     if( val.psz_string && *val.psz_string )
262     {
263         char *psz_parser = strchr( val.psz_string, ':' );
264         unsigned int i_aspect_num = 0, i_aspect_den = 0;
265         float i_aspect = 0;
266         if( psz_parser )
267         {
268             i_aspect_num = strtol( val.psz_string, 0, 10 );
269             i_aspect_den = strtol( ++psz_parser, 0, 10 );
270         }
271         else
272         {
273             i_aspect = us_atof( val.psz_string );
274             vlc_ureduce( &i_aspect_num, &i_aspect_den,
275                          i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
276         }
277         if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
278
279         p_vout->p->i_par_num = i_aspect_num;
280         p_vout->p->i_par_den = i_aspect_den;
281
282         vlc_ureduce( &p_vout->p->i_par_num, &p_vout->p->i_par_den,
283                      p_vout->p->i_par_num, p_vout->p->i_par_den, 0 );
284
285         msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
286                  p_vout->p->i_par_num, p_vout->p->i_par_den );
287         b_force_par = true;
288     }
289     free( val.psz_string );
290
291     /* Aspect-ratio object var */
292     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
293                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
294
295     text.psz_string = _("Aspect-ratio");
296     var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
297
298     val.psz_string = (char*)"";
299     var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
300
301     for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
302     {
303         val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
304         text.psz_string = _( p_aspect_ratio_values[i].psz_label );
305         var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
306     }
307
308     /* Add custom aspect ratios */
309     psz_buf = var_CreateGetNonEmptyString( p_vout, "custom-aspect-ratios" );
310     if( psz_buf )
311     {
312         AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
313         free( psz_buf );
314     }
315
316     var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
317     var_Get( p_vout, "aspect-ratio", &old_val );
318     if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
319         var_TriggerCallback( p_vout, "aspect-ratio" );
320     free( old_val.psz_string );
321
322     /* Add variables to manage scaling video */
323     var_Create( p_vout, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
324                 | VLC_VAR_ISCOMMAND );
325     text.psz_string = _("Autoscale video");
326     var_Change( p_vout, "autoscale", VLC_VAR_SETTEXT, &text, NULL );
327     var_AddCallback( p_vout, "autoscale", ScalingCallback, NULL );
328     p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
329
330     var_Create( p_vout, "scale", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT
331                 | VLC_VAR_ISCOMMAND );
332     text.psz_string = _("Scale factor");
333     var_Change( p_vout, "scale", VLC_VAR_SETTEXT, &text, NULL );
334     var_AddCallback( p_vout, "scale", ScalingCallback, NULL );
335     p_vout->i_zoom = (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
336
337     /* Initialize the dimensions of the video window */
338     InitWindowSize( p_vout, &p_vout->i_window_width,
339                     &p_vout->i_window_height );
340
341     /* Add a variable to indicate if the window should be on top of others */
342     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
343                 | VLC_VAR_ISCOMMAND );
344     text.psz_string = _("Always on top");
345     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
346     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
347
348     /* Add a variable to indicate whether we want window decoration or not */
349     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
350
351     /* Add a fullscreen variable */
352     if( var_CreateGetBoolCommand( p_vout, "fullscreen" ) )
353     {
354         /* user requested fullscreen */
355         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
356     }
357     text.psz_string = _("Fullscreen");
358     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
359     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
360
361     /* Add a snapshot variable */
362     var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
363     text.psz_string = _("Snapshot");
364     var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
365     var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
366
367     /* Mouse coordinates */
368     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
369     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
370     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
371     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
372     var_Create( p_vout, "mouse-clicked", VLC_VAR_BOOL );
373
374     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
375     var_SetBool( p_vout, "intf-change", true );
376 }
377
378 /*****************************************************************************
379  * vout_Snapshot: generates a snapshot.
380  *****************************************************************************/
381 /**
382  * This function will inject a subpicture into the vout with the provided
383  * picture
384  */
385 static int VoutSnapshotPip( vout_thread_t *p_vout, picture_t *p_pic )
386 {
387     subpicture_t *p_subpic = subpicture_NewFromPicture( VLC_OBJECT(p_vout),
388                                                         p_pic, VLC_CODEC_YUVA );
389     if( !p_subpic )
390         return VLC_EGENERIC;
391
392     /* FIXME DEFAULT_CHAN is not good (used by the text) but
393      * hardcoded 0 doesn't seem right */
394     p_subpic->i_channel = 0;
395     p_subpic->i_start = mdate();
396     p_subpic->i_stop  = p_subpic->i_start + 4000000;
397     p_subpic->b_ephemer = true;
398     p_subpic->b_fade = true;
399
400     /* Reduce the picture to 1/4^2 of the screen */
401     p_subpic->i_original_picture_width  *= 4;
402     p_subpic->i_original_picture_height *= 4;
403
404     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
405     return VLC_SUCCESS;
406 }
407
408 /**
409  * This function will display the name and a PIP of the provided snapshot
410  */
411 static void VoutOsdSnapshot( vout_thread_t *p_vout, picture_t *p_pic, const char *psz_filename )
412 {
413     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
414     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN, "%s", psz_filename );
415
416     if( var_GetBool( p_vout, "snapshot-preview" ) )
417     {
418         if( VoutSnapshotPip( p_vout, p_pic ) )
419             msg_Warn( p_vout, "Failed to display snapshot" );
420     }
421 }
422
423 /* */
424 int vout_GetSnapshot( vout_thread_t *p_vout,
425                       block_t **pp_image, picture_t **pp_picture,
426                       video_format_t *p_fmt,
427                       const char *psz_format, mtime_t i_timeout )
428 {
429     picture_t *p_picture = vout_snapshot_Get( &p_vout->p->snapshot, i_timeout );
430     if( !p_picture )
431     {
432         msg_Err( p_vout, "Failed to grab a snapshot" );
433         return VLC_EGENERIC;
434     }
435
436     if( pp_image )
437     {
438         vlc_fourcc_t i_format = VLC_CODEC_PNG;
439         if( psz_format && image_Type2Fourcc( psz_format ) )
440             i_format = image_Type2Fourcc( psz_format );
441
442         const int i_override_width  = var_GetInteger( p_vout, "snapshot-width" );
443         const int i_override_height = var_GetInteger( p_vout, "snapshot-height" );
444
445         if( picture_Export( VLC_OBJECT(p_vout), pp_image, p_fmt,
446                             p_picture, i_format, i_override_width, i_override_height ) )
447         {
448             msg_Err( p_vout, "Failed to convert image for snapshot" );
449             picture_Release( p_picture );
450             return VLC_EGENERIC;
451         }
452     }
453     if( pp_picture )
454         *pp_picture = p_picture;
455     else
456         picture_Release( p_picture );
457     return VLC_SUCCESS;
458 }
459
460 /**
461  * This function will handle a snapshot request
462  */
463 static void VoutSaveSnapshot( vout_thread_t *p_vout )
464 {
465     char *psz_path = var_GetNonEmptyString( p_vout, "snapshot-path" );
466     char *psz_format = var_GetNonEmptyString( p_vout, "snapshot-format" );
467     char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
468
469     /* */
470     picture_t *p_picture;
471     block_t *p_image;
472     video_format_t fmt;
473
474     /* 500ms timeout
475      * XXX it will cause trouble with low fps video (< 2fps) */
476     if( vout_GetSnapshot( p_vout, &p_image, &p_picture, &fmt, psz_format, 500*1000 ) )
477     {
478         p_picture = NULL;
479         p_image = NULL;
480         goto exit;
481     }
482
483     if( !psz_path )
484     {
485         psz_path = vout_snapshot_GetDirectory();
486         if( !psz_path )
487         {
488             msg_Err( p_vout, "no path specified for snapshots" );
489             goto exit;
490         }
491     }
492
493     vout_snapshot_save_cfg_t cfg;
494     memset( &cfg, 0, sizeof(cfg) );
495     cfg.is_sequential = var_GetBool( p_vout, "snapshot-sequential" );
496     cfg.sequence = var_GetInteger( p_vout, "snapshot-num" );
497     cfg.path = psz_path;
498     cfg.format = psz_format;
499     cfg.prefix_fmt = psz_prefix;
500
501     char *psz_filename;
502     int  i_sequence;
503     if (vout_snapshot_SaveImage( &psz_filename, &i_sequence,
504                                  p_image, VLC_OBJECT(p_vout), &cfg ) )
505         goto exit;
506     if( cfg.is_sequential )
507         var_SetInteger( p_vout, "snapshot-num", i_sequence + 1 );
508
509     VoutOsdSnapshot( p_vout, p_picture, psz_filename );
510
511     /* signal creation of a new snapshot file */
512     var_SetString( p_vout->p_libvlc, "snapshot-file", psz_filename );
513
514     free( psz_filename );
515
516 exit:
517     if( p_image )
518         block_Release( p_image );
519     if( p_picture )
520         picture_Release( p_picture );
521     free( psz_prefix );
522     free( psz_format );
523     free( psz_path );
524 }
525
526 /*****************************************************************************
527  * Handle filters
528  *****************************************************************************/
529
530 void vout_EnableFilter( vout_thread_t *p_vout, const char *psz_name,
531                         bool b_add, bool b_setconfig )
532 {
533     char *psz_parser;
534     char *psz_string;
535     const char *psz_filter_type;
536
537     /* FIXME temporary hack */
538     const char *psz_module_name = psz_name;
539     if( !strcmp( psz_name, "magnify" ) ||
540         !strcmp( psz_name, "puzzle" ) ||
541         !strcmp( psz_name, "logo" ) ||
542         !strcmp( psz_name, "wall" ) ||
543         !strcmp( psz_name, "clone" ) )
544         psz_module_name = "video_filter_wrapper";
545
546     module_t *p_obj = module_find( psz_module_name );
547     if( !p_obj )
548     {
549         msg_Err( p_vout, "Unable to find filter module \"%s\".", psz_name );
550         return;
551     }
552
553     if( module_provides( p_obj, "video filter" ) )
554     {
555         psz_filter_type = "vout-filter";
556     }
557     else if( module_provides( p_obj, "video filter2" ) )
558     {
559         psz_filter_type = "video-filter";
560     }
561     else if( module_provides( p_obj, "sub filter" ) )
562     {
563         psz_filter_type = "sub-filter";
564     }
565     else
566     {
567         module_release( p_obj );
568         msg_Err( p_vout, "Unknown video filter type." );
569         return;
570     }
571     module_release( p_obj );
572
573     if( !strcmp( psz_filter_type, "sub-filter") )
574         psz_string = var_GetString( vout_GetSpu( p_vout ), psz_filter_type );
575     else
576         psz_string = var_GetString( p_vout, psz_filter_type );
577
578     /* Todo : Use some generic chain manipulation functions */
579     if( !psz_string ) psz_string = strdup("");
580
581     psz_parser = strstr( psz_string, psz_name );
582     if( b_add )
583     {
584         if( !psz_parser )
585         {
586             psz_parser = psz_string;
587             if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
588                           psz_string, psz_name ) == -1 )
589             {
590                 free( psz_parser );
591                 return;
592             }
593             free( psz_parser );
594         }
595         else
596             return;
597     }
598     else
599     {
600         if( psz_parser )
601         {
602             memmove( psz_parser, psz_parser + strlen(psz_name) +
603                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
604                             strlen(psz_parser + strlen(psz_name)) + 1 );
605
606             /* Remove trailing : : */
607             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
608             {
609                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
610             }
611          }
612          else
613          {
614              free( psz_string );
615              return;
616          }
617     }
618
619     if( b_setconfig )
620     {
621         if( !strcmp( psz_filter_type, "sub-filter") )
622             config_PutPsz( vout_GetSpu( p_vout ), psz_filter_type, psz_string );
623         else
624             config_PutPsz( p_vout, psz_filter_type, psz_string );
625     }
626
627     if( !strcmp( psz_filter_type, "sub-filter") )
628         var_SetString( vout_GetSpu( p_vout ), psz_filter_type, psz_string );
629     else
630         var_SetString( p_vout, psz_filter_type, psz_string );
631
632     free( psz_string );
633 }
634
635 /*****************************************************************************
636  * InitWindowSize: find the initial dimensions the video window should have.
637  *****************************************************************************
638  * This function will check the "width", "height" and "zoom" config options and
639  * will calculate the size that the video window should have.
640  *****************************************************************************/
641 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
642                             unsigned *pi_height )
643 {
644 #define FP_FACTOR 1000                             /* our fixed point factor */
645
646     int i_width = var_GetInteger( p_vout, "width" );
647     int i_height = var_GetInteger( p_vout, "height" );
648     float f_zoom = var_GetFloat( p_vout, "zoom" );
649     uint64_t ll_zoom = (uint64_t)( FP_FACTOR * f_zoom );
650
651     if( i_width > 0 && i_height > 0)
652     {
653         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
654         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
655     }
656     else if( i_width > 0 )
657     {
658         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
659         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
660             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
661             FP_FACTOR / p_vout->fmt_in.i_visible_width );
662     }
663     else if( i_height > 0 )
664     {
665         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
666         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
667             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
668             FP_FACTOR / p_vout->fmt_in.i_visible_height );
669     }
670     else if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 )
671     {
672         msg_Warn( p_vout, "aspect ratio screwed up" );
673         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
674         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
675     }
676     else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
677     {
678         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
679             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
680         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
681             / FP_FACTOR );
682     }
683     else
684     {
685         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
686             / FP_FACTOR );
687         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
688             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
689     }
690
691     msg_Dbg( p_vout, "window size: %ux%u", *pi_width, *pi_height );
692
693 #undef FP_FACTOR
694 }
695
696 /*****************************************************************************
697  * Object variables callbacks
698  *****************************************************************************/
699 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
700                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
701 {
702     vout_thread_t *p_vout = (vout_thread_t *)p_this;
703     (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
704     InitWindowSize( p_vout, &p_vout->i_window_width,
705                     &p_vout->i_window_height );
706     vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
707                   p_vout->i_window_height );
708     return VLC_SUCCESS;
709 }
710
711 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
712                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
713 {
714     vout_thread_t *p_vout = (vout_thread_t *)p_this;
715     int64_t i_aspect_num, i_aspect_den;
716     unsigned int i_width, i_height;
717
718     (void)oldval; (void)p_data;
719
720     /* Restore defaults */
721     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
722     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
723     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
724     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
725
726     if( !strcmp( psz_cmd, "crop" ) )
727     {
728         char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
729         if( psz_parser )
730         {
731             /* We're using the 3:4 syntax */
732             i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
733             if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
734
735             i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
736             if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
737
738             i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
739                 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
740             i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
741                 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
742
743             if( i_width < p_vout->fmt_render.i_visible_width )
744             {
745                 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
746                     (p_vout->fmt_render.i_visible_width - i_width) / 2;
747                 p_vout->fmt_in.i_visible_width = i_width;
748             }
749             else
750             {
751                 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
752                     (p_vout->fmt_render.i_visible_height - i_height) / 2;
753                 p_vout->fmt_in.i_visible_height = i_height;
754             }
755         }
756         else
757         {
758             psz_parser = strchr( newval.psz_string, 'x' );
759             if( psz_parser )
760             {
761                 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
762                 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
763
764                 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
765                 if( psz_end != psz_parser ) goto crop_end;
766
767                 psz_parser = strchr( ++psz_end, '+' );
768                 i_crop_height = strtol( psz_end, &psz_end, 10 );
769                 if( psz_end != psz_parser ) goto crop_end;
770
771                 psz_parser = strchr( ++psz_end, '+' );
772                 i_crop_left = strtol( psz_end, &psz_end, 10 );
773                 if( psz_end != psz_parser ) goto crop_end;
774
775                 psz_end++;
776                 i_crop_top = strtol( psz_end, &psz_end, 10 );
777                 if( *psz_end != '\0' ) goto crop_end;
778
779                 if( i_crop_top + i_crop_height >= p_vout->fmt_render.i_visible_height ||
780                     i_crop_left + i_crop_width >= p_vout->fmt_render.i_visible_width )
781                 {
782                     msg_Err( p_vout, "Unable to crop over picture boundaries");
783                     return VLC_EGENERIC;
784                 }
785
786                 i_width = i_crop_width;
787                 p_vout->fmt_in.i_visible_width = i_width;
788
789                 i_height = i_crop_height;
790                 p_vout->fmt_in.i_visible_height = i_height;
791
792                 p_vout->fmt_in.i_x_offset = i_crop_left;
793                 p_vout->fmt_in.i_y_offset = i_crop_top;
794             }
795             else
796             {
797                 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
798                 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
799
800                 psz_parser = strchr( newval.psz_string, '+' );
801                 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
802                 if( psz_end != psz_parser ) goto crop_end;
803
804                 psz_parser = strchr( ++psz_end, '+' );
805                 i_crop_top = strtol( psz_end, &psz_end, 10 );
806                 if( psz_end != psz_parser ) goto crop_end;
807
808                 psz_parser = strchr( ++psz_end, '+' );
809                 i_crop_right = strtol( psz_end, &psz_end, 10 );
810                 if( psz_end != psz_parser ) goto crop_end;
811
812                 psz_end++;
813                 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
814                 if( *psz_end != '\0' ) goto crop_end;
815
816                 if( i_crop_top + i_crop_bottom >= p_vout->fmt_render.i_visible_height ||
817                     i_crop_right + i_crop_left >= p_vout->fmt_render.i_visible_width )
818                 {
819                     msg_Err( p_vout, "Unable to crop over picture boundaries" );
820                     return VLC_EGENERIC;
821                 }
822
823                 i_width = p_vout->fmt_render.i_visible_width
824                           - i_crop_left - i_crop_right;
825                 p_vout->fmt_in.i_visible_width = i_width;
826
827                 i_height = p_vout->fmt_render.i_visible_height
828                            - i_crop_top - i_crop_bottom;
829                 p_vout->fmt_in.i_visible_height = i_height;
830
831                 p_vout->fmt_in.i_x_offset = i_crop_left;
832                 p_vout->fmt_in.i_y_offset = i_crop_top;
833             }
834         }
835     }
836     else if( !strcmp( psz_cmd, "crop-top" )
837           || !strcmp( psz_cmd, "crop-left" )
838           || !strcmp( psz_cmd, "crop-bottom" )
839           || !strcmp( psz_cmd, "crop-right" ) )
840     {
841         unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
842
843         i_crop_top = var_GetInteger( p_vout, "crop-top" );
844         i_crop_left = var_GetInteger( p_vout, "crop-left" );
845         i_crop_right = var_GetInteger( p_vout, "crop-right" );
846         i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
847
848         if( i_crop_top + i_crop_bottom >= p_vout->fmt_render.i_visible_height ||
849             i_crop_right + i_crop_left >= p_vout->fmt_render.i_visible_width )
850         {
851             msg_Err( p_vout, "Unable to crop over picture boundaries" );
852             return VLC_EGENERIC;
853         }
854
855         i_width = p_vout->fmt_render.i_visible_width
856                   - i_crop_left - i_crop_right;
857         p_vout->fmt_in.i_visible_width = i_width;
858
859         i_height = p_vout->fmt_render.i_visible_height
860                    - i_crop_top - i_crop_bottom;
861         p_vout->fmt_in.i_visible_height = i_height;
862
863         p_vout->fmt_in.i_x_offset = i_crop_left;
864         p_vout->fmt_in.i_y_offset = i_crop_top;
865     }
866
867  crop_end:
868     InitWindowSize( p_vout, &p_vout->i_window_width,
869                     &p_vout->i_window_height );
870
871     p_vout->i_changes |= VOUT_CROP_CHANGE;
872
873     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
874              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
875              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
876              p_vout->fmt_in.i_visible_width,
877              p_vout->fmt_in.i_visible_height );
878
879     var_TriggerCallback( p_vout, "crop-update" );
880
881     return VLC_SUCCESS;
882 }
883
884 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
885                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
886 {
887     vout_thread_t *p_vout = (vout_thread_t *)p_this;
888     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
889     vlc_value_t val;
890
891     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
892     (void)psz_cmd; (void)oldval; (void)p_data;
893
894     /* Restore defaults */
895     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
896     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
897     p_vout->render.i_aspect = (int64_t)p_vout->fmt_render.i_sar_num *
898                                        p_vout->fmt_render.i_width *
899                                        VOUT_ASPECT_FACTOR /
900                                        p_vout->fmt_render.i_sar_den / p_vout->fmt_render.i_height;
901
902     if( !psz_parser ) goto aspect_end;
903
904     i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
905     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
906
907     i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
908     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
909
910     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
911     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
912     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
913     p_vout->fmt_in.i_sar_num = i_sar_num;
914     p_vout->fmt_in.i_sar_den = i_sar_den;
915     p_vout->render.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
916
917  aspect_end:
918     if( p_vout->p->i_par_num && p_vout->p->i_par_den )
919     {
920         p_vout->fmt_in.i_sar_num *= p_vout->p->i_par_den;
921         p_vout->fmt_in.i_sar_den *= p_vout->p->i_par_num;
922         p_vout->render.i_aspect = (int64_t)p_vout->fmt_in.i_sar_num *
923                                            p_vout->fmt_in.i_width *
924                                            VOUT_ASPECT_FACTOR /
925                                            p_vout->fmt_in.i_sar_den /
926                                            p_vout->fmt_in.i_height;
927     }
928
929     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
930
931     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
932              p_vout->fmt_in.i_sar_num * p_vout->fmt_in.i_width,
933              p_vout->fmt_in.i_sar_den * p_vout->fmt_in.i_height,
934              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
935
936     if( var_Get( p_vout, "crop", &val ) )
937         return VLC_EGENERIC;
938
939     int i_ret = CropCallback( p_this, "crop", val, val, 0 );
940     free( val.psz_string );
941     return i_ret;
942 }
943
944 static int ScalingCallback( vlc_object_t *p_this, char const *psz_cmd,
945                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
946 {
947     vout_thread_t *p_vout = (vout_thread_t *)p_this;
948     (void)oldval; (void)newval; (void)p_data;
949
950     vlc_mutex_lock( &p_vout->change_lock );
951
952     if( !strcmp( psz_cmd, "autoscale" ) )
953     {
954         p_vout->i_changes |= VOUT_SCALE_CHANGE;
955     }
956     else if( !strcmp( psz_cmd, "scale" ) )
957     {
958         p_vout->i_changes |= VOUT_ZOOM_CHANGE;
959     }
960
961     vlc_mutex_unlock( &p_vout->change_lock );
962
963     return VLC_SUCCESS;
964 }
965
966 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
967                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
968 {
969     vout_thread_t *p_vout = (vout_thread_t *)p_this;
970
971     vlc_mutex_lock( &p_vout->change_lock );
972     p_vout->i_changes |= VOUT_ON_TOP_CHANGE;
973     p_vout->b_on_top = newval.b_bool;
974     vlc_mutex_unlock( &p_vout->change_lock );
975
976     /* Modify libvlc as well because the vout might have to be restarted */
977     var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
978     var_Set( p_vout->p_libvlc, "video-on-top", newval );
979
980     (void)psz_cmd; (void)oldval; (void)p_data;
981     return VLC_SUCCESS;
982 }
983
984 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
985                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
986 {
987     vout_thread_t *p_vout = (vout_thread_t *)p_this;
988     vlc_value_t val;
989     (void)psz_cmd; (void)oldval; (void)p_data;
990
991     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
992
993     /* Modify libvlc as well because the vout might have to be restarted */
994     var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
995     var_Set( p_vout->p_libvlc, "fullscreen", newval );
996
997     val.b_bool = true;
998     var_Set( p_vout, "intf-change", val );
999     return VLC_SUCCESS;
1000 }
1001
1002 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1003                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1004 {
1005     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1006     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1007     VLC_UNUSED(newval); VLC_UNUSED(p_data);
1008
1009     VoutSaveSnapshot( p_vout );
1010     return VLC_SUCCESS;
1011 }
1012
1013 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1014                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1015 {
1016     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1017     VLC_UNUSED(p_data);
1018     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1019     p_vout->p->b_title_show = newval.b_bool;
1020     return VLC_SUCCESS;
1021 }
1022
1023 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1024                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1025 {
1026     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1027     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1028     p_vout->p->i_title_timeout = (mtime_t) newval.i_int;
1029     return VLC_SUCCESS;
1030 }
1031
1032 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1033                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1034 {
1035     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1036     VLC_UNUSED(p_data);
1037     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1038     p_vout->p->i_title_position = newval.i_int;
1039     return VLC_SUCCESS;
1040 }