• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavfilter/libmpcodecs/vf_perspective.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
00003  *
00004  * This file is part of MPlayer.
00005  *
00006  * MPlayer is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * MPlayer is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
00018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <inttypes.h>
00025 #include <assert.h>
00026 #include <math.h>
00027 
00028 #include "config.h"
00029 #include "mp_msg.h"
00030 
00031 #if HAVE_MALLOC_H
00032 #include <malloc.h>
00033 #endif
00034 
00035 #include "img_format.h"
00036 #include "mp_image.h"
00037 #include "vf.h"
00038 
00039 #define SUB_PIXEL_BITS 8
00040 #define SUB_PIXELS (1<<SUB_PIXEL_BITS)
00041 #define COEFF_BITS 11
00042 
00043 //===========================================================================//
00044 
00045 struct vf_priv_s {
00046     double ref[4][2];
00047     int32_t coeff[1<<SUB_PIXEL_BITS][4];
00048     int32_t (*pv)[2];
00049     int pvStride;
00050     int cubic;
00051 };
00052 
00053 
00054 /***************************************************************************/
00055 
00056 static void initPv(struct vf_priv_s *priv, int W, int H){
00057     double a,b,c,d,e,f,g,h,D;
00058     double (*ref)[2]= priv->ref;
00059     int x,y;
00060 
00061     g= (  (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[2][1] - ref[3][1])
00062         - (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[2][0] - ref[3][0]))*H;
00063     h= (  (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[1][0] - ref[3][0])
00064         - (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[1][1] - ref[3][1]))*W;
00065     D=   (ref[1][0] - ref[3][0])*(ref[2][1] - ref[3][1])
00066        - (ref[2][0] - ref[3][0])*(ref[1][1] - ref[3][1]);
00067 
00068     a= D*(ref[1][0] - ref[0][0])*H + g*ref[1][0];
00069     b= D*(ref[2][0] - ref[0][0])*W + h*ref[2][0];
00070     c= D*ref[0][0]*W*H;
00071     d= D*(ref[1][1] - ref[0][1])*H + g*ref[1][1];
00072     e= D*(ref[2][1] - ref[0][1])*W + h*ref[2][1];
00073     f= D*ref[0][1]*W*H;
00074 
00075     for(y=0; y<H; y++){
00076         for(x=0; x<W; x++){
00077             int u, v;
00078 
00079             u= (int)floor( SUB_PIXELS*(a*x + b*y + c)/(g*x + h*y + D*W*H) + 0.5);
00080             v= (int)floor( SUB_PIXELS*(d*x + e*y + f)/(g*x + h*y + D*W*H) + 0.5);
00081 
00082             priv->pv[x + y*W][0]= u;
00083             priv->pv[x + y*W][1]= v;
00084         }
00085     }
00086 }
00087 
00088 static double getCoeff(double d){
00089     double A= -0.60;
00090     double coeff;
00091 
00092     d= fabs(d);
00093 
00094     // Equation is from VirtualDub
00095     if(d<1.0)
00096         coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
00097     else if(d<2.0)
00098         coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
00099     else
00100         coeff=0.0;
00101 
00102     return coeff;
00103 }
00104 
00105 static int config(struct vf_instance *vf,
00106     int width, int height, int d_width, int d_height,
00107     unsigned int flags, unsigned int outfmt){
00108     int i, j;
00109 
00110     vf->priv->pvStride= width;
00111     vf->priv->pv= av_malloc(width*height*2*sizeof(int32_t));
00112     initPv(vf->priv, width, height);
00113 
00114     for(i=0; i<SUB_PIXELS; i++){
00115         double d= i/(double)SUB_PIXELS;
00116         double temp[4];
00117         double sum=0;
00118 
00119         for(j=0; j<4; j++)
00120             temp[j]= getCoeff(j - d - 1);
00121 
00122         for(j=0; j<4; j++)
00123             sum+= temp[j];
00124 
00125         for(j=0; j<4; j++)
00126             vf->priv->coeff[i][j]= (int)floor((1<<COEFF_BITS)*temp[j]/sum + 0.5);
00127     }
00128 
00129     return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
00130 }
00131 
00132 static void uninit(struct vf_instance *vf){
00133     if(!vf->priv) return;
00134 
00135     av_free(vf->priv->pv);
00136     vf->priv->pv= NULL;
00137 
00138     free(vf->priv);
00139     vf->priv=NULL;
00140 }
00141 
00142 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){
00143     int x, y;
00144     struct vf_priv_s priv= *privParam;
00145 
00146     for(y=0; y<h; y++){
00147         for(x=0; x<w; x++){
00148             int u, v, subU, subV, sum, sx, sy;
00149 
00150             sx= x << xShift;
00151             sy= y << yShift;
00152             u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
00153             v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
00154             subU= u & (SUB_PIXELS-1);
00155             subV= v & (SUB_PIXELS-1);
00156             u >>= SUB_PIXEL_BITS;
00157             v >>= SUB_PIXEL_BITS;
00158 
00159             if(u>0 && v>0 && u<w-2 && v<h-2){
00160                 const int index= u + v*srcStride;
00161                 const int a= priv.coeff[subU][0];
00162                 const int b= priv.coeff[subU][1];
00163                 const int c= priv.coeff[subU][2];
00164                 const int d= priv.coeff[subU][3];
00165 
00166                 sum=
00167                  priv.coeff[subV][0]*(  a*src[index - 1 - srcStride] + b*src[index - 0 - srcStride]
00168                                       + c*src[index + 1 - srcStride] + d*src[index + 2 - srcStride])
00169                 +priv.coeff[subV][1]*(  a*src[index - 1            ] + b*src[index - 0            ]
00170                                       + c*src[index + 1            ] + d*src[index + 2            ])
00171                 +priv.coeff[subV][2]*(  a*src[index - 1 + srcStride] + b*src[index - 0 + srcStride]
00172                                       + c*src[index + 1 + srcStride] + d*src[index + 2 + srcStride])
00173                 +priv.coeff[subV][3]*(  a*src[index - 1+2*srcStride] + b*src[index - 0+2*srcStride]
00174                                       + c*src[index + 1+2*srcStride] + d*src[index + 2+2*srcStride]);
00175             }else{
00176                 int dx, dy;
00177                 sum=0;
00178 
00179                 for(dy=0; dy<4; dy++){
00180                     int iy= v + dy - 1;
00181                     if     (iy< 0) iy=0;
00182                     else if(iy>=h) iy=h-1;
00183                     for(dx=0; dx<4; dx++){
00184                         int ix= u + dx - 1;
00185                         if     (ix< 0) ix=0;
00186                         else if(ix>=w) ix=w-1;
00187 
00188                         sum+=  priv.coeff[subU][dx]*priv.coeff[subV][dy]
00189                               *src[ ix + iy*srcStride];
00190                     }
00191                 }
00192             }
00193             sum= (sum + (1<<(COEFF_BITS*2-1)) ) >> (COEFF_BITS*2);
00194             if(sum&~255){
00195                 if(sum<0) sum=0;
00196                 else      sum=255;
00197             }
00198             dst[ x + y*dstStride]= sum;
00199         }
00200     }
00201 }
00202 
00203 static inline void resampleLinear(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride,
00204                   struct vf_priv_s *privParam, int xShift, int yShift){
00205     int x, y;
00206     struct vf_priv_s priv= *privParam;
00207 
00208     for(y=0; y<h; y++){
00209         for(x=0; x<w; x++){
00210             int u, v, subU, subV, sum, sx, sy, index, subUI, subVI;
00211 
00212             sx= x << xShift;
00213             sy= y << yShift;
00214             u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
00215             v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
00216             subU= u & (SUB_PIXELS-1);
00217             subV= v & (SUB_PIXELS-1);
00218             u >>= SUB_PIXEL_BITS;
00219             v >>= SUB_PIXEL_BITS;
00220             index= u + v*srcStride;
00221             subUI= SUB_PIXELS - subU;
00222             subVI= SUB_PIXELS - subV;
00223 
00224             if((unsigned)u < (unsigned)(w - 1)){
00225                 if((unsigned)v < (unsigned)(h - 1)){
00226                     sum= subVI*(subUI*src[index          ] + subU*src[index          +1])
00227                         +subV *(subUI*src[index+srcStride] + subU*src[index+srcStride+1]);
00228                     sum= (sum + (1<<(SUB_PIXEL_BITS*2-1)) ) >> (SUB_PIXEL_BITS*2);
00229                 }else{
00230                     if(v<0) v= 0;
00231                     else    v= h-1;
00232                     index= u + v*srcStride;
00233                     sum= subUI*src[index] + subU*src[index+1];
00234                     sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
00235                 }
00236             }else{
00237                 if((unsigned)v < (unsigned)(h - 1)){
00238                     if(u<0) u= 0;
00239                     else    u= w-1;
00240                     index= u + v*srcStride;
00241                     sum= subVI*src[index] + subV*src[index+srcStride];
00242                     sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
00243                 }else{
00244                     if(u<0) u= 0;
00245                     else    u= w-1;
00246                     if(v<0) v= 0;
00247                     else    v= h-1;
00248                     index= u + v*srcStride;
00249                     sum= src[index];
00250                 }
00251             }
00252             if(sum&~255){
00253                 if(sum<0) sum=0;
00254                 else      sum=255;
00255             }
00256             dst[ x + y*dstStride]= sum;
00257         }
00258     }
00259 }
00260 
00261 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
00262     int cw= mpi->w >> mpi->chroma_x_shift;
00263     int ch= mpi->h >> mpi->chroma_y_shift;
00264 
00265     mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
00266         MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
00267         mpi->w,mpi->h);
00268 
00269     assert(mpi->flags&MP_IMGFLAG_PLANAR);
00270 
00271     if(vf->priv->cubic){
00272         resampleCubic(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
00273                 vf->priv, 0, 0);
00274         resampleCubic(dmpi->planes[1], mpi->planes[1], cw    , ch   , dmpi->stride[1], mpi->stride[1],
00275                 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
00276         resampleCubic(dmpi->planes[2], mpi->planes[2], cw    , ch   , dmpi->stride[2], mpi->stride[2],
00277                 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
00278     }else{
00279         resampleLinear(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
00280                 vf->priv, 0, 0);
00281         resampleLinear(dmpi->planes[1], mpi->planes[1], cw    , ch   , dmpi->stride[1], mpi->stride[1],
00282                 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
00283         resampleLinear(dmpi->planes[2], mpi->planes[2], cw    , ch   , dmpi->stride[2], mpi->stride[2],
00284                 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
00285     }
00286 
00287     return vf_next_put_image(vf,dmpi, pts);
00288 }
00289 
00290 //===========================================================================//
00291 
00292 static int query_format(struct vf_instance *vf, unsigned int fmt){
00293     switch(fmt)
00294     {
00295     case IMGFMT_YV12:
00296     case IMGFMT_I420:
00297     case IMGFMT_IYUV:
00298     case IMGFMT_YVU9:
00299     case IMGFMT_444P:
00300     case IMGFMT_422P:
00301     case IMGFMT_411P:
00302         return vf_next_query_format(vf, fmt);
00303     }
00304     return 0;
00305 }
00306 
00307 static int vf_open(vf_instance_t *vf, char *args){
00308     int e;
00309 
00310     vf->config=config;
00311     vf->put_image=put_image;
00312 //  vf->get_image=get_image;
00313     vf->query_format=query_format;
00314     vf->uninit=uninit;
00315     vf->priv=malloc(sizeof(struct vf_priv_s));
00316     memset(vf->priv, 0, sizeof(struct vf_priv_s));
00317 
00318     if(args==NULL) return 0;
00319 
00320     e=sscanf(args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf:%d",
00321         &vf->priv->ref[0][0], &vf->priv->ref[0][1],
00322         &vf->priv->ref[1][0], &vf->priv->ref[1][1],
00323         &vf->priv->ref[2][0], &vf->priv->ref[2][1],
00324         &vf->priv->ref[3][0], &vf->priv->ref[3][1],
00325         &vf->priv->cubic
00326         );
00327 
00328     if(e!=9)
00329         return 0;
00330 
00331     return 1;
00332 }
00333 
00334 const vf_info_t vf_info_perspective = {
00335     "perspective correcture",
00336     "perspective",
00337     "Michael Niedermayer",
00338     "",
00339     vf_open,
00340     NULL
00341 };
00342 
00343 //===========================================================================//

Generated on Fri Feb 22 2013 07:24:30 for FFmpeg by  doxygen 1.7.1