]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
* toolbox:
[vlc] / modules / video_filter / logo.c
1 /*****************************************************************************
2  * logo.c : logo video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5  * $Id: logo.c,v 1.6 2003/12/22 02:24:53 sam Exp $
6  *
7  * Authors: Simon Latapie <garf@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <png.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/vout.h>
34
35 #include "filter_common.h"
36
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  Create    ( vlc_object_t * );
42 static void Destroy   ( vlc_object_t * );
43
44 static int  Init      ( vout_thread_t * );
45 static void End       ( vout_thread_t * );
46 static void Render    ( vout_thread_t *, picture_t * );
47
48 static int  SendEvents( vlc_object_t *, char const *,
49                         vlc_value_t, vlc_value_t, void * );
50
51 static int MouseEvent( vlc_object_t *, char const *,
52                        vlc_value_t , vlc_value_t , void * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57
58 #define FILE_TEXT N_("Logo filename")
59 #define FILE_LONGTEXT N_("The file must be in PNG RGBA 8bits format (for now)")
60 #define POSX_TEXT N_("X coordinate of the logo")
61 #define POSX_LONGTEXT N_("You can move the logo by left-clicking on it" )
62 #define POSY_TEXT N_("Y coordinate of the logo")
63 #define POSY_LONGTEXT N_("You can move the logo by left-clicking on it" )
64 #define TRANS_TEXT N_("transparency of the logo")
65 #define TRANS_LONGTEXT N_("You can change it by middle-clicking and moving mouse left or right")
66
67 vlc_module_begin();
68     add_category_hint( N_("logo"), NULL, VLC_FALSE );
69     add_file( "logo_file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, VLC_FALSE );
70     add_integer( "logo_x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
71     add_integer( "logo_y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
72     add_float( "logo_transparency", 1, NULL, TRANS_TEXT, TRANS_LONGTEXT, VLC_FALSE );
73     set_description( _("logo video filter") );
74     set_capability( "video filter", 0 );
75     add_shortcut( "logo" );
76     set_callbacks( Create, Destroy );
77 vlc_module_end();
78
79 /*****************************************************************************
80  * vout_sys_t: logo video output method descriptor
81  *****************************************************************************
82  * This structure is part of the video output thread descriptor.
83  * It describes the Invert specific properties of an output thread.
84  *****************************************************************************/
85 struct vout_sys_t
86 {
87     vout_thread_t *p_vout;
88     png_uint_32 height;
89     int bit_depth;
90     png_uint_32 width;
91     uint8_t * png_image[3];
92     uint8_t * png_image_u;
93     uint8_t * png_image_v;
94     uint8_t * png_image_a[3];
95     uint8_t * png_image_a_little;
96     int error;
97     int posx, posy;
98     int trans;
99 };
100
101 /*****************************************************************************
102  * Create: allocates logo video thread output method
103  *****************************************************************************
104  * This function allocates and initializes a Invert vout method.
105  *****************************************************************************/
106 static int Create( vlc_object_t *p_this )
107 {
108     vout_thread_t *p_vout = (vout_thread_t *)p_this;
109
110     /* Allocate structure */
111     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
112     if( p_vout->p_sys == NULL )
113     {
114         msg_Err( p_vout, "out of memory" );
115         return VLC_ENOMEM;
116     }
117
118     p_vout->pf_init = Init;
119     p_vout->pf_end = End;
120     p_vout->pf_manage = NULL;
121     p_vout->pf_render = Render;
122     p_vout->pf_display = NULL;
123
124     return VLC_SUCCESS;
125 }
126
127 /*****************************************************************************
128  * Init: initialize logo video thread output method
129  *****************************************************************************/
130 static int Init( vout_thread_t *p_vout )
131 {
132     int i_index;
133     picture_t *p_pic;
134     char * filename;
135     FILE * fp;
136     int color_type;
137     int interlace_type;
138     int compression_type;
139     int filter_type;
140     png_structp png_ptr;
141     png_bytep * row_pointers;
142     png_infop info_ptr;
143     unsigned int i;
144 //    unsigned int j;
145     unsigned int x;
146     unsigned int y;
147     int temp;
148     int i_size;
149     int i_parity_width;
150     int i_parity_height;
151
152     /*  read png file  */
153     filename = config_GetPsz( p_vout, "logo_file" );
154     fp = fopen( filename , "rb");
155     png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL , NULL , NULL);
156     info_ptr = png_create_info_struct(png_ptr);
157
158     if (fp == NULL)
159     {
160         p_vout->p_sys->error=1;
161         msg_Err( p_vout , "file not found %s", filename );
162         free( filename );
163     }
164     else
165     {
166         free( filename );
167         p_vout->p_sys->error=0;
168         png_init_io(png_ptr, fp);
169         png_read_info(png_ptr, info_ptr);
170         png_get_IHDR(png_ptr, info_ptr, &p_vout->p_sys->width, &p_vout->p_sys->height,
171                      &p_vout->p_sys->bit_depth, &color_type, &interlace_type,
172                      &compression_type, &filter_type);
173         row_pointers= malloc( sizeof(png_bytep) * p_vout->p_sys->height );
174         for( i = 0; i < p_vout->p_sys->height; i++ )
175         {
176             row_pointers[i] = malloc( 4 * ( p_vout->p_sys->bit_depth + 7 ) / 8 * p_vout->p_sys->width );
177         }
178         png_read_image(png_ptr, row_pointers);
179         fclose(fp);
180         /* finish to read the image in the file. Now We have to convert it YUV */
181         /* initialize yuv plans of the image */
182         i_parity_width = p_vout->p_sys->width % 2;
183         i_parity_height = p_vout->p_sys->height % 2;
184
185         p_vout->p_sys->height = p_vout->p_sys->height
186                              + (p_vout->p_sys->height % 2);
187         p_vout->p_sys->width = p_vout->p_sys->width
188                             + (p_vout->p_sys->width % 2);
189         i_size = p_vout->p_sys->height * p_vout->p_sys->width;
190
191         p_vout->p_sys->png_image[0] = malloc( i_size );
192         p_vout->p_sys->png_image[1] = malloc( i_size / 4 );
193         p_vout->p_sys->png_image[2] = malloc( i_size / 4 );
194
195         p_vout->p_sys->png_image_a[0] = malloc( i_size );
196         p_vout->p_sys->png_image_a[1] = malloc( i_size / 4 );
197         p_vout->p_sys->png_image_a[2] = p_vout->p_sys->png_image_a[1];
198
199         for( y = 0; y < p_vout->p_sys->height ; y++)
200         {
201             for( x = 0; x < p_vout->p_sys->width ; x++)
202             {
203                 uint8_t (*p)[4];
204                 int idx;
205                 int idxc;
206
207                 /* FIXME FIXME */
208                 p = (void*)row_pointers[y];
209                 idx = x + y * p_vout->p_sys->width;
210                 idxc= x/2 + (y/2) * (p_vout->p_sys->width/2);
211
212                 if( ((i_parity_width == 0) || (x != (p_vout->p_sys->width - 1))) &&
213                     ((i_parity_height == 0) || (y != (p_vout->p_sys->height - 1))))
214                 {
215                     p_vout->p_sys->png_image_a[0][idx]= p[x][3];
216                     p_vout->p_sys->png_image[0][idx]= (p[x][0] * 257
217                                                      + p[x][1] * 504
218                                                      + p[x][2] * 98)/1000 + 16;
219
220                     if( ( x % 2 == 0 ) && ( y % 2 == 0 ) )
221                     {
222
223                         temp = (p[x][2] * 439
224                               - p[x][0] * 148
225                               - p[x][1] * 291)/1000 + 128;
226
227                         temp = __MAX( __MIN( temp, 255 ), 0 );
228                         p_vout->p_sys->png_image[1][idxc] = temp;
229
230                         temp = ( p[x][0] * 439
231                                - p[x][1] * 368
232                                - p[x][2] * 71)/1000 + 128;
233                         temp = __MAX( __MIN( temp, 255 ), 0 );
234                         p_vout->p_sys->png_image[2][idxc] = temp;
235                         p_vout->p_sys->png_image_a[1][idxc] = p_vout->p_sys->png_image_a[0][idx];
236
237                     }
238
239                 } else
240                 {
241                     p_vout->p_sys->png_image_a[0][idx]= 0;
242                 }
243             }
244         }
245         /* now we can free row_pointers*/
246         free(row_pointers);
247     }
248
249     I_OUTPUTPICTURES = 0;
250
251     /* Initialize the output structure */
252     p_vout->output.i_chroma = p_vout->render.i_chroma;
253     p_vout->output.i_width  = p_vout->render.i_width;
254     p_vout->output.i_height = p_vout->render.i_height;
255     p_vout->output.i_aspect = p_vout->render.i_aspect;
256
257     /* Try to open the real video output */
258     msg_Dbg( p_vout, "spawning the real video output" );
259
260     p_vout->p_sys->p_vout = vout_Create( p_vout,
261                            p_vout->render.i_width, p_vout->render.i_height,
262                            p_vout->render.i_chroma, p_vout->render.i_aspect );
263
264     /* Everything failed */
265     if( p_vout->p_sys->p_vout == NULL )
266     {
267         msg_Err( p_vout, "can't open vout, aborting" );
268
269         return VLC_EGENERIC;
270     }
271
272     var_AddCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
273     var_AddCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
274
275
276     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
277
278     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
279
280     ADD_PARENT_CALLBACKS( SendEventsToChild );
281
282     p_vout->p_sys->posx = config_GetInt( p_vout, "logo_x" );
283     p_vout->p_sys->posy = config_GetInt( p_vout, "logo_y" );
284     p_vout->p_sys->trans = (int)(config_GetFloat( p_vout, "logo_transparency" ) * 255);
285
286     return VLC_SUCCESS;
287 }
288
289 /*****************************************************************************
290  * End: terminate logo video thread output method
291  *****************************************************************************/
292 static void End( vout_thread_t *p_vout )
293 {
294     int i_index;
295
296     /* Free the fake output buffers we allocated */
297     for( i_index = I_OUTPUTPICTURES ; i_index ; )
298     {
299         i_index--;
300         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
301     }
302
303     var_DelCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
304     var_DelCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
305
306     DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
307     vlc_object_detach( p_vout->p_sys->p_vout );
308     vout_Destroy( p_vout->p_sys->p_vout );
309
310     config_PutInt( p_vout, "logo_x", p_vout->p_sys->posx );
311     config_PutInt( p_vout, "logo_y", p_vout->p_sys->posy );
312     config_PutFloat( p_vout, "logo_transparency", (float)(p_vout->p_sys->trans) / 255.0 );
313
314     if (p_vout->p_sys->error == 0)
315     {
316         free(p_vout->p_sys->png_image[0]);
317         free(p_vout->p_sys->png_image[1]);
318         free(p_vout->p_sys->png_image[2]);
319         free(p_vout->p_sys->png_image_a[0]);
320         free(p_vout->p_sys->png_image_a[1]);
321     }
322 }
323
324 /*****************************************************************************
325  * Destroy: destroy logo video thread output method
326  *****************************************************************************
327  * Terminate an output method created by InvertCreateOutputMethod
328  *****************************************************************************/
329 static void Destroy( vlc_object_t *p_this )
330 {
331     vout_thread_t *p_vout = (vout_thread_t *)p_this;
332
333     DEL_PARENT_CALLBACKS( SendEventsToChild );
334
335     free( p_vout->p_sys );
336 }
337
338 /*****************************************************************************
339  * Render: displays previously rendered output
340  *****************************************************************************
341  * This function send the currently rendered image to Invert image, waits
342  * until it is displayed and switch the two rendering buffers, preparing next
343  * frame.
344  *****************************************************************************/
345 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
346 {
347     picture_t *p_outpic;
348     int i_index;
349     int tr;
350
351     /* This is a new frame. Get a structure from the video_output. */
352     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
353               == NULL )
354     {
355         if( p_vout->b_die || p_vout->b_error )
356         {
357             return;
358         }
359         msleep( VOUT_OUTMEM_SLEEP );
360     }
361
362     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
363     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
364
365
366     tr = p_vout->p_sys->trans;
367
368     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
369     {
370         memcpy( p_outpic->p[i_index].p_pixels,
371                 p_pic->p[i_index].p_pixels,
372                 p_pic->p[i_index].i_lines * p_pic->p[i_index].i_pitch);
373
374
375         if (p_vout->p_sys->error == 0)
376         {
377             unsigned int i;
378             unsigned int j;
379             uint8_t *p_out, *p_in_a, *p_in;
380             int i_delta;
381             unsigned int i_max;
382             unsigned int j_max;
383
384             if (i_index == 0)
385             {
386                 p_out  = p_outpic->p[i_index].p_pixels +
387                             p_vout->p_sys->posy * p_outpic->p[i_index].i_pitch +
388                             p_vout->p_sys->posx;
389                 i_max = p_vout->p_sys->height;
390                 j_max = p_vout->p_sys->width;
391             } else
392             {
393                 p_out  = p_outpic->p[i_index].p_pixels +
394                          (p_vout->p_sys->posy/2)* p_outpic->p[i_index].i_pitch +
395                          p_vout->p_sys->posx / 2;
396                 i_max = p_vout->p_sys->height / 2;
397                 j_max = p_vout->p_sys->width / 2;
398             }
399             i_delta = p_outpic->p[i_index].i_pitch - j_max;
400
401             p_in_a = p_vout->p_sys->png_image_a[i_index];
402             p_in   = p_vout->p_sys->png_image[i_index];
403
404
405             for( i = 0; i < i_max ; i++ )
406             {
407                 for( j = 0 ; j < j_max ; j++)
408                 {
409                     *p_out = ( *p_out * ( 65025 - *p_in_a * tr) + *p_in * *p_in_a * tr) >> 16;
410                     p_out++;
411                     p_in++;
412                     p_in_a++;
413                 }
414                 p_out += i_delta;
415             }
416          }
417     }
418
419     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
420
421     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
422 }
423
424 /*****************************************************************************
425  * SendEvents: forward mouse and keyboard events to the parent p_vout
426  *****************************************************************************/
427 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
428                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
429 {
430     var_Set( (vlc_object_t *)p_data, psz_var, newval );
431
432     return VLC_SUCCESS;
433 }
434
435
436 /*****************************************************************************
437  * MouseEvent: callback for mouse events
438  ******************************************************************************/
439 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
440                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
441 {
442     vout_thread_t *p_vout = (vout_thread_t*)p_data;
443     vlc_value_t valb;
444     int i_delta;
445
446     #define posx p_vout->p_sys->posx
447     #define posy p_vout->p_sys->posy
448     #define width p_vout->p_sys->width
449     #define height p_vout->p_sys->height
450     #define trans p_vout->p_sys->trans
451
452     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
453
454     i_delta = newval.i_int - oldval.i_int;
455
456     if ((valb.i_int & 0x2) == 2 && psz_var[6] == 'x' )
457     {
458         trans = __MIN( __MAX( trans + i_delta , 0 ) , 255 );
459         return VLC_SUCCESS;
460     }
461     if ((valb.i_int & 0x1) == 0)
462     {
463         return VLC_SUCCESS;
464     }
465
466     if( psz_var[6] == 'x' )
467     {
468         vlc_value_t valy;
469         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
470         if ((newval.i_int >= (int)posx) && (valy.i_int >= (int)posy) && (newval.i_int <= (int)(posx + width)) && (valy.i_int <= (int)(posy + height)))
471         {
472             posx = __MIN( __MAX( posx + i_delta , 0 ) , p_vout->output.i_width - width );
473         }
474
475     }
476     else if( psz_var[6] == 'y' )
477     {
478         vlc_value_t valx;
479         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
480         if ((valx.i_int >= (int)posx) && (newval.i_int >= (int)posy) && (valx.i_int <= (int)(posx + width)) && (newval.i_int <= (int)(posy + height)))
481         {
482             posy = __MIN( __MAX( posy + i_delta , 0 ) , p_vout->output.i_height - height );
483         }
484
485     }
486     else if( psz_var[6] == 'c' )
487     {
488         if ((valb.i_int & 0x8) == 1)
489         {
490             p_vout->p_sys->trans++;
491         }
492         else if ((valb.i_int & 0x10) == 1)
493         {
494             p_vout->p_sys->trans--;
495         }
496     }
497
498     #undef posx
499     #undef posy
500     #undef width
501     #undef height
502     #undef trans
503
504     return VLC_SUCCESS;
505 }
506
507 /*****************************************************************************
508  * SendEventsToChild: forward events to the child/children vout
509  *****************************************************************************/
510 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
511                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
512 {
513     vout_thread_t *p_vout = (vout_thread_t *)p_this;
514     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
515     return VLC_SUCCESS;
516 }