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.

465 lines
14KB

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