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