2 Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "img_format.h"
37 #define SUB_PIXEL_BITS 8
38 #define SUB_PIXELS (1<<SUB_PIXEL_BITS)
41 //===========================================================================//
45 int32_t coeff
[1<<SUB_PIXEL_BITS
][4];
52 /***************************************************************************/
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
;
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]);
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];
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];
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);
80 priv
->pv
[x
+ y
*W
][0]= u
;
81 priv
->pv
[x
+ y
*W
][1]= v
;
86 static double getCoeff(double d
){
92 // Equation is from VirtualDub
94 coeff
= (1.0 - (A
+3.0)*d
*d
+ (A
+2.0)*d
*d
*d
);
96 coeff
= (-4.0*A
+ 8.0*A
*d
- 5.0*A
*d
*d
+ A
*d
*d
*d
);
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
){
108 vf
->priv
->pvStride
= width
;
109 vf
->priv
->pv
= (void*)memalign(8, width
*height
*2*sizeof(int32_t));
110 initPv(vf
->priv
, width
, height
);
112 for(i
=0; i
<SUB_PIXELS
; i
++){
113 double d
= i
/(double)SUB_PIXELS
;
118 temp
[j
]= getCoeff(j
- d
- 1);
124 vf
->priv
->coeff
[i
][j
]= (int)floor((1<<COEFF_BITS
)*temp
[j
]/sum
+ 0.5);
127 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
130 static void uninit(struct vf_instance_s
* vf
){
131 if(!vf
->priv
) return;
133 if(vf
->priv
->pv
) free(vf
->priv
->pv
);
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
){
142 struct vf_priv_s priv
= *privParam
;
146 int u
, v
, subU
, subV
, sum
, sx
, sy
;
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
;
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];
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
]);
177 for(dy
=0; dy
<4; dy
++){
180 else if(iy
>=h
) iy
=h
-1;
181 for(dx
=0; dx
<4; dx
++){
184 else if(ix
>=w
) ix
=w
-1;
186 sum
+= priv
.coeff
[subU
][dx
]*priv
.coeff
[subV
][dy
]
187 *src
[ ix
+ iy
*srcStride
];
191 sum
= (sum
+ (1<<(COEFF_BITS
*2-1)) ) >> (COEFF_BITS
*2);
196 dst
[ x
+ y
*dstStride
]= sum
;
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
){
204 struct vf_priv_s priv
= *privParam
;
208 int u
, v
, subU
, subV
, sum
, sx
, sy
, index
, subUI
, subVI
;
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
;
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);
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
;
235 if((unsigned)v
< (unsigned)(h
- 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
;
246 index
= u
+ v
*srcStride
;
254 dst
[ x
+ y
*dstStride
]= sum
;
259 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
260 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
261 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
263 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
264 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
267 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
270 resampleCubic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[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
);
277 resampleLinear(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[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
);
285 return vf_next_put_image(vf
,dmpi
, pts
);
288 //===========================================================================//
290 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
300 return vf_next_query_format(vf
, fmt
);
305 static int open(vf_instance_t
*vf
, char* args
){
309 vf
->put_image
=put_image
;
310 // vf->get_image=get_image;
311 vf
->query_format
=query_format
;
313 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
314 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
316 if(args
==NULL
) return 0;
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],
332 const vf_info_t vf_info_perspective
= {
333 "perspective correcture",
335 "Michael Niedermayer",
341 //===========================================================================//