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