vf_perspective.c 17 KB

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