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 $
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 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")
68 set_description( _("Logo video filter") );
69 set_capability( "video filter", 0 );
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 );
77 add_shortcut( "logo" );
78 set_callbacks( Create, Destroy );
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 *****************************************************************************/
89 vout_thread_t *p_vout;
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;
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 )
110 vout_thread_t *p_vout = (vout_thread_t *)p_this;
112 /* Allocate structure */
113 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
114 if( p_vout->p_sys == NULL )
116 msg_Err( p_vout, "out of memory" );
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;
129 /*****************************************************************************
130 * Init: initialize logo video thread output method
131 *****************************************************************************/
132 static int Init( vout_thread_t *p_vout )
140 int compression_type;
143 png_bytep * row_pointers;
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);
162 p_vout->p_sys->error=1;
163 msg_Err( p_vout , "file not found %s", 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++ )
178 row_pointers[i] = malloc( 4 * ( p_vout->p_sys->bit_depth + 7 ) / 8 * p_vout->p_sys->width );
180 png_read_image(png_ptr, row_pointers);
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;
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;
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 );
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];
201 for( y = 0; y < p_vout->p_sys->height ; y++)
203 for( x = 0; x < p_vout->p_sys->width ; x++)
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);
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))))
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
220 + p[x][2] * 98)/1000 + 16;
222 if( ( x % 2 == 0 ) && ( y % 2 == 0 ) )
225 temp = (p[x][2] * 439
227 - p[x][1] * 291)/1000 + 128;
229 temp = __MAX( __MIN( temp, 255 ), 0 );
230 p_vout->p_sys->png_image[1][idxc] = temp;
232 temp = ( p[x][0] * 439
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];
243 p_vout->p_sys->png_image_a[0][idx]= 0;
247 /* now we can free row_pointers*/
251 I_OUTPUTPICTURES = 0;
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;
259 /* Try to open the real video output */
260 msg_Dbg( p_vout, "spawning the real video output" );
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 );
266 /* Everything failed */
267 if( p_vout->p_sys->p_vout == NULL )
269 msg_Err( p_vout, "can't open vout, aborting" );
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);
278 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
280 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
282 ADD_PARENT_CALLBACKS( SendEventsToChild );
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");
291 /*****************************************************************************
292 * End: terminate logo video thread output method
293 *****************************************************************************/
294 static void End( vout_thread_t *p_vout )
298 /* Free the fake output buffers we allocated */
299 for( i_index = I_OUTPUTPICTURES ; i_index ; )
302 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
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);
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 );
312 config_PutInt( p_vout, "logo-x", p_vout->p_sys->posx );
313 config_PutInt( p_vout, "logo-y", p_vout->p_sys->posy );
315 if (p_vout->p_sys->error == 0)
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]);
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 )
332 vout_thread_t *p_vout = (vout_thread_t *)p_this;
334 DEL_PARENT_CALLBACKS( SendEventsToChild );
336 free( p_vout->p_sys );
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
345 *****************************************************************************/
346 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
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 ) )
356 if( p_vout->b_die || p_vout->b_error )
360 msleep( VOUT_OUTMEM_SLEEP );
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 );
367 tr = p_vout->p_sys->trans;
369 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
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);
376 if (p_vout->p_sys->error == 0)
380 uint8_t *p_out, *p_in_a, *p_in;
387 p_out = p_outpic->p[i_index].p_pixels +
388 p_vout->p_sys->posy * p_outpic->p[i_index].i_pitch +
390 i_max = p_vout->p_sys->height;
391 j_max = p_vout->p_sys->width;
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;
400 i_delta = p_outpic->p[i_index].i_pitch - j_max;
402 p_in_a = p_vout->p_sys->png_image_a[i_index];
403 p_in = p_vout->p_sys->png_image[i_index];
406 for( i = 0; i < i_max ; i++ )
408 for( j = 0 ; j < j_max ; j++)
410 *p_out = ( *p_out * ( 65025 - *p_in_a * tr) + *p_in * *p_in_a * tr ) >> 16;
420 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
422 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
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 )
431 var_Set( (vlc_object_t *)p_data, psz_var, newval );
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 )
443 vout_thread_t *p_vout = (vout_thread_t*)p_data;
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
453 var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &valb );
455 i_delta = newval.i_int - oldval.i_int;
457 if ((valb.i_int & 0x2) == 2 && psz_var[6] == 'x' )
459 trans = __MIN( __MAX( trans + i_delta , 0 ) , 255 );
462 if ((valb.i_int & 0x1) == 0)
467 if( psz_var[6] == 'x' )
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)))
473 posx = __MIN( __MAX( posx + i_delta , 0 ) , p_vout->output.i_width - width );
477 else if( psz_var[6] == 'y' )
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)))
483 posy = __MIN( __MAX( posy + i_delta , 0 ) , p_vout->output.i_height - height );
487 else if( psz_var[6] == 'c' )
489 if ((valb.i_int & 0x8) == 1)
491 p_vout->p_sys->trans++;
493 else if ((valb.i_int & 0x10) == 1)
495 p_vout->p_sys->trans--;
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 )
514 vout_thread_t *p_vout = (vout_thread_t *)p_this;
515 var_Set( p_vout->p_sys->p_vout, psz_var, newval );