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