]> git.sesse.net Git - vlc/blob - modules/video_filter/logo.c
logo.c : gcc != 3 compile fix
[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.2 2003/07/04 19:00:43 titer 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 File")
59 #define FILE_LONGTEXT N_("It must be a PNG in RGBA 8bits (for now)")
60 #define POSX_TEXT N_("x postion of the logo")
61 #define POSX_LONGTEXT N_("You can move the logo by left-clicking on it" )
62 #define POSY_TEXT N_("y position 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     int temp;
146     int i_size;
147
148     /*  read png file  */
149     filename = config_GetPsz( p_vout, "logo_file" );
150     fp = fopen( filename , "rb");
151     png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL , NULL , NULL);
152     info_ptr = png_create_info_struct(png_ptr);
153     
154     if (fp == NULL)
155     {
156         p_vout->p_sys->error=1;
157         msg_Err( p_vout , "file not found %s", filename );
158         free( filename );
159     } else
160     {
161         free( filename );
162         p_vout->p_sys->error=0;
163         png_init_io(png_ptr, fp);
164         png_read_info(png_ptr, info_ptr);
165         png_get_IHDR(png_ptr, info_ptr, &p_vout->p_sys->width, &p_vout->p_sys->height,
166                      &p_vout->p_sys->bit_depth, &color_type, &interlace_type,
167                      &compression_type, &filter_type);
168         row_pointers= malloc( sizeof(png_bytep) * p_vout->p_sys->height );
169         for( i = 0; i < p_vout->p_sys->height; i++ )
170         {
171             row_pointers[i] = malloc( 4 * ( p_vout->p_sys->bit_depth + 7 ) / 8 * p_vout->p_sys->width );
172         }
173         png_read_image(png_ptr, row_pointers);
174         fclose(fp);
175         /* finish to read the image in the file. Now We have to convert it YUV */
176         /* initialize yuv plans of the image */
177         i_size = p_vout->p_sys->height * p_vout->p_sys->width;
178
179         p_vout->p_sys->png_image[0] = malloc( i_size );
180         p_vout->p_sys->png_image[1] = malloc( i_size / 4 );
181         p_vout->p_sys->png_image[2] = malloc( i_size / 4 );
182
183         p_vout->p_sys->png_image_a[0] = malloc( i_size );
184         p_vout->p_sys->png_image_a[1] = malloc( i_size / 4 );
185         p_vout->p_sys->png_image_a[2] = p_vout->p_sys->png_image_a[1];
186
187         /* conversion */
188         j = 0;
189         for( i= 0; i < p_vout->p_sys->height * p_vout->p_sys->width ; i++)
190         {
191             uint8_t (*p)[4];
192             int x;
193             int y;
194             
195             x = i % p_vout->p_sys->width;
196             y = i / p_vout->p_sys->width;
197
198             /* FIXME FIXME */
199             p = (void*)row_pointers[y];
200
201             p_vout->p_sys->png_image_a[0][i]= p[x][3];        
202             p_vout->p_sys->png_image[0][i]= (p[x][0] * 257
203                                            + p[x][1] * 504
204                                            + p[x][2] * 98)/1000 + 16;
205
206             if( ( x % 2 == 0 ) && ( y % 2 == 0 ) )
207             {
208                 temp = (p[x][2] * 439
209                       - p[x][0] * 148
210                       - p[x][1] * 291)/1000 + 128;
211
212                 temp = (uint8_t)( temp < 0 ? 0 : temp );
213                 p_vout->p_sys->png_image[1][j] = temp;
214
215                 temp = ( p[x][0] * 439
216                        - p[x][1] * 368
217                        - p[x][2] * 71)/1000 + 128;
218                 temp = __MAX( __MIN( temp, 255 ), 0 );
219
220                 p_vout->p_sys->png_image[2][j] = (uint8_t)( temp < 0 ? 0 : temp );
221                 p_vout->p_sys->png_image_a[1][j] = p_vout->p_sys->png_image_a[0][i];
222                 j++;
223             }
224
225         }
226         /* now we can free row_pointers*/
227         free(row_pointers);
228     }
229     
230     I_OUTPUTPICTURES = 0;
231
232     /* Initialize the output structure */
233     p_vout->output.i_chroma = p_vout->render.i_chroma;
234     p_vout->output.i_width  = p_vout->render.i_width;
235     p_vout->output.i_height = p_vout->render.i_height;
236     p_vout->output.i_aspect = p_vout->render.i_aspect;
237
238     /* Try to open the real video output */
239     msg_Dbg( p_vout, "spawning the real video output" );
240
241     p_vout->p_sys->p_vout = vout_Create( p_vout,
242                            p_vout->render.i_width, p_vout->render.i_height,
243                            p_vout->render.i_chroma, p_vout->render.i_aspect );
244
245     /* Everything failed */
246     if( p_vout->p_sys->p_vout == NULL )
247     {
248         msg_Err( p_vout, "can't open vout, aborting" );
249
250         return VLC_EGENERIC;
251     }
252
253     var_AddCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
254     var_AddCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
255
256
257     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
258
259     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
260     
261     p_vout->p_sys->posx = config_GetInt( p_vout, "logo_x" );
262     p_vout->p_sys->posy = config_GetInt( p_vout, "logo_y" );
263     p_vout->p_sys->trans = (int)(config_GetFloat( p_vout, "logo_transparency" ) * 255);
264
265     return VLC_SUCCESS;
266 }
267
268 /*****************************************************************************
269  * End: terminate logo video thread output method
270  *****************************************************************************/
271 static void End( vout_thread_t *p_vout )
272 {
273     int i_index;
274
275     /* Free the fake output buffers we allocated */
276     for( i_index = I_OUTPUTPICTURES ; i_index ; )
277     {
278         i_index--;
279         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
280     }
281
282     var_DelCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
283     var_DelCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
284
285     DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
286     vlc_object_detach( p_vout->p_sys->p_vout );
287     vout_Destroy( p_vout->p_sys->p_vout );
288
289     config_PutInt( p_vout, "logo_x", p_vout->p_sys->posx );
290     config_PutInt( p_vout, "logo_y", p_vout->p_sys->posy );
291     config_PutFloat( p_vout, "logo_transparency", (float)(p_vout->p_sys->trans) / 255.0 );
292
293     if (p_vout->p_sys->error == 0)
294     {
295         free(p_vout->p_sys->png_image[0]);
296         free(p_vout->p_sys->png_image[1]);
297         free(p_vout->p_sys->png_image[2]);
298         free(p_vout->p_sys->png_image_a[0]);
299         free(p_vout->p_sys->png_image_a[1]);
300     }
301 }
302
303 /*****************************************************************************
304  * Destroy: destroy logo video thread output method
305  *****************************************************************************
306  * Terminate an output method created by InvertCreateOutputMethod
307  *****************************************************************************/
308 static void Destroy( vlc_object_t *p_this )
309 {
310     vout_thread_t *p_vout = (vout_thread_t *)p_this;
311
312     free( p_vout->p_sys );
313 }
314
315 /*****************************************************************************
316  * Render: displays previously rendered output
317  *****************************************************************************
318  * This function send the currently rendered image to Invert image, waits
319  * until it is displayed and switch the two rendering buffers, preparing next
320  * frame.
321  *****************************************************************************/
322 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
323 {
324     picture_t *p_outpic;
325     int i_index;
326     int i_pic_width;
327     int tr;
328
329     /* This is a new frame. Get a structure from the video_output. */
330     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
331               == NULL )
332     {
333         if( p_vout->b_die || p_vout->b_error )
334         {
335             return;
336         }
337         msleep( VOUT_OUTMEM_SLEEP );
338     }
339
340     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
341     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
342
343     i_pic_width=p_vout->output.i_width;
344     
345     tr = p_vout->p_sys->trans;
346     
347     for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
348     {
349
350         memcpy( p_outpic->p[i_index].p_pixels,
351                 p_pic->p[i_index].p_pixels, 
352                 p_pic->p[i_index].i_lines * p_pic->p[i_index].i_pitch);
353
354         if (p_vout->p_sys->error == 0)
355         {
356             unsigned int i;
357             unsigned int j;
358             uint8_t *p_out, *p_in_a, *p_in;
359             int i_delta;
360             unsigned int i_max;
361             unsigned int j_max;
362
363             if (i_index == 0)
364             {
365                 p_out  = p_outpic->p[i_index].p_pixels + p_vout->p_sys->posy * i_pic_width + p_vout->p_sys->posx;
366                 i_delta = i_pic_width - p_vout->p_sys->width;
367                 i_max = p_vout->p_sys->height;
368                 j_max = p_vout->p_sys->width;
369             } else
370             {
371                 p_out  = p_outpic->p[i_index].p_pixels + (p_vout->p_sys->posy / 2)* (i_pic_width / 2) + p_vout->p_sys->posx / 2;
372                 i_delta = (i_pic_width - p_vout->p_sys->width) / 2;
373                 i_max = p_vout->p_sys->height / 2;
374                 j_max = p_vout->p_sys->width / 2;
375             }
376             
377             p_in_a = p_vout->p_sys->png_image_a[i_index];
378             p_in   = p_vout->p_sys->png_image[i_index];
379
380             
381             for( i = 0; i < i_max ; i++ )
382             {
383                 for( j = 0 ; j < j_max ; j++)
384                 {
385                     *p_out = ( *p_out * ( 65025 - *p_in_a * tr) + *p_in * *p_in_a * tr) >> 16;
386
387                     p_out++;
388                     p_in++;
389                     p_in_a++;
390                 }
391                 p_out += i_delta;
392             }
393          }
394
395     }
396
397     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
398
399     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
400 }
401
402 /*****************************************************************************
403  * SendEvents: forward mouse and keyboard events to the parent p_vout
404  *****************************************************************************/
405 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
406                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
407 {
408     var_Set( (vlc_object_t *)p_data, psz_var, newval );
409
410     return VLC_SUCCESS;
411 }
412
413
414 /*****************************************************************************
415  * MouseEvent: callback for mouse events
416  ******************************************************************************/
417 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
418                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
419 {
420     vout_thread_t *p_vout = (vout_thread_t*)p_data;
421     vlc_value_t valb;
422     int i_delta;
423
424     #define posx p_vout->p_sys->posx
425     #define posy p_vout->p_sys->posy
426     #define width p_vout->p_sys->width
427     #define height p_vout->p_sys->height
428     #define trans p_vout->p_sys->trans
429         
430     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
431
432     i_delta = newval.i_int - oldval.i_int;
433
434     if ((valb.i_int & 0x2) == 2 && psz_var[6] == 'x' )
435     {
436         trans = __MIN( __MAX( trans + i_delta , 0 ) , 255 );
437         return VLC_SUCCESS;
438     }
439     if ((valb.i_int & 0x1) == 0)
440     {
441         return VLC_SUCCESS;
442     }
443
444     if( psz_var[6] == 'x' )
445     {
446         vlc_value_t valy;
447         var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
448         if ((newval.i_int >= (int)posx) && (valy.i_int >= (int)posy) && (newval.i_int <= (int)(posx + width)) && (valy.i_int <= (int)(posy + height)))
449         {
450             posx = __MIN( __MAX( posx + i_delta , 0 ) , p_vout->output.i_width - width );
451         }
452         
453     }
454     else if( psz_var[6] == 'y' )
455     {
456         vlc_value_t valx;
457         var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
458         if ((valx.i_int >= (int)posx) && (newval.i_int >= (int)posy) && (valx.i_int <= (int)(posx + width)) && (newval.i_int <= (int)(posy + height)))
459         {
460             posy = __MIN( __MAX( posy + i_delta , 0 ) , p_vout->output.i_height - height );
461         }
462
463     }
464     else if( psz_var[6] == 'c' )
465     {
466         if ((valb.i_int & 0x8) == 1)
467         {
468             p_vout->p_sys->trans++;
469         }
470         else if ((valb.i_int & 0x10) == 1)
471         {
472             p_vout->p_sys->trans--;
473         }
474     }
475
476     #undef posx
477     #undef posy
478     #undef width
479     #undef height
480     #undef trans
481
482     return VLC_SUCCESS;
483 }