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