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 /* Free the fake output buffers we allocated */
190 for( i_index = I_OUTPUTPICTURES ; i_index ; )
193 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
196 var_DelCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
197 var_DelCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
198 var_DelCallback( p_vout->p_sys->p_vout, "mouse-clicked", MouseEvent, p_vout);
201 /*****************************************************************************
202 * Destroy: destroy Magnify video thread output method
203 *****************************************************************************/
204 static void Destroy( vlc_object_t *p_this )
206 vout_thread_t *p_vout = (vout_thread_t *)p_this;
208 if( p_vout->p_sys->p_vout )
210 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
211 vlc_object_detach( p_vout->p_sys->p_vout );
212 vlc_object_release( p_vout->p_sys->p_vout );
215 image_HandlerDelete( p_vout->p_sys->p_image );
217 DEL_PARENT_CALLBACKS( SendEventsToChild );
219 free( p_vout->p_sys );
222 /*****************************************************************************
223 * Render: displays previously rendered output
224 *****************************************************************************/
225 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
229 int o_x = p_vout->p_sys->i_x;
230 int o_y = p_vout->p_sys->i_y;
231 int o_zoom = p_vout->p_sys->i_zoom;
234 video_format_t fmt_out;
235 picture_t *p_converted;
239 memset( &fmt_out, 0, sizeof(video_format_t) );
240 /* This is a new frame. Get a structure from the video_output. */
241 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
244 if( !vlc_object_alive (p_vout) || p_vout->b_error )
248 msleep( VOUT_OUTMEM_SLEEP );
251 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
253 p_oyp = &(p_outpic->p[Y_PLANE]);
255 /* background magnified image */
256 if( o_zoom != ZOOM_FACTOR )
258 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
260 o_yp = o_y*p_outpic->p[i_plane].i_lines
261 /p_outpic->p[Y_PLANE].i_lines;
262 o_xp = o_x*p_outpic->p[i_plane].i_pitch
263 /p_outpic->p[Y_PLANE].i_pitch;
264 int i_pitch = p_outpic->p[i_plane].i_pitch;
266 int o_zoom2 = o_zoom*o_zoom;
267 int o_zoom3 = o_zoom*o_zoom*o_zoom;
268 int o_zoom4 = o_zoom*o_zoom*o_zoom*o_zoom;
269 int o_zoom6 = o_zoom*o_zoom*o_zoom * o_zoom*o_zoom*o_zoom;
271 for( y=0; y<p_outpic->p[i_plane].i_visible_lines; y++ )
273 for( x=0; x<p_outpic->p[i_plane].i_visible_pitch; x++ )
276 /* Nearest neighbor */
277 int nx = o_xp + x*ZOOM_FACTOR/o_zoom;
278 int ny = o_yp + y*ZOOM_FACTOR/o_zoom;
279 p_outpic->p[i_plane].p_pixels[y*i_pitch+x] =
280 p_pic->p[i_plane].p_pixels[ny*i_pitch+nx];
283 int nx_real = o_xp*o_zoom + x*ZOOM_FACTOR;
284 int ny_real = o_yp*o_zoom + y*ZOOM_FACTOR;
285 int nx = nx_real/o_zoom;
286 int ny = ny_real/o_zoom;
287 int wtl = ((nx+1)*o_zoom-nx_real)*((ny+1)*o_zoom-ny_real);
288 int wtr = (nx_real-nx*o_zoom)*((ny+1)*o_zoom-ny_real);
289 int wbl = ((nx+1)*o_zoom-nx_real)*(ny_real-ny*o_zoom);
290 int wbr = (nx_real-nx*o_zoom)*(ny_real-ny*o_zoom);
291 p_outpic->p[i_plane].p_pixels[y*i_pitch+x] =
292 ( wtl*p_pic->p[i_plane].p_pixels[ny*i_pitch+nx]
293 + wtr*p_pic->p[i_plane].p_pixels[ny*i_pitch+(nx+1)]
294 + wbl*p_pic->p[i_plane].p_pixels[(ny+1)*i_pitch+nx]
295 + wbr*p_pic->p[i_plane].p_pixels[(ny+1)*i_pitch+(nx+1)]
299 /* FIXME: doesn't work */
300 /* \Sigma_{i=0..3} \Sigma_{j=0..3} \alpha_{i,j} x^i y^j
301 * 16 \alpha_{i,j} so we need to use the 16 nearest pixels
303 * In fact we should be able to write it as:
304 * \Pi_{i=1..16} [ \Pi_{j!=i} ( x - x_j ) * ( y - y_j ) ]
305 * * z_i / [ \Pi_{j!=i} ( x_i - x_j ) * ( y_i - y_j ) ]
306 * We also have to make sure that we don't use any
307 * x_j == x_i or y_j == y_i in the \Pi on j (else we get
308 * a 0/0 which kind of sucks) .
310 uint8_t *p = p_pic->p[i_plane].p_pixels;
311 int nx_real = o_xp*o_zoom + x*ZOOM_FACTOR;
312 int ny_real = o_yp*o_zoom + y*ZOOM_FACTOR;
314 int nx = nx_real/o_zoom;
315 int ny = ny_real/o_zoom;
318 int my = __MAX( ny-1, 0 );
319 int mx = __MAX( nx-1, 0 );
320 //p_outpic->p[i_plane].i_visible_lines - 4
321 //p_outpic->p[i_plane].i_visible_pitch - 4
323 for( yi = my; yi <= my+3; yi++ )
328 for( yj = my; yj <= my+3; yj++ )
332 numy *= (ny_real-yj*o_zoom);
337 for( xi = mx; xi <= mx+3; xi++ )
341 for( xj = mx; xj <= mx+3; xj++ )
345 num *= (nx_real-xj*o_zoom);
349 v = ( v * (p[yi*i_pitch+xi] * num) ) / ( den * o_zoom3 );
352 p_outpic->p[i_plane].p_pixels[y*i_pitch+x] = v;
360 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
362 vlc_memcpy( p_outpic->p[i_plane].p_pixels, p_pic->p[i_plane].p_pixels,
363 p_outpic->p[i_plane].i_lines * p_outpic->p[i_plane].i_pitch );
367 if( p_vout->p_sys->b_visible )
369 /* image visualization */
370 fmt_out = p_vout->fmt_out;
371 fmt_out.i_width = p_vout->render.i_width/VIS_ZOOM;
372 fmt_out.i_height = p_vout->render.i_height/VIS_ZOOM;
373 p_converted = image_Convert( p_vout->p_sys->p_image, p_pic,
374 &(p_pic->format), &fmt_out );
375 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
377 for( y=0; y<p_converted->p[i_plane].i_visible_lines; y++)
380 p_outpic->p[i_plane].p_pixels+y*p_outpic->p[i_plane].i_pitch,
381 p_converted->p[i_plane].p_pixels+y*p_converted->p[i_plane].i_pitch,
382 p_converted->p[i_plane].i_visible_pitch );
385 p_converted->pf_release( p_converted );
387 /* white rectangle on visualization */
388 v_w = p_oyp->i_pitch*ZOOM_FACTOR/(VIS_ZOOM*o_zoom);
389 v_h = (o_y+p_oyp->i_lines*ZOOM_FACTOR/o_zoom)/VIS_ZOOM;
391 vlc_memset( p_oyp->p_pixels
392 + o_y/VIS_ZOOM*p_oyp->i_pitch
393 + o_x/VIS_ZOOM, 0xff, v_w+1 );
395 for( y = o_y/VIS_ZOOM+1; y < v_h; y++ )
399 y*p_oyp->i_pitch+o_x/VIS_ZOOM
403 y*p_oyp->i_pitch+o_x/VIS_ZOOM + v_w
407 vlc_memset( p_oyp->p_pixels
409 + o_x/VIS_ZOOM, 0xff, v_w+1 );
412 v_h = p_oyp->i_lines/VIS_ZOOM;
419 /* print a small "VLC ZOOM" ... gruikkkkkkkkk */
420 #define DRAW(a) {int c,l=1;L a;}
422 #define X ;draw(l,c);c+=1
424 #define draw(y,x) p_oyp->p_pixels[(v_h+y)*p_oyp->i_pitch+x] = 0xff;
425 if( p_vout->p_sys->b_visible )
427 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
428 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
429 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
430 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
431 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
435 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
436 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
437 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
438 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
439 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
447 if( p_vout->p_sys->b_visible )
450 vlc_memset( p_oyp->p_pixels + (v_h+9)*p_oyp->i_pitch, 0xff, 41 );
451 for( y = v_h + 10; y < v_h + 90; y++ )
453 int width = v_h + 90 - y;
454 width = (width*width)/160;
455 if( (80 - y + v_h)*ZOOM_FACTOR/10 < o_zoom )
457 vlc_memset( p_oyp->p_pixels + y*p_oyp->i_pitch, 0xff, width );
461 p_oyp->p_pixels[y*p_oyp->i_pitch] = 0xff;
462 p_oyp->p_pixels[y*p_oyp->i_pitch + width - 1] = 0xff;
467 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
470 /*****************************************************************************
471 * SendEvents: forward mouse and keyboard events to the parent p_vout
472 *****************************************************************************/
473 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
474 vlc_value_t oldval, vlc_value_t newval, void *p_data )
476 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
477 var_Set( (vlc_object_t *)p_data, psz_var, newval );
482 /*****************************************************************************
483 * SendEventsToChild: forward events to the child/children vout
484 *****************************************************************************/
485 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
486 vlc_value_t oldval, vlc_value_t newval, void *p_data )
488 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
489 vout_thread_t *p_vout = (vout_thread_t *)p_this;
490 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
494 /*****************************************************************************
495 * MouseEvent: callback for mouse events
496 *****************************************************************************/
497 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
498 vlc_value_t oldval, vlc_value_t newval, void *p_data )
501 vout_thread_t *p_vout = (vout_thread_t*)p_data;
502 vlc_value_t vald,valx,valy;
505 #define MOUSE_CLICKED 2
506 #define MOUSE_MOVE_X 4
507 #define MOUSE_MOVE_Y 8
508 #define MOUSE_MOVE 12
511 int v_h = p_vout->output.i_height*ZOOM_FACTOR/p_vout->p_sys->i_zoom;
512 int v_w = p_vout->output.i_width*ZOOM_FACTOR/p_vout->p_sys->i_zoom;
514 if( psz_var[6] == 'x' ) mouse |= MOUSE_MOVE_X;
515 if( psz_var[6] == 'y' ) mouse |= MOUSE_MOVE_Y;
516 if( psz_var[6] == 'c' ) mouse |= MOUSE_CLICKED;
518 var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &vald );
519 if( vald.i_int & 0x1 ) mouse |= MOUSE_DOWN;
520 var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
521 var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
523 if( ( mouse&MOUSE_MOVE && mouse&MOUSE_DOWN)
524 || mouse&MOUSE_CLICKED )
526 /* (mouse moved and mouse button is down) or (mouse clicked) */
527 if( p_vout->p_sys->b_visible )
530 && valy.i_int < (int)p_vout->output.i_height/VIS_ZOOM
532 && valx.i_int < (int)p_vout->output.i_width/VIS_ZOOM )
534 /* mouse is over visualisation */
535 p_vout->p_sys->i_x = __MIN( __MAX( valx.i_int*VIS_ZOOM - v_w/2, 0 ),
536 p_vout->output.i_width - v_w - 1);
537 p_vout->p_sys->i_y = __MIN( __MAX( valy.i_int * VIS_ZOOM - v_h/2,
538 0 ), p_vout->output.i_height - v_h - 1);
540 else if( valx.i_int >= 0 && valx.i_int < 80
541 && valy.i_int >= (int)p_vout->output.i_height/VIS_ZOOM
542 && valy.i_int < (int)p_vout->output.i_height/VIS_ZOOM + 9
543 && mouse&MOUSE_CLICKED )
545 /* mouse is over the "VLC ZOOM HIDE" text */
546 p_vout->p_sys->b_visible = false;
548 else if( (int)p_vout->output.i_height/VIS_ZOOM + 9 <= valy.i_int
549 && valy.i_int <= (int)p_vout->output.i_height/VIS_ZOOM + 90
552 (( (int)p_vout->output.i_height/VIS_ZOOM + 90 - valy.i_int)
553 *( (int)p_vout->output.i_height/VIS_ZOOM + 90 - valy.i_int))/160 )
555 /* mouse is over zoom gauge */
556 p_vout->p_sys->i_zoom = __MAX( ZOOM_FACTOR,
557 ( 80 + (int)p_vout->output.i_height/VIS_ZOOM
558 - valy.i_int + 2) * ZOOM_FACTOR/10 );
560 else if( mouse&MOUSE_MOVE_X && !(mouse&MOUSE_CLICKED) )
562 p_vout->p_sys->i_x -= (newval.i_int - oldval.i_int)
563 *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
565 else if( mouse&MOUSE_MOVE_Y && !(mouse&MOUSE_CLICKED) )
567 p_vout->p_sys->i_y -= (newval.i_int - oldval.i_int)
568 *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
573 if( valx.i_int >= 0 && valx.i_int < 80 && valy.i_int >= 0
574 && valy.i_int <= 10 && mouse&MOUSE_CLICKED )
576 /* mouse is over the "VLC ZOOM SHOW" text */
577 p_vout->p_sys->b_visible = true;
579 else if( mouse&MOUSE_MOVE_X && !(mouse&MOUSE_CLICKED) )
581 p_vout->p_sys->i_x -= (newval.i_int - oldval.i_int)
582 *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
584 else if( mouse&MOUSE_MOVE_Y && !(mouse&MOUSE_CLICKED) )
586 p_vout->p_sys->i_y -= (newval.i_int - oldval.i_int)
587 *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
593 __MAX( 0, __MIN( p_vout->p_sys->i_x, (int)p_vout->output.i_width
594 - (int)p_vout->output.i_width*ZOOM_FACTOR/p_vout->p_sys->i_zoom - 1 ));
596 __MAX( 0, __MIN( p_vout->p_sys->i_y, (int)p_vout->output.i_height
597 - (int)p_vout->output.i_height*ZOOM_FACTOR/p_vout->p_sys->i_zoom - 1 ));