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