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