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