]> git.sesse.net Git - vlc/blob - modules/video_filter/magnify.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / video_filter / magnify.c
1 /*****************************************************************************
2  * magnify.c : Magnify/Zoom interactive effect
3  *****************************************************************************
4  * Copyright (C) 2005-2009 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 #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 "filter_picture.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45
46 vlc_module_begin ()
47     set_description( N_("Magnify/Zoom interactive video filter") )
48     set_shortname( N_( "Magnify" ))
49     set_capability( "video filter2", 0 )
50     set_category( CAT_VIDEO )
51     set_subcategory( SUBCAT_VIDEO_VFILTER )
52
53     set_callbacks( Create, Destroy )
54 vlc_module_end ()
55
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static picture_t *Filter( filter_t *, picture_t * );
61 static int Mouse( filter_t *, vlc_mouse_t *, const vlc_mouse_t *, const vlc_mouse_t * );
62
63 /* */
64 static void DrawZoomStatus( uint8_t *, int i_pitch, int i_width, int i_height,
65                             int i_offset_x, int i_offset_y, bool b_visible );
66 static void DrawRectangle( uint8_t *, int i_pitch, int i_width, int i_height,
67                            int x, int y, int i_w, int i_h );
68
69 /* */
70 struct filter_sys_t
71 {
72     image_handler_t *p_image;
73
74     int64_t i_hide_timeout;
75
76     int i_zoom; /* zoom level in percent */
77     int i_x, i_y; /* top left corner coordinates in original image */
78
79     bool b_visible; /* is "interface" visible ? */
80
81     int64_t i_last_activity;
82 };
83
84 #define VIS_ZOOM 4
85 #define ZOOM_FACTOR 8
86
87 /*****************************************************************************
88  * Create:
89  *****************************************************************************/
90 static int Create( vlc_object_t *p_this )
91 {
92     filter_t *p_filter = (filter_t *)p_this;
93     filter_sys_t *p_sys;
94
95     /* */
96     switch( p_filter->fmt_in.i_codec )
97     {
98     CASE_PLANAR_YUV
99     case VLC_CODEC_GREY:
100         break;
101     default:
102         msg_Err( p_filter, "Unsupported chroma" );
103         return VLC_EGENERIC;
104     }
105     if( !es_format_IsSimilar( &p_filter->fmt_in, &p_filter->fmt_out ) )
106     {
107         msg_Err( p_filter, "Input and output format does not match" );
108         return VLC_EGENERIC;
109     }
110
111     /* Allocate structure */
112     p_filter->p_sys = p_sys = malloc( sizeof( *p_sys ) );
113     if( !p_filter->p_sys )
114         return VLC_ENOMEM;
115
116     p_sys->p_image = image_HandlerCreate( p_filter );
117     if( !p_sys->p_image )
118     {
119         free( p_sys );
120         return VLC_EGENERIC;
121     }
122
123     p_sys->i_x = 0;
124     p_sys->i_y = 0;
125     p_sys->i_zoom = 2*ZOOM_FACTOR;
126     p_sys->b_visible = true;
127     p_sys->i_last_activity = mdate();
128     p_sys->i_hide_timeout = 1000 * var_CreateGetInteger( p_filter, "mouse-hide-timeout" ); /* FIXME */
129
130     /* */
131     p_filter->pf_video_filter = Filter;
132     p_filter->pf_video_mouse = Mouse;
133     return VLC_SUCCESS;
134 }
135
136 /*****************************************************************************
137  * Destroy:
138  *****************************************************************************/
139 static void Destroy( vlc_object_t *p_this )
140 {
141     filter_t *p_filter = (filter_t *)p_this;
142     filter_sys_t *p_sys = p_filter->p_sys;
143
144     image_HandlerDelete( p_sys->p_image );
145
146     free( p_sys );
147 }
148
149 /*****************************************************************************
150  * Render: displays previously rendered output
151  *****************************************************************************/
152 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
153 {
154     filter_sys_t *p_sys = p_filter->p_sys;
155     picture_t *p_outpic;
156
157     int v_w, v_h;
158     picture_t *p_converted;
159     plane_t *p_oyp;
160     int i_plane;
161
162     p_outpic = filter_NewPicture( p_filter );
163     if( !p_outpic )
164     {
165         picture_Release( p_pic );
166         return NULL;
167     }
168
169     /* */
170     const bool b_visible = p_sys->b_visible;
171     const int o_x = p_sys->i_x;
172     const int o_y = p_sys->i_y;
173     const int o_zoom = p_sys->i_zoom;
174
175     /* background magnified image */
176     if( o_zoom != ZOOM_FACTOR )
177     {
178         video_format_t fmt_in;
179         video_format_t fmt_out;
180         picture_t crop;
181
182         crop = *p_pic;
183         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
184         {
185             const int o_yp = o_y * p_outpic->p[i_plane].i_lines / p_outpic->p[Y_PLANE].i_lines;
186             const int o_xp = o_x * p_outpic->p[i_plane].i_pitch / p_outpic->p[Y_PLANE].i_pitch;
187
188             crop.p[i_plane].p_pixels += o_yp * p_pic->p[i_plane].i_pitch + o_xp;
189         }
190
191         /* */
192         fmt_in = p_filter->fmt_in.video;
193         fmt_in.i_width  = (fmt_in.i_width  * ZOOM_FACTOR / o_zoom) & ~1;
194         fmt_in.i_height = (fmt_in.i_height * ZOOM_FACTOR / o_zoom) & ~1;
195
196         /* */
197         fmt_out = p_filter->fmt_out.video;
198
199         p_converted = image_Convert( p_sys->p_image, &crop, &fmt_in, &fmt_out );
200
201         picture_CopyPixels( p_outpic, p_converted );
202
203         picture_Release( p_converted );
204     }
205     else
206     {
207         picture_CopyPixels( p_outpic, p_pic );
208     }
209
210     /* */
211     p_oyp = &p_outpic->p[Y_PLANE];
212     if( b_visible )
213     {
214         video_format_t fmt_out;
215
216         /* image visualization */
217         fmt_out = p_filter->fmt_out.video;
218         fmt_out.i_width  = (fmt_out.i_width /VIS_ZOOM) & ~1;
219         fmt_out.i_height = (fmt_out.i_height/VIS_ZOOM) & ~1;
220         p_converted = image_Convert( p_sys->p_image, p_pic,
221                                      &p_pic->format, &fmt_out );
222
223         /* It will put only what can be copied at the top left */
224         picture_CopyPixels( p_outpic, p_converted );
225
226         picture_Release( p_converted );
227
228         /* white rectangle on visualization */
229         v_w = __MIN( fmt_out.i_width  * ZOOM_FACTOR / o_zoom, fmt_out.i_width - 1 );
230         v_h = __MIN( fmt_out.i_height * ZOOM_FACTOR / o_zoom, fmt_out.i_height - 1 );
231
232         DrawRectangle( p_oyp->p_pixels, p_oyp->i_pitch,
233                        p_oyp->i_pitch, p_oyp->i_lines,
234                        o_x/VIS_ZOOM, o_y/VIS_ZOOM,
235                        v_w, v_h );
236
237         /* */
238         v_h = fmt_out.i_height + 1;
239     }
240     else
241     {
242         v_h = 1;
243     }
244
245     /* print a small "VLC ZOOM" */
246
247     if( b_visible || p_sys->i_last_activity + p_sys->i_hide_timeout > mdate() )
248         DrawZoomStatus( p_oyp->p_pixels, p_oyp->i_pitch, p_oyp->i_pitch, p_oyp->i_lines,
249                         1, v_h, b_visible );
250
251     if( b_visible )
252     {
253         /* zoom gauge */
254         memset( p_oyp->p_pixels + (v_h+9)*p_oyp->i_pitch, 0xff, 41 );
255         for( int y = v_h + 10; y < v_h + 90; y++ )
256         {
257             int i_width = v_h + 90 - y;
258             i_width = i_width * i_width / 160;
259             if( (80 - y + v_h)*ZOOM_FACTOR/10 < o_zoom )
260             {
261                 memset( p_oyp->p_pixels + y*p_oyp->i_pitch, 0xff, i_width );
262             }
263             else
264             {
265                 p_oyp->p_pixels[y*p_oyp->i_pitch] = 0xff;
266                 p_oyp->p_pixels[y*p_oyp->i_pitch + i_width - 1] = 0xff;
267             }
268         }
269     }
270
271     return CopyInfoAndRelease( p_outpic, p_pic );
272 }
273
274 static void DrawZoomStatus( uint8_t *pb_dst, int i_pitch, int i_width, int i_height,
275                             int i_offset_x, int i_offset_y, bool b_visible )
276 {
277     static const char *p_hide =
278         "X   X X      XXXX   XXXXX  XXX   XXX  XX XX   X   X XXXXX XXXX  XXXXXL"
279         "X   X X     X          X  X   X X   X X X X   X   X   X   X   X X    L"
280         " X X  X     X         X   X   X X   X X   X   XXXXX   X   X   X XXXX L"
281         " X X  X     X        X    X   X X   X X   X   X   X   X   X   X X    L"
282         "  X   XXXXX  XXXX   XXXXX  XXX   XXX  X   X   X   X XXXXX XXXX  XXXXXL";
283     static const char *p_show = 
284         "X   X X      XXXX   XXXXX  XXX   XXX  XX XX    XXXX X   X  XXX  X   XL"
285         "X   X X     X          X  X   X X   X X X X   X     X   X X   X X   XL"
286         " X X  X     X         X   X   X X   X X   X    XXX  XXXXX X   X X X XL"
287         " X X  X     X        X    X   X X   X X   X       X X   X X   X X X XL"
288         "  X   XXXXX  XXXX   XXXXX  XXX   XXX  X   X   XXXX  X   X  XXX   X X L";
289     const char *p_draw = b_visible ? p_hide : p_show;
290     int i, y, x;
291
292     for( i = 0, x = i_offset_x, y = i_offset_y; p_draw[i] != '\0'; i++ )
293     {
294         if( p_draw[i] == 'X' )
295         {
296             if( x < i_width && y < i_height )
297                 pb_dst[y*i_pitch + x] = 0xff;
298             x++;
299         }
300         else if( p_draw[i] == ' ' )
301         {
302             x++;
303         }
304         else if( p_draw[i] == 'L' )
305         {
306             x = i_offset_x;
307             y++;
308         }
309     }
310 }
311 static void DrawRectangle( uint8_t *pb_dst, int i_pitch, int i_width, int i_height,
312                            int x, int y, int i_w, int i_h )
313 {
314     int dy;
315
316     if( x + i_w > i_width || y + i_h > i_height )
317         return;
318
319     /* top line */
320     vlc_memset( &pb_dst[y * i_pitch + x], 0xff, i_w );
321
322     /* left and right */
323     for( dy = 1; dy < i_h-1; dy++ )
324     {
325         pb_dst[(y+dy) * i_pitch + x +     0] = 0xff;
326         pb_dst[(y+dy) * i_pitch + x + i_w-1] = 0xff;
327     }
328
329     /* bottom line */
330     vlc_memset( &pb_dst[(y+i_h-1) * i_pitch + x], 0xff, i_w );
331 }
332
333 static int Mouse( filter_t *p_filter, vlc_mouse_t *p_mouse, const vlc_mouse_t *p_old, const vlc_mouse_t *p_new )
334 {
335     filter_sys_t *p_sys = p_filter->p_sys;
336     const video_format_t *p_fmt = &p_filter->fmt_in.video;
337
338     /* */
339     const bool b_click = vlc_mouse_HasPressed( p_old, p_new, MOUSE_BUTTON_LEFT );
340     const bool b_pressed = vlc_mouse_IsLeftPressed( p_new );
341
342     bool b_grab = false;
343
344     /* Find the mouse position */
345     if( p_sys->b_visible )
346     {
347         const int i_visu_width  = p_fmt->i_width  / VIS_ZOOM;
348         const int i_visu_height = p_fmt->i_height / VIS_ZOOM;
349
350         if( p_new->i_x >= 0 && p_new->i_x < i_visu_width &&
351             p_new->i_y >= 0 && p_new->i_y < i_visu_height )
352         {
353             /* Visualization */
354             if( b_pressed )
355             {
356                 const int v_w = p_fmt->i_width  * ZOOM_FACTOR / p_sys->i_zoom;
357                 const int v_h = p_fmt->i_height * ZOOM_FACTOR / p_sys->i_zoom;
358
359                 p_sys->i_x = __MIN( __MAX( p_new->i_x * VIS_ZOOM - v_w/2, 0 ),
360                                            (int)p_fmt->i_width  - v_w - 1);
361                 p_sys->i_y = __MIN( __MAX( p_new->i_y * VIS_ZOOM - v_h/2, 0 ),
362                                            (int)p_fmt->i_height - v_h - 1);
363
364                 b_grab = true;
365             }
366         }
367         else if( p_new->i_x >= 0 && p_new->i_x < 80 &&
368                  p_new->i_y >= i_visu_height &&
369                  p_new->i_y <  i_visu_height + 9 )
370         {
371             /* Hide text */
372             if( b_click )
373             {
374                 p_sys->b_visible = false;
375                 b_grab = true;
376             }
377         }
378         else if( p_new->i_x >= 0 &&
379                  p_new->i_x <= ( i_visu_height + 90 - p_new->i_y ) *
380                                ( i_visu_height + 90 - p_new->i_y ) / 160 &&
381                  p_new->i_y >= i_visu_height + 9 &&
382                  p_new->i_y <= i_visu_height + 90 )
383         {
384             /* Zoom gauge */
385             if( b_pressed )
386             {
387                 p_sys->i_zoom = __MAX( ZOOM_FACTOR,
388                                        (80 + i_visu_height - p_new->i_y + 2) *
389                                            ZOOM_FACTOR / 10 );
390
391                 const int v_w = p_fmt->i_width  * ZOOM_FACTOR / p_sys->i_zoom;
392                 const int v_h = p_fmt->i_height * ZOOM_FACTOR / p_sys->i_zoom;
393                 p_sys->i_x = __MAX( __MIN( p_sys->i_x, (int)p_fmt->i_width  - v_w - 1 ), 0 );
394                 p_sys->i_y = __MAX( __MIN( p_sys->i_y, (int)p_fmt->i_height - v_h - 1 ), 0 );
395
396                 b_grab = true;
397             }
398         }
399     }
400     else
401     {
402         if( p_new->i_x >= 0 && p_new->i_x <  80 &&
403             p_new->i_y >= 0 && p_new->i_y <= 10 )
404         {
405             /* Show text */
406             if( b_click )
407             {
408                 p_sys->b_visible = true;
409                 b_grab = true;
410             }
411         }
412     }
413
414     if( vlc_mouse_HasMoved( p_old, p_new ) )
415         p_sys->i_last_activity = mdate();
416
417     if( b_grab )
418         return VLC_EGENERIC;
419
420     /* */
421     *p_mouse = *p_new;
422     p_mouse->i_x = p_sys->i_x + p_new->i_x * ZOOM_FACTOR / p_sys->i_zoom;
423     p_mouse->i_y = p_sys->i_y + p_new->i_y * ZOOM_FACTOR / p_sys->i_zoom;
424     return VLC_SUCCESS;
425 }
426