FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_perspective.c
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 0 256 0.0%
Functions: 0 8 0.0%
Branches: 0 96 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2013 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/opt.h"
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "video.h"
31
32 #define SUB_PIXEL_BITS 8
33 #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
34 #define COEFF_BITS 11
35
36 #define LINEAR 0
37 #define CUBIC 1
38
39 typedef struct PerspectiveContext {
40 const AVClass *class;
41 char *expr_str[4][2];
42 double ref[4][2];
43 int32_t (*pv)[2];
44 int32_t coeff[SUB_PIXELS][4];
45 int interpolation;
46 int linesize[4];
47 int height[4];
48 int hsub, vsub;
49 int nb_planes;
50 int sense;
51 int eval_mode;
52
53 int (*perspective)(AVFilterContext *ctx,
54 void *arg, int job, int nb_jobs);
55 } PerspectiveContext;
56
57 #define OFFSET(x) offsetof(PerspectiveContext, x)
58 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
59
60 enum PERSPECTIVESense {
61 PERSPECTIVE_SENSE_SOURCE = 0, ///< coordinates give locations in source of corners of destination.
62 PERSPECTIVE_SENSE_DESTINATION = 1, ///< coordinates give locations in destination of corners of source.
63 };
64
65 enum EvalMode {
66 EVAL_MODE_INIT,
67 EVAL_MODE_FRAME,
68 EVAL_MODE_NB
69 };
70
71 static const AVOption perspective_options[] = {
72 { "x0", "set top left x coordinate", OFFSET(expr_str[0][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
73 { "y0", "set top left y coordinate", OFFSET(expr_str[0][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
74 { "x1", "set top right x coordinate", OFFSET(expr_str[1][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
75 { "y1", "set top right y coordinate", OFFSET(expr_str[1][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
76 { "x2", "set bottom left x coordinate", OFFSET(expr_str[2][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
77 { "y2", "set bottom left y coordinate", OFFSET(expr_str[2][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
78 { "x3", "set bottom right x coordinate", OFFSET(expr_str[3][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
79 { "y3", "set bottom right y coordinate", OFFSET(expr_str[3][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
80 { "interpolation", "set interpolation", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, 1, FLAGS, .unit = "interpolation" },
81 { "linear", "", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, .unit = "interpolation" },
82 { "cubic", "", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC}, 0, 0, FLAGS, .unit = "interpolation" },
83 { "sense", "specify the sense of the coordinates", OFFSET(sense), AV_OPT_TYPE_INT, {.i64=PERSPECTIVE_SENSE_SOURCE}, 0, 1, FLAGS, .unit = "sense"},
84 { "source", "specify locations in source to send to corners in destination",
85 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE_SENSE_SOURCE}, 0, 0, FLAGS, .unit = "sense"},
86 { "destination", "specify locations in destination to send corners of source",
87 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE_SENSE_DESTINATION}, 0, 0, FLAGS, .unit = "sense"},
88 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, .unit = "eval" },
89 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
90 { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
91
92 { NULL }
93 };
94
95 AVFILTER_DEFINE_CLASS(perspective);
96
97 static const enum AVPixelFormat pix_fmts[] = {
98 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
99 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
100 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
101 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
102 };
103
104 static inline double get_coeff(double d)
105 {
106 double coeff, A = -0.60;
107
108 d = fabs(d);
109
110 if (d < 1.0)
111 coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
112 else if (d < 2.0)
113 coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
114 else
115 coeff = 0.0;
116
117 return coeff;
118 }
119
120 static const char *const var_names[] = { "W", "H", "in", "on", NULL };
121 enum { VAR_W, VAR_H, VAR_IN, VAR_ON, VAR_VARS_NB };
122
123 static int calc_persp_luts(AVFilterContext *ctx, AVFilterLink *inlink)
124 {
125 PerspectiveContext *s = ctx->priv;
126 AVFilterLink *outlink = ctx->outputs[0];
127 double (*ref)[2] = s->ref;
128
129 double values[VAR_VARS_NB] = { [VAR_W] = inlink->w, [VAR_H] = inlink->h,
130 [VAR_IN] = inlink->frame_count_out + 1,
131 [VAR_ON] = outlink->frame_count_in + 1 };
132 const int h = values[VAR_H];
133 const int w = values[VAR_W];
134 double x0, x1, x2, x3, x4, x5, x6, x7, x8, q;
135 double t0, t1, t2, t3;
136 int x, y, i, j, ret;
137
138 for (i = 0; i < 4; i++) {
139 for (j = 0; j < 2; j++) {
140 if (!s->expr_str[i][j])
141 return AVERROR(EINVAL);
142 ret = av_expr_parse_and_eval(&s->ref[i][j], s->expr_str[i][j],
143 var_names, &values[0],
144 NULL, NULL, NULL, NULL,
145 0, 0, ctx);
146 if (ret < 0)
147 return ret;
148 }
149 }
150
151 switch (s->sense) {
152 case PERSPECTIVE_SENSE_SOURCE:
153 x6 = ((ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
154 (ref[2][1] - ref[3][1]) -
155 ( ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
156 (ref[2][0] - ref[3][0])) * h;
157 x7 = ((ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
158 (ref[1][0] - ref[3][0]) -
159 ( ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
160 (ref[1][1] - ref[3][1])) * w;
161 q = ( ref[1][0] - ref[3][0]) * (ref[2][1] - ref[3][1]) -
162 ( ref[2][0] - ref[3][0]) * (ref[1][1] - ref[3][1]);
163
164 x0 = q * (ref[1][0] - ref[0][0]) * h + x6 * ref[1][0];
165 x1 = q * (ref[2][0] - ref[0][0]) * w + x7 * ref[2][0];
166 x2 = q * ref[0][0] * w * h;
167 x3 = q * (ref[1][1] - ref[0][1]) * h + x6 * ref[1][1];
168 x4 = q * (ref[2][1] - ref[0][1]) * w + x7 * ref[2][1];
169 x5 = q * ref[0][1] * w * h;
170 x8 = q * w * h;
171 break;
172 case PERSPECTIVE_SENSE_DESTINATION:
173 t0 = ref[0][0] * (ref[3][1] - ref[1][1]) +
174 ref[1][0] * (ref[0][1] - ref[3][1]) +
175 ref[3][0] * (ref[1][1] - ref[0][1]);
176 t1 = ref[1][0] * (ref[2][1] - ref[3][1]) +
177 ref[2][0] * (ref[3][1] - ref[1][1]) +
178 ref[3][0] * (ref[1][1] - ref[2][1]);
179 t2 = ref[0][0] * (ref[3][1] - ref[2][1]) +
180 ref[2][0] * (ref[0][1] - ref[3][1]) +
181 ref[3][0] * (ref[2][1] - ref[0][1]);
182 t3 = ref[0][0] * (ref[1][1] - ref[2][1]) +
183 ref[1][0] * (ref[2][1] - ref[0][1]) +
184 ref[2][0] * (ref[0][1] - ref[1][1]);
185
186 x0 = t0 * t1 * w * (ref[2][1] - ref[0][1]);
187 x1 = t0 * t1 * w * (ref[0][0] - ref[2][0]);
188 x2 = t0 * t1 * w * (ref[0][1] * ref[2][0] - ref[0][0] * ref[2][1]);
189 x3 = t1 * t2 * h * (ref[1][1] - ref[0][1]);
190 x4 = t1 * t2 * h * (ref[0][0] - ref[1][0]);
191 x5 = t1 * t2 * h * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]);
192 x6 = t1 * t2 * (ref[1][1] - ref[0][1]) +
193 t0 * t3 * (ref[2][1] - ref[3][1]);
194 x7 = t1 * t2 * (ref[0][0] - ref[1][0]) +
195 t0 * t3 * (ref[3][0] - ref[2][0]);
196 x8 = t1 * t2 * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]) +
197 t0 * t3 * (ref[2][0] * ref[3][1] - ref[2][1] * ref[3][0]);
198 break;
199 default:
200 av_assert0(0);
201 }
202
203 for (y = 0; y < h; y++){
204 for (x = 0; x < w; x++){
205 int u, v;
206
207 u = lrint(SUB_PIXELS * (x0 * x + x1 * y + x2) /
208 (x6 * x + x7 * y + x8));
209 v = lrint(SUB_PIXELS * (x3 * x + x4 * y + x5) /
210 (x6 * x + x7 * y + x8));
211
212 s->pv[x + y * w][0] = u;
213 s->pv[x + y * w][1] = v;
214 }
215 }
216
217 return 0;
218 }
219
220 static int config_input(AVFilterLink *inlink)
221 {
222 AVFilterContext *ctx = inlink->dst;
223 PerspectiveContext *s = ctx->priv;
224 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
225 int h = inlink->h;
226 int w = inlink->w;
227 int i, j, ret;
228 s->hsub = desc->log2_chroma_w;
229 s->vsub = desc->log2_chroma_h;
230 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
231 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
232 return ret;
233
234 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
235 s->height[0] = s->height[3] = inlink->h;
236
237 s->pv = av_realloc_f(s->pv, w * h, 2 * sizeof(*s->pv));
238 if (!s->pv)
239 return AVERROR(ENOMEM);
240
241 if (s->eval_mode == EVAL_MODE_INIT) {
242 if ((ret = calc_persp_luts(ctx, inlink)) < 0) {
243 return ret;
244 }
245 }
246
247 for (i = 0; i < SUB_PIXELS; i++){
248 double d = i / (double)SUB_PIXELS;
249 double temp[4];
250 double sum = 0;
251
252 for (j = 0; j < 4; j++)
253 temp[j] = get_coeff(j - d - 1);
254
255 for (j = 0; j < 4; j++)
256 sum += temp[j];
257
258 for (j = 0; j < 4; j++)
259 s->coeff[i][j] = lrint((1 << COEFF_BITS) * temp[j] / sum);
260 }
261
262 return 0;
263 }
264
265 typedef struct ThreadData {
266 uint8_t *dst;
267 int dst_linesize;
268 uint8_t *src;
269 int src_linesize;
270 int w, h;
271 int hsub, vsub;
272 } ThreadData;
273
274 static int resample_cubic(AVFilterContext *ctx, void *arg,
275 int job, int nb_jobs)
276 {
277 PerspectiveContext *s = ctx->priv;
278 ThreadData *td = arg;
279 uint8_t *dst = td->dst;
280 int dst_linesize = td->dst_linesize;
281 uint8_t *src = td->src;
282 int src_linesize = td->src_linesize;
283 int w = td->w;
284 int h = td->h;
285 int hsub = td->hsub;
286 int vsub = td->vsub;
287 int start = (h * job) / nb_jobs;
288 int end = (h * (job+1)) / nb_jobs;
289 const int linesize = s->linesize[0];
290 int x, y;
291
292 for (y = start; y < end; y++) {
293 int sy = y << vsub;
294 for (x = 0; x < w; x++) {
295 int u, v, subU, subV, sum, sx;
296
297 sx = x << hsub;
298 u = s->pv[sx + sy * linesize][0] >> hsub;
299 v = s->pv[sx + sy * linesize][1] >> vsub;
300 subU = u & (SUB_PIXELS - 1);
301 subV = v & (SUB_PIXELS - 1);
302 u >>= SUB_PIXEL_BITS;
303 v >>= SUB_PIXEL_BITS;
304
305 if (u > 0 && v > 0 && u < w - 2 && v < h - 2){
306 const int index = u + v*src_linesize;
307 const int a = s->coeff[subU][0];
308 const int b = s->coeff[subU][1];
309 const int c = s->coeff[subU][2];
310 const int d = s->coeff[subU][3];
311
312 sum = s->coeff[subV][0] * (a * src[index - 1 - src_linesize] + b * src[index - 0 - src_linesize] +
313 c * src[index + 1 - src_linesize] + d * src[index + 2 - src_linesize]) +
314 s->coeff[subV][1] * (a * src[index - 1 ] + b * src[index - 0 ] +
315 c * src[index + 1 ] + d * src[index + 2 ]) +
316 s->coeff[subV][2] * (a * src[index - 1 + src_linesize] + b * src[index - 0 + src_linesize] +
317 c * src[index + 1 + src_linesize] + d * src[index + 2 + src_linesize]) +
318 s->coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize] +
319 c * src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]);
320 } else {
321 int dx, dy;
322
323 sum = 0;
324
325 for (dy = 0; dy < 4; dy++) {
326 int iy = v + dy - 1;
327
328 if (iy < 0)
329 iy = 0;
330 else if (iy >= h)
331 iy = h-1;
332 for (dx = 0; dx < 4; dx++) {
333 int ix = u + dx - 1;
334
335 if (ix < 0)
336 ix = 0;
337 else if (ix >= w)
338 ix = w - 1;
339
340 sum += s->coeff[subU][dx] * s->coeff[subV][dy] * src[ ix + iy * src_linesize];
341 }
342 }
343 }
344
345 sum = (sum + (1<<(COEFF_BITS * 2 - 1))) >> (COEFF_BITS * 2);
346 sum = av_clip_uint8(sum);
347 dst[x + y * dst_linesize] = sum;
348 }
349 }
350 return 0;
351 }
352
353 static int resample_linear(AVFilterContext *ctx, void *arg,
354 int job, int nb_jobs)
355 {
356 PerspectiveContext *s = ctx->priv;
357 ThreadData *td = arg;
358 uint8_t *dst = td->dst;
359 int dst_linesize = td->dst_linesize;
360 uint8_t *src = td->src;
361 int src_linesize = td->src_linesize;
362 int w = td->w;
363 int h = td->h;
364 int hsub = td->hsub;
365 int vsub = td->vsub;
366 int start = (h * job) / nb_jobs;
367 int end = (h * (job+1)) / nb_jobs;
368 const int linesize = s->linesize[0];
369 int x, y;
370
371 for (y = start; y < end; y++){
372 int sy = y << vsub;
373 for (x = 0; x < w; x++){
374 int u, v, subU, subV, sum, sx, index, subUI, subVI;
375
376 sx = x << hsub;
377 u = s->pv[sx + sy * linesize][0] >> hsub;
378 v = s->pv[sx + sy * linesize][1] >> vsub;
379 subU = u & (SUB_PIXELS - 1);
380 subV = v & (SUB_PIXELS - 1);
381 u >>= SUB_PIXEL_BITS;
382 v >>= SUB_PIXEL_BITS;
383
384 index = u + v * src_linesize;
385 subUI = SUB_PIXELS - subU;
386 subVI = SUB_PIXELS - subV;
387
388 if ((unsigned)u < (unsigned)(w - 1)){
389 if((unsigned)v < (unsigned)(h - 1)){
390 sum = subVI * (subUI * src[index] + subU * src[index + 1]) +
391 subV * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]);
392 sum = (sum + (1 << (SUB_PIXEL_BITS * 2 - 1)))>> (SUB_PIXEL_BITS * 2);
393 } else {
394 if (v < 0)
395 v = 0;
396 else
397 v = h - 1;
398 index = u + v * src_linesize;
399 sum = subUI * src[index] + subU * src[index + 1];
400 sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
401 }
402 } else {
403 if (u < 0)
404 u = 0;
405 else
406 u = w - 1;
407 if ((unsigned)v < (unsigned)(h - 1)){
408 index = u + v * src_linesize;
409 sum = subVI * src[index] + subV * src[index + src_linesize];
410 sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
411 } else {
412 if (v < 0)
413 v = 0;
414 else
415 v = h - 1;
416 index = u + v * src_linesize;
417 sum = src[index];
418 }
419 }
420
421 sum = av_clip_uint8(sum);
422 dst[x + y * dst_linesize] = sum;
423 }
424 }
425 return 0;
426 }
427
428 static av_cold int init(AVFilterContext *ctx)
429 {
430 PerspectiveContext *s = ctx->priv;
431
432 switch (s->interpolation) {
433 case LINEAR: s->perspective = resample_linear; break;
434 case CUBIC: s->perspective = resample_cubic; break;
435 }
436
437 return 0;
438 }
439
440 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
441 {
442 AVFilterContext *ctx = inlink->dst;
443 AVFilterLink *outlink = ctx->outputs[0];
444 PerspectiveContext *s = ctx->priv;
445 AVFrame *out;
446 int plane;
447 int ret;
448
449 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
450 if (!out) {
451 av_frame_free(&frame);
452 return AVERROR(ENOMEM);
453 }
454 av_frame_copy_props(out, frame);
455
456 if (s->eval_mode == EVAL_MODE_FRAME) {
457 if ((ret = calc_persp_luts(ctx, inlink)) < 0) {
458 av_frame_free(&out);
459 return ret;
460 }
461 }
462
463 for (plane = 0; plane < s->nb_planes; plane++) {
464 int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
465 int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
466 ThreadData td = {.dst = out->data[plane],
467 .dst_linesize = out->linesize[plane],
468 .src = frame->data[plane],
469 .src_linesize = frame->linesize[plane],
470 .w = s->linesize[plane],
471 .h = s->height[plane],
472 .hsub = hsub,
473 .vsub = vsub };
474 ff_filter_execute(ctx, s->perspective, &td, NULL,
475 FFMIN(td.h, ff_filter_get_nb_threads(ctx)));
476 }
477
478 av_frame_free(&frame);
479 return ff_filter_frame(outlink, out);
480 }
481
482 static av_cold void uninit(AVFilterContext *ctx)
483 {
484 PerspectiveContext *s = ctx->priv;
485
486 av_freep(&s->pv);
487 }
488
489 static const AVFilterPad perspective_inputs[] = {
490 {
491 .name = "default",
492 .type = AVMEDIA_TYPE_VIDEO,
493 .filter_frame = filter_frame,
494 .config_props = config_input,
495 },
496 };
497
498 const AVFilter ff_vf_perspective = {
499 .name = "perspective",
500 .description = NULL_IF_CONFIG_SMALL("Correct the perspective of video."),
501 .priv_size = sizeof(PerspectiveContext),
502 .init = init,
503 .uninit = uninit,
504 FILTER_INPUTS(perspective_inputs),
505 FILTER_OUTPUTS(ff_video_default_filterpad),
506 FILTER_PIXFMTS_ARRAY(pix_fmts),
507 .priv_class = &perspective_class,
508 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
509 };
510