vf_perspective.c 15 KB

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