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