31 #define SUB_PIXEL_BITS 8
32 #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
52 void *
arg,
int job,
int nb_jobs);
55 #define OFFSET(x) offsetof(PerspectiveContext, x)
56 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
76 {
"source",
"specify locations in source to send to corners in destination",
78 {
"destination",
"specify locations in destination to send corners of source",
106 coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
108 coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
115 static const char *
const var_names[] = {
"W",
"H", NULL };
120 double x0, x1, x2, x3, x4, x5, x6, x7, x8, q;
124 double (*ref)[2] = s->
ref;
131 for (i = 0; i < 4; i++) {
132 for (j = 0; j < 2; j++) {
137 NULL, NULL, NULL, NULL,
159 x6 = ((ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
160 (ref[2][1] - ref[3][1]) -
161 ( ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
162 (ref[2][0] - ref[3][0])) * h;
163 x7 = ((ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
164 (ref[1][0] - ref[3][0]) -
165 ( ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
166 (ref[1][1] - ref[3][1])) * w;
167 q = ( ref[1][0] - ref[3][0]) * (ref[2][1] - ref[3][1]) -
168 ( ref[2][0] - ref[3][0]) * (ref[1][1] - ref[3][1]);
170 x0 = q * (ref[1][0] - ref[0][0]) * h + x6 * ref[1][0];
171 x1 = q * (ref[2][0] - ref[0][0]) * w + x7 * ref[2][0];
172 x2 = q * ref[0][0] * w * h;
173 x3 = q * (ref[1][1] - ref[0][1]) * h + x6 * ref[1][1];
174 x4 = q * (ref[2][1] - ref[0][1]) * w + x7 * ref[2][1];
175 x5 = q * ref[0][1] * w * h;
179 t0 = ref[0][0] * (ref[3][1] - ref[1][1]) +
180 ref[1][0] * (ref[0][1] - ref[3][1]) +
181 ref[3][0] * (ref[1][1] - ref[0][1]);
182 t1 = ref[1][0] * (ref[2][1] - ref[3][1]) +
183 ref[2][0] * (ref[3][1] - ref[1][1]) +
184 ref[3][0] * (ref[1][1] - ref[2][1]);
185 t2 = ref[0][0] * (ref[3][1] - ref[2][1]) +
186 ref[2][0] * (ref[0][1] - ref[3][1]) +
187 ref[3][0] * (ref[2][1] - ref[0][1]);
188 t3 = ref[0][0] * (ref[1][1] - ref[2][1]) +
189 ref[1][0] * (ref[2][1] - ref[0][1]) +
190 ref[2][0] * (ref[0][1] - ref[1][1]);
192 x0 = t0 * t1 * w * (ref[2][1] - ref[0][1]);
193 x1 = t0 * t1 * w * (ref[0][0] - ref[2][0]);
194 x2 = t0 * t1 * w * (ref[0][1] * ref[2][0] - ref[0][0] * ref[2][1]);
195 x3 = t1 * t2 * h * (ref[1][1] - ref[0][1]);
196 x4 = t1 * t2 * h * (ref[0][0] - ref[1][0]);
197 x5 = t1 * t2 * h * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]);
198 x6 = t1 * t2 * (ref[1][1] - ref[0][1]) +
199 t0 * t3 * (ref[2][1] - ref[3][1]);
200 x7 = t1 * t2 * (ref[0][0] - ref[1][0]) +
201 t0 * t3 * (ref[3][0] - ref[2][0]);
202 x8 = t1 * t2 * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]) +
203 t0 * t3 * (ref[2][0] * ref[3][1] - ref[2][1] * ref[3][0]);
207 for (y = 0; y < h; y++){
208 for (x = 0; x < w; x++){
211 u = (int)floor(
SUB_PIXELS * (x0 * x + x1 * y + x2) /
212 (x6 * x + x7 * y + x8) + 0.5);
213 v = (int)floor(
SUB_PIXELS * (x3 * x + x4 * y + x5) /
214 (x6 * x + x7 * y + x8) + 0.5);
216 s->
pv[x + y * w][0] =
u;
217 s->
pv[x + y * w][1] =
v;
222 double d = i / (double)SUB_PIXELS;
226 for (j = 0; j < 4; j++)
229 for (j = 0; j < 4; j++)
232 for (j = 0; j < 4; j++)
249 int job,
int nb_jobs)
261 int start = (h * job) / nb_jobs;
262 int end = (h * (job+1)) / nb_jobs;
263 const int linesize = s->
linesize[0];
266 for (y = start; y <
end; y++) {
268 for (x = 0; x < w; x++) {
269 int u,
v, subU, subV, sum, sx;
272 u = s->
pv[sx + sy * linesize][0] >> hsub;
273 v = s->
pv[sx + sy * linesize][1] >> vsub;
279 if (u > 0 && v > 0 && u < w - 2 && v < h - 2){
280 const int index = u + v*src_linesize;
281 const int a = s->
coeff[subU][0];
282 const int b = s->
coeff[subU][1];
283 const int c = s->
coeff[subU][2];
284 const int d = s->
coeff[subU][3];
286 sum = s->
coeff[subV][0] * (a * src[index - 1 - src_linesize] + b * src[index - 0 - src_linesize] +
287 c * src[index + 1 - src_linesize] + d * src[index + 2 - src_linesize]) +
288 s->
coeff[subV][1] * (a * src[index - 1 ] + b * src[index - 0 ] +
289 c * src[index + 1 ] + d * src[index + 2 ]) +
290 s->
coeff[subV][2] * (a * src[index - 1 + src_linesize] + b * src[index - 0 + src_linesize] +
291 c * src[index + 1 + src_linesize] + d * src[index + 2 + src_linesize]) +
292 s->
coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize] +
293 c * src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]);
299 for (dy = 0; dy < 4; dy++) {
306 for (dx = 0; dx < 4; dx++) {
314 sum += s->
coeff[subU][dx] * s->
coeff[subV][dy] * src[ ix + iy * src_linesize];
320 sum = av_clip(sum, 0, 255);
321 dst[x + y * dst_linesize] = sum;
328 int job,
int nb_jobs)
340 int start = (h * job) / nb_jobs;
341 int end = (h * (job+1)) / nb_jobs;
342 const int linesize = s->
linesize[0];
345 for (y = start; y <
end; y++){
347 for (x = 0; x < w; x++){
348 int u,
v, subU, subV, sum, sx,
index, subUI, subVI;
351 u = s->
pv[sx + sy * linesize][0] >> hsub;
352 v = s->
pv[sx + sy * linesize][1] >> vsub;
358 index = u + v * src_linesize;
362 if ((
unsigned)u < (unsigned)(w - 1)){
363 if((
unsigned)v < (unsigned)(h - 1)){
364 sum = subVI * (subUI * src[
index] + subU * src[index + 1]) +
365 subV * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]);
372 index = u + v * src_linesize;
373 sum = subUI * src[
index] + subU * src[index + 1];
381 if ((
unsigned)v < (unsigned)(h - 1)){
382 index = u + v * src_linesize;
383 sum = subVI * src[
index] + subV * src[index + src_linesize];
390 index = u + v * src_linesize;
395 sum = av_clip(sum, 0, 255);
396 dst[x + y * dst_linesize] = sum;
429 for (plane = 0; plane < s->
nb_planes; plane++) {
430 int hsub = plane == 1 || plane == 2 ? s->
hsub : 0;
431 int vsub = plane == 1 || plane == 2 ? s->
vsub : 0;
433 .dst_linesize = out->
linesize[plane],
434 .src = frame->
data[plane],
435 .src_linesize = frame->
linesize[plane],
473 .
name =
"perspective",
479 .
inputs = perspective_inputs,
480 .
outputs = perspective_outputs,
481 .priv_class = &perspective_class,