]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
* stringreview
[vlc] / modules / video_filter / logo.c
1 /*****************************************************************************
2  * logo.c : logo video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id: logo.c,v 1.7 2004/01/25 03:28:41 hartman 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 (255-0)")
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_int_with_range( "logo_transparency", 255, 0, 255, 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 = config_GetInt( p_vout, "logo_transparency");
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
313     if (p_vout->p_sys->error == 0)
314     {
315         free(p_vout->p_sys->png_image[0]);
316         free(p_vout->p_sys->png_image[1]);
317         free(p_vout->p_sys->png_image[2]);
318         free(p_vout->p_sys->png_image_a[0]);
319         free(p_vout->p_sys->png_image_a[1]);
320     }
321 }
322
323 /*****************************************************************************
324  * Destroy: destroy logo video thread output method
325  *****************************************************************************
326  * Terminate an output method created by InvertCreateOutputMethod
327  *****************************************************************************/
328 static void Destroy( vlc_object_t *p_this )
329 {
330     vout_thread_t *p_vout = (vout_thread_t *)p_this;
331
332     DEL_PARENT_CALLBACKS( SendEventsToChild );
333
334     free( p_vout->p_sys );
335 }
336
337 /*****************************************************************************
338  * Render: displays previously rendered output
339  *****************************************************************************
340  * This function send the currently rendered image to Invert image, waits
341  * until it is displayed and switch the two rendering buffers, preparing next
342  * frame.
343  *****************************************************************************/
344 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
345 {
346     picture_t *p_outpic;
347     int i_index;
348     int tr;
349
350     /* This is a new frame. Get a structure from the video_output. */
351     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
352               == NULL )
353     {
354         if( p_vout->b_die || p_vout->b_error )
355         {
356             return;
357         }
358         msleep( VOUT_OUTMEM_SLEEP );
359     }
360
361     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
362     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
363
364
365     tr = p_vout->p_sys->trans;
366
367     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
368     {
369         memcpy( p_outpic->p[i_index].p_pixels,
370                 p_pic->p[i_index].p_pixels,
371                 p_pic->p[i_index].i_lines * p_pic->p[i_index].i_pitch);
372
373
374         if (p_vout->p_sys->error == 0)
375         {
376             unsigned int i;
377             unsigned int j;
378             uint8_t *p_out, *p_in_a, *p_in;
379             int i_delta;
380             unsigned int i_max;
381             unsigned int j_max;
382
383             if (i_index == 0)
384             {
385                 p_out  = p_outpic->p[i_index].p_pixels +
386                             p_vout->p_sys->posy * p_outpic->p[i_index].i_pitch +
387                             p_vout->p_sys->posx;
388                 i_max = p_vout->p_sys->height;
389                 j_max = p_vout->p_sys->width;
390             } else
391             {
392                 p_out  = p_outpic->p[i_index].p_pixels +
393                          ( p_vout->p_sys->posy  2 )* p_outpic->p[i_index].i_pitch +
394                          p_vout->p_sys->posx / 2;
395                 i_max = p_vout->p_sys->height / 2;
396                 j_max = p_vout->p_sys->width / 2;
397             }
398             i_delta = p_outpic->p[i_index].i_pitch - j_max;
399
400             p_in_a = p_vout->p_sys->png_image_a[i_index];
401             p_in   = p_vout->p_sys->png_image[i_index];
402
403
404             for( i = 0; i < i_max ; i++ )
405             {
406                 for( j = 0 ; j < j_max ; j++)
407                 {
408                     *p_out = ( *p_out * ( 65025 - *p_in_a * tr) + *p_in * *p_in_a * tr ) >> 16;
409                     p_out++;
410                     p_in++;
411                     p_in_a++;
412                 }
413                 p_out += i_delta;
414             }
415          }
416     }
417
418     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
419
420     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
421 }
422
423 /*****************************************************************************
424  * SendEvents: forward mouse and keyboard events to the parent p_vout
425  *****************************************************************************/
426 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
427                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
428 {
429     var_Set( (vlc_object_t *)p_data, psz_var, newval );
430
431     return VLC_SUCCESS;
432 }
433
434
435 /*****************************************************************************
436  * MouseEvent: callback for mouse events
437  ******************************************************************************/
438 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
439                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
440 {
441     vout_thread_t *p_vout = (vout_thread_t*)p_data;
442     vlc_value_t valb;
443     int i_delta;
444
445     #define posx p_vout->p_sys->posx
446     #define posy p_vout->p_sys->posy
447     #define width p_vout->p_sys->width
448     #define height p_vout->p_sys->height
449     #define trans p_vout->p_sys->trans
450
451     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
452
453     i_delta = newval.i_int - oldval.i_int;
454
455     if ((valb.i_int & 0x2) == 2 && psz_var[6] == 'x' )
456     {
457         trans = __MIN( __MAX( trans + i_delta , 0 ) , 255 );
458         return VLC_SUCCESS;
459     }
460     if ((valb.i_int & 0x1) == 0)
461     {
462         return VLC_SUCCESS;
463     }
464
465     if( psz_var[6] == 'x' )
466     {
467         vlc_value_t valy;
468         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
469         if ((newval.i_int >= (int)posx) && (valy.i_int >= (int)posy) && (newval.i_int <= (int)(posx + width)) && (valy.i_int <= (int)(posy + height)))
470         {
471             posx = __MIN( __MAX( posx + i_delta , 0 ) , p_vout->output.i_width - width );
472         }
473
474     }
475     else if( psz_var[6] == 'y' )
476     {
477         vlc_value_t valx;
478         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
479         if ((valx.i_int >= (int)posx) && (newval.i_int >= (int)posy) && (valx.i_int <= (int)(posx + width)) && (newval.i_int <= (int)(posy + height)))
480         {
481             posy = __MIN( __MAX( posy + i_delta , 0 ) , p_vout->output.i_height - height );
482         }
483
484     }
485     else if( psz_var[6] == 'c' )
486     {
487         if ((valb.i_int & 0x8) == 1)
488         {
489             p_vout->p_sys->trans++;
490         }
491         else if ((valb.i_int & 0x10) == 1)
492         {
493             p_vout->p_sys->trans--;
494         }
495     }
496
497     #undef posx
498     #undef posy
499     #undef width
500     #undef height
501     #undef trans
502
503     return VLC_SUCCESS;
504 }
505
506 /*****************************************************************************
507  * SendEventsToChild: forward events to the child/children vout
508  *****************************************************************************/
509 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
510                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
511 {
512     vout_thread_t *p_vout = (vout_thread_t *)p_this;
513     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
514     return VLC_SUCCESS;
515 }