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