1 /*****************************************************************************
2 * logo.c : logo video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Simon Latapie <garf@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
34 #include "vlc_filter.h"
35 #include "filter_common.h"
36 #include "vlc_image.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Create ( vlc_object_t * );
46 static void Destroy ( vlc_object_t * );
48 static int Init ( vout_thread_t * );
49 static void End ( vout_thread_t * );
50 static void Render ( vout_thread_t *, picture_t * );
52 static int SendEvents( vlc_object_t *, char const *,
53 vlc_value_t, vlc_value_t, void * );
54 static int MouseEvent( vlc_object_t *, char const *,
55 vlc_value_t , vlc_value_t , void * );
56 static int Control ( vout_thread_t *, int, va_list );
58 static int CreateFilter ( vlc_object_t * );
59 static void DestroyFilter( vlc_object_t * );
61 static int LogoCallback( vlc_object_t *, char const *,
62 vlc_value_t, vlc_value_t, void * );
64 /*****************************************************************************
66 *****************************************************************************/
67 #define FILE_TEXT N_("Logo filename")
68 #define FILE_LONGTEXT N_("Full path of the PNG file to use.")
69 #define POSX_TEXT N_("X coordinate of the logo")
70 #define POSX_LONGTEXT N_("You can move the logo by left-clicking on it." )
71 #define POSY_TEXT N_("Y coordinate of the logo")
72 #define POSY_LONGTEXT N_("You can move the logo by left-clicking on it." )
73 #define TRANS_TEXT N_("Transparency of the logo")
74 #define TRANS_LONGTEXT N_("You can set the logo transparency value here " \
75 "(from 0 for full transparency to 255 for full opacity)." )
76 #define POS_TEXT N_("Logo position")
77 #define POS_LONGTEXT N_( \
78 "You can enforce the logo position on the video " \
79 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
80 "also use combinations of these values).")
82 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
83 static char *ppsz_pos_descriptions[] =
84 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
85 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
88 set_description( _("Logo video filter") );
89 set_capability( "video filter", 0 );
90 set_shortname( N_("Logo overlay") );
91 set_category( CAT_VIDEO );
92 set_subcategory( SUBCAT_VIDEO_VFILTER );
93 add_shortcut( "logo" );
94 set_callbacks( Create, Destroy );
96 add_file( "logo-file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, VLC_FALSE );
97 add_integer( "logo-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
98 add_integer( "logo-y", -1, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
99 add_integer_with_range( "logo-transparency", 255, 0, 255, NULL,
100 TRANS_TEXT, TRANS_LONGTEXT, VLC_FALSE );
101 add_integer( "logo-position", 6, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
102 change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
104 /* subpicture filter submodule */
106 set_capability( "sub filter", 0 );
107 set_callbacks( CreateFilter, DestroyFilter );
108 set_description( _("Logo sub filter") );
109 add_shortcut( "logo" );
112 /*****************************************************************************
113 * LoadImage: loads the logo image into memory
114 *****************************************************************************/
115 static picture_t *LoadImage( vlc_object_t *p_this, char *psz_filename )
118 image_handler_t *p_image;
119 video_format_t fmt_in = {0}, fmt_out = {0};
121 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
122 p_image = image_HandlerCreate( p_this );
123 p_pic = image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
124 image_HandlerDelete( p_image );
129 /*****************************************************************************
130 * vout_sys_t: logo video output method descriptor
131 *****************************************************************************
132 * This structure is part of the video output thread descriptor.
133 * It describes the Invert specific properties of an output thread.
134 *****************************************************************************/
137 vout_thread_t *p_vout;
142 int i_width, i_height;
148 /*****************************************************************************
149 * Create: allocates logo video thread output method
150 *****************************************************************************/
151 static int Create( vlc_object_t *p_this )
153 vout_thread_t *p_vout = (vout_thread_t *)p_this;
157 /* Allocate structure */
158 p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
161 msg_Err( p_vout, "out of memory" );
165 p_vout->pf_init = Init;
166 p_vout->pf_end = End;
167 p_vout->pf_manage = NULL;
168 p_vout->pf_render = Render;
169 p_vout->pf_display = NULL;
170 p_vout->pf_control = Control;
172 p_sys->psz_filename = var_CreateGetString( p_this , "logo-file" );
173 if( !p_sys->psz_filename || !*p_sys->psz_filename )
175 msg_Err( p_this, "logo file not specified" );
179 var_Create( p_this, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
180 var_Get( p_this, "logo-position", &val );
181 p_sys->pos = val.i_int;
182 var_Create( p_this, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
183 var_Get( p_this, "logo-x", &val );
184 p_sys->posx = val.i_int;
185 var_Create( p_this, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
186 var_Get( p_this, "logo-y", &val );
187 p_sys->posy = val.i_int;
188 var_Create(p_this, "logo-transparency", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
189 var_Get( p_this, "logo-transparency", &val );
190 p_sys->i_trans = __MAX( __MIN( val.i_int, 255 ), 0 );
192 p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
199 p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
200 p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
205 /*****************************************************************************
206 * Init: initialize logo video thread output method
207 *****************************************************************************/
208 static int Init( vout_thread_t *p_vout )
210 vout_sys_t *p_sys = p_vout->p_sys;
214 I_OUTPUTPICTURES = 0;
216 /* Initialize the output structure */
217 p_vout->output.i_chroma = p_vout->render.i_chroma;
218 p_vout->output.i_width = p_vout->render.i_width;
219 p_vout->output.i_height = p_vout->render.i_height;
220 p_vout->output.i_aspect = p_vout->render.i_aspect;
222 /* Load the video blending filter */
223 p_sys->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
224 vlc_object_attach( p_sys->p_blend, p_vout );
225 p_sys->p_blend->fmt_out.video.i_x_offset =
226 p_sys->p_blend->fmt_out.video.i_y_offset = 0;
227 p_sys->p_blend->fmt_in.video.i_x_offset =
228 p_sys->p_blend->fmt_in.video.i_y_offset = 0;
229 p_sys->p_blend->fmt_out.video.i_aspect = p_vout->render.i_aspect;
230 p_sys->p_blend->fmt_out.video.i_chroma = p_vout->output.i_chroma;
231 p_sys->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','A');
232 p_sys->p_blend->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR;
233 p_sys->p_blend->fmt_in.video.i_width =
234 p_sys->p_blend->fmt_in.video.i_visible_width =
235 p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
236 p_sys->p_blend->fmt_in.video.i_height =
237 p_sys->p_blend->fmt_in.video.i_visible_height =
238 p_sys->p_pic->p[Y_PLANE].i_visible_lines;
239 p_sys->p_blend->fmt_out.video.i_width =
240 p_sys->p_blend->fmt_out.video.i_visible_width =
241 p_vout->output.i_width;
242 p_sys->p_blend->fmt_out.video.i_height =
243 p_sys->p_blend->fmt_out.video.i_visible_height =
244 p_vout->output.i_height;
246 p_sys->p_blend->p_module =
247 module_Need( p_sys->p_blend, "video blending", 0, 0 );
248 if( !p_sys->p_blend->p_module )
250 msg_Err( p_vout, "can't open blending filter, aborting" );
251 vlc_object_detach( p_sys->p_blend );
252 vlc_object_destroy( p_sys->p_blend );
256 if( p_sys->posx < 0 || p_sys->posy < 0 )
258 p_sys->posx = 0; p_sys->posy = 0;
260 if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
262 p_sys->posy = p_vout->render.i_height - p_sys->i_height;
264 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
266 p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
269 if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
271 p_sys->posx = p_vout->render.i_width - p_sys->i_width;
273 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
275 p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
279 /* Try to open the real video output */
280 msg_Dbg( p_vout, "spawning the real video output" );
283 vout_Create( p_vout, p_vout->render.i_width, p_vout->render.i_height,
284 p_vout->render.i_chroma, p_vout->render.i_aspect );
286 /* Everything failed */
287 if( p_sys->p_vout == NULL )
289 msg_Err( p_vout, "can't open vout, aborting" );
293 var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
294 var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
296 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
297 ADD_CALLBACKS( p_sys->p_vout, SendEvents );
298 ADD_PARENT_CALLBACKS( SendEventsToChild );
303 /*****************************************************************************
304 * End: terminate logo video thread output method
305 *****************************************************************************/
306 static void End( vout_thread_t *p_vout )
308 vout_sys_t *p_sys = p_vout->p_sys;
311 /* Free the fake output buffers we allocated */
312 for( i_index = I_OUTPUTPICTURES ; i_index ; )
315 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
318 var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
319 var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
323 DEL_CALLBACKS( p_sys->p_vout, SendEvents );
324 vlc_object_detach( p_sys->p_vout );
325 vout_Destroy( p_sys->p_vout );
328 if( p_sys->p_blend->p_module )
329 module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
330 vlc_object_detach( p_sys->p_blend );
331 vlc_object_destroy( p_sys->p_blend );
334 /*****************************************************************************
335 * Destroy: destroy logo video thread output method
336 *****************************************************************************/
337 static void Destroy( vlc_object_t *p_this )
339 vout_thread_t *p_vout = (vout_thread_t *)p_this;
340 vout_sys_t *p_sys = p_vout->p_sys;
342 DEL_PARENT_CALLBACKS( SendEventsToChild );
344 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
348 /*****************************************************************************
349 * Render: render the logo onto the video
350 *****************************************************************************/
351 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
353 vout_sys_t *p_sys = p_vout->p_sys;
356 /* This is a new frame. Get a structure from the video_output. */
357 while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
359 if( p_vout->b_die || p_vout->b_error ) return;
360 msleep( VOUT_OUTMEM_SLEEP );
363 vout_CopyPicture( p_vout, p_outpic, p_pic );
364 vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
366 p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic, p_outpic,
367 p_sys->p_pic, p_sys->posx, p_sys->posy,
370 vout_DisplayPicture( p_sys->p_vout, p_outpic );
373 /*****************************************************************************
374 * SendEvents: forward mouse and keyboard events to the parent p_vout
375 *****************************************************************************/
376 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
377 vlc_value_t oldval, vlc_value_t newval, void *p_data )
379 var_Set( (vlc_object_t *)p_data, psz_var, newval );
383 /*****************************************************************************
384 * MouseEvent: callback for mouse events
385 *****************************************************************************/
386 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
387 vlc_value_t oldval, vlc_value_t newval, void *p_data )
389 vout_thread_t *p_vout = (vout_thread_t*)p_data;
390 vout_sys_t *p_sys = p_vout->p_sys;
394 var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
396 i_delta = newval.i_int - oldval.i_int;
398 if( (valb.i_int & 0x1) == 0 )
403 if( psz_var[6] == 'x' )
406 var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
407 if( newval.i_int >= (int)p_sys->posx &&
408 valy.i_int >= (int)p_sys->posy &&
409 newval.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
410 valy.i_int <= (int)(p_sys->posy + p_sys->i_height) )
412 p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
413 p_vout->output.i_width - p_sys->i_width );
416 else if( psz_var[6] == 'y' )
419 var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
420 if( valx.i_int >= (int)p_sys->posx &&
421 newval.i_int >= (int)p_sys->posy &&
422 valx.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
423 newval.i_int <= (int)(p_sys->posy + p_sys->i_height) )
425 p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
426 p_vout->output.i_height - p_sys->i_height );
433 /*****************************************************************************
434 * Control: control facility for the vout (forwards to child vout)
435 *****************************************************************************/
436 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
438 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
441 /*****************************************************************************
442 * SendEventsToChild: forward events to the child/children vout
443 *****************************************************************************/
444 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
445 vlc_value_t oldval, vlc_value_t newval, void *p_data )
447 vout_thread_t *p_vout = (vout_thread_t *)p_this;
448 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
452 /*****************************************************************************
453 * filter_sys_t: logo filter descriptor
454 *****************************************************************************/
459 int i_width, i_height;
464 vlc_bool_t b_absolute;
468 /* On the fly control variable */
469 vlc_bool_t b_need_update;
470 vlc_bool_t b_new_image;
473 static subpicture_t *Filter( filter_t *, mtime_t );
475 /*****************************************************************************
476 * CreateFilter: allocates logo video filter
477 *****************************************************************************/
478 static int CreateFilter( vlc_object_t *p_this )
480 filter_t *p_filter = (filter_t *)p_this;
482 vlc_object_t *p_input;
484 /* Allocate structure */
485 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
488 msg_Err( p_filter, "out of memory" );
492 /* Hook used for callback variables */
493 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
500 p_sys->psz_filename =
501 var_CreateGetString( p_input->p_libvlc , "logo-file" );
502 if( !p_sys->psz_filename || !*p_sys->psz_filename )
504 msg_Err( p_this, "logo file not specified" );
505 vlc_object_release( p_input );
506 if( p_sys->psz_filename ) free( p_sys->psz_filename );
511 p_sys->posx = var_CreateGetInteger( p_input->p_libvlc , "logo-x" );
512 p_sys->posy = var_CreateGetInteger( p_input->p_libvlc , "logo-y" );
513 p_sys->pos = var_CreateGetInteger( p_input->p_libvlc , "logo-position" );
515 var_CreateGetInteger( p_input->p_libvlc, "logo-transparency");
516 p_sys->i_trans = __MAX( __MIN( p_sys->i_trans, 255 ), 0 );
518 var_AddCallback( p_input->p_libvlc, "logo-file", LogoCallback, p_sys );
519 var_AddCallback( p_input->p_libvlc, "logo-x", LogoCallback, p_sys );
520 var_AddCallback( p_input->p_libvlc, "logo-y", LogoCallback, p_sys );
521 var_AddCallback( p_input->p_libvlc, "logo-position", LogoCallback, p_sys );
522 var_AddCallback( p_input->p_libvlc, "logo-transparency", LogoCallback, p_sys );
523 vlc_object_release( p_input );
525 p_sys->b_absolute = VLC_TRUE;
526 if( p_sys->posx < 0 || p_sys->posy < 0 )
528 p_sys->b_absolute = VLC_FALSE;
529 p_sys->posx = 0; p_sys->posy = 0;
532 p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
536 msg_Err( p_this, "couldn't load logo file" );
541 p_filter->pf_sub_filter = Filter;
542 p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
543 p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
544 p_sys->b_need_update = VLC_TRUE;
545 p_sys->b_new_image = VLC_FALSE;
546 p_sys->i_last_date = 0;
551 /*****************************************************************************
552 * DestroyFilter: destroy logo video filter
553 *****************************************************************************/
554 static void DestroyFilter( vlc_object_t *p_this )
556 filter_t *p_filter = (filter_t *)p_this;
557 filter_sys_t *p_sys = p_filter->p_sys;
558 vlc_object_t *p_input;
560 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
561 if( p_sys->psz_filename ) free( p_sys->psz_filename );
564 /* Delete the logo variables from INPUT */
565 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
566 if( !p_input ) return;
568 var_Destroy( p_input->p_libvlc , "logo-file" );
569 var_Destroy( p_input->p_libvlc , "logo-x" );
570 var_Destroy( p_input->p_libvlc , "logo-y" );
571 var_Destroy( p_input->p_libvlc , "logo-position" );
572 var_Destroy( p_input->p_libvlc , "logo-transparency" );
573 vlc_object_release( p_input );
576 /*****************************************************************************
577 * Filter: the whole thing
578 *****************************************************************************
579 * This function outputs subpictures at regular time intervals.
580 *****************************************************************************/
581 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
583 filter_sys_t *p_sys = p_filter->p_sys;
585 subpicture_region_t *p_region;
588 if( !p_sys->b_need_update && p_sys->i_last_date +5000000 > date ) return 0;
590 if( p_sys->b_new_image )
592 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
594 p_sys->p_pic = LoadImage( VLC_OBJECT(p_filter), p_sys->psz_filename );
597 p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
598 p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
601 p_sys->b_new_image = VLC_FALSE;
604 p_sys->b_need_update = VLC_FALSE;
606 /* Allocate the subpicture internal data. */
607 p_spu = p_filter->pf_sub_buffer_new( p_filter );
608 if( !p_spu ) return NULL;
610 p_spu->b_absolute = p_sys->b_absolute;
611 p_spu->i_start = p_sys->i_last_date = date;
613 p_spu->b_ephemer = VLC_TRUE;
615 p_sys->b_need_update = VLC_FALSE;
617 if( !p_sys->p_pic || !p_sys->i_trans )
619 /* Send an empty subpicture to clear the display */
623 /* Create new SPU region */
624 memset( &fmt, 0, sizeof(video_format_t) );
625 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
626 fmt.i_aspect = VOUT_ASPECT_FACTOR;
627 fmt.i_sar_num = fmt.i_sar_den = 1;
628 fmt.i_width = fmt.i_visible_width = p_sys->i_width;
629 fmt.i_height = fmt.i_visible_height = p_sys->i_height;
630 fmt.i_x_offset = fmt.i_y_offset = 0;
631 p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
634 msg_Err( p_filter, "cannot allocate SPU region" );
635 p_filter->pf_sub_buffer_del( p_filter, p_spu );
639 vout_CopyPicture( p_filter, &p_region->picture, p_sys->p_pic );
642 p_spu->i_x = p_sys->posx;
643 p_spu->i_y = p_sys->posy;
644 p_spu->i_flags = p_sys->pos;
645 p_spu->p_region = p_region;
646 //p_subpic->i_alpha = p_sys->i_trans;
651 /*****************************************************************************
652 * Callback to update params on the fly
653 *****************************************************************************/
654 static int LogoCallback( vlc_object_t *p_this, char const *psz_var,
655 vlc_value_t oldval, vlc_value_t newval, void *p_data )
657 filter_sys_t *p_sys = (filter_sys_t *)p_data;
659 if( !strncmp( psz_var, "logo-file", 6 ) )
661 if( p_sys->psz_filename ) free( p_sys->psz_filename );
662 p_sys->psz_filename = strdup( newval.psz_string );
663 p_sys->b_new_image = VLC_TRUE;
665 else if ( !strncmp( psz_var, "logo-x", 6 ) )
667 p_sys->posx = newval.i_int;
669 else if ( !strncmp( psz_var, "logo-y", 6 ) )
671 p_sys->posy = newval.i_int;
673 else if ( !strncmp( psz_var, "logo-position", 12 ) )
675 p_sys->pos = newval.i_int;
677 else if ( !strncmp( psz_var, "logo-transparency", 9 ) )
679 p_sys->i_trans = __MAX( __MIN( newval.i_int, 255 ), 0 );
681 p_sys->b_need_update = VLC_TRUE;