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