31 #define SUB_PIXEL_BITS 8
32 #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
51 void *
arg,
int job,
int nb_jobs);
54 #define OFFSET(x) offsetof(PerspectiveContext, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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 const char *
const var_names[] = {
"W",
"H", NULL };
108 double x0, x1, x2, x3, x4, x5, x6, x7, q;
111 double (*ref)[2] = s->
ref;
118 for (i = 0; i < 4; i++) {
119 for (j = 0; j < 2; j++) {
124 NULL, NULL, NULL, NULL,
144 x6 = ((ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
145 (ref[2][1] - ref[3][1]) -
146 ( ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
147 (ref[2][0] - ref[3][0])) * h;
148 x7 = ((ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
149 (ref[1][0] - ref[3][0]) -
150 ( ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
151 (ref[1][1] - ref[3][1])) * w;
152 q = ( ref[1][0] - ref[3][0]) * (ref[2][1] - ref[3][1]) -
153 ( ref[2][0] - ref[3][0]) * (ref[1][1] - ref[3][1]);
155 x0 = q * (ref[1][0] - ref[0][0]) * h + x6 * ref[1][0];
156 x1 = q * (ref[2][0] - ref[0][0]) * w + x7 * ref[2][0];
157 x2 = q * ref[0][0] * w * h;
158 x3 = q * (ref[1][1] - ref[0][1]) * h + x6 * ref[1][1];
159 x4 = q * (ref[2][1] - ref[0][1]) * w + x7 * ref[2][1];
160 x5 = q * ref[0][1] * w * h;
162 for (y = 0; y < h; y++){
163 for (x = 0; x < w; x++){
166 u = (int)floor(
SUB_PIXELS * (x0 * x + x1 * y + x2) /
167 (x6 * x + x7 * y + q * w * h) + 0.5);
168 v = (int)floor(
SUB_PIXELS * (x3 * x + x4 * y + x5) /
169 (x6 * x + x7 * y + q * w * h) + 0.5);
171 s->
pv[x + y * w][0] =
u;
172 s->
pv[x + y * w][1] =
v;
177 double d = i / (double)SUB_PIXELS;
181 for (j = 0; j < 4; j++)
184 for (j = 0; j < 4; j++)
187 for (j = 0; j < 4; j++)
204 int job,
int nb_jobs)
216 int start = (h * job) / nb_jobs;
217 int end = (h * (job+1)) / nb_jobs;
218 const int linesize = s->
linesize[0];
221 for (y = start; y <
end; y++) {
223 for (x = 0; x < w; x++) {
224 int u,
v, subU, subV, sum, sx;
227 u = s->
pv[sx + sy * linesize][0] >> hsub;
228 v = s->
pv[sx + sy * linesize][1] >> vsub;
234 if (u > 0 && v > 0 && u < w - 2 && v < h - 2){
235 const int index = u + v*src_linesize;
236 const int a = s->
coeff[subU][0];
237 const int b = s->
coeff[subU][1];
238 const int c = s->
coeff[subU][2];
239 const int d = s->
coeff[subU][3];
241 sum = s->
coeff[subV][0] * (a * src[index - 1 - src_linesize] + b * src[index - 0 - src_linesize] +
242 c * src[index + 1 - src_linesize] + d * src[index + 2 - src_linesize]) +
243 s->
coeff[subV][1] * (a * src[index - 1 ] + b * src[index - 0 ] +
244 c * src[index + 1 ] + d * src[index + 2 ]) +
245 s->
coeff[subV][2] * (a * src[index - 1 + src_linesize] + b * src[index - 0 + src_linesize] +
246 c * src[index + 1 + src_linesize] + d * src[index + 2 + src_linesize]) +
247 s->
coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize] +
248 c * src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]);
254 for (dy = 0; dy < 4; dy++) {
261 for (dx = 0; dx < 4; dx++) {
269 sum += s->
coeff[subU][dx] * s->
coeff[subV][dy] * src[ ix + iy * src_linesize];
275 sum = av_clip(sum, 0, 255);
276 dst[x + y * dst_linesize] = sum;
283 int job,
int nb_jobs)
295 int start = (h * job) / nb_jobs;
296 int end = (h * (job+1)) / nb_jobs;
297 const int linesize = s->
linesize[0];
300 for (y = start; y <
end; y++){
302 for (x = 0; x < w; x++){
303 int u,
v, subU, subV, sum, sx,
index, subUI, subVI;
306 u = s->
pv[sx + sy * linesize][0] >> hsub;
307 v = s->
pv[sx + sy * linesize][1] >> vsub;
313 index = u + v * src_linesize;
317 if ((
unsigned)u < (unsigned)(w - 1)){
318 if((
unsigned)v < (unsigned)(h - 1)){
319 sum = subVI * (subUI * src[
index] + subU * src[index + 1]) +
320 subV * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]);
327 index = u + v * src_linesize;
328 sum = subUI * src[
index] + subU * src[index + 1];
336 if ((
unsigned)v < (unsigned)(h - 1)){
337 index = u + v * src_linesize;
338 sum = subVI * src[
index] + subV * src[index + src_linesize];
345 index = u + v * src_linesize;
350 sum = av_clip(sum, 0, 255);
351 dst[x + y * dst_linesize] = sum;
384 for (plane = 0; plane < s->
nb_planes; plane++) {
385 int hsub = plane == 1 || plane == 2 ? s->
hsub : 0;
386 int vsub = plane == 1 || plane == 2 ? s->
vsub : 0;
388 .dst_linesize = out->
linesize[plane],
389 .src = frame->
data[plane],
390 .src_linesize = frame->
linesize[plane],
428 .
name =
"perspective",
434 .
inputs = perspective_inputs,
435 .
outputs = perspective_outputs,
436 .priv_class = &perspective_class,