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