]> git.sesse.net Git - vlc/blob - modules/video_filter/magnify.c
Use image_Convert for upscaling the picture (2x faster).
[vlc] / modules / video_filter / magnify.c
1 /*****************************************************************************
2  * magnify.c : Magnify/Zoom interactive effect
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout.h>
35
36 #include <math.h>
37
38 #include "filter_common.h"
39 #include "filter_picture.h"
40
41 #include "vlc_image.h"
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Create    ( vlc_object_t * );
47 static void Destroy   ( vlc_object_t * );
48
49 vlc_module_begin();
50     set_description( N_("Magnify/Zoom interactive video filter") );
51     set_shortname( N_( "Magnify" ));
52     set_capability( "video filter", 0 );
53     set_category( CAT_VIDEO );
54     set_subcategory( SUBCAT_VIDEO_VFILTER );
55
56     set_callbacks( Create, Destroy );
57 vlc_module_end();
58
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static int  Init      ( vout_thread_t * );
64 static void End       ( vout_thread_t * );
65 static void Render    ( vout_thread_t *, picture_t * );
66
67 static int  SendEvents   ( vlc_object_t *, char const *,
68                            vlc_value_t, vlc_value_t, void * );
69 static int  MouseEvent   ( vlc_object_t *, char const *,
70                            vlc_value_t, vlc_value_t, void * );
71
72 static void DrawZoomStatus( uint8_t *, int i_pitch, int i_width, int i_height,
73                             int i_offset_x, int i_offset_y, bool b_visible );
74 static void DrawRectangle( uint8_t *, int i_pitch, int i_width, int i_height,
75                            int x, int y, int i_w, int i_h );
76
77 /*****************************************************************************
78  * vout_sys_t: Magnify video output method descriptor
79  *****************************************************************************/
80 struct vout_sys_t
81 {
82     vout_thread_t *p_vout;
83
84     image_handler_t *p_image;
85
86     vlc_mutex_t lock;
87     int i_zoom; /* zoom level in percent */
88     int i_x, i_y; /* top left corner coordinates in original image */
89
90     bool b_visible; /* is "interface" visible ? */
91 };
92
93 #define VIS_ZOOM 4
94 #define ZOOM_FACTOR 8
95
96 /*****************************************************************************
97  * Control: control facility for the vout (forwards to child vout)
98  *****************************************************************************/
99 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
100 {
101     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
102 }
103
104 /*****************************************************************************
105  * Create: allocates Magnify video thread output method
106  *****************************************************************************/
107 static int Create( vlc_object_t *p_this )
108 {
109     vout_thread_t *p_vout = (vout_thread_t *)p_this;
110
111     switch( p_vout->fmt_in.i_chroma )
112     {
113         CASE_PLANAR_YUV
114         case VLC_FOURCC('G','R','E','Y'):
115             break;
116         default:
117             msg_Err( p_vout, "Unsupported chroma" );
118             return VLC_EGENERIC;
119     }
120
121     /* Allocate structure */
122     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
123     if( p_vout->p_sys == NULL )
124         return VLC_ENOMEM;
125
126     p_vout->p_sys->p_image = image_HandlerCreate( p_vout );
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 Magnify 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     video_format_t fmt;
146
147     memset( &fmt, 0, sizeof(video_format_t) );
148     I_OUTPUTPICTURES = 0;
149
150     /* Initialize the output structure */
151     p_vout->output.i_chroma = p_vout->render.i_chroma;
152     p_vout->output.i_width  = p_vout->render.i_width;
153     p_vout->output.i_height = p_vout->render.i_height;
154     p_vout->output.i_aspect = p_vout->render.i_aspect;
155
156     p_vout->fmt_out = p_vout->fmt_in;
157     fmt = p_vout->fmt_out;
158
159     /* Try to open the real video output */
160     msg_Dbg( p_vout, "spawning the real video output" );
161
162     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
163
164     /* Everything failed */
165     if( p_vout->p_sys->p_vout == NULL )
166     {
167         msg_Err( p_vout, "cannot open vout, aborting" );
168         return VLC_EGENERIC;
169     }
170
171     vlc_mutex_init( &p_vout->p_sys->lock );
172     p_vout->p_sys->i_x = 0;
173     p_vout->p_sys->i_y = 0;
174     p_vout->p_sys->i_zoom = 2*ZOOM_FACTOR;
175     p_vout->p_sys->b_visible = true;
176
177     var_AddCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout );
178     var_AddCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout );
179     var_AddCallback( p_vout->p_sys->p_vout, "mouse-clicked",
180                      MouseEvent, p_vout);
181
182     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
183     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
184     ADD_PARENT_CALLBACKS( SendEventsToChild );
185
186     return VLC_SUCCESS;
187 }
188
189 /*****************************************************************************
190  * End: terminate Magnify video thread output method
191  *****************************************************************************/
192 static void End( vout_thread_t *p_vout )
193 {
194     int i_index;
195
196     DEL_PARENT_CALLBACKS( SendEventsToChild );
197
198     DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
199
200     /* Free the fake output buffers we allocated */
201     for( i_index = I_OUTPUTPICTURES ; i_index ; )
202     {
203         i_index--;
204         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
205     }
206
207     var_DelCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout);
208     var_DelCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout);
209     var_DelCallback( p_vout->p_sys->p_vout, "mouse-clicked", MouseEvent, p_vout);
210
211     vlc_mutex_destroy( &p_vout->p_sys->lock );
212
213     vout_CloseAndRelease( p_vout->p_sys->p_vout );
214 }
215
216 /*****************************************************************************
217  * Destroy: destroy Magnify video thread output method
218  *****************************************************************************/
219 static void Destroy( vlc_object_t *p_this )
220 {
221     vout_thread_t *p_vout = (vout_thread_t *)p_this;
222
223     image_HandlerDelete( p_vout->p_sys->p_image );
224
225
226     free( p_vout->p_sys );
227 }
228
229 /*****************************************************************************
230  * Render: displays previously rendered output
231  *****************************************************************************/
232 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
233 {
234     vout_sys_t *p_sys = p_vout->p_sys;
235     picture_t *p_outpic;
236
237     int v_w, v_h;
238     picture_t *p_converted;
239     plane_t *p_oyp;
240     int i_plane;
241
242     /* This is a new frame. Get a structure from the video_output. */
243     while( ( p_outpic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) )
244               == NULL )
245     {
246         if( !vlc_object_alive (p_vout) || p_vout->b_error )
247         {
248             return;
249         }
250         msleep( VOUT_OUTMEM_SLEEP );
251     }
252
253     vout_DatePicture( p_sys->p_vout, p_outpic, p_pic->date );
254
255
256     vlc_mutex_lock( &p_sys->lock );
257     const bool b_visible = p_sys->b_visible;
258     const int o_x = p_sys->i_x;
259     const int o_y = p_sys->i_y;
260     const int o_zoom = p_sys->i_zoom;
261     vlc_mutex_unlock( &p_sys->lock );
262
263     /* background magnified image */
264     if( o_zoom != ZOOM_FACTOR )
265     {
266         video_format_t fmt_in;
267         video_format_t fmt_out;
268         picture_t crop;
269
270         crop = *p_pic;
271         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
272         {
273             const int o_yp = o_y * p_outpic->p[i_plane].i_lines / p_outpic->p[Y_PLANE].i_lines;
274             const int o_xp = o_x * p_outpic->p[i_plane].i_pitch / p_outpic->p[Y_PLANE].i_pitch;
275
276             crop.p[i_plane].p_pixels += o_yp * p_outpic->p[i_plane].i_pitch + o_xp;
277         }
278
279         /* */
280         fmt_in = p_vout->fmt_out;
281         fmt_in.i_width  = (fmt_in.i_width  * ZOOM_FACTOR / o_zoom) & ~1;
282         fmt_in.i_height = (fmt_in.i_height * ZOOM_FACTOR / o_zoom) & ~1;
283
284         /* */
285         fmt_out = p_vout->fmt_out;
286
287         p_converted = image_Convert( p_sys->p_image, &crop, &fmt_in, &fmt_out );
288
289         picture_CopyPixels( p_outpic, p_converted );
290
291         picture_Release( p_converted );
292     }
293     else
294     {
295         picture_CopyPixels( p_outpic, p_pic );
296     }
297
298     /* */
299     p_oyp = &p_outpic->p[Y_PLANE];
300     if( b_visible )
301     {
302         video_format_t fmt_out;
303
304         /* image visualization */
305         fmt_out = p_vout->fmt_out;
306         fmt_out.i_width  = (p_vout->render.i_width/VIS_ZOOM ) & ~1;
307         fmt_out.i_height = (p_vout->render.i_height/VIS_ZOOM) & ~1;
308         p_converted = image_Convert( p_sys->p_image, p_pic,
309                                      &p_pic->format, &fmt_out );
310         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
311         {
312             int y;
313             for( y = 0; y < p_converted->p[i_plane].i_visible_lines; y++)
314             {
315                 vlc_memcpy(
316                     &p_outpic->p[i_plane].p_pixels[y*p_outpic->p[i_plane].i_pitch],
317                     p_converted->p[i_plane].p_pixels+y*p_converted->p[i_plane].i_pitch,
318                     p_converted->p[i_plane].i_visible_pitch );
319             }
320         }
321         picture_Release( p_converted );
322
323         /* white rectangle on visualization */
324         v_w = __MIN( fmt_out.i_width  * ZOOM_FACTOR / o_zoom, fmt_out.i_width - 1 );
325         v_h = __MIN( fmt_out.i_height * ZOOM_FACTOR / o_zoom, fmt_out.i_height - 1 );
326
327         DrawRectangle( p_oyp->p_pixels, p_oyp->i_pitch,
328                        p_oyp->i_pitch, p_oyp->i_lines,
329                        o_x/VIS_ZOOM, o_y/VIS_ZOOM,
330                        v_w, v_h );
331
332         /* */
333         v_h = fmt_out.i_height + 1;
334     }
335     else
336     {
337         v_h = 1;
338     }
339
340     /* print a small "VLC ZOOM" */
341     DrawZoomStatus( p_oyp->p_pixels, p_oyp->i_pitch, p_oyp->i_pitch, p_oyp->i_lines,
342                     1, v_h, b_visible );
343
344     if( b_visible )
345     {
346         int y;
347
348         /* zoom gauge */
349         vlc_memset( p_oyp->p_pixels + (v_h+9)*p_oyp->i_pitch, 0xff, 41 );
350         for( y = v_h + 10; y < v_h + 90; y++ )
351         {
352             int width = v_h + 90 - y;
353             width = (width*width)/160;
354             if( (80 - y + v_h)*ZOOM_FACTOR/10 < o_zoom )
355             {
356                 vlc_memset( p_oyp->p_pixels + y*p_oyp->i_pitch, 0xff, width );
357             }
358             else
359             {
360                 p_oyp->p_pixels[y*p_oyp->i_pitch] = 0xff;
361                 p_oyp->p_pixels[y*p_oyp->i_pitch + width - 1] = 0xff;
362             }
363         }
364     }
365
366     vout_DisplayPicture( p_sys->p_vout, p_outpic );
367 }
368
369 static void DrawZoomStatus( uint8_t *pb_dst, int i_pitch, int i_width, int i_height,
370                             int i_offset_x, int i_offset_y, bool b_visible )
371 {
372     static const char *p_hide =
373         "X   X X      XXXX   XXXXX  XXX   XXX  XX XX   X   X XXXXX XXXX  XXXXXL"
374         "X   X X     X          X  X   X X   X X X X   X   X   X   X   X X    L"
375         " X X  X     X         X   X   X X   X X   X   XXXXX   X   X   X XXXX L"
376         " X X  X     X        X    X   X X   X X   X   X   X   X   X   X X    L"
377         "  X   XXXXX  XXXX   XXXXX  XXX   XXX  X   X   X   X XXXXX XXXX  XXXXXL";
378     static const char *p_show = 
379         "X   X X      XXXX   XXXXX  XXX   XXX  XX XX    XXXX X   X  XXX  X   XL"
380         "X   X X     X          X  X   X X   X X X X   X     X   X X   X X   XL"
381         " X X  X     X         X   X   X X   X X   X    XXX  XXXXX X   X X X XL"
382         " X X  X     X        X    X   X X   X X   X       X X   X X   X X X XL"
383         "  X   XXXXX  XXXX   XXXXX  XXX   XXX  X   X   XXXX  X   X  XXX   X X L";
384     const char *p_draw = b_visible ? p_hide : p_show;
385     int i, y, x;
386
387     for( i = 0, x = i_offset_x, y = i_offset_y; p_draw[i] != '\0'; i++ )
388     {
389         if( p_draw[i] == 'X' )
390         {
391             if( x < i_width && y < i_height )
392                 pb_dst[y*i_pitch + x] = 0xff;
393             x++;
394         }
395         else if( p_draw[i] == ' ' )
396         {
397             x++;
398         }
399         else if( p_draw[i] == 'L' )
400         {
401             x = i_offset_x;
402             y++;
403         }
404     }
405 }
406 static void DrawRectangle( uint8_t *pb_dst, int i_pitch, int i_width, int i_height,
407                            int x, int y, int i_w, int i_h )
408 {
409     int dy;
410
411     if( x + i_w > i_width || y + i_h > i_height )
412         return;
413
414     /* top line */
415     vlc_memset( &pb_dst[y * i_pitch + x], 0xff, i_w );
416
417     /* left and right */
418     for( dy = 1; dy < i_h-1; dy++ )
419     {
420         pb_dst[(y+dy) * i_pitch + x +     0] = 0xff;
421         pb_dst[(y+dy) * i_pitch + x + i_w-1] = 0xff;
422     }
423
424     /* bottom line */
425     vlc_memset( &pb_dst[(y+i_h-1) * i_pitch + x], 0xff, i_w );
426 }
427
428 /*****************************************************************************
429  * SendEvents: forward mouse and keyboard events to the parent p_vout
430  *****************************************************************************/
431 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
432                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
433 {
434     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
435     var_Set( (vlc_object_t *)p_data, psz_var, newval );
436
437     return VLC_SUCCESS;
438 }
439
440 /*****************************************************************************
441  * SendEventsToChild: forward events to the child/children vout
442  *****************************************************************************/
443 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
444                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
445 {
446     VLC_UNUSED(p_data); VLC_UNUSED(oldval);
447     vout_thread_t *p_vout = (vout_thread_t *)p_this;
448     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
449     return VLC_SUCCESS;
450 }
451
452 /*****************************************************************************
453  * MouseEvent: callback for mouse events
454  *****************************************************************************/
455 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
456                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
457 {
458     vout_thread_t *p_vout = (vout_thread_t*)p_data;
459     vlc_value_t vald,valx,valy;
460
461     VLC_UNUSED(p_this);
462
463 #define MOUSE_DOWN    1
464 #define MOUSE_CLICKED 2
465 #define MOUSE_MOVE_X  4
466 #define MOUSE_MOVE_Y  8
467 #define MOUSE_MOVE    12
468     uint8_t mouse= 0;
469
470     if( psz_var[6] == 'x' ) mouse |= MOUSE_MOVE_X;
471     if( psz_var[6] == 'y' ) mouse |= MOUSE_MOVE_Y;
472     if( psz_var[6] == 'c' ) mouse |= MOUSE_CLICKED;
473
474     var_Get( p_vout->p_sys->p_vout, "mouse-button-down", &vald );
475     if( vald.i_int & 0x1 ) mouse |= MOUSE_DOWN;
476     var_Get( p_vout->p_sys->p_vout, "mouse-y", &valy );
477     var_Get( p_vout->p_sys->p_vout, "mouse-x", &valx );
478
479     vlc_mutex_lock( &p_vout->p_sys->lock );
480
481     const int v_h = p_vout->output.i_height*ZOOM_FACTOR/p_vout->p_sys->i_zoom;
482     const int v_w = p_vout->output.i_width*ZOOM_FACTOR/p_vout->p_sys->i_zoom;
483
484     if( ( mouse&MOUSE_MOVE && mouse&MOUSE_DOWN)
485         || mouse&MOUSE_CLICKED )
486     {
487     /* (mouse moved and mouse button is down) or (mouse clicked) */
488         if( p_vout->p_sys->b_visible )
489         {
490             if(    0 <= valy.i_int
491                 && valy.i_int < (int)p_vout->output.i_height/VIS_ZOOM
492                 && 0 <= valx.i_int
493                 && valx.i_int < (int)p_vout->output.i_width/VIS_ZOOM )
494             {
495             /* mouse is over visualisation */
496                 p_vout->p_sys->i_x = __MIN( __MAX( valx.i_int*VIS_ZOOM - v_w/2, 0 ),
497                                             p_vout->output.i_width - v_w - 1);
498                 p_vout->p_sys->i_y = __MIN( __MAX( valy.i_int * VIS_ZOOM - v_h/2,
499                                         0 ), p_vout->output.i_height - v_h - 1);
500             }
501             else if( valx.i_int >= 0 && valx.i_int < 80
502                 && valy.i_int >= (int)p_vout->output.i_height/VIS_ZOOM
503                 && valy.i_int < (int)p_vout->output.i_height/VIS_ZOOM + 9
504                 && mouse&MOUSE_CLICKED )
505             {
506             /* mouse is over the "VLC ZOOM HIDE" text */
507                 p_vout->p_sys->b_visible = false;
508             }
509             else if(    (int)p_vout->output.i_height/VIS_ZOOM + 9 <= valy.i_int
510                      && valy.i_int <= (int)p_vout->output.i_height/VIS_ZOOM + 90
511                      && 0 <= valx.i_int
512                      && valx.i_int <=
513                      (( (int)p_vout->output.i_height/VIS_ZOOM + 90 -  valy.i_int)
514                *( (int)p_vout->output.i_height/VIS_ZOOM + 90 -  valy.i_int))/160 )
515             {
516             /* mouse is over zoom gauge */
517                 p_vout->p_sys->i_zoom = __MAX( ZOOM_FACTOR,
518                                 ( 80 + (int)p_vout->output.i_height/VIS_ZOOM
519                                    - valy.i_int + 2) * ZOOM_FACTOR/10 );
520             }
521             else if( mouse&MOUSE_MOVE_X && !(mouse&MOUSE_CLICKED) )
522             {
523                 p_vout->p_sys->i_x -= (newval.i_int - oldval.i_int)
524                                       *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
525             }
526             else if( mouse&MOUSE_MOVE_Y && !(mouse&MOUSE_CLICKED) )
527             {
528                 p_vout->p_sys->i_y -= (newval.i_int - oldval.i_int)
529                                       *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
530             }
531         }
532         else
533         {
534             if( valx.i_int >= 0 && valx.i_int < 80 && valy.i_int >= 0
535                 && valy.i_int <= 10 && mouse&MOUSE_CLICKED )
536             {
537             /* mouse is over the "VLC ZOOM SHOW" text */
538                 p_vout->p_sys->b_visible = true;
539             }
540             else if( mouse&MOUSE_MOVE_X && !(mouse&MOUSE_CLICKED) )
541             {
542                 p_vout->p_sys->i_x -= (newval.i_int - oldval.i_int)
543                                       *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
544             }
545             else if( mouse&MOUSE_MOVE_Y && !(mouse&MOUSE_CLICKED) )
546             {
547                 p_vout->p_sys->i_y -= (newval.i_int - oldval.i_int)
548                                       *ZOOM_FACTOR/p_vout->p_sys->i_zoom;
549             }
550         }
551     }
552
553     p_vout->p_sys->i_x =
554          __MAX( 0, __MIN( p_vout->p_sys->i_x, (int)p_vout->output.i_width
555          - (int)p_vout->output.i_width*ZOOM_FACTOR/p_vout->p_sys->i_zoom - 1 ));
556     p_vout->p_sys->i_y =
557          __MAX( 0, __MIN( p_vout->p_sys->i_y, (int)p_vout->output.i_height
558         - (int)p_vout->output.i_height*ZOOM_FACTOR/p_vout->p_sys->i_zoom - 1 ));
559
560     vlc_mutex_unlock( &p_vout->p_sys->lock );
561
562     return VLC_SUCCESS;
563 }