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.

628 lines
22KB

  1. /*
  2. * Copyright (c) 2016 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/attributes.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/eval.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "drawutils.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "framesync.h"
  31. static const char *const var_names[] = {
  32. "w", ///< width of the input video
  33. "h", ///< height of the input video
  34. "x", ///< input value for the pixel from input #1
  35. "y", ///< input value for the pixel from input #2
  36. "bdx", ///< input #1 video bitdepth
  37. "bdy", ///< input #2 video bitdepth
  38. NULL
  39. };
  40. enum var_name {
  41. VAR_W,
  42. VAR_H,
  43. VAR_X,
  44. VAR_Y,
  45. VAR_BITDEPTHX,
  46. VAR_BITDEPTHY,
  47. VAR_VARS_NB
  48. };
  49. typedef struct LUT2Context {
  50. const AVClass *class;
  51. FFFrameSync fs;
  52. int odepth;
  53. char *comp_expr_str[4];
  54. AVExpr *comp_expr[4];
  55. double var_values[VAR_VARS_NB];
  56. uint16_t *lut[4]; ///< lookup table for each component
  57. int width[4], height[4];
  58. int widthx[4], heightx[4];
  59. int widthy[4], heighty[4];
  60. int nb_planesx;
  61. int nb_planesy;
  62. int nb_planes;
  63. int depth, depthx, depthy;
  64. int tlut2;
  65. AVFrame *prev_frame; /* only used with tlut2 */
  66. void (*lut2)(struct LUT2Context *s, AVFrame *dst, AVFrame *srcx, AVFrame *srcy);
  67. } LUT2Context;
  68. #define OFFSET(x) offsetof(LUT2Context, x)
  69. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  70. static const AVOption options[] = {
  71. { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  72. { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  73. { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  74. { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  75. { "d", "set output depth", OFFSET(odepth), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 16, .flags = FLAGS },
  76. { NULL }
  77. };
  78. static av_cold void uninit(AVFilterContext *ctx)
  79. {
  80. LUT2Context *s = ctx->priv;
  81. int i;
  82. ff_framesync_uninit(&s->fs);
  83. av_frame_free(&s->prev_frame);
  84. for (i = 0; i < 4; i++) {
  85. av_expr_free(s->comp_expr[i]);
  86. s->comp_expr[i] = NULL;
  87. av_freep(&s->comp_expr_str[i]);
  88. av_freep(&s->lut[i]);
  89. }
  90. }
  91. #define BIT8_FMTS \
  92. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, \
  93. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, \
  94. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, \
  95. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
  96. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, \
  97. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  98. #define BIT9_FMTS \
  99. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GRAY9, \
  100. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, \
  101. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  102. #define BIT10_FMTS \
  103. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, \
  104. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, \
  105. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  106. #define BIT12_FMTS \
  107. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, \
  108. AV_PIX_FMT_GRAY12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRP12,
  109. #define BIT14_FMTS \
  110. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, \
  111. AV_PIX_FMT_GRAY12, AV_PIX_FMT_GBRP14,
  112. #define BIT16_FMTS \
  113. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, \
  114. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, \
  115. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
  116. static int query_formats(AVFilterContext *ctx)
  117. {
  118. LUT2Context *s = ctx->priv;
  119. static const enum AVPixelFormat all_pix_fmts[] = {
  120. BIT8_FMTS
  121. BIT9_FMTS
  122. BIT10_FMTS
  123. BIT12_FMTS
  124. AV_PIX_FMT_NONE
  125. };
  126. static const enum AVPixelFormat bit8_pix_fmts[] = {
  127. BIT8_FMTS
  128. AV_PIX_FMT_NONE
  129. };
  130. static const enum AVPixelFormat bit9_pix_fmts[] = {
  131. BIT9_FMTS
  132. AV_PIX_FMT_NONE
  133. };
  134. static const enum AVPixelFormat bit10_pix_fmts[] = {
  135. BIT10_FMTS
  136. AV_PIX_FMT_NONE
  137. };
  138. static const enum AVPixelFormat bit12_pix_fmts[] = {
  139. BIT12_FMTS
  140. AV_PIX_FMT_NONE
  141. };
  142. static const enum AVPixelFormat bit14_pix_fmts[] = {
  143. BIT14_FMTS
  144. AV_PIX_FMT_NONE
  145. };
  146. static const enum AVPixelFormat bit16_pix_fmts[] = {
  147. BIT16_FMTS
  148. AV_PIX_FMT_NONE
  149. };
  150. const enum AVPixelFormat *pix_fmts;
  151. int ret;
  152. if (s->tlut2 || !s->odepth)
  153. return ff_set_common_formats(ctx, ff_make_format_list(all_pix_fmts));
  154. ret = ff_formats_ref(ff_make_format_list(all_pix_fmts), &ctx->inputs[0]->out_formats);
  155. if (ret < 0)
  156. return ret;
  157. switch (s->odepth) {
  158. case 8: pix_fmts = bit8_pix_fmts; break;
  159. case 9: pix_fmts = bit9_pix_fmts; break;
  160. case 10: pix_fmts = bit10_pix_fmts; break;
  161. case 12: pix_fmts = bit12_pix_fmts; break;
  162. case 14: pix_fmts = bit14_pix_fmts; break;
  163. case 16: pix_fmts = bit16_pix_fmts; break;
  164. default: av_log(ctx, AV_LOG_ERROR, "Unsupported output bit depth %d.\n", s->odepth);
  165. return AVERROR(EINVAL);
  166. }
  167. return ff_formats_ref(ff_make_format_list(pix_fmts), &ctx->outputs[0]->in_formats);
  168. }
  169. static int config_inputx(AVFilterLink *inlink)
  170. {
  171. AVFilterContext *ctx = inlink->dst;
  172. LUT2Context *s = ctx->priv;
  173. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  174. int hsub = desc->log2_chroma_w;
  175. int vsub = desc->log2_chroma_h;
  176. s->nb_planesx = av_pix_fmt_count_planes(inlink->format);
  177. s->heightx[1] = s->heightx[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  178. s->heightx[0] = s->heightx[3] = inlink->h;
  179. s->widthx[1] = s->widthx[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  180. s->widthx[0] = s->widthx[3] = inlink->w;
  181. s->var_values[VAR_W] = inlink->w;
  182. s->var_values[VAR_H] = inlink->h;
  183. s->depthx = desc->comp[0].depth;
  184. s->var_values[VAR_BITDEPTHX] = s->depthx;
  185. if (s->tlut2) {
  186. s->depthy = desc->comp[0].depth;
  187. s->var_values[VAR_BITDEPTHY] = s->depthy;
  188. }
  189. return 0;
  190. }
  191. static int config_inputy(AVFilterLink *inlink)
  192. {
  193. AVFilterContext *ctx = inlink->dst;
  194. LUT2Context *s = ctx->priv;
  195. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  196. int hsub = desc->log2_chroma_w;
  197. int vsub = desc->log2_chroma_h;
  198. s->nb_planesy = av_pix_fmt_count_planes(inlink->format);
  199. s->depthy = desc->comp[0].depth;
  200. s->var_values[VAR_BITDEPTHY] = s->depthy;
  201. s->heighty[1] = s->heighty[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  202. s->heighty[0] = s->heighty[3] = inlink->h;
  203. s->widthy[1] = s->widthy[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  204. s->widthy[0] = s->widthy[3] = inlink->w;
  205. return 0;
  206. }
  207. #define DEFINE_LUT2(zname, xname, yname, ztype, xtype, ytype, zdiv, xdiv, ydiv) \
  208. static void lut2_##zname##_##xname##_##yname(struct LUT2Context *s, \
  209. AVFrame *out, \
  210. AVFrame *srcx, AVFrame *srcy) \
  211. { \
  212. const int odepth = s->odepth; \
  213. int p, y, x; \
  214. \
  215. for (p = 0; p < s->nb_planes; p++) { \
  216. const uint16_t *lut = s->lut[p]; \
  217. const xtype *srcxx; \
  218. const ytype *srcyy; \
  219. ztype *dst; \
  220. \
  221. dst = (ztype *)out->data[p]; \
  222. srcxx = (const xtype *)srcx->data[p]; \
  223. srcyy = (const ytype *)srcy->data[p]; \
  224. \
  225. for (y = 0; y < s->heightx[p]; y++) { \
  226. for (x = 0; x < s->widthx[p]; x++) { \
  227. dst[x] = av_clip_uintp2_c(lut[(srcyy[x] << s->depthx) | srcxx[x]], odepth); \
  228. } \
  229. \
  230. dst += out->linesize[p] / zdiv; \
  231. srcxx += srcx->linesize[p] / xdiv; \
  232. srcyy += srcy->linesize[p] / ydiv; \
  233. } \
  234. } \
  235. }
  236. DEFINE_LUT2(8, 8, 8, uint8_t, uint8_t, uint8_t, 1, 1, 1)
  237. DEFINE_LUT2(8, 8, 16, uint8_t, uint8_t, uint16_t, 1, 1, 2)
  238. DEFINE_LUT2(8, 16, 8, uint8_t, uint16_t, uint8_t, 1, 2, 1)
  239. DEFINE_LUT2(8, 16, 16, uint8_t, uint16_t, uint16_t, 1, 2, 2)
  240. DEFINE_LUT2(16, 8, 8, uint16_t, uint8_t, uint8_t, 2, 1, 1)
  241. DEFINE_LUT2(16, 8, 16, uint16_t, uint8_t, uint16_t, 2, 1, 2)
  242. DEFINE_LUT2(16, 16, 8, uint16_t, uint16_t, uint8_t, 2, 2, 1)
  243. DEFINE_LUT2(16, 16, 16, uint16_t, uint16_t, uint16_t, 2, 2, 2)
  244. static int process_frame(FFFrameSync *fs)
  245. {
  246. AVFilterContext *ctx = fs->parent;
  247. LUT2Context *s = fs->opaque;
  248. AVFilterLink *outlink = ctx->outputs[0];
  249. AVFrame *out, *srcx = NULL, *srcy = NULL;
  250. int ret;
  251. if ((ret = ff_framesync_get_frame(&s->fs, 0, &srcx, 0)) < 0 ||
  252. (ret = ff_framesync_get_frame(&s->fs, 1, &srcy, 0)) < 0)
  253. return ret;
  254. if (ctx->is_disabled || !srcy) {
  255. out = av_frame_clone(srcx);
  256. if (!out)
  257. return AVERROR(ENOMEM);
  258. } else {
  259. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  260. if (!out)
  261. return AVERROR(ENOMEM);
  262. av_frame_copy_props(out, srcx);
  263. s->lut2(s, out, srcx, srcy);
  264. }
  265. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  266. return ff_filter_frame(outlink, out);
  267. }
  268. static int config_output(AVFilterLink *outlink)
  269. {
  270. AVFilterContext *ctx = outlink->src;
  271. LUT2Context *s = ctx->priv;
  272. int p, ret;
  273. s->depth = s->depthx + s->depthy;
  274. s->nb_planes = s->nb_planesx;
  275. s->lut2 = s->depth > 16 ? lut2_16_16_16 : lut2_8_8_8;
  276. if (s->odepth) {
  277. if (s->depthx == 8 && s->depthy == 8 && s->odepth > 8)
  278. s->lut2 = lut2_16_8_8;
  279. if (s->depthx > 8 && s->depthy == 8 && s->odepth > 8)
  280. s->lut2 = lut2_16_16_8;
  281. if (s->depthx == 8 && s->depthy > 8 && s->odepth > 8)
  282. s->lut2 = lut2_16_8_16;
  283. if (s->depthx == 8 && s->depthy == 8 && s->odepth == 8)
  284. s->lut2 = lut2_8_8_8;
  285. if (s->depthx > 8 && s->depthy == 8 && s->odepth == 8)
  286. s->lut2 = lut2_8_16_8;
  287. if (s->depthx == 8 && s->depthy > 8 && s->odepth == 8)
  288. s->lut2 = lut2_8_8_16;
  289. if (s->depthx > 8 && s->depthy > 8 && s->odepth == 8)
  290. s->lut2 = lut2_8_16_16;
  291. } else {
  292. s->odepth = s->depthx;
  293. }
  294. for (p = 0; p < s->nb_planes; p++) {
  295. s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
  296. if (!s->lut[p])
  297. return AVERROR(ENOMEM);
  298. }
  299. for (p = 0; p < s->nb_planes; p++) {
  300. double res;
  301. int x, y;
  302. /* create the parsed expression */
  303. av_expr_free(s->comp_expr[p]);
  304. s->comp_expr[p] = NULL;
  305. ret = av_expr_parse(&s->comp_expr[p], s->comp_expr_str[p],
  306. var_names, NULL, NULL, NULL, NULL, 0, ctx);
  307. if (ret < 0) {
  308. av_log(ctx, AV_LOG_ERROR,
  309. "Error when parsing the expression '%s' for the component %d.\n",
  310. s->comp_expr_str[p], p);
  311. return AVERROR(EINVAL);
  312. }
  313. /* compute the lut */
  314. for (y = 0; y < (1 << s->depthy); y++) {
  315. s->var_values[VAR_Y] = y;
  316. for (x = 0; x < (1 << s->depthx); x++) {
  317. s->var_values[VAR_X] = x;
  318. res = av_expr_eval(s->comp_expr[p], s->var_values, s);
  319. if (isnan(res)) {
  320. av_log(ctx, AV_LOG_ERROR,
  321. "Error when evaluating the expression '%s' for the values %d and %d for the component %d.\n",
  322. s->comp_expr_str[p], x, y, p);
  323. return AVERROR(EINVAL);
  324. }
  325. s->lut[p][(y << s->depthx) + x] = res;
  326. }
  327. }
  328. }
  329. return 0;
  330. }
  331. static int lut2_config_output(AVFilterLink *outlink)
  332. {
  333. AVFilterContext *ctx = outlink->src;
  334. LUT2Context *s = ctx->priv;
  335. AVFilterLink *srcx = ctx->inputs[0];
  336. AVFilterLink *srcy = ctx->inputs[1];
  337. FFFrameSyncIn *in;
  338. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  339. int hsub = desc->log2_chroma_w;
  340. int vsub = desc->log2_chroma_h;
  341. int ret;
  342. outlink->w = srcx->w;
  343. outlink->h = srcx->h;
  344. outlink->time_base = srcx->time_base;
  345. outlink->sample_aspect_ratio = srcx->sample_aspect_ratio;
  346. outlink->frame_rate = srcx->frame_rate;
  347. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  348. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(outlink->h, vsub);
  349. s->height[0] = s->height[3] = outlink->h;
  350. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(outlink->w, hsub);
  351. s->width[0] = s->width[3] = outlink->w;
  352. if (!s->odepth && srcx->format != srcy->format) {
  353. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  354. return AVERROR(EINVAL);
  355. }
  356. if (srcx->w != srcy->w || srcx->h != srcy->h) {
  357. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  358. "(size %dx%d) do not match the corresponding "
  359. "second input link %s parameters (size %dx%d)\n",
  360. ctx->input_pads[0].name, srcx->w, srcx->h,
  361. ctx->input_pads[1].name,
  362. srcy->w, srcy->h);
  363. return AVERROR(EINVAL);
  364. }
  365. if (s->nb_planesx != s->nb_planesy) {
  366. av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
  367. "(%d) do not match the corresponding "
  368. "second input link %s number of planes (%d)\n",
  369. ctx->input_pads[0].name, s->nb_planesx,
  370. ctx->input_pads[1].name, s->nb_planesy);
  371. return AVERROR(EINVAL);
  372. }
  373. if (s->nb_planesx != s->nb_planes) {
  374. av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
  375. "(%d) do not match the corresponding "
  376. "output link %s number of planes (%d)\n",
  377. ctx->input_pads[0].name, s->nb_planesx,
  378. ctx->output_pads[0].name, s->nb_planes);
  379. return AVERROR(EINVAL);
  380. }
  381. if (s->widthx[1] != s->widthy[1] || s->heightx[1] != s->heighty[1]) {
  382. av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
  383. "(size %dx%d) do not match the corresponding "
  384. "second input link %s 2nd plane (size %dx%d)\n",
  385. ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
  386. ctx->input_pads[1].name,
  387. s->widthy[1], s->heighty[1]);
  388. return AVERROR(EINVAL);
  389. }
  390. if (s->widthx[2] != s->widthy[2] || s->heightx[2] != s->heighty[2]) {
  391. av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
  392. "(size %dx%d) do not match the corresponding "
  393. "second input link %s 3rd plane (size %dx%d)\n",
  394. ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
  395. ctx->input_pads[1].name,
  396. s->widthy[2], s->heighty[2]);
  397. return AVERROR(EINVAL);
  398. }
  399. if (s->widthx[1] != s->width[1] || s->heightx[1] != s->height[1]) {
  400. av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
  401. "(size %dx%d) do not match the corresponding "
  402. "output link %s 2nd plane (size %dx%d)\n",
  403. ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
  404. ctx->output_pads[0].name, s->width[1], s->height[1]);
  405. return AVERROR(EINVAL);
  406. }
  407. if (s->widthx[2] != s->width[2] || s->heightx[2] != s->height[2]) {
  408. av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
  409. "(size %dx%d) do not match the corresponding "
  410. "output link %s 3rd plane (size %dx%d)\n",
  411. ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
  412. ctx->output_pads[0].name, s->width[2], s->height[2]);
  413. return AVERROR(EINVAL);
  414. }
  415. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  416. return ret;
  417. in = s->fs.in;
  418. in[0].time_base = srcx->time_base;
  419. in[1].time_base = srcy->time_base;
  420. in[0].sync = 2;
  421. in[0].before = EXT_STOP;
  422. in[0].after = EXT_INFINITY;
  423. in[1].sync = 1;
  424. in[1].before = EXT_STOP;
  425. in[1].after = EXT_INFINITY;
  426. s->fs.opaque = s;
  427. s->fs.on_event = process_frame;
  428. if ((ret = config_output(outlink)) < 0)
  429. return ret;
  430. return ff_framesync_configure(&s->fs);
  431. }
  432. static int activate(AVFilterContext *ctx)
  433. {
  434. LUT2Context *s = ctx->priv;
  435. return ff_framesync_activate(&s->fs);
  436. }
  437. static const AVFilterPad inputs[] = {
  438. {
  439. .name = "srcx",
  440. .type = AVMEDIA_TYPE_VIDEO,
  441. .config_props = config_inputx,
  442. },
  443. {
  444. .name = "srcy",
  445. .type = AVMEDIA_TYPE_VIDEO,
  446. .config_props = config_inputy,
  447. },
  448. { NULL }
  449. };
  450. static const AVFilterPad outputs[] = {
  451. {
  452. .name = "default",
  453. .type = AVMEDIA_TYPE_VIDEO,
  454. .config_props = lut2_config_output,
  455. },
  456. { NULL }
  457. };
  458. #define lut2_options options
  459. FRAMESYNC_DEFINE_CLASS(lut2, LUT2Context, fs);
  460. AVFilter ff_vf_lut2 = {
  461. .name = "lut2",
  462. .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two video inputs."),
  463. .preinit = lut2_framesync_preinit,
  464. .priv_size = sizeof(LUT2Context),
  465. .priv_class = &lut2_class,
  466. .uninit = uninit,
  467. .query_formats = query_formats,
  468. .activate = activate,
  469. .inputs = inputs,
  470. .outputs = outputs,
  471. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  472. };
  473. #if CONFIG_TLUT2_FILTER
  474. static av_cold int init(AVFilterContext *ctx)
  475. {
  476. LUT2Context *s = ctx->priv;
  477. s->tlut2 = !strcmp(ctx->filter->name, "tlut2");
  478. return 0;
  479. }
  480. static int tlut2_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  481. {
  482. AVFilterContext *ctx = inlink->dst;
  483. LUT2Context *s = ctx->priv;
  484. AVFilterLink *outlink = ctx->outputs[0];
  485. if (s->prev_frame) {
  486. AVFrame *out;
  487. if (ctx->is_disabled) {
  488. out = av_frame_clone(frame);
  489. } else {
  490. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  491. if (!out) {
  492. av_frame_free(&s->prev_frame);
  493. s->prev_frame = frame;
  494. return AVERROR(ENOMEM);
  495. }
  496. av_frame_copy_props(out, frame);
  497. s->lut2(s, out, frame, s->prev_frame);
  498. }
  499. av_frame_free(&s->prev_frame);
  500. s->prev_frame = frame;
  501. return ff_filter_frame(outlink, out);
  502. }
  503. s->prev_frame = frame;
  504. return 0;
  505. }
  506. static const AVOption tlut2_options[] = {
  507. { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  508. { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  509. { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  510. { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  511. { NULL }
  512. };
  513. AVFILTER_DEFINE_CLASS(tlut2);
  514. static const AVFilterPad tlut2_inputs[] = {
  515. {
  516. .name = "default",
  517. .type = AVMEDIA_TYPE_VIDEO,
  518. .filter_frame = tlut2_filter_frame,
  519. .config_props = config_inputx,
  520. },
  521. { NULL }
  522. };
  523. static const AVFilterPad tlut2_outputs[] = {
  524. {
  525. .name = "default",
  526. .type = AVMEDIA_TYPE_VIDEO,
  527. .config_props = config_output,
  528. },
  529. { NULL }
  530. };
  531. AVFilter ff_vf_tlut2 = {
  532. .name = "tlut2",
  533. .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two successive frames."),
  534. .priv_size = sizeof(LUT2Context),
  535. .priv_class = &tlut2_class,
  536. .query_formats = query_formats,
  537. .init = init,
  538. .uninit = uninit,
  539. .inputs = tlut2_inputs,
  540. .outputs = tlut2_outputs,
  541. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  542. };
  543. #endif