]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/magnify.c
Use vlc_memset/vlc_memcpy
[vlc] / modules / video_filter / magnify.c
index e5eca619c19d7ea410c3220f002e463e3779d154..0d6859f3d12e7c8006210bd4fb2b8ad05ab3cb2a 100644 (file)
@@ -2,7 +2,7 @@
  * magnify.c : Magnify/Zoom interactive effect
  *****************************************************************************
  * Copyright (C) 2005 the VideoLAN team
- * $Id$
+ * $Id$
  *
  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
  *
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
 #include <vlc/vlc.h>
-#include <vlc/vout.h>
+#include <vlc_vout.h>
 
 #include <math.h>
 
 #include "filter_common.h"
+#include "filter_picture.h"
+
 #include "vlc_image.h"
 #include "vlc_input.h"
 #include "vlc_playlist.h"
@@ -57,7 +61,7 @@ static int  MouseEvent   ( vlc_object_t *, char const *,
  *****************************************************************************/
 vlc_module_begin();
     set_description( _("Magnify/Zoom interactive video filter") );
-    set_shortname( N_( "Magnify" ));
+    set_shortname( _( "Magnify" ));
     set_capability( "video filter", 0 );
     set_category( CAT_VIDEO );
     set_subcategory( SUBCAT_VIDEO_VFILTER );
@@ -77,7 +81,7 @@ struct vout_sys_t
     int i_zoom; /* zoom level in percent */
     int i_x, i_y; /* top left corner coordinates in original image */
 
-    vlc_bool_t b_visible; /* is "interface" visible ? */
+    bool b_visible; /* is "interface" visible ? */
 };
 
 /*****************************************************************************
@@ -95,6 +99,16 @@ static int Create( vlc_object_t *p_this )
 {
     vout_thread_t *p_vout = (vout_thread_t *)p_this;
 
+    switch( p_vout->fmt_in.i_chroma )
+    {
+        CASE_PLANAR_YUV
+        case VLC_FOURCC('G','R','E','Y'):
+            break;
+        default:
+            msg_Err( p_vout, "Unsupported chroma" );
+            return VLC_EGENERIC;
+    }
+
     /* Allocate structure */
     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
     if( p_vout->p_sys == NULL )
@@ -122,8 +136,9 @@ static int Init( vout_thread_t *p_vout )
 {
     int i_index;
     picture_t *p_pic;
-    video_format_t fmt = {0};
+    video_format_t fmt;
 
+    memset( &fmt, 0, sizeof(video_format_t) );
     I_OUTPUTPICTURES = 0;
 
     /* Initialize the output structure */
@@ -150,8 +165,9 @@ static int Init( vout_thread_t *p_vout )
 #define VIS_ZOOM 4
     p_vout->p_sys->i_x = 0;
     p_vout->p_sys->i_y = 0;
-#define ZOOM_FACTOR 100
-    p_vout->p_sys->i_zoom = 200;
+#define ZOOM_FACTOR 8
+    p_vout->p_sys->i_zoom = 2*ZOOM_FACTOR;
+    p_vout->p_sys->b_visible = true;
 
     var_AddCallback( p_vout->p_sys->p_vout, "mouse-x", MouseEvent, p_vout );
     var_AddCallback( p_vout->p_sys->p_vout, "mouse-y", MouseEvent, p_vout );
@@ -217,10 +233,12 @@ static void Render( vout_thread_t *p_vout, picture_t *p_pic )
     int o_zoom = p_vout->p_sys->i_zoom;
     int x,y,o_yp,o_xp;
     int v_w, v_h;
-    video_format_t fmt_out = {0};
+    video_format_t fmt_out;
     picture_t *p_converted;
     plane_t *p_oyp=NULL;
+    int i_plane;
 
+    memset( &fmt_out, 0, sizeof(video_format_t) );
     /* This is a new frame. Get a structure from the video_output. */
     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
               == NULL )
@@ -239,34 +257,113 @@ static void Render( vout_thread_t *p_vout, picture_t *p_pic )
     /* background magnified image */
     if( o_zoom != ZOOM_FACTOR )
     {
-#define magnify( plane ) \
-    o_yp = o_y*p_outpic->p[plane].i_lines/p_outpic->p[Y_PLANE].i_lines; \
-    o_xp = o_x*p_outpic->p[plane].i_pitch/p_outpic->p[Y_PLANE].i_pitch; \
-    for( y=0; y<p_outpic->p[plane].i_visible_lines; y++ ) \
-    { \
-        for( x=0; x<p_outpic->p[plane].i_visible_pitch; x++ ) \
-        { \
-            p_outpic->p[plane].p_pixels[y*p_outpic->p[plane].i_pitch+x] = \
-                p_pic->p[plane].p_pixels[ \
-                    ( o_yp + y*ZOOM_FACTOR/o_zoom )*p_outpic->p[plane].i_pitch \
-                    + o_xp + x*ZOOM_FACTOR/o_zoom \
-                ]; \
-        } \
-    }
-    magnify( Y_PLANE );
-    magnify( U_PLANE );
-    magnify( V_PLANE );
-#undef magnify
+        for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
+        {
+            o_yp = o_y*p_outpic->p[i_plane].i_lines
+                   /p_outpic->p[Y_PLANE].i_lines;
+            o_xp = o_x*p_outpic->p[i_plane].i_pitch
+                   /p_outpic->p[Y_PLANE].i_pitch;
+            int i_pitch = p_outpic->p[i_plane].i_pitch;
+#if 0
+            int o_zoom2 = o_zoom*o_zoom;
+            int o_zoom3 = o_zoom*o_zoom*o_zoom;
+            int o_zoom4 = o_zoom*o_zoom*o_zoom*o_zoom;
+            int o_zoom6 = o_zoom*o_zoom*o_zoom * o_zoom*o_zoom*o_zoom;
+#endif
+            for( y=0; y<p_outpic->p[i_plane].i_visible_lines; y++ )
+            {
+                for( x=0; x<p_outpic->p[i_plane].i_visible_pitch; x++ )
+                {
+#if 0
+                    /* Nearest neighbor */
+                    int nx = o_xp + x*ZOOM_FACTOR/o_zoom;
+                    int ny = o_yp + y*ZOOM_FACTOR/o_zoom;
+                    p_outpic->p[i_plane].p_pixels[y*i_pitch+x] =
+                        p_pic->p[i_plane].p_pixels[ny*i_pitch+nx];
+#elif 1
+                    /* Bi-linear */
+                    int nx_real = o_xp*o_zoom + x*ZOOM_FACTOR;
+                    int ny_real = o_yp*o_zoom + y*ZOOM_FACTOR;
+                    int nx = nx_real/o_zoom;
+                    int ny = ny_real/o_zoom;
+                    int wtl = ((nx+1)*o_zoom-nx_real)*((ny+1)*o_zoom-ny_real);
+                    int wtr = (nx_real-nx*o_zoom)*((ny+1)*o_zoom-ny_real);
+                    int wbl = ((nx+1)*o_zoom-nx_real)*(ny_real-ny*o_zoom);
+                    int wbr = (nx_real-nx*o_zoom)*(ny_real-ny*o_zoom);
+                    p_outpic->p[i_plane].p_pixels[y*i_pitch+x] =
+                        ( wtl*p_pic->p[i_plane].p_pixels[ny*i_pitch+nx]
+                        + wtr*p_pic->p[i_plane].p_pixels[ny*i_pitch+(nx+1)]
+                        + wbl*p_pic->p[i_plane].p_pixels[(ny+1)*i_pitch+nx]
+                        + wbr*p_pic->p[i_plane].p_pixels[(ny+1)*i_pitch+(nx+1)]
+                        ) / (o_zoom*o_zoom);
+#else
+                    /* Bi-cubic */
+                    /* FIXME: doesn't work */
+                    /* \Sigma_{i=0..3} \Sigma_{j=0..3} \alpha_{i,j} x^i y^j
+                     * 16 \alpha_{i,j} so we need to use the 16 nearest pixels
+                     *
+                     * In fact we should be able to write it as:
+                     * \Pi_{i=1..16} [ \Pi_{j!=i} ( x - x_j ) * ( y - y_j ) ]
+                     *   * z_i / [ \Pi_{j!=i} ( x_i - x_j ) * ( y_i - y_j ) ]
+                     * We also have to make sure that we don't use any
+                     * x_j == x_i or y_j == y_i in the \Pi on j (else we get
+                     * a 0/0 which kind of sucks) .
+                     */
+                    uint8_t *p = p_pic->p[i_plane].p_pixels;
+                    int nx_real = o_xp*o_zoom + x*ZOOM_FACTOR;
+                    int ny_real = o_yp*o_zoom + y*ZOOM_FACTOR;
+
+                    int nx = nx_real/o_zoom;
+                    int ny = ny_real/o_zoom;
+
+                    int xi, yi, xj, yj;
+                    int my = __MAX( ny-1, 0 );
+                    int mx = __MAX( nx-1, 0 );
+                    //p_outpic->p[i_plane].i_visible_lines - 4
+                    //p_outpic->p[i_plane].i_visible_pitch - 4
+                    int v = 1;
+                    for( yi = my; yi <= my+3; yi++ )
+                    {
+                        int numy = 1;
+                        int deny = 1;
+                        /*
+                        for( yj = my; yj <= my+3; yj++ )
+                        {
+                            if( yj != yi )
+                            {
+                                numy *= (ny_real-yj*o_zoom);
+                                deny *= (yi - yj);
+                            }
+                        }
+                        numy /= o_zoom2;*/
+                        for( xi = mx; xi <= mx+3; xi++ )
+                        {
+                            int num = numy;
+                            int den = deny;
+                            for( xj = mx; xj <= mx+3; xj++ )
+                            {
+                                if( xj != xi )
+                                {
+                                    num *= (nx_real-xj*o_zoom);
+                                    den *= (xi - xj);
+                                }
+                            }
+                            v = ( v * (p[yi*i_pitch+xi] * num) ) / ( den * o_zoom3 );
+                        }
+                    }
+                    p_outpic->p[i_plane].p_pixels[y*i_pitch+x] = v;
+#endif
+                }
+            }
+        }
     }
     else
     {
-#define copy( plane ) \
-        memcpy( p_outpic->p[plane].p_pixels, p_pic->p[plane].p_pixels, \
-            p_outpic->p[plane].i_lines * p_outpic->p[plane].i_pitch );
-        copy( Y_PLANE );
-        copy( U_PLANE );
-        copy( V_PLANE );
-#undef copy
+        for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
+        {
+        vlc_memcpy( p_outpic->p[i_plane].p_pixels, p_pic->p[i_plane].p_pixels,
+            p_outpic->p[i_plane].i_lines * p_outpic->p[i_plane].i_pitch );
+        }
     }
 
     if( p_vout->p_sys->b_visible )
@@ -277,26 +374,25 @@ static void Render( vout_thread_t *p_vout, picture_t *p_pic )
         fmt_out.i_height = p_vout->render.i_height/VIS_ZOOM;
         p_converted = image_Convert( p_vout->p_sys->p_image, p_pic,
                                      &(p_pic->format), &fmt_out );
-    #define copyimage( plane ) \
-        for( y=0; y<p_converted->p[plane].i_visible_lines; y++) \
-        { \
-            memcpy( p_outpic->p[plane].p_pixels+y*p_outpic->p[plane].i_pitch, \
-            p_converted->p[plane].p_pixels+y*p_converted->p[plane].i_pitch, \
-            p_converted->p[plane].i_visible_pitch ); \
+        for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
+        {
+            for( y=0; y<p_converted->p[i_plane].i_visible_lines; y++)
+            {
+                vlc_memcpy(
+                p_outpic->p[i_plane].p_pixels+y*p_outpic->p[i_plane].i_pitch,
+                p_converted->p[i_plane].p_pixels+y*p_converted->p[i_plane].i_pitch,
+                p_converted->p[i_plane].i_visible_pitch );
+            }
         }
-        copyimage( Y_PLANE );
-        copyimage( U_PLANE );
-        copyimage( V_PLANE );
-    #undef copyimage
         p_converted->pf_release( p_converted );
 
         /* white rectangle on visualization */
         v_w = p_oyp->i_pitch*ZOOM_FACTOR/(VIS_ZOOM*o_zoom);
         v_h = (o_y+p_oyp->i_lines*ZOOM_FACTOR/o_zoom)/VIS_ZOOM;
         /* top line */
-        memset( p_oyp->p_pixels
-                + o_y/VIS_ZOOM*p_oyp->i_pitch
-                + o_x/VIS_ZOOM, 0xff, v_w+1 );
+        vlc_memset( p_oyp->p_pixels
+                                     + o_y/VIS_ZOOM*p_oyp->i_pitch
+                                     + o_x/VIS_ZOOM, 0xff, v_w+1 );
 
         for( y = o_y/VIS_ZOOM+1; y < v_h; y++ )
         {
@@ -310,9 +406,9 @@ static void Render( vout_thread_t *p_vout, picture_t *p_pic )
             ] = 0xff;
         }
         /* bottom line */
-        memset( p_oyp->p_pixels
-                + v_h*p_oyp->i_pitch
-                + o_x/VIS_ZOOM, 0xff, v_w+1 );
+        vlc_memset( p_oyp->p_pixels
+                                     + v_h*p_oyp->i_pitch
+                                     + o_x/VIS_ZOOM, 0xff, v_w+1 );
 
         /* */
         v_h = p_oyp->i_lines/VIS_ZOOM;
@@ -353,18 +449,14 @@ o o X o o o X X X X X o o X X X X o o o X X X X X o o X X X o o o X X X o o X o
     if( p_vout->p_sys->b_visible )
     {
         /* zoom gauge */
-        memset( p_oyp->p_pixels
-                    + (v_h+9)*p_oyp->i_pitch,
-                    0xff, 41 );
+        vlc_memset( p_oyp->p_pixels + (v_h+9)*p_oyp->i_pitch, 0xff, 41 );
         for( y = v_h + 10; y < v_h + 90; y++ )
         {
             int width = v_h + 90 - y;
             width = (width*width)/160;
             if( (80 - y + v_h)*10 < o_zoom )
             {
-                memset( p_oyp->p_pixels
-                    + y*p_oyp->i_pitch,
-                    0xff, width );
+                vlc_memset( p_oyp->p_pixels + y*p_oyp->i_pitch, 0xff, width );
             }
             else
             {
@@ -383,6 +475,7 @@ o o X o o o X X X X X o o X X X X o o o X X X X X o o X X X o o o X X X o o X o
 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
+    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
     var_Set( (vlc_object_t *)p_data, psz_var, newval );
 
     return VLC_SUCCESS;
@@ -394,6 +487,7 @@ static int SendEvents( vlc_object_t *p_this, char const *psz_var,
 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
+    VLC_UNUSED(p_data); VLC_UNUSED(oldval);
     vout_thread_t *p_vout = (vout_thread_t *)p_this;
     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
     return VLC_SUCCESS;
@@ -405,6 +499,7 @@ static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
+    VLC_UNUSED(p_this);
     vout_thread_t *p_vout = (vout_thread_t*)p_data;
     vlc_value_t vald,valx,valy;
 
@@ -450,7 +545,7 @@ static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
                 && mouse&MOUSE_CLICKED )
             {
             /* mouse is over the "VLC ZOOM HIDE" text */
-                p_vout->p_sys->b_visible = VLC_FALSE;
+                p_vout->p_sys->b_visible = false;
             }
             else if(    (int)p_vout->output.i_height/VIS_ZOOM + 9 <= valy.i_int
                      && valy.i_int <= (int)p_vout->output.i_height/VIS_ZOOM + 90
@@ -481,7 +576,7 @@ static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
                 && valy.i_int <= 10 && mouse&MOUSE_CLICKED )
             {
             /* mouse is over the "VLC ZOOM SHOW" text */
-                p_vout->p_sys->b_visible = VLC_TRUE;
+                p_vout->p_sys->b_visible = true;
             }
             else if( mouse&MOUSE_MOVE_X && !(mouse&MOUSE_CLICKED) )
             {