00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 "libavutil/mem.h"
00036
00037 #include "img_format.h"
00038 #include "mp_image.h"
00039 #include "vf.h"
00040
00041 #define SUB_PIXEL_BITS 8
00042 #define SUB_PIXELS (1<<SUB_PIXEL_BITS)
00043 #define COEFF_BITS 11
00044
00045
00046
00047 struct vf_priv_s {
00048 double ref[4][2];
00049 int32_t coeff[1<<SUB_PIXEL_BITS][4];
00050 int32_t (*pv)[2];
00051 int pvStride;
00052 int cubic;
00053 };
00054
00055
00056
00057
00058 static void initPv(struct vf_priv_s *priv, int W, int H){
00059 double a,b,c,d,e,f,g,h,D;
00060 double (*ref)[2]= priv->ref;
00061 int x,y;
00062
00063 g= ( (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[2][1] - ref[3][1])
00064 - (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[2][0] - ref[3][0]))*H;
00065 h= ( (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[1][0] - ref[3][0])
00066 - (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[1][1] - ref[3][1]))*W;
00067 D= (ref[1][0] - ref[3][0])*(ref[2][1] - ref[3][1])
00068 - (ref[2][0] - ref[3][0])*(ref[1][1] - ref[3][1]);
00069
00070 a= D*(ref[1][0] - ref[0][0])*H + g*ref[1][0];
00071 b= D*(ref[2][0] - ref[0][0])*W + h*ref[2][0];
00072 c= D*ref[0][0]*W*H;
00073 d= D*(ref[1][1] - ref[0][1])*H + g*ref[1][1];
00074 e= D*(ref[2][1] - ref[0][1])*W + h*ref[2][1];
00075 f= D*ref[0][1]*W*H;
00076
00077 for(y=0; y<H; y++){
00078 for(x=0; x<W; x++){
00079 int u, v;
00080
00081 u= (int)floor( SUB_PIXELS*(a*x + b*y + c)/(g*x + h*y + D*W*H) + 0.5);
00082 v= (int)floor( SUB_PIXELS*(d*x + e*y + f)/(g*x + h*y + D*W*H) + 0.5);
00083
00084 priv->pv[x + y*W][0]= u;
00085 priv->pv[x + y*W][1]= v;
00086 }
00087 }
00088 }
00089
00090 static double getCoeff(double d){
00091 double A= -0.60;
00092 double coeff;
00093
00094 d= fabs(d);
00095
00096
00097 if(d<1.0)
00098 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
00099 else if(d<2.0)
00100 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
00101 else
00102 coeff=0.0;
00103
00104 return coeff;
00105 }
00106
00107 static int config(struct vf_instance *vf,
00108 int width, int height, int d_width, int d_height,
00109 unsigned int flags, unsigned int outfmt){
00110 int i, j;
00111
00112 vf->priv->pvStride= width;
00113 vf->priv->pv= av_malloc(width*height*2*sizeof(int32_t));
00114 initPv(vf->priv, width, height);
00115
00116 for(i=0; i<SUB_PIXELS; i++){
00117 double d= i/(double)SUB_PIXELS;
00118 double temp[4];
00119 double sum=0;
00120
00121 for(j=0; j<4; j++)
00122 temp[j]= getCoeff(j - d - 1);
00123
00124 for(j=0; j<4; j++)
00125 sum+= temp[j];
00126
00127 for(j=0; j<4; j++)
00128 vf->priv->coeff[i][j]= (int)floor((1<<COEFF_BITS)*temp[j]/sum + 0.5);
00129 }
00130
00131 return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
00132 }
00133
00134 static void uninit(struct vf_instance *vf){
00135 if(!vf->priv) return;
00136
00137 av_free(vf->priv->pv);
00138 vf->priv->pv= NULL;
00139
00140 free(vf->priv);
00141 vf->priv=NULL;
00142 }
00143
00144 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){
00145 int x, y;
00146 struct vf_priv_s priv= *privParam;
00147
00148 for(y=0; y<h; y++){
00149 for(x=0; x<w; x++){
00150 int u, v, subU, subV, sum, sx, sy;
00151
00152 sx= x << xShift;
00153 sy= y << yShift;
00154 u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
00155 v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
00156 subU= u & (SUB_PIXELS-1);
00157 subV= v & (SUB_PIXELS-1);
00158 u >>= SUB_PIXEL_BITS;
00159 v >>= SUB_PIXEL_BITS;
00160
00161 if(u>0 && v>0 && u<w-2 && v<h-2){
00162 const int index= u + v*srcStride;
00163 const int a= priv.coeff[subU][0];
00164 const int b= priv.coeff[subU][1];
00165 const int c= priv.coeff[subU][2];
00166 const int d= priv.coeff[subU][3];
00167
00168 sum=
00169 priv.coeff[subV][0]*( a*src[index - 1 - srcStride] + b*src[index - 0 - srcStride]
00170 + c*src[index + 1 - srcStride] + d*src[index + 2 - srcStride])
00171 +priv.coeff[subV][1]*( a*src[index - 1 ] + b*src[index - 0 ]
00172 + c*src[index + 1 ] + d*src[index + 2 ])
00173 +priv.coeff[subV][2]*( a*src[index - 1 + srcStride] + b*src[index - 0 + srcStride]
00174 + c*src[index + 1 + srcStride] + d*src[index + 2 + srcStride])
00175 +priv.coeff[subV][3]*( a*src[index - 1+2*srcStride] + b*src[index - 0+2*srcStride]
00176 + c*src[index + 1+2*srcStride] + d*src[index + 2+2*srcStride]);
00177 }else{
00178 int dx, dy;
00179 sum=0;
00180
00181 for(dy=0; dy<4; dy++){
00182 int iy= v + dy - 1;
00183 if (iy< 0) iy=0;
00184 else if(iy>=h) iy=h-1;
00185 for(dx=0; dx<4; dx++){
00186 int ix= u + dx - 1;
00187 if (ix< 0) ix=0;
00188 else if(ix>=w) ix=w-1;
00189
00190 sum+= priv.coeff[subU][dx]*priv.coeff[subV][dy]
00191 *src[ ix + iy*srcStride];
00192 }
00193 }
00194 }
00195 sum= (sum + (1<<(COEFF_BITS*2-1)) ) >> (COEFF_BITS*2);
00196 if(sum&~255){
00197 if(sum<0) sum=0;
00198 else sum=255;
00199 }
00200 dst[ x + y*dstStride]= sum;
00201 }
00202 }
00203 }
00204
00205 static inline void resampleLinear(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride,
00206 struct vf_priv_s *privParam, int xShift, int yShift){
00207 int x, y;
00208 struct vf_priv_s priv= *privParam;
00209
00210 for(y=0; y<h; y++){
00211 for(x=0; x<w; x++){
00212 int u, v, subU, subV, sum, sx, sy, index, subUI, subVI;
00213
00214 sx= x << xShift;
00215 sy= y << yShift;
00216 u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
00217 v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
00218 subU= u & (SUB_PIXELS-1);
00219 subV= v & (SUB_PIXELS-1);
00220 u >>= SUB_PIXEL_BITS;
00221 v >>= SUB_PIXEL_BITS;
00222 index= u + v*srcStride;
00223 subUI= SUB_PIXELS - subU;
00224 subVI= SUB_PIXELS - subV;
00225
00226 if((unsigned)u < (unsigned)(w - 1)){
00227 if((unsigned)v < (unsigned)(h - 1)){
00228 sum= subVI*(subUI*src[index ] + subU*src[index +1])
00229 +subV *(subUI*src[index+srcStride] + subU*src[index+srcStride+1]);
00230 sum= (sum + (1<<(SUB_PIXEL_BITS*2-1)) ) >> (SUB_PIXEL_BITS*2);
00231 }else{
00232 if(v<0) v= 0;
00233 else v= h-1;
00234 index= u + v*srcStride;
00235 sum= subUI*src[index] + subU*src[index+1];
00236 sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
00237 }
00238 }else{
00239 if((unsigned)v < (unsigned)(h - 1)){
00240 if(u<0) u= 0;
00241 else u= w-1;
00242 index= u + v*srcStride;
00243 sum= subVI*src[index] + subV*src[index+srcStride];
00244 sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
00245 }else{
00246 if(u<0) u= 0;
00247 else u= w-1;
00248 if(v<0) v= 0;
00249 else v= h-1;
00250 index= u + v*srcStride;
00251 sum= src[index];
00252 }
00253 }
00254 if(sum&~255){
00255 if(sum<0) sum=0;
00256 else sum=255;
00257 }
00258 dst[ x + y*dstStride]= sum;
00259 }
00260 }
00261 }
00262
00263 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
00264 int cw= mpi->w >> mpi->chroma_x_shift;
00265 int ch= mpi->h >> mpi->chroma_y_shift;
00266
00267 mp_image_t *dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
00268 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
00269 mpi->w,mpi->h);
00270
00271 assert(mpi->flags&MP_IMGFLAG_PLANAR);
00272
00273 if(vf->priv->cubic){
00274 resampleCubic(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
00275 vf->priv, 0, 0);
00276 resampleCubic(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1],
00277 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
00278 resampleCubic(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2],
00279 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
00280 }else{
00281 resampleLinear(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
00282 vf->priv, 0, 0);
00283 resampleLinear(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1],
00284 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
00285 resampleLinear(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2],
00286 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
00287 }
00288
00289 return ff_vf_next_put_image(vf,dmpi, pts);
00290 }
00291
00292
00293
00294 static int query_format(struct vf_instance *vf, unsigned int fmt){
00295 switch(fmt)
00296 {
00297 case IMGFMT_YV12:
00298 case IMGFMT_I420:
00299 case IMGFMT_IYUV:
00300 case IMGFMT_YVU9:
00301 case IMGFMT_444P:
00302 case IMGFMT_422P:
00303 case IMGFMT_411P:
00304 return ff_vf_next_query_format(vf, fmt);
00305 }
00306 return 0;
00307 }
00308
00309 static int vf_open(vf_instance_t *vf, char *args){
00310 int e;
00311
00312 vf->config=config;
00313 vf->put_image=put_image;
00314
00315 vf->query_format=query_format;
00316 vf->uninit=uninit;
00317 vf->priv=malloc(sizeof(struct vf_priv_s));
00318 memset(vf->priv, 0, sizeof(struct vf_priv_s));
00319
00320 if(args==NULL) return 0;
00321
00322 e=sscanf(args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf:%d",
00323 &vf->priv->ref[0][0], &vf->priv->ref[0][1],
00324 &vf->priv->ref[1][0], &vf->priv->ref[1][1],
00325 &vf->priv->ref[2][0], &vf->priv->ref[2][1],
00326 &vf->priv->ref[3][0], &vf->priv->ref[3][1],
00327 &vf->priv->cubic
00328 );
00329
00330 if(e!=9)
00331 return 0;
00332
00333 return 1;
00334 }
00335
00336 const vf_info_t ff_vf_info_perspective = {
00337 "perspective correcture",
00338 "perspective",
00339 "Michael Niedermayer",
00340 "",
00341 vf_open,
00342 NULL
00343 };
00344
00345