1 /*****************************************************************************
2 * marq.c : marquee display video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2005 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
37 #include "vlc_filter.h"
38 #include "vlc_block.h"
40 #include "vlc_playlist.h"
42 #include "vlc_input.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int CreateFilter ( vlc_object_t * );
49 static void DestroyFilter( vlc_object_t * );
50 static subpicture_t *Filter( filter_t *, mtime_t );
53 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
54 vlc_value_t oldval, vlc_value_t newval,
56 static int pi_color_values[] = { 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
57 0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
58 0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
59 0x00000080, 0x000000FF, 0x0000FFFF};
60 static char *ppsz_color_descriptions[] = { N_("Default"), N_("Black"),
61 N_("Gray"), 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"),
66 /*****************************************************************************
67 * filter_sys_t: marquee filter descriptor
68 *****************************************************************************/
71 int i_xoff, i_yoff; /* offsets for the display string in the video window */
72 int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
75 char *psz_marquee; /* marquee string */
77 text_style_t *p_style; /* font control */
81 vlc_bool_t b_need_update;
84 #define MSG_TEXT N_("Text")
85 #define MSG_LONGTEXT N_( \
86 "Marquee text to display. " \
87 "(Available format strings: " \
88 "Time related: %Y = year, %m = month, %d = day, %H = hour, " \
89 "%M = minute, %S = second, ... " \
90 "Meta data related: $a = artist, $b = album, $c = copyright, " \
91 "$d = description, $e = encoded by, $g = genre, " \
92 "$l = language, $n = track num, $p = now playing, " \
93 "$r = rating, $t = title, $u = url, $A = date, " \
94 "$B = audio bitrate (in kb/s), $C = chapter," \
95 "$D = duration, $F = full name with path, $I = title, "\
97 "$N = name, $P = position (in %), $S = audio sample rate (in kHz), " \
98 "$T = time, $U = publisher, $V = volume, $_ = new line) ")
99 #define POSX_TEXT N_("X offset")
100 #define POSX_LONGTEXT N_("X offset, from the left screen edge." )
101 #define POSY_TEXT N_("Y offset")
102 #define POSY_LONGTEXT N_("Y offset, down from the top." )
103 #define TIMEOUT_TEXT N_("Timeout")
104 #define TIMEOUT_LONGTEXT N_("Number of milliseconds the marquee must remain " \
105 "displayed. Default value is " \
106 "0 (remains forever).")
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 " \
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 int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
127 static char *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 /*****************************************************************************
133 *****************************************************************************/
135 set_capability( "sub filter", 0 );
136 set_shortname( N_("Marquee" ));
137 set_callbacks( CreateFilter, DestroyFilter );
138 set_category( CAT_VIDEO );
139 set_subcategory( SUBCAT_VIDEO_SUBPIC );
140 add_string( "marq-marquee", "VLC", NULL, MSG_TEXT, MSG_LONGTEXT,
143 set_section( N_("Position"), NULL );
144 add_integer( "marq-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_TRUE );
145 add_integer( "marq-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_TRUE );
146 add_integer( "marq-position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
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( "marq-opacity", 255, 0, 255, NULL,
152 OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
153 add_integer( "marq-color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT,
155 change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
156 add_integer( "marq-size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE );
158 set_section( N_("Misc"), NULL );
159 add_integer( "marq-timeout", 0, NULL, TIMEOUT_TEXT, TIMEOUT_LONGTEXT,
162 set_description( _("Marquee display") );
163 add_shortcut( "marq" );
164 add_shortcut( "time" );
167 /*****************************************************************************
168 * CreateFilter: allocates marquee video filter
169 *****************************************************************************/
170 static int CreateFilter( vlc_object_t *p_this )
172 filter_t *p_filter = (filter_t *)p_this;
175 /* Allocate structure */
176 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
179 msg_Err( p_filter, "out of memory" );
183 p_sys->p_style = malloc( sizeof( text_style_t ) );
184 memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ) );
186 #define CREATE_VAR( stor, type, var ) \
187 p_sys->stor = var_CreateGet##type( p_filter->p_libvlc, var ); \
188 var_AddCallback( p_filter->p_libvlc, var, MarqueeCallback, p_sys );
190 CREATE_VAR( i_xoff, Integer, "marq-x" );
191 CREATE_VAR( i_yoff, Integer, "marq-y" );
192 CREATE_VAR( i_timeout,Integer, "marq-timeout" );
193 CREATE_VAR( i_pos, Integer, "marq-position" );
194 CREATE_VAR( psz_marquee, String, "marq-marquee" );
195 CREATE_VAR( p_style->i_font_alpha, Integer, "marq-opacity" );
196 CREATE_VAR( p_style->i_font_color, Integer, "marq-color" );
197 CREATE_VAR( p_style->i_font_size, Integer, "marq-size" );
199 p_sys->p_style->i_font_alpha = 255 - p_sys->p_style->i_font_alpha ;
202 p_filter->pf_sub_filter = Filter;
203 p_sys->last_time = ((time_t)-1);
204 p_sys->b_need_update = VLC_TRUE;
208 /*****************************************************************************
209 * DestroyFilter: destroy marquee video filter
210 *****************************************************************************/
211 static void DestroyFilter( vlc_object_t *p_this )
213 filter_t *p_filter = (filter_t *)p_this;
214 filter_sys_t *p_sys = p_filter->p_sys;
216 if( p_sys->p_style ) free( p_sys->p_style );
217 if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
220 /* Delete the marquee variables */
221 #define DEL_VAR(var) \
222 var_DelCallback( p_filter->p_libvlc, var, MarqueeCallback, p_sys ); \
223 var_Destroy( p_filter->p_libvlc, var );
226 DEL_VAR( "marq-marquee" );
227 DEL_VAR( "marq-timeout" );
228 DEL_VAR( "marq-position" );
229 DEL_VAR( "marq-color" );
230 DEL_VAR( "marq-opacity" );
231 DEL_VAR( "marq-size" );
233 /****************************************************************************
234 * String formating functions
235 ****************************************************************************/
237 static char *FormatTime(char *tformat )
241 #if defined(HAVE_LOCALTIME_R)
247 /* Get the current time. */
248 curtime = time( NULL );
250 /* Convert it to local time representation. */
251 #if defined(HAVE_LOCALTIME_R)
252 localtime_r( &curtime, &loctime );
253 strftime( buffer, 255, tformat, &loctime );
255 loctime = localtime( &curtime );
256 strftime( buffer, 255, tformat, loctime );
258 return strdup( buffer );
261 #define INSERT_STRING( check, string ) \
262 if( check && string ) \
264 int len = strlen( string ); \
265 dst = realloc( dst, \
266 i_size = i_size + len + 1 ); \
267 strncpy( d, string, len+1 ); \
275 char *FormatMeta( vlc_object_t *p_object, char *string )
278 char *dst = malloc( 1000 );
282 int i_size = strlen( string );
284 playlist_t *p_playlist = pl_Yield( p_object );
285 input_thread_t *p_input = p_playlist->p_input;
286 input_item_t *p_item = NULL;
287 pl_Release( p_object );
290 vlc_object_yield( p_input );
291 p_item = p_input->input.p_item;
293 vlc_mutex_lock( &p_item->lock );
296 sprintf( dst, string );
305 INSERT_STRING( p_item && p_item->p_meta,
306 p_item->p_meta->psz_artist );
309 INSERT_STRING( p_item && p_item->p_meta,
310 p_item->p_meta->psz_album );
313 INSERT_STRING( p_item && p_item->p_meta,
314 p_item->p_meta->psz_copyright );
317 INSERT_STRING( p_item && p_item->p_meta,
318 p_item->p_meta->psz_description );
321 INSERT_STRING( p_item && p_item->p_meta,
322 p_item->p_meta->psz_encodedby );
325 INSERT_STRING( p_item && p_item->p_meta,
326 p_item->p_meta->psz_genre );
329 INSERT_STRING( p_item && p_item->p_meta,
330 p_item->p_meta->psz_language );
333 INSERT_STRING( p_item && p_item->p_meta,
334 p_item->p_meta->psz_tracknum );
337 INSERT_STRING( p_item && p_item->p_meta,
338 p_item->p_meta->psz_nowplaying );
341 INSERT_STRING( p_item && p_item->p_meta,
342 p_item->p_meta->psz_rating );
345 INSERT_STRING( p_item && p_item->p_meta,
346 p_item->p_meta->psz_title );
349 INSERT_STRING( p_item && p_item->p_meta,
350 p_item->p_meta->psz_url );
353 INSERT_STRING( p_item && p_item->p_meta,
354 p_item->p_meta->psz_date );
359 snprintf( buf, 10, "%d",
360 var_GetInteger( p_input, "bit-rate" )/1000 );
366 INSERT_STRING( 1, buf );
371 snprintf( buf, 10, "%d",
372 var_GetInteger( p_input, "chapter" ) );
378 INSERT_STRING( 1, buf );
383 sprintf( buf, "%02d:%02d:%02d",
384 (int)(p_item->i_duration/(3600000000)),
385 (int)((p_item->i_duration/(60000000))%60),
386 (int)((p_item->i_duration/1000000)%60) );
390 sprintf( buf, "--:--:--" );
392 INSERT_STRING( 1, buf );
395 INSERT_STRING( p_item, p_item->psz_uri );
400 snprintf( buf, 10, "%d",
401 var_GetInteger( p_input, "title" ) );
407 INSERT_STRING( 1, buf );
410 if( p_item && p_input )
412 sprintf( buf, "%02d:%02d:%02d",
413 (int)((p_item->i_duration-p_input->i_time)/(3600000000)),
414 (int)(((p_item->i_duration-p_input->i_time)/(60000000))%60),
415 (int)(((p_item->i_duration-p_input->i_time)/1000000)%60) );
419 sprintf( buf, "--:--:--" );
421 INSERT_STRING( 1, buf );
424 INSERT_STRING( p_item, p_item->psz_name );
429 snprintf( buf, 10, "%2.1lf",
430 var_GetFloat( p_input, "position" ) * 100. );
434 sprintf( buf, "--.-%%" );
436 INSERT_STRING( 1, buf );
441 int r = var_GetInteger( p_input, "sample-rate" );
442 snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
448 INSERT_STRING( 1, buf );
453 sprintf( buf, "%02d:%02d:%02d",
454 (int)(p_input->i_time/(3600000000)),
455 (int)((p_input->i_time/(60000000))%60),
456 (int)((p_input->i_time/1000000)%60) );
460 sprintf( buf, "--:--:--" );
462 INSERT_STRING( 1, buf );
465 INSERT_STRING( p_item && p_item->p_meta,
466 p_item->p_meta->psz_publisher );
470 audio_volume_t volume;
471 aout_VolumeGet( p_object, &volume );
472 snprintf( buf, 10, "%d", volume );
473 INSERT_STRING( 1, buf );
503 vlc_object_release( p_input );
505 vlc_mutex_unlock( &p_item->lock );
511 /****************************************************************************
512 * Filter: the whole thing
513 ****************************************************************************
514 * This function outputs subpictures at regular time intervals.
515 ****************************************************************************/
516 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
518 filter_sys_t *p_sys = p_filter->p_sys;
524 if( p_sys->last_time == time( NULL ) )
529 /* if( p_sys->b_need_update == VLC_FALSE )
534 p_spu = p_filter->pf_sub_buffer_new( p_filter );
535 if( !p_spu ) return NULL;
537 memset( &fmt, 0, sizeof(video_format_t) );
538 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
540 fmt.i_width = fmt.i_height = 0;
543 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
544 if( !p_spu->p_region )
546 p_filter->pf_sub_buffer_del( p_filter, p_spu );
550 t = p_sys->last_time = time( NULL );
552 buf = FormatTime( p_sys->psz_marquee );
553 p_spu->p_region->psz_text = FormatMeta( VLC_OBJECT( p_filter ), buf );
555 p_spu->i_start = date;
556 p_spu->i_stop = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;
557 p_spu->b_ephemer = VLC_TRUE;
559 /* where to locate the string: */
560 if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )
561 { /* set to one of the 9 relative locations */
562 p_spu->i_flags = p_sys->i_pos;
565 p_spu->b_absolute = VLC_FALSE;
568 { /* set to an absolute xy, referenced to upper left corner */
569 p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
570 p_spu->i_x = p_sys->i_xoff;
571 p_spu->i_y = p_sys->i_yoff;
572 p_spu->b_absolute = VLC_TRUE;
574 p_spu->p_region->p_style = p_sys->p_style;
576 p_sys->b_need_update = VLC_FALSE;
580 /**********************************************************************
581 * Callback to update params on the fly
582 **********************************************************************/
583 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
584 vlc_value_t oldval, vlc_value_t newval,
587 filter_sys_t *p_sys = (filter_sys_t *) p_data;
589 if( !strncmp( psz_var, "marq-marquee", 7 ) )
591 if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
592 p_sys->psz_marquee = strdup( newval.psz_string );
594 else if ( !strncmp( psz_var, "marq-x", 6 ) )
596 p_sys->i_xoff = newval.i_int;
598 else if ( !strncmp( psz_var, "marq-y", 6 ) )
600 p_sys->i_yoff = newval.i_int;
602 else if ( !strncmp( psz_var, "marq-color", 8 ) ) /* "marq-col" */
604 p_sys->p_style->i_font_color = newval.i_int;
606 else if ( !strncmp( psz_var, "marq-opacity", 8 ) ) /* "marq-opa" */
608 p_sys->p_style->i_font_alpha = 255 - newval.i_int;
610 else if ( !strncmp( psz_var, "marq-size", 6 ) )
612 p_sys->p_style->i_font_size = newval.i_int;
614 else if ( !strncmp( psz_var, "marq-timeout", 12 ) )
616 p_sys->i_timeout = newval.i_int;
618 else if ( !strncmp( psz_var, "marq-position", 8 ) )
619 /* willing to accept a match against marq-pos */
621 p_sys->i_pos = newval.i_int;
622 p_sys->i_xoff = -1; /* force to relative positioning */
624 p_sys->b_need_update = VLC_TRUE;