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