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"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Create ( vlc_object_t * );
42 static void Destroy ( vlc_object_t * );
44 static int Init ( vout_thread_t * );
45 static void End ( vout_thread_t * );
46 static void Render ( vout_thread_t *, picture_t * );
48 static int SendEvents( vlc_object_t *, char const *,
49 vlc_value_t, vlc_value_t, void * );
50 static int MouseEvent( vlc_object_t *, char const *,
51 vlc_value_t , vlc_value_t , void * );
52 static int Control ( vout_thread_t *, int, va_list );
54 static int CreateFilter ( vlc_object_t * );
55 static void DestroyFilter( vlc_object_t * );
57 static int LogoCallback( vlc_object_t *, char const *,
58 vlc_value_t, vlc_value_t, void * );
60 /*****************************************************************************
62 *****************************************************************************/
63 #define FILE_TEXT N_("Logo filename")
64 #define FILE_LONGTEXT N_("Full path of the PNG file to use.")
65 #define POSX_TEXT N_("X coordinate of the logo")
66 #define POSX_LONGTEXT N_("You can move the logo by left-clicking on it." )
67 #define POSY_TEXT N_("Y coordinate of the logo")
68 #define POSY_LONGTEXT N_("You can move the logo by left-clicking on it." )
69 #define TRANS_TEXT N_("Transparency of the logo")
70 #define TRANS_LONGTEXT N_("You can set the logo transparency value here " \
71 "(from 0 for full transparency to 255 for full opacity)." )
72 #define POS_TEXT N_("Logo position")
73 #define POS_LONGTEXT N_( \
74 "You can enforce the logo position on the video " \
75 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
76 "also use combinations of these values).")
78 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
79 static char *ppsz_pos_descriptions[] =
80 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
81 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
84 set_description( _("Logo video filter") );
85 set_capability( "video filter", 0 );
86 set_shortname( N_("Logo overlay") );
87 set_category( CAT_VIDEO );
88 set_subcategory( SUBCAT_VIDEO_VFILTER );
89 add_shortcut( "logo" );
90 set_callbacks( Create, Destroy );
92 add_file( "logo-file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, VLC_FALSE );
93 add_integer( "logo-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
94 add_integer( "logo-y", -1, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
95 add_integer_with_range( "logo-transparency", 255, 0, 255, NULL,
96 TRANS_TEXT, TRANS_LONGTEXT, VLC_FALSE );
97 add_integer( "logo-position", 6, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
98 change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
100 /* subpicture filter submodule */
102 set_capability( "sub filter", 0 );
103 set_callbacks( CreateFilter, DestroyFilter );
104 set_description( _("Logo sub filter") );
105 add_shortcut( "logo" );
108 /*****************************************************************************
109 * LoadImage: loads the logo image into memory
110 *****************************************************************************/
111 static picture_t *LoadImage( vlc_object_t *p_this, char *psz_filename )
114 image_handler_t *p_image;
115 video_format_t fmt_in = {0}, fmt_out = {0};
117 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
118 p_image = image_HandlerCreate( p_this );
119 p_pic = image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
120 image_HandlerDelete( p_image );
125 /*****************************************************************************
126 * vout_sys_t: logo video output method descriptor
127 *****************************************************************************
128 * This structure is part of the video output thread descriptor.
129 * It describes the Invert specific properties of an output thread.
130 *****************************************************************************/
133 vout_thread_t *p_vout;
138 int i_width, i_height;
144 /*****************************************************************************
145 * Create: allocates logo video thread output method
146 *****************************************************************************/
147 static int Create( vlc_object_t *p_this )
149 vout_thread_t *p_vout = (vout_thread_t *)p_this;
153 /* Allocate structure */
154 p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
157 msg_Err( p_vout, "out of memory" );
161 p_vout->pf_init = Init;
162 p_vout->pf_end = End;
163 p_vout->pf_manage = NULL;
164 p_vout->pf_render = Render;
165 p_vout->pf_display = NULL;
166 p_vout->pf_control = Control;
168 p_sys->psz_filename = var_CreateGetString( p_this , "logo-file" );
169 if( !p_sys->psz_filename || !*p_sys->psz_filename )
171 msg_Err( p_this, "logo file not specified" );
175 var_Create( p_this, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
176 var_Get( p_this, "logo-position", &val );
177 p_sys->pos = val.i_int;
178 var_Create( p_this, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179 var_Get( p_this, "logo-x", &val );
180 p_sys->posx = val.i_int;
181 var_Create( p_this, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
182 var_Get( p_this, "logo-y", &val );
183 p_sys->posy = val.i_int;
184 var_Create(p_this, "logo-transparency", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
185 var_Get( p_this, "logo-transparency", &val );
186 p_sys->i_trans = __MAX( __MIN( val.i_int, 255 ), 0 );
188 p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
195 p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
196 p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
201 /*****************************************************************************
202 * Init: initialize logo video thread output method
203 *****************************************************************************/
204 static int Init( vout_thread_t *p_vout )
206 vout_sys_t *p_sys = p_vout->p_sys;
210 I_OUTPUTPICTURES = 0;
212 /* Initialize the output structure */
213 p_vout->output.i_chroma = p_vout->render.i_chroma;
214 p_vout->output.i_width = p_vout->render.i_width;
215 p_vout->output.i_height = p_vout->render.i_height;
216 p_vout->output.i_aspect = p_vout->render.i_aspect;
218 /* Load the video blending filter */
219 p_sys->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
220 vlc_object_attach( p_sys->p_blend, p_vout );
221 p_sys->p_blend->fmt_out.video.i_x_offset =
222 p_sys->p_blend->fmt_out.video.i_y_offset = 0;
223 p_sys->p_blend->fmt_in.video.i_x_offset =
224 p_sys->p_blend->fmt_in.video.i_y_offset = 0;
225 p_sys->p_blend->fmt_out.video.i_aspect = p_vout->render.i_aspect;
226 p_sys->p_blend->fmt_out.video.i_chroma = p_vout->output.i_chroma;
227 p_sys->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','A');
228 p_sys->p_blend->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR;
229 p_sys->p_blend->fmt_in.video.i_width =
230 p_sys->p_blend->fmt_in.video.i_visible_width =
231 p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
232 p_sys->p_blend->fmt_in.video.i_height =
233 p_sys->p_blend->fmt_in.video.i_visible_height =
234 p_sys->p_pic->p[Y_PLANE].i_visible_lines;
235 p_sys->p_blend->fmt_out.video.i_width =
236 p_sys->p_blend->fmt_out.video.i_visible_width =
237 p_vout->output.i_width;
238 p_sys->p_blend->fmt_out.video.i_height =
239 p_sys->p_blend->fmt_out.video.i_visible_height =
240 p_vout->output.i_height;
242 p_sys->p_blend->p_module =
243 module_Need( p_sys->p_blend, "video blending", 0, 0 );
244 if( !p_sys->p_blend->p_module )
246 msg_Err( p_vout, "can't open blending filter, aborting" );
247 vlc_object_detach( p_sys->p_blend );
248 vlc_object_destroy( p_sys->p_blend );
252 if( p_sys->posx < 0 || p_sys->posy < 0 )
254 p_sys->posx = 0; p_sys->posy = 0;
256 if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
258 p_sys->posy = p_vout->render.i_height - p_sys->i_height;
260 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
262 p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
265 if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
267 p_sys->posx = p_vout->render.i_width - p_sys->i_width;
269 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
271 p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
275 /* Try to open the real video output */
276 msg_Dbg( p_vout, "spawning the real video output" );
279 vout_Create( p_vout, p_vout->render.i_width, p_vout->render.i_height,
280 p_vout->render.i_chroma, p_vout->render.i_aspect );
282 /* Everything failed */
283 if( p_sys->p_vout == NULL )
285 msg_Err( p_vout, "can't open vout, aborting" );
289 var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
290 var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
292 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
293 ADD_CALLBACKS( p_sys->p_vout, SendEvents );
294 ADD_PARENT_CALLBACKS( SendEventsToChild );
299 /*****************************************************************************
300 * End: terminate logo video thread output method
301 *****************************************************************************/
302 static void End( vout_thread_t *p_vout )
304 vout_sys_t *p_sys = p_vout->p_sys;
307 /* Free the fake output buffers we allocated */
308 for( i_index = I_OUTPUTPICTURES ; i_index ; )
311 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
314 var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
315 var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
319 DEL_CALLBACKS( p_sys->p_vout, SendEvents );
320 vlc_object_detach( p_sys->p_vout );
321 vout_Destroy( p_sys->p_vout );
324 if( p_sys->p_blend->p_module )
325 module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
326 vlc_object_detach( p_sys->p_blend );
327 vlc_object_destroy( p_sys->p_blend );
330 /*****************************************************************************
331 * Destroy: destroy logo video thread output method
332 *****************************************************************************/
333 static void Destroy( vlc_object_t *p_this )
335 vout_thread_t *p_vout = (vout_thread_t *)p_this;
336 vout_sys_t *p_sys = p_vout->p_sys;
338 DEL_PARENT_CALLBACKS( SendEventsToChild );
340 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
344 /*****************************************************************************
345 * Render: render the logo onto the video
346 *****************************************************************************/
347 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
349 vout_sys_t *p_sys = p_vout->p_sys;
352 /* This is a new frame. Get a structure from the video_output. */
353 while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
355 if( p_vout->b_die || p_vout->b_error ) return;
356 msleep( VOUT_OUTMEM_SLEEP );
359 vout_CopyPicture( p_vout, p_outpic, p_pic );
360 vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
362 p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic, p_outpic,
363 p_sys->p_pic, p_sys->posx, p_sys->posy,
366 vout_DisplayPicture( p_sys->p_vout, p_outpic );
369 /*****************************************************************************
370 * SendEvents: forward mouse and keyboard events to the parent p_vout
371 *****************************************************************************/
372 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
373 vlc_value_t oldval, vlc_value_t newval, void *p_data )
375 var_Set( (vlc_object_t *)p_data, psz_var, newval );
379 /*****************************************************************************
380 * MouseEvent: callback for mouse events
381 *****************************************************************************/
382 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
383 vlc_value_t oldval, vlc_value_t newval, void *p_data )
385 vout_thread_t *p_vout = (vout_thread_t*)p_data;
386 vout_sys_t *p_sys = p_vout->p_sys;
390 var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
392 i_delta = newval.i_int - oldval.i_int;
394 if( (valb.i_int & 0x1) == 0 )
399 if( psz_var[6] == 'x' )
402 var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
403 if( newval.i_int >= (int)p_sys->posx &&
404 valy.i_int >= (int)p_sys->posy &&
405 newval.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
406 valy.i_int <= (int)(p_sys->posy + p_sys->i_height) )
408 p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
409 p_vout->output.i_width - p_sys->i_width );
412 else if( psz_var[6] == 'y' )
415 var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
416 if( valx.i_int >= (int)p_sys->posx &&
417 newval.i_int >= (int)p_sys->posy &&
418 valx.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
419 newval.i_int <= (int)(p_sys->posy + p_sys->i_height) )
421 p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
422 p_vout->output.i_height - p_sys->i_height );
429 /*****************************************************************************
430 * Control: control facility for the vout (forwards to child vout)
431 *****************************************************************************/
432 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
434 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
437 /*****************************************************************************
438 * SendEventsToChild: forward events to the child/children vout
439 *****************************************************************************/
440 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
441 vlc_value_t oldval, vlc_value_t newval, void *p_data )
443 vout_thread_t *p_vout = (vout_thread_t *)p_this;
444 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
448 /*****************************************************************************
449 * filter_sys_t: logo filter descriptor
450 *****************************************************************************/
455 int i_width, i_height;
460 vlc_bool_t b_absolute;
464 /* On the fly control variable */
465 vlc_bool_t b_need_update;
466 vlc_bool_t b_new_image;
469 static subpicture_t *Filter( filter_t *, mtime_t );
471 /*****************************************************************************
472 * CreateFilter: allocates logo video filter
473 *****************************************************************************/
474 static int CreateFilter( vlc_object_t *p_this )
476 filter_t *p_filter = (filter_t *)p_this;
478 vlc_object_t *p_input;
480 /* Allocate structure */
481 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
484 msg_Err( p_filter, "out of memory" );
488 /* Hook used for callback variables */
489 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
496 p_sys->psz_filename =
497 var_CreateGetString( p_input->p_libvlc , "logo-file" );
498 if( !p_sys->psz_filename || !*p_sys->psz_filename )
500 msg_Err( p_this, "logo file not specified" );
501 vlc_object_release( p_input );
502 if( p_sys->psz_filename ) free( p_sys->psz_filename );
507 p_sys->posx = var_CreateGetInteger( p_input->p_libvlc , "logo-x" );
508 p_sys->posy = var_CreateGetInteger( p_input->p_libvlc , "logo-y" );
509 p_sys->pos = var_CreateGetInteger( p_input->p_libvlc , "logo-position" );
511 var_CreateGetInteger( p_input->p_libvlc, "logo-transparency");
512 p_sys->i_trans = __MAX( __MIN( p_sys->i_trans, 255 ), 0 );
514 var_AddCallback( p_input->p_libvlc, "logo-file", LogoCallback, p_sys );
515 var_AddCallback( p_input->p_libvlc, "logo-x", LogoCallback, p_sys );
516 var_AddCallback( p_input->p_libvlc, "logo-y", LogoCallback, p_sys );
517 var_AddCallback( p_input->p_libvlc, "logo-position", LogoCallback, p_sys );
518 var_AddCallback( p_input->p_libvlc, "logo-transparency", LogoCallback, p_sys );
519 vlc_object_release( p_input );
521 p_sys->b_absolute = VLC_TRUE;
522 if( p_sys->posx < 0 || p_sys->posy < 0 )
524 p_sys->b_absolute = VLC_FALSE;
525 p_sys->posx = 0; p_sys->posy = 0;
528 p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
532 msg_Err( p_this, "couldn't load logo file" );
537 p_filter->pf_sub_filter = Filter;
538 p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
539 p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
540 p_sys->b_need_update = VLC_TRUE;
541 p_sys->b_new_image = VLC_FALSE;
542 p_sys->i_last_date = 0;
547 /*****************************************************************************
548 * DestroyFilter: destroy logo video filter
549 *****************************************************************************/
550 static void DestroyFilter( vlc_object_t *p_this )
552 filter_t *p_filter = (filter_t *)p_this;
553 filter_sys_t *p_sys = p_filter->p_sys;
554 vlc_object_t *p_input;
556 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
557 if( p_sys->psz_filename ) free( p_sys->psz_filename );
560 /* Delete the logo variables from INPUT */
561 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
562 if( !p_input ) return;
564 var_Destroy( p_input->p_libvlc , "logo-file" );
565 var_Destroy( p_input->p_libvlc , "logo-x" );
566 var_Destroy( p_input->p_libvlc , "logo-y" );
567 var_Destroy( p_input->p_libvlc , "logo-position" );
568 var_Destroy( p_input->p_libvlc , "logo-transparency" );
569 vlc_object_release( p_input );
572 /*****************************************************************************
573 * Filter: the whole thing
574 *****************************************************************************
575 * This function outputs subpictures at regular time intervals.
576 *****************************************************************************/
577 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
579 filter_sys_t *p_sys = p_filter->p_sys;
581 subpicture_region_t *p_region;
584 if( !p_sys->b_need_update && p_sys->i_last_date +5000000 > date ) return 0;
586 if( p_sys->b_new_image )
588 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
590 p_sys->p_pic = LoadImage( VLC_OBJECT(p_filter), p_sys->psz_filename );
593 p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
594 p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
597 p_sys->b_new_image = VLC_FALSE;
600 p_sys->b_need_update = VLC_FALSE;
602 /* Allocate the subpicture internal data. */
603 p_spu = p_filter->pf_sub_buffer_new( p_filter );
604 if( !p_spu ) return NULL;
606 p_spu->b_absolute = p_sys->b_absolute;
607 p_spu->i_start = p_sys->i_last_date = date;
609 p_spu->b_ephemer = VLC_TRUE;
611 p_sys->b_need_update = VLC_FALSE;
613 if( !p_sys->p_pic || !p_sys->i_trans )
615 /* Send an empty subpicture to clear the display */
619 /* Create new SPU region */
620 memset( &fmt, 0, sizeof(video_format_t) );
621 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
622 fmt.i_aspect = VOUT_ASPECT_FACTOR;
623 fmt.i_sar_num = fmt.i_sar_den = 1;
624 fmt.i_width = fmt.i_visible_width = p_sys->i_width;
625 fmt.i_height = fmt.i_visible_height = p_sys->i_height;
626 fmt.i_x_offset = fmt.i_y_offset = 0;
627 p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
630 msg_Err( p_filter, "cannot allocate SPU region" );
631 p_filter->pf_sub_buffer_del( p_filter, p_spu );
635 vout_CopyPicture( p_filter, &p_region->picture, p_sys->p_pic );
638 p_spu->i_x = p_sys->posx;
639 p_spu->i_y = p_sys->posy;
640 p_spu->i_flags = p_sys->pos;
641 p_spu->p_region = p_region;
642 //p_subpic->i_alpha = p_sys->i_trans;
647 /*****************************************************************************
648 * Callback to update params on the fly
649 *****************************************************************************/
650 static int LogoCallback( vlc_object_t *p_this, char const *psz_var,
651 vlc_value_t oldval, vlc_value_t newval, void *p_data )
653 filter_sys_t *p_sys = (filter_sys_t *)p_data;
655 if( !strncmp( psz_var, "logo-file", 6 ) )
657 if( p_sys->psz_filename ) free( p_sys->psz_filename );
658 p_sys->psz_filename = strdup( newval.psz_string );
659 p_sys->b_new_image = VLC_TRUE;
661 else if ( !strncmp( psz_var, "logo-x", 6 ) )
663 p_sys->posx = newval.i_int;
665 else if ( !strncmp( psz_var, "logo-y", 6 ) )
667 p_sys->posy = newval.i_int;
669 else if ( !strncmp( psz_var, "logo-position", 12 ) )
671 p_sys->pos = newval.i_int;
673 else if ( !strncmp( psz_var, "logo-transparency", 9 ) )
675 p_sys->i_trans = __MAX( __MIN( newval.i_int, 255 ), 0 );
677 p_sys->b_need_update = VLC_TRUE;