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.

460 lines
13KB

  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. char *comp_expr_str[4];
  53. AVExpr *comp_expr[4];
  54. double var_values[VAR_VARS_NB];
  55. uint16_t *lut[4]; ///< lookup table for each component
  56. int width[4], height[4];
  57. int nb_planes;
  58. int depth, depthx, depthy;
  59. int tlut2;
  60. AVFrame *prev_frame; /* only used with tlut2 */
  61. void (*lut2)(struct LUT2Context *s, AVFrame *dst, AVFrame *srcx, AVFrame *srcy);
  62. } LUT2Context;
  63. #define OFFSET(x) offsetof(LUT2Context, x)
  64. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  65. static const AVOption options[] = {
  66. { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  67. { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  68. { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  69. { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  70. { NULL }
  71. };
  72. static av_cold void uninit(AVFilterContext *ctx)
  73. {
  74. LUT2Context *s = ctx->priv;
  75. int i;
  76. ff_framesync_uninit(&s->fs);
  77. av_frame_free(&s->prev_frame);
  78. for (i = 0; i < 4; i++) {
  79. av_expr_free(s->comp_expr[i]);
  80. s->comp_expr[i] = NULL;
  81. av_freep(&s->comp_expr_str[i]);
  82. av_freep(&s->lut[i]);
  83. }
  84. }
  85. static int query_formats(AVFilterContext *ctx)
  86. {
  87. static const enum AVPixelFormat pix_fmts[] = {
  88. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  89. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  90. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  91. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  92. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  93. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  94. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  95. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  96. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  97. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  98. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  99. AV_PIX_FMT_GBRP12,
  100. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12,
  101. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
  102. AV_PIX_FMT_NONE
  103. };
  104. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  105. }
  106. static int config_inputx(AVFilterLink *inlink)
  107. {
  108. AVFilterContext *ctx = inlink->dst;
  109. LUT2Context *s = ctx->priv;
  110. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  111. int hsub = desc->log2_chroma_w;
  112. int vsub = desc->log2_chroma_h;
  113. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  114. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  115. s->height[0] = s->height[3] = inlink->h;
  116. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  117. s->width[0] = s->width[3] = inlink->w;
  118. s->var_values[VAR_W] = inlink->w;
  119. s->var_values[VAR_H] = inlink->h;
  120. s->depthx = desc->comp[0].depth;
  121. s->var_values[VAR_BITDEPTHX] = s->depthx;
  122. if (s->tlut2) {
  123. s->depthy = desc->comp[0].depth;
  124. s->var_values[VAR_BITDEPTHY] = s->depthy;
  125. }
  126. return 0;
  127. }
  128. static int config_inputy(AVFilterLink *inlink)
  129. {
  130. AVFilterContext *ctx = inlink->dst;
  131. LUT2Context *s = ctx->priv;
  132. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  133. s->depthy = desc->comp[0].depth;
  134. s->var_values[VAR_BITDEPTHY] = s->depthy;
  135. return 0;
  136. }
  137. static void lut2_8bit(struct LUT2Context *s, AVFrame *out, AVFrame *srcx, AVFrame *srcy)
  138. {
  139. int p, y, x;
  140. for (p = 0; p < s->nb_planes; p++) {
  141. const uint16_t *lut = s->lut[p];
  142. const uint8_t *srcxx, *srcyy;
  143. uint8_t *dst;
  144. dst = out->data[p];
  145. srcxx = srcx->data[p];
  146. srcyy = srcy->data[p];
  147. for (y = 0; y < s->height[p]; y++) {
  148. for (x = 0; x < s->width[p]; x++) {
  149. dst[x] = lut[(srcyy[x] << s->depthx) | srcxx[x]];
  150. }
  151. dst += out->linesize[p];
  152. srcxx += srcx->linesize[p];
  153. srcyy += srcy->linesize[p];
  154. }
  155. }
  156. }
  157. static void lut2_16bit(struct LUT2Context *s, AVFrame *out, AVFrame *srcx, AVFrame *srcy)
  158. {
  159. int p, y, x;
  160. for (p = 0; p < s->nb_planes; p++) {
  161. const uint16_t *lut = s->lut[p];
  162. const uint16_t *srcxx, *srcyy;
  163. uint16_t *dst;
  164. dst = (uint16_t *)out->data[p];
  165. srcxx = (uint16_t *)srcx->data[p];
  166. srcyy = (uint16_t *)srcy->data[p];
  167. for (y = 0; y < s->height[p]; y++) {
  168. for (x = 0; x < s->width[p]; x++) {
  169. dst[x] = lut[(srcyy[x] << s->depthx) | srcxx[x]];
  170. }
  171. dst += out->linesize[p] / 2;
  172. srcxx += srcx->linesize[p] / 2;
  173. srcyy += srcy->linesize[p] / 2;
  174. }
  175. }
  176. }
  177. static int process_frame(FFFrameSync *fs)
  178. {
  179. AVFilterContext *ctx = fs->parent;
  180. LUT2Context *s = fs->opaque;
  181. AVFilterLink *outlink = ctx->outputs[0];
  182. AVFrame *out, *srcx = NULL, *srcy = NULL;
  183. int ret;
  184. if ((ret = ff_framesync_get_frame(&s->fs, 0, &srcx, 0)) < 0 ||
  185. (ret = ff_framesync_get_frame(&s->fs, 1, &srcy, 0)) < 0)
  186. return ret;
  187. if (ctx->is_disabled || !srcy) {
  188. out = av_frame_clone(srcx);
  189. if (!out)
  190. return AVERROR(ENOMEM);
  191. } else {
  192. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  193. if (!out)
  194. return AVERROR(ENOMEM);
  195. av_frame_copy_props(out, srcx);
  196. s->lut2(s, out, srcx, srcy);
  197. }
  198. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  199. return ff_filter_frame(outlink, out);
  200. }
  201. static int config_output(AVFilterLink *outlink)
  202. {
  203. AVFilterContext *ctx = outlink->src;
  204. LUT2Context *s = ctx->priv;
  205. int p, ret;
  206. s->depth = s->depthx + s->depthy;
  207. s->lut2 = s->depth > 16 ? lut2_16bit : lut2_8bit;
  208. for (p = 0; p < s->nb_planes; p++) {
  209. s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
  210. if (!s->lut[p])
  211. return AVERROR(ENOMEM);
  212. }
  213. for (p = 0; p < s->nb_planes; p++) {
  214. double res;
  215. int x, y;
  216. /* create the parsed expression */
  217. av_expr_free(s->comp_expr[p]);
  218. s->comp_expr[p] = NULL;
  219. ret = av_expr_parse(&s->comp_expr[p], s->comp_expr_str[p],
  220. var_names, NULL, NULL, NULL, NULL, 0, ctx);
  221. if (ret < 0) {
  222. av_log(ctx, AV_LOG_ERROR,
  223. "Error when parsing the expression '%s' for the component %d.\n",
  224. s->comp_expr_str[p], p);
  225. return AVERROR(EINVAL);
  226. }
  227. /* compute the lut */
  228. for (y = 0; y < (1 << s->depthx); y++) {
  229. s->var_values[VAR_Y] = y;
  230. for (x = 0; x < (1 << s->depthx); x++) {
  231. s->var_values[VAR_X] = x;
  232. res = av_expr_eval(s->comp_expr[p], s->var_values, s);
  233. if (isnan(res)) {
  234. av_log(ctx, AV_LOG_ERROR,
  235. "Error when evaluating the expression '%s' for the values %d and %d for the component %d.\n",
  236. s->comp_expr_str[p], x, y, p);
  237. return AVERROR(EINVAL);
  238. }
  239. s->lut[p][(y << s->depthx) + x] = res;
  240. }
  241. }
  242. }
  243. return 0;
  244. }
  245. static int lut2_config_output(AVFilterLink *outlink)
  246. {
  247. AVFilterContext *ctx = outlink->src;
  248. LUT2Context *s = ctx->priv;
  249. AVFilterLink *srcx = ctx->inputs[0];
  250. AVFilterLink *srcy = ctx->inputs[1];
  251. FFFrameSyncIn *in;
  252. int ret;
  253. if (srcx->format != srcy->format) {
  254. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  255. return AVERROR(EINVAL);
  256. }
  257. if (srcx->w != srcy->w || srcx->h != srcy->h) {
  258. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  259. "(size %dx%d) do not match the corresponding "
  260. "second input link %s parameters (size %dx%d)\n",
  261. ctx->input_pads[0].name, srcx->w, srcx->h,
  262. ctx->input_pads[1].name,
  263. srcy->w, srcy->h);
  264. return AVERROR(EINVAL);
  265. }
  266. outlink->w = srcx->w;
  267. outlink->h = srcx->h;
  268. outlink->time_base = srcx->time_base;
  269. outlink->sample_aspect_ratio = srcx->sample_aspect_ratio;
  270. outlink->frame_rate = srcx->frame_rate;
  271. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  272. return ret;
  273. in = s->fs.in;
  274. in[0].time_base = srcx->time_base;
  275. in[1].time_base = srcy->time_base;
  276. in[0].sync = 2;
  277. in[0].before = EXT_STOP;
  278. in[0].after = EXT_INFINITY;
  279. in[1].sync = 1;
  280. in[1].before = EXT_STOP;
  281. in[1].after = EXT_INFINITY;
  282. s->fs.opaque = s;
  283. s->fs.on_event = process_frame;
  284. if ((ret = config_output(outlink)) < 0)
  285. return ret;
  286. return ff_framesync_configure(&s->fs);
  287. }
  288. static int activate(AVFilterContext *ctx)
  289. {
  290. LUT2Context *s = ctx->priv;
  291. return ff_framesync_activate(&s->fs);
  292. }
  293. static const AVFilterPad inputs[] = {
  294. {
  295. .name = "srcx",
  296. .type = AVMEDIA_TYPE_VIDEO,
  297. .config_props = config_inputx,
  298. },
  299. {
  300. .name = "srcy",
  301. .type = AVMEDIA_TYPE_VIDEO,
  302. .config_props = config_inputy,
  303. },
  304. { NULL }
  305. };
  306. static const AVFilterPad outputs[] = {
  307. {
  308. .name = "default",
  309. .type = AVMEDIA_TYPE_VIDEO,
  310. .config_props = lut2_config_output,
  311. },
  312. { NULL }
  313. };
  314. #define lut2_options options
  315. FRAMESYNC_DEFINE_CLASS(lut2, LUT2Context, fs);
  316. AVFilter ff_vf_lut2 = {
  317. .name = "lut2",
  318. .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two video inputs."),
  319. .preinit = lut2_framesync_preinit,
  320. .priv_size = sizeof(LUT2Context),
  321. .priv_class = &lut2_class,
  322. .uninit = uninit,
  323. .query_formats = query_formats,
  324. .activate = activate,
  325. .inputs = inputs,
  326. .outputs = outputs,
  327. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  328. };
  329. #if CONFIG_TLUT2_FILTER
  330. static av_cold int init(AVFilterContext *ctx)
  331. {
  332. LUT2Context *s = ctx->priv;
  333. s->tlut2 = !strcmp(ctx->filter->name, "tlut2");
  334. return 0;
  335. }
  336. static int tlut2_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  337. {
  338. LUT2Context *s = inlink->dst->priv;
  339. AVFilterLink *outlink = inlink->dst->outputs[0];
  340. if (s->prev_frame) {
  341. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  342. if (!out) {
  343. av_frame_free(&s->prev_frame);
  344. s->prev_frame = frame;
  345. return AVERROR(ENOMEM);
  346. }
  347. av_frame_copy_props(out, frame);
  348. s->lut2(s, out, frame, s->prev_frame);
  349. av_frame_free(&s->prev_frame);
  350. s->prev_frame = frame;
  351. return ff_filter_frame(outlink, out);
  352. }
  353. s->prev_frame = frame;
  354. return 0;
  355. }
  356. #define tlut2_options options
  357. AVFILTER_DEFINE_CLASS(tlut2);
  358. static const AVFilterPad tlut2_inputs[] = {
  359. {
  360. .name = "default",
  361. .type = AVMEDIA_TYPE_VIDEO,
  362. .filter_frame = tlut2_filter_frame,
  363. .config_props = config_inputx,
  364. },
  365. { NULL }
  366. };
  367. static const AVFilterPad tlut2_outputs[] = {
  368. {
  369. .name = "default",
  370. .type = AVMEDIA_TYPE_VIDEO,
  371. .config_props = config_output,
  372. },
  373. { NULL }
  374. };
  375. AVFilter ff_vf_tlut2 = {
  376. .name = "tlut2",
  377. .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two successive frames."),
  378. .priv_size = sizeof(LUT2Context),
  379. .priv_class = &tlut2_class,
  380. .query_formats = query_formats,
  381. .init = init,
  382. .uninit = uninit,
  383. .inputs = tlut2_inputs,
  384. .outputs = tlut2_outputs,
  385. };
  386. #endif