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