]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
* modules/video_filter/logo.c: fixed case when no transparent color is set.
[vlc] / modules / video_filter / logo.c
1 /*****************************************************************************
2  * logo.c : logo video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <png.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/vout.h>
35
36 #include "vlc_filter.h"
37 #include "filter_common.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44
45 static int  Init      ( vout_thread_t * );
46 static void End       ( vout_thread_t * );
47 static void Render    ( vout_thread_t *, picture_t * );
48
49 static int  SendEvents( vlc_object_t *, char const *,
50                         vlc_value_t, vlc_value_t, void * );
51 static int MouseEvent ( vlc_object_t *, char const *,
52                         vlc_value_t , vlc_value_t , void * );
53 static int Control    ( vout_thread_t *, int, va_list );
54
55 static int  CreateFilter ( vlc_object_t * );
56 static void DestroyFilter( vlc_object_t * );
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 #define FILE_TEXT N_("Logo filename")
62 #define FILE_LONGTEXT N_("Full path of the PNG file to use.")
63 #define POSX_TEXT N_("X coordinate of the logo")
64 #define POSX_LONGTEXT N_("You can move the logo by left-clicking on it." )
65 #define POSY_TEXT N_("Y coordinate of the logo")
66 #define POSY_LONGTEXT N_("You can move the logo by left-clicking on it." )
67 #define TRANS_TEXT N_("Transparency of the logo")
68 #define TRANS_LONGTEXT N_("You can set the logo transparency value here " \
69   "(from 0 for full transparency to 255 for full opacity)." )
70
71 vlc_module_begin();
72     set_description( _("Logo video filter") );
73     set_capability( "video filter", 0 );
74     add_shortcut( "logo" );
75     set_callbacks( Create, Destroy );
76
77     add_file( "logo-file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, VLC_FALSE );
78     add_integer( "logo-x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
79     add_integer( "logo-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
80     add_integer_with_range( "logo-transparency", 255, 0, 255, NULL,
81         TRANS_TEXT, TRANS_LONGTEXT, VLC_FALSE );
82
83     /* subpicture filter submodule */
84     add_submodule();
85     set_capability( "sub filter", 0 );
86     set_callbacks( CreateFilter, DestroyFilter );
87     set_description( _("Logo sub filter") );
88     add_shortcut( "logo" );
89 vlc_module_end();
90
91 /*****************************************************************************
92  * LoadPNG: loads the PNG logo into memory
93  *****************************************************************************/
94 static picture_t *LoadPNG( vlc_object_t *p_this )
95 {
96     picture_t *p_pic;
97     char *psz_filename;
98     vlc_value_t val;
99     FILE *file;
100     int i, j, i_trans;
101     vlc_bool_t b_alpha = VLC_TRUE;
102
103     png_uint_32 i_width, i_height;
104     int i_color_type, i_interlace_type, i_compression_type, i_filter_type;
105     int i_bit_depth;
106     png_bytep *p_row_pointers;
107     png_structp p_png;
108     png_infop p_info, p_end_info;
109
110     var_Create( p_this, "logo-file", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
111     var_Get( p_this, "logo-file", &val );
112     psz_filename = val.psz_string;
113     if( !psz_filename ) return 0;
114
115     if( !(file = fopen( psz_filename , "rb" )) )
116     {
117         msg_Err( p_this , "logo file (%s) not found", psz_filename );
118         free( psz_filename );
119         return 0;
120     }
121     free( psz_filename );
122
123     p_png = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
124     p_info = png_create_info_struct( p_png );
125     p_end_info = png_create_info_struct( p_png );
126     png_init_io( p_png, file );
127     png_read_info( p_png, p_info );
128     png_get_IHDR( p_png, p_info, &i_width, &i_height,
129                   &i_bit_depth, &i_color_type, &i_interlace_type,
130                   &i_compression_type, &i_filter_type);
131
132     if( i_color_type == PNG_COLOR_TYPE_PALETTE )
133         png_set_palette_to_rgb( p_png );
134
135     if( i_color_type == PNG_COLOR_TYPE_GRAY ||
136         i_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
137           png_set_gray_to_rgb( p_png );
138
139     if( png_get_valid( p_png, p_info, PNG_INFO_tRNS ) )
140     {
141         png_set_tRNS_to_alpha( p_png );
142     }
143     else
144     {
145         b_alpha = VLC_FALSE;
146     }
147
148     p_row_pointers = malloc( sizeof(png_bytep) * i_height );
149     for( i = 0; i < (int)i_height; i++ )
150     {
151         p_row_pointers[i] = malloc( 4 * ( i_bit_depth + 7 ) / 8 * i_width );
152     }
153     png_read_image( p_png, p_row_pointers );
154     png_read_end( p_png, p_end_info );
155
156     fclose( file );
157     png_destroy_read_struct( &p_png, &p_info, &p_end_info );
158
159     /* Convert to YUVA */
160     p_pic = malloc( sizeof(picture_t) );
161     if( vout_AllocatePicture( p_this, p_pic, VLC_FOURCC('Y','U','V','A'),
162                               i_width, i_height, VOUT_ASPECT_FACTOR ) !=
163         VLC_SUCCESS )
164     {
165         free( p_row_pointers );
166         return 0;
167     }
168
169     var_Create(p_this, "logo-transparency", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
170     var_Get( p_this, "logo-transparency", &val );
171     i_trans = val.i_int;
172
173     for( j = 0; j < (int)i_height ; j++ )
174     {
175         uint8_t *p = (uint8_t *)p_row_pointers[j];
176
177         for( i = 0; i < (int)i_width ; i++ )
178         {
179             int i_offset = i + j * p_pic->p[Y_PLANE].i_pitch;
180
181             p_pic->p[Y_PLANE].p_pixels[i_offset] =
182                 (p[0] * 257L + p[1] * 504 + p[2] * 98)/1000 + 16;
183             p_pic->p[U_PLANE].p_pixels[i_offset] =
184                 (p[2] * 439L - p[0] * 148 - p[1] * 291)/1000 + 128;
185             p_pic->p[V_PLANE].p_pixels[i_offset] =
186                 (p[0] * 439L - p[1] * 368 - p[2] * 71)/1000 + 128;
187             p_pic->p[A_PLANE].p_pixels[i_offset] =
188                 b_alpha ? (p[3] * i_trans) / 255 : 255;
189
190             p += (b_alpha ? 4 : 3);
191         }
192     }
193
194     free( p_row_pointers );
195     return p_pic;
196 }
197
198 /*****************************************************************************
199  * vout_sys_t: logo video output method descriptor
200  *****************************************************************************
201  * This structure is part of the video output thread descriptor.
202  * It describes the Invert specific properties of an output thread.
203  *****************************************************************************/
204 struct vout_sys_t
205 {
206     vout_thread_t *p_vout;
207
208     filter_t *p_blend;
209     picture_t *p_pic;
210
211     int i_width, i_height;
212     int posx, posy;
213 };
214
215 /*****************************************************************************
216  * Create: allocates logo video thread output method
217  *****************************************************************************/
218 static int Create( vlc_object_t *p_this )
219 {
220     vout_thread_t *p_vout = (vout_thread_t *)p_this;
221     vout_sys_t *p_sys;
222     vlc_value_t val;
223
224     /* Allocate structure */
225     p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
226     if( p_sys == NULL )
227     {
228         msg_Err( p_vout, "out of memory" );
229         return VLC_ENOMEM;
230     }
231
232     p_vout->pf_init = Init;
233     p_vout->pf_end = End;
234     p_vout->pf_manage = NULL;
235     p_vout->pf_render = Render;
236     p_vout->pf_display = NULL;
237     p_vout->pf_control = Control;
238
239     var_Create( p_this, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
240     var_Get( p_this, "logo-x", &val );
241     p_sys->posx = val.i_int;
242     var_Create( p_this, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
243     var_Get( p_this, "logo-y", &val );
244     p_sys->posy = val.i_int;
245
246     p_sys->p_pic = LoadPNG( p_this );
247     if( !p_sys->p_pic )
248     {
249         free( p_sys );
250         return VLC_EGENERIC;
251     }
252
253     p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
254     p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
255
256     return VLC_SUCCESS;
257 }
258
259 /*****************************************************************************
260  * Init: initialize logo video thread output method
261  *****************************************************************************/
262 static int Init( vout_thread_t *p_vout )
263 {
264     vout_sys_t *p_sys = p_vout->p_sys;
265     picture_t *p_pic;
266     int i_index;
267
268     I_OUTPUTPICTURES = 0;
269
270     /* Initialize the output structure */
271     p_vout->output.i_chroma = p_vout->render.i_chroma;
272     p_vout->output.i_width  = p_vout->render.i_width;
273     p_vout->output.i_height = p_vout->render.i_height;
274     p_vout->output.i_aspect = p_vout->render.i_aspect;
275
276     /* Load the video blending filter */
277     p_sys->p_blend = vlc_object_create( p_vout, sizeof(filter_t) );
278     vlc_object_attach( p_sys->p_blend, p_vout );
279     p_sys->p_blend->fmt_out.video.i_x_offset =
280         p_sys->p_blend->fmt_out.video.i_y_offset = 0;
281     p_sys->p_blend->fmt_in.video.i_x_offset =
282         p_sys->p_blend->fmt_in.video.i_y_offset = 0;
283     p_sys->p_blend->fmt_out.video.i_aspect = p_vout->render.i_aspect;
284     p_sys->p_blend->fmt_out.video.i_chroma = p_vout->output.i_chroma;
285     p_sys->p_blend->fmt_in.video.i_chroma = VLC_FOURCC('Y','U','V','A');
286     p_sys->p_blend->fmt_in.video.i_aspect = VOUT_ASPECT_FACTOR;
287     p_sys->p_blend->fmt_in.video.i_width =
288         p_sys->p_blend->fmt_in.video.i_visible_width =
289             p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
290     p_sys->p_blend->fmt_in.video.i_height =
291         p_sys->p_blend->fmt_in.video.i_visible_height =
292             p_sys->p_pic->p[Y_PLANE].i_visible_lines;
293     p_sys->p_blend->fmt_out.video.i_width =
294         p_sys->p_blend->fmt_out.video.i_visible_width =
295            p_vout->output.i_width;
296     p_sys->p_blend->fmt_out.video.i_height =
297         p_sys->p_blend->fmt_out.video.i_visible_height =
298             p_vout->output.i_height;
299
300     p_sys->p_blend->p_module =
301         module_Need( p_sys->p_blend, "video blending", 0, 0 );
302     if( !p_sys->p_blend->p_module )
303     {
304         msg_Err( p_vout, "can't open blending filter, aborting" );
305         vlc_object_detach( p_sys->p_blend );
306         vlc_object_destroy( p_sys->p_blend );
307         return VLC_EGENERIC;
308     }
309
310     /* Try to open the real video output */
311     msg_Dbg( p_vout, "spawning the real video output" );
312
313     p_sys->p_vout =
314         vout_Create( p_vout, p_vout->render.i_width, p_vout->render.i_height,
315                      p_vout->render.i_chroma, p_vout->render.i_aspect );
316
317     /* Everything failed */
318     if( p_sys->p_vout == NULL )
319     {
320         msg_Err( p_vout, "can't open vout, aborting" );
321         return VLC_EGENERIC;
322     }
323
324     var_AddCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
325     var_AddCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
326
327     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
328     ADD_CALLBACKS( p_sys->p_vout, SendEvents );
329     ADD_PARENT_CALLBACKS( SendEventsToChild );
330
331     return VLC_SUCCESS;
332 }
333
334 /*****************************************************************************
335  * End: terminate logo video thread output method
336  *****************************************************************************/
337 static void End( vout_thread_t *p_vout )
338 {
339     vout_sys_t *p_sys = p_vout->p_sys;
340     int i_index;
341
342     /* Free the fake output buffers we allocated */
343     for( i_index = I_OUTPUTPICTURES ; i_index ; )
344     {
345         i_index--;
346         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
347     }
348
349     var_DelCallback( p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
350     var_DelCallback( p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
351
352     if( p_sys->p_vout )
353     {
354         DEL_CALLBACKS( p_sys->p_vout, SendEvents );
355         vlc_object_detach( p_sys->p_vout );
356         vout_Destroy( p_sys->p_vout );
357     }
358
359     if( p_sys->p_blend->p_module )
360         module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
361     vlc_object_detach( p_sys->p_blend );
362     vlc_object_destroy( p_sys->p_blend );
363 }
364
365 /*****************************************************************************
366  * Destroy: destroy logo video thread output method
367  *****************************************************************************/
368 static void Destroy( vlc_object_t *p_this )
369 {
370     vout_thread_t *p_vout = (vout_thread_t *)p_this;
371     vout_sys_t *p_sys = p_vout->p_sys;
372
373     DEL_PARENT_CALLBACKS( SendEventsToChild );
374
375     if( p_sys->p_pic && p_sys->p_pic->p_data_orig )
376         free( p_sys->p_pic->p_data_orig );
377     if( p_sys->p_pic ) free( p_sys->p_pic );
378
379     free( p_sys );
380 }
381
382 /*****************************************************************************
383  * Render: render the logo onto the video
384  *****************************************************************************/
385 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
386 {
387     vout_sys_t *p_sys = p_vout->p_sys;
388     picture_t *p_outpic;
389
390     /* This is a new frame. Get a structure from the video_output. */
391     while( !(p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 )) )
392     {
393         if( p_vout->b_die || p_vout->b_error ) return;
394         msleep( VOUT_OUTMEM_SLEEP );
395     }
396
397     vout_CopyPicture( p_vout, p_outpic, p_pic );
398     vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
399
400     p_sys->p_blend->pf_video_blend( p_sys->p_blend, p_outpic, p_outpic,
401                                     p_sys->p_pic, p_sys->posx, p_sys->posy );
402
403     vout_DisplayPicture( p_sys->p_vout, p_outpic );
404 }
405
406 /*****************************************************************************
407  * SendEvents: forward mouse and keyboard events to the parent p_vout
408  *****************************************************************************/
409 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
410                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
411 {
412     var_Set( (vlc_object_t *)p_data, psz_var, newval );
413     return VLC_SUCCESS;
414 }
415
416 /*****************************************************************************
417  * MouseEvent: callback for mouse events
418  *****************************************************************************/
419 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
420                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
421 {
422     vout_thread_t *p_vout = (vout_thread_t*)p_data;
423     vout_sys_t *p_sys = p_vout->p_sys;
424     vlc_value_t valb;
425     int i_delta;
426
427     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
428
429     i_delta = newval.i_int - oldval.i_int;
430
431     if( (valb.i_int & 0x1) == 0 )
432     {
433         return VLC_SUCCESS;
434     }
435
436     if( psz_var[6] == 'x' )
437     {
438         vlc_value_t valy;
439         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
440         if( newval.i_int >= (int)p_sys->posx &&
441             valy.i_int >= (int)p_sys->posy &&
442             newval.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
443             valy.i_int <= (int)(p_sys->posy + p_sys->i_height) )
444         {
445             p_sys->posx = __MIN( __MAX( p_sys->posx + i_delta, 0 ),
446                           p_vout->output.i_width - p_sys->i_width );
447         }
448     }
449     else if( psz_var[6] == 'y' )
450     {
451         vlc_value_t valx;
452         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
453         if( valx.i_int >= (int)p_sys->posx &&
454             newval.i_int >= (int)p_sys->posy &&
455             valx.i_int <= (int)(p_sys->posx + p_sys->i_width) &&
456             newval.i_int <= (int)(p_sys->posy + p_sys->i_height) )
457         {
458             p_sys->posy = __MIN( __MAX( p_sys->posy + i_delta, 0 ),
459                           p_vout->output.i_height - p_sys->i_height );
460         }
461     }
462
463     return VLC_SUCCESS;
464 }
465
466 /*****************************************************************************
467  * Control: control facility for the vout (forwards to child vout)
468  *****************************************************************************/
469 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
470 {
471     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
472 }
473
474 /*****************************************************************************
475  * SendEventsToChild: forward events to the child/children vout
476  *****************************************************************************/
477 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
478                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
479 {
480     vout_thread_t *p_vout = (vout_thread_t *)p_this;
481     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
482     return VLC_SUCCESS;
483 }
484
485 /*****************************************************************************
486  * filter_sys_t: logo filter descriptor
487  *****************************************************************************/
488 struct filter_sys_t
489 {
490     picture_t *p_pic;
491
492     int i_width, i_height;
493     int posx, posy;
494
495     mtime_t i_last_date;
496 };
497
498 static subpicture_t *Filter( filter_t *, mtime_t );
499
500 /*****************************************************************************
501  * CreateFilter: allocates logo video filter
502  *****************************************************************************/
503 static int CreateFilter( vlc_object_t *p_this )
504 {
505     filter_t *p_filter = (filter_t *)p_this;
506     filter_sys_t *p_sys;
507     vlc_value_t val;
508
509     /* Allocate structure */
510     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
511     if( p_sys == NULL )
512     {
513         msg_Err( p_filter, "out of memory" );
514         return VLC_ENOMEM;
515     }
516
517     var_Create( p_this, "logo-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
518     var_Get( p_this, "logo-x", &val );
519     p_sys->posx = val.i_int;
520     var_Create( p_this, "logo-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
521     var_Get( p_this, "logo-y", &val );
522     p_sys->posy = val.i_int;
523
524     p_sys->p_pic = LoadPNG( p_this );
525     if( !p_sys->p_pic )
526     {
527         free( p_sys );
528         return VLC_EGENERIC;
529     }
530
531     p_sys->i_width = p_sys->p_pic->p[Y_PLANE].i_visible_pitch;
532     p_sys->i_height = p_sys->p_pic->p[Y_PLANE].i_visible_lines;
533     p_sys->i_last_date = 0;
534
535     /* Misc init */
536     p_filter->pf_sub_filter = Filter;
537
538     return VLC_SUCCESS;
539 }
540
541 /*****************************************************************************
542  * DestroyFilter: destroy logo video filter
543  *****************************************************************************/
544 static void DestroyFilter( vlc_object_t *p_this )
545 {
546     filter_t *p_filter = (filter_t *)p_this;
547     filter_sys_t *p_sys = p_filter->p_sys;
548
549     if( p_sys->p_pic && p_sys->p_pic->p_data_orig )
550         free( p_sys->p_pic->p_data_orig );
551     if( p_sys->p_pic ) free( p_sys->p_pic );
552
553     free( p_sys );
554 }
555
556 /****************************************************************************
557  * Filter: the whole thing
558  ****************************************************************************
559  * This function outputs subpictures at regular time intervals.
560  ****************************************************************************/
561 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
562 {
563     filter_sys_t *p_sys = p_filter->p_sys;
564     subpicture_t *p_spu;
565     subpicture_region_t *p_region;
566     video_format_t fmt;
567
568     if( p_sys->i_last_date && p_sys->i_last_date + 5000000 > date ) return 0;
569
570     /* Allocate the subpicture internal data. */
571     p_spu = p_filter->pf_sub_buffer_new( p_filter );
572     if( !p_spu ) return NULL;
573
574     /* Create new SPU region */
575     memset( &fmt, 0, sizeof(video_format_t) );
576     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
577     fmt.i_aspect = VOUT_ASPECT_FACTOR;
578     fmt.i_width = fmt.i_visible_width = p_sys->i_width;
579     fmt.i_height = fmt.i_visible_height = p_sys->i_height;
580     fmt.i_x_offset = fmt.i_y_offset = 0;
581     p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
582     if( !p_region )
583     {
584         msg_Err( p_filter, "cannot allocate SPU region" );
585         p_filter->pf_sub_buffer_del( p_filter, p_spu );
586         return NULL;
587     }
588
589     vout_CopyPicture( p_filter, &p_region->picture, p_sys->p_pic );
590     p_region->i_x = 0;
591     p_region->i_y = 0;
592     p_spu->i_x = p_sys->posx;
593     p_spu->i_y = p_sys->posy;
594     p_spu->p_region = p_region;
595
596     p_spu->i_start = p_sys->i_last_date = date;
597     p_spu->i_stop = 0;
598     p_spu->b_ephemer = VLC_TRUE;
599
600     return p_spu;
601 }