vf_perspective.c 18 KB

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