]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
* modules/video_filter/*: use p_vout->fmt_in/out.
[vlc] / modules / video_filter / logo.c
1 /*****************************************************************************
2  * logo.c : logo video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
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 "vlc_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_TRUE );
99     add_integer( "logo-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_TRUE );
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_FALSE );
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     p_vout->fmt_out = p_vout->fmt_in;
224     fmt = p_vout->fmt_out;
225
226     /* Load the video blending filter */
227     p_sys->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
228     vlc_object_attach( p_sys->p_blend, p_vout );
229     p_sys->p_blend->fmt_out.video.i_x_offset =
230         p_sys->p_blend->fmt_out.video.i_y_offset = 0;
231     p_sys->p_blend->fmt_in.video.i_x_offset =
232         p_sys->p_blend->fmt_in.video.i_y_offset = 0;
233     p_sys->p_blend->fmt_out.video.i_aspect = p_vout->render.i_aspect;
234     p_sys->p_blend->fmt_out.video.i_chroma = p_vout->output.i_chroma;
235     p_sys->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','A');
236     p_sys->p_blend->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR;
237     p_sys->p_blend->fmt_in.video.i_width =
238         p_sys->p_blend->fmt_in.video.i_visible_width =
239             p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
240     p_sys->p_blend->fmt_in.video.i_height =
241         p_sys->p_blend->fmt_in.video.i_visible_height =
242             p_sys->p_pic->p[Y_PLANE].i_visible_lines;
243     p_sys->p_blend->fmt_out.video.i_width =
244         p_sys->p_blend->fmt_out.video.i_visible_width =
245            p_vout->output.i_width;
246     p_sys->p_blend->fmt_out.video.i_height =
247         p_sys->p_blend->fmt_out.video.i_visible_height =
248             p_vout->output.i_height;
249
250     p_sys->p_blend->p_module =
251         module_Need( p_sys->p_blend, "video blending", 0, 0 );
252     if( !p_sys->p_blend->p_module )
253     {
254         msg_Err( p_vout, "can't open blending filter, aborting" );
255         vlc_object_detach( p_sys->p_blend );
256         vlc_object_destroy( p_sys->p_blend );
257         return VLC_EGENERIC;
258     }
259
260     if( p_sys->posx < 0 || p_sys->posy < 0 )
261     {
262         p_sys->posx = 0; p_sys->posy = 0;
263
264         if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
265         {
266             p_sys->posy = p_vout->render.i_height - p_sys->i_height;
267         }
268         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
269         {
270             p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
271         }
272
273         if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
274         {
275             p_sys->posx = p_vout->render.i_width - p_sys->i_width;
276         }
277         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
278         {
279             p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
280         }
281     }
282
283     /* Try to open the real video output */
284     msg_Dbg( p_vout, "spawning the real video output" );
285
286     p_sys->p_vout = vout_Create( p_vout, &fmt );
287
288     /* Everything failed */
289     if( p_sys->p_vout == NULL )
290     {
291         msg_Err( p_vout, "can't open vout, aborting" );
292         return VLC_EGENERIC;
293     }
294
295     var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
296     var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
297
298     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
299     ADD_CALLBACKS( p_sys->p_vout, SendEvents );
300     ADD_PARENT_CALLBACKS( SendEventsToChild );
301
302     return VLC_SUCCESS;
303 }
304
305 /*****************************************************************************
306  * End: terminate logo video thread output method
307  *****************************************************************************/
308 static void End( vout_thread_t *p_vout )
309 {
310     vout_sys_t *p_sys = p_vout->p_sys;
311     int i_index;
312
313     /* Free the fake output buffers we allocated */
314     for( i_index = I_OUTPUTPICTURES ; i_index ; )
315     {
316         i_index--;
317         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
318     }
319
320     var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
321     var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
322
323     if( p_sys->p_vout )
324     {
325         DEL_CALLBACKS( p_sys->p_vout, SendEvents );
326         vlc_object_detach( p_sys->p_vout );
327         vout_Destroy( p_sys->p_vout );
328     }
329
330     if( p_sys->p_blend->p_module )
331         module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
332     vlc_object_detach( p_sys->p_blend );
333     vlc_object_destroy( p_sys->p_blend );
334 }
335
336 /*****************************************************************************
337  * Destroy: destroy logo video thread output method
338  *****************************************************************************/
339 static void Destroy( vlc_object_t *p_this )
340 {
341     vout_thread_t *p_vout = (vout_thread_t *)p_this;
342     vout_sys_t *p_sys = p_vout->p_sys;
343
344     DEL_PARENT_CALLBACKS( SendEventsToChild );
345
346     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
347     free( p_sys );
348 }
349
350 /*****************************************************************************
351  * Render: render the logo onto the video
352  *****************************************************************************/
353 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
354 {
355     vout_sys_t *p_sys = p_vout->p_sys;
356     picture_t *p_outpic;
357
358     /* This is a new frame. Get a structure from the video_output. */
359     while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
360     {
361         if( p_vout->b_die || p_vout->b_error ) return;
362         msleep( VOUT_OUTMEM_SLEEP );
363     }
364
365     vout_CopyPicture( p_vout, p_outpic, p_pic );
366     vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
367
368     p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic, p_outpic,
369                                     p_sys->p_pic, p_sys->posx, p_sys->posy,
370                                     p_sys->i_trans );
371
372     vout_DisplayPicture( p_sys->p_vout, p_outpic );
373 }
374
375 /*****************************************************************************
376  * SendEvents: forward mouse and keyboard events to the parent p_vout
377  *****************************************************************************/
378 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
379                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
380 {
381     var_Set( (vlc_object_t *)p_data, psz_var, newval );
382     return VLC_SUCCESS;
383 }
384
385 /*****************************************************************************
386  * MouseEvent: callback for mouse events
387  *****************************************************************************/
388 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
389                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
390 {
391     vout_thread_t *p_vout = (vout_thread_t*)p_data;
392     vout_sys_t *p_sys = p_vout->p_sys;
393     vlc_value_t valb;
394     int i_delta;
395
396     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
397
398     i_delta = newval.i_int - oldval.i_int;
399
400     if( (valb.i_int & 0x1) == 0 )
401     {
402         return VLC_SUCCESS;
403     }
404
405     if( psz_var[6] == 'x' )
406     {
407         vlc_value_t valy;
408         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
409         if( newval.i_int >= (int)p_sys->posx &&
410             valy.i_int >= (int)p_sys->posy &&
411             newval.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
412             valy.i_int <= (int)(p_sys->posy + p_sys->i_height) )
413         {
414             p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
415                           p_vout->output.i_width - p_sys->i_width );
416         }
417     }
418     else if( psz_var[6] == 'y' )
419     {
420         vlc_value_t valx;
421         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
422         if( valx.i_int >= (int)p_sys->posx &&
423             newval.i_int >= (int)p_sys->posy &&
424             valx.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
425             newval.i_int <= (int)(p_sys->posy + p_sys->i_height) )
426         {
427             p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
428                           p_vout->output.i_height - p_sys->i_height );
429         }
430     }
431
432     return VLC_SUCCESS;
433 }
434
435 /*****************************************************************************
436  * Control: control facility for the vout (forwards to child vout)
437  *****************************************************************************/
438 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
439 {
440     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
441 }
442
443 /*****************************************************************************
444  * SendEventsToChild: forward events to the child/children vout
445  *****************************************************************************/
446 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
447                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
448 {
449     vout_thread_t *p_vout = (vout_thread_t *)p_this;
450     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
451     return VLC_SUCCESS;
452 }
453
454 /*****************************************************************************
455  * filter_sys_t: logo filter descriptor
456  *****************************************************************************/
457 struct filter_sys_t
458 {
459     picture_t *p_pic;
460
461     int i_width, i_height;
462     int pos, posx, posy;
463     char *psz_filename;
464     int i_trans;
465
466     vlc_bool_t b_absolute;
467
468     mtime_t i_last_date;
469
470     /* On the fly control variable */
471     vlc_bool_t b_need_update;
472     vlc_bool_t b_new_image;
473 };
474
475 static subpicture_t *Filter( filter_t *, mtime_t );
476
477 /*****************************************************************************
478  * CreateFilter: allocates logo video filter
479  *****************************************************************************/
480 static int CreateFilter( vlc_object_t *p_this )
481 {
482     filter_t *p_filter = (filter_t *)p_this;
483     filter_sys_t *p_sys;
484     vlc_object_t *p_input;
485
486     /* Allocate structure */
487     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
488     if( p_sys == NULL )
489     {
490         msg_Err( p_filter, "out of memory" );
491         return VLC_ENOMEM;
492     }
493
494     /* Hook used for callback variables */
495     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
496     if( !p_input )
497     {
498         free( p_sys );
499         return VLC_ENOOBJ;
500     }
501
502     p_sys->psz_filename =
503         var_CreateGetString( p_input->p_libvlc , "logo-file" );
504     if( !p_sys->psz_filename || !*p_sys->psz_filename )
505     {
506         msg_Err( p_this, "logo file not specified" );
507         vlc_object_release( p_input );
508         if( p_sys->psz_filename ) free( p_sys->psz_filename );
509         free( p_sys );
510         return VLC_EGENERIC;
511     }
512
513     p_sys->posx = var_CreateGetInteger( p_input->p_libvlc , "logo-x" );
514     p_sys->posy = var_CreateGetInteger( p_input->p_libvlc , "logo-y" );
515     p_sys->pos = var_CreateGetInteger( p_input->p_libvlc , "logo-position" );
516     p_sys->i_trans =
517         var_CreateGetInteger( p_input->p_libvlc, "logo-transparency");
518     p_sys->i_trans = __MAX( __MIN( p_sys->i_trans, 255 ), 0 );
519
520     var_AddCallback( p_input->p_libvlc, "logo-file", LogoCallback, p_sys );
521     var_AddCallback( p_input->p_libvlc, "logo-x", LogoCallback, p_sys );
522     var_AddCallback( p_input->p_libvlc, "logo-y", LogoCallback, p_sys );
523     var_AddCallback( p_input->p_libvlc, "logo-position", LogoCallback, p_sys );
524     var_AddCallback( p_input->p_libvlc, "logo-transparency", LogoCallback, p_sys );
525     vlc_object_release( p_input );
526
527     p_sys->p_pic = LoadImage( p_this, p_sys->psz_filename );
528     if( !p_sys->p_pic )
529     {
530         free( p_sys );
531         msg_Err( p_this, "couldn't load logo file" );
532         return VLC_EGENERIC;
533     }
534
535     /* Misc init */
536     p_filter->pf_sub_filter = Filter;
537     p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
538     p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
539     p_sys->b_need_update = VLC_TRUE;
540     p_sys->b_new_image = VLC_FALSE;
541     p_sys->i_last_date = 0;
542
543     return VLC_SUCCESS;
544 }
545
546 /*****************************************************************************
547  * DestroyFilter: destroy logo video filter
548  *****************************************************************************/
549 static void DestroyFilter( vlc_object_t *p_this )
550 {
551     filter_t *p_filter = (filter_t *)p_this;
552     filter_sys_t *p_sys = p_filter->p_sys;
553     vlc_object_t *p_input;
554
555     if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
556     if( p_sys->psz_filename ) free( p_sys->psz_filename );
557     free( p_sys );
558
559     /* Delete the logo variables from INPUT */
560     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
561     if( !p_input ) return;
562
563     var_Destroy( p_input->p_libvlc , "logo-file" );
564     var_Destroy( p_input->p_libvlc , "logo-x" );
565     var_Destroy( p_input->p_libvlc , "logo-y" );
566     var_Destroy( p_input->p_libvlc , "logo-position" );
567     var_Destroy( p_input->p_libvlc , "logo-transparency" );
568     vlc_object_release( p_input );
569 }
570
571 /*****************************************************************************
572  * Filter: the whole thing
573  *****************************************************************************
574  * This function outputs subpictures at regular time intervals.
575  *****************************************************************************/
576 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
577 {
578     filter_sys_t *p_sys = p_filter->p_sys;
579     subpicture_t *p_spu;
580     subpicture_region_t *p_region;
581     video_format_t fmt;
582
583     if( !p_sys->b_need_update && p_sys->i_last_date +5000000 > date ) return 0;
584
585     if( p_sys->b_new_image )
586     {
587         if( p_sys->p_pic ) p_sys->p_pic->pf_release( p_sys->p_pic );
588
589         p_sys->p_pic = LoadImage( VLC_OBJECT(p_filter), p_sys->psz_filename );
590         if( p_sys->p_pic )
591         {
592             p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
593             p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
594         }
595
596         p_sys->b_new_image = VLC_FALSE;
597     }
598
599     p_sys->b_need_update = VLC_FALSE;
600
601     /* Allocate the subpicture internal data. */
602     p_spu = p_filter->pf_sub_buffer_new( p_filter );
603     if( !p_spu ) return NULL;
604
605     p_spu->b_absolute = p_sys->b_absolute;
606     p_spu->i_start = p_sys->i_last_date = date;
607     p_spu->i_stop = 0;
608     p_spu->b_ephemer = VLC_TRUE;
609
610     p_sys->b_need_update = VLC_FALSE;
611
612     if( !p_sys->p_pic || !p_sys->i_trans )
613     {
614         /* Send an empty subpicture to clear the display */
615         return p_spu;
616     }
617
618     /* Create new SPU region */
619     memset( &fmt, 0, sizeof(video_format_t) );
620     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
621     fmt.i_aspect = VOUT_ASPECT_FACTOR;
622     fmt.i_sar_num = fmt.i_sar_den = 1;
623     fmt.i_width = fmt.i_visible_width = p_sys->i_width;
624     fmt.i_height = fmt.i_visible_height = p_sys->i_height;
625     fmt.i_x_offset = fmt.i_y_offset = 0;
626     p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
627     if( !p_region )
628     {
629         msg_Err( p_filter, "cannot allocate SPU region" );
630         p_filter->pf_sub_buffer_del( p_filter, p_spu );
631         return NULL;
632     }
633
634     vout_CopyPicture( p_filter, &p_region->picture, p_sys->p_pic );
635
636     /*  where to locate the logo: */
637     if( p_sys->posx < 0 || p_sys->posy < 0 )
638     {   /* set to one of the 9 relative locations */
639         p_spu->i_flags = p_sys->pos;
640         p_spu->i_x = 0;
641         p_spu->i_y = 0;
642         p_spu->b_absolute = VLC_FALSE;
643     }
644     else
645     {   /*  set to an absolute xy, referenced to upper left corner */
646         p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
647         p_spu->i_x = p_sys->posx;
648         p_spu->i_y = p_sys->posy;
649         p_spu->b_absolute = VLC_TRUE;
650     }
651
652     p_spu->p_region = p_region;
653     p_spu->i_alpha = p_sys->i_trans;
654
655     return p_spu;
656 }
657
658 /*****************************************************************************
659  * Callback to update params on the fly
660  *****************************************************************************/
661 static int LogoCallback( vlc_object_t *p_this, char const *psz_var,
662                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
663 {
664     filter_sys_t *p_sys = (filter_sys_t *)p_data;
665
666     if( !strncmp( psz_var, "logo-file", 6 ) )
667     {
668         if( p_sys->psz_filename ) free( p_sys->psz_filename );
669         p_sys->psz_filename = strdup( newval.psz_string );
670         p_sys->b_new_image = VLC_TRUE;
671     }
672     else if ( !strncmp( psz_var, "logo-x", 6 ) )
673     {
674         p_sys->posx = newval.i_int;
675     }
676     else if ( !strncmp( psz_var, "logo-y", 6 ) )
677     {
678         p_sys->posy = newval.i_int;
679     }
680     else if ( !strncmp( psz_var, "logo-position", 12 ) )
681     {
682         p_sys->pos = newval.i_int;
683     }
684     else if ( !strncmp( psz_var, "logo-transparency", 9 ) )
685     {
686         p_sys->i_trans = __MAX( __MIN( newval.i_int, 255 ), 0 );
687     }
688     p_sys->b_need_update = VLC_TRUE;
689     return VLC_SUCCESS;
690 }