]> git.sesse.net Git - vlc/blob - modules/video_filter/marq.c
freetype and rc extensions. i_font_color and i_font_opacity added to subpictures...
[vlc] / modules / video_filter / marq.c
1 /*****************************************************************************\r
2  * marq.c : marquee display video plugin for vlc\r
3  *****************************************************************************\r
4  * Copyright (C) 2003-2005 VideoLAN\r
5  * $Id: time.c 8751 2004-09-20 21:51:41Z gbazin $\r
6  *\r
7  * Authors: Mark Moriarty\r
8  *\r
9  * This program is free software; you can redistribute it and/or modify\r
10  * it under the terms of the GNU General Public License as published by\r
11  * the Free Software Foundation; either version 2 of the License, or\r
12  * (at your option) any later version.\r
13  *\r
14  * This program is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU General Public License for more details.\r
18  *\r
19  * You should have received a copy of the GNU General Public License\r
20  * along with this program; if not, write to the Free Software\r
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.\r
22  *****************************************************************************/\r
23 \r
24 /*****************************************************************************\r
25  * Preamble\r
26  *****************************************************************************/\r
27 #include <stdlib.h>                                      /* malloc(), free() */\r
28 #include <string.h>\r
29 \r
30 #include <vlc/vlc.h>\r
31 #include <vlc/vout.h>\r
32 \r
33 #include "vlc_filter.h"\r
34 #include "vlc_block.h"\r
35 #include "osd.h"\r
36 \r
37 /*****************************************************************************\r
38  * Local prototypes\r
39  *****************************************************************************/\r
40 static int  CreateFilter ( vlc_object_t * );\r
41 static void DestroyFilter( vlc_object_t * );\r
42 static subpicture_t *Filter( filter_t *, mtime_t );\r
43 \r
44 \r
45 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,\r
46                             vlc_value_t oldval, vlc_value_t newval,\r
47                             void *p_data );\r
48 static int pi_color_values[] = { 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0, \r
49                0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00, \r
50                0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080, \r
51                0x00000080, 0x000000FF, 0x0000FFFF}; \r
52 static char *ppsz_color_descriptions[] = { N_("Default"), N_("Black"), \r
53                N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),\r
54                N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"), \r
55                N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"), \r
56                N_("Aqua") };\r
57 \r
58 /*****************************************************************************\r
59  * filter_sys_t: marquee filter descriptor\r
60  *****************************************************************************/\r
61 struct filter_sys_t\r
62 {\r
63     int i_xoff, i_yoff;  /* offsets for the display string in the video window */\r
64     int i_pos; /* permit relative positioning (top, bottom, left, right, center) */\r
65     int i_timeout;\r
66 \r
67     char *psz_marquee;    /* marquee string */\r
68 \r
69     int  i_font_color, i_font_opacity; /* font color control */\r
70     \r
71     time_t last_time;\r
72     vlc_bool_t b_absolute; /* position control, relative vs. absolute */\r
73 \r
74     vlc_bool_t b_need_update;\r
75 };\r
76 \r
77 #define MSG_TEXT N_("Marquee text")\r
78 #define MSG_LONGTEXT N_("Marquee text to display")\r
79 #define POSX_TEXT N_("X offset, from left")\r
80 #define POSX_LONGTEXT N_("X offset, from the left screen edge" )\r
81 #define POSY_TEXT N_("Y offset, from the top")\r
82 #define POSY_LONGTEXT N_("Y offset, down from the top" )\r
83 #define TIMEOUT_TEXT N_("Marquee timeout")\r
84 #define TIMEOUT_LONGTEXT N_("Defines the time the marquee must remain " \\r
85                             "displayed, in milliseconds. Default value is " \\r
86                             "0 (remain forever).")\r
87 #define OPACITY_TEXT N_("Opacity, -1..255")\r
88 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of overlay text. " \\r
89     "-1 = use freetype-opacity, 0 = transparent, 255 = totally opaque. " )\r
90 #define COLOR_TEXT N_("Text Default Color")\r
91 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, hexadecimal." \\r
92     "-1 = use freetype-color, #000000 = all colors off, " \\r
93     "0xFF0000 = just Red, 0xFFFFFF = all color on [White]" )\r
94 \r
95 #define POS_TEXT N_("Marquee position")\r
96 #define POS_LONGTEXT N_( \\r
97   "You can enforce the marquee position on the video " \\r
98   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \\r
99   "also use combinations of these values by adding them).")\r
100 \r
101 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };\r
102 static char *ppsz_pos_descriptions[] =\r
103      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),\r
104      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };\r
105 \r
106 /*****************************************************************************\r
107  * Module descriptor\r
108  *****************************************************************************/\r
109 vlc_module_begin();\r
110     set_capability( "sub filter", 0 );\r
111     set_shortname( N_("Marquee" ));\r
112     set_callbacks( CreateFilter, DestroyFilter );\r
113     set_category( CAT_VIDEO );\r
114     set_subcategory( SUBCAT_VIDEO_SUBPIC );\r
115     add_string( "marq-marquee", "Marquee", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );\r
116     add_integer( "marq-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );\r
117     add_integer( "marq-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );\r
118     add_integer( "marq-timeout", 0, NULL, TIMEOUT_TEXT, TIMEOUT_LONGTEXT,\r
119                  VLC_FALSE );\r
120     add_integer( "marq-position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );\r
121     /* 5 sets the default to top [1] left [4] */\r
122     change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );\r
123     add_integer_with_range( "marq-opacity", -1, -1, 255, NULL,\r
124         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );\r
125     add_integer( "marq-color", -1, NULL, COLOR_TEXT, COLOR_LONGTEXT, VLC_TRUE );\r
126         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );\r
127 \r
128     set_description( _("Marquee display sub filter") );\r
129     add_shortcut( "marq" );\r
130 vlc_module_end();\r
131 \r
132 /*****************************************************************************\r
133  * CreateFilter: allocates marquee video filter\r
134  *****************************************************************************/\r
135 static int CreateFilter( vlc_object_t *p_this )\r
136 {\r
137     filter_t *p_filter = (filter_t *)p_this;\r
138     filter_sys_t *p_sys;\r
139     vlc_object_t *p_input;\r
140     vlc_value_t     val;\r
141 \r
142     /* Allocate structure */\r
143     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );\r
144     if( p_sys == NULL )\r
145     {\r
146         msg_Err( p_filter, "out of memory" );\r
147         return VLC_ENOMEM;\r
148     }\r
149 \r
150     /* Hook used for callback variables */\r
151     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );\r
152     if( !p_input )\r
153     {\r
154         return VLC_ENOOBJ;\r
155     }\r
156 \r
157     p_sys->i_xoff = var_CreateGetInteger( p_input->p_libvlc , "marq-x" );\r
158     p_sys->i_yoff = var_CreateGetInteger( p_input->p_libvlc , "marq-y" );\r
159     p_sys->i_timeout = var_CreateGetInteger( p_input->p_libvlc , "marq-timeout" );\r
160     p_sys->i_pos = var_CreateGetInteger( p_input->p_libvlc , "marq-position" );\r
161     p_sys->psz_marquee =  var_CreateGetString( p_input->p_libvlc, "marq-marquee" );\r
162     var_Create( p_input->p_libvlc, "marq-opacity", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );\r
163     p_sys->i_font_opacity = var_CreateGetInteger( p_input->p_libvlc , "marq-opacity" );\r
164     p_sys->i_font_color = var_CreateGetInteger( p_input->p_libvlc , "marq-color" );\r
165 \r
166     var_AddCallback( p_input->p_libvlc, "marq-x", MarqueeCallback, p_sys );\r
167     var_AddCallback( p_input->p_libvlc, "marq-y", MarqueeCallback, p_sys );\r
168     var_AddCallback( p_input->p_libvlc, "marq-marquee", MarqueeCallback, p_sys );\r
169     var_AddCallback( p_input->p_libvlc, "marq-timeout", MarqueeCallback, p_sys );\r
170     var_AddCallback( p_input->p_libvlc, "marq-position", MarqueeCallback, p_sys );\r
171     var_AddCallback( p_input->p_libvlc, "marq-color", MarqueeCallback, p_sys );\r
172     var_AddCallback( p_input->p_libvlc, "marq-opacity", MarqueeCallback, p_sys );\r
173 \r
174     vlc_object_release( p_input );\r
175 \r
176 \r
177     /* Misc init */\r
178     p_filter->pf_sub_filter = Filter;\r
179     p_sys->last_time = ((time_t)-1);\r
180     p_sys->b_need_update = VLC_TRUE;\r
181 \r
182     return VLC_SUCCESS;\r
183 }\r
184 /*****************************************************************************\r
185  * DestroyFilter: destroy marquee video filter\r
186  *****************************************************************************/\r
187 static void DestroyFilter( vlc_object_t *p_this )\r
188 {\r
189     filter_t *p_filter = (filter_t *)p_this;\r
190     filter_sys_t *p_sys = p_filter->p_sys;\r
191     vlc_object_t *p_input;\r
192 \r
193     if( p_sys->psz_marquee ) free( p_sys->psz_marquee );\r
194     free( p_sys );\r
195 \r
196     /* Delete the marquee variables */\r
197     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );\r
198     if( !p_input )\r
199     {\r
200         return;\r
201     }\r
202     var_Destroy( p_input->p_libvlc , "marq-marquee" );\r
203     var_Destroy( p_input->p_libvlc , "marq-x" );\r
204     var_Destroy( p_input->p_libvlc , "marq-y" );\r
205     var_Destroy( p_input->p_libvlc , "marq-timeout" );\r
206     var_Destroy( p_input->p_libvlc , "marq-position" );\r
207     var_Destroy( p_input->p_libvlc , "marq-color");\r
208     var_Destroy( p_input->p_libvlc , "marq-opacity");\r
209     vlc_object_release( p_input );\r
210 }\r
211 \r
212 /****************************************************************************\r
213  * Filter: the whole thing\r
214  ****************************************************************************\r
215  * This function outputs subpictures at regular time intervals.\r
216  ****************************************************************************/\r
217 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )\r
218 {\r
219     filter_sys_t *p_sys = p_filter->p_sys;\r
220     subpicture_t *p_spu;\r
221     video_format_t fmt;\r
222     time_t t;\r
223 \r
224     if( p_sys->last_time == time( NULL ) )\r
225     {\r
226         return NULL;\r
227     }\r
228 \r
229     if( p_sys->b_need_update == VLC_FALSE )\r
230     {\r
231         return NULL;\r
232     }\r
233     \r
234     p_sys->b_absolute = VLC_TRUE;\r
235     if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )\r
236     {\r
237         p_sys->b_absolute = VLC_FALSE;\r
238     }\r
239 \r
240     p_spu = p_filter->pf_sub_buffer_new( p_filter );\r
241     if( !p_spu ) return NULL;\r
242 \r
243     p_spu->b_absolute = p_sys->b_absolute;\r
244     memset( &fmt, 0, sizeof(video_format_t) );\r
245     fmt.i_chroma = VLC_FOURCC('T','E','X','T');\r
246     fmt.i_aspect = 0;\r
247     fmt.i_width = fmt.i_height = 0;\r
248     fmt.i_x_offset = 0;\r
249     fmt.i_y_offset = 0;\r
250     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );\r
251     if( !p_spu->p_region )\r
252     {\r
253         p_filter->pf_sub_buffer_del( p_filter, p_spu );\r
254         return NULL;\r
255     }\r
256 \r
257     t = p_sys->last_time = time( NULL );\r
258 \r
259     p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);\r
260     p_spu->i_start = date;\r
261     p_spu->i_stop  = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;\r
262     p_spu->b_ephemer = VLC_TRUE;\r
263     p_spu->i_x = p_sys->i_xoff;\r
264     p_spu->i_y = p_sys->i_yoff;\r
265     p_spu->p_region->i_font_color = p_sys->i_font_color;\r
266     p_spu->p_region->i_font_opacity = p_sys->i_font_opacity;;\r
267     \r
268     p_spu->i_flags = p_sys->i_pos;\r
269 \r
270     p_sys->b_need_update = VLC_FALSE;\r
271     return p_spu;\r
272 }\r
273 \r
274 /**********************************************************************\r
275  * Callback to update params on the fly\r
276  **********************************************************************/\r
277 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,\r
278                             vlc_value_t oldval, vlc_value_t newval,\r
279                             void *p_data )\r
280 {\r
281     filter_sys_t *p_sys = (filter_sys_t *) p_data;\r
282 \r
283     if( !strncmp( psz_var, "marq-marquee", 7 ) )\r
284     {\r
285         if( p_sys->psz_marquee ) free( p_sys->psz_marquee );\r
286         p_sys->psz_marquee = strdup( newval.psz_string );\r
287     }\r
288     else if ( !strncmp( psz_var, "marq-x", 6 ) )\r
289     {\r
290         p_sys->i_xoff = newval.i_int;\r
291     }\r
292     else if ( !strncmp( psz_var, "marq-y", 6 ) )\r
293     {\r
294         p_sys->i_yoff = newval.i_int;\r
295     }\r
296     else if ( !strncmp( psz_var, "marq-color", 8 ) )  /* "marq-col" */ \r
297     {\r
298         p_sys->i_font_color = newval.i_int;\r
299     }\r
300     else if ( !strncmp( psz_var, "marq-opacity", 8 ) ) /* "marq-opa" */ \r
301     {\r
302         p_sys->i_font_opacity = newval.i_int;\r
303     }\r
304     else if ( !strncmp( psz_var, "marq-timeout", 12 ) )\r
305     {\r
306         p_sys->i_timeout = newval.i_int;\r
307     }\r
308     else if ( !strncmp( psz_var, "marq-position", 8 ) )\r
309     /* willing to accept a match against marq-pos */\r
310     {\r
311         p_sys->i_pos = newval.i_int;\r
312         p_sys->i_xoff = -1;       /* force to relative positioning */\r
313     }\r
314     p_sys->b_need_update = VLC_TRUE;\r
315     return VLC_SUCCESS;\r
316 }\r