1 /*****************************************************************************
2 * magnify.c : Magnify/Zoom interactive effect
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
38 #include "filter_common.h"
39 #include "filter_picture.h"
41 #include "vlc_image.h"
42 #include "vlc_input.h"
43 #include "vlc_playlist.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Create ( vlc_object_t * );
49 static void Destroy ( vlc_object_t * );
51 static int Init ( vout_thread_t * );
52 static void End ( vout_thread_t * );
53 static void Render ( vout_thread_t *, picture_t * );
55 static int SendEvents ( vlc_object_t *, char const *,
56 vlc_value_t, vlc_value_t, void * );
57 static int MouseEvent ( vlc_object_t *, char const *,
58 vlc_value_t, vlc_value_t, void * );
60 /*****************************************************************************
62 *****************************************************************************/
64 set_description( N_("Magnify/Zoom interactive video filter") );
65 set_shortname( N_( "Magnify" ));
66 set_capability( "video filter", 0 );
67 set_category( CAT_VIDEO );
68 set_subcategory( SUBCAT_VIDEO_VFILTER );
70 set_callbacks( Create, Destroy );
73 /*****************************************************************************
74 * vout_sys_t: Magnify video output method descriptor
75 *****************************************************************************/
78 vout_thread_t *p_vout;
80 image_handler_t *p_image;
82 int i_zoom; /* zoom level in percent */
83 int i_x, i_y; /* top left corner coordinates in original image */
85 bool b_visible; /* is "interface" visible ? */
88 /*****************************************************************************
89 * Control: control facility for the vout (forwards to child vout)
90 *****************************************************************************/
91 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
93 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
96 /*****************************************************************************
97 * Create: allocates Magnify video thread output method
98 *****************************************************************************/
99 static int Create( vlc_object_t *p_this )
101 vout_thread_t *p_vout = (vout_thread_t *)p_this;
103 switch( p_vout->fmt_in.i_chroma )
106 case VLC_FOURCC('G','R','E','Y'):
109 msg_Err( p_vout, "Unsupported chroma" );
113 /* Allocate structure */
114 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
115 if( p_vout->p_sys == NULL )
118 p_vout->p_sys->p_image = image_HandlerCreate( p_vout );
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;
125 p_vout->pf_control = Control;
130 /*****************************************************************************
131 * Init: initialize Magnify video thread output method
132 *****************************************************************************/
133 static int Init( vout_thread_t *p_vout )
139 memset( &fmt, 0, sizeof(video_format_t) );
140 I_OUTPUTPICTURES = 0;
142 /* Initialize the output structure */
143 p_vout->output.i_chroma = p_vout->render.i_chroma;
144 p_vout->output.i_width = p_vout->render.i_width;
145 p_vout->output.i_height = p_vout->render.i_height;
146 p_vout->output.i_aspect = p_vout->render.i_aspect;
148 p_vout->fmt_out = p_vout->fmt_in;
149 fmt = p_vout->fmt_out;
151 /* Try to open the real video output */
152 msg_Dbg( p_vout, "spawning the real video output" );
154 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
156 /* Everything failed */
157 if( p_vout->p_sys->p_vout == NULL )
159 msg_Err( p_vout, "cannot open vout, aborting" );
164 p_vout->p_sys->i_x = 0;
165 p_vout->p_sys->i_y = 0;
166 #define ZOOM_FACTOR 8
167 p_vout->p_sys->i_zoom = 2*ZOOM_FACTOR;
168 p_vout->p_sys->b_visible = true;
170 var_AddCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout );
171 var_AddCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout );
172 var_AddCallback( p_vout->p_sys->p_vout, "mouse-clicked",
175 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
176 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
177 ADD_PARENT_CALLBACKS( SendEventsToChild );
182 /*****************************************************************************
183 * End: terminate Magnify video thread output method
184 *****************************************************************************/
185 static void End( vout_thread_t *p_vout )
189 DEL_PARENT_CALLBACKS( SendEventsToChild );
191 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
193 /* Free the fake output buffers we allocated */
194 for( i_index = I_OUTPUTPICTURES ; i_index ; )
197 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
200 var_DelCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
201 var_DelCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
202 var_DelCallback( p_vout->p_sys->p_vout, "mouse-clicked", MouseEvent, p_vout);
204 vout_Destroy( p_vout->p_sys->p_vout );
207 /*****************************************************************************
208 * Destroy: destroy Magnify video thread output method
209 *****************************************************************************/
210 static void Destroy( vlc_object_t *p_this )
212 vout_thread_t *p_vout = (vout_thread_t *)p_this;
214 image_HandlerDelete( p_vout->p_sys->p_image );
217 free( p_vout->p_sys );
220 /*****************************************************************************
221 * Render: displays previously rendered output
222 *****************************************************************************/
223 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
227 int o_x = p_vout->p_sys->i_x;
228 int o_y = p_vout->p_sys->i_y;
229 int o_zoom = p_vout->p_sys->i_zoom;
232 video_format_t fmt_out;
233 picture_t *p_converted;
237 memset( &fmt_out, 0, sizeof(video_format_t) );
238 /* This is a new frame. Get a structure from the video_output. */
239 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
242 if( !vlc_object_alive (p_vout) || p_vout->b_error )
246 msleep( VOUT_OUTMEM_SLEEP );
249 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
251 p_oyp = &(p_outpic->p[Y_PLANE]);
253 /* background magnified image */
254 if( o_zoom != ZOOM_FACTOR )
256 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
258 o_yp = o_y*p_outpic->p[i_plane].i_lines
259 /p_outpic->p[Y_PLANE].i_lines;
260 o_xp = o_x*p_outpic->p[i_plane].i_pitch
261 /p_outpic->p[Y_PLANE].i_pitch;
262 int i_pitch = p_outpic->p[i_plane].i_pitch;
264 int o_zoom2 = o_zoom*o_zoom;
265 int o_zoom3 = o_zoom*o_zoom*o_zoom;
266 int o_zoom4 = o_zoom*o_zoom*o_zoom*o_zoom;
267 int o_zoom6 = o_zoom*o_zoom*o_zoom * o_zoom*o_zoom*o_zoom;
269 for( y=0; y<p_outpic->p[i_plane].i_visible_lines; y++ )
271 for( x=0; x<p_outpic->p[i_plane].i_visible_pitch; x++ )
274 /* Nearest neighbor */
275 int nx = o_xp + x*ZOOM_FACTOR/o_zoom;
276 int ny = o_yp + y*ZOOM_FACTOR/o_zoom;
277 p_outpic->p[i_plane].p_pixels[y*i_pitch+x] =
278 p_pic->p[i_plane].p_pixels[ny*i_pitch+nx];
281 int nx_real = o_xp*o_zoom + x*ZOOM_FACTOR;
282 int ny_real = o_yp*o_zoom + y*ZOOM_FACTOR;
283 int nx = nx_real/o_zoom;
284 int ny = ny_real/o_zoom;
285 int wtl = ((nx+1)*o_zoom-nx_real)*((ny+1)*o_zoom-ny_real);
286 int wtr = (nx_real-nx*o_zoom)*((ny+1)*o_zoom-ny_real);
287 int wbl = ((nx+1)*o_zoom-nx_real)*(ny_real-ny*o_zoom);
288 int wbr = (nx_real-nx*o_zoom)*(ny_real-ny*o_zoom);
289 p_outpic->p[i_plane].p_pixels[y*i_pitch+x] =
290 ( wtl*p_pic->p[i_plane].p_pixels[ny*i_pitch+nx]
291 + wtr*p_pic->p[i_plane].p_pixels[ny*i_pitch+(nx+1)]
292 + wbl*p_pic->p[i_plane].p_pixels[(ny+1)*i_pitch+nx]
293 + wbr*p_pic->p[i_plane].p_pixels[(ny+1)*i_pitch+(nx+1)]
297 /* FIXME: doesn't work */
298 /* \Sigma_{i=0..3} \Sigma_{j=0..3} \alpha_{i,j} x^i y^j
299 * 16 \alpha_{i,j} so we need to use the 16 nearest pixels
301 * In fact we should be able to write it as:
302 * \Pi_{i=1..16} [ \Pi_{j!=i} ( x - x_j ) * ( y - y_j ) ]
303 * * z_i / [ \Pi_{j!=i} ( x_i - x_j ) * ( y_i - y_j ) ]
304 * We also have to make sure that we don't use any
305 * x_j == x_i or y_j == y_i in the \Pi on j (else we get
306 * a 0/0 which kind of sucks) .
308 uint8_t *p = p_pic->p[i_plane].p_pixels;
309 int nx_real = o_xp*o_zoom + x*ZOOM_FACTOR;
310 int ny_real = o_yp*o_zoom + y*ZOOM_FACTOR;
312 int nx = nx_real/o_zoom;
313 int ny = ny_real/o_zoom;
316 int my = __MAX( ny-1, 0 );
317 int mx = __MAX( nx-1, 0 );
318 //p_outpic->p[i_plane].i_visible_lines - 4
319 //p_outpic->p[i_plane].i_visible_pitch - 4
321 for( yi = my; yi <= my+3; yi++ )
326 for( yj = my; yj <= my+3; yj++ )
330 numy *= (ny_real-yj*o_zoom);
335 for( xi = mx; xi <= mx+3; xi++ )
339 for( xj = mx; xj <= mx+3; xj++ )
343 num *= (nx_real-xj*o_zoom);
347 v = ( v * (p[yi*i_pitch+xi] * num) ) / ( den * o_zoom3 );
350 p_outpic->p[i_plane].p_pixels[y*i_pitch+x] = v;
358 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
360 vlc_memcpy( p_outpic->p[i_plane].p_pixels, p_pic->p[i_plane].p_pixels,
361 p_outpic->p[i_plane].i_lines * p_outpic->p[i_plane].i_pitch );
365 if( p_vout->p_sys->b_visible )
367 /* image visualization */
368 fmt_out = p_vout->fmt_out;
369 fmt_out.i_width = p_vout->render.i_width/VIS_ZOOM;
370 fmt_out.i_height = p_vout->render.i_height/VIS_ZOOM;
371 p_converted = image_Convert( p_vout->p_sys->p_image, p_pic,
372 &(p_pic->format), &fmt_out );
373 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
375 for( y=0; y<p_converted->p[i_plane].i_visible_lines; y++)
378 p_outpic->p[i_plane].p_pixels+y*p_outpic->p[i_plane].i_pitch,
379 p_converted->p[i_plane].p_pixels+y*p_converted->p[i_plane].i_pitch,
380 p_converted->p[i_plane].i_visible_pitch );
383 p_converted->pf_release( p_converted );
385 /* white rectangle on visualization */
386 v_w = p_oyp->i_pitch*ZOOM_FACTOR/(VIS_ZOOM*o_zoom);
387 v_h = (o_y+p_oyp->i_lines*ZOOM_FACTOR/o_zoom)/VIS_ZOOM;
389 vlc_memset( p_oyp->p_pixels
390 + o_y/VIS_ZOOM*p_oyp->i_pitch
391 + o_x/VIS_ZOOM, 0xff, v_w+1 );
393 for( y = o_y/VIS_ZOOM+1; y < v_h; y++ )
397 y*p_oyp->i_pitch+o_x/VIS_ZOOM
401 y*p_oyp->i_pitch+o_x/VIS_ZOOM + v_w
405 vlc_memset( p_oyp->p_pixels
407 + o_x/VIS_ZOOM, 0xff, v_w+1 );
410 v_h = p_oyp->i_lines/VIS_ZOOM;
417 /* print a small "VLC ZOOM" ... gruikkkkkkkkk */
418 #define DRAW(a) {int c,l=1;L a;}
420 #define X ;draw(l,c);c+=1
422 #define draw(y,x) p_oyp->p_pixels[(v_h+y)*p_oyp->i_pitch+x] = 0xff;
423 if( p_vout->p_sys->b_visible )
425 X o o o X o X o o o o o o X X X X o o o X X X X X o o X X X o o o X X X o o X X o X X o o o X o o o X o X X X X X o X X X X o o X X X X X L
426 X o o o X o X o o o o o X o o o o o o o o o o X o o X o o o X o X o o o X o X o X o X o o o X o o o X o o o X o o o X o o o X o X o o o o L
427 o X o X o o X o o o o o X o o o o o o o o o X o o o X o o o X o X o o o X o X o o o X o o o X X X X X o o o X o o o X o o o X o X X X X o L
428 o X o X o o X o o o o o X o o o o o o o o X o o o o X o o o X o X o o o X o X o o o X o o o X o o o X o o o X o o o X o o o X o X o o o o L
429 o o X o o o X X X X X o o X X X X o o o X X X X X o o X X X o o o X X X o o X o o o X o o o X o o o X o X X X X X o X X X X o o X X X X X L
433 X o o o X o X o o o o o o X X X X o o o X X X X X o o X X X o o o X X X o o X X o X X o o o o X X X X o X o o o X o o X X X o o X o o o X L
434 X o o o X o X o o o o o X o o o o o o o o o o X o o X o o o X o X o o o X o X o X o X o o o X o o o o o X o o o X o X o o o X o X o o o X L
435 o X o X o o X o o o o o X o o o o o o o o o X o o o X o o o X o X o o o X o X o o o X o o o o X X X o o X X X X X o X o o o X o X o X o X L
436 o X o X o o X o o o o o X o o o o o o o o X o o o o X o o o X o X o o o X o X o o o X o o o o o o o X o X o o o X o X o o o X o X o X o X L
437 o o X o o o X X X X X o o X X X X o o o X X X X X o o X X X o o o X X X o o X o o o X o o o X X X X o o X o o o X o o X X X o o o X o X o L
445 if( p_vout->p_sys->b_visible )
448 vlc_memset( p_oyp->p_pixels + (v_h+9)*p_oyp->i_pitch, 0xff, 41 );
449 for( y = v_h + 10; y < v_h + 90; y++ )
451 int width = v_h + 90 - y;
452 width = (width*width)/160;
453 if( (80 - y + v_h)*ZOOM_FACTOR/10 < o_zoom )
455 vlc_memset( p_oyp->p_pixels + y*p_oyp->i_pitch, 0xff, width );
459 p_oyp->p_pixels[y*p_oyp->i_pitch] = 0xff;
460 p_oyp->p_pixels[y*p_oyp->i_pitch + width - 1] = 0xff;
465 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
468 /*****************************************************************************
469 * SendEvents: forward mouse and keyboard events to the parent p_vout
470 *****************************************************************************/
471 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
472 vlc_value_t oldval, vlc_value_t newval, void *p_data )
474 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
475 var_Set( (vlc_object_t *)p_data, psz_var, newval );
480 /*****************************************************************************
481 * SendEventsToChild: forward events to the child/children vout
482 *****************************************************************************/
483 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
484 vlc_value_t oldval, vlc_value_t newval, void *p_data )
486 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
487 vout_thread_t *p_vout = (vout_thread_t *)p_this;
488 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
492 /*****************************************************************************
493 * MouseEvent: callback for mouse events
494 *****************************************************************************/
495 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
496 vlc_value_t oldval, vlc_value_t newval, void *p_data )
499 vout_thread_t *p_vout = (vout_thread_t*)p_data;
500 vlc_value_t vald,valx,valy;
503 #define MOUSE_CLICKED 2
504 #define MOUSE_MOVE_X 4
505 #define MOUSE_MOVE_Y 8
506 #define MOUSE_MOVE 12
509 int v_h = p_vout->output.i_height*ZOOM_FACTOR/p_vout->p_sys->i_zoom;
510 int v_w = p_vout->output.i_width*ZOOM_FACTOR/p_vout->p_sys->i_zoom;
512 if( psz_var[6] == 'x' ) mouse |= MOUSE_MOVE_X;
513 if( psz_var[6] == 'y' ) mouse |= MOUSE_MOVE_Y;
514 if( psz_var[6] == 'c' ) mouse |= MOUSE_CLICKED;
516 var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &vald );
517 if( vald.i_int & 0x1 ) mouse |= MOUSE_DOWN;
518 var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
519 var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
521 if( ( mouse&MOUSE_MOVE && mouse&MOUSE_DOWN)
522 || mouse&MOUSE_CLICKED )
524 /* (mouse moved and mouse button is down) or (mouse clicked) */
525 if( p_vout->p_sys->b_visible )
528 && valy.i_int < (int)p_vout->output.i_height/VIS_ZOOM
530 && valx.i_int < (int)p_vout->output.i_width/VIS_ZOOM )
532 /* mouse is over visualisation */
533 p_vout->p_sys->i_x = __MIN( __MAX( valx.i_int*VIS_ZOOM - v_w/2, 0 ),
534 p_vout->output.i_width - v_w - 1);
535 p_vout->p_sys->i_y = __MIN( __MAX( valy.i_int * VIS_ZOOM - v_h/2,
536 0 ), p_vout->output.i_height - v_h - 1);
538 else if( valx.i_int >= 0 && valx.i_int < 80
539 && valy.i_int >= (int)p_vout->output.i_height/VIS_ZOOM
540 && valy.i_int < (int)p_vout->output.i_height/VIS_ZOOM + 9
541 && mouse&MOUSE_CLICKED )
543 /* mouse is over the "VLC ZOOM HIDE" text */
544 p_vout->p_sys->b_visible = false;
546 else if( (int)p_vout->output.i_height/VIS_ZOOM + 9 <= valy.i_int
547 && valy.i_int <= (int)p_vout->output.i_height/VIS_ZOOM + 90
550 (( (int)p_vout->output.i_height/VIS_ZOOM + 90 - valy.i_int)
551 *( (int)p_vout->output.i_height/VIS_ZOOM + 90 - valy.i_int))/160 )
553 /* mouse is over zoom gauge */
554 p_vout->p_sys->i_zoom = __MAX( ZOOM_FACTOR,
555 ( 80 + (int)p_vout->output.i_height/VIS_ZOOM
556 - valy.i_int + 2) * ZOOM_FACTOR/10 );
558 else if( mouse&MOUSE_MOVE_X && !(mouse&MOUSE_CLICKED) )
560 p_vout->p_sys->i_x -= (newval.i_int - oldval.i_int)
561 *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
563 else if( mouse&MOUSE_MOVE_Y && !(mouse&MOUSE_CLICKED) )
565 p_vout->p_sys->i_y -= (newval.i_int - oldval.i_int)
566 *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
571 if( valx.i_int >= 0 && valx.i_int < 80 && valy.i_int >= 0
572 && valy.i_int <= 10 && mouse&MOUSE_CLICKED )
574 /* mouse is over the "VLC ZOOM SHOW" text */
575 p_vout->p_sys->b_visible = true;
577 else if( mouse&MOUSE_MOVE_X && !(mouse&MOUSE_CLICKED) )
579 p_vout->p_sys->i_x -= (newval.i_int - oldval.i_int)
580 *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
582 else if( mouse&MOUSE_MOVE_Y && !(mouse&MOUSE_CLICKED) )
584 p_vout->p_sys->i_y -= (newval.i_int - oldval.i_int)
585 *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
591 __MAX( 0, __MIN( p_vout->p_sys->i_x, (int)p_vout->output.i_width
592 - (int)p_vout->output.i_width*ZOOM_FACTOR/p_vout->p_sys->i_zoom - 1 ));
594 __MAX( 0, __MIN( p_vout->p_sys->i_y, (int)p_vout->output.i_height
595 - (int)p_vout->output.i_height*ZOOM_FACTOR/p_vout->p_sys->i_zoom - 1 ));