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