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"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Create ( vlc_object_t * );
47 static void Destroy ( vlc_object_t * );
49 static int Init ( vout_thread_t * );
50 static void End ( vout_thread_t * );
51 static void Render ( vout_thread_t *, picture_t * );
53 static int SendEvents( vlc_object_t *, char const *,
54 vlc_value_t, vlc_value_t, void * );
55 static int MouseEvent( vlc_object_t *, char const *,
56 vlc_value_t , vlc_value_t , void * );
57 static int Control ( vout_thread_t *, int, va_list );
59 static int CreateFilter ( vlc_object_t * );
60 static void DestroyFilter( vlc_object_t * );
62 static int LogoCallback( vlc_object_t *, char const *,
63 vlc_value_t, vlc_value_t, void * );
65 /*****************************************************************************
67 *****************************************************************************/
68 #define FILE_TEXT N_("Logo filename")
69 #define FILE_LONGTEXT N_("Full path of the PNG file to use.")
70 #define POSX_TEXT N_("X coordinate of the logo")
71 #define POSX_LONGTEXT N_("You can move the logo by left-clicking on it." )
72 #define POSY_TEXT N_("Y coordinate of the logo")
73 #define POSY_LONGTEXT N_("You can move the logo by left-clicking on it." )
74 #define TRANS_TEXT N_("Transparency of the logo")
75 #define TRANS_LONGTEXT N_("You can set the logo transparency value here " \
76 "(from 0 for full transparency to 255 for full opacity)." )
77 #define POS_TEXT N_("Logo position")
78 #define POS_LONGTEXT N_( \
79 "You can enforce the logo position on the video " \
80 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
81 "also use combinations of these values).")
83 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
84 static char *ppsz_pos_descriptions[] =
85 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
86 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
89 set_description( _("Logo video filter") );
90 set_capability( "video filter", 0 );
91 set_shortname( N_("Logo overlay") );
92 set_category( CAT_VIDEO );
93 set_subcategory( SUBCAT_VIDEO_SUBPIC );
94 add_shortcut( "logo" );
95 set_callbacks( Create, Destroy );
97 add_file( "logo-file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, VLC_FALSE );
98 add_integer( "logo-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
99 add_integer( "logo-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
100 add_integer_with_range( "logo-transparency", 255, 0, 255, NULL,
101 TRANS_TEXT, TRANS_LONGTEXT, VLC_FALSE );
102 add_integer( "logo-position", 6, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
103 change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
105 /* subpicture filter submodule */
107 set_capability( "sub filter", 0 );
108 set_callbacks( CreateFilter, DestroyFilter );
109 set_description( _("Logo sub filter") );
110 add_shortcut( "logo" );
113 /*****************************************************************************
114 * LoadImage: loads the logo image into memory
115 *****************************************************************************/
116 static picture_t *LoadImage( vlc_object_t *p_this, char *psz_filename )
119 image_handler_t *p_image;
120 video_format_t fmt_in = {0}, fmt_out = {0};
122 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
123 p_image = image_HandlerCreate( p_this );
124 p_pic = image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
125 image_HandlerDelete( p_image );
130 /*****************************************************************************
131 * vout_sys_t: logo video output method descriptor
132 *****************************************************************************
133 * This structure is part of the video output thread descriptor.
134 * It describes the Invert specific properties of an output thread.
135 *****************************************************************************/
138 vout_thread_t *p_vout;
143 int i_width, i_height;
149 /*****************************************************************************
150 * Create: allocates logo video thread output method
151 *****************************************************************************/
152 static int Create( vlc_object_t *p_this )
154 vout_thread_t *p_vout = (vout_thread_t *)p_this;
158 /* Allocate structure */
159 p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
162 msg_Err( p_vout, "out of memory" );
166 p_vout->pf_init = Init;
167 p_vout->pf_end = End;
168 p_vout->pf_manage = NULL;
169 p_vout->pf_render = Render;
170 p_vout->pf_display = NULL;
171 p_vout->pf_control = Control;
173 p_sys->psz_filename = var_CreateGetString( p_this , "logo-file" );
174 if( !p_sys->psz_filename || !*p_sys->psz_filename )
176 msg_Err( p_this, "logo file not specified" );
180 var_Create( p_this, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
181 var_Get( p_this, "logo-position", &val );
182 p_sys->pos = val.i_int;
183 var_Create( p_this, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
184 var_Get( p_this, "logo-x", &val );
185 p_sys->posx = val.i_int;
186 var_Create( p_this, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
187 var_Get( p_this, "logo-y", &val );
188 p_sys->posy = val.i_int;
189 var_Create(p_this, "logo-transparency", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
190 var_Get( p_this, "logo-transparency", &val );
191 p_sys->i_trans = __MAX( __MIN( val.i_int, 255 ), 0 );
193 p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
200 p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
201 p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
206 /*****************************************************************************
207 * Init: initialize logo video thread output method
208 *****************************************************************************/
209 static int Init( vout_thread_t *p_vout )
211 vout_sys_t *p_sys = p_vout->p_sys;
215 I_OUTPUTPICTURES = 0;
217 /* Initialize the output structure */
218 p_vout->output.i_chroma = p_vout->render.i_chroma;
219 p_vout->output.i_width = p_vout->render.i_width;
220 p_vout->output.i_height = p_vout->render.i_height;
221 p_vout->output.i_aspect = p_vout->render.i_aspect;
223 /* Load the video blending filter */
224 p_sys->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
225 vlc_object_attach( p_sys->p_blend, p_vout );
226 p_sys->p_blend->fmt_out.video.i_x_offset =
227 p_sys->p_blend->fmt_out.video.i_y_offset = 0;
228 p_sys->p_blend->fmt_in.video.i_x_offset =
229 p_sys->p_blend->fmt_in.video.i_y_offset = 0;
230 p_sys->p_blend->fmt_out.video.i_aspect = p_vout->render.i_aspect;
231 p_sys->p_blend->fmt_out.video.i_chroma = p_vout->output.i_chroma;
232 p_sys->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','A');
233 p_sys->p_blend->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR;
234 p_sys->p_blend->fmt_in.video.i_width =
235 p_sys->p_blend->fmt_in.video.i_visible_width =
236 p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
237 p_sys->p_blend->fmt_in.video.i_height =
238 p_sys->p_blend->fmt_in.video.i_visible_height =
239 p_sys->p_pic->p[Y_PLANE].i_visible_lines;
240 p_sys->p_blend->fmt_out.video.i_width =
241 p_sys->p_blend->fmt_out.video.i_visible_width =
242 p_vout->output.i_width;
243 p_sys->p_blend->fmt_out.video.i_height =
244 p_sys->p_blend->fmt_out.video.i_visible_height =
245 p_vout->output.i_height;
247 p_sys->p_blend->p_module =
248 module_Need( p_sys->p_blend, "video blending", 0, 0 );
249 if( !p_sys->p_blend->p_module )
251 msg_Err( p_vout, "can't open blending filter, aborting" );
252 vlc_object_detach( p_sys->p_blend );
253 vlc_object_destroy( p_sys->p_blend );
257 if( p_sys->posx < 0 || p_sys->posy < 0 )
259 p_sys->posx = 0; p_sys->posy = 0;
261 if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
263 p_sys->posy = p_vout->render.i_height - p_sys->i_height;
265 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
267 p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
270 if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
272 p_sys->posx = p_vout->render.i_width - p_sys->i_width;
274 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
276 p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
280 /* Try to open the real video output */
281 msg_Dbg( p_vout, "spawning the real video output" );
284 vout_Create( p_vout, p_vout->render.i_width, p_vout->render.i_height,
285 p_vout->render.i_chroma, p_vout->render.i_aspect );
287 /* Everything failed */
288 if( p_sys->p_vout == NULL )
290 msg_Err( p_vout, "can't open vout, aborting" );
294 var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
295 var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
297 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
298 ADD_CALLBACKS( p_sys->p_vout, SendEvents );
299 ADD_PARENT_CALLBACKS( SendEventsToChild );
304 /*****************************************************************************
305 * End: terminate logo video thread output method
306 *****************************************************************************/
307 static void End( vout_thread_t *p_vout )
309 vout_sys_t *p_sys = p_vout->p_sys;
312 /* Free the fake output buffers we allocated */
313 for( i_index = I_OUTPUTPICTURES ; i_index ; )
316 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
319 var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
320 var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
324 DEL_CALLBACKS( p_sys->p_vout, SendEvents );
325 vlc_object_detach( p_sys->p_vout );
326 vout_Destroy( p_sys->p_vout );
329 if( p_sys->p_blend->p_module )
330 module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
331 vlc_object_detach( p_sys->p_blend );
332 vlc_object_destroy( p_sys->p_blend );
335 /*****************************************************************************
336 * Destroy: destroy logo video thread output method
337 *****************************************************************************/
338 static void Destroy( vlc_object_t *p_this )
340 vout_thread_t *p_vout = (vout_thread_t *)p_this;
341 vout_sys_t *p_sys = p_vout->p_sys;
343 DEL_PARENT_CALLBACKS( SendEventsToChild );
345 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
349 /*****************************************************************************
350 * Render: render the logo onto the video
351 *****************************************************************************/
352 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
354 vout_sys_t *p_sys = p_vout->p_sys;
357 /* This is a new frame. Get a structure from the video_output. */
358 while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
360 if( p_vout->b_die || p_vout->b_error ) return;
361 msleep( VOUT_OUTMEM_SLEEP );
364 vout_CopyPicture( p_vout, p_outpic, p_pic );
365 vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
367 p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic, p_outpic,
368 p_sys->p_pic, p_sys->posx, p_sys->posy,
371 vout_DisplayPicture( p_sys->p_vout, p_outpic );
374 /*****************************************************************************
375 * SendEvents: forward mouse and keyboard events to the parent p_vout
376 *****************************************************************************/
377 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
378 vlc_value_t oldval, vlc_value_t newval, void *p_data )
380 var_Set( (vlc_object_t *)p_data, psz_var, newval );
384 /*****************************************************************************
385 * MouseEvent: callback for mouse events
386 *****************************************************************************/
387 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
388 vlc_value_t oldval, vlc_value_t newval, void *p_data )
390 vout_thread_t *p_vout = (vout_thread_t*)p_data;
391 vout_sys_t *p_sys = p_vout->p_sys;
395 var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
397 i_delta = newval.i_int - oldval.i_int;
399 if( (valb.i_int & 0x1) == 0 )
404 if( psz_var[6] == 'x' )
407 var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
408 if( newval.i_int >= (int)p_sys->posx &&
409 valy.i_int >= (int)p_sys->posy &&
410 newval.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
411 valy.i_int <= (int)(p_sys->posy + p_sys->i_height) )
413 p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
414 p_vout->output.i_width - p_sys->i_width );
417 else if( psz_var[6] == 'y' )
420 var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
421 if( valx.i_int >= (int)p_sys->posx &&
422 newval.i_int >= (int)p_sys->posy &&
423 valx.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
424 newval.i_int <= (int)(p_sys->posy + p_sys->i_height) )
426 p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
427 p_vout->output.i_height - p_sys->i_height );
434 /*****************************************************************************
435 * Control: control facility for the vout (forwards to child vout)
436 *****************************************************************************/
437 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
439 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
442 /*****************************************************************************
443 * SendEventsToChild: forward events to the child/children vout
444 *****************************************************************************/
445 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
446 vlc_value_t oldval, vlc_value_t newval, void *p_data )
448 vout_thread_t *p_vout = (vout_thread_t *)p_this;
449 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
453 /*****************************************************************************
454 * filter_sys_t: logo filter descriptor
455 *****************************************************************************/
460 int i_width, i_height;
465 vlc_bool_t b_absolute;
469 /* On the fly control variable */
470 vlc_bool_t b_need_update;
471 vlc_bool_t b_new_image;
474 static subpicture_t *Filter( filter_t *, mtime_t );
476 /*****************************************************************************
477 * CreateFilter: allocates logo video filter
478 *****************************************************************************/
479 static int CreateFilter( vlc_object_t *p_this )
481 filter_t *p_filter = (filter_t *)p_this;
483 vlc_object_t *p_input;
485 /* Allocate structure */
486 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
489 msg_Err( p_filter, "out of memory" );
493 /* Hook used for callback variables */
494 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
501 p_sys->psz_filename =
502 var_CreateGetString( p_input->p_libvlc , "logo-file" );
503 if( !p_sys->psz_filename || !*p_sys->psz_filename )
505 msg_Err( p_this, "logo file not specified" );
506 vlc_object_release( p_input );
507 if( p_sys->psz_filename ) free( p_sys->psz_filename );
512 p_sys->posx = var_CreateGetInteger( p_input->p_libvlc , "logo-x" );
513 p_sys->posy = var_CreateGetInteger( p_input->p_libvlc , "logo-y" );
514 p_sys->pos = var_CreateGetInteger( p_input->p_libvlc , "logo-position" );
516 var_CreateGetInteger( p_input->p_libvlc, "logo-transparency");
517 p_sys->i_trans = __MAX( __MIN( p_sys->i_trans, 255 ), 0 );
519 var_AddCallback( p_input->p_libvlc, "logo-file", LogoCallback, p_sys );
520 var_AddCallback( p_input->p_libvlc, "logo-x", LogoCallback, p_sys );
521 var_AddCallback( p_input->p_libvlc, "logo-y", LogoCallback, p_sys );
522 var_AddCallback( p_input->p_libvlc, "logo-position", LogoCallback, p_sys );
523 var_AddCallback( p_input->p_libvlc, "logo-transparency", LogoCallback, p_sys );
524 vlc_object_release( p_input );
526 p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
530 msg_Err( p_this, "couldn't load logo file" );
535 p_filter->pf_sub_filter = Filter;
536 p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
537 p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
538 p_sys->b_need_update = VLC_TRUE;
539 p_sys->b_new_image = VLC_FALSE;
540 p_sys->i_last_date = 0;
545 /*****************************************************************************
546 * DestroyFilter: destroy logo video filter
547 *****************************************************************************/
548 static void DestroyFilter( vlc_object_t *p_this )
550 filter_t *p_filter = (filter_t *)p_this;
551 filter_sys_t *p_sys = p_filter->p_sys;
552 vlc_object_t *p_input;
554 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
555 if( p_sys->psz_filename ) free( p_sys->psz_filename );
558 /* Delete the logo variables from INPUT */
559 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
560 if( !p_input ) return;
562 var_Destroy( p_input->p_libvlc , "logo-file" );
563 var_Destroy( p_input->p_libvlc , "logo-x" );
564 var_Destroy( p_input->p_libvlc , "logo-y" );
565 var_Destroy( p_input->p_libvlc , "logo-position" );
566 var_Destroy( p_input->p_libvlc , "logo-transparency" );
567 vlc_object_release( p_input );
570 /*****************************************************************************
571 * Filter: the whole thing
572 *****************************************************************************
573 * This function outputs subpictures at regular time intervals.
574 *****************************************************************************/
575 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
577 filter_sys_t *p_sys = p_filter->p_sys;
579 subpicture_region_t *p_region;
582 if( !p_sys->b_need_update && p_sys->i_last_date +5000000 > date ) return 0;
584 if( p_sys->b_new_image )
586 if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
588 p_sys->p_pic = LoadImage( VLC_OBJECT(p_filter), p_sys->psz_filename );
591 p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
592 p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
595 p_sys->b_new_image = VLC_FALSE;
598 p_sys->b_need_update = VLC_FALSE;
600 /* Allocate the subpicture internal data. */
601 p_spu = p_filter->pf_sub_buffer_new( p_filter );
602 if( !p_spu ) return NULL;
604 p_spu->b_absolute = p_sys->b_absolute;
605 p_spu->i_start = p_sys->i_last_date = date;
607 p_spu->b_ephemer = VLC_TRUE;
609 p_sys->b_need_update = VLC_FALSE;
611 if( !p_sys->p_pic || !p_sys->i_trans )
613 /* Send an empty subpicture to clear the display */
617 /* Create new SPU region */
618 memset( &fmt, 0, sizeof(video_format_t) );
619 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
620 fmt.i_aspect = VOUT_ASPECT_FACTOR;
621 fmt.i_sar_num = fmt.i_sar_den = 1;
622 fmt.i_width = fmt.i_visible_width = p_sys->i_width;
623 fmt.i_height = fmt.i_visible_height = p_sys->i_height;
624 fmt.i_x_offset = fmt.i_y_offset = 0;
625 p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
628 msg_Err( p_filter, "cannot allocate SPU region" );
629 p_filter->pf_sub_buffer_del( p_filter, p_spu );
633 vout_CopyPicture( p_filter, &p_region->picture, p_sys->p_pic );
635 /* where to locate the logo: */
636 if( p_sys->posx < 0 || p_sys->posy < 0 )
637 { /* set to one of the 9 relative locations */
638 p_spu->i_flags = p_sys->pos;
641 p_spu->b_absolute = VLC_FALSE;
644 { /* set to an absolute xy, referenced to upper left corner */
645 p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
646 p_spu->i_x = p_sys->posx;
647 p_spu->i_y = p_sys->posy;
648 p_spu->b_absolute = VLC_TRUE;
651 p_spu->p_region = p_region;
652 p_spu->i_alpha = p_sys->i_trans;
657 /*****************************************************************************
658 * Callback to update params on the fly
659 *****************************************************************************/
660 static int LogoCallback( vlc_object_t *p_this, char const *psz_var,
661 vlc_value_t oldval, vlc_value_t newval, void *p_data )
663 filter_sys_t *p_sys = (filter_sys_t *)p_data;
665 if( !strncmp( psz_var, "logo-file", 6 ) )
667 if( p_sys->psz_filename ) free( p_sys->psz_filename );
668 p_sys->psz_filename = strdup( newval.psz_string );
669 p_sys->b_new_image = VLC_TRUE;
671 else if ( !strncmp( psz_var, "logo-x", 6 ) )
673 p_sys->posx = newval.i_int;
675 else if ( !strncmp( psz_var, "logo-y", 6 ) )
677 p_sys->posy = newval.i_int;
679 else if ( !strncmp( psz_var, "logo-position", 12 ) )
681 p_sys->pos = newval.i_int;
683 else if ( !strncmp( psz_var, "logo-transparency", 9 ) )
685 p_sys->i_trans = __MAX( __MIN( newval.i_int, 255 ), 0 );
687 p_sys->b_need_update = VLC_TRUE;