32 #define SUB_PIXEL_BITS 8
33 #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
53 void *
arg,
int job,
int nb_jobs);
56 #define OFFSET(x) offsetof(PerspectiveContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
77 {
"source",
"specify locations in source to send to corners in destination",
79 {
"destination",
"specify locations in destination to send corners of source",
107 coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
109 coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
121 double x0, x1, x2, x3, x4, x5, x6, x7, x8, q;
125 double (*ref)[2] = s->
ref;
132 for (i = 0; i < 4; i++) {
133 for (j = 0; j < 2; j++) {
160 x6 = ((ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
161 (ref[2][1] - ref[3][1]) -
162 ( ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
163 (ref[2][0] - ref[3][0])) * h;
164 x7 = ((ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
165 (ref[1][0] - ref[3][0]) -
166 ( ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
167 (ref[1][1] - ref[3][1])) * w;
168 q = ( ref[1][0] - ref[3][0]) * (ref[2][1] - ref[3][1]) -
169 ( ref[2][0] - ref[3][0]) * (ref[1][1] - ref[3][1]);
171 x0 = q * (ref[1][0] - ref[0][0]) * h + x6 * ref[1][0];
172 x1 = q * (ref[2][0] - ref[0][0]) * w + x7 * ref[2][0];
173 x2 = q * ref[0][0] * w * h;
174 x3 = q * (ref[1][1] - ref[0][1]) * h + x6 * ref[1][1];
175 x4 = q * (ref[2][1] - ref[0][1]) * w + x7 * ref[2][1];
176 x5 = q * ref[0][1] * w * h;
180 t0 = ref[0][0] * (ref[3][1] - ref[1][1]) +
181 ref[1][0] * (ref[0][1] - ref[3][1]) +
182 ref[3][0] * (ref[1][1] - ref[0][1]);
183 t1 = ref[1][0] * (ref[2][1] - ref[3][1]) +
184 ref[2][0] * (ref[3][1] - ref[1][1]) +
185 ref[3][0] * (ref[1][1] - ref[2][1]);
186 t2 = ref[0][0] * (ref[3][1] - ref[2][1]) +
187 ref[2][0] * (ref[0][1] - ref[3][1]) +
188 ref[3][0] * (ref[2][1] - ref[0][1]);
189 t3 = ref[0][0] * (ref[1][1] - ref[2][1]) +
190 ref[1][0] * (ref[2][1] - ref[0][1]) +
191 ref[2][0] * (ref[0][1] - ref[1][1]);
193 x0 = t0 * t1 * w * (ref[2][1] - ref[0][1]);
194 x1 = t0 * t1 * w * (ref[0][0] - ref[2][0]);
195 x2 = t0 * t1 * w * (ref[0][1] * ref[2][0] - ref[0][0] * ref[2][1]);
196 x3 = t1 * t2 * h * (ref[1][1] - ref[0][1]);
197 x4 = t1 * t2 * h * (ref[0][0] - ref[1][0]);
198 x5 = t1 * t2 * h * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]);
199 x6 = t1 * t2 * (ref[1][1] - ref[0][1]) +
200 t0 * t3 * (ref[2][1] - ref[3][1]);
201 x7 = t1 * t2 * (ref[0][0] - ref[1][0]) +
202 t0 * t3 * (ref[3][0] - ref[2][0]);
203 x8 = t1 * t2 * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]) +
204 t0 * t3 * (ref[2][0] * ref[3][1] - ref[2][1] * ref[3][0]);
210 for (y = 0; y < h; y++){
211 for (x = 0; x < w; x++){
214 u = (int)floor(
SUB_PIXELS * (x0 * x + x1 * y + x2) /
215 (x6 * x + x7 * y + x8) + 0.5);
216 v = (int)floor(
SUB_PIXELS * (x3 * x + x4 * y + x5) /
217 (x6 * x + x7 * y + x8) + 0.5);
219 s->
pv[x + y * w][0] =
u;
220 s->
pv[x + y * w][1] =
v;
225 double d = i / (double)SUB_PIXELS;
229 for (j = 0; j < 4; j++)
232 for (j = 0; j < 4; j++)
235 for (j = 0; j < 4; j++)
252 int job,
int nb_jobs)
264 int start = (h * job) / nb_jobs;
265 int end = (h * (job+1)) / nb_jobs;
266 const int linesize = s->
linesize[0];
269 for (y = start; y <
end; y++) {
271 for (x = 0; x < w; x++) {
272 int u,
v, subU, subV, sum, sx;
275 u = s->
pv[sx + sy * linesize][0] >> hsub;
276 v = s->
pv[sx + sy * linesize][1] >> vsub;
282 if (u > 0 && v > 0 && u < w - 2 && v < h - 2){
283 const int index = u + v*src_linesize;
284 const int a = s->
coeff[subU][0];
285 const int b = s->
coeff[subU][1];
286 const int c = s->
coeff[subU][2];
287 const int d = s->
coeff[subU][3];
289 sum = s->
coeff[subV][0] * (a * src[index - 1 - src_linesize] + b * src[index - 0 - src_linesize] +
290 c * src[index + 1 - src_linesize] + d * src[index + 2 - src_linesize]) +
291 s->
coeff[subV][1] * (a * src[index - 1 ] + b * src[index - 0 ] +
292 c * src[index + 1 ] + d * src[index + 2 ]) +
293 s->
coeff[subV][2] * (a * src[index - 1 + src_linesize] + b * src[index - 0 + src_linesize] +
294 c * src[index + 1 + src_linesize] + d * src[index + 2 + src_linesize]) +
295 s->
coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize] +
296 c * src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]);
302 for (dy = 0; dy < 4; dy++) {
309 for (dx = 0; dx < 4; dx++) {
317 sum += s->
coeff[subU][dx] * s->
coeff[subV][dy] * src[ ix + iy * src_linesize];
323 sum = av_clip_uint8(sum);
324 dst[x + y * dst_linesize] = sum;
331 int job,
int nb_jobs)
343 int start = (h * job) / nb_jobs;
344 int end = (h * (job+1)) / nb_jobs;
345 const int linesize = s->
linesize[0];
348 for (y = start; y <
end; y++){
350 for (x = 0; x < w; x++){
351 int u,
v, subU, subV, sum, sx,
index, subUI, subVI;
354 u = s->
pv[sx + sy * linesize][0] >> hsub;
355 v = s->
pv[sx + sy * linesize][1] >> vsub;
361 index = u + v * src_linesize;
365 if ((
unsigned)u < (unsigned)(w - 1)){
366 if((
unsigned)v < (unsigned)(h - 1)){
367 sum = subVI * (subUI * src[
index] + subU * src[index + 1]) +
368 subV * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]);
375 index = u + v * src_linesize;
376 sum = subUI * src[
index] + subU * src[index + 1];
384 if ((
unsigned)v < (unsigned)(h - 1)){
385 index = u + v * src_linesize;
386 sum = subVI * src[
index] + subV * src[index + src_linesize];
393 index = u + v * src_linesize;
398 sum = av_clip_uint8(sum);
399 dst[x + y * dst_linesize] = sum;
432 for (plane = 0; plane < s->
nb_planes; plane++) {
433 int hsub = plane == 1 || plane == 2 ? s->
hsub : 0;
434 int vsub = plane == 1 || plane == 2 ? s->
vsub : 0;
436 .dst_linesize = out->
linesize[plane],
437 .src = frame->
data[plane],
438 .src_linesize = frame->
linesize[plane],
476 .
name =
"perspective",
482 .
inputs = perspective_inputs,
483 .
outputs = perspective_outputs,
484 .priv_class = &perspective_class,