]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
Make logo-x, logo-y operate relative to upper left corner. logo-x/-y/-position/...
[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 #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
215     I_OUTPUTPICTURES = 0;
216
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;
222
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;
246
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 )
250     {
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 );
254         return VLC_EGENERIC;
255     }
256
257     if( p_sys->posx < 0 || p_sys->posy < 0 )
258     {
259         p_sys->posx = 0; p_sys->posy = 0;
260
261         if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
262         {
263             p_sys->posy = p_vout->render.i_height - p_sys->i_height;
264         }
265         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
266         {
267             p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
268         }
269
270         if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
271         {
272             p_sys->posx = p_vout->render.i_width - p_sys->i_width;
273         }
274         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
275         {
276             p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
277         }
278     }
279
280     /* Try to open the real video output */
281     msg_Dbg( p_vout, "spawning the real video output" );
282
283     p_sys->p_vout =
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 );
286
287     /* Everything failed */
288     if( p_sys->p_vout == NULL )
289     {
290         msg_Err( p_vout, "can't open vout, aborting" );
291         return VLC_EGENERIC;
292     }
293
294     var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
295     var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
296
297     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
298     ADD_CALLBACKS( p_sys->p_vout, SendEvents );
299     ADD_PARENT_CALLBACKS( SendEventsToChild );
300
301     return VLC_SUCCESS;
302 }
303
304 /*****************************************************************************
305  * End: terminate logo video thread output method
306  *****************************************************************************/
307 static void End( vout_thread_t *p_vout )
308 {
309     vout_sys_t *p_sys = p_vout->p_sys;
310     int i_index;
311
312     /* Free the fake output buffers we allocated */
313     for( i_index = I_OUTPUTPICTURES ; i_index ; )
314     {
315         i_index--;
316         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
317     }
318
319     var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
320     var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
321
322     if( p_sys->p_vout )
323     {
324         DEL_CALLBACKS( p_sys->p_vout, SendEvents );
325         vlc_object_detach( p_sys->p_vout );
326         vout_Destroy( p_sys->p_vout );
327     }
328
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 );
333 }
334
335 /*****************************************************************************
336  * Destroy: destroy logo video thread output method
337  *****************************************************************************/
338 static void Destroy( vlc_object_t *p_this )
339 {
340     vout_thread_t *p_vout = (vout_thread_t *)p_this;
341     vout_sys_t *p_sys = p_vout->p_sys;
342
343     DEL_PARENT_CALLBACKS( SendEventsToChild );
344
345     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
346     free( p_sys );
347 }
348
349 /*****************************************************************************
350  * Render: render the logo onto the video
351  *****************************************************************************/
352 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
353 {
354     vout_sys_t *p_sys = p_vout->p_sys;
355     picture_t *p_outpic;
356
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 )) )
359     {
360         if( p_vout->b_die || p_vout->b_error ) return;
361         msleep( VOUT_OUTMEM_SLEEP );
362     }
363
364     vout_CopyPicture( p_vout, p_outpic, p_pic );
365     vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
366
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,
369                                     p_sys->i_trans );
370
371     vout_DisplayPicture( p_sys->p_vout, p_outpic );
372 }
373
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 )
379 {
380     var_Set( (vlc_object_t *)p_data, psz_var, newval );
381     return VLC_SUCCESS;
382 }
383
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 )
389 {
390     vout_thread_t *p_vout = (vout_thread_t*)p_data;
391     vout_sys_t *p_sys = p_vout->p_sys;
392     vlc_value_t valb;
393     int i_delta;
394
395     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
396
397     i_delta = newval.i_int - oldval.i_int;
398
399     if( (valb.i_int & 0x1) == 0 )
400     {
401         return VLC_SUCCESS;
402     }
403
404     if( psz_var[6] == 'x' )
405     {
406         vlc_value_t valy;
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) )
412         {
413             p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
414                           p_vout->output.i_width - p_sys->i_width );
415         }
416     }
417     else if( psz_var[6] == 'y' )
418     {
419         vlc_value_t valx;
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) )
425         {
426             p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
427                           p_vout->output.i_height - p_sys->i_height );
428         }
429     }
430
431     return VLC_SUCCESS;
432 }
433
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 )
438 {
439     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
440 }
441
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 )
447 {
448     vout_thread_t *p_vout = (vout_thread_t *)p_this;
449     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
450     return VLC_SUCCESS;
451 }
452
453 /*****************************************************************************
454  * filter_sys_t: logo filter descriptor
455  *****************************************************************************/
456 struct filter_sys_t
457 {
458     picture_t *p_pic;
459
460     int i_width, i_height;
461     int pos, posx, posy;
462     char *psz_filename;
463     int i_trans;
464     
465     vlc_bool_t b_absolute;
466
467     mtime_t i_last_date;
468
469     /* On the fly control variable */
470     vlc_bool_t b_need_update;
471     vlc_bool_t b_new_image;
472 };
473
474 static subpicture_t *Filter( filter_t *, mtime_t );
475
476 /*****************************************************************************
477  * CreateFilter: allocates logo video filter
478  *****************************************************************************/
479 static int CreateFilter( vlc_object_t *p_this )
480 {
481     filter_t *p_filter = (filter_t *)p_this;
482     filter_sys_t *p_sys;
483     vlc_object_t *p_input;
484
485     /* Allocate structure */
486     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
487     if( p_sys == NULL )
488     {
489         msg_Err( p_filter, "out of memory" );
490         return VLC_ENOMEM;
491     }
492
493     /* Hook used for callback variables */
494     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
495     if( !p_input )
496     {
497         free( p_sys );
498         return VLC_ENOOBJ;
499     }
500
501     p_sys->psz_filename =
502         var_CreateGetString( p_input->p_libvlc , "logo-file" ); 
503     if( !p_sys->psz_filename || !*p_sys->psz_filename )
504     {
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 );
508         free( p_sys );
509         return VLC_EGENERIC;
510     }
511
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" );
515     p_sys->i_trans =
516         var_CreateGetInteger( p_input->p_libvlc, "logo-transparency");
517     p_sys->i_trans = __MAX( __MIN( p_sys->i_trans, 255 ), 0 );
518
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 );
525
526     p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
527     if( !p_sys->p_pic )
528     {
529         free( p_sys );
530         msg_Err( p_this, "couldn't load logo file" );
531         return VLC_EGENERIC;
532     }
533
534     /* Misc init */
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;
541
542     return VLC_SUCCESS;
543 }
544
545 /*****************************************************************************
546  * DestroyFilter: destroy logo video filter
547  *****************************************************************************/
548 static void DestroyFilter( vlc_object_t *p_this )
549 {
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;
553
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 );
556     free( p_sys );
557
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;
561
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 );
568 }
569
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 )
576 {
577     filter_sys_t *p_sys = p_filter->p_sys;
578     subpicture_t *p_spu;
579     subpicture_region_t *p_region;
580     video_format_t fmt;
581
582     if( !p_sys->b_need_update && p_sys->i_last_date +5000000 > date ) return 0;
583
584     if( p_sys->b_new_image )
585     {
586         if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
587
588         p_sys->p_pic = LoadImage( VLC_OBJECT(p_filter), p_sys->psz_filename );
589         if( p_sys->p_pic )
590         {
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;
593         }
594
595         p_sys->b_new_image = VLC_FALSE;
596     }
597
598     p_sys->b_need_update = VLC_FALSE;
599
600     /* Allocate the subpicture internal data. */
601     p_spu = p_filter->pf_sub_buffer_new( p_filter );
602     if( !p_spu ) return NULL;
603
604     p_spu->b_absolute = p_sys->b_absolute;
605     p_spu->i_start = p_sys->i_last_date = date;
606     p_spu->i_stop = 0;
607     p_spu->b_ephemer = VLC_TRUE;
608
609     p_sys->b_need_update = VLC_FALSE;
610
611     if( !p_sys->p_pic || !p_sys->i_trans )
612     {
613         /* Send an empty subpicture to clear the display */
614         return p_spu;
615     }
616
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 );
626     if( !p_region )
627     {
628         msg_Err( p_filter, "cannot allocate SPU region" );
629         p_filter->pf_sub_buffer_del( p_filter, p_spu );
630         return NULL;
631     }
632
633     vout_CopyPicture( p_filter, &p_region->picture, p_sys->p_pic );
634
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;
639         p_spu->i_x = 0;
640         p_spu->i_y = 0;
641         p_spu->b_absolute = VLC_FALSE;
642     }
643     else
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;
649     }
650
651     p_spu->p_region = p_region;
652     p_spu->i_alpha = p_sys->i_trans;
653
654     return p_spu;
655 }
656
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 )
662 {
663     filter_sys_t *p_sys = (filter_sys_t *)p_data;
664
665     if( !strncmp( psz_var, "logo-file", 6 ) )
666     {
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;
670     }
671     else if ( !strncmp( psz_var, "logo-x", 6 ) )
672     {
673         p_sys->posx = newval.i_int;
674     }
675     else if ( !strncmp( psz_var, "logo-y", 6 ) )
676     {
677         p_sys->posy = newval.i_int;
678     }
679     else if ( !strncmp( psz_var, "logo-position", 12 ) )
680     {
681         p_sys->pos = newval.i_int;
682     }
683     else if ( !strncmp( psz_var, "logo-transparency", 9 ) )
684     {
685         p_sys->i_trans = __MAX( __MIN( newval.i_int, 255 ), 0 );
686     }
687     p_sys->b_need_update = VLC_TRUE;
688     return VLC_SUCCESS;
689 }