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