]> git.sesse.net Git - vlc/blob - modules/video_filter/osdmenu.c
f573d3a64d51557d4bc992d74299e97b66b45918
[vlc] / modules / video_filter / osdmenu.c
1 /*****************************************************************************
2  * osdmenu.c: osd filter module
3  *****************************************************************************
4  * Copyright (C) 2004-2005 M2X
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implid warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include <vlc_filter.h>
33 #include <vlc_video.h>
34
35 #include <vlc_osd.h>
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40
41 /* FIXME: Future extension make the definition file in XML format. */
42 #define OSD_FILE_TEXT N_("Configuration file")
43 /// \bug [String] missing dot
44 #define OSD_FILE_LONGTEXT N_( \
45     "Configuration file for the OSD Menu" )
46 #define OSD_PATH_TEXT N_("Path to OSD menu images")
47 #define OSD_PATH_LONGTEXT N_( \
48     "Path to the OSD menu images. This will override the path defined in the " \
49     "OSD configuration file." )
50
51 #define POSX_TEXT N_("X coordinate")
52 #define POSX_LONGTEXT N_("You can move the OSD menu by left-clicking on it." )
53
54 #define POSY_TEXT N_("Y coordinate")
55 #define POSY_LONGTEXT N_("You can move the OSD menu by left-clicking on it." )
56
57 #define POS_TEXT N_("Menu position")
58 #define POS_LONGTEXT N_( \
59   "You can enforce the OSD menu position on the video " \
60   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
61   "also use combinations of these values, eg. 6 = top-right).")
62
63 #define TIMEOUT_TEXT N_("Menu timeout")
64 #define TIMEOUT_LONGTEXT N_( \
65     "OSD menu pictures get a default timeout of 15 seconds added to their " \
66     "remaining time. This will ensure that they are at least the specified " \
67     "time visible.")
68
69 #define OSD_UPDATE_TEXT N_("Menu update interval" )
70 #define OSD_UPDATE_LONGTEXT N_( \
71     "The default is to update the OSD menu picture every 200 ms. Shorten the" \
72     " update time for environments that experience transmissions errors. " \
73     "Be careful with this option as encoding OSD menu pictures is very " \
74     "computing intensive. The range is 0 - 1000 ms." )
75
76 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
77 static char *ppsz_pos_descriptions[] =
78 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
79   N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
80
81 /* subfilter functions */
82 static int  CreateFilter ( vlc_object_t * );
83 static void DestroyFilter( vlc_object_t * );
84 static subpicture_t *Filter( filter_t *, mtime_t );
85 static int OSDMenuUpdateEvent( vlc_object_t *, char const *,
86                     vlc_value_t, vlc_value_t, void * );                    
87 static int OSDMenuVisibleEvent( vlc_object_t *, char const *,
88                     vlc_value_t, vlc_value_t, void * );
89
90 #define OSD_CFG "osdmenu-"
91
92 #if defined( WIN32 ) || defined( UNDER_CE )
93 #define OSD_DEFAULT_CFG "osdmenu/default.cfg"
94 #else
95 #define OSD_DEFAULT_CFG "share/osdmenu/default.cfg"
96 #endif
97
98 #define OSD_UPDATE_MIN     0
99 #define OSD_UPDATE_DEFAULT 0
100 #define OSD_UPDATE_MAX     1000
101
102 vlc_module_begin();
103     add_integer( OSD_CFG "x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
104     add_integer( OSD_CFG "y", -1, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
105     add_integer( OSD_CFG "position", 8, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
106         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
107     add_string( OSD_CFG "file", OSD_DEFAULT_CFG, NULL, OSD_FILE_TEXT,
108         OSD_FILE_LONGTEXT, VLC_FALSE );
109     add_string( OSD_CFG "file-path", NULL, NULL, OSD_PATH_TEXT,
110         OSD_PATH_LONGTEXT, VLC_FALSE );
111     add_integer( OSD_CFG "timeout", 15, NULL, TIMEOUT_TEXT,
112         TIMEOUT_LONGTEXT, VLC_FALSE );
113     add_integer_with_range( OSD_CFG "update", OSD_UPDATE_DEFAULT,
114         OSD_UPDATE_MIN, OSD_UPDATE_MAX, NULL, OSD_UPDATE_TEXT,
115         OSD_UPDATE_LONGTEXT, VLC_TRUE );
116
117     set_capability( "sub filter", 100 );
118     set_description( N_("On Screen Display menu") );
119     set_shortname( N_("OSD menu") );
120     add_shortcut( "osdmenu" );
121 /*
122     set_category( CAT_VIDEO );
123     set_subcategory( SUBCAT_VIDEO_SUBPIC );
124 */
125     set_callbacks( CreateFilter, DestroyFilter );
126 vlc_module_end();
127
128 /*****************************************************************************
129  * Sub filter code
130  *****************************************************************************/
131
132 /*****************************************************************************
133  * Local prototypes
134  *****************************************************************************/
135 struct filter_sys_t
136 {
137     vlc_mutex_t  lock;
138
139     int          position;      /* relative positioning of SPU images */
140     mtime_t      i_last_date;   /* last mdate SPU object has been sent to SPU subsytem */
141     mtime_t      i_timeout;     /* duration SPU object is valid on the video output in seconds */
142
143     vlc_bool_t   b_absolute;    /* do we use absolute positioning or relative? */
144     vlc_bool_t   b_update;      /* Update OSD Menu by sending SPU objects */
145     vlc_bool_t   b_visible;     /* OSD Menu is visible */
146     mtime_t      i_update;      /* Update the OSD menu every n ms */
147     mtime_t      i_end_date;    /* End data of display OSD menu */
148
149     char        *psz_file;      /* OSD Menu configuration file */
150     osd_menu_t  *p_menu;        /* pointer to OSD Menu object */
151 };
152
153 /*****************************************************************************
154  * CreateFilter: Create the filter and open the definition file
155  *****************************************************************************/
156 static int CreateFilter ( vlc_object_t *p_this )
157 {
158     filter_t *p_filter = (filter_t *)p_this;
159     vlc_value_t val;
160     int i_posx, i_posy;
161
162     p_filter->p_sys = (filter_sys_t *) malloc( sizeof( filter_sys_t ) );
163     if( !p_filter->p_sys )
164     {
165         msg_Err( p_filter, "out of memory" );
166         return VLC_ENOMEM;
167     }
168
169     /* Populating struct */
170     p_filter->p_sys->p_menu = NULL;
171     p_filter->p_sys->psz_file = NULL;
172
173     vlc_mutex_init( p_filter, &p_filter->p_sys->lock );
174
175     p_filter->p_sys->psz_file = config_GetPsz( p_filter, OSD_CFG "file" );
176     if( p_filter->p_sys->psz_file == NULL || *p_filter->p_sys->psz_file == '\0' ) 
177     {
178         msg_Err( p_filter, "unable to get filename" );
179         goto error;
180     }
181
182     var_Create( p_this, OSD_CFG "position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
183     var_Get( p_this, OSD_CFG "position", &val );
184     p_filter->p_sys->position = val.i_int;
185     var_Create( p_this, OSD_CFG "x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
186     var_Get( p_this, OSD_CFG "x", &val );
187     i_posx = val.i_int;
188     var_Create( p_this, OSD_CFG "y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
189     var_Get( p_this, OSD_CFG "y", &val );
190     i_posy = val.i_int;
191
192     /* in micro seconds - divide by 2 to match user expectations */
193     var_Create( p_this, OSD_CFG "timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
194     var_Get( p_this, OSD_CFG "timeout", &val );
195     p_filter->p_sys->i_timeout = (mtime_t)(val.i_int * 1000000) >> 2; 
196     var_Create( p_this, OSD_CFG "update", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
197     var_Get( p_this, OSD_CFG "update", &val );
198     p_filter->p_sys->i_update = (mtime_t)(val.i_int * 1000); /* in micro seconds */
199
200     /* Load the osd menu subsystem */
201     p_filter->p_sys->p_menu = osd_MenuCreate( p_this, p_filter->p_sys->psz_file );
202     if( p_filter->p_sys->p_menu == NULL )
203         goto error;
204
205     /* Check if menu position was overridden */
206     p_filter->p_sys->b_absolute = VLC_TRUE;
207     if( i_posx < 0 || i_posy < 0)
208     {
209         p_filter->p_sys->b_absolute = VLC_FALSE;
210         p_filter->p_sys->p_menu->i_x = 0;
211         p_filter->p_sys->p_menu->i_y = 0;
212     }
213     else if( i_posx >= 0 || i_posy >= 0 )
214     {
215         p_filter->p_sys->p_menu->i_x = i_posx;
216         p_filter->p_sys->p_menu->i_y = i_posy;
217     }
218     else if( p_filter->p_sys->p_menu->i_x < 0 || p_filter->p_sys->p_menu->i_y < 0 )
219     {
220         p_filter->p_sys->b_absolute = VLC_FALSE;
221         p_filter->p_sys->p_menu->i_x = 0;
222         p_filter->p_sys->p_menu->i_y = 0;
223     }
224
225     /* Set up p_filter */
226     p_filter->p_sys->i_last_date = mdate();
227
228     /* Keep track of OSD Events */
229     p_filter->p_sys->b_update  = VLC_FALSE;
230     p_filter->p_sys->b_visible = VLC_FALSE;
231
232     var_AddCallback( p_filter->p_sys->p_menu, "osd-menu-update", OSDMenuUpdateEvent, p_filter );        
233     var_AddCallback( p_filter->p_sys->p_menu, "osd-menu-visible", OSDMenuVisibleEvent, p_filter );        
234
235     /* Attach subpicture filter callback */
236     p_filter->pf_sub_filter = Filter;
237
238     es_format_Init( &p_filter->fmt_out, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
239     p_filter->fmt_out.i_priority = 0;
240
241     msg_Dbg( p_filter, "successfully loaded osdmenu filter" );    
242     return VLC_SUCCESS;
243
244 error:
245     msg_Err( p_filter, "osdmenu filter discarded" );
246     vlc_mutex_destroy( &p_filter->p_sys->lock );
247     if( p_filter->p_sys->p_menu )
248     {
249         osd_MenuDelete( p_this, p_filter->p_sys->p_menu );
250         p_filter->p_sys->p_menu = NULL;
251     }
252     if( p_filter->p_sys->psz_file ) free( p_filter->p_sys->psz_file );
253     if( p_filter->p_sys ) free( p_filter->p_sys );
254     return VLC_EGENERIC;    
255 }
256
257 /*****************************************************************************
258  * DestroyFilter: Make a clean exit of this plugin
259  *****************************************************************************/
260 static void DestroyFilter( vlc_object_t *p_this )
261 {
262     filter_t     *p_filter = (filter_t*)p_this;
263     filter_sys_t *p_sys = p_filter->p_sys;
264
265     var_Destroy( p_this, OSD_CFG "file" );
266     var_Destroy( p_this, OSD_CFG "x" );
267     var_Destroy( p_this, OSD_CFG "y" );
268     var_Destroy( p_this, OSD_CFG "position" );
269     var_Destroy( p_this, OSD_CFG "timeout" );
270     var_Destroy( p_this, OSD_CFG "update" );
271
272     var_DelCallback( p_sys->p_menu, "osd-menu-update", OSDMenuUpdateEvent, p_filter );
273     var_DelCallback( p_sys->p_menu, "osd-menu-visible", OSDMenuVisibleEvent, p_filter );
274
275     osd_MenuDelete( p_filter, p_sys->p_menu );
276
277     vlc_mutex_destroy( &p_filter->p_sys->lock );
278     if( p_sys->psz_file) free( p_sys->psz_file );
279     if( p_sys ) free( p_sys );
280
281     msg_Dbg( p_filter, "osdmenu filter destroyed" );
282 }
283
284 /*****************************************************************************
285  * OSDMenuEvent: callback for OSD Menu events
286  *****************************************************************************/
287 static int OSDMenuVisibleEvent( vlc_object_t *p_this, char const *psz_var,
288                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
289 {
290     filter_t *p_filter = (filter_t *) p_data;
291
292     p_filter->p_sys->b_visible = VLC_TRUE;
293     return VLC_SUCCESS;
294 }
295
296 static int OSDMenuUpdateEvent( vlc_object_t *p_this, char const *psz_var,
297                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
298 {
299     filter_t *p_filter = (filter_t *) p_data;
300
301     p_filter->p_sys->b_update = VLC_TRUE;
302     p_filter->p_sys->i_end_date = (mtime_t) 0;
303     return VLC_SUCCESS;
304 }
305
306 #if 0
307 /*****************************************************************************
308  * create_text_region : compose a text region SPU
309  *****************************************************************************/
310 static subpicture_region_t *create_text_region( filter_t *p_filter, subpicture_t *p_spu, 
311     int i_width, int i_height, const char *psz_text )
312 {
313     subpicture_region_t *p_region;
314     video_format_t       fmt;
315
316     /* Create new SPU region */
317     memset( &fmt, 0, sizeof(video_format_t) );
318     fmt.i_chroma = VLC_FOURCC( 'T','E','X','T' );
319     fmt.i_aspect = VOUT_ASPECT_FACTOR;
320     fmt.i_sar_num = fmt.i_sar_den = 1;
321     fmt.i_width = fmt.i_visible_width = i_width;
322     fmt.i_height = fmt.i_visible_height = i_height;
323     fmt.i_x_offset = fmt.i_y_offset = 0;
324     p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
325     if( !p_region )
326     {
327         msg_Err( p_filter, "cannot allocate another SPU region" );
328         return NULL;
329     }
330     p_region->psz_text = strdup( psz_text );
331     p_region->i_x = 0; 
332     p_region->i_y = 40;
333 #if 1
334     msg_Dbg( p_filter, "SPU text region position (%d,%d) (%d,%d) [%s]", 
335         p_region->i_x, p_region->i_y, 
336         p_region->fmt.i_width, p_region->fmt.i_height, p_region->psz_text );
337 #endif
338     return p_region;
339 }
340 #endif
341
342 /*****************************************************************************
343  * create_picture_region : compose a picture region SPU
344  *****************************************************************************/
345 static subpicture_region_t *create_picture_region( filter_t *p_filter, subpicture_t *p_spu,
346     int i_width, int i_height, picture_t *p_pic )
347 {
348     subpicture_region_t *p_region;
349     video_format_t       fmt;
350
351     if( !p_spu ) return NULL;
352
353     /* Create new SPU region */
354     memset( &fmt, 0, sizeof(video_format_t) );
355     fmt.i_chroma = (p_pic == NULL) ? VLC_FOURCC('Y','U','V','P') : VLC_FOURCC('Y','U','V','A');
356     fmt.i_aspect = VOUT_ASPECT_FACTOR;
357     fmt.i_sar_num = fmt.i_sar_den = 1;
358     fmt.i_width = fmt.i_visible_width = i_width;
359     fmt.i_height = fmt.i_visible_height = i_height;
360     fmt.i_x_offset = fmt.i_y_offset = 0;
361     p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
362     if( !p_region )
363     {
364         msg_Err( p_filter, "cannot allocate SPU region" );
365         p_filter->pf_sub_buffer_del( p_filter, p_spu );
366         return NULL;
367     }
368     if( !p_pic && ( fmt.i_chroma == VLC_FOURCC('Y','U','V','P') ) )
369     {
370         p_region->fmt.p_palette->i_entries = 0;
371         p_region->fmt.i_width = p_region->fmt.i_visible_width = 0;
372         p_region->fmt.i_height = p_region->fmt.i_visible_height = 0;
373     }
374     if( p_pic != NULL )
375         vout_CopyPicture( p_filter, &p_region->picture, p_pic );
376
377     p_region->i_x = 0;
378     p_region->i_y = 0;
379 #if 0
380     msg_Dbg( p_filter, "SPU picture region position (%d,%d) (%d,%d) [%p]",
381         p_region->i_x, p_region->i_y,
382         p_region->fmt.i_width, p_region->fmt.i_height, p_pic );
383 #endif
384     return p_region;
385 }
386
387 /****************************************************************************
388  * Filter: the whole thing
389  ****************************************************************************
390  * This function outputs subpictures at regular time intervals.
391  ****************************************************************************/
392 static subpicture_t *Filter( filter_t *p_filter, mtime_t i_date )
393 {
394     filter_sys_t *p_sys = p_filter->p_sys;
395     subpicture_t *p_spu;
396     subpicture_region_t *p_region;
397
398     if( !p_sys->b_update )
399             return NULL;
400             
401     /* Am I too early? */
402     if( ( ( p_sys->i_last_date + p_sys->i_update ) > i_date ) &&
403         ( p_sys->i_end_date > 0 ) )
404         return NULL; /* we are too early, so wait */
405     
406     /* Allocate the subpicture internal data. */
407     p_spu = p_filter->pf_sub_buffer_new( p_filter );
408     if( !p_spu ) return NULL;
409     p_spu->b_ephemer = VLC_TRUE;
410     p_spu->b_fade = VLC_TRUE;    
411     p_spu->b_absolute = p_sys->b_absolute;
412     p_spu->i_flags = p_sys->position;
413
414     /* Determine the duration of the subpicture */
415     if( p_sys->i_end_date > 0 )
416     {
417         /* Display the subpicture again. */
418         p_spu->i_stop = p_sys->i_end_date - i_date;
419         if( ( i_date + p_sys->i_update ) >= p_sys->i_end_date )
420             p_sys->b_update = VLC_FALSE;
421     }
422     else
423     {
424         /* There is a new OSD picture to display */
425         p_spu->i_stop = i_date + p_sys->i_timeout;
426         p_sys->i_end_date = p_spu->i_stop;
427     }
428     
429     p_sys->i_last_date = i_date;
430     p_spu->i_start = p_sys->i_last_date = i_date;
431
432     /* Send an empty subpicture to clear the display
433      * when OSD menu should be hidden and menu picture is not allocated.
434      */
435     if( !p_filter->p_sys->p_menu->p_state->p_pic ||
436         ( p_filter->p_sys->b_visible == VLC_FALSE ) )
437     {
438         /* Create new spu regions and allocate an empty picture in it. */
439         p_region = create_picture_region( p_filter, p_spu,
440             p_filter->p_sys->p_menu->p_state->i_width,
441             p_filter->p_sys->p_menu->p_state->i_height,
442             NULL );
443
444         /* proper positioning of OSD menu image */
445         p_spu->i_x = p_filter->p_sys->p_menu->p_state->i_x;
446         p_spu->i_y = p_filter->p_sys->p_menu->p_state->i_y;
447         p_spu->p_region = p_region;
448         p_spu->i_alpha = 0xFF; /* Picture is completely transparent. */
449         return p_spu;
450     }
451
452     /* Create new spu regions */
453     p_region = create_picture_region( p_filter, p_spu,
454         p_filter->p_sys->p_menu->p_state->i_width,
455         p_filter->p_sys->p_menu->p_state->i_height,
456         p_filter->p_sys->p_menu->p_state->p_pic );
457 #if 0
458     p_region->p_next = create_text_region( p_filter, p_spu,
459         p_filter->p_sys->p_menu->p_state->i_width, p_filter->p_sys->p_menu->p_state->i_height,
460         p_filter->p_sys->p_menu->p_state->p_visible->psz_action );
461 #endif
462
463     /* proper positioning of OSD menu image */
464     p_spu->i_x = p_filter->p_sys->p_menu->p_state->i_x;
465     p_spu->i_y = p_filter->p_sys->p_menu->p_state->i_y;
466     p_spu->p_region = p_region;
467     return p_spu;
468 }