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 $
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;
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);
156 p_vout->p_sys->error=1;
157 msg_Err( p_vout , "file not found %s", 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++ )
171 row_pointers[i] = malloc( 4 * ( p_vout->p_sys->bit_depth + 7 ) / 8 * p_vout->p_sys->width );
173 png_read_image(png_ptr, row_pointers);
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;
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 );
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];
189 for( i= 0; i < p_vout->p_sys->height * p_vout->p_sys->width ; i++)
195 x = i % p_vout->p_sys->width;
196 y = i / p_vout->p_sys->width;
199 p = (void*)row_pointers[y];
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
204 + p[x][2] * 98)/1000 + 16;
206 if( ( x % 2 == 0 ) && ( y % 2 == 0 ) )
208 temp = (p[x][2] * 439
210 - p[x][1] * 291)/1000 + 128;
212 temp = (uint8_t)( temp < 0 ? 0 : temp );
213 p_vout->p_sys->png_image[1][j] = temp;
215 temp = ( p[x][0] * 439
217 - p[x][2] * 71)/1000 + 128;
218 temp = __MAX( __MIN( temp, 255 ), 0 );
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];
226 /* now we can free row_pointers*/
230 I_OUTPUTPICTURES = 0;
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;
238 /* Try to open the real video output */
239 msg_Dbg( p_vout, "spawning the real video output" );
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 );
245 /* Everything failed */
246 if( p_vout->p_sys->p_vout == NULL )
248 msg_Err( p_vout, "can't open vout, aborting" );
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);
257 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
259 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
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);
268 /*****************************************************************************
269 * End: terminate logo video thread output method
270 *****************************************************************************/
271 static void End( vout_thread_t *p_vout )
275 /* Free the fake output buffers we allocated */
276 for( i_index = I_OUTPUTPICTURES ; i_index ; )
279 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
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);
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 );
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 );
293 if (p_vout->p_sys->error == 0)
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]);
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 )
310 vout_thread_t *p_vout = (vout_thread_t *)p_this;
312 free( p_vout->p_sys );
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
321 *****************************************************************************/
322 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
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 ) )
333 if( p_vout->b_die || p_vout->b_error )
337 msleep( VOUT_OUTMEM_SLEEP );
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 );
343 i_pic_width=p_vout->output.i_width;
345 tr = p_vout->p_sys->trans;
347 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
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);
354 if (p_vout->p_sys->error == 0)
358 uint8_t *p_out, *p_in_a, *p_in;
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;
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;
377 p_in_a = p_vout->p_sys->png_image_a[i_index];
378 p_in = p_vout->p_sys->png_image[i_index];
381 for( i = 0; i < i_max ; i++ )
383 for( j = 0 ; j < j_max ; j++)
385 *p_out = ( *p_out * ( 65025 - *p_in_a * tr) + *p_in * *p_in_a * tr) >> 16;
397 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
399 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
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 )
408 var_Set( (vlc_object_t *)p_data, psz_var, newval );
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 )
420 vout_thread_t *p_vout = (vout_thread_t*)p_data;
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
430 var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
432 i_delta = newval.i_int - oldval.i_int;
434 if ((valb.i_int & 0x2) == 2 && psz_var[6] == 'x' )
436 trans = __MIN( __MAX( trans + i_delta , 0 ) , 255 );
439 if ((valb.i_int & 0x1) == 0)
444 if( psz_var[6] == 'x' )
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)))
450 posx = __MIN( __MAX( posx + i_delta , 0 ) , p_vout->output.i_width - width );
454 else if( psz_var[6] == 'y' )
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)))
460 posy = __MIN( __MAX( posy + i_delta , 0 ) , p_vout->output.i_height - height );
464 else if( psz_var[6] == 'c' )
466 if ((valb.i_int & 0x8) == 1)
468 p_vout->p_sys->trans++;
470 else if ((valb.i_int & 0x10) == 1)
472 p_vout->p_sys->trans--;