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.

656 lines
23KB

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