demux: avi: invalidate skipped chunks
[vlc.git] / modules / spu / marq.c
blob1834e57f55e0ae21ef8ce399d8f282e11a39c23c
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>
42 #include <vlc_subpicture.h>
44 /*****************************************************************************
45 * Local prototypes
46 *****************************************************************************/
47 static int CreateFilter ( vlc_object_t * );
48 static void DestroyFilter( vlc_object_t * );
49 static subpicture_t *Filter( filter_t *, mtime_t );
51 static char *MarqueeReadFile( filter_t *, const char * );
52 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
53 vlc_value_t oldval, vlc_value_t newval,
54 void *p_data );
55 static const int pi_color_values[] = {
56 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
57 0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
58 0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
59 0x00000080, 0x000000FF, 0x0000FFFF};
60 static const char *const ppsz_color_descriptions[] = {
61 N_("Default"), N_("Black"), N_("Gray"),
62 N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),
63 N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"),
64 N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"),
65 N_("Aqua") };
67 /*****************************************************************************
68 * filter_sys_t: marquee filter descriptor
69 *****************************************************************************/
70 struct filter_sys_t
72 vlc_mutex_t lock;
74 int i_xoff, i_yoff; /* offsets for the display string in the video window */
75 int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
76 int i_timeout;
78 char *format; /**< marquee text format */
79 char *filepath; /**< marquee file path */
80 char *message; /**< marquee plain text */
82 text_style_t *p_style; /* font control */
84 mtime_t last_time;
85 mtime_t i_refresh;
88 #define MSG_TEXT N_("Text")
89 #define MSG_LONGTEXT N_( \
90 "Marquee text to display. " \
91 "(Available format strings: " \
92 "%Y = year, %m = month, %d = day, %H = hour, " \
93 "%M = minute, %S = second, ...)" )
94 #define FILE_TEXT N_("Text file")
95 #define FILE_LONGTEXT N_("File to read the marquee text from.")
96 #define POSX_TEXT N_("X offset")
97 #define POSX_LONGTEXT N_("X offset, from the left screen edge." )
98 #define POSY_TEXT N_("Y offset")
99 #define POSY_LONGTEXT N_("Y offset, down from the top." )
100 #define TIMEOUT_TEXT N_("Timeout")
101 #define TIMEOUT_LONGTEXT N_("Number of milliseconds the marquee must remain " \
102 "displayed. Default value is " \
103 "0 (remains forever).")
104 #define REFRESH_TEXT N_("Refresh period in ms")
105 #define REFRESH_LONGTEXT N_("Number of milliseconds between string updates. " \
106 "This is mainly useful when using meta data " \
107 "or time format string sequences.")
108 #define OPACITY_TEXT N_("Opacity")
109 #define OPACITY_LONGTEXT N_("Opacity (inverse of transparency) of " \
110 "overlayed text. 0 = transparent, 255 = totally opaque. " )
111 #define SIZE_TEXT N_("Font size, pixels")
112 #define SIZE_LONGTEXT N_("Font size, in pixels. Default is 0 (use default " \
113 "font size)." )
115 #define COLOR_TEXT N_("Color")
116 #define COLOR_LONGTEXT N_("Color of the text that will be rendered on "\
117 "the video. This must be an hexadecimal (like HTML colors). The first two "\
118 "chars are for red, then green, then blue. #000000 = black, #FF0000 = red,"\
119 " #00FF00 = green, #FFFF00 = yellow (red + green), #FFFFFF = white" )
121 #define POS_TEXT N_("Marquee position")
122 #define POS_LONGTEXT N_( \
123 "You can enforce the marquee position on the video " \
124 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
125 "also use combinations of these values, eg 6 = top-right).")
127 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
128 static const char *const ppsz_pos_descriptions[] =
129 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
130 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
132 #define CFG_PREFIX "marq-"
134 #define MARQUEE_HELP N_("Display text above the video")
136 /*****************************************************************************
137 * Module descriptor
138 *****************************************************************************/
139 vlc_module_begin ()
140 set_capability( "sub source", 0 )
141 set_shortname( N_("Marquee" ))
142 set_description( N_("Marquee display") )
143 set_help(MARQUEE_HELP)
144 set_callbacks( CreateFilter, DestroyFilter )
145 set_category( CAT_VIDEO )
146 set_subcategory( SUBCAT_VIDEO_SUBPIC )
147 add_string( CFG_PREFIX "marquee", "VLC", MSG_TEXT, MSG_LONGTEXT,
148 false )
149 add_loadfile( CFG_PREFIX "file", NULL, FILE_TEXT, FILE_LONGTEXT, true )
151 set_section( N_("Position"), NULL )
152 add_integer( CFG_PREFIX "x", 0, POSX_TEXT, POSX_LONGTEXT, true )
153 add_integer( CFG_PREFIX "y", 0, POSY_TEXT, POSY_LONGTEXT, true )
154 add_integer( CFG_PREFIX "position", -1, POS_TEXT, POS_LONGTEXT, false )
155 change_integer_list( pi_pos_values, ppsz_pos_descriptions )
157 set_section( N_("Font"), NULL )
158 /* 5 sets the default to top [1] left [4] */
159 add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255,
160 OPACITY_TEXT, OPACITY_LONGTEXT, false )
161 add_rgb( CFG_PREFIX "color", 0xFFFFFF, COLOR_TEXT, COLOR_LONGTEXT,
162 false )
163 change_integer_list( pi_color_values, ppsz_color_descriptions )
164 add_integer( CFG_PREFIX "size", 0, SIZE_TEXT, SIZE_LONGTEXT,
165 false )
166 change_integer_range( 0, 4096)
168 set_section( N_("Misc"), NULL )
169 add_integer( CFG_PREFIX "timeout", 0, TIMEOUT_TEXT, TIMEOUT_LONGTEXT,
170 false )
171 add_integer( CFG_PREFIX "refresh", 1000, REFRESH_TEXT,
172 REFRESH_LONGTEXT, false )
174 add_shortcut( "time" )
175 vlc_module_end ()
177 static const char *const ppsz_filter_options[] = {
178 "marquee", "x", "y", "position", "color", "size", "timeout", "refresh",
179 "opacity","file",
180 NULL
183 /*****************************************************************************
184 * CreateFilter: allocates marquee video filter
185 *****************************************************************************/
186 static int CreateFilter( vlc_object_t *p_this )
188 filter_t *p_filter = (filter_t *)p_this;
189 filter_sys_t *p_sys;
191 /* Allocate structure */
192 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
193 if( p_sys == NULL )
194 return VLC_ENOMEM;
196 p_sys->p_style = text_style_Create( STYLE_NO_DEFAULTS );
197 if(unlikely(!p_sys->p_style))
199 free(p_sys);
200 return VLC_ENOMEM;
202 vlc_mutex_init( &p_sys->lock );
204 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
205 p_filter->p_cfg );
208 #define CREATE_VAR( stor, type, var ) \
209 p_sys->stor = var_CreateGet##type##Command( p_filter, var ); \
210 var_AddCallback( p_filter, var, MarqueeCallback, p_sys );
212 CREATE_VAR( i_xoff, Integer, "marq-x" );
213 CREATE_VAR( i_yoff, Integer, "marq-y" );
214 CREATE_VAR( i_timeout,Integer, "marq-timeout" );
215 p_sys->i_refresh = 1000 * var_CreateGetIntegerCommand( p_filter,
216 "marq-refresh" );
217 var_AddCallback( p_filter, "marq-refresh", MarqueeCallback, p_sys );
218 CREATE_VAR( i_pos, Integer, "marq-position" );
219 CREATE_VAR( format, String, "marq-marquee" );
220 p_sys->filepath = var_InheritString( p_filter, "marq-file" );
221 p_sys->message = NULL;
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 p_sys->p_style->i_features |= STYLE_HAS_FONT_ALPHA;
226 CREATE_VAR( p_style->i_font_color, Integer, "marq-color" );
227 p_sys->p_style->i_features |= STYLE_HAS_FONT_COLOR;
228 CREATE_VAR( p_style->i_font_size, Integer, "marq-size" );
230 /* Misc init */
231 p_filter->pf_sub_source = Filter;
232 p_sys->last_time = 0;
234 return VLC_SUCCESS;
236 /*****************************************************************************
237 * DestroyFilter: destroy marquee video filter
238 *****************************************************************************/
239 static void DestroyFilter( vlc_object_t *p_this )
241 filter_t *p_filter = (filter_t *)p_this;
242 filter_sys_t *p_sys = p_filter->p_sys;
244 /* Delete the marquee variables */
245 #define DEL_VAR(var) \
246 var_DelCallback( p_filter, var, MarqueeCallback, p_sys ); \
247 var_Destroy( p_filter, var );
248 DEL_VAR( "marq-x" );
249 DEL_VAR( "marq-y" );
250 DEL_VAR( "marq-timeout" );
251 DEL_VAR( "marq-refresh" );
252 DEL_VAR( "marq-position" );
253 DEL_VAR( "marq-marquee" );
254 DEL_VAR( "marq-opacity" );
255 DEL_VAR( "marq-color" );
256 DEL_VAR( "marq-size" );
258 vlc_mutex_destroy( &p_sys->lock );
259 text_style_Delete( p_sys->p_style );
260 free( p_sys->format );
261 free( p_sys->filepath );
262 free( p_sys->message );
263 free( p_sys );
266 /****************************************************************************
267 * Filter: the whole thing
268 ****************************************************************************
269 * This function outputs subpictures at regular time intervals.
270 ****************************************************************************/
271 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
273 filter_sys_t *p_sys = p_filter->p_sys;
274 subpicture_t *p_spu = NULL;
276 vlc_mutex_lock( &p_sys->lock );
277 if( p_sys->last_time + p_sys->i_refresh > date )
278 goto out;
280 if( p_sys->filepath != NULL )
282 char *fmt = MarqueeReadFile( p_filter, p_sys->filepath );
283 if( fmt != NULL )
285 free( p_sys->format );
286 p_sys->format = fmt;
290 char *msg = vlc_strftime( p_sys->format ? p_sys->format : "" );
291 if( unlikely( msg == NULL ) )
292 goto out;
293 if( p_sys->message != NULL && !strcmp( msg, p_sys->message ) )
295 free( msg );
296 goto out;
298 free( p_sys->message );
299 p_sys->message = msg;
301 p_spu = filter_NewSubpicture( p_filter );
302 if( !p_spu )
303 goto out;
305 video_format_t vfmt;
306 video_format_Init( &vfmt, VLC_CODEC_TEXT );
307 vfmt.i_sar_den = vfmt.i_sar_num = 1;
308 p_spu->p_region = subpicture_region_New( &vfmt );
309 if( !p_spu->p_region )
311 subpicture_Delete( p_spu );
312 p_spu = NULL;
313 goto out;
316 p_sys->last_time = date;
318 p_spu->p_region->p_text = text_segment_New( msg );
319 p_spu->i_start = date;
320 p_spu->i_stop = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;
321 p_spu->b_ephemer = true;
323 /* where to locate the string: */
324 if( p_sys->i_pos < 0 )
325 { /* set to an absolute xy */
326 p_spu->p_region->i_align = SUBPICTURE_ALIGN_LEFT | SUBPICTURE_ALIGN_TOP;
327 p_spu->b_absolute = true;
329 else
330 { /* set to one of the 9 relative locations */
331 p_spu->p_region->i_align = p_sys->i_pos;
332 p_spu->b_absolute = false;
335 p_spu->p_region->i_x = p_sys->i_xoff;
336 p_spu->p_region->i_y = p_sys->i_yoff;
338 p_spu->p_region->p_text->style = text_style_Duplicate( p_sys->p_style );
340 out:
341 vlc_mutex_unlock( &p_sys->lock );
342 return p_spu;
345 static char *MarqueeReadFile( filter_t *obj, const char *path )
347 FILE *stream = vlc_fopen( path, "rt" );
348 if( stream == NULL )
350 msg_Err( obj, "cannot open %s: %s", path, vlc_strerror_c(errno) );
351 return NULL;
354 char *line = NULL;
356 ssize_t len = getline( &line, &(size_t){ 0 }, stream );
357 if( len == -1 )
359 msg_Err( obj, "cannot read %s: %s", path, vlc_strerror_c(errno) );
360 clearerr( stream );
361 line = NULL;
363 fclose( stream );
365 if( len >= 1 && line[len - 1] == '\n' )
366 line[--len] = '\0';
367 return line;
370 /**********************************************************************
371 * Callback to update params on the fly
372 **********************************************************************/
373 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
374 vlc_value_t oldval, vlc_value_t newval,
375 void *p_data )
377 filter_sys_t *p_sys = (filter_sys_t *) p_data;
379 VLC_UNUSED(oldval);
380 VLC_UNUSED(p_this);
382 vlc_mutex_lock( &p_sys->lock );
383 if( !strcmp( psz_var, "marq-marquee" ) )
385 free( p_sys->format );
386 p_sys->format = strdup( newval.psz_string );
388 else if ( !strcmp( psz_var, "marq-x" ) )
390 p_sys->i_xoff = newval.i_int;
392 else if ( !strcmp( psz_var, "marq-y" ) )
394 p_sys->i_yoff = newval.i_int;
396 else if ( !strcmp( psz_var, "marq-color" ) )
398 p_sys->p_style->i_font_color = newval.i_int;
400 else if ( !strcmp( psz_var, "marq-opacity" ) )
402 p_sys->p_style->i_font_alpha = newval.i_int;
404 else if ( !strcmp( psz_var, "marq-size" ) )
406 p_sys->p_style->i_font_size = newval.i_int;
408 else if ( !strcmp( psz_var, "marq-timeout" ) )
410 p_sys->i_timeout = newval.i_int;
412 else if ( !strcmp( psz_var, "marq-refresh" ) )
414 p_sys->i_refresh = newval.i_int * 1000;
416 else if ( !strcmp( psz_var, "marq-position" ) )
417 /* willing to accept a match against marq-pos */
419 p_sys->i_pos = newval.i_int;
422 free( p_sys->message );
423 p_sys->message = NULL; /* force update */
425 vlc_mutex_unlock( &p_sys->lock );
426 return VLC_SUCCESS;