2 * Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of MPlayer.
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.
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.
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.
35 #include "libavutil/mem.h"
37 #include "img_format.h"
41 #define SUB_PIXEL_BITS 8
42 #define SUB_PIXELS (1<<SUB_PIXEL_BITS)
45 //===========================================================================//
49 int32_t coeff
[1<<SUB_PIXEL_BITS
][4];
56 /***************************************************************************/
58 static void initPv(struct vf_priv_s
*priv
, int W
, int H
){
59 double a
,b
,c
,d
,e
,f
,g
,h
,D
;
60 double (*ref
)[2]= priv
->ref
;
63 g
= ( (ref
[0][0] - ref
[1][0] - ref
[2][0] + ref
[3][0])*(ref
[2][1] - ref
[3][1])
64 - (ref
[0][1] - ref
[1][1] - ref
[2][1] + ref
[3][1])*(ref
[2][0] - ref
[3][0]))*H
;
65 h
= ( (ref
[0][1] - ref
[1][1] - ref
[2][1] + ref
[3][1])*(ref
[1][0] - ref
[3][0])
66 - (ref
[0][0] - ref
[1][0] - ref
[2][0] + ref
[3][0])*(ref
[1][1] - ref
[3][1]))*W
;
67 D
= (ref
[1][0] - ref
[3][0])*(ref
[2][1] - ref
[3][1])
68 - (ref
[2][0] - ref
[3][0])*(ref
[1][1] - ref
[3][1]);
70 a
= D
*(ref
[1][0] - ref
[0][0])*H
+ g
*ref
[1][0];
71 b
= D
*(ref
[2][0] - ref
[0][0])*W
+ h
*ref
[2][0];
73 d
= D
*(ref
[1][1] - ref
[0][1])*H
+ g
*ref
[1][1];
74 e
= D
*(ref
[2][1] - ref
[0][1])*W
+ h
*ref
[2][1];
81 u
= (int)floor( SUB_PIXELS
*(a
*x
+ b
*y
+ c
)/(g
*x
+ h
*y
+ D
*W
*H
) + 0.5);
82 v
= (int)floor( SUB_PIXELS
*(d
*x
+ e
*y
+ f
)/(g
*x
+ h
*y
+ D
*W
*H
) + 0.5);
84 priv
->pv
[x
+ y
*W
][0]= u
;
85 priv
->pv
[x
+ y
*W
][1]= v
;
90 static double getCoeff(double d
){
96 // Equation is from VirtualDub
98 coeff
= (1.0 - (A
+3.0)*d
*d
+ (A
+2.0)*d
*d
*d
);
100 coeff
= (-4.0*A
+ 8.0*A
*d
- 5.0*A
*d
*d
+ A
*d
*d
*d
);
107 static int config(struct vf_instance
*vf
,
108 int width
, int height
, int d_width
, int d_height
,
109 unsigned int flags
, unsigned int outfmt
){
112 vf
->priv
->pvStride
= width
;
113 vf
->priv
->pv
= av_malloc(width
*height
*2*sizeof(int32_t));
114 initPv(vf
->priv
, width
, height
);
116 for(i
=0; i
<SUB_PIXELS
; i
++){
117 double d
= i
/(double)SUB_PIXELS
;
122 temp
[j
]= getCoeff(j
- d
- 1);
128 vf
->priv
->coeff
[i
][j
]= (int)floor((1<<COEFF_BITS
)*temp
[j
]/sum
+ 0.5);
131 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
134 static void uninit(struct vf_instance
*vf
){
135 if(!vf
->priv
) return;
137 av_free(vf
->priv
->pv
);
144 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
){
146 struct vf_priv_s priv
= *privParam
;
150 int u
, v
, subU
, subV
, sum
, sx
, sy
;
154 u
= priv
.pv
[sx
+ sy
*priv
.pvStride
][0]>>xShift
;
155 v
= priv
.pv
[sx
+ sy
*priv
.pvStride
][1]>>yShift
;
156 subU
= u
& (SUB_PIXELS
-1);
157 subV
= v
& (SUB_PIXELS
-1);
158 u
>>= SUB_PIXEL_BITS
;
159 v
>>= SUB_PIXEL_BITS
;
161 if(u
>0 && v
>0 && u
<w
-2 && v
<h
-2){
162 const int index
= u
+ v
*srcStride
;
163 const int a
= priv
.coeff
[subU
][0];
164 const int b
= priv
.coeff
[subU
][1];
165 const int c
= priv
.coeff
[subU
][2];
166 const int d
= priv
.coeff
[subU
][3];
169 priv
.coeff
[subV
][0]*( 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
][1]*( a
*src
[index
- 1 ] + b
*src
[index
- 0 ]
172 + c
*src
[index
+ 1 ] + d
*src
[index
+ 2 ])
173 +priv
.coeff
[subV
][2]*( a
*src
[index
- 1 + srcStride
] + b
*src
[index
- 0 + srcStride
]
174 + c
*src
[index
+ 1 + srcStride
] + d
*src
[index
+ 2 + srcStride
])
175 +priv
.coeff
[subV
][3]*( a
*src
[index
- 1+2*srcStride
] + b
*src
[index
- 0+2*srcStride
]
176 + c
*src
[index
+ 1+2*srcStride
] + d
*src
[index
+ 2+2*srcStride
]);
181 for(dy
=0; dy
<4; dy
++){
184 else if(iy
>=h
) iy
=h
-1;
185 for(dx
=0; dx
<4; dx
++){
188 else if(ix
>=w
) ix
=w
-1;
190 sum
+= priv
.coeff
[subU
][dx
]*priv
.coeff
[subV
][dy
]
191 *src
[ ix
+ iy
*srcStride
];
195 sum
= (sum
+ (1<<(COEFF_BITS
*2-1)) ) >> (COEFF_BITS
*2);
200 dst
[ x
+ y
*dstStride
]= sum
;
205 static inline void resampleLinear(uint8_t *dst
, uint8_t *src
, int w
, int h
, int dstStride
, int srcStride
,
206 struct vf_priv_s
*privParam
, int xShift
, int yShift
){
208 struct vf_priv_s priv
= *privParam
;
212 int u
, v
, subU
, subV
, sum
, sx
, sy
, index
, subUI
, subVI
;
216 u
= priv
.pv
[sx
+ sy
*priv
.pvStride
][0]>>xShift
;
217 v
= priv
.pv
[sx
+ sy
*priv
.pvStride
][1]>>yShift
;
218 subU
= u
& (SUB_PIXELS
-1);
219 subV
= v
& (SUB_PIXELS
-1);
220 u
>>= SUB_PIXEL_BITS
;
221 v
>>= SUB_PIXEL_BITS
;
222 index
= u
+ v
*srcStride
;
223 subUI
= SUB_PIXELS
- subU
;
224 subVI
= SUB_PIXELS
- subV
;
226 if((unsigned)u
< (unsigned)(w
- 1)){
227 if((unsigned)v
< (unsigned)(h
- 1)){
228 sum
= subVI
*(subUI
*src
[index
] + subU
*src
[index
+1])
229 +subV
*(subUI
*src
[index
+srcStride
] + subU
*src
[index
+srcStride
+1]);
230 sum
= (sum
+ (1<<(SUB_PIXEL_BITS
*2-1)) ) >> (SUB_PIXEL_BITS
*2);
234 index
= u
+ v
*srcStride
;
235 sum
= subUI
*src
[index
] + subU
*src
[index
+1];
236 sum
= (sum
+ (1<<(SUB_PIXEL_BITS
-1)) ) >> SUB_PIXEL_BITS
;
239 if((unsigned)v
< (unsigned)(h
- 1)){
242 index
= u
+ v
*srcStride
;
243 sum
= subVI
*src
[index
] + subV
*src
[index
+srcStride
];
244 sum
= (sum
+ (1<<(SUB_PIXEL_BITS
-1)) ) >> SUB_PIXEL_BITS
;
250 index
= u
+ v
*srcStride
;
258 dst
[ x
+ y
*dstStride
]= sum
;
263 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
){
264 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
265 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
267 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
268 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
271 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
274 resampleCubic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0],
276 resampleCubic(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1],
277 vf
->priv
, mpi
->chroma_x_shift
, mpi
->chroma_y_shift
);
278 resampleCubic(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2],
279 vf
->priv
, mpi
->chroma_x_shift
, mpi
->chroma_y_shift
);
281 resampleLinear(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0],
283 resampleLinear(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1],
284 vf
->priv
, mpi
->chroma_x_shift
, mpi
->chroma_y_shift
);
285 resampleLinear(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2],
286 vf
->priv
, mpi
->chroma_x_shift
, mpi
->chroma_y_shift
);
289 return vf_next_put_image(vf
,dmpi
, pts
);
292 //===========================================================================//
294 static int query_format(struct vf_instance
*vf
, unsigned int fmt
){
304 return vf_next_query_format(vf
, fmt
);
309 static int vf_open(vf_instance_t
*vf
, char *args
){
313 vf
->put_image
=put_image
;
314 // vf->get_image=get_image;
315 vf
->query_format
=query_format
;
317 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
318 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
320 if(args
==NULL
) return 0;
322 e
=sscanf(args
, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf:%d",
323 &vf
->priv
->ref
[0][0], &vf
->priv
->ref
[0][1],
324 &vf
->priv
->ref
[1][0], &vf
->priv
->ref
[1][1],
325 &vf
->priv
->ref
[2][0], &vf
->priv
->ref
[2][1],
326 &vf
->priv
->ref
[3][0], &vf
->priv
->ref
[3][1],
336 const vf_info_t vf_info_perspective
= {
337 "perspective correcture",
339 "Michael Niedermayer",
345 //===========================================================================//