vf_perspective.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #include "libavutil/eval.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/opt.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. #define SUB_PIXEL_BITS 8
  30. #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
  31. #define COEFF_BITS 11
  32. #define LINEAR 0
  33. #define CUBIC 1
  34. typedef struct PerspectiveContext {
  35. const AVClass *class;
  36. char *expr_str[4][2];
  37. double ref[4][2];
  38. int32_t (*pv)[2];
  39. int32_t coeff[SUB_PIXELS][4];
  40. int interpolation;
  41. int linesize[4];
  42. int height[4];
  43. int hsub, vsub;
  44. int nb_planes;
  45. void (*perspective)(struct PerspectiveContext *s,
  46. uint8_t *dst, int dst_linesize,
  47. uint8_t *src, int src_linesize,
  48. int w, int h, int hsub, int vsub);
  49. } PerspectiveContext;
  50. #define OFFSET(x) offsetof(PerspectiveContext, x)
  51. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  52. static const AVOption perspective_options[] = {
  53. { "x0", "set top left x coordinate", OFFSET(expr_str[0][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  54. { "y0", "set top left y coordinate", OFFSET(expr_str[0][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  55. { "x1", "set top right x coordinate", OFFSET(expr_str[1][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
  56. { "y1", "set top right y coordinate", OFFSET(expr_str[1][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  57. { "x2", "set bottom left x coordinate", OFFSET(expr_str[2][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
  58. { "y2", "set bottom left y coordinate", OFFSET(expr_str[2][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
  59. { "x3", "set bottom right x coordinate", OFFSET(expr_str[3][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
  60. { "y3", "set bottom right y coordinate", OFFSET(expr_str[3][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
  61. { "interpolation", "set interpolation", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, 1, FLAGS, "interpolation" },
  62. { "linear", "", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "interpolation" },
  63. { "cubic", "", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC}, 0, 0, FLAGS, "interpolation" },
  64. { NULL }
  65. };
  66. AVFILTER_DEFINE_CLASS(perspective);
  67. static int query_formats(AVFilterContext *ctx)
  68. {
  69. static const enum AVPixelFormat pix_fmts[] = {
  70. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  71. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
  72. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  73. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  74. };
  75. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  76. return 0;
  77. }
  78. static inline double get_coeff(double d)
  79. {
  80. double coeff, A = -0.60;
  81. d = fabs(d);
  82. if (d < 1.0)
  83. coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
  84. else if (d < 2.0)
  85. coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
  86. else
  87. coeff = 0.0;
  88. return coeff;
  89. }
  90. static const char *const var_names[] = { "W", "H", NULL };
  91. enum { VAR_W, VAR_H, VAR_VARS_NB };
  92. static int config_input(AVFilterLink *inlink)
  93. {
  94. double x0, x1, x2, x3, x4, x5, x6, x7, q;
  95. AVFilterContext *ctx = inlink->dst;
  96. PerspectiveContext *s = ctx->priv;
  97. double (*ref)[2] = s->ref;
  98. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  99. double values[VAR_VARS_NB] = { [VAR_W] = inlink->w, [VAR_H] = inlink->h };
  100. int h = inlink->h;
  101. int w = inlink->w;
  102. int x, y, i, j, ret;
  103. for (i = 0; i < 4; i++) {
  104. for (j = 0; j < 2; j++) {
  105. if (!s->expr_str[i][j])
  106. return AVERROR(EINVAL);
  107. ret = av_expr_parse_and_eval(&s->ref[i][j], s->expr_str[i][j],
  108. var_names, &values[0],
  109. NULL, NULL, NULL, NULL,
  110. 0, 0, ctx);
  111. if (ret < 0)
  112. return ret;
  113. }
  114. }
  115. s->hsub = desc->log2_chroma_w;
  116. s->vsub = desc->log2_chroma_h;
  117. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  118. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  119. return ret;
  120. s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  121. s->height[0] = s->height[3] = inlink->h;
  122. s->pv = av_realloc_f(s->pv, w * h, 2 * sizeof(*s->pv));
  123. if (!s->pv)
  124. return AVERROR(ENOMEM);
  125. x6 = ((ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
  126. (ref[2][1] - ref[3][1]) -
  127. ( ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
  128. (ref[2][0] - ref[3][0])) * h;
  129. x7 = ((ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
  130. (ref[1][0] - ref[3][0]) -
  131. ( ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
  132. (ref[1][1] - ref[3][1])) * w;
  133. q = ( ref[1][0] - ref[3][0]) * (ref[2][1] - ref[3][1]) -
  134. ( ref[2][0] - ref[3][0]) * (ref[1][1] - ref[3][1]);
  135. x0 = q * (ref[1][0] - ref[0][0]) * h + x6 * ref[1][0];
  136. x1 = q * (ref[2][0] - ref[0][0]) * w + x7 * ref[2][0];
  137. x2 = q * ref[0][0] * w * h;
  138. x3 = q * (ref[1][1] - ref[0][1]) * h + x6 * ref[1][1];
  139. x4 = q * (ref[2][1] - ref[0][1]) * w + x7 * ref[2][1];
  140. x5 = q * ref[0][1] * w * h;
  141. for (y = 0; y < h; y++){
  142. for (x = 0; x < w; x++){
  143. int u, v;
  144. u = (int)floor(SUB_PIXELS * (x0 * x + x1 * y + x2) /
  145. (x6 * x + x7 * y + q * w * h) + 0.5);
  146. v = (int)floor(SUB_PIXELS * (x3 * x + x4 * y + x5) /
  147. (x6 * x + x7 * y + q * w * h) + 0.5);
  148. s->pv[x + y * w][0] = u;
  149. s->pv[x + y * w][1] = v;
  150. }
  151. }
  152. for (i = 0; i < SUB_PIXELS; i++){
  153. double d = i / (double)SUB_PIXELS;
  154. double temp[4];
  155. double sum = 0;
  156. for (j = 0; j < 4; j++)
  157. temp[j] = get_coeff(j - d - 1);
  158. for (j = 0; j < 4; j++)
  159. sum += temp[j];
  160. for (j = 0; j < 4; j++)
  161. s->coeff[i][j] = (int)floor((1 << COEFF_BITS) * temp[j] / sum + 0.5);
  162. }
  163. return 0;
  164. }
  165. static void resample_cubic(PerspectiveContext *s,
  166. uint8_t *dst, int dst_linesize,
  167. uint8_t *src, int src_linesize,
  168. int w, int h, int hsub, int vsub)
  169. {
  170. const int linesize = s->linesize[0];
  171. int x, y;
  172. for (y = 0; y < h; y++) {
  173. int sy = y << vsub;
  174. for (x = 0; x < w; x++) {
  175. int u, v, subU, subV, sum, sx;
  176. sx = x << hsub;
  177. u = s->pv[sx + sy * linesize][0] >> hsub;
  178. v = s->pv[sx + sy * linesize][1] >> vsub;
  179. subU = u & (SUB_PIXELS - 1);
  180. subV = v & (SUB_PIXELS - 1);
  181. u >>= SUB_PIXEL_BITS;
  182. v >>= SUB_PIXEL_BITS;
  183. if (u > 0 && v > 0 && u < w - 2 && v < h - 2){
  184. const int index = u + v*src_linesize;
  185. const int a = s->coeff[subU][0];
  186. const int b = s->coeff[subU][1];
  187. const int c = s->coeff[subU][2];
  188. const int d = s->coeff[subU][3];
  189. sum = s->coeff[subV][0] * (a * src[index - 1 - src_linesize] + b * src[index - 0 - src_linesize] +
  190. c * src[index + 1 - src_linesize] + d * src[index + 2 - src_linesize]) +
  191. s->coeff[subV][1] * (a * src[index - 1 ] + b * src[index - 0 ] +
  192. c * src[index + 1 ] + d * src[index + 2 ]) +
  193. s->coeff[subV][2] * (a * src[index - 1 + src_linesize] + b * src[index - 0 + src_linesize] +
  194. c * src[index + 1 + src_linesize] + d * src[index + 2 + src_linesize]) +
  195. s->coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize] +
  196. c * src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]);
  197. } else {
  198. int dx, dy;
  199. sum = 0;
  200. for (dy = 0; dy < 4; dy++) {
  201. int iy = v + dy - 1;
  202. if (iy < 0)
  203. iy = 0;
  204. else if (iy >= h)
  205. iy = h-1;
  206. for (dx = 0; dx < 4; dx++) {
  207. int ix = u + dx - 1;
  208. if (ix < 0)
  209. ix = 0;
  210. else if (ix >= w)
  211. ix = w - 1;
  212. sum += s->coeff[subU][dx] * s->coeff[subV][dy] * src[ ix + iy * src_linesize];
  213. }
  214. }
  215. }
  216. sum = (sum + (1<<(COEFF_BITS * 2 - 1))) >> (COEFF_BITS * 2);
  217. sum = av_clip(sum, 0, 255);
  218. dst[x + y * dst_linesize] = sum;
  219. }
  220. }
  221. }
  222. static void resample_linear(PerspectiveContext *s,
  223. uint8_t *dst, int dst_linesize,
  224. uint8_t *src, int src_linesize,
  225. int w, int h, int hsub, int vsub)
  226. {
  227. const int linesize = s->linesize[0];
  228. int x, y;
  229. for (y = 0; y < h; y++){
  230. int sy = y << vsub;
  231. for (x = 0; x < w; x++){
  232. int u, v, subU, subV, sum, sx, index, subUI, subVI;
  233. sx = x << hsub;
  234. u = s->pv[sx + sy * linesize][0] >> hsub;
  235. v = s->pv[sx + sy * linesize][1] >> vsub;
  236. subU = u & (SUB_PIXELS - 1);
  237. subV = v & (SUB_PIXELS - 1);
  238. u >>= SUB_PIXEL_BITS;
  239. v >>= SUB_PIXEL_BITS;
  240. index = u + v * src_linesize;
  241. subUI = SUB_PIXELS - subU;
  242. subVI = SUB_PIXELS - subV;
  243. if ((unsigned)u < (unsigned)(w - 1)){
  244. if((unsigned)v < (unsigned)(h - 1)){
  245. sum = subVI * (subUI * src[index] + subU * src[index + 1]) +
  246. subV * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]);
  247. sum = (sum + (1 << (SUB_PIXEL_BITS * 2 - 1)))>> (SUB_PIXEL_BITS * 2);
  248. } else {
  249. if (v < 0)
  250. v = 0;
  251. else
  252. v = h - 1;
  253. index = u + v * src_linesize;
  254. sum = subUI * src[index] + subU * src[index + 1];
  255. sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
  256. }
  257. } else {
  258. if (u < 0)
  259. u = 0;
  260. else
  261. u = w - 1;
  262. if ((unsigned)v < (unsigned)(h - 1)){
  263. index = u + v * src_linesize;
  264. sum = subVI * src[index] + subV * src[index + src_linesize];
  265. sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
  266. } else {
  267. if (v < 0)
  268. v = 0;
  269. else
  270. v = h - 1;
  271. index = u + v * src_linesize;
  272. sum = src[index];
  273. }
  274. }
  275. sum = av_clip(sum, 0, 255);
  276. dst[x + y * dst_linesize] = sum;
  277. }
  278. }
  279. }
  280. static av_cold int init(AVFilterContext *ctx)
  281. {
  282. PerspectiveContext *s = ctx->priv;
  283. switch (s->interpolation) {
  284. case LINEAR: s->perspective = resample_linear; break;
  285. case CUBIC: s->perspective = resample_cubic; break;
  286. }
  287. return 0;
  288. }
  289. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  290. {
  291. AVFilterContext *ctx = inlink->dst;
  292. AVFilterLink *outlink = ctx->outputs[0];
  293. PerspectiveContext *s = ctx->priv;
  294. AVFrame *out;
  295. int plane;
  296. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  297. if (!out) {
  298. av_frame_free(&frame);
  299. return AVERROR(ENOMEM);
  300. }
  301. av_frame_copy_props(out, frame);
  302. for (plane = 0; plane < s->nb_planes; plane++) {
  303. int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
  304. int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
  305. s->perspective(s, out->data[plane], out->linesize[plane],
  306. frame->data[plane], frame->linesize[plane],
  307. s->linesize[plane], s->height[plane], hsub, vsub);
  308. }
  309. av_frame_free(&frame);
  310. return ff_filter_frame(outlink, out);
  311. }
  312. static av_cold void uninit(AVFilterContext *ctx)
  313. {
  314. PerspectiveContext *s = ctx->priv;
  315. av_freep(&s->pv);
  316. }
  317. static const AVFilterPad perspective_inputs[] = {
  318. {
  319. .name = "default",
  320. .type = AVMEDIA_TYPE_VIDEO,
  321. .filter_frame = filter_frame,
  322. .config_props = config_input,
  323. },
  324. { NULL }
  325. };
  326. static const AVFilterPad perspective_outputs[] = {
  327. {
  328. .name = "default",
  329. .type = AVMEDIA_TYPE_VIDEO,
  330. },
  331. { NULL }
  332. };
  333. AVFilter ff_vf_perspective = {
  334. .name = "perspective",
  335. .description = NULL_IF_CONFIG_SMALL("Correct the perspective of video."),
  336. .priv_size = sizeof(PerspectiveContext),
  337. .init = init,
  338. .uninit = uninit,
  339. .query_formats = query_formats,
  340. .inputs = perspective_inputs,
  341. .outputs = perspective_outputs,
  342. .priv_class = &perspective_class,
  343. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  344. };