]> 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.10 2004/01/25 20:05:28 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     set_description( _("Logo video filter") );
69     set_capability( "video filter", 0 );
70
71     add_file( "logo-file", NULL, NULL, FILE_TEXT, FILE_LONGTEXT, VLC_FALSE );
72     add_integer( "logo-x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
73     add_integer( "logo-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
74     add_integer_with_range( "logo-transparency", 255, 0, 255, NULL,
75         TRANS_TEXT, TRANS_LONGTEXT, VLC_FALSE );
76
77     add_shortcut( "logo" );
78     set_callbacks( Create, Destroy );
79 vlc_module_end();
80
81 /*****************************************************************************
82  * vout_sys_t: logo video output method descriptor
83  *****************************************************************************
84  * This structure is part of the video output thread descriptor.
85  * It describes the Invert specific properties of an output thread.
86  *****************************************************************************/
87 struct vout_sys_t
88 {
89     vout_thread_t *p_vout;
90     png_uint_32 height;
91     int bit_depth;
92     png_uint_32 width;
93     uint8_t * png_image[3];
94     uint8_t * png_image_u;
95     uint8_t * png_image_v;
96     uint8_t * png_image_a[3];
97     uint8_t * png_image_a_little;
98     int error;
99     int posx, posy;
100     int trans;
101 };
102
103 /*****************************************************************************
104  * Create: allocates logo video thread output method
105  *****************************************************************************
106  * This function allocates and initializes a Invert vout method.
107  *****************************************************************************/
108 static int Create( vlc_object_t *p_this )
109 {
110     vout_thread_t *p_vout = (vout_thread_t *)p_this;
111
112     /* Allocate structure */
113     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
114     if( p_vout->p_sys == NULL )
115     {
116         msg_Err( p_vout, "out of memory" );
117         return VLC_ENOMEM;
118     }
119
120     p_vout->pf_init = Init;
121     p_vout->pf_end = End;
122     p_vout->pf_manage = NULL;
123     p_vout->pf_render = Render;
124     p_vout->pf_display = NULL;
125
126     return VLC_SUCCESS;
127 }
128
129 /*****************************************************************************
130  * Init: initialize logo video thread output method
131  *****************************************************************************/
132 static int Init( vout_thread_t *p_vout )
133 {
134     int i_index;
135     picture_t *p_pic;
136     char * filename;
137     FILE * fp;
138     int color_type;
139     int interlace_type;
140     int compression_type;
141     int filter_type;
142     png_structp png_ptr;
143     png_bytep * row_pointers;
144     png_infop info_ptr;
145     unsigned int i;
146 //    unsigned int j;
147     unsigned int x;
148     unsigned int y;
149     int temp;
150     int i_size;
151     int i_parity_width;
152     int i_parity_height;
153
154     /*  read png file  */
155     filename = config_GetPsz( p_vout, "logo-file" );
156     fp = fopen( filename , "rb");
157     png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL , NULL , NULL);
158     info_ptr = png_create_info_struct(png_ptr);
159
160     if (fp == NULL)
161     {
162         p_vout->p_sys->error=1;
163         msg_Err( p_vout , "file not found %s", filename );
164         free( filename );
165     }
166     else
167     {
168         free( filename );
169         p_vout->p_sys->error=0;
170         png_init_io(png_ptr, fp);
171         png_read_info(png_ptr, info_ptr);
172         png_get_IHDR(png_ptr, info_ptr, &p_vout->p_sys->width, &p_vout->p_sys->height,
173                      &p_vout->p_sys->bit_depth, &color_type, &interlace_type,
174                      &compression_type, &filter_type);
175         row_pointers= malloc( sizeof(png_bytep) * p_vout->p_sys->height );
176         for( i = 0; i < p_vout->p_sys->height; i++ )
177         {
178             row_pointers[i] = malloc( 4 * ( p_vout->p_sys->bit_depth + 7 ) / 8 * p_vout->p_sys->width );
179         }
180         png_read_image(png_ptr, row_pointers);
181         fclose(fp);
182         /* finish to read the image in the file. Now We have to convert it YUV */
183         /* initialize yuv plans of the image */
184         i_parity_width = p_vout->p_sys->width % 2;
185         i_parity_height = p_vout->p_sys->height % 2;
186
187         p_vout->p_sys->height = p_vout->p_sys->height
188                              + (p_vout->p_sys->height % 2);
189         p_vout->p_sys->width = p_vout->p_sys->width
190                             + (p_vout->p_sys->width % 2);
191         i_size = p_vout->p_sys->height * p_vout->p_sys->width;
192
193         p_vout->p_sys->png_image[0] = malloc( i_size );
194         p_vout->p_sys->png_image[1] = malloc( i_size / 4 );
195         p_vout->p_sys->png_image[2] = malloc( i_size / 4 );
196
197         p_vout->p_sys->png_image_a[0] = malloc( i_size );
198         p_vout->p_sys->png_image_a[1] = malloc( i_size / 4 );
199         p_vout->p_sys->png_image_a[2] = p_vout->p_sys->png_image_a[1];
200
201         for( y = 0; y < p_vout->p_sys->height ; y++)
202         {
203             for( x = 0; x < p_vout->p_sys->width ; x++)
204             {
205                 uint8_t (*p)[4];
206                 int idx;
207                 int idxc;
208
209                 /* FIXME FIXME */
210                 p = (void*)row_pointers[y];
211                 idx = x + y * p_vout->p_sys->width;
212                 idxc= x/2 + (y/2) * (p_vout->p_sys->width/2);
213
214                 if( ((i_parity_width == 0) || (x != (p_vout->p_sys->width - 1))) &&
215                     ((i_parity_height == 0) || (y != (p_vout->p_sys->height - 1))))
216                 {
217                     p_vout->p_sys->png_image_a[0][idx]= p[x][3];
218                     p_vout->p_sys->png_image[0][idx]= (p[x][0] * 257
219                                                      + p[x][1] * 504
220                                                      + p[x][2] * 98)/1000 + 16;
221
222                     if( ( x % 2 == 0 ) && ( y % 2 == 0 ) )
223                     {
224
225                         temp = (p[x][2] * 439
226                               - p[x][0] * 148
227                               - p[x][1] * 291)/1000 + 128;
228
229                         temp = __MAX( __MIN( temp, 255 ), 0 );
230                         p_vout->p_sys->png_image[1][idxc] = temp;
231
232                         temp = ( p[x][0] * 439
233                                - p[x][1] * 368
234                                - p[x][2] * 71)/1000 + 128;
235                         temp = __MAX( __MIN( temp, 255 ), 0 );
236                         p_vout->p_sys->png_image[2][idxc] = temp;
237                         p_vout->p_sys->png_image_a[1][idxc] = p_vout->p_sys->png_image_a[0][idx];
238
239                     }
240
241                 } else
242                 {
243                     p_vout->p_sys->png_image_a[0][idx]= 0;
244                 }
245             }
246         }
247         /* now we can free row_pointers*/
248         free(row_pointers);
249     }
250
251     I_OUTPUTPICTURES = 0;
252
253     /* Initialize the output structure */
254     p_vout->output.i_chroma = p_vout->render.i_chroma;
255     p_vout->output.i_width  = p_vout->render.i_width;
256     p_vout->output.i_height = p_vout->render.i_height;
257     p_vout->output.i_aspect = p_vout->render.i_aspect;
258
259     /* Try to open the real video output */
260     msg_Dbg( p_vout, "spawning the real video output" );
261
262     p_vout->p_sys->p_vout = vout_Create( p_vout,
263                            p_vout->render.i_width, p_vout->render.i_height,
264                            p_vout->render.i_chroma, p_vout->render.i_aspect );
265
266     /* Everything failed */
267     if( p_vout->p_sys->p_vout == NULL )
268     {
269         msg_Err( p_vout, "can't open vout, aborting" );
270
271         return VLC_EGENERIC;
272     }
273
274     var_AddCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
275     var_AddCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
276
277
278     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
279
280     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
281
282     ADD_PARENT_CALLBACKS( SendEventsToChild );
283
284     p_vout->p_sys->posx = config_GetInt( p_vout, "logo-x" );
285     p_vout->p_sys->posy = config_GetInt( p_vout, "logo-y" );
286     p_vout->p_sys->trans = config_GetInt( p_vout, "logo-transparency");
287
288     return VLC_SUCCESS;
289 }
290
291 /*****************************************************************************
292  * End: terminate logo video thread output method
293  *****************************************************************************/
294 static void End( vout_thread_t *p_vout )
295 {
296     int i_index;
297
298     /* Free the fake output buffers we allocated */
299     for( i_index = I_OUTPUTPICTURES ; i_index ; )
300     {
301         i_index--;
302         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
303     }
304
305     var_DelCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
306     var_DelCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
307
308     DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
309     vlc_object_detach( p_vout->p_sys->p_vout );
310     vout_Destroy( p_vout->p_sys->p_vout );
311
312     config_PutInt( p_vout, "logo-x", p_vout->p_sys->posx );
313     config_PutInt( p_vout, "logo-y", p_vout->p_sys->posy );
314
315     if (p_vout->p_sys->error == 0)
316     {
317         free(p_vout->p_sys->png_image[0]);
318         free(p_vout->p_sys->png_image[1]);
319         free(p_vout->p_sys->png_image[2]);
320         free(p_vout->p_sys->png_image_a[0]);
321         free(p_vout->p_sys->png_image_a[1]);
322     }
323 }
324
325 /*****************************************************************************
326  * Destroy: destroy logo video thread output method
327  *****************************************************************************
328  * Terminate an output method created by InvertCreateOutputMethod
329  *****************************************************************************/
330 static void Destroy( vlc_object_t *p_this )
331 {
332     vout_thread_t *p_vout = (vout_thread_t *)p_this;
333
334     DEL_PARENT_CALLBACKS( SendEventsToChild );
335
336     free( p_vout->p_sys );
337 }
338
339 /*****************************************************************************
340  * Render: displays previously rendered output
341  *****************************************************************************
342  * This function send the currently rendered image to Invert image, waits
343  * until it is displayed and switch the two rendering buffers, preparing next
344  * frame.
345  *****************************************************************************/
346 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
347 {
348     picture_t *p_outpic;
349     int i_index;
350     int tr;
351
352     /* This is a new frame. Get a structure from the video_output. */
353     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
354               == NULL )
355     {
356         if( p_vout->b_die || p_vout->b_error )
357         {
358             return;
359         }
360         msleep( VOUT_OUTMEM_SLEEP );
361     }
362
363     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
364     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
365
366
367     tr = p_vout->p_sys->trans;
368
369     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
370     {
371         memcpy( p_outpic->p[i_index].p_pixels,
372                 p_pic->p[i_index].p_pixels,
373                 p_pic->p[i_index].i_lines * p_pic->p[i_index].i_pitch);
374
375
376         if (p_vout->p_sys->error == 0)
377         {
378             unsigned int i;
379             unsigned int j;
380             uint8_t *p_out, *p_in_a, *p_in;
381             int i_delta;
382             unsigned int i_max;
383             unsigned int j_max;
384
385             if (i_index == 0)
386             {
387                 p_out  = p_outpic->p[i_index].p_pixels +
388                             p_vout->p_sys->posy * p_outpic->p[i_index].i_pitch +
389                             p_vout->p_sys->posx;
390                 i_max = p_vout->p_sys->height;
391                 j_max = p_vout->p_sys->width;
392             } else
393             {
394                 p_out  = p_outpic->p[i_index].p_pixels +
395                             p_vout->p_sys->posy / 2 * p_outpic->p[i_index].i_pitch +
396                          p_vout->p_sys->posx / 2;
397                 i_max = p_vout->p_sys->height / 2;
398                 j_max = p_vout->p_sys->width / 2;
399             }
400             i_delta = p_outpic->p[i_index].i_pitch - j_max;
401
402             p_in_a = p_vout->p_sys->png_image_a[i_index];
403             p_in   = p_vout->p_sys->png_image[i_index];
404
405
406             for( i = 0; i < i_max ; i++ )
407             {
408                 for( j = 0 ; j < j_max ; j++)
409                 {
410                     *p_out = ( *p_out * ( 65025 - *p_in_a * tr) + *p_in * *p_in_a * tr ) >> 16;
411                     p_out++;
412                     p_in++;
413                     p_in_a++;
414                 }
415                 p_out += i_delta;
416             }
417          }
418     }
419
420     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
421
422     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
423 }
424
425 /*****************************************************************************
426  * SendEvents: forward mouse and keyboard events to the parent p_vout
427  *****************************************************************************/
428 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
429                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
430 {
431     var_Set( (vlc_object_t *)p_data, psz_var, newval );
432
433     return VLC_SUCCESS;
434 }
435
436
437 /*****************************************************************************
438  * MouseEvent: callback for mouse events
439  ******************************************************************************/
440 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
441                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
442 {
443     vout_thread_t *p_vout = (vout_thread_t*)p_data;
444     vlc_value_t valb;
445     int i_delta;
446
447     #define posx p_vout->p_sys->posx
448     #define posy p_vout->p_sys->posy
449     #define width p_vout->p_sys->width
450     #define height p_vout->p_sys->height
451     #define trans p_vout->p_sys->trans
452
453     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
454
455     i_delta = newval.i_int - oldval.i_int;
456
457     if ((valb.i_int & 0x2) == 2 && psz_var[6] == 'x' )
458     {
459         trans = __MIN( __MAX( trans + i_delta , 0 ) , 255 );
460         return VLC_SUCCESS;
461     }
462     if ((valb.i_int & 0x1) == 0)
463     {
464         return VLC_SUCCESS;
465     }
466
467     if( psz_var[6] == 'x' )
468     {
469         vlc_value_t valy;
470         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
471         if ((newval.i_int >= (int)posx) && (valy.i_int >= (int)posy) && (newval.i_int <= (int)(posx + width)) && (valy.i_int <= (int)(posy + height)))
472         {
473             posx = __MIN( __MAX( posx + i_delta , 0 ) , p_vout->output.i_width - width );
474         }
475
476     }
477     else if( psz_var[6] == 'y' )
478     {
479         vlc_value_t valx;
480         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
481         if ((valx.i_int >= (int)posx) && (newval.i_int >= (int)posy) && (valx.i_int <= (int)(posx + width)) && (newval.i_int <= (int)(posy + height)))
482         {
483             posy = __MIN( __MAX( posy + i_delta , 0 ) , p_vout->output.i_height - height );
484         }
485
486     }
487     else if( psz_var[6] == 'c' )
488     {
489         if ((valb.i_int & 0x8) == 1)
490         {
491             p_vout->p_sys->trans++;
492         }
493         else if ((valb.i_int & 0x10) == 1)
494         {
495             p_vout->p_sys->trans--;
496         }
497     }
498
499     #undef posx
500     #undef posy
501     #undef width
502     #undef height
503     #undef trans
504
505     return VLC_SUCCESS;
506 }
507
508 /*****************************************************************************
509  * SendEventsToChild: forward events to the child/children vout
510  *****************************************************************************/
511 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
512                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
513 {
514     vout_thread_t *p_vout = (vout_thread_t *)p_this;
515     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
516     return VLC_SUCCESS;
517 }