You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

489 lines
18KB

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