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