]> git.sesse.net Git - vlc/blob - modules/video_filter/marq.c
edc71c2810dac10ab546e9434e47fa60ec43fcb9
[vlc] / modules / video_filter / marq.c
1 /*****************************************************************************
2  * marq.c : marquee display video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2005 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 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc_vout.h>
34
35 #include "vlc_filter.h"
36 #include "vlc_block.h"
37 #include "vlc_osd.h"
38
39 #include "vlc_strings.h"
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  CreateFilter ( vlc_object_t * );
45 static void DestroyFilter( vlc_object_t * );
46 static subpicture_t *Filter( filter_t *, mtime_t );
47
48
49 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
50                             vlc_value_t oldval, vlc_value_t newval,
51                             void *p_data );
52 static int pi_color_values[] = { 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
53                0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
54                0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
55                0x00000080, 0x000000FF, 0x0000FFFF};
56 static char *ppsz_color_descriptions[] = { N_("Default"), N_("Black"),
57                N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),
58                N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"),
59                N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"),
60                N_("Aqua") };
61
62 /*****************************************************************************
63  * filter_sys_t: marquee filter descriptor
64  *****************************************************************************/
65 struct filter_sys_t
66 {
67     int i_xoff, i_yoff;  /* offsets for the display string in the video window */
68     int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
69     int i_timeout;
70
71     char *psz_marquee;    /* marquee string */
72
73     text_style_t *p_style; /* font control */
74
75     time_t last_time;
76
77     vlc_bool_t b_need_update;
78 };
79
80 #define MSG_TEXT N_("Text")
81 #define MSG_LONGTEXT N_( \
82     "Marquee text to display. " \
83     "(Available format strings: " \
84     "Time related: %Y = year, %m = month, %d = day, %H = hour, " \
85     "%M = minute, %S = second, ... " \
86     "Meta data related: $a = artist, $b = album, $c = copyright, " \
87     "$d = description, $e = encoded by, $g = genre, " \
88     "$l = language, $n = track num, $p = now playing, " \
89     "$r = rating, $s = subtitles language, $t = title, "\
90     "$u = url, $A = date, " \
91     "$B = audio bitrate (in kb/s), $C = chapter," \
92     "$D = duration, $F = full name with path, $I = title, "\
93     "$L = time left, " \
94     "$N = name, $O = audio language, $P = position (in %), $R = rate, " \
95     "$S = audio sample rate (in kHz), " \
96     "$T = time, $U = publisher, $V = volume, $_ = new line) ")
97 #define POSX_TEXT N_("X offset")
98 #define POSX_LONGTEXT N_("X offset, from the left screen edge." )
99 #define POSY_TEXT N_("Y offset")
100 #define POSY_LONGTEXT N_("Y offset, down from the top." )
101 #define TIMEOUT_TEXT N_("Timeout")
102 #define TIMEOUT_LONGTEXT N_("Number of milliseconds the marquee must remain " \
103                             "displayed. Default value is " \
104                             "0 (remains forever).")
105 #define OPACITY_TEXT N_("Opacity")
106 #define OPACITY_LONGTEXT N_("Opacity (inverse of transparency) of " \
107     "overlayed text. 0 = transparent, 255 = totally opaque. " )
108 #define SIZE_TEXT N_("Font size, pixels")
109 #define SIZE_LONGTEXT N_("Font size, in pixels. Default is -1 (use default " \
110     "font size)." )
111
112 #define COLOR_TEXT N_("Color")
113 #define COLOR_LONGTEXT N_("Color of the text that will be rendered on "\
114     "the video. This must be an hexadecimal (like HTML colors). The first two "\
115     "chars are for red, then green, then blue. #000000 = black, #FF0000 = red,"\
116     " #00FF00 = green, #FFFF00 = yellow (red + green), #FFFFFF = white" )
117
118 #define POS_TEXT N_("Marquee position")
119 #define POS_LONGTEXT N_( \
120   "You can enforce the marquee position on the video " \
121   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
122   "also use combinations of these values, eg 6 = top-right).")
123
124 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
125 static char *ppsz_pos_descriptions[] =
126      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
127      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
128
129 #define CFG_PREFIX "marq-"
130
131 /*****************************************************************************
132  * Module descriptor
133  *****************************************************************************/
134 vlc_module_begin();
135     set_capability( "sub filter", 0 );
136     set_shortname( _("Marquee" ));
137     set_callbacks( CreateFilter, DestroyFilter );
138     set_category( CAT_VIDEO );
139     set_subcategory( SUBCAT_VIDEO_SUBPIC );
140     add_string( CFG_PREFIX "marquee", "VLC", NULL, MSG_TEXT, MSG_LONGTEXT,
141                 VLC_FALSE );
142
143     set_section( N_("Position"), NULL );
144     add_integer( CFG_PREFIX "x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_TRUE );
145     add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_TRUE );
146     add_integer( CFG_PREFIX "position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
147
148     set_section( N_("Font"), NULL );
149     /* 5 sets the default to top [1] left [4] */
150     change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
151     add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255, NULL,
152         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
153     add_integer( CFG_PREFIX "color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT,
154                  VLC_FALSE );
155         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
156     add_integer( CFG_PREFIX "size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT,
157                  VLC_FALSE );
158
159     set_section( N_("Misc"), NULL );
160     add_integer( CFG_PREFIX "timeout", 0, NULL, TIMEOUT_TEXT, TIMEOUT_LONGTEXT,
161                  VLC_FALSE );
162
163     set_description( _("Marquee display") );
164     add_shortcut( "marq" );
165     add_shortcut( "time" );
166 vlc_module_end();
167
168 static const char *ppsz_filter_options[] = {
169     "marquee", "x", "y", "position", "color", "size", "timeout", NULL
170 };
171
172 /*****************************************************************************
173  * CreateFilter: allocates marquee video filter
174  *****************************************************************************/
175 static int CreateFilter( vlc_object_t *p_this )
176 {
177     filter_t *p_filter = (filter_t *)p_this;
178     filter_sys_t *p_sys;
179
180     /* Allocate structure */
181     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
182     if( p_sys == NULL )
183     {
184         msg_Err( p_filter, "out of memory" );
185         return VLC_ENOMEM;
186     }
187
188     p_sys->p_style = malloc( sizeof( text_style_t ) );
189     memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ) );
190
191     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
192                        p_filter->p_cfg );
193
194 #define CREATE_VAR( stor, type, var ) \
195     p_sys->stor = var_CreateGet##type( p_filter, var ); \
196     var_AddCallback( p_filter, var, MarqueeCallback, p_sys );
197
198     CREATE_VAR( i_xoff, Integer, "marq-x" );
199     CREATE_VAR( i_yoff, Integer, "marq-y" );
200     CREATE_VAR( i_timeout,Integer, "marq-timeout" );
201     CREATE_VAR( i_pos, Integer, "marq-position" );
202     CREATE_VAR( psz_marquee, String, "marq-marquee" );
203     CREATE_VAR( p_style->i_font_alpha, Integer, "marq-opacity" );
204     CREATE_VAR( p_style->i_font_color, Integer, "marq-color" );
205     CREATE_VAR( p_style->i_font_size, Integer, "marq-size" );
206
207     p_sys->p_style->i_font_alpha = 255 - p_sys->p_style->i_font_alpha ;
208
209     /* Misc init */
210     p_filter->pf_sub_filter = Filter;
211     p_sys->last_time = ((time_t)-1);
212     p_sys->b_need_update = VLC_TRUE;
213
214     return VLC_SUCCESS;
215 }
216 /*****************************************************************************
217  * DestroyFilter: destroy marquee video filter
218  *****************************************************************************/
219 static void DestroyFilter( vlc_object_t *p_this )
220 {
221     filter_t *p_filter = (filter_t *)p_this;
222     filter_sys_t *p_sys = p_filter->p_sys;
223
224     if( p_sys->p_style ) free( p_sys->p_style );
225     if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
226
227     /* Delete the marquee variables */
228 #define DEL_VAR(var) \
229     var_DelCallback( p_filter, var, MarqueeCallback, p_sys ); \
230     var_Destroy( p_filter, var );
231     DEL_VAR( "marq-x" );
232     DEL_VAR( "marq-y" );
233     DEL_VAR( "marq-marquee" );
234     DEL_VAR( "marq-timeout" );
235     DEL_VAR( "marq-position" );
236     DEL_VAR( "marq-color" );
237     DEL_VAR( "marq-opacity" );
238     DEL_VAR( "marq-size" );
239
240     free( p_sys );
241 }
242
243 /****************************************************************************
244  * Filter: the whole thing
245  ****************************************************************************
246  * This function outputs subpictures at regular time intervals.
247  ****************************************************************************/
248 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
249 {
250     filter_sys_t *p_sys = p_filter->p_sys;
251     subpicture_t *p_spu;
252     video_format_t fmt;
253     time_t t;
254     char *buf;
255
256     if( p_sys->last_time == time( NULL ) )
257     {
258         return NULL;
259     }
260
261     if( p_sys->b_need_update == VLC_FALSE )
262     {
263         return NULL;
264     }
265
266     p_spu = p_filter->pf_sub_buffer_new( p_filter );
267     if( !p_spu ) return NULL;
268
269     memset( &fmt, 0, sizeof(video_format_t) );
270     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
271     fmt.i_aspect = 0;
272     fmt.i_width = fmt.i_height = 0;
273     fmt.i_x_offset = 0;
274     fmt.i_y_offset = 0;
275     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
276     if( !p_spu->p_region )
277     {
278         p_filter->pf_sub_buffer_del( p_filter, p_spu );
279         return NULL;
280     }
281
282     t = p_sys->last_time = time( NULL );
283
284     if( strchr( p_sys->psz_marquee, '%' ) || strchr( p_sys->psz_marquee, '$' ) )
285     {
286         p_sys->b_need_update = VLC_TRUE;
287     }
288     else
289     {
290         p_sys->b_need_update = VLC_FALSE;
291     }
292     buf = str_format_time( p_sys->psz_marquee );
293     p_spu->p_region->psz_text = str_format_meta( p_filter, buf );
294     free( buf );
295     p_spu->i_start = date;
296     p_spu->i_stop  = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;
297     p_spu->b_ephemer = VLC_TRUE;
298
299     /*  where to locate the string: */
300     if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )
301     {   /* set to one of the 9 relative locations */
302         p_spu->i_flags = p_sys->i_pos;
303         p_spu->i_x = 0;
304         p_spu->i_y = 0;
305         p_spu->b_absolute = VLC_FALSE;
306     }
307     else
308     {   /*  set to an absolute xy, referenced to upper left corner */
309         p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
310         p_spu->i_x = p_sys->i_xoff;
311         p_spu->i_y = p_sys->i_yoff;
312         p_spu->b_absolute = VLC_TRUE;
313     }
314     p_spu->p_region->p_style = p_sys->p_style;
315
316     return p_spu;
317 }
318
319 /**********************************************************************
320  * Callback to update params on the fly
321  **********************************************************************/
322 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
323                             vlc_value_t oldval, vlc_value_t newval,
324                             void *p_data )
325 {
326     filter_sys_t *p_sys = (filter_sys_t *) p_data;
327
328     if( !strncmp( psz_var, "marq-marquee", 7 ) )
329     {
330         if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
331         p_sys->psz_marquee = strdup( newval.psz_string );
332     }
333     else if ( !strncmp( psz_var, "marq-x", 6 ) )
334     {
335         p_sys->i_xoff = newval.i_int;
336     }
337     else if ( !strncmp( psz_var, "marq-y", 6 ) )
338     {
339         p_sys->i_yoff = newval.i_int;
340     }
341     else if ( !strncmp( psz_var, "marq-color", 8 ) )  /* "marq-col" */
342     {
343         p_sys->p_style->i_font_color = newval.i_int;
344     }
345     else if ( !strncmp( psz_var, "marq-opacity", 8 ) ) /* "marq-opa" */
346     {
347         p_sys->p_style->i_font_alpha = 255 - newval.i_int;
348     }
349     else if ( !strncmp( psz_var, "marq-size", 6 ) )
350     {
351         p_sys->p_style->i_font_size = newval.i_int;
352     }
353     else if ( !strncmp( psz_var, "marq-timeout", 12 ) )
354     {
355         p_sys->i_timeout = newval.i_int;
356     }
357     else if ( !strncmp( psz_var, "marq-position", 8 ) )
358     /* willing to accept a match against marq-pos */
359     {
360         p_sys->i_pos = newval.i_int;
361         p_sys->i_xoff = -1;       /* force to relative positioning */
362     }
363     p_sys->b_need_update = VLC_TRUE;
364     return VLC_SUCCESS;
365 }