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