]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
Add 2.35 and 2.39 AR
[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_strings.h>
48 #include <vlc_charset.h>
49 #include "../libvlc.h"
50 #include "vout_internal.h"
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
56
57 /* Object variables callbacks */
58 static int ZoomCallback( vlc_object_t *, char const *,
59                          vlc_value_t, vlc_value_t, void * );
60 static int CropCallback( vlc_object_t *, char const *,
61                          vlc_value_t, vlc_value_t, void * );
62 static int AspectCallback( vlc_object_t *, char const *,
63                            vlc_value_t, vlc_value_t, void * );
64 static int ScalingCallback( 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 * );
72
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 * );
79
80 /*****************************************************************************
81  * vout_IntfInit: called during the vout creation to initialise misc things.
82  *****************************************************************************/
83 static const struct
84 {
85     double f_value;
86     const char *psz_label;
87 } p_zoom_values[] = {
88     { 0.25, N_("1:4 Quarter") },
89     { 0.5, N_("1:2 Half") },
90     { 1, N_("1:1 Original") },
91     { 2, N_("2:1 Double") },
92     { 0, NULL } };
93
94 static const struct
95 {
96     const char *psz_value;
97     const char *psz_label;
98 } p_crop_values[] = {
99     { "", N_("Default") },
100     { "16:10", "16:10" },
101     { "16:9", "16:9" },
102     { "185:100", "1.85:1" },
103     { "221:100", "2.21:1" },
104     { "235:100", "2.35:1" },
105     { "239:100", "2.39:1" },
106     { "5:3", "5:3" },
107     { "4:3", "4:3" },
108     { "5:4", "5:4" },
109     { "1:1", "1:1" },
110     { NULL, NULL } };
111
112 static const struct
113 {
114     const char *psz_value;
115     const char *psz_label;
116 } p_aspect_ratio_values[] = {
117     { "", N_("Default") },
118     { "1:1", "1:1" },
119     { "4:3", "4:3" },
120     { "16:9", "16:9" },
121     { "16:10", "16:10" },
122     { "221:100", "2.21:1" },
123     { "235:100", "2.35:1" },
124     { "239:100", "2.39: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-button-down", VLC_VAR_INTEGER );
369     var_Create( p_vout, "mouse-moved", VLC_VAR_COORDS );
370     var_Create( p_vout, "mouse-clicked", VLC_VAR_COORDS );
371     var_Create( p_vout, "mouse-object", VLC_VAR_BOOL );
372
373     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
374     var_SetBool( p_vout, "intf-change", true );
375 }
376
377 /*****************************************************************************
378  * vout_Snapshot: generates a snapshot.
379  *****************************************************************************/
380 /**
381  * This function will inject a subpicture into the vout with the provided
382  * picture
383  */
384 static int VoutSnapshotPip( vout_thread_t *p_vout, picture_t *p_pic )
385 {
386     subpicture_t *p_subpic = subpicture_NewFromPicture( VLC_OBJECT(p_vout),
387                                                         p_pic, VLC_CODEC_YUVA );
388     if( !p_subpic )
389         return VLC_EGENERIC;
390
391     /* FIXME DEFAULT_CHAN is not good (used by the text) but
392      * hardcoded 0 doesn't seem right */
393     p_subpic->i_channel = 0;
394     p_subpic->i_start = mdate();
395     p_subpic->i_stop  = p_subpic->i_start + 4000000;
396     p_subpic->b_ephemer = true;
397     p_subpic->b_fade = true;
398
399     /* Reduce the picture to 1/4^2 of the screen */
400     p_subpic->i_original_picture_width  *= 4;
401     p_subpic->i_original_picture_height *= 4;
402
403     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
404     return VLC_SUCCESS;
405 }
406
407 /**
408  * This function will display the name and a PIP of the provided snapshot
409  */
410 static void VoutOsdSnapshot( vout_thread_t *p_vout, picture_t *p_pic, const char *psz_filename )
411 {
412     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
413     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN, "%s", psz_filename );
414
415     if( var_GetBool( p_vout, "snapshot-preview" ) )
416     {
417         if( VoutSnapshotPip( p_vout, p_pic ) )
418             msg_Warn( p_vout, "Failed to display snapshot" );
419     }
420 }
421
422 /* */
423 int vout_GetSnapshot( vout_thread_t *p_vout,
424                       block_t **pp_image, picture_t **pp_picture,
425                       video_format_t *p_fmt,
426                       const char *psz_format, mtime_t i_timeout )
427 {
428     picture_t *p_picture = vout_snapshot_Get( &p_vout->p->snapshot, i_timeout );
429     if( !p_picture )
430     {
431         msg_Err( p_vout, "Failed to grab a snapshot" );
432         return VLC_EGENERIC;
433     }
434
435     if( pp_image )
436     {
437         vlc_fourcc_t i_format = VLC_CODEC_PNG;
438         if( psz_format && image_Type2Fourcc( psz_format ) )
439             i_format = image_Type2Fourcc( psz_format );
440
441         const int i_override_width  = var_GetInteger( p_vout, "snapshot-width" );
442         const int i_override_height = var_GetInteger( p_vout, "snapshot-height" );
443
444         if( picture_Export( VLC_OBJECT(p_vout), pp_image, p_fmt,
445                             p_picture, i_format, i_override_width, i_override_height ) )
446         {
447             msg_Err( p_vout, "Failed to convert image for snapshot" );
448             picture_Release( p_picture );
449             return VLC_EGENERIC;
450         }
451     }
452     if( pp_picture )
453         *pp_picture = p_picture;
454     else
455         picture_Release( p_picture );
456     return VLC_SUCCESS;
457 }
458
459 /**
460  * This function will handle a snapshot request
461  */
462 static void VoutSaveSnapshot( vout_thread_t *p_vout )
463 {
464     char *psz_path = var_GetNonEmptyString( p_vout, "snapshot-path" );
465     char *psz_format = var_GetNonEmptyString( p_vout, "snapshot-format" );
466     char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
467
468     /* */
469     picture_t *p_picture;
470     block_t *p_image;
471     video_format_t fmt;
472
473     /* 500ms timeout
474      * XXX it will cause trouble with low fps video (< 2fps) */
475     if( vout_GetSnapshot( p_vout, &p_image, &p_picture, &fmt, psz_format, 500*1000 ) )
476     {
477         p_picture = NULL;
478         p_image = NULL;
479         goto exit;
480     }
481
482     if( !psz_path )
483     {
484         psz_path = vout_snapshot_GetDirectory();
485         if( !psz_path )
486         {
487             msg_Err( p_vout, "no path specified for snapshots" );
488             goto exit;
489         }
490     }
491
492     vout_snapshot_save_cfg_t cfg;
493     memset( &cfg, 0, sizeof(cfg) );
494     cfg.is_sequential = var_GetBool( p_vout, "snapshot-sequential" );
495     cfg.sequence = var_GetInteger( p_vout, "snapshot-num" );
496     cfg.path = psz_path;
497     cfg.format = psz_format;
498     cfg.prefix_fmt = psz_prefix;
499
500     char *psz_filename;
501     int  i_sequence;
502     if (vout_snapshot_SaveImage( &psz_filename, &i_sequence,
503                                  p_image, VLC_OBJECT(p_vout), &cfg ) )
504         goto exit;
505     if( cfg.is_sequential )
506         var_SetInteger( p_vout, "snapshot-num", i_sequence + 1 );
507
508     VoutOsdSnapshot( p_vout, p_picture, psz_filename );
509
510     /* signal creation of a new snapshot file */
511     var_SetString( p_vout->p_libvlc, "snapshot-file", psz_filename );
512
513     free( psz_filename );
514
515 exit:
516     if( p_image )
517         block_Release( p_image );
518     if( p_picture )
519         picture_Release( p_picture );
520     free( psz_prefix );
521     free( psz_format );
522     free( psz_path );
523 }
524
525 /*****************************************************************************
526  * Handle filters
527  *****************************************************************************/
528
529 void vout_EnableFilter( vout_thread_t *p_vout, const char *psz_name,
530                         bool b_add, bool b_setconfig )
531 {
532     char *psz_parser;
533     char *psz_string;
534     const char *psz_filter_type;
535
536     /* FIXME temporary hack */
537     const char *psz_module_name = psz_name;
538     if( !strcmp( psz_name, "magnify" ) ||
539         !strcmp( psz_name, "puzzle" ) ||
540         !strcmp( psz_name, "logo" ) ||
541         !strcmp( psz_name, "wall" ) ||
542         !strcmp( psz_name, "clone" ) )
543         psz_module_name = "video_filter_wrapper";
544
545     module_t *p_obj = module_find( psz_module_name );
546     if( !p_obj )
547     {
548         msg_Err( p_vout, "Unable to find filter module \"%s\".", psz_name );
549         return;
550     }
551
552     if( module_provides( p_obj, "video filter" ) )
553     {
554         psz_filter_type = "vout-filter";
555     }
556     else if( module_provides( p_obj, "video filter2" ) )
557     {
558         psz_filter_type = "video-filter";
559     }
560     else if( module_provides( p_obj, "sub filter" ) )
561     {
562         psz_filter_type = "sub-filter";
563     }
564     else
565     {
566         module_release( p_obj );
567         msg_Err( p_vout, "Unknown video filter type." );
568         return;
569     }
570     module_release( p_obj );
571
572     if( !strcmp( psz_filter_type, "sub-filter") )
573         psz_string = var_GetString( vout_GetSpu( p_vout ), psz_filter_type );
574     else
575         psz_string = var_GetString( p_vout, psz_filter_type );
576
577     /* Todo : Use some generic chain manipulation functions */
578     if( !psz_string ) psz_string = strdup("");
579
580     psz_parser = strstr( psz_string, psz_name );
581     if( b_add )
582     {
583         if( !psz_parser )
584         {
585             psz_parser = psz_string;
586             if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
587                           psz_string, psz_name ) == -1 )
588             {
589                 free( psz_parser );
590                 return;
591             }
592             free( psz_parser );
593         }
594         else
595             return;
596     }
597     else
598     {
599         if( psz_parser )
600         {
601             memmove( psz_parser, psz_parser + strlen(psz_name) +
602                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
603                             strlen(psz_parser + strlen(psz_name)) + 1 );
604
605             /* Remove trailing : : */
606             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
607             {
608                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
609             }
610          }
611          else
612          {
613              free( psz_string );
614              return;
615          }
616     }
617
618     if( b_setconfig )
619     {
620         if( !strcmp( psz_filter_type, "sub-filter") )
621             config_PutPsz( vout_GetSpu( p_vout ), psz_filter_type, psz_string );
622         else
623             config_PutPsz( p_vout, psz_filter_type, psz_string );
624     }
625
626     if( !strcmp( psz_filter_type, "sub-filter") )
627         var_SetString( vout_GetSpu( p_vout ), psz_filter_type, psz_string );
628     else
629         var_SetString( p_vout, psz_filter_type, psz_string );
630
631     free( psz_string );
632 }
633
634 /*****************************************************************************
635  * InitWindowSize: find the initial dimensions the video window should have.
636  *****************************************************************************
637  * This function will check the "width", "height" and "zoom" config options and
638  * will calculate the size that the video window should have.
639  *****************************************************************************/
640 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
641                             unsigned *pi_height )
642 {
643 #define FP_FACTOR 1000                             /* our fixed point factor */
644
645     int i_width = var_GetInteger( p_vout, "width" );
646     int i_height = var_GetInteger( p_vout, "height" );
647     float f_zoom = var_GetFloat( p_vout, "zoom" );
648     uint64_t ll_zoom = (uint64_t)( FP_FACTOR * f_zoom );
649
650     if( i_width > 0 && i_height > 0)
651     {
652         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
653         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
654     }
655     else if( i_width > 0 )
656     {
657         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
658         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
659             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
660             FP_FACTOR / p_vout->fmt_in.i_visible_width );
661     }
662     else if( i_height > 0 )
663     {
664         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
665         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
666             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
667             FP_FACTOR / p_vout->fmt_in.i_visible_height );
668     }
669     else if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 )
670     {
671         msg_Warn( p_vout, "aspect ratio screwed up" );
672         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
673         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
674     }
675     else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
676     {
677         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
678             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
679         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
680             / FP_FACTOR );
681     }
682     else
683     {
684         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
685             / FP_FACTOR );
686         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
687             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
688     }
689
690     msg_Dbg( p_vout, "window size: %ux%u", *pi_width, *pi_height );
691
692 #undef FP_FACTOR
693 }
694
695 /*****************************************************************************
696  * Object variables callbacks
697  *****************************************************************************/
698 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
699                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
700 {
701     (void)psz_cmd; (void)oldval; (void)p_data;
702
703     return var_SetFloat( p_this, "scale", newval.f_float );
704 }
705
706 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
707                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
708 {
709     vout_thread_t *p_vout = (vout_thread_t *)p_this;
710     int64_t i_aspect_num, i_aspect_den;
711     unsigned int i_width, i_height;
712
713     (void)oldval; (void)p_data;
714
715     /* Restore defaults */
716     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
717     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
718     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
719     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
720
721     if( !strcmp( psz_cmd, "crop" ) )
722     {
723         char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
724         if( psz_parser )
725         {
726             /* We're using the 3:4 syntax */
727             i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
728             if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
729
730             i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
731             if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
732
733             i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
734                 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
735             i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
736                 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
737
738             if( i_width < p_vout->fmt_render.i_visible_width )
739             {
740                 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
741                     (p_vout->fmt_render.i_visible_width - i_width) / 2;
742                 p_vout->fmt_in.i_visible_width = i_width;
743             }
744             else
745             {
746                 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
747                     (p_vout->fmt_render.i_visible_height - i_height) / 2;
748                 p_vout->fmt_in.i_visible_height = i_height;
749             }
750         }
751         else
752         {
753             psz_parser = strchr( newval.psz_string, 'x' );
754             if( psz_parser )
755             {
756                 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
757                 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
758
759                 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
760                 if( psz_end != psz_parser ) goto crop_end;
761
762                 psz_parser = strchr( ++psz_end, '+' );
763                 i_crop_height = strtol( psz_end, &psz_end, 10 );
764                 if( psz_end != psz_parser ) goto crop_end;
765
766                 psz_parser = strchr( ++psz_end, '+' );
767                 i_crop_left = strtol( psz_end, &psz_end, 10 );
768                 if( psz_end != psz_parser ) goto crop_end;
769
770                 psz_end++;
771                 i_crop_top = strtol( psz_end, &psz_end, 10 );
772                 if( *psz_end != '\0' ) goto crop_end;
773
774                 if( i_crop_top + i_crop_height >= p_vout->fmt_render.i_visible_height ||
775                     i_crop_left + i_crop_width >= p_vout->fmt_render.i_visible_width )
776                 {
777                     msg_Err( p_vout, "Unable to crop over picture boundaries");
778                     return VLC_EGENERIC;
779                 }
780
781                 i_width = i_crop_width;
782                 p_vout->fmt_in.i_visible_width = i_width;
783
784                 i_height = i_crop_height;
785                 p_vout->fmt_in.i_visible_height = i_height;
786
787                 p_vout->fmt_in.i_x_offset = i_crop_left;
788                 p_vout->fmt_in.i_y_offset = i_crop_top;
789             }
790             else
791             {
792                 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
793                 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
794
795                 psz_parser = strchr( newval.psz_string, '+' );
796                 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
797                 if( psz_end != psz_parser ) goto crop_end;
798
799                 psz_parser = strchr( ++psz_end, '+' );
800                 i_crop_top = strtol( psz_end, &psz_end, 10 );
801                 if( psz_end != psz_parser ) goto crop_end;
802
803                 psz_parser = strchr( ++psz_end, '+' );
804                 i_crop_right = strtol( psz_end, &psz_end, 10 );
805                 if( psz_end != psz_parser ) goto crop_end;
806
807                 psz_end++;
808                 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
809                 if( *psz_end != '\0' ) goto crop_end;
810
811                 if( i_crop_top + i_crop_bottom >= p_vout->fmt_render.i_visible_height ||
812                     i_crop_right + i_crop_left >= p_vout->fmt_render.i_visible_width )
813                 {
814                     msg_Err( p_vout, "Unable to crop over picture boundaries" );
815                     return VLC_EGENERIC;
816                 }
817
818                 i_width = p_vout->fmt_render.i_visible_width
819                           - i_crop_left - i_crop_right;
820                 p_vout->fmt_in.i_visible_width = i_width;
821
822                 i_height = p_vout->fmt_render.i_visible_height
823                            - i_crop_top - i_crop_bottom;
824                 p_vout->fmt_in.i_visible_height = i_height;
825
826                 p_vout->fmt_in.i_x_offset = i_crop_left;
827                 p_vout->fmt_in.i_y_offset = i_crop_top;
828             }
829         }
830     }
831     else if( !strcmp( psz_cmd, "crop-top" )
832           || !strcmp( psz_cmd, "crop-left" )
833           || !strcmp( psz_cmd, "crop-bottom" )
834           || !strcmp( psz_cmd, "crop-right" ) )
835     {
836         unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
837
838         i_crop_top = var_GetInteger( p_vout, "crop-top" );
839         i_crop_left = var_GetInteger( p_vout, "crop-left" );
840         i_crop_right = var_GetInteger( p_vout, "crop-right" );
841         i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
842
843         if( i_crop_top + i_crop_bottom >= p_vout->fmt_render.i_visible_height ||
844             i_crop_right + i_crop_left >= p_vout->fmt_render.i_visible_width )
845         {
846             msg_Err( p_vout, "Unable to crop over picture boundaries" );
847             return VLC_EGENERIC;
848         }
849
850         i_width = p_vout->fmt_render.i_visible_width
851                   - i_crop_left - i_crop_right;
852         p_vout->fmt_in.i_visible_width = i_width;
853
854         i_height = p_vout->fmt_render.i_visible_height
855                    - i_crop_top - i_crop_bottom;
856         p_vout->fmt_in.i_visible_height = i_height;
857
858         p_vout->fmt_in.i_x_offset = i_crop_left;
859         p_vout->fmt_in.i_y_offset = i_crop_top;
860     }
861
862  crop_end:
863     InitWindowSize( p_vout, &p_vout->i_window_width,
864                     &p_vout->i_window_height );
865
866     p_vout->i_changes |= VOUT_CROP_CHANGE;
867
868     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
869              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
870              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
871              p_vout->fmt_in.i_visible_width,
872              p_vout->fmt_in.i_visible_height );
873
874     var_TriggerCallback( p_vout, "crop-update" );
875
876     return VLC_SUCCESS;
877 }
878
879 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
880                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
881 {
882     vout_thread_t *p_vout = (vout_thread_t *)p_this;
883     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
884     vlc_value_t val;
885
886     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
887     (void)psz_cmd; (void)oldval; (void)p_data;
888
889     /* Restore defaults */
890     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
891     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
892     p_vout->render.i_aspect = (int64_t)p_vout->fmt_render.i_sar_num *
893                                        p_vout->fmt_render.i_width *
894                                        VOUT_ASPECT_FACTOR /
895                                        p_vout->fmt_render.i_sar_den / p_vout->fmt_render.i_height;
896
897     if( !psz_parser ) goto aspect_end;
898
899     i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
900     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
901
902     i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
903     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
904
905     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
906     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
907     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
908     p_vout->fmt_in.i_sar_num = i_sar_num;
909     p_vout->fmt_in.i_sar_den = i_sar_den;
910     p_vout->render.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
911
912  aspect_end:
913     if( p_vout->p->i_par_num && p_vout->p->i_par_den )
914     {
915         p_vout->fmt_in.i_sar_num *= p_vout->p->i_par_den;
916         p_vout->fmt_in.i_sar_den *= p_vout->p->i_par_num;
917         p_vout->render.i_aspect = (int64_t)p_vout->fmt_in.i_sar_num *
918                                            p_vout->fmt_in.i_width *
919                                            VOUT_ASPECT_FACTOR /
920                                            p_vout->fmt_in.i_sar_den /
921                                            p_vout->fmt_in.i_height;
922     }
923
924     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
925
926     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
927              p_vout->fmt_in.i_sar_num * p_vout->fmt_in.i_width,
928              p_vout->fmt_in.i_sar_den * p_vout->fmt_in.i_height,
929              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
930
931     if( var_Get( p_vout, "crop", &val ) )
932         return VLC_EGENERIC;
933
934     int i_ret = CropCallback( p_this, "crop", val, val, 0 );
935     free( val.psz_string );
936     return i_ret;
937 }
938
939 static int ScalingCallback( vlc_object_t *p_this, char const *psz_cmd,
940                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
941 {
942     vout_thread_t *p_vout = (vout_thread_t *)p_this;
943     (void)oldval; (void)newval; (void)p_data;
944
945     vlc_mutex_lock( &p_vout->change_lock );
946
947     if( !strcmp( psz_cmd, "autoscale" ) )
948     {
949         p_vout->i_changes |= VOUT_SCALE_CHANGE;
950     }
951     else if( !strcmp( psz_cmd, "scale" ) )
952     {
953         p_vout->i_changes |= VOUT_ZOOM_CHANGE;
954     }
955
956     vlc_mutex_unlock( &p_vout->change_lock );
957
958     return VLC_SUCCESS;
959 }
960
961 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
962                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
963 {
964     vout_thread_t *p_vout = (vout_thread_t *)p_this;
965
966     vlc_mutex_lock( &p_vout->change_lock );
967     p_vout->i_changes |= VOUT_ON_TOP_CHANGE;
968     p_vout->b_on_top = newval.b_bool;
969     vlc_mutex_unlock( &p_vout->change_lock );
970
971     (void)psz_cmd; (void)oldval; (void)p_data;
972     return VLC_SUCCESS;
973 }
974
975 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
976                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
977 {
978     vout_thread_t *p_vout = (vout_thread_t *)p_this;
979     vlc_value_t val;
980     (void)psz_cmd; (void)p_data;
981
982     if( oldval.b_bool == newval.b_bool )
983         return VLC_SUCCESS; /* no-op */
984     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
985
986     val.b_bool = true;
987     var_Set( p_vout, "intf-change", val );
988     return VLC_SUCCESS;
989 }
990
991 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
992                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
993 {
994     vout_thread_t *p_vout = (vout_thread_t *)p_this;
995     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
996     VLC_UNUSED(newval); VLC_UNUSED(p_data);
997
998     VoutSaveSnapshot( p_vout );
999     return VLC_SUCCESS;
1000 }
1001
1002 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1003                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1004 {
1005     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1006     VLC_UNUSED(p_data);
1007     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1008     p_vout->p->b_title_show = newval.b_bool;
1009     return VLC_SUCCESS;
1010 }
1011
1012 static int TitleTimeoutCallback( 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); VLC_UNUSED(p_data);
1016     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1017     p_vout->p->i_title_timeout = (mtime_t) newval.i_int;
1018     return VLC_SUCCESS;
1019 }
1020
1021 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1022                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1023 {
1024     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1025     VLC_UNUSED(p_data);
1026     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1027     p_vout->p->i_title_position = newval.i_int;
1028     return VLC_SUCCESS;
1029 }