]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
* modules/video_filter/logo.c: set sample aspect ratio.
[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 <png.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/vout.h>
35
36 #include "vlc_filter.h"
37 #include "filter_common.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44
45 static int  Init      ( vout_thread_t * );
46 static void End       ( vout_thread_t * );
47 static void Render    ( vout_thread_t *, picture_t * );
48
49 static int  SendEvents( vlc_object_t *, char const *,
50                         vlc_value_t, vlc_value_t, void * );
51 static int MouseEvent ( vlc_object_t *, char const *,
52                         vlc_value_t , vlc_value_t , void * );
53 static int Control    ( vout_thread_t *, int, va_list );
54
55 static int  CreateFilter ( vlc_object_t * );
56 static void DestroyFilter( vlc_object_t * );
57
58 static int LogoCallback( vlc_object_t *p_this, char const *psz_var,
59                             vlc_value_t oldval, vlc_value_t newval,
60                             void *p_data );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 #define FILE_TEXT N_("Logo filename")
66 #define FILE_LONGTEXT N_("Full path of the PNG file to use.")
67 #define POSX_TEXT N_("X coordinate of the logo")
68 #define POSX_LONGTEXT N_("You can move the logo by left-clicking on it." )
69 #define POSY_TEXT N_("Y coordinate of the logo")
70 #define POSY_LONGTEXT N_("You can move the logo by left-clicking on it." )
71 #define TRANS_TEXT N_("Transparency of the logo")
72 #define TRANS_LONGTEXT N_("You can set the logo transparency value here " \
73   "(from 0 for full transparency to 255 for full opacity)." )
74 #define POS_TEXT N_("Logo position")
75 #define POS_LONGTEXT N_( \
76   "You can enforce the logo position on the video " \
77   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
78   "also use combinations of these values).")
79
80 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
81 static char *ppsz_pos_descriptions[] =
82 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
83   N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
84
85 vlc_module_begin();
86     set_description( _("Logo video filter") );
87     set_capability( "video filter", 0 );
88     add_shortcut( "logo" );
89     set_callbacks( Create, Destroy );
90
91     add_file( "logo-file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, VLC_FALSE );
92     add_integer( "logo-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
93     add_integer( "logo-y", -1, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
94     add_integer_with_range( "logo-transparency", 255, 0, 255, NULL,
95         TRANS_TEXT, TRANS_LONGTEXT, VLC_FALSE );
96     add_integer( "logo-position", 6, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
97         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
98
99     /* subpicture filter submodule */
100     add_submodule();
101     set_capability( "sub filter", 0 );
102     set_callbacks( CreateFilter, DestroyFilter );
103     set_description( _("Logo sub filter") );
104     add_shortcut( "logo" );
105 vlc_module_end();
106
107 /*****************************************************************************
108  * LoadPNG: loads the PNG logo into memory
109  *****************************************************************************/
110 static picture_t *LoadPNG( vlc_object_t *p_this )
111 {
112     picture_t *p_pic;
113     char *psz_filename;
114     vlc_value_t val;
115     FILE *file;
116     int i, j, i_trans;
117     vlc_bool_t b_alpha = VLC_TRUE;
118
119     png_uint_32 i_width, i_height;
120     int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
121     int i_bit_depth;
122     png_bytep *p_row_pointers;
123     png_structp p_png;
124     png_infop p_info, p_end_info;
125
126     var_Create( p_this, "logo-file", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
127     var_Get( p_this, "logo-file", &val );
128     psz_filename = val.psz_string;
129     if( !psz_filename || !*psz_filename )
130     {
131         msg_Err( p_this, "logo file not specified" );
132         return 0;
133     }
134
135     if( !(file = fopen( psz_filename , "rb" )) )
136     {
137         msg_Err( p_this, "logo file (%s) not found", psz_filename );
138         free( psz_filename );
139         return 0;
140     }
141     free( psz_filename );
142
143     p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
144     p_info = png_create_info_struct( p_png );
145     p_end_info = png_create_info_struct( p_png );
146     png_init_io( p_png, file );
147     png_read_info( p_png, p_info );
148     png_get_IHDR( p_png, p_info, &i_width, &i_height,
149                   &i_bit_depth, &i_color_type, &i_interlace_type,
150                   &i_compression_type, &i_filter_type);
151
152     if( i_color_type == PNG_COLOR_TYPE_PALETTE )
153         png_set_palette_to_rgb( p_png );
154
155     if( i_color_type == PNG_COLOR_TYPE_GRAY ||
156         i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
157           png_set_gray_to_rgb( p_png );
158
159     if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
160     {
161         png_set_tRNS_to_alpha( p_png );
162     }
163     else if( !(i_color_type & PNG_COLOR_MASK_ALPHA) )
164     {
165         b_alpha = VLC_FALSE;
166     }
167
168     p_row_pointers = malloc( sizeof(png_bytep) * i_height );
169     for( i = 0; i < (int)i_height; i++ )
170         p_row_pointers[i] = malloc( 4 * ( i_bit_depth + 7 ) / 8 * i_width );
171
172     png_read_image( p_png, p_row_pointers );
173     png_read_end( p_png, p_end_info );
174
175     fclose( file );
176     png_destroy_read_struct( &p_png, &p_info, &p_end_info );
177
178     /* Convert to YUVA */
179     p_pic = malloc( sizeof(picture_t) );
180     if( vout_AllocatePicture( p_this, p_pic, VLC_FOURCC('Y','U','V','A'),
181                               i_width, i_height, VOUT_ASPECT_FACTOR ) !=
182         VLC_SUCCESS )
183     {
184         for( i = 0; i < (int)i_height; i++ ) free( p_row_pointers[i] );
185         free( p_row_pointers );
186         return 0;
187     }
188
189     var_Create(p_this, "logo-transparency", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
190     var_Get( p_this, "logo-transparency", &val );
191     i_trans = __MAX( __MIN( val.i_int, 255 ), 0 );
192
193     for( j = 0; j < (int)i_height ; j++ )
194     {
195         uint8_t *p = (uint8_t *)p_row_pointers[j];
196
197         for( i = 0; i < (int)i_width ; i++ )
198         {
199             int i_offset = i + j * p_pic->p[Y_PLANE].i_pitch;
200
201             p_pic->p[Y_PLANE].p_pixels[i_offset] =
202                 (p[0] * 257L + p[1] * 504 + p[2] * 98)/1000 + 16;
203             p_pic->p[U_PLANE].p_pixels[i_offset] =
204                 (p[2] * 439L - p[0] * 148 - p[1] * 291)/1000 + 128;
205             p_pic->p[V_PLANE].p_pixels[i_offset] =
206                 (p[0] * 439L - p[1] * 368 - p[2] * 71)/1000 + 128;
207             p_pic->p[A_PLANE].p_pixels[i_offset] =
208                 b_alpha ? (p[3] * i_trans) / 255 : i_trans;
209
210             p += (b_alpha ? 4 : 3);
211         }
212     }
213
214     for( i = 0; i < (int)i_height; i++ ) free( p_row_pointers[i] );
215     free( p_row_pointers );
216     return p_pic;
217 }
218
219 /*****************************************************************************
220  * vout_sys_t: logo video output method descriptor
221  *****************************************************************************
222  * This structure is part of the video output thread descriptor.
223  * It describes the Invert specific properties of an output thread.
224  *****************************************************************************/
225 struct vout_sys_t
226 {
227     vout_thread_t *p_vout;
228
229     filter_t *p_blend;
230     picture_t *p_pic;
231
232     int i_width, i_height;
233     int pos, posx, posy;
234 };
235
236 /*****************************************************************************
237  * Create: allocates logo video thread output method
238  *****************************************************************************/
239 static int Create( vlc_object_t *p_this )
240 {
241     vout_thread_t *p_vout = (vout_thread_t *)p_this;
242     vout_sys_t *p_sys;
243     vlc_value_t val;
244
245     /* Allocate structure */
246     p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
247     if( p_sys == NULL )
248     {
249         msg_Err( p_vout, "out of memory" );
250         return VLC_ENOMEM;
251     }
252
253     p_vout->pf_init = Init;
254     p_vout->pf_end = End;
255     p_vout->pf_manage = NULL;
256     p_vout->pf_render = Render;
257     p_vout->pf_display = NULL;
258     p_vout->pf_control = Control;
259
260     var_Create( p_this, "logo-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
261     var_Get( p_this, "logo-position", &val );
262     p_sys->pos = val.i_int;
263     var_Create( p_this, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
264     var_Get( p_this, "logo-x", &val );
265     p_sys->posx = val.i_int;
266     var_Create( p_this, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
267     var_Get( p_this, "logo-y", &val );
268     p_sys->posy = val.i_int;
269
270     p_sys->p_pic = LoadPNG( p_this );
271     if( !p_sys->p_pic )
272     {
273         free( p_sys );
274         return VLC_EGENERIC;
275     }
276
277     p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
278     p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
279
280     return VLC_SUCCESS;
281 }
282
283 /*****************************************************************************
284  * Init: initialize logo video thread output method
285  *****************************************************************************/
286 static int Init( vout_thread_t *p_vout )
287 {
288     vout_sys_t *p_sys = p_vout->p_sys;
289     picture_t *p_pic;
290     int i_index;
291
292     I_OUTPUTPICTURES = 0;
293
294     /* Initialize the output structure */
295     p_vout->output.i_chroma = p_vout->render.i_chroma;
296     p_vout->output.i_width  = p_vout->render.i_width;
297     p_vout->output.i_height = p_vout->render.i_height;
298     p_vout->output.i_aspect = p_vout->render.i_aspect;
299
300     /* Load the video blending filter */
301     p_sys->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
302     vlc_object_attach( p_sys->p_blend, p_vout );
303     p_sys->p_blend->fmt_out.video.i_x_offset =
304         p_sys->p_blend->fmt_out.video.i_y_offset = 0;
305     p_sys->p_blend->fmt_in.video.i_x_offset =
306         p_sys->p_blend->fmt_in.video.i_y_offset = 0;
307     p_sys->p_blend->fmt_out.video.i_aspect = p_vout->render.i_aspect;
308     p_sys->p_blend->fmt_out.video.i_chroma = p_vout->output.i_chroma;
309     p_sys->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','A');
310     p_sys->p_blend->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR;
311     p_sys->p_blend->fmt_in.video.i_width =
312         p_sys->p_blend->fmt_in.video.i_visible_width =
313             p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
314     p_sys->p_blend->fmt_in.video.i_height =
315         p_sys->p_blend->fmt_in.video.i_visible_height =
316             p_sys->p_pic->p[Y_PLANE].i_visible_lines;
317     p_sys->p_blend->fmt_out.video.i_width =
318         p_sys->p_blend->fmt_out.video.i_visible_width =
319            p_vout->output.i_width;
320     p_sys->p_blend->fmt_out.video.i_height =
321         p_sys->p_blend->fmt_out.video.i_visible_height =
322             p_vout->output.i_height;
323
324     p_sys->p_blend->p_module =
325         module_Need( p_sys->p_blend, "video blending", 0, 0 );
326     if( !p_sys->p_blend->p_module )
327     {
328         msg_Err( p_vout, "can't open blending filter, aborting" );
329         vlc_object_detach( p_sys->p_blend );
330         vlc_object_destroy( p_sys->p_blend );
331         return VLC_EGENERIC;
332     }
333
334     if( p_sys->posx < 0 || p_sys->posy < 0 )
335     {
336         p_sys->posx = 0; p_sys->posy = 0;
337
338         if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
339         {
340             p_sys->posy = p_vout->render.i_height - p_sys->i_height;
341         }
342         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
343         {
344             p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
345         }
346
347         if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
348         {
349             p_sys->posx = p_vout->render.i_width - p_sys->i_width;
350         }
351         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
352         {
353             p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
354         }
355     }
356
357     /* Try to open the real video output */
358     msg_Dbg( p_vout, "spawning the real video output" );
359
360     p_sys->p_vout =
361         vout_Create( p_vout, p_vout->render.i_width, p_vout->render.i_height,
362                      p_vout->render.i_chroma, p_vout->render.i_aspect );
363
364     /* Everything failed */
365     if( p_sys->p_vout == NULL )
366     {
367         msg_Err( p_vout, "can't open vout, aborting" );
368         return VLC_EGENERIC;
369     }
370
371     var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
372     var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
373
374     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
375     ADD_CALLBACKS( p_sys->p_vout, SendEvents );
376     ADD_PARENT_CALLBACKS( SendEventsToChild );
377
378     return VLC_SUCCESS;
379 }
380
381 /*****************************************************************************
382  * End: terminate logo video thread output method
383  *****************************************************************************/
384 static void End( vout_thread_t *p_vout )
385 {
386     vout_sys_t *p_sys = p_vout->p_sys;
387     int i_index;
388
389     /* Free the fake output buffers we allocated */
390     for( i_index = I_OUTPUTPICTURES ; i_index ; )
391     {
392         i_index--;
393         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
394     }
395
396     var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
397     var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
398
399     if( p_sys->p_vout )
400     {
401         DEL_CALLBACKS( p_sys->p_vout, SendEvents );
402         vlc_object_detach( p_sys->p_vout );
403         vout_Destroy( p_sys->p_vout );
404     }
405
406     if( p_sys->p_blend->p_module )
407         module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
408     vlc_object_detach( p_sys->p_blend );
409     vlc_object_destroy( p_sys->p_blend );
410 }
411
412 /*****************************************************************************
413  * Destroy: destroy logo video thread output method
414  *****************************************************************************/
415 static void Destroy( vlc_object_t *p_this )
416 {
417     vout_thread_t *p_vout = (vout_thread_t *)p_this;
418     vout_sys_t *p_sys = p_vout->p_sys;
419
420     DEL_PARENT_CALLBACKS( SendEventsToChild );
421
422     if( p_sys->p_pic && p_sys->p_pic->p_data_orig )
423         free( p_sys->p_pic->p_data_orig );
424     if( p_sys->p_pic ) free( p_sys->p_pic );
425
426     free( p_sys );
427 }
428
429 /*****************************************************************************
430  * Render: render the logo onto the video
431  *****************************************************************************/
432 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
433 {
434     vout_sys_t *p_sys = p_vout->p_sys;
435     picture_t *p_outpic;
436
437     /* This is a new frame. Get a structure from the video_output. */
438     while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
439     {
440         if( p_vout->b_die || p_vout->b_error ) return;
441         msleep( VOUT_OUTMEM_SLEEP );
442     }
443
444     vout_CopyPicture( p_vout, p_outpic, p_pic );
445     vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
446
447     p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic, p_outpic,
448                                     p_sys->p_pic, p_sys->posx, p_sys->posy,
449                                     255 );
450
451     vout_DisplayPicture( p_sys->p_vout, p_outpic );
452 }
453
454 /*****************************************************************************
455  * SendEvents: forward mouse and keyboard events to the parent p_vout
456  *****************************************************************************/
457 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
458                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
459 {
460     var_Set( (vlc_object_t *)p_data, psz_var, newval );
461     return VLC_SUCCESS;
462 }
463
464 /*****************************************************************************
465  * MouseEvent: callback for mouse events
466  *****************************************************************************/
467 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
468                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
469 {
470     vout_thread_t *p_vout = (vout_thread_t*)p_data;
471     vout_sys_t *p_sys = p_vout->p_sys;
472     vlc_value_t valb;
473     int i_delta;
474
475     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
476
477     i_delta = newval.i_int - oldval.i_int;
478
479     if( (valb.i_int & 0x1) == 0 )
480     {
481         return VLC_SUCCESS;
482     }
483
484     if( psz_var[6] == 'x' )
485     {
486         vlc_value_t valy;
487         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
488         if( newval.i_int >= (int)p_sys->posx &&
489             valy.i_int >= (int)p_sys->posy &&
490             newval.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
491             valy.i_int <= (int)(p_sys->posy + p_sys->i_height) )
492         {
493             p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
494                           p_vout->output.i_width - p_sys->i_width );
495         }
496     }
497     else if( psz_var[6] == 'y' )
498     {
499         vlc_value_t valx;
500         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
501         if( valx.i_int >= (int)p_sys->posx &&
502             newval.i_int >= (int)p_sys->posy &&
503             valx.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
504             newval.i_int <= (int)(p_sys->posy + p_sys->i_height) )
505         {
506             p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
507                           p_vout->output.i_height - p_sys->i_height );
508         }
509     }
510
511     return VLC_SUCCESS;
512 }
513
514 /*****************************************************************************
515  * Control: control facility for the vout (forwards to child vout)
516  *****************************************************************************/
517 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
518 {
519     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
520 }
521
522 /*****************************************************************************
523  * SendEventsToChild: forward events to the child/children vout
524  *****************************************************************************/
525 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
526                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
527 {
528     vout_thread_t *p_vout = (vout_thread_t *)p_this;
529     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
530     return VLC_SUCCESS;
531 }
532
533 /*****************************************************************************
534  * filter_sys_t: logo filter descriptor
535  *****************************************************************************/
536 struct filter_sys_t
537 {
538     picture_t *p_pic;
539
540     int i_width, i_height;
541     int pos, posx, posy;
542
543     vlc_bool_t b_absolute;
544
545     mtime_t i_last_date;
546     /*  On the fly control variable: */
547     vlc_bool_t b_need_update;
548
549 };
550
551 static subpicture_t *Filter( filter_t *, mtime_t );
552
553 /*****************************************************************************
554  * CreateFilter: allocates logo video filter
555  *****************************************************************************/
556 static int CreateFilter( vlc_object_t *p_this )
557 {
558     filter_t *p_filter = (filter_t *)p_this;
559     filter_sys_t *p_sys;
560     vlc_value_t val;
561     vlc_object_t *p_input;
562     
563     /* Allocate structure */
564     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
565     if( p_sys == NULL )
566     {
567         msg_Err( p_filter, "out of memory" );
568         return VLC_ENOMEM;
569     }
570     /* Hook used for callback variables */
571     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
572     if( !p_input )
573     {
574         return VLC_ENOOBJ;
575     }
576     
577     p_sys->posx = var_CreateGetInteger( p_input , "logo-x" );
578     p_sys->posy = var_CreateGetInteger( p_input , "logo-y" );
579     p_sys->pos = var_CreateGetInteger( p_input , "logo-position" );
580     var_AddCallback( p_input, "logo-x", LogoCallback, p_sys );
581     var_AddCallback( p_input, "logo-y", LogoCallback, p_sys );
582     var_AddCallback( p_input, "logo-position", LogoCallback, p_sys );
583     vlc_object_release( p_input );
584
585     p_sys->b_absolute = VLC_TRUE;
586     if( p_sys->posx < 0 || p_sys->posy < 0 )
587     {
588         p_sys->b_absolute = VLC_FALSE;
589         p_sys->posx = 0; p_sys->posy = 0;
590     }
591
592     p_sys->p_pic = LoadPNG( p_this );
593     if( !p_sys->p_pic )
594     {
595         free( p_sys );
596         return VLC_EGENERIC;
597     }
598
599     p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
600     p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
601     p_sys->i_last_date = 0;
602
603     /* Misc init */
604     p_filter->pf_sub_filter = Filter;
605     p_sys->b_need_update = VLC_TRUE;
606
607     return VLC_SUCCESS;
608 }
609
610 /*****************************************************************************
611  * DestroyFilter: destroy logo video filter
612  *****************************************************************************/
613 static void DestroyFilter( vlc_object_t *p_this )
614 {
615     filter_t *p_filter = (filter_t *)p_this;
616     filter_sys_t *p_sys = p_filter->p_sys;
617     vlc_object_t *p_input;
618
619     if( p_sys->p_pic && p_sys->p_pic->p_data_orig )
620         free( p_sys->p_pic->p_data_orig );
621     if( p_sys->p_pic ) free( p_sys->p_pic );
622
623     free( p_sys );
624     
625     /* Delete the marquee variables from playlist */
626     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
627     if( !p_input )
628     {
629         return;
630     }
631     var_Destroy( p_input , "logo-x" );
632     var_Destroy( p_input , "logo-y" );
633     var_Destroy( p_input , "logo-position" );
634     vlc_object_release( p_input );
635
636 }
637
638 /****************************************************************************
639  * Filter: the whole thing
640  ****************************************************************************
641  * This function outputs subpictures at regular time intervals.
642  ****************************************************************************/
643 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
644 {
645     filter_sys_t *p_sys = p_filter->p_sys;
646     subpicture_t *p_spu;
647     subpicture_region_t *p_region;
648     video_format_t fmt;
649
650     if( p_sys->i_last_date && p_sys->i_last_date + 5000000 > date ) return 0;
651     if( p_sys->b_need_update == VLC_FALSE )
652     {
653         return NULL;
654     }
655
656     /* Allocate the subpicture internal data. */
657     p_spu = p_filter->pf_sub_buffer_new( p_filter );
658     if( !p_spu ) return NULL;
659
660     /* Create new SPU region */
661     memset( &fmt, 0, sizeof(video_format_t) );
662     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
663     fmt.i_aspect = VOUT_ASPECT_FACTOR;
664     fmt.i_sar_num = fmt.i_sar_den = 1;
665     fmt.i_width = fmt.i_visible_width = p_sys->i_width;
666     fmt.i_height = fmt.i_visible_height = p_sys->i_height;
667     fmt.i_x_offset = fmt.i_y_offset = 0;
668     p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
669     if( !p_region )
670     {
671         msg_Err( p_filter, "cannot allocate SPU region" );
672         p_filter->pf_sub_buffer_del( p_filter, p_spu );
673         return NULL;
674     }
675
676     vout_CopyPicture( p_filter, &p_region->picture, p_sys->p_pic );
677     p_region->i_x = 0;
678     p_region->i_y = 0;
679     p_spu->i_x = p_sys->posx;
680     p_spu->i_y = p_sys->posy;
681     p_spu->i_flags = p_sys->pos;
682     p_spu->b_absolute = p_sys->b_absolute;
683
684     p_spu->p_region = p_region;
685
686     p_spu->i_start = p_sys->i_last_date = date;
687     p_spu->i_stop = 0;
688     p_spu->b_ephemer = VLC_TRUE;
689     p_sys->b_need_update = VLC_FALSE;
690
691     return p_spu;
692 }
693 /**********************************************************************
694  * Callback to update params on the fly
695  **********************************************************************/
696 static int LogoCallback( vlc_object_t *p_this, char const *psz_var,
697                             vlc_value_t oldval, vlc_value_t newval,
698                             void *p_data )
699 {
700     filter_sys_t *p_sys = (filter_sys_t *) p_data;
701
702  /*   if( !strncmp( psz_var, "marq-marquee", 7 ) )
703     {
704         if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
705         p_sys->psz_marquee = strdup( newval.psz_string );
706     }
707     else */ if ( !strncmp( psz_var, "logo-x", 6 ) )
708     {
709         p_sys->posx = newval.i_int;
710     }
711     else if ( !strncmp( psz_var, "logo-y", 6 ) )
712     {
713         p_sys->posy = newval.i_int;
714     }
715     else if ( !strncmp( psz_var, "logo-position", 12 ) )
716     {
717         p_sys->pos = newval.i_int;
718     }
719     p_sys->b_need_update = VLC_TRUE;
720     return VLC_SUCCESS;
721 }