From 0e0224913f327c303797439fa73d023a1c39a11f Mon Sep 17 00:00:00 2001 From: michael Date: Tue, 5 Nov 2002 13:29:10 +0000 Subject: [PATCH] perspective correcture filter git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@8113 b3059339-0415-0410-9bf9-f77b7e298cf2 --- DOCS/tech/vop.txt | 4 + libmpcodecs/Makefile | 2 +- libmpcodecs/vf.c | 2 + libmpcodecs/vf_perspective.c | 341 +++++++++++++++++++++++++++++++++++ 4 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 libmpcodecs/vf_perspective.c diff --git a/DOCS/tech/vop.txt b/DOCS/tech/vop.txt index 0b898c274b..fbb03a3969 100644 --- a/DOCS/tech/vop.txt +++ b/DOCS/tech/vop.txt @@ -209,3 +209,7 @@ Current plugins: radius blur filter strength (~0.1-5.0) (slower if larger) strength (0.0-1.0) -> blur, (-1.0-0.0) -> sharpen threshold 0 -> filter all, (0-30) -> filter flat areas, (-30-0) -> filter edges + +-vop perspective=x0:y0:x1:y1:x2:y2:x3:y3:t + x0,y0,... coordinates of the topleft, topright, bottomleft, bottomright corners + t 0-> linear, 1->cubic resampling diff --git a/libmpcodecs/Makefile b/libmpcodecs/Makefile index 26478252a7..897a0cda95 100644 --- a/libmpcodecs/Makefile +++ b/libmpcodecs/Makefile @@ -14,7 +14,7 @@ VIDEO_SRCS_NAT=vd_null.c vd_cinepak.c vd_qtrpza.c vd_raw.c vd_msvidc.c vd_fli.c VIDEO_SRCS_OPT=vd_realvid.c vd_ffmpeg.c vd_dshow.c vd_vfw.c vd_vfwex.c vd_odivx.c vd_divx4.c vd_xanim.c vd_xvid.c vd_libdv.c VIDEO_SRCS=dec_video.c vd.c $(VIDEO_SRCS_NAT) $(VIDEO_SRCS_LIB) $(VIDEO_SRCS_OPT) -VFILTER_SRCS=vf.c vf_vo.c vf_crop.c vf_expand.c vf_pp.c vf_scale.c vf_format.c vf_yuy2.c vf_flip.c vf_rgb2bgr.c vf_rotate.c vf_mirror.c vf_palette.c vf_lavc.c vf_dvbscale.c vf_cropdetect.c vf_test.c vf_noise.c vf_yvu9.c vf_rectangle.c vf_lavcdeint.c vf_eq.c vf_eq2.c vf_halfpack.c vf_dint.c vf_1bpp.c vf_bmovl.c vf_2xsai.c vf_unsharp.c vf_swapuv.c vf_il.c vf_boxblur.c vf_sab.c vf_smartblur.c +VFILTER_SRCS=vf.c vf_vo.c vf_crop.c vf_expand.c vf_pp.c vf_scale.c vf_format.c vf_yuy2.c vf_flip.c vf_rgb2bgr.c vf_rotate.c vf_mirror.c vf_palette.c vf_lavc.c vf_dvbscale.c vf_cropdetect.c vf_test.c vf_noise.c vf_yvu9.c vf_rectangle.c vf_lavcdeint.c vf_eq.c vf_eq2.c vf_halfpack.c vf_dint.c vf_1bpp.c vf_bmovl.c vf_2xsai.c vf_unsharp.c vf_swapuv.c vf_il.c vf_boxblur.c vf_sab.c vf_smartblur.c vf_perspective.c ENCODER_SRCS=ve.c ve_divx4.c ve_lavc.c ve_vfw.c ve_rawrgb.c ve_libdv.c ve_xvid.c NATIVE_SRCS=native/RTjpegN.c native/cinepak.c native/cyuv.c native/fli.c native/minilzo.c native/msvidc.c native/nuppelvideo.c native/qtrle.c native/qtrpza.c native/qtsmc.c native/roqav.c native/xa_gsm.c native/svq1.c diff --git a/libmpcodecs/vf.c b/libmpcodecs/vf.c index de28754dcc..d348a506a8 100644 --- a/libmpcodecs/vf.c +++ b/libmpcodecs/vf.c @@ -51,6 +51,7 @@ extern vf_info_t vf_info_il; extern vf_info_t vf_info_boxblur; extern vf_info_t vf_info_sab; extern vf_info_t vf_info_smartblur; +extern vf_info_t vf_info_perspective; char** vo_plugin_args=(char**) NULL; @@ -95,6 +96,7 @@ static vf_info_t* filter_list[]={ &vf_info_boxblur, &vf_info_sab, &vf_info_smartblur, + &vf_info_perspective, NULL }; diff --git a/libmpcodecs/vf_perspective.c b/libmpcodecs/vf_perspective.c new file mode 100644 index 0000000000..30ee17fcd3 --- /dev/null +++ b/libmpcodecs/vf_perspective.c @@ -0,0 +1,341 @@ +/* + Copyright (C) 2002 Michael Niedermayer + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + 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-1307 USA +*/ + +#include +#include +#include +#include +#include +#include + +#include "../config.h" +#include "../mp_msg.h" + +#ifdef HAVE_MALLOC_H +#include +#endif + +#include "img_format.h" +#include "mp_image.h" +#include "vf.h" +#include "../libvo/fastmemcpy.h" + +#define SUB_PIXEL_BITS 8 +#define SUB_PIXELS (1<ref; + int x,y; + + g= ( (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[2][1] - ref[3][1]) + - (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[2][0] - ref[3][0]))*H; + h= ( (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[1][0] - ref[3][0]) + - (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[1][1] - ref[3][1]))*W; + D= (ref[1][0] - ref[3][0])*(ref[2][1] - ref[3][1]) + - (ref[2][0] - ref[3][0])*(ref[1][1] - ref[3][1]); + + a= D*(ref[1][0] - ref[0][0])*H + g*ref[1][0]; + b= D*(ref[2][0] - ref[0][0])*W + h*ref[2][0]; + c= D*ref[0][0]*W*H; + d= D*(ref[1][1] - ref[0][1])*H + g*ref[1][1]; + e= D*(ref[2][1] - ref[0][1])*W + h*ref[2][1]; + f= D*ref[0][1]*W*H; + + for(y=0; ypv[x + y*W][0]= u; + priv->pv[x + y*W][1]= v; + } + } +} + +static double getCoeff(double d){ + double A= -0.60; + double coeff; + + d= fabs(d); + + // Equation is from VirtualDub + if(d<1.0) + coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d); + else if(d<2.0) + coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d); + else + coeff=0.0; + + return coeff; +} + +static int config(struct vf_instance_s* vf, + int width, int height, int d_width, int d_height, + unsigned int flags, unsigned int outfmt){ + int i, j; + + vf->priv->pvStride= width; + vf->priv->pv= (void*)memalign(8, width*height*2*sizeof(int32_t)); + initPv(vf->priv, width, height); + + for(i=0; ipriv->coeff[i][j]= (int)floor((1<priv) return; + + if(vf->priv->pv) free(vf->priv->pv); + vf->priv->pv= NULL; + + free(vf->priv); + vf->priv=NULL; +} + +static inline void resampleCubic(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, struct vf_priv_s *privParam, int xShift, int yShift){ + int x, y; + struct vf_priv_s priv= *privParam; + + for(y=0; y>xShift; + v= priv.pv[sx + sy*priv.pvStride][1]>>yShift; + subU= u & (SUB_PIXELS-1); + subV= v & (SUB_PIXELS-1); + u >>= SUB_PIXEL_BITS; + v >>= SUB_PIXEL_BITS; + + if(u>0 && v>0 && u=h) iy=h-1; + for(dx=0; dx<4; dx++){ + int ix= u + dx - 1; + if (ix< 0) ix=0; + else if(ix>=w) ix=w-1; + + sum+= priv.coeff[subU][dx]*priv.coeff[subV][dy] + *src[ ix + iy*srcStride]; + } + } + } + sum= (sum + (1<<(COEFF_BITS*2-1)) ) >> (COEFF_BITS*2); + if(sum&~255){ + if(sum<0) sum=0; + else sum=255; + } + dst[ x + y*dstStride]= sum; + } + } +} + +static inline void resampleLinear(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, + struct vf_priv_s *privParam, int xShift, int yShift){ + int x, y; + struct vf_priv_s priv= *privParam; + + for(y=0; y>xShift; + v= priv.pv[sx + sy*priv.pvStride][1]>>yShift; + subU= u & (SUB_PIXELS-1); + subV= v & (SUB_PIXELS-1); + u >>= SUB_PIXEL_BITS; + v >>= SUB_PIXEL_BITS; + index= u + v*srcStride; + subUI= SUB_PIXELS - subU; + subVI= SUB_PIXELS - subV; + + if((unsigned)u < (unsigned)(w - 1)){ + if((unsigned)v < (unsigned)(h - 1)){ + sum= subVI*(subUI*src[index ] + subU*src[index +1]) + +subV *(subUI*src[index+srcStride] + subU*src[index+srcStride+1]); + sum= (sum + (1<<(SUB_PIXEL_BITS*2-1)) ) >> (SUB_PIXEL_BITS*2); + }else{ + if(v<0) v= 0; + else v= h-1; + index= u + v*srcStride; + sum= subUI*src[index] + subU*src[index+1]; + sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS; + } + }else{ + if((unsigned)v < (unsigned)(h - 1)){ + if(u<0) u= 0; + else u= w-1; + index= u + v*srcStride; + sum= subVI*src[index] + subV*src[index+srcStride]; + sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS; + }else{ + if(u<0) u= 0; + else u= w-1; + if(v<0) v= 0; + else v= h-1; + index= u + v*srcStride; + sum= src[index]; + } + } + if(sum&~255){ + if(sum<0) sum=0; + else sum=255; + } + dst[ x + y*dstStride]= sum; + } + } +} + +static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ + int cw= mpi->w >> mpi->chroma_x_shift; + int ch= mpi->h >> mpi->chroma_y_shift; + + mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt, + MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, + mpi->w,mpi->h); + + assert(mpi->flags&MP_IMGFLAG_PLANAR); + + if(vf->priv->cubic){ + resampleCubic(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], + vf->priv, 0, 0); + resampleCubic(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], + vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift); + resampleCubic(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], + vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift); + }else{ + resampleLinear(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], + vf->priv, 0, 0); + resampleLinear(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], + vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift); + resampleLinear(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], + vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift); + } + + return vf_next_put_image(vf,dmpi); +} + +//===========================================================================// + +static int query_format(struct vf_instance_s* vf, unsigned int fmt){ + switch(fmt) + { + case IMGFMT_YV12: + case IMGFMT_I420: + case IMGFMT_IYUV: + case IMGFMT_YVU9: + case IMGFMT_444P: + case IMGFMT_422P: + case IMGFMT_411P: + return vf_next_query_format(vf, fmt); + } + return 0; +} + +static int open(vf_instance_t *vf, char* args){ + int e; + + vf->config=config; + vf->put_image=put_image; +// vf->get_image=get_image; + vf->query_format=query_format; + vf->uninit=uninit; + vf->priv=malloc(sizeof(struct vf_priv_s)); + memset(vf->priv, 0, sizeof(struct vf_priv_s)); + + if(args==NULL) return 0; + + e=sscanf(args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf:%d", + &vf->priv->ref[0][0], &vf->priv->ref[0][1], + &vf->priv->ref[1][0], &vf->priv->ref[1][1], + &vf->priv->ref[2][0], &vf->priv->ref[2][1], + &vf->priv->ref[3][0], &vf->priv->ref[3][1], + &vf->priv->cubic + ); + + if(e!=9) + return 0; + + return 1; +} + +vf_info_t vf_info_perspective = { + "perspective correcture", + "perspective", + "Michael Niedermayer", + "", + open +}; + +//===========================================================================//