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.

380 lines
12KB

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