]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / video_filter / logo.c
1 /*****************************************************************************
2  * logo.c : logo video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2006 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_vout.h>
31
32 #include "vlc_filter.h"
33 #include "filter_common.h"
34 #include "vlc_image.h"
35 #include "vlc_osd.h"
36
37 #ifdef LoadImage
38 #   undef LoadImage
39 #endif
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  Create    ( vlc_object_t * );
45 static void Destroy   ( vlc_object_t * );
46
47 static int  Init      ( vout_thread_t * );
48 static void End       ( vout_thread_t * );
49 static void Render    ( vout_thread_t *, picture_t * );
50
51 static int  SendEvents( vlc_object_t *, char const *,
52                         vlc_value_t, vlc_value_t, void * );
53 static int  MouseEvent( vlc_object_t *, char const *,
54                         vlc_value_t , vlc_value_t , void * );
55 static int  Control   ( vout_thread_t *, int, va_list );
56
57 static int  CreateFilter ( vlc_object_t * );
58 static void DestroyFilter( vlc_object_t * );
59
60 static int LogoCallback( vlc_object_t *, char const *,
61                          vlc_value_t, vlc_value_t, void * );
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 #define FILE_TEXT N_("Logo filenames")
67 #define FILE_LONGTEXT N_("Full path of the image files to use. Format is " \
68 "<image>[,<delay in ms>[,<alpha>]][;<image>[,<delay>[,<alpha>]]][;...]. " \
69 "If you only have one file, simply enter its filename.")
70 #define REPEAT_TEXT N_("Logo animation # of loops")
71 #define REPEAT_LONGTEXT N_("Number of loops for the logo animation." \
72         "-1 = continuous, 0 = disabled")
73 #define DELAY_TEXT N_("Logo individual image time in ms")
74 #define DELAY_LONGTEXT N_("Individual image display time of 0 - 60000 ms.")
75
76 #define POSX_TEXT N_("X coordinate")
77 #define POSX_LONGTEXT N_("X coordinate of the logo. You can move the logo " \
78                 "by left-clicking it." )
79 #define POSY_TEXT N_("Y coordinate")
80 #define POSY_LONGTEXT N_("Y coordinate of the logo. You can move the logo " \
81                 "by left-clicking it." )
82 #define TRANS_TEXT N_("Transparency of the logo")
83 #define TRANS_LONGTEXT N_("Logo transparency value " \
84   "(from 0 for full transparency to 255 for full opacity)." )
85 #define POS_TEXT N_("Logo position")
86 #define POS_LONGTEXT N_( \
87   "Enforce the logo position on the video " \
88   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
89   "also use combinations of these values, eg 6 = top-right).")
90
91 #define CFG_PREFIX "logo-"
92
93 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
94 static const char *ppsz_pos_descriptions[] =
95 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
96   N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
97
98 vlc_module_begin();
99     set_description( _("Logo video filter") );
100     set_capability( "video filter", 0 );
101     set_shortname( _("Logo overlay") );
102     set_category( CAT_VIDEO );
103     set_subcategory( SUBCAT_VIDEO_SUBPIC );
104     add_shortcut( "logo" );
105     set_callbacks( Create, Destroy );
106
107     add_file( CFG_PREFIX "file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, VLC_FALSE );
108         change_safe();
109     add_integer( CFG_PREFIX "x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_TRUE );
110         change_safe();
111     add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_TRUE );
112         change_safe();
113     /* default to 1000 ms per image, continuously cycle through them */
114     add_integer( CFG_PREFIX "delay", 1000, NULL, DELAY_TEXT, DELAY_LONGTEXT, VLC_TRUE );
115         change_safe();
116     add_integer( CFG_PREFIX "repeat", -1, NULL, REPEAT_TEXT, REPEAT_LONGTEXT, VLC_TRUE );
117         change_safe();
118     add_integer_with_range( CFG_PREFIX "transparency", 255, 0, 255, NULL,
119         TRANS_TEXT, TRANS_LONGTEXT, VLC_FALSE );
120         change_safe();
121     add_integer( CFG_PREFIX "position", -1, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
122         change_safe();
123         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
124
125     /* subpicture filter submodule */
126     add_submodule();
127     set_capability( "sub filter", 0 );
128     set_callbacks( CreateFilter, DestroyFilter );
129     set_description( _("Logo sub filter") );
130 vlc_module_end();
131
132 static const char *ppsz_filter_options[] = {
133     "file", "x", "y", "delay", "repeat", "transparency", "position", NULL
134 };
135
136 /*****************************************************************************
137  * Structure to hold the set of individual logo image names, times,
138  * transparencies
139  ****************************************************************************/
140 typedef struct
141 {
142     char *psz_file;    /* candidate for deletion -- not needed */
143     int i_delay;       /* -1 means use default delay */
144     int i_alpha;       /* -1 means use default alpha */
145     picture_t *p_pic;
146
147 } logo_t;
148
149 /*****************************************************************************
150  * Logo list structure. Common to both the vout and sub picture filter
151  ****************************************************************************/
152 typedef struct
153 {
154     logo_t *p_logo;         /* the parsing's result */
155     unsigned int i_count;   /* the number of logo images to be displayed */
156
157     int i_repeat;         /* how often to repeat the images, image time in ms */
158     mtime_t i_next_pic;     /* when to bring up a new logo image */
159
160     unsigned int i_counter; /* index into the list of logo images */
161
162     int i_delay;            /* default delay (0 - 60000 ms) */
163     int i_alpha;            /* default alpha */
164
165     char *psz_filename;     /* --logo-file string ( is it really useful
166                              * to store it ? ) */
167
168     vlc_mutex_t lock;
169 } logo_list_t;
170
171 /*****************************************************************************
172  * LoadImage: loads the logo image into memory
173  *****************************************************************************/
174 static picture_t *LoadImage( vlc_object_t *p_this, char *psz_filename )
175 {
176     picture_t *p_pic;
177     image_handler_t *p_image;
178     video_format_t fmt_in;
179     video_format_t fmt_out;
180
181     memset( &fmt_in, 0, sizeof(video_format_t) );
182     memset( &fmt_out, 0, sizeof(video_format_t) );
183
184     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
185     p_image = image_HandlerCreate( p_this );
186     p_pic = image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
187     image_HandlerDelete( p_image );
188
189     return p_pic;
190 }
191
192 /*****************************************************************************
193  * LoadLogoList: loads the logo images into memory
194  *****************************************************************************
195  * Read the logo-file input switch, obtaining a list of images and associated
196  * durations and transparencies.  Store the image(s), and times.  An image
197  * without a stated time or transparency will use the logo-delay and
198  * logo-transparency values.
199  *****************************************************************************/
200 #define LoadLogoList( a, b ) __LoadLogoList( VLC_OBJECT( a ), b )
201 static void __LoadLogoList( vlc_object_t *p_this, logo_list_t *p_logo_list )
202 {
203     char *psz_list; /* the list: <logo>[,[<delay>[,[<alpha>]]]][;...] */
204     unsigned int i;
205     logo_t *p_logo;         /* the parsing's result */
206
207     p_logo_list->i_counter = 0;
208     p_logo_list->i_next_pic = 0;
209
210     psz_list = strdup( p_logo_list->psz_filename );
211
212     /* Count the number logos == number of ';' + 1 */
213     p_logo_list->i_count = 1;
214     for( i = 0; i < strlen( psz_list ); i++ )
215     {
216         if( psz_list[i] == ';' ) p_logo_list->i_count++;
217     }
218
219     p_logo_list->p_logo = p_logo =
220         (logo_t *)malloc( p_logo_list->i_count * sizeof(logo_t) );
221
222     /* Fill the data */
223     for( i = 0; i < p_logo_list->i_count; i++ )
224     {
225         char *p_c;
226         char *p_c2;
227         p_c = strchr( psz_list, ';' );
228         p_c2 = strchr( psz_list, ',' );
229
230         p_logo[i].i_alpha = -1; /* use default settings */
231         p_logo[i].i_delay = -1; /* use default settings */
232
233         if( p_c2 && ( p_c2 < p_c || !p_c ) )
234         {
235             /* <logo>,<delay>[,<alpha>] type */
236             if( p_c2[1] != ',' && p_c2[1] != ';' && p_c2[1] != '\0' )
237                 p_logo[i].i_delay = atoi( p_c2+1 );
238             *p_c2 = '\0';
239             if( ( p_c2 = strchr( p_c2+1, ',' ) )
240                 && ( p_c2 < p_c || !p_c ) && p_c2[1] != ';' && p_c2[1] != '\0' )
241                 p_logo[i].i_alpha = atoi( p_c2 + 1 );
242         }
243         else
244         {
245             /* <logo> type */
246             if( p_c ) *p_c = '\0';
247         }
248
249         p_logo[i].psz_file = strdup( psz_list );
250         p_logo[i].p_pic = LoadImage( p_this, p_logo[i].psz_file );
251
252         if( !p_logo[i].p_pic )
253         {
254             msg_Warn( p_this, "error while loading logo %s, will be skipped",
255                       p_logo[i].psz_file );
256         }
257
258         if( p_c ) psz_list = p_c + 1;
259     }
260
261     for( i = 0; i < p_logo_list->i_count; i++ )
262     {
263        msg_Dbg( p_this, "logo file name %s, delay %d, alpha %d",
264                 p_logo[i].psz_file, p_logo[i].i_delay, p_logo[i].i_alpha );
265     }
266
267     /* initialize so that on the first update it will wrap back to 0 */
268     p_logo_list->i_counter = p_logo_list->i_count;
269 }
270
271 /*****************************************************************************
272  * FreeLogoList
273  *****************************************************************************/
274 static void FreeLogoList( logo_list_t *p_logo_list )
275 {
276     unsigned int i;
277     FREENULL( p_logo_list->psz_filename );
278     for( i = 0; i < p_logo_list->i_count; i++ )
279     {
280         logo_t *p_logo = &p_logo_list->p_logo[i];
281         FREENULL( p_logo->psz_file );
282         if( p_logo->p_pic )
283         {
284             p_logo->p_pic->pf_release( p_logo->p_pic );
285             p_logo->p_pic = NULL;
286         }
287     }
288 }
289
290 /*****************************************************************************
291  * vout_sys_t: logo video output method descriptor
292  *****************************************************************************
293  * This structure is part of the video output thread descriptor.
294  * It describes the Invert specific properties of an output thread.
295  *****************************************************************************/
296 struct vout_sys_t
297 {
298     logo_list_t *p_logo_list;
299
300     vout_thread_t *p_vout;
301
302     filter_t *p_blend;
303
304     int i_width, i_height;
305     int pos, posx, posy;
306 };
307
308 /*****************************************************************************
309  * Create: allocates logo video thread output method
310  *****************************************************************************/
311 static int Create( vlc_object_t *p_this )
312 {
313     vout_thread_t *p_vout = (vout_thread_t *)p_this;
314     vout_sys_t *p_sys;
315     logo_list_t *p_logo_list;
316
317     /* Allocate structure */
318     p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
319     if( p_sys == NULL )
320     {
321         msg_Err( p_vout, "out of memory" );
322         return VLC_ENOMEM;
323     }
324     p_logo_list = p_sys->p_logo_list = malloc( sizeof( logo_list_t ) );
325     if( p_logo_list == NULL )
326     {
327         msg_Err( p_vout, "out of memory" );
328         free( p_sys );
329         return VLC_ENOMEM;
330     }
331
332     config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
333                        p_vout->p_cfg );
334
335     p_logo_list->psz_filename = var_CreateGetStringCommand( p_vout,
336                                                             "logo-file" );
337     if( !p_logo_list->psz_filename || !*p_logo_list->psz_filename )
338     {
339         msg_Err( p_vout, "logo file not specified" );
340         return VLC_EGENERIC;
341     }
342
343     p_vout->pf_init = Init;
344     p_vout->pf_end = End;
345     p_vout->pf_manage = NULL;
346     p_vout->pf_render = Render;
347     p_vout->pf_display = NULL;
348     p_vout->pf_control = Control;
349
350     p_sys->pos = var_CreateGetIntegerCommand( p_vout, "logo-position" );
351     p_sys->posx = var_CreateGetIntegerCommand( p_vout, "logo-x" );
352     p_sys->posy = var_CreateGetIntegerCommand( p_vout, "logo-y" );
353     p_logo_list->i_delay = __MAX( __MIN(
354         var_CreateGetIntegerCommand( p_vout, "logo-delay" ) , 60000 ), 0 );
355     p_logo_list->i_repeat = var_CreateGetIntegerCommand( p_vout, "logo-repeat");
356     p_logo_list->i_alpha = __MAX( __MIN(
357         var_CreateGetIntegerCommand( p_vout, "logo-transparency" ), 255 ), 0 );
358
359     LoadLogoList( p_vout, p_logo_list );
360
361     return VLC_SUCCESS;
362 }
363
364 /*****************************************************************************
365  * Init: initialize logo video thread output method
366  *****************************************************************************/
367 static int Init( vout_thread_t *p_vout )
368 {
369     vout_sys_t *p_sys = p_vout->p_sys;
370     picture_t *p_pic;
371     int i_index;
372     video_format_t fmt;
373     logo_list_t *p_logo_list = p_sys->p_logo_list;
374
375     I_OUTPUTPICTURES = 0;
376     memset( &fmt, 0, sizeof(video_format_t) );
377
378     /* adjust index to the next logo */
379     p_logo_list->i_counter =
380                         ( p_logo_list->i_counter + 1 )%p_logo_list->i_count;
381
382     p_pic = p_logo_list->p_logo[p_logo_list->i_counter].p_pic;
383     /* Initialize the output structure */
384     p_vout->output.i_chroma = p_vout->render.i_chroma;
385     p_vout->output.i_width  = p_vout->render.i_width;
386     p_vout->output.i_height = p_vout->render.i_height;
387     p_vout->output.i_aspect = p_vout->render.i_aspect;
388     p_vout->fmt_out = p_vout->fmt_in;
389     fmt = p_vout->fmt_out;
390
391     /* Load the video blending filter */
392     p_sys->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
393     vlc_object_attach( p_sys->p_blend, p_vout );
394     p_sys->p_blend->fmt_out.video.i_x_offset =
395         p_sys->p_blend->fmt_out.video.i_y_offset = 0;
396     p_sys->p_blend->fmt_in.video.i_x_offset =
397         p_sys->p_blend->fmt_in.video.i_y_offset = 0;
398     p_sys->p_blend->fmt_out.video.i_aspect = p_vout->render.i_aspect;
399     p_sys->p_blend->fmt_out.video.i_chroma = p_vout->output.i_chroma;
400     p_sys->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','A');
401     p_sys->p_blend->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR;
402     p_sys->i_width =
403         p_sys->p_blend->fmt_in.video.i_width =
404             p_sys->p_blend->fmt_in.video.i_visible_width =
405                 p_pic ? p_pic->p[Y_PLANE].i_visible_pitch : 0;
406     p_sys->i_height =
407         p_sys->p_blend->fmt_in.video.i_height =
408             p_sys->p_blend->fmt_in.video.i_visible_height =
409                 p_pic ? p_pic->p[Y_PLANE].i_visible_lines : 0;
410     p_sys->p_blend->fmt_out.video.i_width =
411         p_sys->p_blend->fmt_out.video.i_visible_width =
412            p_vout->output.i_width;
413     p_sys->p_blend->fmt_out.video.i_height =
414         p_sys->p_blend->fmt_out.video.i_visible_height =
415             p_vout->output.i_height;
416
417     p_sys->p_blend->p_module =
418         module_Need( p_sys->p_blend, "video blending", 0, 0 );
419     if( !p_sys->p_blend->p_module )
420     {
421         msg_Err( p_vout, "can't open blending filter, aborting" );
422         vlc_object_detach( p_sys->p_blend );
423         vlc_object_destroy( p_sys->p_blend );
424         return VLC_EGENERIC;
425     }
426
427     if( p_sys->posx < 0 || p_sys->posy < 0 )
428     {
429         p_sys->posx = 0; p_sys->posy = 0;
430
431         if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
432         {
433             p_sys->posy = p_vout->render.i_height - p_sys->i_height;
434         }
435         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
436         {
437             p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
438         }
439
440         if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
441         {
442             p_sys->posx = p_vout->render.i_width - p_sys->i_width;
443         }
444         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
445         {
446             p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
447         }
448     }
449     else
450     {
451         p_sys->pos = 0;
452     }
453
454     /* Try to open the real video output */
455     msg_Dbg( p_vout, "spawning the real video output" );
456
457     p_sys->p_vout = vout_Create( p_vout, &fmt );
458
459     /* Everything failed */
460     if( p_sys->p_vout == NULL )
461     {
462         msg_Err( p_vout, "can't open vout, aborting" );
463         return VLC_EGENERIC;
464     }
465
466     var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
467     var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
468
469     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
470     ADD_CALLBACKS( p_sys->p_vout, SendEvents );
471     ADD_PARENT_CALLBACKS( SendEventsToChild );
472
473     return VLC_SUCCESS;
474 }
475
476 /*****************************************************************************
477  * End: terminate logo video thread output method
478  *****************************************************************************/
479 static void End( vout_thread_t *p_vout )
480 {
481     vout_sys_t *p_sys = p_vout->p_sys;
482     int i_index;
483
484     /* Free the fake output buffers we allocated */
485     for( i_index = I_OUTPUTPICTURES ; i_index ; )
486     {
487         i_index--;
488         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
489     }
490
491     var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
492     var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
493
494     if( p_sys->p_vout )
495     {
496         DEL_CALLBACKS( p_sys->p_vout, SendEvents );
497         vlc_object_detach( p_sys->p_vout );
498         vout_Destroy( p_sys->p_vout );
499     }
500
501     if( p_sys->p_blend->p_module )
502         module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
503     vlc_object_detach( p_sys->p_blend );
504     vlc_object_destroy( p_sys->p_blend );
505 }
506
507 /*****************************************************************************
508  * Destroy: destroy logo video thread output method
509  *****************************************************************************/
510 static void Destroy( vlc_object_t *p_this )
511 {
512     vout_thread_t *p_vout = (vout_thread_t *)p_this;
513     vout_sys_t *p_sys = p_vout->p_sys;
514
515     DEL_PARENT_CALLBACKS( SendEventsToChild );
516
517     FreeLogoList( p_sys->p_logo_list );
518     free( p_sys->p_logo_list );
519
520     free( p_sys );
521 }
522
523 /*****************************************************************************
524  * Render: render the logo onto the video
525  *****************************************************************************/
526 static void Render( vout_thread_t *p_vout, picture_t *p_inpic )
527 {
528     vout_sys_t *p_sys = p_vout->p_sys;
529     picture_t *p_outpic;
530     picture_t *p_pic;
531     logo_list_t *p_logo_list;
532     logo_t * p_logo;
533
534     p_logo_list = p_sys->p_logo_list;
535
536     if( p_logo_list->i_next_pic < p_inpic->date )
537     {
538         /* It's time to use a new logo */
539         p_logo_list->i_counter =
540                         ( p_logo_list->i_counter + 1 )%p_logo_list->i_count;
541         p_logo = &p_logo_list->p_logo[p_sys->p_logo_list->i_counter];
542         p_pic = p_logo->p_pic;
543         p_logo_list->i_next_pic = p_inpic->date + ( p_logo->i_delay != -1 ?
544                               p_logo->i_delay : p_logo_list->i_delay ) * 1000;
545         if( p_pic )
546         {
547
548             p_sys->i_width =
549                 p_sys->p_blend->fmt_in.video.i_width =
550                     p_sys->p_blend->fmt_in.video.i_visible_width =
551                         p_pic->p[Y_PLANE].i_visible_pitch;
552             p_sys->i_height =
553                 p_sys->p_blend->fmt_in.video.i_height =
554                     p_sys->p_blend->fmt_in.video.i_visible_height =
555                         p_pic->p[Y_PLANE].i_visible_lines;
556
557             if( p_sys->pos )
558             {
559                 if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
560                 {
561                     p_sys->posy = p_vout->render.i_height - p_sys->i_height;
562                 }
563                 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
564                 {
565                     p_sys->posy = p_vout->render.i_height/2 - p_sys->i_height/2;
566                 }
567                 if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
568                 {
569                     p_sys->posx = p_vout->render.i_width - p_sys->i_width;
570                 }
571                 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
572                 {
573                     p_sys->posx = p_vout->render.i_width/2 - p_sys->i_width/2;
574                 }
575             }
576         }
577
578     }
579     else
580     {
581         p_logo = &p_logo_list->p_logo[p_sys->p_logo_list->i_counter];
582         p_pic = p_logo->p_pic;
583     }
584
585     /* This is a new frame. Get a structure from the video_output. */
586     while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
587     {
588         if( p_vout->b_die || p_vout->b_error ) return;
589         msleep( VOUT_OUTMEM_SLEEP );
590     }
591
592     vout_CopyPicture( p_vout, p_outpic, p_inpic );
593     vout_DatePicture( p_sys->p_vout, p_outpic, p_inpic->date );
594
595     if( p_pic )
596     p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic, p_outpic,
597                                     p_pic, p_sys->posx, p_sys->posy,
598                                     p_logo->i_alpha != -1 ? p_logo->i_alpha
599                                     : p_logo_list->i_alpha );
600
601     vout_DisplayPicture( p_sys->p_vout, p_outpic );
602 }
603
604 /*****************************************************************************
605  * SendEvents: forward mouse and keyboard events to the parent p_vout
606  *****************************************************************************/
607 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
608                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
609 {
610     var_Set( (vlc_object_t *)p_data, psz_var, newval );
611     return VLC_SUCCESS;
612 }
613
614 /*****************************************************************************
615  * MouseEvent: callback for mouse events
616  *****************************************************************************/
617 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
618                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
619 {
620     vout_thread_t *p_vout = (vout_thread_t*)p_data;
621     vout_sys_t *p_sys = p_vout->p_sys;
622     vlc_value_t valb;
623     int i_delta;
624
625     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
626
627     i_delta = newval.i_int - oldval.i_int;
628
629     if( (valb.i_int & 0x1) == 0 )
630     {
631         return VLC_SUCCESS;
632     }
633
634     if( psz_var[6] == 'x' )
635     {
636         vlc_value_t valy;
637         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
638         if( newval.i_int >= (int)p_sys->posx &&
639             valy.i_int >= (int)p_sys->posy &&
640             newval.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
641             valy.i_int <= (int)(p_sys->posy + p_sys->i_height) )
642         {
643             p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
644                           p_vout->output.i_width - p_sys->i_width );
645         }
646     }
647     else if( psz_var[6] == 'y' )
648     {
649         vlc_value_t valx;
650         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
651         if( valx.i_int >= (int)p_sys->posx &&
652             newval.i_int >= (int)p_sys->posy &&
653             valx.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
654             newval.i_int <= (int)(p_sys->posy + p_sys->i_height) )
655         {
656             p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
657                           p_vout->output.i_height - p_sys->i_height );
658         }
659     }
660
661     return VLC_SUCCESS;
662 }
663
664 /*****************************************************************************
665  * Control: control facility for the vout (forwards to child vout)
666  *****************************************************************************/
667 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
668 {
669     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
670 }
671
672 /*****************************************************************************
673  * SendEventsToChild: forward events to the child/children vout
674  *****************************************************************************/
675 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
676                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
677 {
678     vout_thread_t *p_vout = (vout_thread_t *)p_this;
679     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
680     return VLC_SUCCESS;
681 }
682
683 /*****************************************************************************
684  * filter_sys_t: logo filter descriptor
685  *****************************************************************************/
686 struct filter_sys_t
687 {
688     logo_list_t *p_logo_list;
689
690     int pos, posx, posy;
691
692     vlc_bool_t b_absolute;
693     mtime_t i_last_date;
694
695     /* On the fly control variable */
696     vlc_bool_t b_need_update;
697 };
698
699 static subpicture_t *Filter( filter_t *, mtime_t );
700
701 /*****************************************************************************
702  * CreateFilter: allocates logo video filter
703  *****************************************************************************/
704 static int CreateFilter( vlc_object_t *p_this )
705 {
706     filter_t *p_filter = (filter_t *)p_this;
707     filter_sys_t *p_sys;
708     logo_list_t *p_logo_list;
709
710     /* Allocate structure */
711     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
712     if( p_sys == NULL )
713     {
714         msg_Err( p_filter, "out of memory" );
715         return VLC_ENOMEM;
716     }
717     p_logo_list = p_sys->p_logo_list = malloc( sizeof( logo_list_t ) );
718     if( p_logo_list == NULL )
719     {
720         msg_Err( p_filter, "out of memory" );
721         free( p_sys );
722         return VLC_ENOMEM;
723     }
724
725     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
726                        p_filter->p_cfg );
727
728     /* Hook used for callback variables */
729     p_logo_list->psz_filename =
730         var_CreateGetStringCommand( p_filter, "logo-file" );
731     if( !p_logo_list->psz_filename || !*p_logo_list->psz_filename )
732     {
733         msg_Err( p_this, "logo file not specified" );
734         free( p_sys );
735         free( p_logo_list );
736         return VLC_EGENERIC;
737     }
738
739     p_sys->posx = var_CreateGetIntegerCommand( p_filter, "logo-x" );
740     p_sys->posy = var_CreateGetIntegerCommand( p_filter, "logo-y" );
741     p_sys->pos = var_CreateGetIntegerCommand( p_filter, "logo-position" );
742     p_logo_list->i_alpha = __MAX( __MIN( var_CreateGetIntegerCommand(
743                            p_filter, "logo-transparency"), 255 ), 0 );
744     p_logo_list->i_delay =
745         var_CreateGetIntegerCommand( p_filter, "logo-delay" );
746     p_logo_list->i_repeat =
747         var_CreateGetIntegerCommand( p_filter, "logo-repeat" );
748
749     var_AddCallback( p_filter, "logo-file", LogoCallback, p_sys );
750     var_AddCallback( p_filter, "logo-x", LogoCallback, p_sys );
751     var_AddCallback( p_filter, "logo-y", LogoCallback, p_sys );
752     var_AddCallback( p_filter, "logo-position", LogoCallback, p_sys );
753     var_AddCallback( p_filter, "logo-transparency", LogoCallback, p_sys );
754     var_AddCallback( p_filter, "logo-repeat", LogoCallback, p_sys );
755
756     vlc_mutex_init( p_filter, &p_logo_list->lock );
757     vlc_mutex_lock( &p_logo_list->lock );
758
759     LoadLogoList( p_this, p_logo_list );
760
761     vlc_mutex_unlock( &p_logo_list->lock );
762
763     /* Misc init */
764     p_filter->pf_sub_filter = Filter;
765     p_sys->b_need_update = VLC_TRUE;
766
767     p_sys->i_last_date = 0;
768
769     return VLC_SUCCESS;
770 }
771
772 /*****************************************************************************
773  * DestroyFilter: destroy logo video filter
774  *****************************************************************************/
775 static void DestroyFilter( vlc_object_t *p_this )
776 {
777     filter_t *p_filter = (filter_t *)p_this;
778     filter_sys_t *p_sys = p_filter->p_sys;
779
780     vlc_mutex_destroy( &p_sys->p_logo_list->lock );
781     FreeLogoList( p_sys->p_logo_list );
782     free( p_sys->p_logo_list );
783     free( p_sys );
784
785     /* Delete the logo variables from INPUT */
786     var_Destroy( p_filter->p_libvlc, "logo-file" );
787     var_Destroy( p_filter->p_libvlc, "logo-x" );
788     var_Destroy( p_filter->p_libvlc, "logo-y" );
789     var_Destroy( p_filter->p_libvlc, "logo-delay" );
790     var_Destroy( p_filter->p_libvlc, "logo-repeat" );
791     var_Destroy( p_filter->p_libvlc, "logo-position" );
792     var_Destroy( p_filter->p_libvlc, "logo-transparency" );
793 }
794
795 /*****************************************************************************
796  * Filter: the whole thing
797  *****************************************************************************
798  * This function outputs subpictures at regular time intervals.
799  *****************************************************************************/
800 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
801 {
802     filter_sys_t *p_sys = p_filter->p_sys;
803     logo_list_t *p_logo_list = p_sys->p_logo_list;
804     subpicture_t *p_spu;
805     subpicture_region_t *p_region;
806     video_format_t fmt;
807     picture_t *p_pic;
808     logo_t *p_logo;
809
810     vlc_mutex_lock( &p_logo_list->lock );
811     /* Basic test:  b_need_update occurs on a dynamic change,
812                     & i_next_pic is the general timer, when to
813                     look at updating the logo image */
814
815     if( ( ( !p_sys->b_need_update ) && ( p_logo_list->i_next_pic > date ) )
816         || !p_logo_list->i_repeat )
817     {
818         vlc_mutex_unlock( &p_logo_list->lock );
819         return 0;
820     }
821     /* prior code tested on && p_sys->i_last_date +5000000 > date ) return 0; */
822
823     /* adjust index to the next logo */
824     p_logo_list->i_counter =
825                         ( p_logo_list->i_counter + 1 )%p_logo_list->i_count;
826
827     p_logo = &p_logo_list->p_logo[p_logo_list->i_counter];
828     p_pic = p_logo->p_pic;
829
830     /* Allocate the subpicture internal data. */
831     p_spu = p_filter->pf_sub_buffer_new( p_filter );
832     if( !p_spu )
833     {
834         vlc_mutex_unlock( &p_logo_list->lock );
835         return NULL;
836     }
837
838     p_spu->b_absolute = p_sys->b_absolute;
839     p_spu->i_start = p_sys->i_last_date = date;
840     p_spu->i_stop = 0;
841     p_spu->b_ephemer = VLC_TRUE;
842
843     p_sys->b_need_update = VLC_FALSE;
844     p_logo_list->i_next_pic = date +
845     ( p_logo->i_delay != -1 ? p_logo->i_delay : p_logo_list->i_delay ) * 1000;
846
847     if( p_logo_list->i_repeat != -1
848         && p_logo_list->i_counter == 0 )
849     {
850         p_logo_list->i_repeat--;
851         if( p_logo_list->i_repeat == 0 )
852         {
853             vlc_mutex_unlock( &p_logo_list->lock );
854             return p_spu;
855         }
856     }
857
858     if( !p_pic || !p_logo->i_alpha
859         || ( p_logo->i_alpha == -1 && !p_logo_list->i_alpha ) )
860     {
861         /* Send an empty subpicture to clear the display */
862         vlc_mutex_unlock( &p_logo_list->lock );
863         return p_spu;
864     }
865
866     /* Create new SPU region */
867     memset( &fmt, 0, sizeof(video_format_t) );
868     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
869     fmt.i_aspect = VOUT_ASPECT_FACTOR;
870     fmt.i_sar_num = fmt.i_sar_den = 1;
871     fmt.i_width = fmt.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
872     fmt.i_height = fmt.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
873     fmt.i_x_offset = fmt.i_y_offset = 0;
874     p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
875     if( !p_region )
876     {
877         msg_Err( p_filter, "cannot allocate SPU region" );
878         p_filter->pf_sub_buffer_del( p_filter, p_spu );
879         vlc_mutex_unlock( &p_logo_list->lock );
880         return NULL;
881     }
882
883     vout_CopyPicture( p_filter, &p_region->picture, p_pic );
884     vlc_mutex_unlock( &p_logo_list->lock );
885
886     /*  where to locate the logo: */
887     if( p_sys->pos < 0 )
888     {   /*  set to an absolute xy */
889         p_spu->p_region->i_align = OSD_ALIGN_RIGHT | OSD_ALIGN_TOP;
890         p_spu->b_absolute = VLC_TRUE;
891     }
892     else
893     {   /* set to one of the 9 relative locations */
894         p_spu->p_region->i_align = p_sys->pos;
895         p_spu->b_absolute = VLC_FALSE;
896     }
897
898     p_spu->i_x = p_sys->posx;
899     p_spu->i_y = p_sys->posy;
900
901     p_spu->p_region = p_region;
902
903     p_spu->i_alpha = ( p_logo->i_alpha != -1 ?
904                        p_logo->i_alpha : p_logo_list->i_alpha );
905
906     return p_spu;
907 }
908
909 /*****************************************************************************
910  * Callback to update params on the fly
911  *****************************************************************************/
912 static int LogoCallback( vlc_object_t *p_this, char const *psz_var,
913                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
914 {
915     filter_sys_t *p_sys = (filter_sys_t *)p_data;
916     logo_list_t *p_logo_list = p_sys->p_logo_list;
917
918     if( !strncmp( psz_var, "logo-file", 6 ) )
919     {
920         vlc_mutex_lock( &p_logo_list->lock );
921         FreeLogoList( p_logo_list );
922         p_logo_list->psz_filename = strdup( newval.psz_string );
923         LoadLogoList( p_this, p_logo_list );
924         vlc_mutex_unlock( &p_logo_list->lock );
925         p_sys->b_need_update = VLC_TRUE;
926     }
927     else if ( !strncmp( psz_var, "logo-x", 6 ) )
928     {
929         p_sys->posx = newval.i_int;
930     }
931     else if ( !strncmp( psz_var, "logo-y", 6 ) )
932     {
933         p_sys->posy = newval.i_int;
934     }
935     else if ( !strncmp( psz_var, "logo-position", 12 ) )
936     {
937         p_sys->pos = newval.i_int;
938     }
939     else if ( !strncmp( psz_var, "logo-transparency", 9 ) )
940     {
941         vlc_mutex_lock( &p_logo_list->lock );
942         p_logo_list->i_alpha = __MAX( __MIN( newval.i_int, 255 ), 0 );
943         vlc_mutex_unlock( &p_logo_list->lock );
944     }
945     else if ( !strncmp( psz_var, "logo-repeat", 11 ) )
946     {
947         vlc_mutex_lock( &p_logo_list->lock );
948         p_logo_list->i_repeat = newval.i_int;
949         vlc_mutex_unlock( &p_logo_list->lock );
950     }
951     p_sys->b_need_update = VLC_TRUE;
952     return VLC_SUCCESS;
953 }