vf_perspective.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "img_format.h"
  32. #include "mp_image.h"
  33. #include "vf.h"
  34. #define SUB_PIXEL_BITS 8
  35. #define SUB_PIXELS (1<<SUB_PIXEL_BITS)
  36. #define COEFF_BITS 11
  37. //===========================================================================//
  38. struct vf_priv_s {
  39. double ref[4][2];
  40. int32_t coeff[1<<SUB_PIXEL_BITS][4];
  41. int32_t (*pv)[2];
  42. int pvStride;
  43. int cubic;
  44. };
  45. /***************************************************************************/
  46. static void initPv(struct vf_priv_s *priv, int W, int H){
  47. double a,b,c,d,e,f,g,h,D;
  48. double (*ref)[2]= priv->ref;
  49. int x,y;
  50. g= ( (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[2][1] - ref[3][1])
  51. - (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[2][0] - ref[3][0]))*H;
  52. h= ( (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[1][0] - ref[3][0])
  53. - (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[1][1] - ref[3][1]))*W;
  54. D= (ref[1][0] - ref[3][0])*(ref[2][1] - ref[3][1])
  55. - (ref[2][0] - ref[3][0])*(ref[1][1] - ref[3][1]);
  56. a= D*(ref[1][0] - ref[0][0])*H + g*ref[1][0];
  57. b= D*(ref[2][0] - ref[0][0])*W + h*ref[2][0];
  58. c= D*ref[0][0]*W*H;
  59. d= D*(ref[1][1] - ref[0][1])*H + g*ref[1][1];
  60. e= D*(ref[2][1] - ref[0][1])*W + h*ref[2][1];
  61. f= D*ref[0][1]*W*H;
  62. for(y=0; y<H; y++){
  63. for(x=0; x<W; x++){
  64. int u, v;
  65. u= (int)floor( SUB_PIXELS*(a*x + b*y + c)/(g*x + h*y + D*W*H) + 0.5);
  66. v= (int)floor( SUB_PIXELS*(d*x + e*y + f)/(g*x + h*y + D*W*H) + 0.5);
  67. priv->pv[x + y*W][0]= u;
  68. priv->pv[x + y*W][1]= v;
  69. }
  70. }
  71. }
  72. static double getCoeff(double d){
  73. double A= -0.60;
  74. double coeff;
  75. d= fabs(d);
  76. // Equation is from VirtualDub
  77. if(d<1.0)
  78. coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  79. else if(d<2.0)
  80. coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
  81. else
  82. coeff=0.0;
  83. return coeff;
  84. }
  85. static int config(struct vf_instance *vf,
  86. int width, int height, int d_width, int d_height,
  87. unsigned int flags, unsigned int outfmt){
  88. int i, j;
  89. vf->priv->pvStride= width;
  90. vf->priv->pv= av_malloc(width*height*2*sizeof(int32_t));
  91. initPv(vf->priv, width, height);
  92. for(i=0; i<SUB_PIXELS; i++){
  93. double d= i/(double)SUB_PIXELS;
  94. double temp[4];
  95. double sum=0;
  96. for(j=0; j<4; j++)
  97. temp[j]= getCoeff(j - d - 1);
  98. for(j=0; j<4; j++)
  99. sum+= temp[j];
  100. for(j=0; j<4; j++)
  101. vf->priv->coeff[i][j]= (int)floor((1<<COEFF_BITS)*temp[j]/sum + 0.5);
  102. }
  103. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  104. }
  105. static void uninit(struct vf_instance *vf){
  106. if(!vf->priv) return;
  107. av_free(vf->priv->pv);
  108. vf->priv->pv= NULL;
  109. free(vf->priv);
  110. vf->priv=NULL;
  111. }
  112. 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){
  113. int x, y;
  114. struct vf_priv_s priv= *privParam;
  115. for(y=0; y<h; y++){
  116. for(x=0; x<w; x++){
  117. int u, v, subU, subV, sum, sx, sy;
  118. sx= x << xShift;
  119. sy= y << yShift;
  120. u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
  121. v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
  122. subU= u & (SUB_PIXELS-1);
  123. subV= v & (SUB_PIXELS-1);
  124. u >>= SUB_PIXEL_BITS;
  125. v >>= SUB_PIXEL_BITS;
  126. if(u>0 && v>0 && u<w-2 && v<h-2){
  127. const int index= u + v*srcStride;
  128. const int a= priv.coeff[subU][0];
  129. const int b= priv.coeff[subU][1];
  130. const int c= priv.coeff[subU][2];
  131. const int d= priv.coeff[subU][3];
  132. sum=
  133. priv.coeff[subV][0]*( a*src[index - 1 - srcStride] + b*src[index - 0 - srcStride]
  134. + c*src[index + 1 - srcStride] + d*src[index + 2 - srcStride])
  135. +priv.coeff[subV][1]*( a*src[index - 1 ] + b*src[index - 0 ]
  136. + c*src[index + 1 ] + d*src[index + 2 ])
  137. +priv.coeff[subV][2]*( a*src[index - 1 + srcStride] + b*src[index - 0 + srcStride]
  138. + c*src[index + 1 + srcStride] + d*src[index + 2 + srcStride])
  139. +priv.coeff[subV][3]*( a*src[index - 1+2*srcStride] + b*src[index - 0+2*srcStride]
  140. + c*src[index + 1+2*srcStride] + d*src[index + 2+2*srcStride]);
  141. }else{
  142. int dx, dy;
  143. sum=0;
  144. for(dy=0; dy<4; dy++){
  145. int iy= v + dy - 1;
  146. if (iy< 0) iy=0;
  147. else if(iy>=h) iy=h-1;
  148. for(dx=0; dx<4; dx++){
  149. int ix= u + dx - 1;
  150. if (ix< 0) ix=0;
  151. else if(ix>=w) ix=w-1;
  152. sum+= priv.coeff[subU][dx]*priv.coeff[subV][dy]
  153. *src[ ix + iy*srcStride];
  154. }
  155. }
  156. }
  157. sum= (sum + (1<<(COEFF_BITS*2-1)) ) >> (COEFF_BITS*2);
  158. if(sum&~255){
  159. if(sum<0) sum=0;
  160. else sum=255;
  161. }
  162. dst[ x + y*dstStride]= sum;
  163. }
  164. }
  165. }
  166. static inline void resampleLinear(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride,
  167. struct vf_priv_s *privParam, int xShift, int yShift){
  168. int x, y;
  169. struct vf_priv_s priv= *privParam;
  170. for(y=0; y<h; y++){
  171. for(x=0; x<w; x++){
  172. int u, v, subU, subV, sum, sx, sy, index, subUI, subVI;
  173. sx= x << xShift;
  174. sy= y << yShift;
  175. u= priv.pv[sx + sy*priv.pvStride][0]>>xShift;
  176. v= priv.pv[sx + sy*priv.pvStride][1]>>yShift;
  177. subU= u & (SUB_PIXELS-1);
  178. subV= v & (SUB_PIXELS-1);
  179. u >>= SUB_PIXEL_BITS;
  180. v >>= SUB_PIXEL_BITS;
  181. index= u + v*srcStride;
  182. subUI= SUB_PIXELS - subU;
  183. subVI= SUB_PIXELS - subV;
  184. if((unsigned)u < (unsigned)(w - 1)){
  185. if((unsigned)v < (unsigned)(h - 1)){
  186. sum= subVI*(subUI*src[index ] + subU*src[index +1])
  187. +subV *(subUI*src[index+srcStride] + subU*src[index+srcStride+1]);
  188. sum= (sum + (1<<(SUB_PIXEL_BITS*2-1)) ) >> (SUB_PIXEL_BITS*2);
  189. }else{
  190. if(v<0) v= 0;
  191. else v= h-1;
  192. index= u + v*srcStride;
  193. sum= subUI*src[index] + subU*src[index+1];
  194. sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
  195. }
  196. }else{
  197. if((unsigned)v < (unsigned)(h - 1)){
  198. if(u<0) u= 0;
  199. else u= w-1;
  200. index= u + v*srcStride;
  201. sum= subVI*src[index] + subV*src[index+srcStride];
  202. sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS;
  203. }else{
  204. if(u<0) u= 0;
  205. else u= w-1;
  206. if(v<0) v= 0;
  207. else v= h-1;
  208. index= u + v*srcStride;
  209. sum= src[index];
  210. }
  211. }
  212. if(sum&~255){
  213. if(sum<0) sum=0;
  214. else sum=255;
  215. }
  216. dst[ x + y*dstStride]= sum;
  217. }
  218. }
  219. }
  220. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  221. int cw= mpi->w >> mpi->chroma_x_shift;
  222. int ch= mpi->h >> mpi->chroma_y_shift;
  223. mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
  224. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  225. mpi->w,mpi->h);
  226. assert(mpi->flags&MP_IMGFLAG_PLANAR);
  227. if(vf->priv->cubic){
  228. resampleCubic(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
  229. vf->priv, 0, 0);
  230. resampleCubic(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1],
  231. vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
  232. resampleCubic(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2],
  233. vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
  234. }else{
  235. resampleLinear(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0],
  236. vf->priv, 0, 0);
  237. resampleLinear(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1],
  238. vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
  239. resampleLinear(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2],
  240. vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift);
  241. }
  242. return vf_next_put_image(vf,dmpi, pts);
  243. }
  244. //===========================================================================//
  245. static int query_format(struct vf_instance *vf, unsigned int fmt){
  246. switch(fmt)
  247. {
  248. case IMGFMT_YV12:
  249. case IMGFMT_I420:
  250. case IMGFMT_IYUV:
  251. case IMGFMT_YVU9:
  252. case IMGFMT_444P:
  253. case IMGFMT_422P:
  254. case IMGFMT_411P:
  255. return vf_next_query_format(vf, fmt);
  256. }
  257. return 0;
  258. }
  259. static int vf_open(vf_instance_t *vf, char *args){
  260. int e;
  261. vf->config=config;
  262. vf->put_image=put_image;
  263. // vf->get_image=get_image;
  264. vf->query_format=query_format;
  265. vf->uninit=uninit;
  266. vf->priv=malloc(sizeof(struct vf_priv_s));
  267. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  268. if(args==NULL) return 0;
  269. e=sscanf(args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf:%d",
  270. &vf->priv->ref[0][0], &vf->priv->ref[0][1],
  271. &vf->priv->ref[1][0], &vf->priv->ref[1][1],
  272. &vf->priv->ref[2][0], &vf->priv->ref[2][1],
  273. &vf->priv->ref[3][0], &vf->priv->ref[3][1],
  274. &vf->priv->cubic
  275. );
  276. if(e!=9)
  277. return 0;
  278. return 1;
  279. }
  280. const vf_info_t vf_info_perspective = {
  281. "perspective correcture",
  282. "perspective",
  283. "Michael Niedermayer",
  284. "",
  285. vf_open,
  286. NULL
  287. };
  288. //===========================================================================//