]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
* modules/video_filter/logo.c: --logo-transparency works again.
[vlc] / modules / video_filter / logo.c
1 /*****************************************************************************
2  * logo.c : logo video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
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
38 #ifdef LoadImage
39 #   undef LoadImage
40 #endif
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  Create    ( vlc_object_t * );
46 static void Destroy   ( vlc_object_t * );
47
48 static int  Init      ( vout_thread_t * );
49 static void End       ( vout_thread_t * );
50 static void Render    ( vout_thread_t *, picture_t * );
51
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 );
57
58 static int  CreateFilter ( vlc_object_t * );
59 static void DestroyFilter( vlc_object_t * );
60
61 static int LogoCallback( vlc_object_t *, char const *,
62                          vlc_value_t, vlc_value_t, void * );
63
64 /*****************************************************************************
65  * Module descriptor
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).")
81
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") };
86
87 vlc_module_begin();
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 );
95
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 );
103
104     /* subpicture filter submodule */
105     add_submodule();
106     set_capability( "sub filter", 0 );
107     set_callbacks( CreateFilter, DestroyFilter );
108     set_description( _("Logo sub filter") );
109     add_shortcut( "logo" );
110 vlc_module_end();
111
112 /*****************************************************************************
113  * LoadImage: loads the logo image into memory
114  *****************************************************************************/
115 static picture_t *LoadImage( vlc_object_t *p_this, char *psz_filename )
116 {
117     picture_t *p_pic;
118     image_handler_t *p_image;
119     video_format_t fmt_in = {0}, fmt_out = {0};
120
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 );
125
126     return p_pic;
127 }
128
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  *****************************************************************************/
135 struct vout_sys_t
136 {
137     vout_thread_t *p_vout;
138
139     filter_t *p_blend;
140     picture_t *p_pic;
141
142     int i_width, i_height;
143     int pos, posx, posy;
144     char *psz_filename;
145     int i_trans;
146 };
147
148 /*****************************************************************************
149  * Create: allocates logo video thread output method
150  *****************************************************************************/
151 static int Create( vlc_object_t *p_this )
152 {
153     vout_thread_t *p_vout = (vout_thread_t *)p_this;
154     vout_sys_t *p_sys;
155     vlc_value_t val;
156
157     /* Allocate structure */
158     p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
159     if( p_sys == NULL )
160     {
161         msg_Err( p_vout, "out of memory" );
162         return VLC_ENOMEM;
163     }
164
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;
171     
172     p_sys->psz_filename = var_CreateGetString( p_this , "logo-file" ); 
173     if( !p_sys->psz_filename || !*p_sys->psz_filename )
174     {
175         msg_Err( p_this, "logo file not specified" );
176         return 0;
177     }
178
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 );
191
192     p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
193     if( !p_sys->p_pic )
194     {
195         free( p_sys );
196         return VLC_EGENERIC;
197     }
198
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;
201
202     return VLC_SUCCESS;
203 }
204
205 /*****************************************************************************
206  * Init: initialize logo video thread output method
207  *****************************************************************************/
208 static int Init( vout_thread_t *p_vout )
209 {
210     vout_sys_t *p_sys = p_vout->p_sys;
211     picture_t *p_pic;
212     int i_index;
213
214     I_OUTPUTPICTURES = 0;
215
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;
221
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;
245
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 )
249     {
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 );
253         return VLC_EGENERIC;
254     }
255
256     if( p_sys->posx < 0 || p_sys->posy < 0 )
257     {
258         p_sys->posx = 0; p_sys->posy = 0;
259
260         if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
261         {
262             p_sys->posy = p_vout->render.i_height - p_sys->i_height;
263         }
264         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
265         {
266             p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
267         }
268
269         if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
270         {
271             p_sys->posx = p_vout->render.i_width - p_sys->i_width;
272         }
273         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
274         {
275             p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
276         }
277     }
278
279     /* Try to open the real video output */
280     msg_Dbg( p_vout, "spawning the real video output" );
281
282     p_sys->p_vout =
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 );
285
286     /* Everything failed */
287     if( p_sys->p_vout == NULL )
288     {
289         msg_Err( p_vout, "can't open vout, aborting" );
290         return VLC_EGENERIC;
291     }
292
293     var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
294     var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
295
296     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
297     ADD_CALLBACKS( p_sys->p_vout, SendEvents );
298     ADD_PARENT_CALLBACKS( SendEventsToChild );
299
300     return VLC_SUCCESS;
301 }
302
303 /*****************************************************************************
304  * End: terminate logo video thread output method
305  *****************************************************************************/
306 static void End( vout_thread_t *p_vout )
307 {
308     vout_sys_t *p_sys = p_vout->p_sys;
309     int i_index;
310
311     /* Free the fake output buffers we allocated */
312     for( i_index = I_OUTPUTPICTURES ; i_index ; )
313     {
314         i_index--;
315         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
316     }
317
318     var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
319     var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
320
321     if( p_sys->p_vout )
322     {
323         DEL_CALLBACKS( p_sys->p_vout, SendEvents );
324         vlc_object_detach( p_sys->p_vout );
325         vout_Destroy( p_sys->p_vout );
326     }
327
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 );
332 }
333
334 /*****************************************************************************
335  * Destroy: destroy logo video thread output method
336  *****************************************************************************/
337 static void Destroy( vlc_object_t *p_this )
338 {
339     vout_thread_t *p_vout = (vout_thread_t *)p_this;
340     vout_sys_t *p_sys = p_vout->p_sys;
341
342     DEL_PARENT_CALLBACKS( SendEventsToChild );
343
344     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
345     free( p_sys );
346 }
347
348 /*****************************************************************************
349  * Render: render the logo onto the video
350  *****************************************************************************/
351 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
352 {
353     vout_sys_t *p_sys = p_vout->p_sys;
354     picture_t *p_outpic;
355
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 )) )
358     {
359         if( p_vout->b_die || p_vout->b_error ) return;
360         msleep( VOUT_OUTMEM_SLEEP );
361     }
362
363     vout_CopyPicture( p_vout, p_outpic, p_pic );
364     vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
365
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,
368                                     p_sys->i_trans );
369
370     vout_DisplayPicture( p_sys->p_vout, p_outpic );
371 }
372
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 )
378 {
379     var_Set( (vlc_object_t *)p_data, psz_var, newval );
380     return VLC_SUCCESS;
381 }
382
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 )
388 {
389     vout_thread_t *p_vout = (vout_thread_t*)p_data;
390     vout_sys_t *p_sys = p_vout->p_sys;
391     vlc_value_t valb;
392     int i_delta;
393
394     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
395
396     i_delta = newval.i_int - oldval.i_int;
397
398     if( (valb.i_int & 0x1) == 0 )
399     {
400         return VLC_SUCCESS;
401     }
402
403     if( psz_var[6] == 'x' )
404     {
405         vlc_value_t valy;
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) )
411         {
412             p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
413                           p_vout->output.i_width - p_sys->i_width );
414         }
415     }
416     else if( psz_var[6] == 'y' )
417     {
418         vlc_value_t valx;
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) )
424         {
425             p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
426                           p_vout->output.i_height - p_sys->i_height );
427         }
428     }
429
430     return VLC_SUCCESS;
431 }
432
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 )
437 {
438     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
439 }
440
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 )
446 {
447     vout_thread_t *p_vout = (vout_thread_t *)p_this;
448     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
449     return VLC_SUCCESS;
450 }
451
452 /*****************************************************************************
453  * filter_sys_t: logo filter descriptor
454  *****************************************************************************/
455 struct filter_sys_t
456 {
457     picture_t *p_pic;
458
459     int i_width, i_height;
460     int pos, posx, posy;
461     char *psz_filename;
462     int i_trans;
463     
464     vlc_bool_t b_absolute;
465
466     mtime_t i_last_date;
467
468     /* On the fly control variable */
469     vlc_bool_t b_need_update;
470     vlc_bool_t b_new_image;
471 };
472
473 static subpicture_t *Filter( filter_t *, mtime_t );
474
475 /*****************************************************************************
476  * CreateFilter: allocates logo video filter
477  *****************************************************************************/
478 static int CreateFilter( vlc_object_t *p_this )
479 {
480     filter_t *p_filter = (filter_t *)p_this;
481     filter_sys_t *p_sys;
482     vlc_object_t *p_input;
483
484     /* Allocate structure */
485     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
486     if( p_sys == NULL )
487     {
488         msg_Err( p_filter, "out of memory" );
489         return VLC_ENOMEM;
490     }
491
492     /* Hook used for callback variables */
493     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
494     if( !p_input )
495     {
496         free( p_sys );
497         return VLC_ENOOBJ;
498     }
499
500     p_sys->psz_filename =
501         var_CreateGetString( p_input->p_libvlc , "logo-file" ); 
502     if( !p_sys->psz_filename || !*p_sys->psz_filename )
503     {
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 );
507         free( p_sys );
508         return VLC_EGENERIC;
509     }
510
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" );
514     p_sys->i_trans =
515         var_CreateGetInteger( p_input->p_libvlc, "logo-transparency");
516     p_sys->i_trans = __MAX( __MIN( p_sys->i_trans, 255 ), 0 );
517
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 );
524
525     p_sys->b_absolute = VLC_TRUE;
526     if( p_sys->posx < 0 || p_sys->posy < 0 )
527     {
528         p_sys->b_absolute = VLC_FALSE;
529         p_sys->posx = 0; p_sys->posy = 0;
530     }
531
532     p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
533     if( !p_sys->p_pic )
534     {
535         free( p_sys );
536         msg_Err( p_this, "couldn't load logo file" );
537         return VLC_EGENERIC;
538     }
539
540     /* Misc init */
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;
547
548     return VLC_SUCCESS;
549 }
550
551 /*****************************************************************************
552  * DestroyFilter: destroy logo video filter
553  *****************************************************************************/
554 static void DestroyFilter( vlc_object_t *p_this )
555 {
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;
559
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 );
562     free( p_sys );
563
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;
567
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 );
574 }
575
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 )
582 {
583     filter_sys_t *p_sys = p_filter->p_sys;
584     subpicture_t *p_spu;
585     subpicture_region_t *p_region;
586     video_format_t fmt;
587
588     if( !p_sys->b_need_update && p_sys->i_last_date +5000000 > date ) return 0;
589
590     if( p_sys->b_new_image )
591     {
592         if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
593
594         p_sys->p_pic = LoadImage( VLC_OBJECT(p_filter), p_sys->psz_filename );
595         if( p_sys->p_pic )
596         {
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;
599         }
600
601         p_sys->b_new_image = VLC_FALSE;
602     }
603
604     p_sys->b_need_update = VLC_FALSE;
605
606     /* Allocate the subpicture internal data. */
607     p_spu = p_filter->pf_sub_buffer_new( p_filter );
608     if( !p_spu ) return NULL;
609
610     p_spu->b_absolute = p_sys->b_absolute;
611     p_spu->i_start = p_sys->i_last_date = date;
612     p_spu->i_stop = 0;
613     p_spu->b_ephemer = VLC_TRUE;
614
615     p_sys->b_need_update = VLC_FALSE;
616
617     if( !p_sys->p_pic || !p_sys->i_trans )
618     {
619         /* Send an empty subpicture to clear the display */
620         return p_spu;
621     }
622
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 );
632     if( !p_region )
633     {
634         msg_Err( p_filter, "cannot allocate SPU region" );
635         p_filter->pf_sub_buffer_del( p_filter, p_spu );
636         return NULL;
637     }
638
639     vout_CopyPicture( p_filter, &p_region->picture, p_sys->p_pic );
640     p_region->i_x = 0;
641     p_region->i_y = 0;
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_spu->i_alpha = p_sys->i_trans;
647
648     return p_spu;
649 }
650
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 )
656 {
657     filter_sys_t *p_sys = (filter_sys_t *)p_data;
658
659     if( !strncmp( psz_var, "logo-file", 6 ) )
660     {
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;
664     }
665     else if ( !strncmp( psz_var, "logo-x", 6 ) )
666     {
667         p_sys->posx = newval.i_int;
668     }
669     else if ( !strncmp( psz_var, "logo-y", 6 ) )
670     {
671         p_sys->posy = newval.i_int;
672     }
673     else if ( !strncmp( psz_var, "logo-position", 12 ) )
674     {
675         p_sys->pos = newval.i_int;
676     }
677     else if ( !strncmp( psz_var, "logo-transparency", 9 ) )
678     {
679         p_sys->i_trans = __MAX( __MIN( newval.i_int, 255 ), 0 );
680     }
681     p_sys->b_need_update = VLC_TRUE;
682     return VLC_SUCCESS;
683 }