2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2013 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/avassert.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/opt.h"
32 #define SUB_PIXEL_BITS 8
33 #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
39 typedef struct PerspectiveContext {
44 int32_t coeff[SUB_PIXELS][4];
52 int (*perspective)(AVFilterContext *ctx,
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
59 enum PERSPECTIVESense {
60 PERSPECTIVE_SENSE_SOURCE = 0, ///< coordinates give locations in source of corners of destination.
61 PERSPECTIVE_SENSE_DESTINATION = 1, ///< coordinates give locations in destination of corners of source.
64 static const AVOption perspective_options[] = {
65 { "x0", "set top left x coordinate", OFFSET(expr_str[0][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
66 { "y0", "set top left y coordinate", OFFSET(expr_str[0][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
67 { "x1", "set top right x coordinate", OFFSET(expr_str[1][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
68 { "y1", "set top right y coordinate", OFFSET(expr_str[1][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
69 { "x2", "set bottom left x coordinate", OFFSET(expr_str[2][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
70 { "y2", "set bottom left y coordinate", OFFSET(expr_str[2][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
71 { "x3", "set bottom right x coordinate", OFFSET(expr_str[3][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
72 { "y3", "set bottom right y coordinate", OFFSET(expr_str[3][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
73 { "interpolation", "set interpolation", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, 1, FLAGS, "interpolation" },
74 { "linear", "", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "interpolation" },
75 { "cubic", "", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC}, 0, 0, FLAGS, "interpolation" },
76 { "sense", "specify the sense of the coordinates", OFFSET(sense), AV_OPT_TYPE_INT, {.i64=PERSPECTIVE_SENSE_SOURCE}, 0, 1, FLAGS, "sense"},
77 { "source", "specify locations in source to send to corners in destination",
78 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE_SENSE_SOURCE}, 0, 0, FLAGS, "sense"},
79 { "destination", "specify locations in destination to send corners of source",
80 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE_SENSE_DESTINATION}, 0, 0, FLAGS, "sense"},
85 AVFILTER_DEFINE_CLASS(perspective);
87 static int query_formats(AVFilterContext *ctx)
89 static const enum AVPixelFormat pix_fmts[] = {
90 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
91 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
92 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
93 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
96 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
98 return AVERROR(ENOMEM);
99 return ff_set_common_formats(ctx, fmts_list);
102 static inline double get_coeff(double d)
104 double coeff, A = -0.60;
109 coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
111 coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
118 static const char *const var_names[] = { "W", "H", NULL };
119 enum { VAR_W, VAR_H, VAR_VARS_NB };
121 static int config_input(AVFilterLink *inlink)
123 double x0, x1, x2, x3, x4, x5, x6, x7, x8, q;
124 double t0, t1, t2, t3;
125 AVFilterContext *ctx = inlink->dst;
126 PerspectiveContext *s = ctx->priv;
127 double (*ref)[2] = s->ref;
128 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
129 double values[VAR_VARS_NB] = { [VAR_W] = inlink->w, [VAR_H] = inlink->h };
134 for (i = 0; i < 4; i++) {
135 for (j = 0; j < 2; j++) {
136 if (!s->expr_str[i][j])
137 return AVERROR(EINVAL);
138 ret = av_expr_parse_and_eval(&s->ref[i][j], s->expr_str[i][j],
139 var_names, &values[0],
140 NULL, NULL, NULL, NULL,
147 s->hsub = desc->log2_chroma_w;
148 s->vsub = desc->log2_chroma_h;
149 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
150 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
153 s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
154 s->height[0] = s->height[3] = inlink->h;
156 s->pv = av_realloc_f(s->pv, w * h, 2 * sizeof(*s->pv));
158 return AVERROR(ENOMEM);
161 case PERSPECTIVE_SENSE_SOURCE:
162 x6 = ((ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
163 (ref[2][1] - ref[3][1]) -
164 ( ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
165 (ref[2][0] - ref[3][0])) * h;
166 x7 = ((ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
167 (ref[1][0] - ref[3][0]) -
168 ( ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
169 (ref[1][1] - ref[3][1])) * w;
170 q = ( ref[1][0] - ref[3][0]) * (ref[2][1] - ref[3][1]) -
171 ( ref[2][0] - ref[3][0]) * (ref[1][1] - ref[3][1]);
173 x0 = q * (ref[1][0] - ref[0][0]) * h + x6 * ref[1][0];
174 x1 = q * (ref[2][0] - ref[0][0]) * w + x7 * ref[2][0];
175 x2 = q * ref[0][0] * w * h;
176 x3 = q * (ref[1][1] - ref[0][1]) * h + x6 * ref[1][1];
177 x4 = q * (ref[2][1] - ref[0][1]) * w + x7 * ref[2][1];
178 x5 = q * ref[0][1] * w * h;
181 case PERSPECTIVE_SENSE_DESTINATION:
182 t0 = ref[0][0] * (ref[3][1] - ref[1][1]) +
183 ref[1][0] * (ref[0][1] - ref[3][1]) +
184 ref[3][0] * (ref[1][1] - ref[0][1]);
185 t1 = ref[1][0] * (ref[2][1] - ref[3][1]) +
186 ref[2][0] * (ref[3][1] - ref[1][1]) +
187 ref[3][0] * (ref[1][1] - ref[2][1]);
188 t2 = ref[0][0] * (ref[3][1] - ref[2][1]) +
189 ref[2][0] * (ref[0][1] - ref[3][1]) +
190 ref[3][0] * (ref[2][1] - ref[0][1]);
191 t3 = ref[0][0] * (ref[1][1] - ref[2][1]) +
192 ref[1][0] * (ref[2][1] - ref[0][1]) +
193 ref[2][0] * (ref[0][1] - ref[1][1]);
195 x0 = t0 * t1 * w * (ref[2][1] - ref[0][1]);
196 x1 = t0 * t1 * w * (ref[0][0] - ref[2][0]);
197 x2 = t0 * t1 * w * (ref[0][1] * ref[2][0] - ref[0][0] * ref[2][1]);
198 x3 = t1 * t2 * h * (ref[1][1] - ref[0][1]);
199 x4 = t1 * t2 * h * (ref[0][0] - ref[1][0]);
200 x5 = t1 * t2 * h * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]);
201 x6 = t1 * t2 * (ref[1][1] - ref[0][1]) +
202 t0 * t3 * (ref[2][1] - ref[3][1]);
203 x7 = t1 * t2 * (ref[0][0] - ref[1][0]) +
204 t0 * t3 * (ref[3][0] - ref[2][0]);
205 x8 = t1 * t2 * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]) +
206 t0 * t3 * (ref[2][0] * ref[3][1] - ref[2][1] * ref[3][0]);
212 for (y = 0; y < h; y++){
213 for (x = 0; x < w; x++){
216 u = (int)floor(SUB_PIXELS * (x0 * x + x1 * y + x2) /
217 (x6 * x + x7 * y + x8) + 0.5);
218 v = (int)floor(SUB_PIXELS * (x3 * x + x4 * y + x5) /
219 (x6 * x + x7 * y + x8) + 0.5);
221 s->pv[x + y * w][0] = u;
222 s->pv[x + y * w][1] = v;
226 for (i = 0; i < SUB_PIXELS; i++){
227 double d = i / (double)SUB_PIXELS;
231 for (j = 0; j < 4; j++)
232 temp[j] = get_coeff(j - d - 1);
234 for (j = 0; j < 4; j++)
237 for (j = 0; j < 4; j++)
238 s->coeff[i][j] = (int)floor((1 << COEFF_BITS) * temp[j] / sum + 0.5);
244 typedef struct ThreadData {
253 static int resample_cubic(AVFilterContext *ctx, void *arg,
254 int job, int nb_jobs)
256 PerspectiveContext *s = ctx->priv;
257 ThreadData *td = arg;
258 uint8_t *dst = td->dst;
259 int dst_linesize = td->dst_linesize;
260 uint8_t *src = td->src;
261 int src_linesize = td->src_linesize;
266 int start = (h * job) / nb_jobs;
267 int end = (h * (job+1)) / nb_jobs;
268 const int linesize = s->linesize[0];
271 for (y = start; y < end; y++) {
273 for (x = 0; x < w; x++) {
274 int u, v, subU, subV, sum, sx;
277 u = s->pv[sx + sy * linesize][0] >> hsub;
278 v = s->pv[sx + sy * linesize][1] >> vsub;
279 subU = u & (SUB_PIXELS - 1);
280 subV = v & (SUB_PIXELS - 1);
281 u >>= SUB_PIXEL_BITS;
282 v >>= SUB_PIXEL_BITS;
284 if (u > 0 && v > 0 && u < w - 2 && v < h - 2){
285 const int index = u + v*src_linesize;
286 const int a = s->coeff[subU][0];
287 const int b = s->coeff[subU][1];
288 const int c = s->coeff[subU][2];
289 const int d = s->coeff[subU][3];
291 sum = s->coeff[subV][0] * (a * src[index - 1 - src_linesize] + b * src[index - 0 - src_linesize] +
292 c * src[index + 1 - src_linesize] + d * src[index + 2 - src_linesize]) +
293 s->coeff[subV][1] * (a * src[index - 1 ] + b * src[index - 0 ] +
294 c * src[index + 1 ] + d * src[index + 2 ]) +
295 s->coeff[subV][2] * (a * src[index - 1 + src_linesize] + b * src[index - 0 + src_linesize] +
296 c * src[index + 1 + src_linesize] + d * src[index + 2 + src_linesize]) +
297 s->coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize] +
298 c * src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]);
304 for (dy = 0; dy < 4; dy++) {
311 for (dx = 0; dx < 4; dx++) {
319 sum += s->coeff[subU][dx] * s->coeff[subV][dy] * src[ ix + iy * src_linesize];
324 sum = (sum + (1<<(COEFF_BITS * 2 - 1))) >> (COEFF_BITS * 2);
325 sum = av_clip_uint8(sum);
326 dst[x + y * dst_linesize] = sum;
332 static int resample_linear(AVFilterContext *ctx, void *arg,
333 int job, int nb_jobs)
335 PerspectiveContext *s = ctx->priv;
336 ThreadData *td = arg;
337 uint8_t *dst = td->dst;
338 int dst_linesize = td->dst_linesize;
339 uint8_t *src = td->src;
340 int src_linesize = td->src_linesize;
345 int start = (h * job) / nb_jobs;
346 int end = (h * (job+1)) / nb_jobs;
347 const int linesize = s->linesize[0];
350 for (y = start; y < end; y++){
352 for (x = 0; x < w; x++){
353 int u, v, subU, subV, sum, sx, index, subUI, subVI;
356 u = s->pv[sx + sy * linesize][0] >> hsub;
357 v = s->pv[sx + sy * linesize][1] >> vsub;
358 subU = u & (SUB_PIXELS - 1);
359 subV = v & (SUB_PIXELS - 1);
360 u >>= SUB_PIXEL_BITS;
361 v >>= SUB_PIXEL_BITS;
363 index = u + v * src_linesize;
364 subUI = SUB_PIXELS - subU;
365 subVI = SUB_PIXELS - subV;
367 if ((unsigned)u < (unsigned)(w - 1)){
368 if((unsigned)v < (unsigned)(h - 1)){
369 sum = subVI * (subUI * src[index] + subU * src[index + 1]) +
370 subV * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]);
371 sum = (sum + (1 << (SUB_PIXEL_BITS * 2 - 1)))>> (SUB_PIXEL_BITS * 2);
377 index = u + v * src_linesize;
378 sum = subUI * src[index] + subU * src[index + 1];
379 sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
386 if ((unsigned)v < (unsigned)(h - 1)){
387 index = u + v * src_linesize;
388 sum = subVI * src[index] + subV * src[index + src_linesize];
389 sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
395 index = u + v * src_linesize;
400 sum = av_clip_uint8(sum);
401 dst[x + y * dst_linesize] = sum;
407 static av_cold int init(AVFilterContext *ctx)
409 PerspectiveContext *s = ctx->priv;
411 switch (s->interpolation) {
412 case LINEAR: s->perspective = resample_linear; break;
413 case CUBIC: s->perspective = resample_cubic; break;
419 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
421 AVFilterContext *ctx = inlink->dst;
422 AVFilterLink *outlink = ctx->outputs[0];
423 PerspectiveContext *s = ctx->priv;
427 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
429 av_frame_free(&frame);
430 return AVERROR(ENOMEM);
432 av_frame_copy_props(out, frame);
434 for (plane = 0; plane < s->nb_planes; plane++) {
435 int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
436 int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
437 ThreadData td = {.dst = out->data[plane],
438 .dst_linesize = out->linesize[plane],
439 .src = frame->data[plane],
440 .src_linesize = frame->linesize[plane],
441 .w = s->linesize[plane],
442 .h = s->height[plane],
445 ctx->internal->execute(ctx, s->perspective, &td, NULL, FFMIN(td.h, ctx->graph->nb_threads));
448 av_frame_free(&frame);
449 return ff_filter_frame(outlink, out);
452 static av_cold void uninit(AVFilterContext *ctx)
454 PerspectiveContext *s = ctx->priv;
459 static const AVFilterPad perspective_inputs[] = {
462 .type = AVMEDIA_TYPE_VIDEO,
463 .filter_frame = filter_frame,
464 .config_props = config_input,
469 static const AVFilterPad perspective_outputs[] = {
472 .type = AVMEDIA_TYPE_VIDEO,
477 AVFilter ff_vf_perspective = {
478 .name = "perspective",
479 .description = NULL_IF_CONFIG_SMALL("Correct the perspective of video."),
480 .priv_size = sizeof(PerspectiveContext),
483 .query_formats = query_formats,
484 .inputs = perspective_inputs,
485 .outputs = perspective_outputs,
486 .priv_class = &perspective_class,
487 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,