]> git.sesse.net Git - vlc/blob - modules/video_filter/marq.c
OpenCV example: drop an unused variable
[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 #include <vlc_fs.h>
40
41 #include <vlc_strings.h>
42
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 );
49
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") };
65
66 /*****************************************************************************
67  * filter_sys_t: marquee filter descriptor
68  *****************************************************************************/
69 struct filter_sys_t
70 {
71     vlc_mutex_t lock;
72
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;
76
77     char *format; /**< marquee text format */
78     char *filepath; /**< marquee file path */
79     char *message; /**< marquee plain text */
80
81     text_style_t *p_style; /* font control */
82
83     mtime_t last_time;
84     mtime_t i_refresh;
85 };
86
87 #define MSG_TEXT N_("Text")
88 #define MSG_LONGTEXT N_( \
89     "Marquee text to display. " \
90     "(Available format strings: " \
91     "Time related: %Y = year, %m = month, %d = day, %H = hour, " \
92     "%M = minute, %S = second, ... " \
93     "Meta data related: $a = artist, $b = album, $c = copyright, " \
94     "$d = description, $e = encoded by, $g = genre, " \
95     "$l = language, $n = track num, $p = now playing, " \
96     "$r = rating, $s = subtitles language, $t = title, "\
97     "$u = url, $A = date, " \
98     "$B = audio bitrate (in kb/s), $C = chapter," \
99     "$D = duration, $F = full name with path, $I = title, "\
100     "$L = time left, " \
101     "$N = name, $O = audio language, $P = position (in %), $R = rate, " \
102     "$S = audio sample rate (in kHz), " \
103     "$T = time, $U = publisher, $V = volume, $_ = new line) ")
104 #define FILE_TEXT N_("Text file")
105 #define FILE_LONGTEXT N_("File to read the marquee text from.")
106 #define POSX_TEXT N_("X offset")
107 #define POSX_LONGTEXT N_("X offset, from the left screen edge." )
108 #define POSY_TEXT N_("Y offset")
109 #define POSY_LONGTEXT N_("Y offset, down from the top." )
110 #define TIMEOUT_TEXT N_("Timeout")
111 #define TIMEOUT_LONGTEXT N_("Number of milliseconds the marquee must remain " \
112                             "displayed. Default value is " \
113                             "0 (remains forever).")
114 #define REFRESH_TEXT N_("Refresh period in ms")
115 #define REFRESH_LONGTEXT N_("Number of milliseconds between string updates. " \
116                             "This is mainly useful when using meta data " \
117                             "or time format string sequences.")
118 #define OPACITY_TEXT N_("Opacity")
119 #define OPACITY_LONGTEXT N_("Opacity (inverse of transparency) of " \
120     "overlayed text. 0 = transparent, 255 = totally opaque. " )
121 #define SIZE_TEXT N_("Font size, pixels")
122 #define SIZE_LONGTEXT N_("Font size, in pixels. Default is -1 (use default " \
123     "font size)." )
124
125 #define COLOR_TEXT N_("Color")
126 #define COLOR_LONGTEXT N_("Color of the text that will be rendered on "\
127     "the video. This must be an hexadecimal (like HTML colors). The first two "\
128     "chars are for red, then green, then blue. #000000 = black, #FF0000 = red,"\
129     " #00FF00 = green, #FFFF00 = yellow (red + green), #FFFFFF = white" )
130
131 #define POS_TEXT N_("Marquee position")
132 #define POS_LONGTEXT N_( \
133   "You can enforce the marquee position on the video " \
134   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
135   "also use combinations of these values, eg 6 = top-right).")
136
137 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
138 static const char *const ppsz_pos_descriptions[] =
139      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
140      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
141
142 #define CFG_PREFIX "marq-"
143
144 #define MARQUEE_HELP N_("Display text above the video")
145
146 /*****************************************************************************
147  * Module descriptor
148  *****************************************************************************/
149 vlc_module_begin ()
150     set_capability( "sub source", 0 )
151     set_shortname( N_("Marquee" ))
152     set_description( N_("Marquee display") )
153     set_help(MARQUEE_HELP)
154     set_callbacks( CreateFilter, DestroyFilter )
155     set_category( CAT_VIDEO )
156     set_subcategory( SUBCAT_VIDEO_SUBPIC )
157     add_string( CFG_PREFIX "marquee", "VLC", MSG_TEXT, MSG_LONGTEXT,
158                 false )
159     add_loadfile( CFG_PREFIX "file", NULL, FILE_TEXT, FILE_LONGTEXT, true )
160
161     set_section( N_("Position"), NULL )
162     add_integer( CFG_PREFIX "x", 0, POSX_TEXT, POSX_LONGTEXT, true )
163     add_integer( CFG_PREFIX "y", 0, POSY_TEXT, POSY_LONGTEXT, true )
164     add_integer( CFG_PREFIX "position", -1, POS_TEXT, POS_LONGTEXT, false )
165         change_integer_list( pi_pos_values, ppsz_pos_descriptions )
166
167     set_section( N_("Font"), NULL )
168     /* 5 sets the default to top [1] left [4] */
169     add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255,
170         OPACITY_TEXT, OPACITY_LONGTEXT, false )
171     add_rgb( CFG_PREFIX "color", 0xFFFFFF, COLOR_TEXT, COLOR_LONGTEXT,
172                  false )
173         change_integer_list( pi_color_values, ppsz_color_descriptions )
174     add_integer( CFG_PREFIX "size", -1, SIZE_TEXT, SIZE_LONGTEXT,
175                  false )
176         change_integer_range( -1, 4096)
177
178     set_section( N_("Misc"), NULL )
179     add_integer( CFG_PREFIX "timeout", 0, TIMEOUT_TEXT, TIMEOUT_LONGTEXT,
180                  false )
181     add_integer( CFG_PREFIX "refresh", 1000, REFRESH_TEXT,
182                  REFRESH_LONGTEXT, false )
183
184     add_shortcut( "time" )
185 vlc_module_end ()
186
187 static const char *const ppsz_filter_options[] = {
188     "marquee", "x", "y", "position", "color", "size", "timeout", "refresh",
189     "opacity",
190     NULL
191 };
192
193 /*****************************************************************************
194  * CreateFilter: allocates marquee video filter
195  *****************************************************************************/
196 static int CreateFilter( vlc_object_t *p_this )
197 {
198     filter_t *p_filter = (filter_t *)p_this;
199     filter_sys_t *p_sys;
200
201     /* Allocate structure */
202     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
203     if( p_sys == NULL )
204         return VLC_ENOMEM;
205
206     vlc_mutex_init( &p_sys->lock );
207     p_sys->p_style = text_style_New();
208
209     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
210                        p_filter->p_cfg );
211
212
213 #define CREATE_VAR( stor, type, var ) \
214     p_sys->stor = var_CreateGet##type##Command( p_filter, var ); \
215     var_AddCallback( p_filter, var, MarqueeCallback, p_sys );
216
217     CREATE_VAR( i_xoff, Integer, "marq-x" );
218     CREATE_VAR( i_yoff, Integer, "marq-y" );
219     CREATE_VAR( i_timeout,Integer, "marq-timeout" );
220     p_sys->i_refresh = 1000 * var_CreateGetIntegerCommand( p_filter,
221                                                            "marq-refresh" );
222     var_AddCallback( p_filter, "marq-refresh", MarqueeCallback, p_sys );
223     CREATE_VAR( i_pos, Integer, "marq-position" );
224     CREATE_VAR( format, String, "marq-marquee" );
225     p_sys->filepath = var_InheritString( p_filter, "marq-file" );
226     p_sys->message = NULL;
227     p_sys->p_style->i_font_alpha = var_CreateGetIntegerCommand( p_filter,
228                                                             "marq-opacity" );
229     var_AddCallback( p_filter, "marq-opacity", MarqueeCallback, p_sys );
230     CREATE_VAR( p_style->i_font_color, Integer, "marq-color" );
231     CREATE_VAR( p_style->i_font_size, Integer, "marq-size" );
232
233     /* Misc init */
234     p_filter->pf_sub_source = Filter;
235     p_sys->last_time = 0;
236
237     return VLC_SUCCESS;
238 }
239 /*****************************************************************************
240  * DestroyFilter: destroy marquee video filter
241  *****************************************************************************/
242 static void DestroyFilter( vlc_object_t *p_this )
243 {
244     filter_t *p_filter = (filter_t *)p_this;
245     filter_sys_t *p_sys = p_filter->p_sys;
246
247     /* Delete the marquee variables */
248 #define DEL_VAR(var) \
249     var_DelCallback( p_filter, var, MarqueeCallback, p_sys ); \
250     var_Destroy( p_filter, var );
251     DEL_VAR( "marq-x" );
252     DEL_VAR( "marq-y" );
253     DEL_VAR( "marq-timeout" );
254     DEL_VAR( "marq-refresh" );
255     DEL_VAR( "marq-position" );
256     DEL_VAR( "marq-marquee" );
257     DEL_VAR( "marq-opacity" );
258     DEL_VAR( "marq-color" );
259     DEL_VAR( "marq-size" );
260
261     vlc_mutex_destroy( &p_sys->lock );
262     text_style_Delete( p_sys->p_style );
263     free( p_sys->format );
264     free( p_sys->filepath );
265     free( p_sys->message );
266     free( p_sys );
267 }
268
269 /****************************************************************************
270  * Filter: the whole thing
271  ****************************************************************************
272  * This function outputs subpictures at regular time intervals.
273  ****************************************************************************/
274 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
275 {
276     filter_sys_t *p_sys = p_filter->p_sys;
277     subpicture_t *p_spu = NULL;
278     video_format_t fmt;
279
280     vlc_mutex_lock( &p_sys->lock );
281     if( p_sys->last_time + p_sys->i_refresh > date )
282         goto out;
283
284     if( p_sys->filepath != NULL )
285     {
286         char *fmt = MarqueeReadFile( p_filter, p_sys->filepath );
287         if( fmt != NULL )
288         {
289             free( p_sys->format );
290             p_sys->format = fmt;
291         }
292     }
293
294     char *msg = str_format( p_filter, p_sys->format ? p_sys->format : "" );
295     if( unlikely( msg == NULL ) )
296         goto out;
297     if( p_sys->message != NULL && !strcmp( msg, p_sys->message ) )
298     {
299         free( msg );
300         goto out;
301     }
302     free( p_sys->message );
303     p_sys->message = msg;
304
305     p_spu = filter_NewSubpicture( p_filter );
306     if( !p_spu )
307         goto out;
308
309     memset( &fmt, 0, sizeof(video_format_t) );
310     fmt.i_chroma = VLC_CODEC_TEXT;
311     fmt.i_width = fmt.i_height = 0;
312     fmt.i_x_offset = 0;
313     fmt.i_y_offset = 0;
314     p_spu->p_region = subpicture_region_New( &fmt );
315     if( !p_spu->p_region )
316     {
317         p_filter->pf_sub_buffer_del( p_filter, p_spu );
318         p_spu = NULL;
319         goto out;
320     }
321
322     p_sys->last_time = date;
323
324     p_spu->p_region->psz_text = strdup( msg );
325     p_spu->i_start = date;
326     p_spu->i_stop  = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;
327     p_spu->b_ephemer = true;
328
329     /*  where to locate the string: */
330     if( p_sys->i_pos < 0 )
331     {   /*  set to an absolute xy */
332         p_spu->p_region->i_align = SUBPICTURE_ALIGN_LEFT | SUBPICTURE_ALIGN_TOP;
333         p_spu->b_absolute = true;
334     }
335     else
336     {   /* set to one of the 9 relative locations */
337         p_spu->p_region->i_align = p_sys->i_pos;
338         p_spu->b_absolute = false;
339     }
340
341     p_spu->p_region->i_x = p_sys->i_xoff;
342     p_spu->p_region->i_y = p_sys->i_yoff;
343
344     p_spu->p_region->p_style = text_style_Duplicate( p_sys->p_style );
345
346 out:
347     vlc_mutex_unlock( &p_sys->lock );
348     return p_spu;
349 }
350
351 static char *MarqueeReadFile( filter_t *obj, const char *path )
352 {
353     FILE *stream = vlc_fopen( path, "rt" );
354     if( stream == NULL )
355     {
356         msg_Err( obj, "cannot open %s: %m", path );
357         return NULL;
358     }
359
360     char *line = NULL;
361
362     ssize_t len = getline( &line, &(size_t){ 0 }, stream );
363     if( len == -1 )
364     {
365         msg_Err( obj, "cannot read %s: %m", path );
366         clearerr( stream );
367         line = NULL;
368     }
369     fclose( stream );
370
371     if( len >= 1 && line[len - 1] == '\n' )
372         line[--len]  = '\0';
373     return line;
374 }
375
376 /**********************************************************************
377  * Callback to update params on the fly
378  **********************************************************************/
379 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
380                             vlc_value_t oldval, vlc_value_t newval,
381                             void *p_data )
382 {
383     filter_sys_t *p_sys = (filter_sys_t *) p_data;
384
385     VLC_UNUSED(oldval);
386     VLC_UNUSED(p_this);
387
388     vlc_mutex_lock( &p_sys->lock );
389     if( !strcmp( psz_var, "marq-marquee" ) )
390     {
391         free( p_sys->format );
392         p_sys->format = strdup( newval.psz_string );
393     }
394     else if ( !strcmp( psz_var, "marq-x" ) )
395     {
396         p_sys->i_xoff = newval.i_int;
397     }
398     else if ( !strcmp( psz_var, "marq-y" ) )
399     {
400         p_sys->i_yoff = newval.i_int;
401     }
402     else if ( !strcmp( psz_var, "marq-color" ) )
403     {
404         p_sys->p_style->i_font_color = newval.i_int;
405     }
406     else if ( !strcmp( psz_var, "marq-opacity" ) )
407     {
408         p_sys->p_style->i_font_alpha = newval.i_int;
409     }
410     else if ( !strcmp( psz_var, "marq-size" ) )
411     {
412         p_sys->p_style->i_font_size = newval.i_int;
413     }
414     else if ( !strcmp( psz_var, "marq-timeout" ) )
415     {
416         p_sys->i_timeout = newval.i_int;
417     }
418     else if ( !strcmp( psz_var, "marq-refresh" ) )
419     {
420         p_sys->i_refresh = newval.i_int * 1000;
421     }
422     else if ( !strcmp( psz_var, "marq-position" ) )
423     /* willing to accept a match against marq-pos */
424     {
425         p_sys->i_pos = newval.i_int;
426         p_sys->i_xoff = -1;       /* force to relative positioning */
427     }
428
429     free( p_sys->message );
430     p_sys->message = NULL; /* force update */
431
432     vlc_mutex_unlock( &p_sys->lock );
433     return VLC_SUCCESS;
434 }