1 /*****************************************************************************
2  * magnify.c : Magnify/Zoom interactive effect
3  *****************************************************************************
4  * Copyright (C) 2005-2009 VLC authors and VideoLAN
5  * $Id: 5f94f2d6fc2d48899b3002a85f1a0b2e04077de8 $
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 #include <math.h>
32 #include <assert.h>
33 
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_image.h>
37 #include <vlc_filter.h>
38 #include <vlc_mouse.h>
39 #include <vlc_picture.h>
40 #include "filter_picture.h"
41 
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Create    ( vlc_object_t * );
46 static void Destroy   ( vlc_object_t * );
47 
48 vlc_module_begin ()
49     set_description( N_("Magnify/Zoom interactive video filter") )
50     set_shortname( N_( "Magnify" ))
51     set_capability( "video filter", 0 )
52     set_category( CAT_VIDEO )
53     set_subcategory( SUBCAT_VIDEO_VFILTER )
54 
55     set_callbacks( Create, Destroy )
56 vlc_module_end ()
57 
58 
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static picture_t *Filter( filter_t *, picture_t * );
63 static int Mouse( filter_t *, vlc_mouse_t *, const vlc_mouse_t *, const vlc_mouse_t * );
64 
65 /* */
66 static void DrawZoomStatus( uint8_t *, int i_pitch, int i_width, int i_height,
67                             int i_offset_x, int i_offset_y, bool b_visible );
68 static void DrawRectangle( uint8_t *, int i_pitch, int i_width, int i_height,
69                            int x, int y, int i_w, int i_h );
70 
71 /* */
72 struct filter_sys_t
73 {
74     image_handler_t *p_image;
75 
76     int64_t i_hide_timeout;
77 
78     int i_zoom; /* zoom level in percent */
79     int i_x, i_y; /* top left corner coordinates in original image */
80 
81     bool b_visible; /* is "interface" visible ? */
82 
83     int64_t i_last_activity;
84 };
85 
86 #define VIS_ZOOM 4
87 #define ZOOM_FACTOR 8
88 
89 /*****************************************************************************
90  * Create:
91  *****************************************************************************/
Create(vlc_object_t * p_this)92 static int Create( vlc_object_t *p_this )
93 {
94     filter_t *p_filter = (filter_t *)p_this;
95     filter_sys_t *p_sys;
96 
97     /* */
98     switch( p_filter->fmt_in.i_codec )
99     {
100     CASE_PLANAR_YUV
101     case VLC_CODEC_GREY:
102         break;
103     default:
104         msg_Err( p_filter, "Unsupported chroma %4.4s", (char *)&p_filter->fmt_in.i_codec );
105         return VLC_EGENERIC;
106     }
107     if( !es_format_IsSimilar( &p_filter->fmt_in, &p_filter->fmt_out ) )
108     {
109         msg_Err( p_filter, "Input and output format does not match" );
110         return VLC_EGENERIC;
111     }
112 
113     /* Allocate structure */
114     p_filter->p_sys = p_sys = malloc( sizeof( *p_sys ) );
115     if( !p_filter->p_sys )
116         return VLC_ENOMEM;
117 
118     p_sys->p_image = image_HandlerCreate( p_filter );
119     if( !p_sys->p_image )
120     {
121         free( p_sys );
122         return VLC_EGENERIC;
123     }
124 
125     p_sys->i_x = 0;
126     p_sys->i_y = 0;
127     p_sys->i_zoom = 2*ZOOM_FACTOR;
128     p_sys->b_visible = true;
129     p_sys->i_last_activity = mdate();
130     p_sys->i_hide_timeout = 1000 * var_InheritInteger( p_filter, "mouse-hide-timeout" );
131 
132     /* */
133     p_filter->pf_video_filter = Filter;
134     p_filter->pf_video_mouse = Mouse;
135     return VLC_SUCCESS;
136 }
137 
138 /*****************************************************************************
139  * Destroy:
140  *****************************************************************************/
Destroy(vlc_object_t * p_this)141 static void Destroy( vlc_object_t *p_this )
142 {
143     filter_t *p_filter = (filter_t *)p_this;
144     filter_sys_t *p_sys = p_filter->p_sys;
145 
146     image_HandlerDelete( p_sys->p_image );
147 
148     free( p_sys );
149 }
150 
plane_CopyVisiblePixels(plane_t * p_dst,const plane_t * p_src)151 static void plane_CopyVisiblePixels( plane_t *p_dst, const plane_t *p_src )
152 {
153     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
154                                      p_src->i_visible_pitch );
155     const unsigned i_height = __MIN( p_dst->i_visible_lines,
156                                      p_src->i_visible_lines );
157 
158     /* The 2x visible pitch check does two things:
159        1) Makes field plane_t's work correctly (see the deinterlacer module)
160        2) Moves less data if the pitch and visible pitch differ much.
161     */
162     if( p_src->i_pitch == p_dst->i_pitch  &&
163         p_src->i_pitch < 2*p_src->i_visible_pitch )
164     {
165         /* There are margins, but with the same width : perfect ! */
166         memcpy( p_dst->p_pixels, p_src->p_pixels,
167                     p_src->i_pitch * i_height );
168     }
169     else
170     {
171         /* We need to proceed line by line */
172         uint8_t *p_in = p_src->p_pixels;
173         uint8_t *p_out = p_dst->p_pixels;
174 
175         assert( p_in );
176         assert( p_out );
177 
178         for( int i_line = i_height; i_line--; )
179         {
180             memcpy( p_out, p_in, i_width );
181             p_in += p_src->i_pitch;
182             p_out += p_dst->i_pitch;
183         }
184     }
185 }
186 
picture_CopyVisiblePixels(picture_t * p_dst,const picture_t * p_src)187 static void picture_CopyVisiblePixels( picture_t *p_dst, const picture_t *p_src )
188 {
189     for( int i = 0; i < p_src->i_planes ; i++ )
190         plane_CopyVisiblePixels( p_dst->p+i, p_src->p+i );
191 
192     assert( p_dst->context == NULL );
193 
194     if( p_src->context != NULL )
195         p_dst->context = p_src->context->copy( p_src->context );
196 }
197 
198 /*****************************************************************************
199  * Render: displays previously rendered output
200  *****************************************************************************/
Filter(filter_t * p_filter,picture_t * p_pic)201 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
202 {
203     filter_sys_t *p_sys = p_filter->p_sys;
204     picture_t *p_outpic;
205 
206     int v_w, v_h;
207     picture_t *p_converted;
208     plane_t *p_oyp;
209 
210     p_outpic = filter_NewPicture( p_filter );
211     if( !p_outpic )
212     {
213         picture_Release( p_pic );
214         return NULL;
215     }
216 
217     /* */
218     const bool b_visible = p_sys->b_visible;
219     const int o_x = p_sys->i_x;
220     const int o_y = p_sys->i_y;
221     const int o_zoom = p_sys->i_zoom;
222 
223     /* background magnified image */
224     if( o_zoom != ZOOM_FACTOR )
225     {
226         video_format_t fmt_in;
227         video_format_t fmt_out;
228         plane_t orig_planes[PICTURE_PLANE_MAX];
229         memcpy(orig_planes, p_pic->p, sizeof orig_planes);
230 
231         for( int i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
232         {
233             const int o_yp = o_y * p_outpic->p[i_plane].i_visible_lines / p_outpic->p[Y_PLANE].i_visible_lines;
234             const int o_xp = o_x * p_outpic->p[i_plane].i_visible_pitch / p_outpic->p[Y_PLANE].i_visible_pitch;
235 
236             p_pic->p[i_plane].p_pixels += o_yp * p_pic->p[i_plane].i_pitch + o_xp;
237         }
238 
239         /* */
240         fmt_in = p_filter->fmt_in.video;
241         fmt_in.i_width  = fmt_in.i_visible_width  = (fmt_in.i_visible_width  * ZOOM_FACTOR / o_zoom) & ~1;
242         fmt_in.i_height = fmt_in.i_visible_height = (fmt_in.i_visible_height * ZOOM_FACTOR / o_zoom) & ~1;
243 
244         /* */
245         fmt_out = p_filter->fmt_out.video;
246         p_converted = image_Convert( p_sys->p_image, p_pic, &fmt_in, &fmt_out );
247         memcpy(p_pic->p, orig_planes, sizeof orig_planes);
248 
249         picture_CopyPixels( p_outpic, p_converted );
250 
251         picture_Release( p_converted );
252     }
253     else
254     {
255         picture_CopyPixels( p_outpic, p_pic );
256     }
257 
258     /* */
259     p_oyp = &p_outpic->p[Y_PLANE];
260     if( b_visible )
261     {
262         video_format_t fmt_out;
263 
264         /* image visualization */
265         fmt_out = p_filter->fmt_out.video;
266         fmt_out.i_width  = fmt_out.i_visible_width  = (fmt_out.i_visible_width /VIS_ZOOM) & ~1;
267         fmt_out.i_height = fmt_out.i_visible_height = (fmt_out.i_visible_height/VIS_ZOOM) & ~1;
268         p_converted = image_Convert( p_sys->p_image, p_pic,
269                                      &p_pic->format, &fmt_out );
270 
271         /* It will put only what can be copied at the top left */
272         picture_CopyVisiblePixels( p_outpic, p_converted );
273 
274         picture_Release( p_converted );
275 
276         /* white rectangle on visualization */
277         v_w = __MIN( fmt_out.i_visible_width  * ZOOM_FACTOR / o_zoom, fmt_out.i_visible_width - 1 );
278         v_h = __MIN( fmt_out.i_visible_height * ZOOM_FACTOR / o_zoom, fmt_out.i_visible_height - 1 );
279 
280         DrawRectangle( p_oyp->p_pixels, p_oyp->i_pitch,
281                        p_oyp->i_visible_pitch, p_oyp->i_visible_lines,
282                        o_x/VIS_ZOOM, o_y/VIS_ZOOM,
283                        v_w, v_h );
284 
285         /* */
286         v_h = fmt_out.i_visible_height + 1;
287     }
288     else
289     {
290         v_h = 1;
291     }
292 
293     /* print a small "VLC ZOOM" */
294 
295     if( b_visible || p_sys->i_last_activity + p_sys->i_hide_timeout > mdate() )
296         DrawZoomStatus( p_oyp->p_pixels, p_oyp->i_visible_pitch, p_oyp->i_pitch, p_oyp->i_lines,
297                         1, v_h, b_visible );
298 
299     if( b_visible )
300     {
301         /* zoom gauge */
302         memset( p_oyp->p_pixels + (v_h+9)*p_oyp->i_pitch, 0xff, 41 );
303         for( int y = v_h + 10; y < v_h + 90; y++ )
304         {
305             int i_width = v_h + 90 - y;
306             i_width = i_width * i_width / 160;
307             if( (80 - y + v_h)*ZOOM_FACTOR/10 < o_zoom )
308             {
309                 memset( p_oyp->p_pixels + y*p_oyp->i_pitch, 0xff, i_width );
310             }
311             else
312             {
313                 p_oyp->p_pixels[y*p_oyp->i_pitch] = 0xff;
314                 p_oyp->p_pixels[y*p_oyp->i_pitch + i_width - 1] = 0xff;
315             }
316         }
317     }
318 
319     return CopyInfoAndRelease( p_outpic, p_pic );
320 }
321 
DrawZoomStatus(uint8_t * pb_dst,int i_pitch,int i_width,int i_height,int i_offset_x,int i_offset_y,bool b_visible)322 static void DrawZoomStatus( uint8_t *pb_dst, int i_pitch, int i_width, int i_height,
323                             int i_offset_x, int i_offset_y, bool b_visible )
324 {
325     static const char *p_hide =
326         "X   X X      XXXX   XXXXX  XXX   XXX  XX XX   X   X XXXXX XXXX  XXXXXL"
327         "X   X X     X          X  X   X X   X X X X   X   X   X   X   X X    L"
328         " X X  X     X         X   X   X X   X X   X   XXXXX   X   X   X XXXX L"
329         " X X  X     X        X    X   X X   X X   X   X   X   X   X   X X    L"
330         "  X   XXXXX  XXXX   XXXXX  XXX   XXX  X   X   X   X XXXXX XXXX  XXXXXL";
331     static const char *p_show =
332         "X   X X      XXXX   XXXXX  XXX   XXX  XX XX    XXXX X   X  XXX  X   XL"
333         "X   X X     X          X  X   X X   X X X X   X     X   X X   X X   XL"
334         " X X  X     X         X   X   X X   X X   X    XXX  XXXXX X   X X X XL"
335         " X X  X     X        X    X   X X   X X   X       X X   X X   X X X XL"
336         "  X   XXXXX  XXXX   XXXXX  XXX   XXX  X   X   XXXX  X   X  XXX   X X L";
337     const char *p_draw = b_visible ? p_hide : p_show;
338 
339     for( int i = 0, x = i_offset_x, y = i_offset_y; p_draw[i] != '\0'; i++ )
340     {
341         if( p_draw[i] == 'X' )
342         {
343             if( x < i_width && y < i_height )
344                 pb_dst[y*i_pitch + x] = 0xff;
345             x++;
346         }
347         else if( p_draw[i] == ' ' )
348         {
349             x++;
350         }
351         else if( p_draw[i] == 'L' )
352         {
353             x = i_offset_x;
354             y++;
355         }
356     }
357 }
DrawRectangle(uint8_t * pb_dst,int i_pitch,int i_width,int i_height,int x,int y,int i_w,int i_h)358 static void DrawRectangle( uint8_t *pb_dst, int i_pitch, int i_width, int i_height,
359                            int x, int y, int i_w, int i_h )
360 {
361     if( x + i_w > i_width || y + i_h > i_height )
362         return;
363 
364     /* top line */
365     memset( &pb_dst[y * i_pitch + x], 0xff, i_w );
366 
367     /* left and right */
368     for( int dy = 1; dy < i_h-1; dy++ )
369     {
370         pb_dst[(y+dy) * i_pitch + x +     0] = 0xff;
371         pb_dst[(y+dy) * i_pitch + x + i_w-1] = 0xff;
372     }
373 
374     /* bottom line */
375     memset( &pb_dst[(y+i_h-1) * i_pitch + x], 0xff, i_w );
376 }
377 
Mouse(filter_t * p_filter,vlc_mouse_t * p_mouse,const vlc_mouse_t * p_old,const vlc_mouse_t * p_new)378 static int Mouse( filter_t *p_filter, vlc_mouse_t *p_mouse, const vlc_mouse_t *p_old, const vlc_mouse_t *p_new )
379 {
380     filter_sys_t *p_sys = p_filter->p_sys;
381     const video_format_t *p_fmt = &p_filter->fmt_in.video;
382 
383     /* */
384     const bool b_click = vlc_mouse_HasPressed( p_old, p_new, MOUSE_BUTTON_LEFT );
385     const bool b_pressed = vlc_mouse_IsLeftPressed( p_new );
386 
387     bool b_grab = false;
388 
389     /* Find the mouse position */
390     if( p_sys->b_visible )
391     {
392         const int i_visu_width  = p_fmt->i_visible_width  / VIS_ZOOM;
393         const int i_visu_height = p_fmt->i_visible_height / VIS_ZOOM;
394 
395         if( p_new->i_x >= 0 && p_new->i_x < i_visu_width &&
396             p_new->i_y >= 0 && p_new->i_y < i_visu_height )
397         {
398             /* Visualization */
399             if( b_pressed )
400             {
401                 const int v_w = p_fmt->i_visible_width  * ZOOM_FACTOR / p_sys->i_zoom;
402                 const int v_h = p_fmt->i_visible_height * ZOOM_FACTOR / p_sys->i_zoom;
403 
404                 p_sys->i_x = VLC_CLIP( p_new->i_x * VIS_ZOOM - v_w/2, 0,
405                                            (int)p_fmt->i_visible_width  - v_w - 1);
406                 p_sys->i_y = VLC_CLIP( p_new->i_y * VIS_ZOOM - v_h/2, 0,
407                                            (int)p_fmt->i_visible_height - v_h - 1);
408 
409                 b_grab = true;
410             }
411         }
412         else if( p_new->i_x >= 0 && p_new->i_x < 80 &&
413                  p_new->i_y >= i_visu_height &&
414                  p_new->i_y <  i_visu_height + 9 )
415         {
416             /* Hide text */
417             if( b_click )
418             {
419                 p_sys->b_visible = false;
420                 b_grab = true;
421             }
422         }
423         else if( p_new->i_x >= 0 &&
424                  p_new->i_x <= ( i_visu_height + 90 - p_new->i_y ) *
425                                ( i_visu_height + 90 - p_new->i_y ) / 160 &&
426                  p_new->i_y >= i_visu_height + 9 &&
427                  p_new->i_y <= i_visu_height + 90 )
428         {
429             /* Zoom gauge */
430             if( b_pressed )
431             {
432                 p_sys->i_zoom = __MAX( ZOOM_FACTOR,
433                                        (80 + i_visu_height - p_new->i_y + 2) *
434                                            ZOOM_FACTOR / 10 );
435 
436                 const int v_w = p_fmt->i_visible_width  * ZOOM_FACTOR / p_sys->i_zoom;
437                 const int v_h = p_fmt->i_visible_height * ZOOM_FACTOR / p_sys->i_zoom;
438                 p_sys->i_x = VLC_CLIP( p_sys->i_x, 0, (int)p_fmt->i_visible_width  - v_w - 1 );
439                 p_sys->i_y = VLC_CLIP( p_sys->i_y, 0, (int)p_fmt->i_visible_height - v_h - 1 );
440 
441                 b_grab = true;
442             }
443         }
444     }
445     else
446     {
447         if( p_new->i_x >= 0 && p_new->i_x <  80 &&
448             p_new->i_y >= 0 && p_new->i_y <= 10 )
449         {
450             /* Show text */
451             if( b_click )
452             {
453                 p_sys->b_visible = true;
454                 b_grab = true;
455             }
456         }
457     }
458 
459     if( vlc_mouse_HasMoved( p_old, p_new ) )
460         p_sys->i_last_activity = mdate();
461 
462     if( b_grab )
463         return VLC_EGENERIC;
464 
465     /* */
466     *p_mouse = *p_new;
467     p_mouse->i_x = p_sys->i_x + p_new->i_x * ZOOM_FACTOR / p_sys->i_zoom;
468     p_mouse->i_y = p_sys->i_y + p_new->i_y * ZOOM_FACTOR / p_sys->i_zoom;
469     return VLC_SUCCESS;
470 }
471 
472