]> git.sesse.net Git - vlc/blob - modules/video_filter/marq.c
Add ranges to font sizes
[vlc] / modules / video_filter / marq.c
1 /*****************************************************************************
2  * marq.c : marquee display video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Mark Moriarty
8  *          Sigmund Augdal Helberg <dnumgis@videolan.org>
9  *          Antoine Cellerier <dionoea . videolan \ org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36
37 #include <vlc_filter.h>
38 #include <vlc_block.h>
39
40 #include <vlc_strings.h>
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  CreateFilter ( vlc_object_t * );
46 static void DestroyFilter( vlc_object_t * );
47 static subpicture_t *Filter( filter_t *, mtime_t );
48
49
50 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
51                             vlc_value_t oldval, vlc_value_t newval,
52                             void *p_data );
53 static const int pi_color_values[] = {
54                0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
55                0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
56                0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
57                0x00000080, 0x000000FF, 0x0000FFFF};
58 static const char *const ppsz_color_descriptions[] = {
59                N_("Default"), N_("Black"), N_("Gray"),
60                N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),
61                N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"),
62                N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"),
63                N_("Aqua") };
64
65 /*****************************************************************************
66  * filter_sys_t: marquee filter descriptor
67  *****************************************************************************/
68 struct filter_sys_t
69 {
70     vlc_mutex_t lock;
71
72     int i_xoff, i_yoff;  /* offsets for the display string in the video window */
73     int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
74     int i_timeout;
75
76     char *psz_marquee;    /* marquee string */
77
78     text_style_t *p_style; /* font control */
79
80     mtime_t last_time;
81     mtime_t i_refresh;
82
83     bool b_need_update;
84 };
85
86 #define MSG_TEXT N_("Text")
87 #define MSG_LONGTEXT N_( \
88     "Marquee text to display. " \
89     "(Available format strings: " \
90     "Time related: %Y = year, %m = month, %d = day, %H = hour, " \
91     "%M = minute, %S = second, ... " \
92     "Meta data related: $a = artist, $b = album, $c = copyright, " \
93     "$d = description, $e = encoded by, $g = genre, " \
94     "$l = language, $n = track num, $p = now playing, " \
95     "$r = rating, $s = subtitles language, $t = title, "\
96     "$u = url, $A = date, " \
97     "$B = audio bitrate (in kb/s), $C = chapter," \
98     "$D = duration, $F = full name with path, $I = title, "\
99     "$L = time left, " \
100     "$N = name, $O = audio language, $P = position (in %), $R = rate, " \
101     "$S = audio sample rate (in kHz), " \
102     "$T = time, $U = publisher, $V = volume, $_ = new line) ")
103 #define POSX_TEXT N_("X offset")
104 #define POSX_LONGTEXT N_("X offset, from the left screen edge." )
105 #define POSY_TEXT N_("Y offset")
106 #define POSY_LONGTEXT N_("Y offset, down from the top." )
107 #define TIMEOUT_TEXT N_("Timeout")
108 #define TIMEOUT_LONGTEXT N_("Number of milliseconds the marquee must remain " \
109                             "displayed. Default value is " \
110                             "0 (remains forever).")
111 #define REFRESH_TEXT N_("Refresh period in ms")
112 #define REFRESH_LONGTEXT N_("Number of milliseconds between string updates. " \
113                             "This is mainly useful when using meta data " \
114                             "or time format string sequences.")
115 #define OPACITY_TEXT N_("Opacity")
116 #define OPACITY_LONGTEXT N_("Opacity (inverse of transparency) of " \
117     "overlayed text. 0 = transparent, 255 = totally opaque. " )
118 #define SIZE_TEXT N_("Font size, pixels")
119 #define SIZE_LONGTEXT N_("Font size, in pixels. Default is -1 (use default " \
120     "font size)." )
121
122 #define COLOR_TEXT N_("Color")
123 #define COLOR_LONGTEXT N_("Color of the text that will be rendered on "\
124     "the video. This must be an hexadecimal (like HTML colors). The first two "\
125     "chars are for red, then green, then blue. #000000 = black, #FF0000 = red,"\
126     " #00FF00 = green, #FFFF00 = yellow (red + green), #FFFFFF = white" )
127
128 #define POS_TEXT N_("Marquee position")
129 #define POS_LONGTEXT N_( \
130   "You can enforce the marquee position on the video " \
131   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
132   "also use combinations of these values, eg 6 = top-right).")
133
134 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
135 static const char *const ppsz_pos_descriptions[] =
136      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
137      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
138
139 #define CFG_PREFIX "marq-"
140
141 #define MARQUEE_HELP N_("Display text above the video")
142
143 /*****************************************************************************
144  * Module descriptor
145  *****************************************************************************/
146 vlc_module_begin ()
147     set_capability( "sub source", 0 )
148     set_shortname( N_("Marquee" ))
149     set_description( N_("Marquee display") )
150     set_help(MARQUEE_HELP)
151     set_callbacks( CreateFilter, DestroyFilter )
152     set_category( CAT_VIDEO )
153     set_subcategory( SUBCAT_VIDEO_SUBPIC )
154     add_string( CFG_PREFIX "marquee", "VLC", MSG_TEXT, MSG_LONGTEXT,
155                 false )
156
157     set_section( N_("Position"), NULL )
158     add_integer( CFG_PREFIX "x", 0, POSX_TEXT, POSX_LONGTEXT, true )
159     add_integer( CFG_PREFIX "y", 0, POSY_TEXT, POSY_LONGTEXT, true )
160     add_integer( CFG_PREFIX "position", -1, POS_TEXT, POS_LONGTEXT, false )
161         change_integer_list( pi_pos_values, ppsz_pos_descriptions )
162
163     set_section( N_("Font"), NULL )
164     /* 5 sets the default to top [1] left [4] */
165     add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255,
166         OPACITY_TEXT, OPACITY_LONGTEXT, false )
167     add_rgb( CFG_PREFIX "color", 0xFFFFFF, COLOR_TEXT, COLOR_LONGTEXT,
168                  false )
169         change_integer_list( pi_color_values, ppsz_color_descriptions )
170     add_integer( CFG_PREFIX "size", -1, SIZE_TEXT, SIZE_LONGTEXT,
171                  false )
172         change_integer_range( -1, 4096)
173
174     set_section( N_("Misc"), NULL )
175     add_integer( CFG_PREFIX "timeout", 0, TIMEOUT_TEXT, TIMEOUT_LONGTEXT,
176                  false )
177     add_integer( CFG_PREFIX "refresh", 1000, REFRESH_TEXT,
178                  REFRESH_LONGTEXT, false )
179
180     add_shortcut( "time" )
181 vlc_module_end ()
182
183 static const char *const ppsz_filter_options[] = {
184     "marquee", "x", "y", "position", "color", "size", "timeout", "refresh",
185     "opacity",
186     NULL
187 };
188
189 /*****************************************************************************
190  * CreateFilter: allocates marquee video filter
191  *****************************************************************************/
192 static int CreateFilter( vlc_object_t *p_this )
193 {
194     filter_t *p_filter = (filter_t *)p_this;
195     filter_sys_t *p_sys;
196
197     /* Allocate structure */
198     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
199     if( p_sys == NULL )
200         return VLC_ENOMEM;
201
202     vlc_mutex_init( &p_sys->lock );
203     p_sys->p_style = text_style_New();
204
205     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
206                        p_filter->p_cfg );
207
208
209 #define CREATE_VAR( stor, type, var ) \
210     p_sys->stor = var_CreateGet##type##Command( p_filter, var ); \
211     var_AddCallback( p_filter, var, MarqueeCallback, p_sys );
212
213     p_sys->b_need_update = true;
214     CREATE_VAR( i_xoff, Integer, "marq-x" );
215     CREATE_VAR( i_yoff, Integer, "marq-y" );
216     CREATE_VAR( i_timeout,Integer, "marq-timeout" );
217     p_sys->i_refresh = 1000 * var_CreateGetIntegerCommand( p_filter,
218                                                            "marq-refresh" );
219     var_AddCallback( p_filter, "marq-refresh", MarqueeCallback, p_sys );
220     CREATE_VAR( i_pos, Integer, "marq-position" );
221     CREATE_VAR( psz_marquee, String, "marq-marquee" );
222     p_sys->p_style->i_font_alpha = var_CreateGetIntegerCommand( p_filter,
223                                                             "marq-opacity" );
224     var_AddCallback( p_filter, "marq-opacity", MarqueeCallback, p_sys );
225     CREATE_VAR( p_style->i_font_color, Integer, "marq-color" );
226     CREATE_VAR( p_style->i_font_size, Integer, "marq-size" );
227
228     /* Misc init */
229     p_filter->pf_sub_source = Filter;
230     p_sys->last_time = 0;
231
232     return VLC_SUCCESS;
233 }
234 /*****************************************************************************
235  * DestroyFilter: destroy marquee video filter
236  *****************************************************************************/
237 static void DestroyFilter( vlc_object_t *p_this )
238 {
239     filter_t *p_filter = (filter_t *)p_this;
240     filter_sys_t *p_sys = p_filter->p_sys;
241
242     /* Delete the marquee variables */
243 #define DEL_VAR(var) \
244     var_DelCallback( p_filter, var, MarqueeCallback, p_sys ); \
245     var_Destroy( p_filter, var );
246     DEL_VAR( "marq-x" );
247     DEL_VAR( "marq-y" );
248     DEL_VAR( "marq-timeout" );
249     DEL_VAR( "marq-refresh" );
250     DEL_VAR( "marq-position" );
251     DEL_VAR( "marq-marquee" );
252     DEL_VAR( "marq-opacity" );
253     DEL_VAR( "marq-color" );
254     DEL_VAR( "marq-size" );
255
256     vlc_mutex_destroy( &p_sys->lock );
257     text_style_Delete( p_sys->p_style );
258     free( p_sys->psz_marquee );
259     free( p_sys );
260 }
261
262 /****************************************************************************
263  * Filter: the whole thing
264  ****************************************************************************
265  * This function outputs subpictures at regular time intervals.
266  ****************************************************************************/
267 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
268 {
269     filter_sys_t *p_sys = p_filter->p_sys;
270     subpicture_t *p_spu = NULL;
271     video_format_t fmt;
272
273     vlc_mutex_lock( &p_sys->lock );
274     if( p_sys->last_time + p_sys->i_refresh > date )
275         goto out;
276     if( !p_sys->b_need_update )
277         goto out;
278
279     p_spu = filter_NewSubpicture( p_filter );
280     if( !p_spu )
281         goto out;
282
283     memset( &fmt, 0, sizeof(video_format_t) );
284     fmt.i_chroma = VLC_CODEC_TEXT;
285     fmt.i_width = fmt.i_height = 0;
286     fmt.i_x_offset = 0;
287     fmt.i_y_offset = 0;
288     p_spu->p_region = subpicture_region_New( &fmt );
289     if( !p_spu->p_region )
290     {
291         p_filter->pf_sub_buffer_del( p_filter, p_spu );
292         p_spu = NULL;
293         goto out;
294     }
295
296     p_sys->last_time = date;
297
298     if( !strchr( p_sys->psz_marquee, '%' )
299      && !strchr( p_sys->psz_marquee, '$' ) )
300         p_sys->b_need_update = false;
301
302     p_spu->p_region->psz_text = str_format( p_filter, p_sys->psz_marquee );
303     p_spu->i_start = date;
304     p_spu->i_stop  = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;
305     p_spu->b_ephemer = true;
306
307     /*  where to locate the string: */
308     if( p_sys->i_pos < 0 )
309     {   /*  set to an absolute xy */
310         p_spu->p_region->i_align = SUBPICTURE_ALIGN_LEFT | SUBPICTURE_ALIGN_TOP;
311         p_spu->b_absolute = true;
312     }
313     else
314     {   /* set to one of the 9 relative locations */
315         p_spu->p_region->i_align = p_sys->i_pos;
316         p_spu->b_absolute = false;
317     }
318
319     p_spu->p_region->i_x = p_sys->i_xoff;
320     p_spu->p_region->i_y = p_sys->i_yoff;
321
322     p_spu->p_region->p_style = text_style_Duplicate( p_sys->p_style );
323
324 out:
325     vlc_mutex_unlock( &p_sys->lock );
326     return p_spu;
327 }
328
329 /**********************************************************************
330  * Callback to update params on the fly
331  **********************************************************************/
332 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
333                             vlc_value_t oldval, vlc_value_t newval,
334                             void *p_data )
335 {
336     filter_sys_t *p_sys = (filter_sys_t *) p_data;
337
338     VLC_UNUSED(oldval);
339     VLC_UNUSED(p_this);
340
341     vlc_mutex_lock( &p_sys->lock );
342     if( !strcmp( psz_var, "marq-marquee" ) )
343     {
344         free( p_sys->psz_marquee );
345         p_sys->psz_marquee = strdup( newval.psz_string );
346     }
347     else if ( !strcmp( psz_var, "marq-x" ) )
348     {
349         p_sys->i_xoff = newval.i_int;
350     }
351     else if ( !strcmp( psz_var, "marq-y" ) )
352     {
353         p_sys->i_yoff = newval.i_int;
354     }
355     else if ( !strcmp( psz_var, "marq-color" ) )
356     {
357         p_sys->p_style->i_font_color = newval.i_int;
358     }
359     else if ( !strcmp( psz_var, "marq-opacity" ) )
360     {
361         p_sys->p_style->i_font_alpha = newval.i_int;
362     }
363     else if ( !strcmp( psz_var, "marq-size" ) )
364     {
365         p_sys->p_style->i_font_size = newval.i_int;
366     }
367     else if ( !strcmp( psz_var, "marq-timeout" ) )
368     {
369         p_sys->i_timeout = newval.i_int;
370     }
371     else if ( !strcmp( psz_var, "marq-refresh" ) )
372     {
373         p_sys->i_refresh = newval.i_int * 1000;
374     }
375     else if ( !strcmp( psz_var, "marq-position" ) )
376     /* willing to accept a match against marq-pos */
377     {
378         p_sys->i_pos = newval.i_int;
379         p_sys->i_xoff = -1;       /* force to relative positioning */
380     }
381     p_sys->b_need_update = true;
382     vlc_mutex_unlock( &p_sys->lock );
383     return VLC_SUCCESS;
384 }