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