1 /*****************************************************************************
2 * logo.c : logo video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2006 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
7 * Simon Latapie <garf@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
36 #include <vlc_mouse.h>
37 #include <vlc_subpicture.h>
39 #include <vlc_image.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 #define FILE_TEXT N_("Logo filenames")
49 #define FILE_LONGTEXT N_("Full path of the image files to use. Format is " \
50 "<image>[,<delay in ms>[,<alpha>]][;<image>[,<delay>[,<alpha>]]][;...]. " \
51 "If you only have one file, simply enter its filename.")
52 #define REPEAT_TEXT N_("Logo animation # of loops")
53 #define REPEAT_LONGTEXT N_("Number of loops for the logo animation. " \
54 "-1 = continuous, 0 = disabled")
55 #define DELAY_TEXT N_("Logo individual image time in ms")
56 #define DELAY_LONGTEXT N_("Individual image display time of 0 - 60000 ms.")
58 #define POSX_TEXT N_("X coordinate")
59 #define POSX_LONGTEXT N_("X coordinate of the logo. You can move the logo " \
60 "by left-clicking it." )
61 #define POSY_TEXT N_("Y coordinate")
62 #define POSY_LONGTEXT N_("Y coordinate of the logo. You can move the logo " \
63 "by left-clicking it." )
64 #define OPACITY_TEXT N_("Opacity of the logo")
65 #define OPACITY_LONGTEXT N_("Logo opacity value " \
66 "(from 0 for full transparency to 255 for full opacity)." )
67 #define POS_TEXT N_("Logo position")
68 #define POS_LONGTEXT N_( \
69 "Enforce the logo position on the video " \
70 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
71 "also use combinations of these values, eg 6 = top-right).")
73 #define LOGO_HELP N_("Use a local picture as logo on the video")
75 #define CFG_PREFIX "logo-"
77 static const int pi_pos_values
[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
78 static const char *const ppsz_pos_descriptions
[] =
79 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
80 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
82 static int OpenSub ( vlc_object_t
* );
83 static int OpenVideo( vlc_object_t
* );
84 static void Close ( vlc_object_t
* );
87 set_category( CAT_VIDEO
)
88 set_subcategory( SUBCAT_VIDEO_SUBPIC
)
90 set_capability( "sub source", 0 )
91 set_callbacks( OpenSub
, Close
)
92 set_description( N_("Logo sub source") )
93 set_shortname( N_("Logo overlay") )
94 add_shortcut( "logo" )
96 add_loadfile(CFG_PREFIX
"file", NULL
, FILE_TEXT
, FILE_LONGTEXT
)
97 add_integer( CFG_PREFIX
"x", -1, POSX_TEXT
, POSX_LONGTEXT
, true )
98 add_integer( CFG_PREFIX
"y", -1, POSY_TEXT
, POSY_LONGTEXT
, true )
99 /* default to 1000 ms per image, continuously cycle through them */
100 add_integer( CFG_PREFIX
"delay", 1000, DELAY_TEXT
, DELAY_LONGTEXT
, true )
101 add_integer( CFG_PREFIX
"repeat", -1, REPEAT_TEXT
, REPEAT_LONGTEXT
, true )
102 add_integer_with_range( CFG_PREFIX
"opacity", 255, 0, 255,
103 OPACITY_TEXT
, OPACITY_LONGTEXT
, false )
104 add_integer( CFG_PREFIX
"position", -1, POS_TEXT
, POS_LONGTEXT
, false )
105 change_integer_list( pi_pos_values
, ppsz_pos_descriptions
)
107 /* video output filter submodule */
109 set_capability( "video filter", 0 )
110 set_callbacks( OpenVideo
, Close
)
111 set_description( N_("Logo video filter") )
112 add_shortcut( "logo" )
116 /*****************************************************************************
118 *****************************************************************************/
121 * Structure to hold the set of individual logo image names, times,
126 int i_delay
; /* -1 means use default delay */
127 int i_alpha
; /* -1 means use default alpha */
133 * Logo list structure.
137 logo_t
*p_logo
; /* the parsing's result */
138 unsigned int i_count
; /* the number of logo images to be displayed */
140 int i_repeat
; /* how often to repeat the images, image time in ms */
141 vlc_tick_t i_next_pic
; /* when to bring up a new logo image */
143 unsigned int i_counter
; /* index into the list of logo images */
145 int i_delay
; /* default delay (0 - 60000 ms) */
146 int i_alpha
; /* default alpha */
151 * Private logo data holder
155 vlc_blender_t
*p_blend
;
166 /* On the fly control variable */
173 static const char *const ppsz_filter_options
[] = {
174 "file", "x", "y", "delay", "repeat", "opacity", "position", NULL
177 static const char *const ppsz_filter_callbacks
[] = {
187 static int OpenCommon( vlc_object_t
*, bool b_sub
);
189 static subpicture_t
*FilterSub( filter_t
*, vlc_tick_t
);
190 static picture_t
*FilterVideo( filter_t
*, picture_t
* );
192 static int Mouse( filter_t
*, vlc_mouse_t
*, const vlc_mouse_t
* );
194 static int LogoCallback( vlc_object_t
*, char const *,
195 vlc_value_t
, vlc_value_t
, void * );
197 static void LogoListLoad( vlc_object_t
*, logo_list_t
*, const char * );
198 static void LogoListUnload( logo_list_t
* );
199 static logo_t
*LogoListNext( logo_list_t
*p_list
, vlc_tick_t i_date
);
200 static logo_t
*LogoListCurrent( logo_list_t
*p_list
);
203 * Open the sub source
205 static int OpenSub( vlc_object_t
*p_this
)
207 return OpenCommon( p_this
, true );
211 * Open the video filter
213 static int OpenVideo( vlc_object_t
*p_this
)
215 return OpenCommon( p_this
, false );
219 * Common open function
221 static int OpenCommon( vlc_object_t
*p_this
, bool b_sub
)
223 filter_t
*p_filter
= (filter_t
*)p_this
;
228 if( !b_sub
&& !es_format_IsSimilar( &p_filter
->fmt_in
, &p_filter
->fmt_out
) )
230 msg_Err( p_filter
, "Input and output format does not match" );
235 p_filter
->p_sys
= p_sys
= malloc( sizeof( *p_sys
) );
240 p_sys
->p_blend
= NULL
;
244 p_sys
->p_blend
= filter_NewBlend( VLC_OBJECT(p_filter
),
245 &p_filter
->fmt_in
.video
);
246 if( !p_sys
->p_blend
)
254 config_ChainParse( p_filter
, CFG_PREFIX
, ppsz_filter_options
,
258 logo_list_t
*p_list
= &p_sys
->list
;
260 psz_filename
= var_CreateGetStringCommand( p_filter
, "logo-file" );
264 filter_DeleteBlend( p_sys
->p_blend
);
268 if( *psz_filename
== '\0' )
269 msg_Warn( p_this
, "no logo file specified" );
271 p_list
->i_alpha
= var_CreateGetIntegerCommand( p_filter
, "logo-opacity");
272 p_list
->i_alpha
= VLC_CLIP( p_list
->i_alpha
, 0, 255 );
273 p_list
->i_delay
= var_CreateGetIntegerCommand( p_filter
, "logo-delay" );
274 p_list
->i_repeat
= var_CreateGetIntegerCommand( p_filter
, "logo-repeat" );
276 p_sys
->i_pos
= var_CreateGetIntegerCommand( p_filter
, "logo-position" );
277 p_sys
->i_pos_x
= var_CreateGetIntegerCommand( p_filter
, "logo-x" );
278 p_sys
->i_pos_y
= var_CreateGetIntegerCommand( p_filter
, "logo-y" );
279 p_sys
->b_absolute
= (p_sys
->i_pos
< 0);
281 /* Ignore aligment if a position is given for video filter */
282 if( !b_sub
&& p_sys
->i_pos_x
>= 0 && p_sys
->i_pos_y
>= 0 )
285 vlc_mutex_init( &p_sys
->lock
);
286 LogoListLoad( p_this
, p_list
, psz_filename
);
287 p_sys
->b_spu_update
= true;
288 p_sys
->b_mouse_grab
= false;
290 for( int i
= 0; ppsz_filter_callbacks
[i
]; i
++ )
291 var_AddCallback( p_filter
, ppsz_filter_callbacks
[i
],
292 LogoCallback
, p_sys
);
297 p_filter
->pf_sub_source
= FilterSub
;
301 p_filter
->pf_video_filter
= FilterVideo
;
302 p_filter
->pf_video_mouse
= Mouse
;
305 free( psz_filename
);
310 * Common close function
312 static void Close( vlc_object_t
*p_this
)
314 filter_t
*p_filter
= (filter_t
*)p_this
;
315 filter_sys_t
*p_sys
= p_filter
->p_sys
;
317 for( int i
= 0; ppsz_filter_callbacks
[i
]; i
++ )
318 var_DelCallback( p_filter
, ppsz_filter_callbacks
[i
],
319 LogoCallback
, p_sys
);
322 filter_DeleteBlend( p_sys
->p_blend
);
324 LogoListUnload( &p_sys
->list
);
331 static subpicture_t
*FilterSub( filter_t
*p_filter
, vlc_tick_t date
)
333 filter_sys_t
*p_sys
= p_filter
->p_sys
;
334 logo_list_t
*p_list
= &p_sys
->list
;
337 subpicture_region_t
*p_region
;
342 vlc_mutex_lock( &p_sys
->lock
);
343 /* Basic test: b_spu_update occurs on a dynamic change,
344 & i_next_pic is the general timer, when to
345 look at updating the logo image */
347 if( ( !p_sys
->b_spu_update
&& p_list
->i_next_pic
> date
) ||
350 vlc_mutex_unlock( &p_sys
->lock
);
354 /* adjust index to the next logo */
355 p_logo
= LogoListNext( p_list
, date
);
356 p_sys
->b_spu_update
= false;
358 p_pic
= p_logo
->p_pic
;
360 /* Allocate the subpicture internal data. */
361 p_spu
= filter_NewSubpicture( p_filter
);
365 p_spu
->b_absolute
= p_sys
->b_absolute
;
366 p_spu
->i_start
= date
;
368 p_spu
->b_ephemer
= true;
370 /* Send an empty subpicture to clear the display when needed */
371 if( p_list
->i_repeat
!= -1 && p_list
->i_counter
== 0 )
374 if( p_list
->i_repeat
< 0 )
377 if( !p_pic
|| !p_logo
->i_alpha
||
378 ( p_logo
->i_alpha
== -1 && !p_list
->i_alpha
) )
381 /* Create new SPU region */
382 video_format_Init( &fmt
, VLC_CODEC_YUVA
);
383 fmt
.i_sar_num
= fmt
.i_sar_den
= 1;
384 fmt
.i_width
= fmt
.i_visible_width
= p_pic
->p
[Y_PLANE
].i_visible_pitch
;
385 fmt
.i_height
= fmt
.i_visible_height
= p_pic
->p
[Y_PLANE
].i_visible_lines
;
386 fmt
.i_x_offset
= fmt
.i_y_offset
= 0;
387 fmt
.transfer
= p_pic
->format
.transfer
;
388 fmt
.primaries
= p_pic
->format
.primaries
;
389 fmt
.space
= p_pic
->format
.space
;
390 fmt
.color_range
= p_pic
->format
.color_range
;
391 p_region
= subpicture_region_New( &fmt
);
394 msg_Err( p_filter
, "cannot allocate SPU region" );
395 subpicture_Delete( p_spu
);
401 picture_Copy( p_region
->p_picture
, p_pic
);
403 /* where to locate the logo: */
404 if( p_sys
->i_pos
< 0 )
405 { /* set to an absolute xy */
406 p_region
->i_align
= SUBPICTURE_ALIGN_RIGHT
| SUBPICTURE_ALIGN_TOP
;
407 p_spu
->b_absolute
= true;
410 { /* set to one of the 9 relative locations */
411 p_region
->i_align
= p_sys
->i_pos
;
412 p_spu
->b_absolute
= false;
415 p_region
->i_x
= p_sys
->i_pos_x
> 0 ? p_sys
->i_pos_x
: 0;
416 p_region
->i_y
= p_sys
->i_pos_y
> 0 ? p_sys
->i_pos_y
: 0;
418 p_spu
->p_region
= p_region
;
420 p_spu
->i_alpha
= ( p_logo
->i_alpha
!= -1 ?
421 p_logo
->i_alpha
: p_list
->i_alpha
);
424 vlc_mutex_unlock( &p_sys
->lock
);
432 static picture_t
*FilterVideo( filter_t
*p_filter
, picture_t
*p_src
)
434 filter_sys_t
*p_sys
= p_filter
->p_sys
;
435 logo_list_t
*p_list
= &p_sys
->list
;
437 picture_t
*p_dst
= filter_NewPicture( p_filter
);
441 picture_Copy( p_dst
, p_src
);
444 vlc_mutex_lock( &p_sys
->lock
);
447 if( p_list
->i_next_pic
< p_src
->date
)
448 p_logo
= LogoListNext( p_list
, p_src
->date
);
450 p_logo
= LogoListCurrent( p_list
);
453 const picture_t
*p_pic
= p_logo
->p_pic
;
456 const video_format_t
*p_fmt
= &p_pic
->format
;
457 const int i_dst_w
= p_filter
->fmt_out
.video
.i_visible_width
;
458 const int i_dst_h
= p_filter
->fmt_out
.video
.i_visible_height
;
462 if( p_sys
->i_pos
& SUBPICTURE_ALIGN_BOTTOM
)
464 p_sys
->i_pos_y
= i_dst_h
- p_fmt
->i_visible_height
;
466 else if ( !(p_sys
->i_pos
& SUBPICTURE_ALIGN_TOP
) )
468 p_sys
->i_pos_y
= ( i_dst_h
- p_fmt
->i_visible_height
) / 2;
475 if( p_sys
->i_pos
& SUBPICTURE_ALIGN_RIGHT
)
477 p_sys
->i_pos_x
= i_dst_w
- p_fmt
->i_visible_width
;
479 else if ( !(p_sys
->i_pos
& SUBPICTURE_ALIGN_LEFT
) )
481 p_sys
->i_pos_x
= ( i_dst_w
- p_fmt
->i_visible_width
) / 2;
489 if( p_sys
->i_pos_x
< 0 || p_sys
->i_pos_y
< 0 )
492 "logo(%ix%i) doesn't fit into video(%ix%i)",
493 p_fmt
->i_visible_width
, p_fmt
->i_visible_height
,
495 p_sys
->i_pos_x
= (p_sys
->i_pos_x
> 0) ? p_sys
->i_pos_x
: 0;
496 p_sys
->i_pos_y
= (p_sys
->i_pos_y
> 0) ? p_sys
->i_pos_y
: 0;
500 const int i_alpha
= p_logo
->i_alpha
!= -1 ? p_logo
->i_alpha
: p_list
->i_alpha
;
501 if( filter_ConfigureBlend( p_sys
->p_blend
, i_dst_w
, i_dst_h
, p_fmt
) ||
502 filter_Blend( p_sys
->p_blend
, p_dst
, p_sys
->i_pos_x
, p_sys
->i_pos_y
,
505 msg_Err( p_filter
, "failed to blend a picture" );
508 vlc_mutex_unlock( &p_sys
->lock
);
511 picture_Release( p_src
);
515 static int Mouse( filter_t
*p_filter
, vlc_mouse_t
*p_new
,
516 const vlc_mouse_t
*p_old
)
518 filter_sys_t
*p_sys
= p_filter
->p_sys
;
520 vlc_mutex_lock( &p_sys
->lock
);
521 logo_t
*p_logo
= LogoListCurrent( &p_sys
->list
);
522 const picture_t
*p_pic
= p_logo
->p_pic
;
526 const video_format_t
*p_fmt
= &p_pic
->format
;
527 const int i_logo_w
= p_fmt
->i_visible_width
;
528 const int i_logo_h
= p_fmt
->i_visible_height
;
530 /* Check if we are over the logo */
531 const bool b_over
= p_new
->i_x
>= p_sys
->i_pos_x
&&
532 p_new
->i_x
< p_sys
->i_pos_x
+ i_logo_w
&&
533 p_new
->i_y
>= p_sys
->i_pos_y
&&
534 p_new
->i_y
< p_sys
->i_pos_y
+ i_logo_h
;
536 if( b_over
&& vlc_mouse_HasPressed( p_old
, p_new
, MOUSE_BUTTON_LEFT
) )
537 p_sys
->b_mouse_grab
= true;
538 else if( vlc_mouse_HasReleased( p_old
, p_new
, MOUSE_BUTTON_LEFT
) )
539 p_sys
->b_mouse_grab
= false;
541 if( p_sys
->b_mouse_grab
)
544 vlc_mouse_GetMotion( &i_dx
, &i_dy
, p_old
, p_new
);
545 p_sys
->i_pos_x
= VLC_CLIP( p_sys
->i_pos_x
+ i_dx
, 0,
546 (int)p_filter
->fmt_in
.video
.i_width
- i_logo_w
);
547 p_sys
->i_pos_y
= VLC_CLIP( p_sys
->i_pos_y
+ i_dy
, 0,
548 (int)p_filter
->fmt_in
.video
.i_height
- i_logo_h
);
551 if( p_sys
->b_mouse_grab
|| b_over
)
553 vlc_mutex_unlock( &p_sys
->lock
);
557 vlc_mutex_unlock( &p_sys
->lock
);
562 /*****************************************************************************
563 * Callback to update params on the fly
564 *****************************************************************************/
565 static int LogoCallback( vlc_object_t
*p_this
, char const *psz_var
,
566 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
569 filter_sys_t
*p_sys
= (filter_sys_t
*)p_data
;
570 logo_list_t
*p_list
= &p_sys
->list
;
572 vlc_mutex_lock( &p_sys
->lock
);
573 if( !strcmp( psz_var
, "logo-file" ) )
575 LogoListUnload( p_list
);
576 LogoListLoad( p_this
, p_list
, newval
.psz_string
);
578 else if ( !strcmp( psz_var
, "logo-x" ) )
580 p_sys
->i_pos_x
= newval
.i_int
;
582 else if ( !strcmp( psz_var
, "logo-y" ) )
584 p_sys
->i_pos_y
= newval
.i_int
;
586 else if ( !strcmp( psz_var
, "logo-position" ) )
588 p_sys
->i_pos
= newval
.i_int
;
590 else if ( !strcmp( psz_var
, "logo-opacity" ) )
592 p_list
->i_alpha
= VLC_CLIP( newval
.i_int
, 0, 255 );
594 else if ( !strcmp( psz_var
, "logo-repeat" ) )
596 p_list
->i_repeat
= newval
.i_int
;
598 p_sys
->b_spu_update
= true;
599 vlc_mutex_unlock( &p_sys
->lock
);
605 * It loads the logo image into memory.
607 static picture_t
*LoadImage( vlc_object_t
*p_this
, const char *psz_filename
)
612 image_handler_t
*p_image
= image_HandlerCreate( p_this
);
616 video_format_t fmt_out
;
617 video_format_Init( &fmt_out
, VLC_CODEC_YUVA
);
619 char *psz_url
= vlc_path2uri( psz_filename
, NULL
);
620 picture_t
*p_pic
= image_ReadUrl( p_image
, psz_url
, &fmt_out
);
622 image_HandlerDelete( p_image
);
623 video_format_Clean( &fmt_out
);
629 * It loads the logo images into memory.
631 * Read the logo-file input switch, obtaining a list of images and
632 * associated durations and transparencies. Store the image(s), and
633 * times. An image without a stated time or opacity will use the
634 * logo-delay and logo-opacity values.
636 static void LogoListLoad( vlc_object_t
*p_this
, logo_list_t
*p_logo_list
,
637 const char *psz_filename
)
639 char *psz_list
; /* the list: <logo>[,[<delay>[,[<alpha>]]]][;...] */
641 logo_t
*p_logo
; /* the parsing's result */
643 p_logo_list
->i_counter
= 0;
644 p_logo_list
->i_next_pic
= 0;
646 psz_original
= psz_list
= strdup( psz_filename
);
651 /* Count the number logos == number of ';' + 1 */
652 p_logo_list
->i_count
= 1;
653 for( unsigned i
= 0; i
< strlen( psz_list
); i
++ )
655 if( psz_list
[i
] == ';' )
656 p_logo_list
->i_count
++;
659 p_logo_list
->p_logo
=
660 p_logo
= calloc( p_logo_list
->i_count
, sizeof(*p_logo
) );
669 for( unsigned i
= 0; i
< p_logo_list
->i_count
; i
++ )
671 char *p_c
= strchr( psz_list
, ';' );
672 char *p_c2
= strchr( psz_list
, ',' );
674 p_logo
[i
].i_alpha
= -1; /* use default settings */
675 p_logo
[i
].i_delay
= -1; /* use default settings */
677 if( p_c2
&& ( p_c2
< p_c
|| !p_c
) )
679 /* <logo>,<delay>[,<alpha>] type */
680 if( p_c2
[1] != ',' && p_c2
[1] != ';' && p_c2
[1] != '\0' )
681 p_logo
[i
].i_delay
= atoi( p_c2
+1 );
683 if( ( p_c2
= strchr( p_c2
+1, ',' ) )
684 && ( p_c2
< p_c
|| !p_c
) && p_c2
[1] != ';' && p_c2
[1] != '\0' )
685 p_logo
[i
].i_alpha
= atoi( p_c2
+ 1 );
694 msg_Dbg( p_this
, "logo file name %s, delay %d, alpha %d",
695 psz_list
, p_logo
[i
].i_delay
, p_logo
[i
].i_alpha
);
696 p_logo
[i
].p_pic
= LoadImage( p_this
, psz_list
);
697 if( !p_logo
[i
].p_pic
)
699 msg_Warn( p_this
, "error while loading logo %s, will be skipped",
707 /* initialize so that on the first update it will wrap back to 0 */
708 p_logo_list
->i_counter
= p_logo_list
->i_count
- 1;
710 free( psz_original
);
714 * Unload a list of logo and release associated ressources.
716 static void LogoListUnload( logo_list_t
*p_list
)
718 for( unsigned i
= 0; i
< p_list
->i_count
; i
++ )
720 logo_t
*p_logo
= &p_list
->p_logo
[i
];
723 picture_Release( p_logo
->p_pic
);
725 free( p_list
->p_logo
);
729 * Go to the next logo and return its pointer.
731 static logo_t
*LogoListNext( logo_list_t
*p_list
, vlc_tick_t i_date
)
733 p_list
->i_counter
= ( p_list
->i_counter
+ 1 ) % p_list
->i_count
;
735 logo_t
*p_logo
= LogoListCurrent( p_list
);
737 p_list
->i_next_pic
= i_date
+ VLC_TICK_FROM_MS( p_logo
->i_delay
!= -1 ?
738 p_logo
->i_delay
: p_list
->i_delay
);
742 * Return the current logo pointer
744 static logo_t
*LogoListCurrent( logo_list_t
*p_list
)
746 return &p_list
->p_logo
[p_list
->i_counter
];