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