]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[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_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_vout.h>
36 #include <assert.h>
37
38 #include "vlc_filter.h"
39 #include "filter_common.h"
40 #include "vlc_image.h"
41 #include "vlc_osd.h"
42
43 #ifdef LoadImage
44 #   undef LoadImage
45 #endif
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int  Create    ( vlc_object_t * );
51 static void Destroy   ( vlc_object_t * );
52
53 static int  Init      ( vout_thread_t * );
54 static void End       ( vout_thread_t * );
55 static void Render    ( vout_thread_t *, picture_t * );
56
57 static int  MouseEvent( vlc_object_t *, char const *,
58                         vlc_value_t , vlc_value_t , void * );
59 static int  Control   ( vout_thread_t *, int, va_list );
60
61 static int  CreateFilter ( vlc_object_t * );
62 static void DestroyFilter( vlc_object_t * );
63
64 static int LogoCallback( vlc_object_t *, char const *,
65                          vlc_value_t, vlc_value_t, void * );
66
67 /*****************************************************************************
68  * Module descriptor
69  *****************************************************************************/
70 #define FILE_TEXT N_("Logo filenames")
71 #define FILE_LONGTEXT N_("Full path of the image files to use. Format is " \
72 "<image>[,<delay in ms>[,<alpha>]][;<image>[,<delay>[,<alpha>]]][;...]. " \
73 "If you only have one file, simply enter its filename.")
74 #define REPEAT_TEXT N_("Logo animation # of loops")
75 #define REPEAT_LONGTEXT N_("Number of loops for the logo animation." \
76         "-1 = continuous, 0 = disabled")
77 #define DELAY_TEXT N_("Logo individual image time in ms")
78 #define DELAY_LONGTEXT N_("Individual image display time of 0 - 60000 ms.")
79
80 #define POSX_TEXT N_("X coordinate")
81 #define POSX_LONGTEXT N_("X coordinate of the logo. You can move the logo " \
82                 "by left-clicking it." )
83 #define POSY_TEXT N_("Y coordinate")
84 #define POSY_LONGTEXT N_("Y coordinate of the logo. You can move the logo " \
85                 "by left-clicking it." )
86 #define TRANS_TEXT N_("Transparency of the logo")
87 #define TRANS_LONGTEXT N_("Logo transparency value " \
88   "(from 0 for full transparency to 255 for full opacity)." )
89 #define POS_TEXT N_("Logo position")
90 #define POS_LONGTEXT N_( \
91   "Enforce the logo position on the video " \
92   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
93   "also use combinations of these values, eg 6 = top-right).")
94
95 #define CFG_PREFIX "logo-"
96
97 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
98 static const char *const ppsz_pos_descriptions[] =
99 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
100   N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
101
102 vlc_module_begin ()
103     set_capability( "sub filter", 0 )
104     set_callbacks( CreateFilter, DestroyFilter )
105     set_description( N_("Logo sub filter") )
106     set_shortname( N_("Logo overlay") )
107     set_category( CAT_VIDEO )
108     set_subcategory( SUBCAT_VIDEO_SUBPIC )
109     add_shortcut( "logo" )
110
111     add_file( CFG_PREFIX "file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, false )
112     add_integer( CFG_PREFIX "x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, true )
113     add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, true )
114     /* default to 1000 ms per image, continuously cycle through them */
115     add_integer( CFG_PREFIX "delay", 1000, NULL, DELAY_TEXT, DELAY_LONGTEXT, true )
116     add_integer( CFG_PREFIX "repeat", -1, NULL, REPEAT_TEXT, REPEAT_LONGTEXT, true )
117     add_integer_with_range( CFG_PREFIX "transparency", 255, 0, 255, NULL,
118         TRANS_TEXT, TRANS_LONGTEXT, false )
119     add_integer( CFG_PREFIX "position", -1, NULL, POS_TEXT, POS_LONGTEXT, false )
120         change_integer_list( pi_pos_values, ppsz_pos_descriptions, NULL )
121
122     /* video output filter submodule */
123     add_submodule ()
124     set_capability( "video filter", 0 )
125     set_callbacks( Create, Destroy )
126     set_description( N_("Logo video filter") )
127     add_shortcut( "logo" )
128 vlc_module_end ()
129
130 static const char *const 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             picture_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         return VLC_ENOMEM;
319     p_logo_list = p_sys->p_logo_list = malloc( sizeof( logo_list_t ) );
320     if( p_logo_list == NULL )
321     {
322         free( p_sys );
323         return VLC_ENOMEM;
324     }
325
326     config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
327                        p_vout->p_cfg );
328
329     p_logo_list->psz_filename = var_CreateGetStringCommand( p_vout,
330                                                             "logo-file" );
331     if( !p_logo_list->psz_filename || !*p_logo_list->psz_filename )
332     {
333         msg_Err( p_vout, "logo file not specified" );
334         free( p_logo_list->psz_filename );
335         free( p_sys );
336         return VLC_EGENERIC;
337     }
338
339     p_vout->pf_init = Init;
340     p_vout->pf_end = End;
341     p_vout->pf_manage = NULL;
342     p_vout->pf_render = Render;
343     p_vout->pf_display = NULL;
344     p_vout->pf_control = Control;
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 = var_CreateGetIntegerCommand( p_vout, "logo-delay" );
350     p_logo_list->i_delay = __MAX( __MIN( p_logo_list->i_delay, 60000 ), 0 );
351     p_logo_list->i_repeat = var_CreateGetIntegerCommand( p_vout, "logo-repeat");
352     p_logo_list->i_alpha = var_CreateGetIntegerCommand( p_vout,
353                                                         "logo-transparency" );
354     p_logo_list->i_alpha = __MAX( __MIN( p_logo_list->i_alpha, 255 ), 0 );
355
356     LoadLogoList( p_vout, p_logo_list );
357
358     return VLC_SUCCESS;
359 }
360
361 /*****************************************************************************
362  * Init: initialize logo video thread output method
363  *****************************************************************************/
364 static int Init( vout_thread_t *p_vout )
365 {
366     vout_sys_t *p_sys = p_vout->p_sys;
367     picture_t *p_pic;
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", NULL, false );
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_release( 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     vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
463
464     vout_filter_AddChild( p_vout, p_sys->p_vout, MouseEvent );
465
466     return VLC_SUCCESS;
467 }
468
469 /*****************************************************************************
470  * End: terminate logo video thread output method
471  *****************************************************************************/
472 static void End( vout_thread_t *p_vout )
473 {
474     vout_sys_t *p_sys = p_vout->p_sys;
475
476     vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
477     vout_CloseAndRelease( p_sys->p_vout );
478
479     vout_filter_ReleaseDirectBuffers( p_vout );
480
481     if( p_sys->p_blend->p_module )
482         module_unneed( p_sys->p_blend, p_sys->p_blend->p_module );
483     vlc_object_detach( p_sys->p_blend );
484     vlc_object_release( p_sys->p_blend );
485 }
486
487 /*****************************************************************************
488  * Destroy: destroy logo video thread output method
489  *****************************************************************************/
490 static void Destroy( vlc_object_t *p_this )
491 {
492     vout_thread_t *p_vout = (vout_thread_t *)p_this;
493     vout_sys_t *p_sys = p_vout->p_sys;
494
495
496     FreeLogoList( p_sys->p_logo_list );
497     free( p_sys->p_logo_list );
498
499     free( p_sys );
500 }
501
502 /*****************************************************************************
503  * Render: render the logo onto the video
504  *****************************************************************************/
505 static void Render( vout_thread_t *p_vout, picture_t *p_inpic )
506 {
507     vout_sys_t *p_sys = p_vout->p_sys;
508     picture_t *p_outpic;
509     picture_t *p_pic;
510     logo_list_t *p_logo_list;
511     logo_t * p_logo;
512
513     p_logo_list = p_sys->p_logo_list;
514
515     if( p_logo_list->i_next_pic < p_inpic->date )
516     {
517         /* It's time to use a new logo */
518         p_logo_list->i_counter =
519                         ( p_logo_list->i_counter + 1 )%p_logo_list->i_count;
520         p_logo = &p_logo_list->p_logo[p_sys->p_logo_list->i_counter];
521         p_pic = p_logo->p_pic;
522         p_logo_list->i_next_pic = p_inpic->date + ( p_logo->i_delay != -1 ?
523                               p_logo->i_delay : p_logo_list->i_delay ) * 1000;
524         if( p_pic )
525         {
526
527             p_sys->i_width =
528                 p_sys->p_blend->fmt_in.video.i_width =
529                     p_sys->p_blend->fmt_in.video.i_visible_width =
530                         p_pic->p[Y_PLANE].i_visible_pitch;
531             p_sys->i_height =
532                 p_sys->p_blend->fmt_in.video.i_height =
533                     p_sys->p_blend->fmt_in.video.i_visible_height =
534                         p_pic->p[Y_PLANE].i_visible_lines;
535
536             if( p_sys->pos )
537             {
538                 if( p_sys->pos & SUBPICTURE_ALIGN_BOTTOM )
539                 {
540                     p_sys->posy = p_vout->render.i_height - p_sys->i_height;
541                 }
542                 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_TOP) )
543                 {
544                     p_sys->posy = p_vout->render.i_height/2 - p_sys->i_height/2;
545                 }
546                 if( p_sys->pos & SUBPICTURE_ALIGN_RIGHT )
547                 {
548                     p_sys->posx = p_vout->render.i_width - p_sys->i_width;
549                 }
550                 else if ( !(p_sys->pos & SUBPICTURE_ALIGN_LEFT) )
551                 {
552                     p_sys->posx = p_vout->render.i_width/2 - p_sys->i_width/2;
553                 }
554             }
555         }
556
557     }
558     else
559     {
560         p_logo = &p_logo_list->p_logo[p_sys->p_logo_list->i_counter];
561         p_pic = p_logo->p_pic;
562     }
563
564     /* This is a new frame. Get a structure from the video_output. */
565     while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
566     {
567         if( !vlc_object_alive (p_vout) || p_vout->b_error ) return;
568         msleep( VOUT_OUTMEM_SLEEP );
569     }
570
571     picture_Copy( p_outpic, p_inpic );
572
573     if( p_pic )
574         p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic,
575                                         p_pic, p_sys->posx, p_sys->posy,
576                                         p_logo->i_alpha != -1 ? p_logo->i_alpha
577                                         : p_logo_list->i_alpha );
578
579     vout_DisplayPicture( p_sys->p_vout, p_outpic );
580 }
581
582 /*****************************************************************************
583  * MouseEvent: callback for mouse events
584  *****************************************************************************/
585 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
586                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
587 {
588     vout_thread_t *p_vout = p_data;
589     assert( p_this == VLC_OBJECT(p_vout->p_sys->p_vout) );
590     VLC_UNUSED(oldval);
591
592     vout_sys_t *p_sys = p_vout->p_sys;
593     const int i_delta = newval.i_int - oldval.i_int;
594     const int i_bdown = var_GetInteger( p_sys->p_vout, "mouse-button-down" );
595
596     if( (i_bdown & 0x1) == 0 )
597         goto forward;
598
599     int i_x, i_y;
600     int i_dx = 0;
601     int i_dy = 0;
602     if( psz_var[6] == 'x' )
603     {
604         i_y = var_GetInteger( p_sys->p_vout, "mouse-y" );
605         i_x = newval.i_int;
606         i_dx = i_delta;
607     }
608     else if( psz_var[6] == 'y' )
609     {
610         i_y = newval.i_int;
611         i_x = var_GetInteger( p_sys->p_vout, "mouse-x" );
612         i_dy = i_delta;
613     }
614     else
615     {
616         goto forward;
617     }
618
619     /* FIXME missing lock */
620     if( i_x < (int)p_sys->posx ||
621         i_y < (int)p_sys->posy ||
622         i_x > (int)(p_sys->posx + p_sys->i_width) ||
623         i_y > (int)(p_sys->posy + p_sys->i_height) )
624         goto forward;
625
626     p_sys->posx = __MIN( __MAX( p_sys->posx + i_dx, 0 ),
627                          p_vout->output.i_width - p_sys->i_width );
628     p_sys->posy = __MIN( __MAX( p_sys->posy + i_dy, 0 ),
629                          p_vout->output.i_height - p_sys->i_height );
630     return VLC_SUCCESS;
631
632 forward:
633     return var_Set( p_vout, psz_var, newval );
634 }
635
636 /*****************************************************************************
637  * Control: control facility for the vout (forwards to child vout)
638  *****************************************************************************/
639 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
640 {
641     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
642 }
643
644 /*****************************************************************************
645  * filter_sys_t: logo filter descriptor
646  *****************************************************************************/
647 struct filter_sys_t
648 {
649     logo_list_t *p_logo_list;
650
651     int pos, posx, posy;
652
653     bool b_absolute;
654     mtime_t i_last_date;
655
656     /* On the fly control variable */
657     bool b_need_update;
658 };
659
660 static subpicture_t *Filter( filter_t *, mtime_t );
661
662 /*****************************************************************************
663  * CreateFilter: allocates logo video filter
664  *****************************************************************************/
665 static int CreateFilter( vlc_object_t *p_this )
666 {
667     filter_t *p_filter = (filter_t *)p_this;
668     filter_sys_t *p_sys;
669     logo_list_t *p_logo_list;
670
671     /* Allocate structure */
672     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
673     if( p_sys == NULL )
674         return VLC_ENOMEM;
675     p_logo_list = p_sys->p_logo_list = malloc( sizeof( logo_list_t ) );
676     if( p_logo_list == NULL )
677     {
678         free( p_sys );
679         return VLC_ENOMEM;
680     }
681
682     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
683                        p_filter->p_cfg );
684
685     /* Hook used for callback variables */
686     p_logo_list->psz_filename =
687         var_CreateGetStringCommand( p_filter, "logo-file" );
688     if( !p_logo_list->psz_filename || !*p_logo_list->psz_filename )
689     {
690         msg_Err( p_this, "logo file not specified" );
691         free( p_sys );
692         free( p_logo_list );
693         return VLC_EGENERIC;
694     }
695
696     p_sys->posx = var_CreateGetIntegerCommand( p_filter, "logo-x" );
697     p_sys->posy = var_CreateGetIntegerCommand( p_filter, "logo-y" );
698     p_sys->pos = var_CreateGetIntegerCommand( p_filter, "logo-position" );
699     p_logo_list->i_alpha = var_CreateGetIntegerCommand( p_filter,
700                                                         "logo-transparency");
701     p_logo_list->i_alpha = __MAX( __MIN( p_logo_list->i_alpha, 255 ), 0 );
702     p_logo_list->i_delay =
703         var_CreateGetIntegerCommand( p_filter, "logo-delay" );
704     p_logo_list->i_repeat =
705         var_CreateGetIntegerCommand( p_filter, "logo-repeat" );
706
707     vlc_mutex_init( &p_logo_list->lock );
708     LoadLogoList( p_this, p_logo_list );
709
710     var_AddCallback( p_filter, "logo-file", LogoCallback, p_sys );
711     var_AddCallback( p_filter, "logo-x", LogoCallback, p_sys );
712     var_AddCallback( p_filter, "logo-y", LogoCallback, p_sys );
713     var_AddCallback( p_filter, "logo-position", LogoCallback, p_sys );
714     var_AddCallback( p_filter, "logo-transparency", LogoCallback, p_sys );
715     var_AddCallback( p_filter, "logo-repeat", LogoCallback, p_sys );
716
717     /* Misc init */
718     p_filter->pf_sub_filter = Filter;
719     p_sys->b_need_update = true;
720
721     p_sys->i_last_date = 0;
722
723     return VLC_SUCCESS;
724 }
725
726 /*****************************************************************************
727  * DestroyFilter: destroy logo video filter
728  *****************************************************************************/
729 static void DestroyFilter( vlc_object_t *p_this )
730 {
731     filter_t *p_filter = (filter_t *)p_this;
732     filter_sys_t *p_sys = p_filter->p_sys;
733
734     /* Delete the logo variables from INPUT */
735     var_Destroy( p_filter->p_libvlc, "logo-file" );
736     var_Destroy( p_filter->p_libvlc, "logo-x" );
737     var_Destroy( p_filter->p_libvlc, "logo-y" );
738     var_Destroy( p_filter->p_libvlc, "logo-delay" );
739     var_Destroy( p_filter->p_libvlc, "logo-repeat" );
740     var_Destroy( p_filter->p_libvlc, "logo-position" );
741     var_Destroy( p_filter->p_libvlc, "logo-transparency" );
742
743     vlc_mutex_destroy( &p_sys->p_logo_list->lock );
744     FreeLogoList( p_sys->p_logo_list );
745     free( p_sys->p_logo_list );
746     free( p_sys );
747 }
748
749 /*****************************************************************************
750  * Filter: the whole thing
751  *****************************************************************************
752  * This function outputs subpictures at regular time intervals.
753  *****************************************************************************/
754 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
755 {
756     filter_sys_t *p_sys = p_filter->p_sys;
757     logo_list_t *p_logo_list = p_sys->p_logo_list;
758     subpicture_t *p_spu;
759     subpicture_region_t *p_region;
760     video_format_t fmt;
761     picture_t *p_pic;
762     logo_t *p_logo;
763
764     vlc_mutex_lock( &p_logo_list->lock );
765     /* Basic test:  b_need_update occurs on a dynamic change,
766                     & i_next_pic is the general timer, when to
767                     look at updating the logo image */
768
769     if( ( ( !p_sys->b_need_update ) && ( p_logo_list->i_next_pic > date ) )
770         || !p_logo_list->i_repeat )
771     {
772         vlc_mutex_unlock( &p_logo_list->lock );
773         return 0;
774     }
775     /* prior code tested on && p_sys->i_last_date +5000000 > date ) return 0; */
776
777     /* adjust index to the next logo */
778     p_logo_list->i_counter =
779                         ( p_logo_list->i_counter + 1 )%p_logo_list->i_count;
780
781     p_logo = &p_logo_list->p_logo[p_logo_list->i_counter];
782     p_pic = p_logo->p_pic;
783
784     /* Allocate the subpicture internal data. */
785     p_spu = filter_NewSubpicture( p_filter );
786     if( !p_spu )
787     {
788         vlc_mutex_unlock( &p_logo_list->lock );
789         return NULL;
790     }
791
792     p_spu->b_absolute = p_sys->b_absolute;
793     p_spu->i_start = p_sys->i_last_date = date;
794     p_spu->i_stop = 0;
795     p_spu->b_ephemer = true;
796
797     p_sys->b_need_update = false;
798     p_logo_list->i_next_pic = date +
799     ( p_logo->i_delay != -1 ? p_logo->i_delay : p_logo_list->i_delay ) * 1000;
800
801     if( p_logo_list->i_repeat != -1
802         && p_logo_list->i_counter == 0 )
803     {
804         p_logo_list->i_repeat--;
805         if( p_logo_list->i_repeat == 0 )
806         {
807             vlc_mutex_unlock( &p_logo_list->lock );
808             return p_spu;
809         }
810     }
811
812     if( !p_pic || !p_logo->i_alpha
813         || ( p_logo->i_alpha == -1 && !p_logo_list->i_alpha ) )
814     {
815         /* Send an empty subpicture to clear the display */
816         vlc_mutex_unlock( &p_logo_list->lock );
817         return p_spu;
818     }
819
820     /* Create new SPU region */
821     memset( &fmt, 0, sizeof(video_format_t) );
822     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
823     fmt.i_aspect = VOUT_ASPECT_FACTOR;
824     fmt.i_sar_num = fmt.i_sar_den = 1;
825     fmt.i_width = fmt.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
826     fmt.i_height = fmt.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
827     fmt.i_x_offset = fmt.i_y_offset = 0;
828     p_region = subpicture_region_New( &fmt );
829     if( !p_region )
830     {
831         msg_Err( p_filter, "cannot allocate SPU region" );
832         p_filter->pf_sub_buffer_del( p_filter, p_spu );
833         vlc_mutex_unlock( &p_logo_list->lock );
834         return NULL;
835     }
836
837     /* FIXME the copy is probably not needed anymore */
838     picture_Copy( p_region->p_picture, p_pic );
839     vlc_mutex_unlock( &p_logo_list->lock );
840
841     /*  where to locate the logo: */
842     if( p_sys->pos < 0 )
843     {   /*  set to an absolute xy */
844         p_region->i_align = OSD_ALIGN_RIGHT | OSD_ALIGN_TOP;
845         p_spu->b_absolute = true;
846     }
847     else
848     {   /* set to one of the 9 relative locations */
849         p_region->i_align = p_sys->pos;
850         p_spu->b_absolute = false;
851     }
852
853     p_region->i_x = p_sys->posx;
854     p_region->i_y = p_sys->posy;
855
856     p_spu->p_region = p_region;
857
858     p_spu->i_alpha = ( p_logo->i_alpha != -1 ?
859                        p_logo->i_alpha : p_logo_list->i_alpha );
860
861     return p_spu;
862 }
863
864 /*****************************************************************************
865  * Callback to update params on the fly
866  *****************************************************************************/
867 static int LogoCallback( vlc_object_t *p_this, char const *psz_var,
868                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
869 {
870     VLC_UNUSED(oldval);
871     filter_sys_t *p_sys = (filter_sys_t *)p_data;
872     logo_list_t *p_logo_list = p_sys->p_logo_list;
873
874     if( !strcmp( psz_var, "logo-file" ) )
875     {
876         vlc_mutex_lock( &p_logo_list->lock );
877         FreeLogoList( p_logo_list );
878         p_logo_list->psz_filename = strdup( newval.psz_string );
879         LoadLogoList( p_this, p_logo_list );
880         vlc_mutex_unlock( &p_logo_list->lock );
881     }
882     else if ( !strcmp( psz_var, "logo-x" ) )
883     {
884         p_sys->posx = newval.i_int;
885     }
886     else if ( !strcmp( psz_var, "logo-y" ) )
887     {
888         p_sys->posy = newval.i_int;
889     }
890     else if ( !strcmp( psz_var, "logo-position" ) )
891     {
892         p_sys->pos = newval.i_int;
893     }
894     else if ( !strcmp( psz_var, "logo-transparency" ) )
895     {
896         vlc_mutex_lock( &p_logo_list->lock );
897         p_logo_list->i_alpha = __MAX( __MIN( newval.i_int, 255 ), 0 );
898         vlc_mutex_unlock( &p_logo_list->lock );
899     }
900     else if ( !strcmp( psz_var, "logo-repeat" ) )
901     {
902         vlc_mutex_lock( &p_logo_list->lock );
903         p_logo_list->i_repeat = newval.i_int;
904         vlc_mutex_unlock( &p_logo_list->lock );
905     }
906     p_sys->b_need_update = true;
907     return VLC_SUCCESS;
908 }