]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
Copyright fixes
[vlc] / modules / video_filter / logo.c
1 /*****************************************************************************
2  * logo.c : logo video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Simon Latapie <garf@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33
34 #include "vlc_filter.h"
35 #include "filter_common.h"
36 #include "vlc_image.h"
37 #include "osd.h"
38
39 #ifdef LoadImage
40 #   undef LoadImage
41 #endif
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Create    ( vlc_object_t * );
47 static void Destroy   ( vlc_object_t * );
48
49 static int  Init      ( vout_thread_t * );
50 static void End       ( vout_thread_t * );
51 static void Render    ( vout_thread_t *, picture_t * );
52
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 );
58
59 static int  CreateFilter ( vlc_object_t * );
60 static void DestroyFilter( vlc_object_t * );
61
62 static int LogoCallback( vlc_object_t *, char const *,
63                          vlc_value_t, vlc_value_t, void * );
64
65 /*****************************************************************************
66  * Module descriptor
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).")
82
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") };
87
88 vlc_module_begin();
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 );
96
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 );
104
105     /* subpicture filter submodule */
106     add_submodule();
107     set_capability( "sub filter", 0 );
108     set_callbacks( CreateFilter, DestroyFilter );
109     set_description( _("Logo sub filter") );
110     add_shortcut( "logo" );
111 vlc_module_end();
112
113 /*****************************************************************************
114  * LoadImage: loads the logo image into memory
115  *****************************************************************************/
116 static picture_t *LoadImage( vlc_object_t *p_this, char *psz_filename )
117 {
118     picture_t *p_pic;
119     image_handler_t *p_image;
120     video_format_t fmt_in = {0}, fmt_out = {0};
121
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 );
126
127     return p_pic;
128 }
129
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  *****************************************************************************/
136 struct vout_sys_t
137 {
138     vout_thread_t *p_vout;
139
140     filter_t *p_blend;
141     picture_t *p_pic;
142
143     int i_width, i_height;
144     int pos, posx, posy;
145     char *psz_filename;
146     int i_trans;
147 };
148
149 /*****************************************************************************
150  * Create: allocates logo video thread output method
151  *****************************************************************************/
152 static int Create( vlc_object_t *p_this )
153 {
154     vout_thread_t *p_vout = (vout_thread_t *)p_this;
155     vout_sys_t *p_sys;
156     vlc_value_t val;
157
158     /* Allocate structure */
159     p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
160     if( p_sys == NULL )
161     {
162         msg_Err( p_vout, "out of memory" );
163         return VLC_ENOMEM;
164     }
165
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;
172     
173     p_sys->psz_filename = var_CreateGetString( p_this , "logo-file" ); 
174     if( !p_sys->psz_filename || !*p_sys->psz_filename )
175     {
176         msg_Err( p_this, "logo file not specified" );
177         return 0;
178     }
179
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 );
192
193     p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
194     if( !p_sys->p_pic )
195     {
196         free( p_sys );
197         return VLC_EGENERIC;
198     }
199     
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;
202
203     return VLC_SUCCESS;
204 }
205
206 /*****************************************************************************
207  * Init: initialize logo video thread output method
208  *****************************************************************************/
209 static int Init( vout_thread_t *p_vout )
210 {
211     vout_sys_t *p_sys = p_vout->p_sys;
212     picture_t *p_pic;
213     int i_index;
214     video_format_t fmt = {0};
215
216     I_OUTPUTPICTURES = 0;
217
218     /* Initialize the output structure */
219     p_vout->output.i_chroma = p_vout->render.i_chroma;
220     p_vout->output.i_width  = p_vout->render.i_width;
221     p_vout->output.i_height = p_vout->render.i_height;
222     p_vout->output.i_aspect = p_vout->render.i_aspect;
223
224     fmt.i_width = fmt.i_visible_width = p_vout->render.i_width;
225     fmt.i_height = fmt.i_visible_height = p_vout->render.i_height;
226     fmt.i_x_offset = fmt.i_y_offset = 0;
227     fmt.i_chroma = p_vout->render.i_chroma;
228     fmt.i_aspect = p_vout->render.i_aspect;
229     fmt.i_sar_num = p_vout->render.i_aspect * fmt.i_height / fmt.i_width;
230     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
231
232     /* Load the video blending filter */
233     p_sys->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
234     vlc_object_attach( p_sys->p_blend, p_vout );
235     p_sys->p_blend->fmt_out.video.i_x_offset =
236         p_sys->p_blend->fmt_out.video.i_y_offset = 0;
237     p_sys->p_blend->fmt_in.video.i_x_offset =
238         p_sys->p_blend->fmt_in.video.i_y_offset = 0;
239     p_sys->p_blend->fmt_out.video.i_aspect = p_vout->render.i_aspect;
240     p_sys->p_blend->fmt_out.video.i_chroma = p_vout->output.i_chroma;
241     p_sys->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','A');
242     p_sys->p_blend->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR;
243     p_sys->p_blend->fmt_in.video.i_width =
244         p_sys->p_blend->fmt_in.video.i_visible_width =
245             p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
246     p_sys->p_blend->fmt_in.video.i_height =
247         p_sys->p_blend->fmt_in.video.i_visible_height =
248             p_sys->p_pic->p[Y_PLANE].i_visible_lines;
249     p_sys->p_blend->fmt_out.video.i_width =
250         p_sys->p_blend->fmt_out.video.i_visible_width =
251            p_vout->output.i_width;
252     p_sys->p_blend->fmt_out.video.i_height =
253         p_sys->p_blend->fmt_out.video.i_visible_height =
254             p_vout->output.i_height;
255
256     p_sys->p_blend->p_module =
257         module_Need( p_sys->p_blend, "video blending", 0, 0 );
258     if( !p_sys->p_blend->p_module )
259     {
260         msg_Err( p_vout, "can't open blending filter, aborting" );
261         vlc_object_detach( p_sys->p_blend );
262         vlc_object_destroy( p_sys->p_blend );
263         return VLC_EGENERIC;
264     }
265
266     if( p_sys->posx < 0 || p_sys->posy < 0 )
267     {
268         p_sys->posx = 0; p_sys->posy = 0;
269
270         if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
271         {
272             p_sys->posy = p_vout->render.i_height - p_sys->i_height;
273         }
274         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
275         {
276             p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
277         }
278
279         if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
280         {
281             p_sys->posx = p_vout->render.i_width - p_sys->i_width;
282         }
283         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
284         {
285             p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
286         }
287     }
288
289     /* Try to open the real video output */
290     msg_Dbg( p_vout, "spawning the real video output" );
291
292     p_sys->p_vout = vout_Create( p_vout, &fmt );
293
294     /* Everything failed */
295     if( p_sys->p_vout == NULL )
296     {
297         msg_Err( p_vout, "can't open vout, aborting" );
298         return VLC_EGENERIC;
299     }
300
301     var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
302     var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
303
304     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
305     ADD_CALLBACKS( p_sys->p_vout, SendEvents );
306     ADD_PARENT_CALLBACKS( SendEventsToChild );
307
308     return VLC_SUCCESS;
309 }
310
311 /*****************************************************************************
312  * End: terminate logo video thread output method
313  *****************************************************************************/
314 static void End( vout_thread_t *p_vout )
315 {
316     vout_sys_t *p_sys = p_vout->p_sys;
317     int i_index;
318
319     /* Free the fake output buffers we allocated */
320     for( i_index = I_OUTPUTPICTURES ; i_index ; )
321     {
322         i_index--;
323         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
324     }
325
326     var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
327     var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
328
329     if( p_sys->p_vout )
330     {
331         DEL_CALLBACKS( p_sys->p_vout, SendEvents );
332         vlc_object_detach( p_sys->p_vout );
333         vout_Destroy( p_sys->p_vout );
334     }
335
336     if( p_sys->p_blend->p_module )
337         module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
338     vlc_object_detach( p_sys->p_blend );
339     vlc_object_destroy( p_sys->p_blend );
340 }
341
342 /*****************************************************************************
343  * Destroy: destroy logo video thread output method
344  *****************************************************************************/
345 static void Destroy( vlc_object_t *p_this )
346 {
347     vout_thread_t *p_vout = (vout_thread_t *)p_this;
348     vout_sys_t *p_sys = p_vout->p_sys;
349
350     DEL_PARENT_CALLBACKS( SendEventsToChild );
351
352     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
353     free( p_sys );
354 }
355
356 /*****************************************************************************
357  * Render: render the logo onto the video
358  *****************************************************************************/
359 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
360 {
361     vout_sys_t *p_sys = p_vout->p_sys;
362     picture_t *p_outpic;
363
364     /* This is a new frame. Get a structure from the video_output. */
365     while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
366     {
367         if( p_vout->b_die || p_vout->b_error ) return;
368         msleep( VOUT_OUTMEM_SLEEP );
369     }
370
371     vout_CopyPicture( p_vout, p_outpic, p_pic );
372     vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
373
374     p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic, p_outpic,
375                                     p_sys->p_pic, p_sys->posx, p_sys->posy,
376                                     p_sys->i_trans );
377
378     vout_DisplayPicture( p_sys->p_vout, p_outpic );
379 }
380
381 /*****************************************************************************
382  * SendEvents: forward mouse and keyboard events to the parent p_vout
383  *****************************************************************************/
384 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
385                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
386 {
387     var_Set( (vlc_object_t *)p_data, psz_var, newval );
388     return VLC_SUCCESS;
389 }
390
391 /*****************************************************************************
392  * MouseEvent: callback for mouse events
393  *****************************************************************************/
394 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
395                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
396 {
397     vout_thread_t *p_vout = (vout_thread_t*)p_data;
398     vout_sys_t *p_sys = p_vout->p_sys;
399     vlc_value_t valb;
400     int i_delta;
401
402     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
403
404     i_delta = newval.i_int - oldval.i_int;
405
406     if( (valb.i_int & 0x1) == 0 )
407     {
408         return VLC_SUCCESS;
409     }
410
411     if( psz_var[6] == 'x' )
412     {
413         vlc_value_t valy;
414         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
415         if( newval.i_int >= (int)p_sys->posx &&
416             valy.i_int >= (int)p_sys->posy &&
417             newval.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
418             valy.i_int <= (int)(p_sys->posy + p_sys->i_height) )
419         {
420             p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
421                           p_vout->output.i_width - p_sys->i_width );
422         }
423     }
424     else if( psz_var[6] == 'y' )
425     {
426         vlc_value_t valx;
427         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
428         if( valx.i_int >= (int)p_sys->posx &&
429             newval.i_int >= (int)p_sys->posy &&
430             valx.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
431             newval.i_int <= (int)(p_sys->posy + p_sys->i_height) )
432         {
433             p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
434                           p_vout->output.i_height - p_sys->i_height );
435         }
436     }
437
438     return VLC_SUCCESS;
439 }
440
441 /*****************************************************************************
442  * Control: control facility for the vout (forwards to child vout)
443  *****************************************************************************/
444 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
445 {
446     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
447 }
448
449 /*****************************************************************************
450  * SendEventsToChild: forward events to the child/children vout
451  *****************************************************************************/
452 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
453                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
454 {
455     vout_thread_t *p_vout = (vout_thread_t *)p_this;
456     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
457     return VLC_SUCCESS;
458 }
459
460 /*****************************************************************************
461  * filter_sys_t: logo filter descriptor
462  *****************************************************************************/
463 struct filter_sys_t
464 {
465     picture_t *p_pic;
466
467     int i_width, i_height;
468     int pos, posx, posy;
469     char *psz_filename;
470     int i_trans;
471     
472     vlc_bool_t b_absolute;
473
474     mtime_t i_last_date;
475
476     /* On the fly control variable */
477     vlc_bool_t b_need_update;
478     vlc_bool_t b_new_image;
479 };
480
481 static subpicture_t *Filter( filter_t *, mtime_t );
482
483 /*****************************************************************************
484  * CreateFilter: allocates logo video filter
485  *****************************************************************************/
486 static int CreateFilter( vlc_object_t *p_this )
487 {
488     filter_t *p_filter = (filter_t *)p_this;
489     filter_sys_t *p_sys;
490     vlc_object_t *p_input;
491
492     /* Allocate structure */
493     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
494     if( p_sys == NULL )
495     {
496         msg_Err( p_filter, "out of memory" );
497         return VLC_ENOMEM;
498     }
499
500     /* Hook used for callback variables */
501     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
502     if( !p_input )
503     {
504         free( p_sys );
505         return VLC_ENOOBJ;
506     }
507
508     p_sys->psz_filename =
509         var_CreateGetString( p_input->p_libvlc , "logo-file" ); 
510     if( !p_sys->psz_filename || !*p_sys->psz_filename )
511     {
512         msg_Err( p_this, "logo file not specified" );
513         vlc_object_release( p_input );
514         if( p_sys->psz_filename ) free( p_sys->psz_filename );
515         free( p_sys );
516         return VLC_EGENERIC;
517     }
518
519     p_sys->posx = var_CreateGetInteger( p_input->p_libvlc , "logo-x" );
520     p_sys->posy = var_CreateGetInteger( p_input->p_libvlc , "logo-y" );
521     p_sys->pos = var_CreateGetInteger( p_input->p_libvlc , "logo-position" );
522     p_sys->i_trans =
523         var_CreateGetInteger( p_input->p_libvlc, "logo-transparency");
524     p_sys->i_trans = __MAX( __MIN( p_sys->i_trans, 255 ), 0 );
525
526     var_AddCallback( p_input->p_libvlc, "logo-file", LogoCallback, p_sys );
527     var_AddCallback( p_input->p_libvlc, "logo-x", LogoCallback, p_sys );
528     var_AddCallback( p_input->p_libvlc, "logo-y", LogoCallback, p_sys );
529     var_AddCallback( p_input->p_libvlc, "logo-position", LogoCallback, p_sys );
530     var_AddCallback( p_input->p_libvlc, "logo-transparency", LogoCallback, p_sys );
531     vlc_object_release( p_input );
532
533     p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
534     if( !p_sys->p_pic )
535     {
536         free( p_sys );
537         msg_Err( p_this, "couldn't load logo file" );
538         return VLC_EGENERIC;
539     }
540
541     /* Misc init */
542     p_filter->pf_sub_filter = Filter;
543     p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
544     p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
545     p_sys->b_need_update = VLC_TRUE;
546     p_sys->b_new_image = VLC_FALSE;
547     p_sys->i_last_date = 0;
548
549     return VLC_SUCCESS;
550 }
551
552 /*****************************************************************************
553  * DestroyFilter: destroy logo video filter
554  *****************************************************************************/
555 static void DestroyFilter( vlc_object_t *p_this )
556 {
557     filter_t *p_filter = (filter_t *)p_this;
558     filter_sys_t *p_sys = p_filter->p_sys;
559     vlc_object_t *p_input;
560
561     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
562     if( p_sys->psz_filename ) free( p_sys->psz_filename );
563     free( p_sys );
564
565     /* Delete the logo variables from INPUT */
566     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
567     if( !p_input ) return;
568
569     var_Destroy( p_input->p_libvlc , "logo-file" );
570     var_Destroy( p_input->p_libvlc , "logo-x" );
571     var_Destroy( p_input->p_libvlc , "logo-y" );
572     var_Destroy( p_input->p_libvlc , "logo-position" );
573     var_Destroy( p_input->p_libvlc , "logo-transparency" );
574     vlc_object_release( p_input );
575 }
576
577 /*****************************************************************************
578  * Filter: the whole thing
579  *****************************************************************************
580  * This function outputs subpictures at regular time intervals.
581  *****************************************************************************/
582 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
583 {
584     filter_sys_t *p_sys = p_filter->p_sys;
585     subpicture_t *p_spu;
586     subpicture_region_t *p_region;
587     video_format_t fmt;
588
589     if( !p_sys->b_need_update && p_sys->i_last_date +5000000 > date ) return 0;
590
591     if( p_sys->b_new_image )
592     {
593         if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
594
595         p_sys->p_pic = LoadImage( VLC_OBJECT(p_filter), p_sys->psz_filename );
596         if( p_sys->p_pic )
597         {
598             p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
599             p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
600         }
601
602         p_sys->b_new_image = VLC_FALSE;
603     }
604
605     p_sys->b_need_update = VLC_FALSE;
606
607     /* Allocate the subpicture internal data. */
608     p_spu = p_filter->pf_sub_buffer_new( p_filter );
609     if( !p_spu ) return NULL;
610
611     p_spu->b_absolute = p_sys->b_absolute;
612     p_spu->i_start = p_sys->i_last_date = date;
613     p_spu->i_stop = 0;
614     p_spu->b_ephemer = VLC_TRUE;
615
616     p_sys->b_need_update = VLC_FALSE;
617
618     if( !p_sys->p_pic || !p_sys->i_trans )
619     {
620         /* Send an empty subpicture to clear the display */
621         return p_spu;
622     }
623
624     /* Create new SPU region */
625     memset( &fmt, 0, sizeof(video_format_t) );
626     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
627     fmt.i_aspect = VOUT_ASPECT_FACTOR;
628     fmt.i_sar_num = fmt.i_sar_den = 1;
629     fmt.i_width = fmt.i_visible_width = p_sys->i_width;
630     fmt.i_height = fmt.i_visible_height = p_sys->i_height;
631     fmt.i_x_offset = fmt.i_y_offset = 0;
632     p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
633     if( !p_region )
634     {
635         msg_Err( p_filter, "cannot allocate SPU region" );
636         p_filter->pf_sub_buffer_del( p_filter, p_spu );
637         return NULL;
638     }
639
640     vout_CopyPicture( p_filter, &p_region->picture, p_sys->p_pic );
641
642     /*  where to locate the logo: */
643     if( p_sys->posx < 0 || p_sys->posy < 0 )
644     {   /* set to one of the 9 relative locations */
645         p_spu->i_flags = p_sys->pos;
646         p_spu->i_x = 0;
647         p_spu->i_y = 0;
648         p_spu->b_absolute = VLC_FALSE;
649     }
650     else
651     {   /*  set to an absolute xy, referenced to upper left corner */
652             p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
653         p_spu->i_x = p_sys->posx;
654         p_spu->i_y = p_sys->posy;
655         p_spu->b_absolute = VLC_TRUE;
656     }
657
658     p_spu->p_region = p_region;
659     p_spu->i_alpha = p_sys->i_trans;
660
661     return p_spu;
662 }
663
664 /*****************************************************************************
665  * Callback to update params on the fly
666  *****************************************************************************/
667 static int LogoCallback( vlc_object_t *p_this, char const *psz_var,
668                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
669 {
670     filter_sys_t *p_sys = (filter_sys_t *)p_data;
671
672     if( !strncmp( psz_var, "logo-file", 6 ) )
673     {
674         if( p_sys->psz_filename ) free( p_sys->psz_filename );
675         p_sys->psz_filename = strdup( newval.psz_string ); 
676         p_sys->b_new_image = VLC_TRUE;
677     }
678     else if ( !strncmp( psz_var, "logo-x", 6 ) )
679     {
680         p_sys->posx = newval.i_int;
681     }
682     else if ( !strncmp( psz_var, "logo-y", 6 ) )
683     {
684         p_sys->posy = newval.i_int;
685     }
686     else if ( !strncmp( psz_var, "logo-position", 12 ) )
687     {
688         p_sys->pos = newval.i_int;
689     }
690     else if ( !strncmp( psz_var, "logo-transparency", 9 ) )
691     {
692         p_sys->i_trans = __MAX( __MIN( newval.i_int, 255 ), 0 );
693     }
694     p_sys->b_need_update = VLC_TRUE;
695     return VLC_SUCCESS;
696 }