A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / video_filter / marq.c
blob550ce7d878ca3fa63bcac99589f6b5e610eb41c8
1 /*****************************************************************************
2 * marq.c : marquee display video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2008 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Mark Moriarty
8 * Sigmund Augdal Helberg <dnumgis@videolan.org>
9 * Antoine Cellerier <dionoea . videolan \ org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <errno.h>
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_filter.h>
39 #include <vlc_block.h>
40 #include <vlc_fs.h>
41 #include <vlc_strings.h>
43 /*****************************************************************************
44 * Local prototypes
45 *****************************************************************************/
46 static int CreateFilter ( vlc_object_t * );
47 static void DestroyFilter( vlc_object_t * );
48 static subpicture_t *Filter( filter_t *, mtime_t );
50 static char *MarqueeReadFile( filter_t *, const char * );
51 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
52 vlc_value_t oldval, vlc_value_t newval,
53 void *p_data );
54 static const int pi_color_values[] = {
55 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
56 0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
57 0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
58 0x00000080, 0x000000FF, 0x0000FFFF};
59 static const char *const ppsz_color_descriptions[] = {
60 N_("Default"), N_("Black"), N_("Gray"),
61 N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),
62 N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"),
63 N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"),
64 N_("Aqua") };
66 /*****************************************************************************
67 * filter_sys_t: marquee filter descriptor
68 *****************************************************************************/
69 struct filter_sys_t
71 vlc_mutex_t lock;
73 int i_xoff, i_yoff; /* offsets for the display string in the video window */
74 int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
75 int i_timeout;
77 char *format; /**< marquee text format */
78 char *filepath; /**< marquee file path */
79 char *message; /**< marquee plain text */
81 text_style_t *p_style; /* font control */
83 mtime_t last_time;
84 mtime_t i_refresh;
87 #define MSG_TEXT N_("Text")
88 #define MSG_LONGTEXT N_( \
89 "Marquee text to display. " \
90 "(Available format strings: " \
91 "%Y = year, %m = month, %d = day, %H = hour, " \
92 "%M = minute, %S = second, ...)" )
93 #define FILE_TEXT N_("Text file")
94 #define FILE_LONGTEXT N_("File to read the marquee text from.")
95 #define POSX_TEXT N_("X offset")
96 #define POSX_LONGTEXT N_("X offset, from the left screen edge." )
97 #define POSY_TEXT N_("Y offset")
98 #define POSY_LONGTEXT N_("Y offset, down from the top." )
99 #define TIMEOUT_TEXT N_("Timeout")
100 #define TIMEOUT_LONGTEXT N_("Number of milliseconds the marquee must remain " \
101 "displayed. Default value is " \
102 "0 (remains forever).")
103 #define REFRESH_TEXT N_("Refresh period in ms")
104 #define REFRESH_LONGTEXT N_("Number of milliseconds between string updates. " \
105 "This is mainly useful when using meta data " \
106 "or time format string sequences.")
107 #define OPACITY_TEXT N_("Opacity")
108 #define OPACITY_LONGTEXT N_("Opacity (inverse of transparency) of " \
109 "overlayed text. 0 = transparent, 255 = totally opaque. " )
110 #define SIZE_TEXT N_("Font size, pixels")
111 #define SIZE_LONGTEXT N_("Font size, in pixels. Default is -1 (use default " \
112 "font size)." )
114 #define COLOR_TEXT N_("Color")
115 #define COLOR_LONGTEXT N_("Color of the text that will be rendered on "\
116 "the video. This must be an hexadecimal (like HTML colors). The first two "\
117 "chars are for red, then green, then blue. #000000 = black, #FF0000 = red,"\
118 " #00FF00 = green, #FFFF00 = yellow (red + green), #FFFFFF = white" )
120 #define POS_TEXT N_("Marquee position")
121 #define POS_LONGTEXT N_( \
122 "You can enforce the marquee position on the video " \
123 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
124 "also use combinations of these values, eg 6 = top-right).")
126 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
127 static const char *const ppsz_pos_descriptions[] =
128 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
129 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
131 #define CFG_PREFIX "marq-"
133 #define MARQUEE_HELP N_("Display text above the video")
135 /*****************************************************************************
136 * Module descriptor
137 *****************************************************************************/
138 vlc_module_begin ()
139 set_capability( "sub source", 0 )
140 set_shortname( N_("Marquee" ))
141 set_description( N_("Marquee display") )
142 set_help(MARQUEE_HELP)
143 set_callbacks( CreateFilter, DestroyFilter )
144 set_category( CAT_VIDEO )
145 set_subcategory( SUBCAT_VIDEO_SUBPIC )
146 add_string( CFG_PREFIX "marquee", "VLC", MSG_TEXT, MSG_LONGTEXT,
147 false )
148 add_loadfile( CFG_PREFIX "file", NULL, FILE_TEXT, FILE_LONGTEXT, true )
150 set_section( N_("Position"), NULL )
151 add_integer( CFG_PREFIX "x", 0, POSX_TEXT, POSX_LONGTEXT, true )
152 add_integer( CFG_PREFIX "y", 0, POSY_TEXT, POSY_LONGTEXT, true )
153 add_integer( CFG_PREFIX "position", -1, POS_TEXT, POS_LONGTEXT, false )
154 change_integer_list( pi_pos_values, ppsz_pos_descriptions )
156 set_section( N_("Font"), NULL )
157 /* 5 sets the default to top [1] left [4] */
158 add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255,
159 OPACITY_TEXT, OPACITY_LONGTEXT, false )
160 add_rgb( CFG_PREFIX "color", 0xFFFFFF, COLOR_TEXT, COLOR_LONGTEXT,
161 false )
162 change_integer_list( pi_color_values, ppsz_color_descriptions )
163 add_integer( CFG_PREFIX "size", -1, SIZE_TEXT, SIZE_LONGTEXT,
164 false )
165 change_integer_range( -1, 4096)
167 set_section( N_("Misc"), NULL )
168 add_integer( CFG_PREFIX "timeout", 0, TIMEOUT_TEXT, TIMEOUT_LONGTEXT,
169 false )
170 add_integer( CFG_PREFIX "refresh", 1000, REFRESH_TEXT,
171 REFRESH_LONGTEXT, false )
173 add_shortcut( "time" )
174 vlc_module_end ()
176 static const char *const ppsz_filter_options[] = {
177 "marquee", "x", "y", "position", "color", "size", "timeout", "refresh",
178 "opacity","file",
179 NULL
182 /*****************************************************************************
183 * CreateFilter: allocates marquee video filter
184 *****************************************************************************/
185 static int CreateFilter( vlc_object_t *p_this )
187 filter_t *p_filter = (filter_t *)p_this;
188 filter_sys_t *p_sys;
190 /* Allocate structure */
191 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
192 if( p_sys == NULL )
193 return VLC_ENOMEM;
195 vlc_mutex_init( &p_sys->lock );
196 p_sys->p_style = text_style_New();
198 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
199 p_filter->p_cfg );
202 #define CREATE_VAR( stor, type, var ) \
203 p_sys->stor = var_CreateGet##type##Command( p_filter, var ); \
204 var_AddCallback( p_filter, var, MarqueeCallback, p_sys );
206 CREATE_VAR( i_xoff, Integer, "marq-x" );
207 CREATE_VAR( i_yoff, Integer, "marq-y" );
208 CREATE_VAR( i_timeout,Integer, "marq-timeout" );
209 p_sys->i_refresh = 1000 * var_CreateGetIntegerCommand( p_filter,
210 "marq-refresh" );
211 var_AddCallback( p_filter, "marq-refresh", MarqueeCallback, p_sys );
212 CREATE_VAR( i_pos, Integer, "marq-position" );
213 CREATE_VAR( format, String, "marq-marquee" );
214 p_sys->filepath = var_InheritString( p_filter, "marq-file" );
215 p_sys->message = NULL;
216 p_sys->p_style->i_font_alpha = var_CreateGetIntegerCommand( p_filter,
217 "marq-opacity" );
218 var_AddCallback( p_filter, "marq-opacity", MarqueeCallback, p_sys );
219 CREATE_VAR( p_style->i_font_color, Integer, "marq-color" );
220 CREATE_VAR( p_style->i_font_size, Integer, "marq-size" );
222 /* Misc init */
223 p_filter->pf_sub_source = Filter;
224 p_sys->last_time = 0;
226 return VLC_SUCCESS;
228 /*****************************************************************************
229 * DestroyFilter: destroy marquee video filter
230 *****************************************************************************/
231 static void DestroyFilter( vlc_object_t *p_this )
233 filter_t *p_filter = (filter_t *)p_this;
234 filter_sys_t *p_sys = p_filter->p_sys;
236 /* Delete the marquee variables */
237 #define DEL_VAR(var) \
238 var_DelCallback( p_filter, var, MarqueeCallback, p_sys ); \
239 var_Destroy( p_filter, var );
240 DEL_VAR( "marq-x" );
241 DEL_VAR( "marq-y" );
242 DEL_VAR( "marq-timeout" );
243 DEL_VAR( "marq-refresh" );
244 DEL_VAR( "marq-position" );
245 DEL_VAR( "marq-marquee" );
246 DEL_VAR( "marq-opacity" );
247 DEL_VAR( "marq-color" );
248 DEL_VAR( "marq-size" );
250 vlc_mutex_destroy( &p_sys->lock );
251 text_style_Delete( p_sys->p_style );
252 free( p_sys->format );
253 free( p_sys->filepath );
254 free( p_sys->message );
255 free( p_sys );
258 /****************************************************************************
259 * Filter: the whole thing
260 ****************************************************************************
261 * This function outputs subpictures at regular time intervals.
262 ****************************************************************************/
263 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
265 filter_sys_t *p_sys = p_filter->p_sys;
266 subpicture_t *p_spu = NULL;
267 video_format_t fmt;
269 vlc_mutex_lock( &p_sys->lock );
270 if( p_sys->last_time + p_sys->i_refresh > date )
271 goto out;
273 if( p_sys->filepath != NULL )
275 char *fmt = MarqueeReadFile( p_filter, p_sys->filepath );
276 if( fmt != NULL )
278 free( p_sys->format );
279 p_sys->format = fmt;
283 char *msg = str_format_time( p_sys->format ? p_sys->format : "" );
284 if( unlikely( msg == NULL ) )
285 goto out;
286 if( p_sys->message != NULL && !strcmp( msg, p_sys->message ) )
288 free( msg );
289 goto out;
291 free( p_sys->message );
292 p_sys->message = msg;
294 p_spu = filter_NewSubpicture( p_filter );
295 if( !p_spu )
296 goto out;
298 memset( &fmt, 0, sizeof(video_format_t) );
299 fmt.i_chroma = VLC_CODEC_TEXT;
300 fmt.i_width = fmt.i_height = 0;
301 fmt.i_x_offset = 0;
302 fmt.i_y_offset = 0;
303 p_spu->p_region = subpicture_region_New( &fmt );
304 if( !p_spu->p_region )
306 filter_DeleteSubpicture( p_filter, p_spu );
307 p_spu = NULL;
308 goto out;
311 p_sys->last_time = date;
313 p_spu->p_region->psz_text = strdup( msg );
314 p_spu->i_start = date;
315 p_spu->i_stop = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;
316 p_spu->b_ephemer = true;
318 /* where to locate the string: */
319 if( p_sys->i_pos < 0 )
320 { /* set to an absolute xy */
321 p_spu->p_region->i_align = SUBPICTURE_ALIGN_LEFT | SUBPICTURE_ALIGN_TOP;
322 p_spu->b_absolute = true;
324 else
325 { /* set to one of the 9 relative locations */
326 p_spu->p_region->i_align = p_sys->i_pos;
327 p_spu->b_absolute = false;
330 p_spu->p_region->i_x = p_sys->i_xoff;
331 p_spu->p_region->i_y = p_sys->i_yoff;
333 p_spu->p_region->p_style = text_style_Duplicate( p_sys->p_style );
335 out:
336 vlc_mutex_unlock( &p_sys->lock );
337 return p_spu;
340 static char *MarqueeReadFile( filter_t *obj, const char *path )
342 FILE *stream = vlc_fopen( path, "rt" );
343 if( stream == NULL )
345 msg_Err( obj, "cannot open %s: %s", path, vlc_strerror_c(errno) );
346 return NULL;
349 char *line = NULL;
351 ssize_t len = getline( &line, &(size_t){ 0 }, stream );
352 if( len == -1 )
354 msg_Err( obj, "cannot read %s: %s", path, vlc_strerror_c(errno) );
355 clearerr( stream );
356 line = NULL;
358 fclose( stream );
360 if( len >= 1 && line[len - 1] == '\n' )
361 line[--len] = '\0';
362 return line;
365 /**********************************************************************
366 * Callback to update params on the fly
367 **********************************************************************/
368 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
369 vlc_value_t oldval, vlc_value_t newval,
370 void *p_data )
372 filter_sys_t *p_sys = (filter_sys_t *) p_data;
374 VLC_UNUSED(oldval);
375 VLC_UNUSED(p_this);
377 vlc_mutex_lock( &p_sys->lock );
378 if( !strcmp( psz_var, "marq-marquee" ) )
380 free( p_sys->format );
381 p_sys->format = strdup( newval.psz_string );
383 else if ( !strcmp( psz_var, "marq-x" ) )
385 p_sys->i_xoff = newval.i_int;
387 else if ( !strcmp( psz_var, "marq-y" ) )
389 p_sys->i_yoff = newval.i_int;
391 else if ( !strcmp( psz_var, "marq-color" ) )
393 p_sys->p_style->i_font_color = newval.i_int;
395 else if ( !strcmp( psz_var, "marq-opacity" ) )
397 p_sys->p_style->i_font_alpha = newval.i_int;
399 else if ( !strcmp( psz_var, "marq-size" ) )
401 p_sys->p_style->i_font_size = newval.i_int;
403 else if ( !strcmp( psz_var, "marq-timeout" ) )
405 p_sys->i_timeout = newval.i_int;
407 else if ( !strcmp( psz_var, "marq-refresh" ) )
409 p_sys->i_refresh = newval.i_int * 1000;
411 else if ( !strcmp( psz_var, "marq-position" ) )
412 /* willing to accept a match against marq-pos */
414 p_sys->i_pos = newval.i_int;
415 p_sys->i_xoff = -1; /* force to relative positioning */
418 free( p_sys->message );
419 p_sys->message = NULL; /* force update */
421 vlc_mutex_unlock( &p_sys->lock );
422 return VLC_SUCCESS;