]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
2de5dc89c52d22d4536c72c882ac36f9f5c170aa
[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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_vout.h>
35
36 #include "vlc_filter.h"
37 #include "filter_common.h"
38 #include "vlc_image.h"
39 #include "vlc_osd.h"
40
41 #ifdef LoadImage
42 #   undef LoadImage
43 #endif
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int  Create    ( vlc_object_t * );
49 static void Destroy   ( vlc_object_t * );
50
51 static int  Init      ( vout_thread_t * );
52 static void End       ( vout_thread_t * );
53 static void Render    ( vout_thread_t *, picture_t * );
54
55 static int  SendEvents( vlc_object_t *, char const *,
56                         vlc_value_t, vlc_value_t, void * );
57 static int  MouseEvent( vlc_object_t *, char const *,
58                         vlc_value_t , vlc_value_t , void * );
59 static int  Control   ( vout_thread_t *, int, va_list );
60
61 static int  CreateFilter ( vlc_object_t * );
62 static void DestroyFilter( vlc_object_t * );
63
64 static int LogoCallback( vlc_object_t *, char const *,
65                          vlc_value_t, vlc_value_t, void * );
66
67 /*****************************************************************************
68  * Module descriptor
69  *****************************************************************************/
70 #define FILE_TEXT N_("Logo filenames")
71 #define FILE_LONGTEXT N_("Full path of the image files to use. Format is " \
72 "<image>[,<delay in ms>[,<alpha>]][;<image>[,<delay>[,<alpha>]]][;...]. " \
73 "If you only have one file, simply enter its filename.")
74 #define REPEAT_TEXT N_("Logo animation # of loops")
75 #define REPEAT_LONGTEXT N_("Number of loops for the logo animation." \
76         "-1 = continuous, 0 = disabled")
77 #define DELAY_TEXT N_("Logo individual image time in ms")
78 #define DELAY_LONGTEXT N_("Individual image display time of 0 - 60000 ms.")
79
80 #define POSX_TEXT N_("X coordinate")
81 #define POSX_LONGTEXT N_("X coordinate of the logo. You can move the logo " \
82                 "by left-clicking it." )
83 #define POSY_TEXT N_("Y coordinate")
84 #define POSY_LONGTEXT N_("Y coordinate of the logo. You can move the logo " \
85                 "by left-clicking it." )
86 #define TRANS_TEXT N_("Transparency of the logo")
87 #define TRANS_LONGTEXT N_("Logo transparency value " \
88   "(from 0 for full transparency to 255 for full opacity)." )
89 #define POS_TEXT N_("Logo position")
90 #define POS_LONGTEXT N_( \
91   "Enforce the logo position on the video " \
92   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
93   "also use combinations of these values, eg 6 = top-right).")
94
95 #define CFG_PREFIX "logo-"
96
97 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
98 static const char *ppsz_pos_descriptions[] =
99 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
100   N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
101
102 vlc_module_begin();
103     set_description( _("Logo video filter") );
104     set_capability( "video filter", 0 );
105     set_shortname( _("Logo overlay") );
106     set_category( CAT_VIDEO );
107     set_subcategory( SUBCAT_VIDEO_SUBPIC );
108     add_shortcut( "logo" );
109     set_callbacks( Create, Destroy );
110
111     add_file( CFG_PREFIX "file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, false );
112     add_integer( CFG_PREFIX "x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, true );
113     add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, true );
114     /* default to 1000 ms per image, continuously cycle through them */
115     add_integer( CFG_PREFIX "delay", 1000, NULL, DELAY_TEXT, DELAY_LONGTEXT, true );
116     add_integer( CFG_PREFIX "repeat", -1, NULL, REPEAT_TEXT, REPEAT_LONGTEXT, true );
117     add_integer_with_range( CFG_PREFIX "transparency", 255, 0, 255, NULL,
118         TRANS_TEXT, TRANS_LONGTEXT, false );
119     add_integer( CFG_PREFIX "position", -1, NULL, POS_TEXT, POS_LONGTEXT, false );
120         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
121
122     /* subpicture filter submodule */
123     add_submodule();
124     set_capability( "sub filter", 0 );
125     set_callbacks( CreateFilter, DestroyFilter );
126     set_description( _("Logo sub filter") );
127 vlc_module_end();
128
129 static const char *ppsz_filter_options[] = {
130     "file", "x", "y", "delay", "repeat", "transparency", "position", NULL
131 };
132
133 /*****************************************************************************
134  * Structure to hold the set of individual logo image names, times,
135  * transparencies
136  ****************************************************************************/
137 typedef struct
138 {
139     char *psz_file;    /* candidate for deletion -- not needed */
140     int i_delay;       /* -1 means use default delay */
141     int i_alpha;       /* -1 means use default alpha */
142     picture_t *p_pic;
143
144 } logo_t;
145
146 /*****************************************************************************
147  * Logo list structure. Common to both the vout and sub picture filter
148  ****************************************************************************/
149 typedef struct
150 {
151     logo_t *p_logo;         /* the parsing's result */
152     unsigned int i_count;   /* the number of logo images to be displayed */
153
154     int i_repeat;         /* how often to repeat the images, image time in ms */
155     mtime_t i_next_pic;     /* when to bring up a new logo image */
156
157     unsigned int i_counter; /* index into the list of logo images */
158
159     int i_delay;            /* default delay (0 - 60000 ms) */
160     int i_alpha;            /* default alpha */
161
162     char *psz_filename;     /* --logo-file string ( is it really useful
163                              * to store it ? ) */
164
165     vlc_mutex_t lock;
166 } logo_list_t;
167
168 /*****************************************************************************
169  * LoadImage: loads the logo image into memory
170  *****************************************************************************/
171 static picture_t *LoadImage( vlc_object_t *p_this, char *psz_filename )
172 {
173     picture_t *p_pic;
174     image_handler_t *p_image;
175     video_format_t fmt_in;
176     video_format_t fmt_out;
177
178     memset( &fmt_in, 0, sizeof(video_format_t) );
179     memset( &fmt_out, 0, sizeof(video_format_t) );
180
181     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
182     p_image = image_HandlerCreate( p_this );
183     p_pic = image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
184     image_HandlerDelete( p_image );
185
186     return p_pic;
187 }
188
189 /*****************************************************************************
190  * LoadLogoList: loads the logo images into memory
191  *****************************************************************************
192  * Read the logo-file input switch, obtaining a list of images and associated
193  * durations and transparencies.  Store the image(s), and times.  An image
194  * without a stated time or transparency will use the logo-delay and
195  * logo-transparency values.
196  *****************************************************************************/
197 #define LoadLogoList( a, b ) __LoadLogoList( VLC_OBJECT( a ), b )
198 static void __LoadLogoList( vlc_object_t *p_this, logo_list_t *p_logo_list )
199 {
200     char *psz_list; /* the list: <logo>[,[<delay>[,[<alpha>]]]][;...] */
201     unsigned int i;
202     logo_t *p_logo;         /* the parsing's result */
203
204     p_logo_list->i_counter = 0;
205     p_logo_list->i_next_pic = 0;
206
207     psz_list = strdup( p_logo_list->psz_filename );
208
209     /* Count the number logos == number of ';' + 1 */
210     p_logo_list->i_count = 1;
211     for( i = 0; i < strlen( psz_list ); i++ )
212     {
213         if( psz_list[i] == ';' ) p_logo_list->i_count++;
214     }
215
216     p_logo_list->p_logo = p_logo =
217         (logo_t *)malloc( p_logo_list->i_count * sizeof(logo_t) );
218
219     /* Fill the data */
220     for( i = 0; i < p_logo_list->i_count; i++ )
221     {
222         char *p_c;
223         char *p_c2;
224         p_c = strchr( psz_list, ';' );
225         p_c2 = strchr( psz_list, ',' );
226
227         p_logo[i].i_alpha = -1; /* use default settings */
228         p_logo[i].i_delay = -1; /* use default settings */
229
230         if( p_c2 && ( p_c2 < p_c || !p_c ) )
231         {
232             /* <logo>,<delay>[,<alpha>] type */
233             if( p_c2[1] != ',' && p_c2[1] != ';' && p_c2[1] != '\0' )
234                 p_logo[i].i_delay = atoi( p_c2+1 );
235             *p_c2 = '\0';
236             if( ( p_c2 = strchr( p_c2+1, ',' ) )
237                 && ( p_c2 < p_c || !p_c ) && p_c2[1] != ';' && p_c2[1] != '\0' )
238                 p_logo[i].i_alpha = atoi( p_c2 + 1 );
239         }
240         else
241         {
242             /* <logo> type */
243             if( p_c ) *p_c = '\0';
244         }
245
246         p_logo[i].psz_file = strdup( psz_list );
247         p_logo[i].p_pic = LoadImage( p_this, p_logo[i].psz_file );
248
249         if( !p_logo[i].p_pic )
250         {
251             msg_Warn( p_this, "error while loading logo %s, will be skipped",
252                       p_logo[i].psz_file );
253         }
254
255         if( p_c ) psz_list = p_c + 1;
256     }
257
258     for( i = 0; i < p_logo_list->i_count; i++ )
259     {
260        msg_Dbg( p_this, "logo file name %s, delay %d, alpha %d",
261                 p_logo[i].psz_file, p_logo[i].i_delay, p_logo[i].i_alpha );
262     }
263
264     /* initialize so that on the first update it will wrap back to 0 */
265     p_logo_list->i_counter = p_logo_list->i_count;
266 }
267
268 /*****************************************************************************
269  * FreeLogoList
270  *****************************************************************************/
271 static void FreeLogoList( logo_list_t *p_logo_list )
272 {
273     unsigned int i;
274     FREENULL( p_logo_list->psz_filename );
275     for( i = 0; i < p_logo_list->i_count; i++ )
276     {
277         logo_t *p_logo = &p_logo_list->p_logo[i];
278         FREENULL( p_logo->psz_file );
279         if( p_logo->p_pic )
280         {
281             p_logo->p_pic->pf_release( p_logo->p_pic );
282             p_logo->p_pic = NULL;
283         }
284     }
285 }
286
287 /*****************************************************************************
288  * vout_sys_t: logo video output method descriptor
289  *****************************************************************************
290  * This structure is part of the video output thread descriptor.
291  * It describes the Invert specific properties of an output thread.
292  *****************************************************************************/
293 struct vout_sys_t
294 {
295     logo_list_t *p_logo_list;
296
297     vout_thread_t *p_vout;
298
299     filter_t *p_blend;
300
301     int i_width, i_height;
302     int pos, posx, posy;
303 };
304
305 /*****************************************************************************
306  * Create: allocates logo video thread output method
307  *****************************************************************************/
308 static int Create( vlc_object_t *p_this )
309 {
310     vout_thread_t *p_vout = (vout_thread_t *)p_this;
311     vout_sys_t *p_sys;
312     logo_list_t *p_logo_list;
313
314     /* Allocate structure */
315     p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
316     if( p_sys == NULL )
317     {
318         msg_Err( p_vout, "out of memory" );
319         return VLC_ENOMEM;
320     }
321     p_logo_list = p_sys->p_logo_list = malloc( sizeof( logo_list_t ) );
322     if( p_logo_list == NULL )
323     {
324         msg_Err( p_vout, "out of memory" );
325         free( p_sys );
326         return VLC_ENOMEM;
327     }
328
329     config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
330                        p_vout->p_cfg );
331
332     p_logo_list->psz_filename = var_CreateGetStringCommand( p_vout,
333                                                             "logo-file" );
334     if( !p_logo_list->psz_filename || !*p_logo_list->psz_filename )
335     {
336         msg_Err( p_vout, "logo file not specified" );
337         return VLC_EGENERIC;
338     }
339
340     p_vout->pf_init = Init;
341     p_vout->pf_end = End;
342     p_vout->pf_manage = NULL;
343     p_vout->pf_render = Render;
344     p_vout->pf_display = NULL;
345     p_vout->pf_control = Control;
346
347     p_sys->pos = var_CreateGetIntegerCommand( p_vout, "logo-position" );
348     p_sys->posx = var_CreateGetIntegerCommand( p_vout, "logo-x" );
349     p_sys->posy = var_CreateGetIntegerCommand( p_vout, "logo-y" );
350     p_logo_list->i_delay = __MAX( __MIN(
351         var_CreateGetIntegerCommand( p_vout, "logo-delay" ) , 60000 ), 0 );
352     p_logo_list->i_repeat = var_CreateGetIntegerCommand( p_vout, "logo-repeat");
353     p_logo_list->i_alpha = __MAX( __MIN(
354         var_CreateGetIntegerCommand( p_vout, "logo-transparency" ), 255 ), 0 );
355
356     LoadLogoList( p_vout, p_logo_list );
357
358     return VLC_SUCCESS;
359 }
360
361 /*****************************************************************************
362  * Init: initialize logo video thread output method
363  *****************************************************************************/
364 static int Init( vout_thread_t *p_vout )
365 {
366     vout_sys_t *p_sys = p_vout->p_sys;
367     picture_t *p_pic;
368     int i_index;
369     video_format_t fmt;
370     logo_list_t *p_logo_list = p_sys->p_logo_list;
371
372     I_OUTPUTPICTURES = 0;
373     memset( &fmt, 0, sizeof(video_format_t) );
374
375     /* adjust index to the next logo */
376     p_logo_list->i_counter =
377                         ( p_logo_list->i_counter + 1 )%p_logo_list->i_count;
378
379     p_pic = p_logo_list->p_logo[p_logo_list->i_counter].p_pic;
380     /* Initialize the output structure */
381     p_vout->output.i_chroma = p_vout->render.i_chroma;
382     p_vout->output.i_width  = p_vout->render.i_width;
383     p_vout->output.i_height = p_vout->render.i_height;
384     p_vout->output.i_aspect = p_vout->render.i_aspect;
385     p_vout->fmt_out = p_vout->fmt_in;
386     fmt = p_vout->fmt_out;
387
388     /* Load the video blending filter */
389     p_sys->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
390     vlc_object_attach( p_sys->p_blend, p_vout );
391     p_sys->p_blend->fmt_out.video.i_x_offset =
392         p_sys->p_blend->fmt_out.video.i_y_offset = 0;
393     p_sys->p_blend->fmt_in.video.i_x_offset =
394         p_sys->p_blend->fmt_in.video.i_y_offset = 0;
395     p_sys->p_blend->fmt_out.video.i_aspect = p_vout->render.i_aspect;
396     p_sys->p_blend->fmt_out.video.i_chroma = p_vout->output.i_chroma;
397     p_sys->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','A');
398     p_sys->p_blend->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR;
399     p_sys->i_width =
400         p_sys->p_blend->fmt_in.video.i_width =
401             p_sys->p_blend->fmt_in.video.i_visible_width =
402                 p_pic ? p_pic->p[Y_PLANE].i_visible_pitch : 0;
403     p_sys->i_height =
404         p_sys->p_blend->fmt_in.video.i_height =
405             p_sys->p_blend->fmt_in.video.i_visible_height =
406                 p_pic ? p_pic->p[Y_PLANE].i_visible_lines : 0;
407     p_sys->p_blend->fmt_out.video.i_width =
408         p_sys->p_blend->fmt_out.video.i_visible_width =
409            p_vout->output.i_width;
410     p_sys->p_blend->fmt_out.video.i_height =
411         p_sys->p_blend->fmt_out.video.i_visible_height =
412             p_vout->output.i_height;
413
414     p_sys->p_blend->p_module =
415         module_Need( p_sys->p_blend, "video blending", 0, 0 );
416     if( !p_sys->p_blend->p_module )
417     {
418         msg_Err( p_vout, "can't open blending filter, aborting" );
419         vlc_object_detach( p_sys->p_blend );
420         vlc_object_release( p_sys->p_blend );
421         return VLC_EGENERIC;
422     }
423
424     if( p_sys->posx < 0 || p_sys->posy < 0 )
425     {
426         p_sys->posx = 0; p_sys->posy = 0;
427
428         if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
429         {
430             p_sys->posy = p_vout->render.i_height - p_sys->i_height;
431         }
432         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
433         {
434             p_sys->posy = p_vout->render.i_height / 2 - p_sys->i_height / 2;
435         }
436
437         if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
438         {
439             p_sys->posx = p_vout->render.i_width - p_sys->i_width;
440         }
441         else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
442         {
443             p_sys->posx = p_vout->render.i_width / 2 - p_sys->i_width / 2;
444         }
445     }
446     else
447     {
448         p_sys->pos = 0;
449     }
450
451     /* Try to open the real video output */
452     msg_Dbg( p_vout, "spawning the real video output" );
453
454     p_sys->p_vout = vout_Create( p_vout, &fmt );
455
456     /* Everything failed */
457     if( p_sys->p_vout == NULL )
458     {
459         msg_Err( p_vout, "can't open vout, aborting" );
460         return VLC_EGENERIC;
461     }
462
463     var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
464     var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
465
466     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
467     ADD_CALLBACKS( p_sys->p_vout, SendEvents );
468     ADD_PARENT_CALLBACKS( SendEventsToChild );
469
470     return VLC_SUCCESS;
471 }
472
473 /*****************************************************************************
474  * End: terminate logo video thread output method
475  *****************************************************************************/
476 static void End( vout_thread_t *p_vout )
477 {
478     vout_sys_t *p_sys = p_vout->p_sys;
479     int i_index;
480
481     /* Free the fake output buffers we allocated */
482     for( i_index = I_OUTPUTPICTURES ; i_index ; )
483     {
484         i_index--;
485         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
486     }
487
488     var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
489     var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
490
491     if( p_sys->p_vout )
492     {
493         DEL_CALLBACKS( p_sys->p_vout, SendEvents );
494         vlc_object_detach( p_sys->p_vout );
495         vout_Destroy( p_sys->p_vout );
496     }
497
498     if( p_sys->p_blend->p_module )
499         module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
500     vlc_object_detach( p_sys->p_blend );
501     vlc_object_release( p_sys->p_blend );
502 }
503
504 /*****************************************************************************
505  * Destroy: destroy logo video thread output method
506  *****************************************************************************/
507 static void Destroy( vlc_object_t *p_this )
508 {
509     vout_thread_t *p_vout = (vout_thread_t *)p_this;
510     vout_sys_t *p_sys = p_vout->p_sys;
511
512     DEL_PARENT_CALLBACKS( SendEventsToChild );
513
514     FreeLogoList( p_sys->p_logo_list );
515     free( p_sys->p_logo_list );
516
517     free( p_sys );
518 }
519
520 /*****************************************************************************
521  * Render: render the logo onto the video
522  *****************************************************************************/
523 static void Render( vout_thread_t *p_vout, picture_t *p_inpic )
524 {
525     vout_sys_t *p_sys = p_vout->p_sys;
526     picture_t *p_outpic;
527     picture_t *p_pic;
528     logo_list_t *p_logo_list;
529     logo_t * p_logo;
530
531     p_logo_list = p_sys->p_logo_list;
532
533     if( p_logo_list->i_next_pic < p_inpic->date )
534     {
535         /* It's time to use a new logo */
536         p_logo_list->i_counter =
537                         ( p_logo_list->i_counter + 1 )%p_logo_list->i_count;
538         p_logo = &p_logo_list->p_logo[p_sys->p_logo_list->i_counter];
539         p_pic = p_logo->p_pic;
540         p_logo_list->i_next_pic = p_inpic->date + ( p_logo->i_delay != -1 ?
541                               p_logo->i_delay : p_logo_list->i_delay ) * 1000;
542         if( p_pic )
543         {
544
545             p_sys->i_width =
546                 p_sys->p_blend->fmt_in.video.i_width =
547                     p_sys->p_blend->fmt_in.video.i_visible_width =
548                         p_pic->p[Y_PLANE].i_visible_pitch;
549             p_sys->i_height =
550                 p_sys->p_blend->fmt_in.video.i_height =
551                     p_sys->p_blend->fmt_in.video.i_visible_height =
552                         p_pic->p[Y_PLANE].i_visible_lines;
553
554             if( p_sys->pos )
555             {
556                 if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
557                 {
558                     p_sys->posy = p_vout->render.i_height - p_sys->i_height;
559                 }
560                 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
561                 {
562                     p_sys->posy = p_vout->render.i_height/2 - p_sys->i_height/2;
563                 }
564                 if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
565                 {
566                     p_sys->posx = p_vout->render.i_width - p_sys->i_width;
567                 }
568                 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
569                 {
570                     p_sys->posx = p_vout->render.i_width/2 - p_sys->i_width/2;
571                 }
572             }
573         }
574
575     }
576     else
577     {
578         p_logo = &p_logo_list->p_logo[p_sys->p_logo_list->i_counter];
579         p_pic = p_logo->p_pic;
580     }
581
582     /* This is a new frame. Get a structure from the video_output. */
583     while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
584     {
585         if( p_vout->b_die || p_vout->b_error ) return;
586         msleep( VOUT_OUTMEM_SLEEP );
587     }
588
589     vout_CopyPicture( p_vout, p_outpic, p_inpic );
590     vout_DatePicture( p_sys->p_vout, p_outpic, p_inpic->date );
591
592     if( p_pic )
593     p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic, p_outpic,
594                                     p_pic, p_sys->posx, p_sys->posy,
595                                     p_logo->i_alpha != -1 ? p_logo->i_alpha
596                                     : p_logo_list->i_alpha );
597
598     vout_DisplayPicture( p_sys->p_vout, p_outpic );
599 }
600
601 /*****************************************************************************
602  * SendEvents: forward mouse and keyboard events to the parent p_vout
603  *****************************************************************************/
604 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
605                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
606 {
607     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
608     var_Set( (vlc_object_t *)p_data, psz_var, newval );
609     return VLC_SUCCESS;
610 }
611
612 /*****************************************************************************
613  * MouseEvent: callback for mouse events
614  *****************************************************************************/
615 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
616                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
617 {
618     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
619     vout_thread_t *p_vout = (vout_thread_t*)p_data;
620     vout_sys_t *p_sys = p_vout->p_sys;
621     vlc_value_t valb;
622     int i_delta;
623
624     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
625
626     i_delta = newval.i_int - oldval.i_int;
627
628     if( (valb.i_int & 0x1) == 0 )
629     {
630         return VLC_SUCCESS;
631     }
632
633     if( psz_var[6] == 'x' )
634     {
635         vlc_value_t valy;
636         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
637         if( newval.i_int >= (int)p_sys->posx &&
638             valy.i_int >= (int)p_sys->posy &&
639             newval.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
640             valy.i_int <= (int)(p_sys->posy + p_sys->i_height) )
641         {
642             p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
643                           p_vout->output.i_width - p_sys->i_width );
644         }
645     }
646     else if( psz_var[6] == 'y' )
647     {
648         vlc_value_t valx;
649         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
650         if( valx.i_int >= (int)p_sys->posx &&
651             newval.i_int >= (int)p_sys->posy &&
652             valx.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
653             newval.i_int <= (int)(p_sys->posy + p_sys->i_height) )
654         {
655             p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
656                           p_vout->output.i_height - p_sys->i_height );
657         }
658     }
659
660     return VLC_SUCCESS;
661 }
662
663 /*****************************************************************************
664  * Control: control facility for the vout (forwards to child vout)
665  *****************************************************************************/
666 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
667 {
668     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
669 }
670
671 /*****************************************************************************
672  * SendEventsToChild: forward events to the child/children vout
673  *****************************************************************************/
674 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
675                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
676 {
677     VLC_UNUSED(p_data); VLC_UNUSED(oldval);
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     bool b_absolute;
693     mtime_t i_last_date;
694
695     /* On the fly control variable */
696     bool 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_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 = 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 = true;
842
843     p_sys->b_need_update = 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_region->i_align = OSD_ALIGN_RIGHT | OSD_ALIGN_TOP;
890         p_spu->b_absolute = true;
891     }
892     else
893     {   /* set to one of the 9 relative locations */
894         p_region->i_align = p_sys->pos;
895         p_spu->b_absolute = 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     VLC_UNUSED(oldval);
916     filter_sys_t *p_sys = (filter_sys_t *)p_data;
917     logo_list_t *p_logo_list = p_sys->p_logo_list;
918
919     if( !strncmp( psz_var, "logo-file", 6 ) )
920     {
921         vlc_mutex_lock( &p_logo_list->lock );
922         FreeLogoList( p_logo_list );
923         p_logo_list->psz_filename = strdup( newval.psz_string );
924         LoadLogoList( p_this, p_logo_list );
925         vlc_mutex_unlock( &p_logo_list->lock );
926         p_sys->b_need_update = true;
927     }
928     else if ( !strncmp( psz_var, "logo-x", 6 ) )
929     {
930         p_sys->posx = newval.i_int;
931     }
932     else if ( !strncmp( psz_var, "logo-y", 6 ) )
933     {
934         p_sys->posy = newval.i_int;
935     }
936     else if ( !strncmp( psz_var, "logo-position", 12 ) )
937     {
938         p_sys->pos = newval.i_int;
939     }
940     else if ( !strncmp( psz_var, "logo-transparency", 9 ) )
941     {
942         vlc_mutex_lock( &p_logo_list->lock );
943         p_logo_list->i_alpha = __MAX( __MIN( newval.i_int, 255 ), 0 );
944         vlc_mutex_unlock( &p_logo_list->lock );
945     }
946     else if ( !strncmp( psz_var, "logo-repeat", 11 ) )
947     {
948         vlc_mutex_lock( &p_logo_list->lock );
949         p_logo_list->i_repeat = newval.i_int;
950         vlc_mutex_unlock( &p_logo_list->lock );
951     }
952     p_sys->b_need_update = true;
953     return VLC_SUCCESS;
954 }