vf_perspective.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of MPlayer.
  5. *
  6. * MPlayer is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * MPlayer is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <inttypes.h>
  24. #include <assert.h>
  25. #include <math.h>
  26. #include "config.h"
  27. #include "mp_msg.h"
  28. #if HAVE_MALLOC_H
  29. #include <malloc.h>
  30. #endif
  31. #include "libavutil/mem.h"
  32. #include "img_format.h"
  33. #include "mp_image.h"
  34. #include "vf.h"
  35. #define SUB_PIXEL_BITS 8
  36. #define SUB_PIXELS (1<<SUB_PIXEL_BITS)
  37. #define COEFF_BITS 11
  38. //===========================================================================//
  39. struct vf_priv_s {
  40. double ref[4][2];
  41. int32_t coeff[1<<SUB_PIXEL_BITS][4];
  42. int32_t (*pv)[2];
  43. int pvStride;
  44. int cubic;
  45. };
  46. /***************************************************************************/
  47. static void initPv(struct vf_priv_s *priv, int W, int H){
  48. double a,b,c,d,e,f,g,h,D;
  49. double (*ref)[2]= priv->ref;
  50. int x,y;
  51. g= ( (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[2][1] - ref[3][1])
  52. - (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[2][0] - ref[3][0]))*H;
  53. h= ( (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[1][0] - ref[3][0])
  54. - (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[1][1] - ref[3][1]))*W;
  55. D= (ref[1][0] - ref[3][0])*(ref[2][1] - ref[3][1])
  56. - (ref[2][0] - ref[3][0])*(ref[1][1] - ref[3][1]);
  57. a= D*(ref[1][0] - ref[0][0])*H + g*ref[1][0];
  58. b= D*(ref[2][0] - ref[0][0])*W + h*ref[2][0];
  59. c= D*ref[0][0]*W*H;
  60. d= D*(ref[1][1] - ref[0][1])*H + g*ref[1][1];
  61. e= D*(ref[2][1] - ref[0][1])*W + h*ref[2][1];
  62. f= D*ref[0][1]*W*H;
  63. for(y=0; y<H; y++){
  64. for(x=0; x<W; x++){
  65. int u, v;
  66. u= (int)floor( SUB_PIXELS*(a*x + b*y + c)/(g*x + h*y + D*W*H) + 0.5);
  67. v= (int)floor( SUB_PIXELS*(d*x + e*y + f)/(g*x + h*y + D*W*H) + 0.5);
  68. priv->pv[x + y*W][0]= u;
  69. priv->pv[x + y*W][1]= v;
  70. }
  71. }
  72. }
  73. static double getCoeff(double d){
  74. double A= -0.60;
  75. double coeff;
  76. d= fabs(d);
  77. // Equation is from VirtualDub
  78. if(d<1.0)
  79. coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  80. else if(d<2.0)
  81. coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
  82. else
  83. coeff=0.0;
  84. return coeff;
  85. }
  86. static int config(struct vf_instance *vf,
  87. int width, int height, int d_width, int d_height,
  88. unsigned int flags, unsigned int outfmt){
  89. int i, j;
  90. vf->priv->pvStride= width;
  91. vf->priv->pv= av_malloc(width*height*2*sizeof(int32_t));
  92. initPv(vf->priv, width, height);
  93. for(i=0; i<SUB_PIXELS; i++){
  94. double d= i/(double)SUB_PIXELS;
  95. double temp[4];
  96. double sum=0;
  97. for(j=0; j<4; j++)
  98. temp[j]= getCoeff(j - d - 1);
  99. for(j=0; j<4; j++)
  100. sum+= temp[j];
  101. for(j=0; j<4; j++)
  102. vf->priv->coeff[i][j]= (int)floor((1<<COEFF_BITS)*temp[j]/sum + 0.5);
  103. }
  104. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  105. }
  106. static void uninit(struct vf_instance *vf){
  107. if(!vf->priv) return;
  108. av_free(vf->priv->pv);
  109. vf->priv->pv= NULL;
  110. free(vf->priv);
  111. vf->priv=NULL;
  112. }
  113. 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){
  114. int x, y;
  115. struct vf_priv_s priv= *privParam;
  116. for(y=0; y<h; y++){
  117. for(x=0; x<w; x++){
  118. int u, v, subU, subV, sum, sx, sy;
  119. sx= x << xShift;
  120. sy= y << yShift;
  121. u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
  122. v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
  123. subU= u & (SUB_PIXELS-1);
  124. subV= v & (SUB_PIXELS-1);
  125. u >>= SUB_PIXEL_BITS;
  126. v >>= SUB_PIXEL_BITS;
  127. if(u>0 && v>0 && u<w-2 && v<h-2){
  128. const int index= u + v*srcStride;
  129. const int a= priv.coeff[subU][0];
  130. const int b= priv.coeff[subU][1];
  131. const int c= priv.coeff[subU][2];
  132. const int d= priv.coeff[subU][3];
  133. sum=
  134. priv.coeff[subV][0]*( a*src[index - 1 - srcStride] + b*src[index - 0 - srcStride]
  135. + c*src[index + 1 - srcStride] + d*src[index + 2 - srcStride])
  136. +priv.coeff[subV][1]*( a*src[index - 1 ] + b*src[index - 0 ]
  137. + c*src[index + 1 ] + d*src[index + 2 ])
  138. +priv.coeff[subV][2]*( a*src[index - 1 + srcStride] + b*src[index - 0 + srcStride]
  139. + c*src[index + 1 + srcStride] + d*src[index + 2 + srcStride])
  140. +priv.coeff[subV][3]*( a*src[index - 1+2*srcStride] + b*src[index - 0+2*srcStride]
  141. + c*src[index + 1+2*srcStride] + d*src[index + 2+2*srcStride]);
  142. }else{
  143. int dx, dy;
  144. sum=0;
  145. for(dy=0; dy<4; dy++){
  146. int iy= v + dy - 1;
  147. if (iy< 0) iy=0;
  148. else if(iy>=h) iy=h-1;
  149. for(dx=0; dx<4; dx++){
  150. int ix= u + dx - 1;
  151. if (ix< 0) ix=0;
  152. else if(ix>=w) ix=w-1;
  153. sum+= priv.coeff[subU][dx]*priv.coeff[subV][dy]
  154. *src[ ix + iy*srcStride];
  155. }
  156. }
  157. }
  158. sum= (sum + (1<<(COEFF_BITS*2-1)) ) >> (COEFF_BITS*2);
  159. if(sum&~255){
  160. if(sum<0) sum=0;
  161. else sum=255;
  162. }
  163. dst[ x + y*dstStride]= sum;
  164. }
  165. }
  166. }
  167. static inline void resampleLinear(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride,
  168. struct vf_priv_s *privParam, int xShift, int yShift){
  169. int x, y;
  170. struct vf_priv_s priv= *privParam;
  171. for(y=0; y<h; y++){
  172. for(x=0; x<w; x++){
  173. int u, v, subU, subV, sum, sx, sy, index, subUI, subVI;
  174. sx= x << xShift;
  175. sy= y << yShift;
  176. u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
  177. v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
  178. subU= u & (SUB_PIXELS-1);
  179. subV= v & (SUB_PIXELS-1);
  180. u >>= SUB_PIXEL_BITS;
  181. v >>= SUB_PIXEL_BITS;
  182. index= u + v*srcStride;
  183. subUI= SUB_PIXELS - subU;
  184. subVI= SUB_PIXELS - subV;
  185. if((unsigned)u < (unsigned)(w - 1)){
  186. if((unsigned)v < (unsigned)(h - 1)){
  187. sum= subVI*(subUI*src[index ] + subU*src[index +1])
  188. +subV *(subUI*src[index+srcStride] + subU*src[index+srcStride+1]);
  189. sum= (sum + (1<<(SUB_PIXEL_BITS*2-1)) ) >> (SUB_PIXEL_BITS*2);
  190. }else{
  191. if(v<0) v= 0;
  192. else v= h-1;
  193. index= u + v*srcStride;
  194. sum= subUI*src[index] + subU*src[index+1];
  195. sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
  196. }
  197. }else{
  198. if((unsigned)v < (unsigned)(h - 1)){
  199. if(u<0) u= 0;
  200. else u= w-1;
  201. index= u + v*srcStride;
  202. sum= subVI*src[index] + subV*src[index+srcStride];
  203. sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
  204. }else{
  205. if(u<0) u= 0;
  206. else u= w-1;
  207. if(v<0) v= 0;
  208. else v= h-1;
  209. index= u + v*srcStride;
  210. sum= src[index];
  211. }
  212. }
  213. if(sum&~255){
  214. if(sum<0) sum=0;
  215. else sum=255;
  216. }
  217. dst[ x + y*dstStride]= sum;
  218. }
  219. }
  220. }
  221. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  222. int cw= mpi->w >> mpi->chroma_x_shift;
  223. int ch= mpi->h >> mpi->chroma_y_shift;
  224. mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
  225. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  226. mpi->w,mpi->h);
  227. assert(mpi->flags&MP_IMGFLAG_PLANAR);
  228. if(vf->priv->cubic){
  229. resampleCubic(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
  230. vf->priv, 0, 0);
  231. resampleCubic(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1],
  232. vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
  233. resampleCubic(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2],
  234. vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
  235. }else{
  236. resampleLinear(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
  237. vf->priv, 0, 0);
  238. resampleLinear(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1],
  239. vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
  240. resampleLinear(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2],
  241. vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
  242. }
  243. return vf_next_put_image(vf,dmpi, pts);
  244. }
  245. //===========================================================================//
  246. static int query_format(struct vf_instance *vf, unsigned int fmt){
  247. switch(fmt)
  248. {
  249. case IMGFMT_YV12:
  250. case IMGFMT_I420:
  251. case IMGFMT_IYUV:
  252. case IMGFMT_YVU9:
  253. case IMGFMT_444P:
  254. case IMGFMT_422P:
  255. case IMGFMT_411P:
  256. return vf_next_query_format(vf, fmt);
  257. }
  258. return 0;
  259. }
  260. static int vf_open(vf_instance_t *vf, char *args){
  261. int e;
  262. vf->config=config;
  263. vf->put_image=put_image;
  264. // vf->get_image=get_image;
  265. vf->query_format=query_format;
  266. vf->uninit=uninit;
  267. vf->priv=malloc(sizeof(struct vf_priv_s));
  268. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  269. if(args==NULL) return 0;
  270. e=sscanf(args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf:%d",
  271. &vf->priv->ref[0][0], &vf->priv->ref[0][1],
  272. &vf->priv->ref[1][0], &vf->priv->ref[1][1],
  273. &vf->priv->ref[2][0], &vf->priv->ref[2][1],
  274. &vf->priv->ref[3][0], &vf->priv->ref[3][1],
  275. &vf->priv->cubic
  276. );
  277. if(e!=9)
  278. return 0;
  279. return 1;
  280. }
  281. const vf_info_t vf_info_perspective = {
  282. "perspective correcture",
  283. "perspective",
  284. "Michael Niedermayer",
  285. "",
  286. vf_open,
  287. NULL
  288. };
  289. //===========================================================================//