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