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