Mercurial > mplayer.hg
annotate libmpcodecs/vf_perspective.c @ 26425:3357581cefa5
10l: Rename remaining instances of $i to $lang.
patch by Andrew Savchenko, Bircoph list ru
author | diego |
---|---|
date | Fri, 18 Apr 2008 07:16:22 +0000 |
parents | 00fff9a3b735 |
children | 82601a38e2a7 |
rev | line source |
---|---|
8112 | 1 /* |
2 Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at> | |
3 | |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2 of the License, or | |
7 (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program; if not, write to the Free Software | |
17367
401b440a6d76
Update licensing information: The FSF changed postal address.
diego
parents:
17012
diff
changeset
|
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
8112 | 17 */ |
18 | |
19 #include <stdio.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 #include <inttypes.h> | |
23 #include <assert.h> | |
24 #include <math.h> | |
25 | |
17012 | 26 #include "config.h" |
27 #include "mp_msg.h" | |
8112 | 28 |
29 #ifdef HAVE_MALLOC_H | |
30 #include <malloc.h> | |
31 #endif | |
32 | |
33 #include "img_format.h" | |
34 #include "mp_image.h" | |
35 #include "vf.h" | |
36 | |
37 #define SUB_PIXEL_BITS 8 | |
38 #define SUB_PIXELS (1<<SUB_PIXEL_BITS) | |
39 #define COEFF_BITS 11 | |
40 | |
41 //===========================================================================// | |
42 | |
43 struct vf_priv_s { | |
44 double ref[4][2]; | |
45 int32_t coeff[1<<SUB_PIXEL_BITS][4]; | |
46 int32_t (*pv)[2]; | |
47 int pvStride; | |
48 int cubic; | |
49 }; | |
50 | |
51 | |
52 /***************************************************************************/ | |
53 | |
54 static void initPv(struct vf_priv_s *priv, int W, int H){ | |
55 double a,b,c,d,e,f,g,h,D; | |
56 double (*ref)[2]= priv->ref; | |
57 int x,y; | |
58 | |
59 g= ( (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[2][1] - ref[3][1]) | |
60 - (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[2][0] - ref[3][0]))*H; | |
61 h= ( (ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1])*(ref[1][0] - ref[3][0]) | |
62 - (ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0])*(ref[1][1] - ref[3][1]))*W; | |
63 D= (ref[1][0] - ref[3][0])*(ref[2][1] - ref[3][1]) | |
64 - (ref[2][0] - ref[3][0])*(ref[1][1] - ref[3][1]); | |
65 | |
66 a= D*(ref[1][0] - ref[0][0])*H + g*ref[1][0]; | |
67 b= D*(ref[2][0] - ref[0][0])*W + h*ref[2][0]; | |
68 c= D*ref[0][0]*W*H; | |
69 d= D*(ref[1][1] - ref[0][1])*H + g*ref[1][1]; | |
70 e= D*(ref[2][1] - ref[0][1])*W + h*ref[2][1]; | |
71 f= D*ref[0][1]*W*H; | |
72 | |
73 for(y=0; y<H; y++){ | |
74 for(x=0; x<W; x++){ | |
75 int u, v; | |
76 | |
77 u= (int)floor( SUB_PIXELS*(a*x + b*y + c)/(g*x + h*y + D*W*H) + 0.5); | |
78 v= (int)floor( SUB_PIXELS*(d*x + e*y + f)/(g*x + h*y + D*W*H) + 0.5); | |
79 | |
80 priv->pv[x + y*W][0]= u; | |
81 priv->pv[x + y*W][1]= v; | |
82 } | |
83 } | |
84 } | |
85 | |
86 static double getCoeff(double d){ | |
87 double A= -0.60; | |
88 double coeff; | |
89 | |
90 d= fabs(d); | |
91 | |
92 // Equation is from VirtualDub | |
93 if(d<1.0) | |
94 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d); | |
95 else if(d<2.0) | |
96 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d); | |
97 else | |
98 coeff=0.0; | |
99 | |
100 return coeff; | |
101 } | |
102 | |
103 static int config(struct vf_instance_s* vf, | |
104 int width, int height, int d_width, int d_height, | |
105 unsigned int flags, unsigned int outfmt){ | |
106 int i, j; | |
107 | |
108 vf->priv->pvStride= width; | |
109 vf->priv->pv= (void*)memalign(8, width*height*2*sizeof(int32_t)); | |
110 initPv(vf->priv, width, height); | |
111 | |
112 for(i=0; i<SUB_PIXELS; i++){ | |
113 double d= i/(double)SUB_PIXELS; | |
114 double temp[4]; | |
115 double sum=0; | |
116 | |
117 for(j=0; j<4; j++) | |
118 temp[j]= getCoeff(j - d - 1); | |
119 | |
120 for(j=0; j<4; j++) | |
121 sum+= temp[j]; | |
122 | |
123 for(j=0; j<4; j++) | |
124 vf->priv->coeff[i][j]= (int)floor((1<<COEFF_BITS)*temp[j]/sum + 0.5); | |
125 } | |
126 | |
127 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
128 } | |
129 | |
130 static void uninit(struct vf_instance_s* vf){ | |
131 if(!vf->priv) return; | |
132 | |
133 if(vf->priv->pv) free(vf->priv->pv); | |
134 vf->priv->pv= NULL; | |
135 | |
136 free(vf->priv); | |
137 vf->priv=NULL; | |
138 } | |
139 | |
140 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){ | |
141 int x, y; | |
142 struct vf_priv_s priv= *privParam; | |
143 | |
144 for(y=0; y<h; y++){ | |
145 for(x=0; x<w; x++){ | |
146 int u, v, subU, subV, sum, sx, sy; | |
147 | |
148 sx= x << xShift; | |
149 sy= y << yShift; | |
150 u= priv.pv[sx + sy*priv.pvStride][0]>>xShift; | |
151 v= priv.pv[sx + sy*priv.pvStride][1]>>yShift; | |
152 subU= u & (SUB_PIXELS-1); | |
153 subV= v & (SUB_PIXELS-1); | |
154 u >>= SUB_PIXEL_BITS; | |
155 v >>= SUB_PIXEL_BITS; | |
156 | |
157 if(u>0 && v>0 && u<w-2 && v<h-2){ | |
158 const int index= u + v*srcStride; | |
159 const int a= priv.coeff[subU][0]; | |
160 const int b= priv.coeff[subU][1]; | |
161 const int c= priv.coeff[subU][2]; | |
162 const int d= priv.coeff[subU][3]; | |
163 | |
164 sum= | |
165 priv.coeff[subV][0]*( a*src[index - 1 - srcStride] + b*src[index - 0 - srcStride] | |
166 + c*src[index + 1 - srcStride] + d*src[index + 2 - srcStride]) | |
167 +priv.coeff[subV][1]*( a*src[index - 1 ] + b*src[index - 0 ] | |
168 + c*src[index + 1 ] + d*src[index + 2 ]) | |
169 +priv.coeff[subV][2]*( a*src[index - 1 + srcStride] + b*src[index - 0 + srcStride] | |
170 + c*src[index + 1 + srcStride] + d*src[index + 2 + srcStride]) | |
171 +priv.coeff[subV][3]*( a*src[index - 1+2*srcStride] + b*src[index - 0+2*srcStride] | |
172 + c*src[index + 1+2*srcStride] + d*src[index + 2+2*srcStride]); | |
173 }else{ | |
174 int dx, dy; | |
175 sum=0; | |
176 | |
177 for(dy=0; dy<4; dy++){ | |
178 int iy= v + dy - 1; | |
179 if (iy< 0) iy=0; | |
180 else if(iy>=h) iy=h-1; | |
181 for(dx=0; dx<4; dx++){ | |
182 int ix= u + dx - 1; | |
183 if (ix< 0) ix=0; | |
184 else if(ix>=w) ix=w-1; | |
185 | |
186 sum+= priv.coeff[subU][dx]*priv.coeff[subV][dy] | |
187 *src[ ix + iy*srcStride]; | |
188 } | |
189 } | |
190 } | |
191 sum= (sum + (1<<(COEFF_BITS*2-1)) ) >> (COEFF_BITS*2); | |
192 if(sum&~255){ | |
193 if(sum<0) sum=0; | |
194 else sum=255; | |
195 } | |
196 dst[ x + y*dstStride]= sum; | |
197 } | |
198 } | |
199 } | |
200 | |
201 static inline void resampleLinear(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, | |
202 struct vf_priv_s *privParam, int xShift, int yShift){ | |
203 int x, y; | |
204 struct vf_priv_s priv= *privParam; | |
205 | |
206 for(y=0; y<h; y++){ | |
207 for(x=0; x<w; x++){ | |
208 int u, v, subU, subV, sum, sx, sy, index, subUI, subVI; | |
209 | |
210 sx= x << xShift; | |
211 sy= y << yShift; | |
212 u= priv.pv[sx + sy*priv.pvStride][0]>>xShift; | |
213 v= priv.pv[sx + sy*priv.pvStride][1]>>yShift; | |
214 subU= u & (SUB_PIXELS-1); | |
215 subV= v & (SUB_PIXELS-1); | |
216 u >>= SUB_PIXEL_BITS; | |
217 v >>= SUB_PIXEL_BITS; | |
218 index= u + v*srcStride; | |
219 subUI= SUB_PIXELS - subU; | |
220 subVI= SUB_PIXELS - subV; | |
221 | |
222 if((unsigned)u < (unsigned)(w - 1)){ | |
223 if((unsigned)v < (unsigned)(h - 1)){ | |
224 sum= subVI*(subUI*src[index ] + subU*src[index +1]) | |
225 +subV *(subUI*src[index+srcStride] + subU*src[index+srcStride+1]); | |
226 sum= (sum + (1<<(SUB_PIXEL_BITS*2-1)) ) >> (SUB_PIXEL_BITS*2); | |
227 }else{ | |
228 if(v<0) v= 0; | |
229 else v= h-1; | |
230 index= u + v*srcStride; | |
231 sum= subUI*src[index] + subU*src[index+1]; | |
232 sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS; | |
233 } | |
234 }else{ | |
235 if((unsigned)v < (unsigned)(h - 1)){ | |
236 if(u<0) u= 0; | |
237 else u= w-1; | |
238 index= u + v*srcStride; | |
239 sum= subVI*src[index] + subV*src[index+srcStride]; | |
240 sum= (sum + (1<<(SUB_PIXEL_BITS-1)) ) >> SUB_PIXEL_BITS; | |
241 }else{ | |
242 if(u<0) u= 0; | |
243 else u= w-1; | |
244 if(v<0) v= 0; | |
245 else v= h-1; | |
246 index= u + v*srcStride; | |
247 sum= src[index]; | |
248 } | |
249 } | |
250 if(sum&~255){ | |
251 if(sum<0) sum=0; | |
252 else sum=255; | |
253 } | |
254 dst[ x + y*dstStride]= sum; | |
255 } | |
256 } | |
257 } | |
258 | |
17906
20aca9baf5d8
passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents:
17367
diff
changeset
|
259 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){ |
8112 | 260 int cw= mpi->w >> mpi->chroma_x_shift; |
261 int ch= mpi->h >> mpi->chroma_y_shift; | |
262 | |
263 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
264 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, | |
265 mpi->w,mpi->h); | |
266 | |
267 assert(mpi->flags&MP_IMGFLAG_PLANAR); | |
268 | |
269 if(vf->priv->cubic){ | |
270 resampleCubic(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], | |
271 vf->priv, 0, 0); | |
272 resampleCubic(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], | |
273 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift); | |
274 resampleCubic(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], | |
275 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift); | |
276 }else{ | |
277 resampleLinear(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], | |
278 vf->priv, 0, 0); | |
279 resampleLinear(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], | |
280 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift); | |
281 resampleLinear(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], | |
282 vf->priv, mpi->chroma_x_shift, mpi->chroma_y_shift); | |
283 } | |
284 | |
17906
20aca9baf5d8
passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents:
17367
diff
changeset
|
285 return vf_next_put_image(vf,dmpi, pts); |
8112 | 286 } |
287 | |
288 //===========================================================================// | |
289 | |
290 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
291 switch(fmt) | |
292 { | |
293 case IMGFMT_YV12: | |
294 case IMGFMT_I420: | |
295 case IMGFMT_IYUV: | |
296 case IMGFMT_YVU9: | |
297 case IMGFMT_444P: | |
298 case IMGFMT_422P: | |
299 case IMGFMT_411P: | |
300 return vf_next_query_format(vf, fmt); | |
301 } | |
302 return 0; | |
303 } | |
304 | |
305 static int open(vf_instance_t *vf, char* args){ | |
306 int e; | |
307 | |
308 vf->config=config; | |
309 vf->put_image=put_image; | |
310 // vf->get_image=get_image; | |
311 vf->query_format=query_format; | |
312 vf->uninit=uninit; | |
313 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
314 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
315 | |
316 if(args==NULL) return 0; | |
317 | |
318 e=sscanf(args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf:%d", | |
319 &vf->priv->ref[0][0], &vf->priv->ref[0][1], | |
320 &vf->priv->ref[1][0], &vf->priv->ref[1][1], | |
321 &vf->priv->ref[2][0], &vf->priv->ref[2][1], | |
322 &vf->priv->ref[3][0], &vf->priv->ref[3][1], | |
323 &vf->priv->cubic | |
324 ); | |
325 | |
326 if(e!=9) | |
327 return 0; | |
328 | |
329 return 1; | |
330 } | |
331 | |
25221 | 332 const vf_info_t vf_info_perspective = { |
8112 | 333 "perspective correcture", |
334 "perspective", | |
335 "Michael Niedermayer", | |
336 "", | |
9593
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
8112
diff
changeset
|
337 open, |
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
8112
diff
changeset
|
338 NULL |
8112 | 339 }; |
340 | |
341 //===========================================================================// |