1 /*****************************************************************************
2 * logo.c : logo video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5 * $Id: logo.c,v 1.3 2003/09/18 21:42:54 garf Exp $
7 * Authors: Simon Latapie <garf@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
35 #include "filter_common.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Create ( vlc_object_t * );
42 static void Destroy ( vlc_object_t * );
44 static int Init ( vout_thread_t * );
45 static void End ( vout_thread_t * );
46 static void Render ( vout_thread_t *, picture_t * );
48 static int SendEvents( vlc_object_t *, char const *,
49 vlc_value_t, vlc_value_t, void * );
51 static int MouseEvent( vlc_object_t *, char const *,
52 vlc_value_t , vlc_value_t , void * );
54 /*****************************************************************************
56 *****************************************************************************/
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")
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 );
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 *****************************************************************************/
87 vout_thread_t *p_vout;
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;
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 )
108 vout_thread_t *p_vout = (vout_thread_t *)p_this;
110 /* Allocate structure */
111 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
112 if( p_vout->p_sys == NULL )
114 msg_Err( p_vout, "out of memory" );
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;
127 /*****************************************************************************
128 * Init: initialize logo video thread output method
129 *****************************************************************************/
130 static int Init( vout_thread_t *p_vout )
138 int compression_type;
141 png_bytep * row_pointers;
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);
160 p_vout->p_sys->error=1;
161 msg_Err( p_vout , "file not found %s", 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++ )
176 row_pointers[i] = malloc( 4 * ( p_vout->p_sys->bit_depth + 7 ) / 8 * p_vout->p_sys->width );
178 png_read_image(png_ptr, row_pointers);
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;
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;
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 );
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];
199 for( y = 0; y < p_vout->p_sys->height ; y++)
201 for( x = 0; x < p_vout->p_sys->width ; x++)
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);
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))))
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
218 + p[x][2] * 98)/1000 + 16;
220 if( ( x % 2 == 0 ) && ( y % 2 == 0 ) )
223 temp = (p[x][2] * 439
225 - p[x][1] * 291)/1000 + 128;
227 temp = __MAX( __MIN( temp, 255 ), 0 );
228 p_vout->p_sys->png_image[1][idxc] = temp;
230 temp = ( p[x][0] * 439
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];
241 p_vout->p_sys->png_image_a[0][idx]= 0;
245 /* now we can free row_pointers*/
249 I_OUTPUTPICTURES = 0;
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;
257 /* Try to open the real video output */
258 msg_Dbg( p_vout, "spawning the real video output" );
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 );
264 /* Everything failed */
265 if( p_vout->p_sys->p_vout == NULL )
267 msg_Err( p_vout, "can't open vout, aborting" );
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);
276 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
278 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
280 p_vout->p_sys->posx = config_GetInt( p_vout, "logo_x" );
281 p_vout->p_sys->posy = config_GetInt( p_vout, "logo_y" );
282 p_vout->p_sys->trans = (int)(config_GetFloat( p_vout, "logo_transparency" ) * 255);
287 /*****************************************************************************
288 * End: terminate logo video thread output method
289 *****************************************************************************/
290 static void End( vout_thread_t *p_vout )
294 /* Free the fake output buffers we allocated */
295 for( i_index = I_OUTPUTPICTURES ; i_index ; )
298 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
301 var_DelCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
302 var_DelCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
304 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
305 vlc_object_detach( p_vout->p_sys->p_vout );
306 vout_Destroy( p_vout->p_sys->p_vout );
308 config_PutInt( p_vout, "logo_x", p_vout->p_sys->posx );
309 config_PutInt( p_vout, "logo_y", p_vout->p_sys->posy );
310 config_PutFloat( p_vout, "logo_transparency", (float)(p_vout->p_sys->trans) / 255.0 );
312 if (p_vout->p_sys->error == 0)
314 free(p_vout->p_sys->png_image[0]);
315 free(p_vout->p_sys->png_image[1]);
316 free(p_vout->p_sys->png_image[2]);
317 free(p_vout->p_sys->png_image_a[0]);
318 free(p_vout->p_sys->png_image_a[1]);
322 /*****************************************************************************
323 * Destroy: destroy logo video thread output method
324 *****************************************************************************
325 * Terminate an output method created by InvertCreateOutputMethod
326 *****************************************************************************/
327 static void Destroy( vlc_object_t *p_this )
329 vout_thread_t *p_vout = (vout_thread_t *)p_this;
331 free( p_vout->p_sys );
334 /*****************************************************************************
335 * Render: displays previously rendered output
336 *****************************************************************************
337 * This function send the currently rendered image to Invert image, waits
338 * until it is displayed and switch the two rendering buffers, preparing next
340 *****************************************************************************/
341 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
347 /* This is a new frame. Get a structure from the video_output. */
348 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
351 if( p_vout->b_die || p_vout->b_error )
355 msleep( VOUT_OUTMEM_SLEEP );
358 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
359 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
362 tr = p_vout->p_sys->trans;
364 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
366 memcpy( p_outpic->p[i_index].p_pixels,
367 p_pic->p[i_index].p_pixels,
368 p_pic->p[i_index].i_lines * p_pic->p[i_index].i_pitch);
371 if (p_vout->p_sys->error == 0)
375 uint8_t *p_out, *p_in_a, *p_in;
382 p_out = p_outpic->p[i_index].p_pixels +
383 p_vout->p_sys->posy * p_outpic->p[i_index].i_pitch +
385 i_max = p_vout->p_sys->height;
386 j_max = p_vout->p_sys->width;
389 p_out = p_outpic->p[i_index].p_pixels +
390 (p_vout->p_sys->posy/2)* p_outpic->p[i_index].i_pitch +
391 p_vout->p_sys->posx / 2;
392 i_max = p_vout->p_sys->height / 2;
393 j_max = p_vout->p_sys->width / 2;
395 i_delta = p_outpic->p[i_index].i_pitch - j_max;
397 p_in_a = p_vout->p_sys->png_image_a[i_index];
398 p_in = p_vout->p_sys->png_image[i_index];
401 for( i = 0; i < i_max ; i++ )
403 for( j = 0 ; j < j_max ; j++)
405 *p_out = ( *p_out * ( 65025 - *p_in_a * tr) + *p_in * *p_in_a * tr) >> 16;
415 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
417 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
420 /*****************************************************************************
421 * SendEvents: forward mouse and keyboard events to the parent p_vout
422 *****************************************************************************/
423 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
424 vlc_value_t oldval, vlc_value_t newval, void *p_data )
426 var_Set( (vlc_object_t *)p_data, psz_var, newval );
432 /*****************************************************************************
433 * MouseEvent: callback for mouse events
434 ******************************************************************************/
435 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
436 vlc_value_t oldval, vlc_value_t newval, void *p_data )
438 vout_thread_t *p_vout = (vout_thread_t*)p_data;
442 #define posx p_vout->p_sys->posx
443 #define posy p_vout->p_sys->posy
444 #define width p_vout->p_sys->width
445 #define height p_vout->p_sys->height
446 #define trans p_vout->p_sys->trans
448 var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
450 i_delta = newval.i_int - oldval.i_int;
452 if ((valb.i_int & 0x2) == 2 && psz_var[6] == 'x' )
454 trans = __MIN( __MAX( trans + i_delta , 0 ) , 255 );
457 if ((valb.i_int & 0x1) == 0)
462 if( psz_var[6] == 'x' )
465 var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
466 if ((newval.i_int >= (int)posx) && (valy.i_int >= (int)posy) && (newval.i_int <= (int)(posx + width)) && (valy.i_int <= (int)(posy + height)))
468 posx = __MIN( __MAX( posx + i_delta , 0 ) , p_vout->output.i_width - width );
472 else if( psz_var[6] == 'y' )
475 var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
476 if ((valx.i_int >= (int)posx) && (newval.i_int >= (int)posy) && (valx.i_int <= (int)(posx + width)) && (newval.i_int <= (int)(posy + height)))
478 posy = __MIN( __MAX( posy + i_delta , 0 ) , p_vout->output.i_height - height );
482 else if( psz_var[6] == 'c' )
484 if ((valb.i_int & 0x8) == 1)
486 p_vout->p_sys->trans++;
488 else if ((valb.i_int & 0x10) == 1)
490 p_vout->p_sys->trans--;