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/eval.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/opt.h"
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30 
31 #define SUB_PIXEL_BITS 8
32 #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
33 #define COEFF_BITS 11
34 
35 #define LINEAR 0
36 #define CUBIC 1
37 
38 typedef struct PerspectiveContext {
39  const AVClass *class;
40  char *expr_str[4][2];
41  double ref[4][2];
42  int32_t (*pv)[2];
45  int linesize[4];
46  int height[4];
47  int hsub, vsub;
48  int nb_planes;
49  int sense;
50 
52  void *arg, int job, int nb_jobs);
54 
55 #define OFFSET(x) offsetof(PerspectiveContext, x)
56 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
57 
59  PERSPECTIVE_SENSE_SOURCE = 0, ///< coordinates give locations in source of corners of destination.
60  PERSPECTIVE_SENSE_DESTINATION = 1, ///< coordinates give locations in destination of corners of source.
61 };
62 
63 static const AVOption perspective_options[] = {
64  { "x0", "set top left x coordinate", OFFSET(expr_str[0][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
65  { "y0", "set top left y coordinate", OFFSET(expr_str[0][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
66  { "x1", "set top right x coordinate", OFFSET(expr_str[1][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
67  { "y1", "set top right y coordinate", OFFSET(expr_str[1][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
68  { "x2", "set bottom left x coordinate", OFFSET(expr_str[2][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
69  { "y2", "set bottom left y coordinate", OFFSET(expr_str[2][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
70  { "x3", "set bottom right x coordinate", OFFSET(expr_str[3][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
71  { "y3", "set bottom right y coordinate", OFFSET(expr_str[3][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
72  { "interpolation", "set interpolation", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, 1, FLAGS, "interpolation" },
73  { "linear", "", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "interpolation" },
74  { "cubic", "", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC}, 0, 0, FLAGS, "interpolation" },
75  { "sense", "specify the sense of the coordinates", OFFSET(sense), AV_OPT_TYPE_INT, {.i64=PERSPECTIVE_SENSE_SOURCE}, 0, 1, FLAGS, "sense"},
76  { "source", "specify locations in source to send to corners in destination",
77  0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE_SENSE_SOURCE}, 0, 0, FLAGS, "sense"},
78  { "destination", "specify locations in destination to send corners of source",
79  0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE_SENSE_DESTINATION}, 0, 0, FLAGS, "sense"},
80 
81  { NULL }
82 };
83 
84 AVFILTER_DEFINE_CLASS(perspective);
85 
87 {
88  static const enum AVPixelFormat pix_fmts[] = {
93  };
94 
96  return 0;
97 }
98 
99 static inline double get_coeff(double d)
100 {
101  double coeff, A = -0.60;
102 
103  d = fabs(d);
104 
105  if (d < 1.0)
106  coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
107  else if (d < 2.0)
108  coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
109  else
110  coeff = 0.0;
111 
112  return coeff;
113 }
114 
115 static const char *const var_names[] = { "W", "H", NULL };
117 
118 static int config_input(AVFilterLink *inlink)
119 {
120  double x0, x1, x2, x3, x4, x5, x6, x7, x8, q;
121  double t0, t1, t2, t3;
122  AVFilterContext *ctx = inlink->dst;
123  PerspectiveContext *s = ctx->priv;
124  double (*ref)[2] = s->ref;
125  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
126  double values[VAR_VARS_NB] = { [VAR_W] = inlink->w, [VAR_H] = inlink->h };
127  int h = inlink->h;
128  int w = inlink->w;
129  int x, y, i, j, ret;
130 
131  for (i = 0; i < 4; i++) {
132  for (j = 0; j < 2; j++) {
133  if (!s->expr_str[i][j])
134  return AVERROR(EINVAL);
135  ret = av_expr_parse_and_eval(&s->ref[i][j], s->expr_str[i][j],
136  var_names, &values[0],
137  NULL, NULL, NULL, NULL,
138  0, 0, ctx);
139  if (ret < 0)
140  return ret;
141  }
142  }
143 
144  s->hsub = desc->log2_chroma_w;
145  s->vsub = desc->log2_chroma_h;
147  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
148  return ret;
149 
150  s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
151  s->height[0] = s->height[3] = inlink->h;
152 
153  s->pv = av_realloc_f(s->pv, w * h, 2 * sizeof(*s->pv));
154  if (!s->pv)
155  return AVERROR(ENOMEM);
156 
157  switch (s->sense) {
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]);
169 
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;
176  x8 = q * w * h;
177  break;
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]);
191 
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]);
204  break;
205  }
206 
207  for (y = 0; y < h; y++){
208  for (x = 0; x < w; x++){
209  int u, v;
210 
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);
215 
216  s->pv[x + y * w][0] = u;
217  s->pv[x + y * w][1] = v;
218  }
219  }
220 
221  for (i = 0; i < SUB_PIXELS; i++){
222  double d = i / (double)SUB_PIXELS;
223  double temp[4];
224  double sum = 0;
225 
226  for (j = 0; j < 4; j++)
227  temp[j] = get_coeff(j - d - 1);
228 
229  for (j = 0; j < 4; j++)
230  sum += temp[j];
231 
232  for (j = 0; j < 4; j++)
233  s->coeff[i][j] = (int)floor((1 << COEFF_BITS) * temp[j] / sum + 0.5);
234  }
235 
236  return 0;
237 }
238 
239 typedef struct ThreadData {
244  int w, h;
245  int hsub, vsub;
246 } ThreadData;
247 
248 static int resample_cubic(AVFilterContext *ctx, void *arg,
249  int job, int nb_jobs)
250 {
251  PerspectiveContext *s = ctx->priv;
252  ThreadData *td = arg;
253  uint8_t *dst = td->dst;
254  int dst_linesize = td->dst_linesize;
255  uint8_t *src = td->src;
256  int src_linesize = td->src_linesize;
257  int w = td->w;
258  int h = td->h;
259  int hsub = td->hsub;
260  int vsub = td->vsub;
261  int start = (h * job) / nb_jobs;
262  int end = (h * (job+1)) / nb_jobs;
263  const int linesize = s->linesize[0];
264  int x, y;
265 
266  for (y = start; y < end; y++) {
267  int sy = y << vsub;
268  for (x = 0; x < w; x++) {
269  int u, v, subU, subV, sum, sx;
270 
271  sx = x << hsub;
272  u = s->pv[sx + sy * linesize][0] >> hsub;
273  v = s->pv[sx + sy * linesize][1] >> vsub;
274  subU = u & (SUB_PIXELS - 1);
275  subV = v & (SUB_PIXELS - 1);
276  u >>= SUB_PIXEL_BITS;
277  v >>= SUB_PIXEL_BITS;
278 
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];
285 
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]);
294  } else {
295  int dx, dy;
296 
297  sum = 0;
298 
299  for (dy = 0; dy < 4; dy++) {
300  int iy = v + dy - 1;
301 
302  if (iy < 0)
303  iy = 0;
304  else if (iy >= h)
305  iy = h-1;
306  for (dx = 0; dx < 4; dx++) {
307  int ix = u + dx - 1;
308 
309  if (ix < 0)
310  ix = 0;
311  else if (ix >= w)
312  ix = w - 1;
313 
314  sum += s->coeff[subU][dx] * s->coeff[subV][dy] * src[ ix + iy * src_linesize];
315  }
316  }
317  }
318 
319  sum = (sum + (1<<(COEFF_BITS * 2 - 1))) >> (COEFF_BITS * 2);
320  sum = av_clip(sum, 0, 255);
321  dst[x + y * dst_linesize] = sum;
322  }
323  }
324  return 0;
325 }
326 
327 static int resample_linear(AVFilterContext *ctx, void *arg,
328  int job, int nb_jobs)
329 {
330  PerspectiveContext *s = ctx->priv;
331  ThreadData *td = arg;
332  uint8_t *dst = td->dst;
333  int dst_linesize = td->dst_linesize;
334  uint8_t *src = td->src;
335  int src_linesize = td->src_linesize;
336  int w = td->w;
337  int h = td->h;
338  int hsub = td->hsub;
339  int vsub = td->vsub;
340  int start = (h * job) / nb_jobs;
341  int end = (h * (job+1)) / nb_jobs;
342  const int linesize = s->linesize[0];
343  int x, y;
344 
345  for (y = start; y < end; y++){
346  int sy = y << vsub;
347  for (x = 0; x < w; x++){
348  int u, v, subU, subV, sum, sx, index, subUI, subVI;
349 
350  sx = x << hsub;
351  u = s->pv[sx + sy * linesize][0] >> hsub;
352  v = s->pv[sx + sy * linesize][1] >> vsub;
353  subU = u & (SUB_PIXELS - 1);
354  subV = v & (SUB_PIXELS - 1);
355  u >>= SUB_PIXEL_BITS;
356  v >>= SUB_PIXEL_BITS;
357 
358  index = u + v * src_linesize;
359  subUI = SUB_PIXELS - subU;
360  subVI = SUB_PIXELS - subV;
361 
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]);
366  sum = (sum + (1 << (SUB_PIXEL_BITS * 2 - 1)))>> (SUB_PIXEL_BITS * 2);
367  } else {
368  if (v < 0)
369  v = 0;
370  else
371  v = h - 1;
372  index = u + v * src_linesize;
373  sum = subUI * src[index] + subU * src[index + 1];
374  sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
375  }
376  } else {
377  if (u < 0)
378  u = 0;
379  else
380  u = w - 1;
381  if ((unsigned)v < (unsigned)(h - 1)){
382  index = u + v * src_linesize;
383  sum = subVI * src[index] + subV * src[index + src_linesize];
384  sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
385  } else {
386  if (v < 0)
387  v = 0;
388  else
389  v = h - 1;
390  index = u + v * src_linesize;
391  sum = src[index];
392  }
393  }
394 
395  sum = av_clip(sum, 0, 255);
396  dst[x + y * dst_linesize] = sum;
397  }
398  }
399  return 0;
400 }
401 
402 static av_cold int init(AVFilterContext *ctx)
403 {
404  PerspectiveContext *s = ctx->priv;
405 
406  switch (s->interpolation) {
407  case LINEAR: s->perspective = resample_linear; break;
408  case CUBIC: s->perspective = resample_cubic; break;
409  }
410 
411  return 0;
412 }
413 
414 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
415 {
416  AVFilterContext *ctx = inlink->dst;
417  AVFilterLink *outlink = ctx->outputs[0];
418  PerspectiveContext *s = ctx->priv;
419  AVFrame *out;
420  int plane;
421 
422  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
423  if (!out) {
424  av_frame_free(&frame);
425  return AVERROR(ENOMEM);
426  }
427  av_frame_copy_props(out, frame);
428 
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;
432  ThreadData td = {.dst = out->data[plane],
433  .dst_linesize = out->linesize[plane],
434  .src = frame->data[plane],
435  .src_linesize = frame->linesize[plane],
436  .w = s->linesize[plane],
437  .h = s->height[plane],
438  .hsub = hsub,
439  .vsub = vsub };
440  ctx->internal->execute(ctx, s->perspective, &td, NULL, FFMIN(td.h, ctx->graph->nb_threads));
441  }
442 
443  av_frame_free(&frame);
444  return ff_filter_frame(outlink, out);
445 }
446 
447 static av_cold void uninit(AVFilterContext *ctx)
448 {
449  PerspectiveContext *s = ctx->priv;
450 
451  av_freep(&s->pv);
452 }
453 
454 static const AVFilterPad perspective_inputs[] = {
455  {
456  .name = "default",
457  .type = AVMEDIA_TYPE_VIDEO,
458  .filter_frame = filter_frame,
459  .config_props = config_input,
460  },
461  { NULL }
462 };
463 
465  {
466  .name = "default",
467  .type = AVMEDIA_TYPE_VIDEO,
468  },
469  { NULL }
470 };
471 
473  .name = "perspective",
474  .description = NULL_IF_CONFIG_SMALL("Correct the perspective of video."),
475  .priv_size = sizeof(PerspectiveContext),
476  .init = init,
477  .uninit = uninit,
479  .inputs = perspective_inputs,
480  .outputs = perspective_outputs,
481  .priv_class = &perspective_class,
483 };