FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_perspective.c
Go to the documentation of this file.
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/pixdesc.h"
26 #include "libavutil/opt.h"
27 #include "avfilter.h"
28 #include "formats.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];
46  int linesize[4];
47  int height[4];
48  int hsub, vsub;
49  int nb_planes;
50  int sense;
51 
53  void *arg, int job, int nb_jobs);
55 
56 #define OFFSET(x) offsetof(PerspectiveContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
58 
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.
62 };
63 
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"},
81 
82  { NULL }
83 };
84 
85 AVFILTER_DEFINE_CLASS(perspective);
86 
88 {
89  static const enum AVPixelFormat pix_fmts[] = {
94  };
95 
97  return 0;
98 }
99 
100 static inline double get_coeff(double d)
101 {
102  double coeff, A = -0.60;
103 
104  d = fabs(d);
105 
106  if (d < 1.0)
107  coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
108  else if (d < 2.0)
109  coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
110  else
111  coeff = 0.0;
112 
113  return coeff;
114 }
115 
116 static const char *const var_names[] = { "W", "H", NULL };
118 
119 static int config_input(AVFilterLink *inlink)
120 {
121  double x0, x1, x2, x3, x4, x5, x6, x7, x8, q;
122  double t0, t1, t2, t3;
123  AVFilterContext *ctx = inlink->dst;
124  PerspectiveContext *s = ctx->priv;
125  double (*ref)[2] = s->ref;
126  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
127  double values[VAR_VARS_NB] = { [VAR_W] = inlink->w, [VAR_H] = inlink->h };
128  int h = inlink->h;
129  int w = inlink->w;
130  int x, y, i, j, ret;
131 
132  for (i = 0; i < 4; i++) {
133  for (j = 0; j < 2; j++) {
134  if (!s->expr_str[i][j])
135  return AVERROR(EINVAL);
136  ret = av_expr_parse_and_eval(&s->ref[i][j], s->expr_str[i][j],
137  var_names, &values[0],
138  NULL, NULL, NULL, NULL,
139  0, 0, ctx);
140  if (ret < 0)
141  return ret;
142  }
143  }
144 
145  s->hsub = desc->log2_chroma_w;
146  s->vsub = desc->log2_chroma_h;
148  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
149  return ret;
150 
151  s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
152  s->height[0] = s->height[3] = inlink->h;
153 
154  s->pv = av_realloc_f(s->pv, w * h, 2 * sizeof(*s->pv));
155  if (!s->pv)
156  return AVERROR(ENOMEM);
157 
158  switch (s->sense) {
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]);
170 
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;
177  x8 = q * w * h;
178  break;
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]);
192 
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]);
205  break;
206  default:
207  av_assert0(0);
208  }
209 
210  for (y = 0; y < h; y++){
211  for (x = 0; x < w; x++){
212  int u, v;
213 
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);
218 
219  s->pv[x + y * w][0] = u;
220  s->pv[x + y * w][1] = v;
221  }
222  }
223 
224  for (i = 0; i < SUB_PIXELS; i++){
225  double d = i / (double)SUB_PIXELS;
226  double temp[4];
227  double sum = 0;
228 
229  for (j = 0; j < 4; j++)
230  temp[j] = get_coeff(j - d - 1);
231 
232  for (j = 0; j < 4; j++)
233  sum += temp[j];
234 
235  for (j = 0; j < 4; j++)
236  s->coeff[i][j] = (int)floor((1 << COEFF_BITS) * temp[j] / sum + 0.5);
237  }
238 
239  return 0;
240 }
241 
242 typedef struct ThreadData {
247  int w, h;
248  int hsub, vsub;
249 } ThreadData;
250 
251 static int resample_cubic(AVFilterContext *ctx, void *arg,
252  int job, int nb_jobs)
253 {
254  PerspectiveContext *s = ctx->priv;
255  ThreadData *td = arg;
256  uint8_t *dst = td->dst;
257  int dst_linesize = td->dst_linesize;
258  uint8_t *src = td->src;
259  int src_linesize = td->src_linesize;
260  int w = td->w;
261  int h = td->h;
262  int hsub = td->hsub;
263  int vsub = td->vsub;
264  int start = (h * job) / nb_jobs;
265  int end = (h * (job+1)) / nb_jobs;
266  const int linesize = s->linesize[0];
267  int x, y;
268 
269  for (y = start; y < end; y++) {
270  int sy = y << vsub;
271  for (x = 0; x < w; x++) {
272  int u, v, subU, subV, sum, sx;
273 
274  sx = x << hsub;
275  u = s->pv[sx + sy * linesize][0] >> hsub;
276  v = s->pv[sx + sy * linesize][1] >> vsub;
277  subU = u & (SUB_PIXELS - 1);
278  subV = v & (SUB_PIXELS - 1);
279  u >>= SUB_PIXEL_BITS;
280  v >>= SUB_PIXEL_BITS;
281 
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];
288 
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]);
297  } else {
298  int dx, dy;
299 
300  sum = 0;
301 
302  for (dy = 0; dy < 4; dy++) {
303  int iy = v + dy - 1;
304 
305  if (iy < 0)
306  iy = 0;
307  else if (iy >= h)
308  iy = h-1;
309  for (dx = 0; dx < 4; dx++) {
310  int ix = u + dx - 1;
311 
312  if (ix < 0)
313  ix = 0;
314  else if (ix >= w)
315  ix = w - 1;
316 
317  sum += s->coeff[subU][dx] * s->coeff[subV][dy] * src[ ix + iy * src_linesize];
318  }
319  }
320  }
321 
322  sum = (sum + (1<<(COEFF_BITS * 2 - 1))) >> (COEFF_BITS * 2);
323  sum = av_clip_uint8(sum);
324  dst[x + y * dst_linesize] = sum;
325  }
326  }
327  return 0;
328 }
329 
330 static int resample_linear(AVFilterContext *ctx, void *arg,
331  int job, int nb_jobs)
332 {
333  PerspectiveContext *s = ctx->priv;
334  ThreadData *td = arg;
335  uint8_t *dst = td->dst;
336  int dst_linesize = td->dst_linesize;
337  uint8_t *src = td->src;
338  int src_linesize = td->src_linesize;
339  int w = td->w;
340  int h = td->h;
341  int hsub = td->hsub;
342  int vsub = td->vsub;
343  int start = (h * job) / nb_jobs;
344  int end = (h * (job+1)) / nb_jobs;
345  const int linesize = s->linesize[0];
346  int x, y;
347 
348  for (y = start; y < end; y++){
349  int sy = y << vsub;
350  for (x = 0; x < w; x++){
351  int u, v, subU, subV, sum, sx, index, subUI, subVI;
352 
353  sx = x << hsub;
354  u = s->pv[sx + sy * linesize][0] >> hsub;
355  v = s->pv[sx + sy * linesize][1] >> vsub;
356  subU = u & (SUB_PIXELS - 1);
357  subV = v & (SUB_PIXELS - 1);
358  u >>= SUB_PIXEL_BITS;
359  v >>= SUB_PIXEL_BITS;
360 
361  index = u + v * src_linesize;
362  subUI = SUB_PIXELS - subU;
363  subVI = SUB_PIXELS - subV;
364 
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]);
369  sum = (sum + (1 << (SUB_PIXEL_BITS * 2 - 1)))>> (SUB_PIXEL_BITS * 2);
370  } else {
371  if (v < 0)
372  v = 0;
373  else
374  v = h - 1;
375  index = u + v * src_linesize;
376  sum = subUI * src[index] + subU * src[index + 1];
377  sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
378  }
379  } else {
380  if (u < 0)
381  u = 0;
382  else
383  u = w - 1;
384  if ((unsigned)v < (unsigned)(h - 1)){
385  index = u + v * src_linesize;
386  sum = subVI * src[index] + subV * src[index + src_linesize];
387  sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
388  } else {
389  if (v < 0)
390  v = 0;
391  else
392  v = h - 1;
393  index = u + v * src_linesize;
394  sum = src[index];
395  }
396  }
397 
398  sum = av_clip_uint8(sum);
399  dst[x + y * dst_linesize] = sum;
400  }
401  }
402  return 0;
403 }
404 
405 static av_cold int init(AVFilterContext *ctx)
406 {
407  PerspectiveContext *s = ctx->priv;
408 
409  switch (s->interpolation) {
410  case LINEAR: s->perspective = resample_linear; break;
411  case CUBIC: s->perspective = resample_cubic; break;
412  }
413 
414  return 0;
415 }
416 
417 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
418 {
419  AVFilterContext *ctx = inlink->dst;
420  AVFilterLink *outlink = ctx->outputs[0];
421  PerspectiveContext *s = ctx->priv;
422  AVFrame *out;
423  int plane;
424 
425  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
426  if (!out) {
427  av_frame_free(&frame);
428  return AVERROR(ENOMEM);
429  }
430  av_frame_copy_props(out, frame);
431 
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;
435  ThreadData td = {.dst = out->data[plane],
436  .dst_linesize = out->linesize[plane],
437  .src = frame->data[plane],
438  .src_linesize = frame->linesize[plane],
439  .w = s->linesize[plane],
440  .h = s->height[plane],
441  .hsub = hsub,
442  .vsub = vsub };
443  ctx->internal->execute(ctx, s->perspective, &td, NULL, FFMIN(td.h, ctx->graph->nb_threads));
444  }
445 
446  av_frame_free(&frame);
447  return ff_filter_frame(outlink, out);
448 }
449 
450 static av_cold void uninit(AVFilterContext *ctx)
451 {
452  PerspectiveContext *s = ctx->priv;
453 
454  av_freep(&s->pv);
455 }
456 
457 static const AVFilterPad perspective_inputs[] = {
458  {
459  .name = "default",
460  .type = AVMEDIA_TYPE_VIDEO,
461  .filter_frame = filter_frame,
462  .config_props = config_input,
463  },
464  { NULL }
465 };
466 
468  {
469  .name = "default",
470  .type = AVMEDIA_TYPE_VIDEO,
471  },
472  { NULL }
473 };
474 
476  .name = "perspective",
477  .description = NULL_IF_CONFIG_SMALL("Correct the perspective of video."),
478  .priv_size = sizeof(PerspectiveContext),
479  .init = init,
480  .uninit = uninit,
482  .inputs = perspective_inputs,
483  .outputs = perspective_outputs,
484  .priv_class = &perspective_class,
486 };